configure_cots_bandrich_ue.py 4.32 KB
Newer Older
1 2 3 4 5 6 7 8 9
import time
import serial
import os
from pyroute2 import IPRoute
import sys
import re
import threading
import signal
import traceback
10
import os
11 12

# configure the serial connections (the parameters differs on the device you are connecting to)
13 14
#First we find an open port to work with
serial_port=''
15 16 17
ser=serial.Serial()
def find_open_port():
   global serial_port, ser
18 19 20
   max_ports=100
   if os.path.exists(serial_port) == True:
     return serial_port
21
   for port in range(2,100):
22 23 24 25
      serial_port = '/dev/ttyUSB'+str(port)
      if os.path.exists(serial_port) == True:
         print 'New Serial Port : ' + serial_port
         break
26

27 28 29 30
   ser = serial.Serial(port=serial_port)
   return

find_open_port()
31 32 33
print 'Using Serial port : ' + serial_port  
    
#serial_port = '/dev/ttyUSB2'
34 35 36 37 38 39 40 41 42 43 44 45 46 47
bandrich_ppd_config = '$OPENAIR_DIR/cmake_targets/autotests/tools/wdial.bandrich.conf'

exit_flag=0

def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        print('Resetting the UE to detached state')
        timeout=10
        exit_flag=1
        send_command('AT+CGATT=0' , 'OK' , timeout)
        sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

#ser.open()
#ser.isOpen()

class pppThread (threading.Thread):
    def __init__(self, threadID, name, counter):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
    def run(self):
        print "Starting " + self.name
        #Here we keep running pppd thread in indefinite loop as this script terminates sometimes
        #while 1:
        while 1:
           time.sleep(5) #Hard coded, do not reduce this number! 
           print "Starting wvdial now..."
           print 'exit_flag = ' + str(exit_flag)
           send_command('AT+CGATT=1','OK', 300)
           os.system('wvdial -C ' + bandrich_ppd_config + '' )
           if exit_flag == 1:
              print "Exit flag set to true. Exiting pppThread now"
           print "Terminating wvdial now..."

def send_command (cmd, response, timeout):
   count=0
   sleep_duration = 1
76 77
   print 'In function: send_command: cmd ' + cmd + '> response: <' + response + '> \n'
   global serial_port, ser
78
   while count <= timeout:
Rohit Gupta's avatar
Rohit Gupta committed
79
      try:
80 81 82
        #Sometimes the port does not exist coz of reset in modem.
        #In that case, we need to search for this port again
        if os.path.exists(serial_port) == False:
83
            find_open_port()
Rohit Gupta's avatar
Rohit Gupta committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97
        ser.write (cmd + '\r\n')
        out = ''
        time.sleep(sleep_duration)
        count = count + sleep_duration
        while ser.inWaiting() > 0:
            out += ser.read(1)
        print 'out = <' + out + '> response = <' + response + '> \n'
        if re.search(response, out):
          break
      except Exception, e:
        error = ' cmd : ' + cmd + ' response : ' + response
        error = error + ' In function: ' + sys._getframe().f_code.co_name + ': *** Caught exception: '  + str(e.__class__) + " : " + str( e)
        error = error + traceback.format_exc()
        print error
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        

def start_ue () :
   #print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
   timeout=60 #timeout in seconds
   send_command('AT', 'OK' , timeout)
   send_command('AT+CGATT=0' , 'OK' , timeout)
   send_command('AT+CGATT=1','OK', 300)
   #os.system('wvdial -C ' + bandrich_ppd_config + ' &' )
   
   thread_ppp = pppThread(1, "ppp_thread", 1)
   thread_ppp.start()

   iface='ppp0'
   
   while 1:
     time.sleep ( 2)
     #Now we check if ppp0 interface is up and running
     try:
        if exit_flag == 1:
          break
        ip = IPRoute()
        idx = ip.link_lookup(ifname=iface)[0]
        os.system ('route add 192.172.0.1 ppp0')
        break
     except Exception, e:
        error = ' Interface ' + iface + 'does not exist...'
        error = error + ' In function: ' + sys._getframe().f_code.co_name + ': *** Caught exception: '  + str(e.__class__) + " : " + str( e)
        error = error + traceback.format_exc()
        print error
    
   thread_ppp.join()

def stop_ue():
   timeout=60
   os.system('killall wvdial')
   send_command('AT', 'OK' , timeout)
   send_command('AT+CGATT=0' , 'OK' , timeout)

for arg in sys.argv[1:]:
    if arg == '--start-ue' :
        start_ue()
    elif arg == '--stop-ue' :
        stop_ue()
    else :
        print " Script called with wrong arguments, arg = " + arg
        sys.exit()