def udp_echo(self, host, port, echo_delay, echo_wait, verbose=True): echo_host = '35.212.147.4' echo_port = '3030' listen_port = '3032' ser = self.myserial # Create a packet session in case there is not one self.create_packet_session() # Close socket if open rmutils.write(ser, 'AT#SL=1,0,' + listen_port + ',0', delay=1) rmutils.write(ser, 'AT#SH=1', delay=1) # Create UDP socket for sending and receiving mycmd = 'AT#SD=1,1,' + echo_port + ',"' + echo_host + '",0,' + listen_port + ',1,0,1' rmutils.write(ser, mycmd, delay=1) # Send our UDP packet udppacket = str( '{"delay":' + str(echo_delay * 1000) + ', "ip":' + self.my_ip + ',"port":' + listen_port + '}' + chr(26)) rmutils.write(ser, 'AT#SSEND=1', udppacket, delay=1) # Sending packets to socket aerisutils.print_log('Sent Echo command to remote UDP server') # Wait for data if echo_wait > 0: echo_wait = round(echo_wait + echo_delay) # Wait for data to come in; handle case where we go to sleep rmutils.wait_urc(ser, echo_wait, self.com_port, returnonreset=True, returnonvalue='APP RDY') # Try to read data rmutils.write(ser, 'AT#SRECV=1,1500,1', delay=1)
def udp_echo(self, host, port, echo_delay, echo_wait, verbose=True): ser = self.myserial echo_host = '35.212.147.4' port = '3030' write_sock = '0' # Use socket 0 for sending if self.udp_listen(port, 0, verbose=verbose): # Open listen port aerisutils.print_log('Listening on port: ' + port) else: return False # Open UDP socket to the host for sending echo command rmutils.write(ser, 'AT+QICLOSE=0', delay=1, verbose=verbose) # Make sure no sockets open mycmd = 'AT+QIOPEN=1,0,\"UDP\",\"' + echo_host + '\",' + port + ',0,1' rmutils.write( ser, mycmd, delay=1, verbose=verbose) # Create UDP socket connection as a client sostate = rmutils.write(ser, 'AT+QISTATE=1,0', verbose=verbose) # Check socket state if "UDP" not in sostate: # Try one more time with a delay if not connected sostate = rmutils.write(ser, 'AT+QISTATE=1,0', delay=1, verbose=verbose) # Check socket state # Send data udppacket = str('{"delay":' + str(echo_delay * 1000) + ', "ip":"' + self.my_ip + '","port":' + str(port) + '}') #udppacket = str('Echo test!') # print('UDP packet: ' + udppacket) mycmd = 'AT+QISEND=0,' + str(len(udppacket)) rmutils.write(ser, mycmd, udppacket, delay=0, verbose=verbose) # Write udp packet rmutils.write(ser, 'AT+QISEND=0,0', verbose=verbose) # Check how much data sent aerisutils.print_log('Sent echo command: ' + udppacket) if echo_wait == 0: # True indicates we sent the echo return True else: echo_wait = round(echo_wait + echo_delay) vals = rmutils.wait_urc( ser, echo_wait, self.com_port, returnonreset=True, returnonvalue='OK' ) # Wait up to X seconds to confirm data sent #print('Return: ' + str(vals)) vals = rmutils.wait_urc( ser, echo_wait, self.com_port, returnonreset=True, returnonvalue='+QIURC:' ) # Wait up to X seconds for UDP data to come in vals = super().parse_response(vals, '+QIURC:') print('Return: ' + str(vals)) if len(vals) > 2 and int(vals[2]) == len(udppacket): return True else: return False
def wait(ctx, timeout): """Wait for a urc \f """ rmutils.wait_urc(my_module.myserial, timeout, my_module.com_port, verbose=ctx.obj['verbose']) # Wait up to X seconds for urc
def lookup(self, host, verbose): ser = self.myserial self.create_packet_session() rmutils.write(ser, 'AT+QIDNSCFG=1') # Check DNS server mycmd = 'AT+QIDNSGIP=1,\"' + host + '\"' rmutils.write(ser, mycmd, timeout=0) # Write a dns lookup command rmutils.wait_urc( ser, 4, self.com_port ) # Wait up to 4 seconds for results to come back via urc
def mqtt_demo(self, project, region, registry, cacert, clientkey, algorithm, deviceid, verbose): ser = self.myserial self.configure_mqtt(ser, cacert) rmutils.write(ser, 'AT+QMTOPEN=0,"mqtt.googleapis.com",8883') vals = rmutils.wait_urc(ser, 10, self.com_port, returnonreset=True, returnonvalue='+QMTOPEN:') vals = super().parse_response(vals, '+QMTOPEN:') print('Network Status: ' + str(vals)) if vals[1] != '0': print('Failed to connect to MQTT Network') else: print('Successfully opened Network to MQTT Server') token = self.create_jwt(project, clientkey, algorithm) cmd = 'AT+QMTCONN=0,"projects/' + project + '/locations/' + region + '/registries/' + registry + '/devices/' + deviceid + '","unused","' + token + '"' rmutils.write(ser, cmd) vals = rmutils.wait_urc(ser, 10, self.com_port, returnonreset=True, returnonvalue='+QMTCONN:') vals = super().parse_response(vals, '+QMTCONN:') print('Connection Response: ' + str(vals)) if vals[2] != '0': print('Unable to establish Connection') else: print('Successfully Established MQTT Connection') rmutils.write( ser, 'AT+QMTSUB=0,1,"/devices/' + deviceid + '/config",1') vals = rmutils.wait_urc(ser, 5, self.com_port, returnonreset=True, returnonvalue='+QMTRECV:') vals = super().parse_response(vals, '+QMTRECV:') print('Received Message : ' + str(vals)) rmutils.write( ser, 'AT+QMTPUB=0,1,1,0,"/devices/' + deviceid + '/events"') rmutils.write(ser, 'helloserver' + chr(26)) vals = rmutils.wait_urc(ser, 5, self.com_port, returnonreset=True, returnonvalue='+QMTPUB:') vals = super().parse_response(vals, '+QMTPUB:') print('Message Publish Status : ' + str(vals)) rmutils.write(ser, 'AT+QMTDISC=0', delay=1) print('MQTT Connection Closed')
def udp_listen(self, listen_wait, verbose): ser = self.myserial read_sock = '1' # Use socket 1 for listen if self.create_packet_session(): aerisutils.print_log('Packet session active: ' + self.my_ip) else: return False # Open UDP socket for listen rmutils.write(ser, 'AT#SLUDP=1,1,3030', delay=1) # Starts listener rmutils.write(ser, 'AT#SS', delay=1) if listen_wait > 0: rmutils.wait_urc(ser, listen_wait, self.com_port, returnonreset=True) # Wait up to X seconds for UDP data to come in rmutils.write(ser, 'AT#SS', delay=1) return True
def fw_update(self): ser = self.myserial modem = XMODEM(self.getc, self.putc) # stream = open('/home/pi/share/fw/0bb_stg1_pkg1-0m_L56A0200_to_L58A0204.bin', 'rb') stream = open('/home/pi/share/fw/0bb_stg2_L56A0200_to_L58A0204.bin', 'rb') rmutils.write(ser, 'AT+UFWUPD=3') rmutils.wait_urc(ser, 20, self.com_port) modem.send(stream) stream.close() ser.flushOutput() rmutils.wait_urc(ser, 20, self.com_port) # print(stream) rmutils.write(ser, 'AT+UFWINSTALL') rmutils.write(ser, 'AT+UFWINSTALL?')
def udp_echo(self, host, port, echo_delay, echo_wait, verbose=True): ser = self.myserial echo_host = host listen_port = port udp_socket = 0 # echo_host = '195.34.89.241' # ublox echo server # port = '7' # ublox echo server port # Make sure we have a packet session self.create_packet_session(verbose=verbose) # Close our read socket self.close_socket(udp_socket, verbose) # Create a UDP socket mycmd = 'AT+USOCR=17,' + str(listen_port) socket_id = (super().get_values_for_cmd(mycmd,'+USOCR:'))[0] #print('Socket ID = ' + str(socket_id)) # Send data udppacket = str( '{"delay":' + str(echo_delay * 1000) + ', "ip":"' + self.my_ip + '","port":' + str(listen_port) + '}') mycmd = 'AT+USOST=' + str(socket_id) + ',"' + echo_host + '",' + str(port) + ',' + str(len(udppacket)) rmutils.write(ser, mycmd, udppacket, delay=0, verbose=verbose) # Write udp packet aerisutils.print_log('Sent echo command: ' + udppacket, verbose) # Always wait long enough to verify packet sent vals = rmutils.wait_urc(ser, 5, self.com_port, returnonvalue='OK', verbose=verbose) #print('Return: ' + str(vals)) if echo_wait == 0: # True indicates we sent the echo return True else: # Wait for data echo_wait = round(echo_wait + echo_delay) # vals = rmutils.wait_urc(ser, echo_wait, self.com_port, returnonreset=True, # returnonvalue='APP RDY') # Wait up to X seconds for UDP data to come in vals = rmutils.wait_urc(ser, echo_wait, self.com_port, returnonreset=True, returnonvalue='+UUSORF:', verbose=verbose) #print('Return: ' + str(vals)) mycmd = 'AT+USORF=0,' + str(len(udppacket)) #vals = rmutils.write(ser, mycmd, verbose=verbose) # Read from socket vals = (super().get_values_for_cmd(mycmd,'+USORF:')) #print('Return: ' + str(vals)) if len(vals) > 3 and int(vals[3]) == len(udppacket): return True else: return False
def udp_listen(self, listen_port, listen_wait, verbose=True): ser = self.myserial udp_socket = 0 if self.create_packet_session(verbose=verbose): aerisutils.print_log('Packet session active: ' + self.my_ip) else: return False # Close our read socket self.close_socket(udp_socket, verbose) # Open UDP socket socket_id = (super().get_values_for_cmd('AT+USOCR=17','+USOCR:'))[0] print('Socket ID = ' + str(socket_id)) # Listen on udp socket port mycmd = 'AT+USOLI=' + str(socket_id) + ',' + str(listen_port) val = rmutils.write(ser, mycmd, verbose=verbose) # Wait for data up to X seconds if listen_wait > 0: rmutils.wait_urc(ser, listen_wait, self.com_port, returnonreset=True) return True
def udp_listen(self, listen_port, listen_wait, verbose=True, returnbytes=False): '''Starts listening for UDP packets. Parameters ---------- listen_port : int The port on which to listen. listen_wait : int Greater than zero if this method should wait for that many seconds for received packets. If less than or equal to zero, this method will return a boolean type. verbose : bool, optional returnbytes : bool, optional If True, returns bytes, instead of a string. Returns ------- s : bool False if a packet data session was not active, or if setting up the UDP socket failed. True if the modem successfully started listening for packets. m : str or bytes Any URCs that arrived while listening for packets. ''' ser = self.myserial read_sock = '1' # Use socket 1 for listen if self.create_packet_session(verbose=verbose): aerisutils.print_log('Packet session active: ' + self.my_ip) else: return False # Open UDP socket for listen mycmd = 'AT+QIOPEN=1,' + read_sock + ',"UDP SERVICE","127.0.0.1",0,' + str( listen_port) + ',1' rmutils.write(ser, mycmd, delay=1, verbose=verbose) # Create UDP socket connection sostate = rmutils.write(ser, 'AT+QISTATE=1,' + read_sock, verbose=verbose) # Check socket state if "UDP" not in sostate: # Try one more time with a delay if not connected sostate = rmutils.write(ser, 'AT+QISTATE=1,' + read_sock, delay=1, verbose=verbose) # Check socket state if "UDP" not in sostate: return False # Wait for data if listen_wait > 0: return rmutils.wait_urc( ser, listen_wait, self.com_port, returnonreset=True, returnbytes=returnbytes ) # Wait up to X seconds for UDP data to come in return True
def load_app(self, path, filename): ser = self.myserial #filename = 'oem_app_path.ini' #filename = 'program.bin' #path = '/home/pi/share/pio-bg96-1/.pio/build/bg96/' + filename stats = os.stat(path + filename) filesize = stats.st_size print('Size of file is ' + str(stats.st_size) + ' bytes') f = open(path + filename, 'rb') mycmd = 'AT+QFUPL="EUFS:/datatx/' + filename + '",' + str(filesize) rmutils.write(ser, mycmd) i = 0 while i < filesize: self.putc(f.read(1)) i += 1 f.close() rmutils.wait_urc( ser, 5, self.com_port ) # Wait up to 5 seconds for results to come back via urc return True
def file_upload(self, src_path, dst_path, filename): ser = self.myserial # Ensure source file exists stats = os.stat(src_path + filename) filesize = stats.st_size print('Size of file is ' + str(stats.st_size) + ' bytes') # Open source file for reading f = open(src_path + filename, 'rb') # Issue upload command to destination path #mycmd = 'AT+QFUPL="EUFS:/datatx/' + filename+ '",' + str(filesize) mycmd = 'AT+QFUPL="' + dst_path + filename + '",' + str(filesize) rmutils.write(ser, mycmd) i = 0 while i < filesize: self.putc(f.read(1)) i += 1 f.close() rmutils.wait_urc( ser, 5, self.com_port ) # Wait up to 5 seconds for results to come back via urc return True
def test(ctx, timeout, psmtau, psmat, delay): """Test PSM mode \f """ echo_host = '35.212.147.4' echo_port = 3030 echo_delay = delay echo_wait = 4 # Enable PSM my_module.enable_psm(psmtau, psmat, verbose=ctx.obj['verbose']) time.sleep(1.0) # Sleep to allow enable to complete # Make sure network allowed the configuration we asked for psm_settings = my_module.get_psm_info(ctx.obj['verbose']) if 'tau_network' not in psm_settings: exit() tau_network = int(psm_settings['tau_network']) if tau_network - psmtau > 120: my_module.disable_psm(verbose=ctx.obj['verbose']) aerisutils.print_log('Network settings not within tolerance.') return False aerisutils.print_log('Network tau: ' + str(tau_network)) # Get ready to do some timing start_time = time.time() elapsed_time = 0 aerisutils.print_log('Starting test for {0} seconds'.format(timeout)) while elapsed_time < timeout: #my_module.udp_echo(delay, 4, verbose=ctx.obj['verbose']) success = my_module.udp_echo(echo_host, echo_port, echo_delay, echo_wait, verbose=ctx.obj['verbose']) aerisutils.print_log('Success: ' + str(success)) rmutils.wait_urc(my_module.myserial, timeout, my_module.com_port, returnonreset=True, returnonvalue='APP RDY', verbose=ctx.obj['verbose']) # Wait up to X seconds for app rdy time.sleep(5.0) # Sleep in case it helps telit be able to connect my_module.init_serial(ctx.obj['comPort'], ctx.obj['apn'], verbose=ctx.obj['verbose']) rmutils.write(my_module.myserial, 'ATE0', verbose=ctx.obj['verbose']) # Turn off echo aerisutils.print_log('Connection state: ' + str(my_module.get_packet_info(verbose=ctx.obj['verbose']))) elapsed_time = time.time() - start_time # Do some cleanup tasks my_module.disable_psm(verbose=ctx.obj['verbose']) aerisutils.print_log('Finished test')
def http_get(self, host, port=80, verbose=True): ser = self.myserial self.create_packet_session() rmutils.write(ser, 'AT+CMEE=2', verbose=verbose) # Enable verbose errors # http profile commands works as <profile-id>, <opcode> rmutils.write(ser, 'AT+UHTTP=0', verbose=verbose) # Reset http profile #0 try: # Try to treat the host as an IP address; we will get ValueError if not network = ipaddress.IPv4Network(host) mycmd = 'AT+UHTTP=0,0,"' + host + '"' # Opcode = Set host by IP address mylookup = None except ValueError: mycmd = 'AT+UHTTP=0,1,"' + host + '"' # Opcode = Set host by dns name mylookup = 'AT+UDNSRN=0,"' + host + '"' # Perform lookup # set the server (either ip address or host) rmutils.write(ser, mycmd, verbose=verbose) if mylookup: # Do DNS lookup if we need one ipvals = super().get_values_for_cmd(mylookup, '+UDNSRN:') if len(ipvals) < 1 or ipvals[0] == '': return False # Set http port rmutils.write(ser, 'AT+UHTTP=0,5,' + str(port), verbose=verbose) # List files before the request rmutils.write(ser, 'AT+ULSTFILE=', verbose=verbose) # Make http get request; store in get.ffs file rmutils.write(ser, 'AT+UHTTPC=0,1,"/","get.ffs"', verbose=verbose) # Wait for response vals = rmutils.wait_urc(ser, 60, self.com_port, returnonreset=True, returnonvalue='+UUHTTPCR:', verbose=verbose) # List files after the request rmutils.write(ser, 'AT+ULSTFILE=', verbose=verbose) # Read the file 'get.ffs' mycmd = 'AT+URDFILE="get.ffs"' response = rmutils.write(ser, 'AT+URDFILE="get.ffs"', verbose=verbose) #vals = super().get_values_for_cmd(mycmd, '+URDFILE:') vals = self.parse_http_response(response, '+URDFILE:') if len(vals) < 3: response = False else: response = vals[2] if response == '""': response = False # Delete the file 'get.ffs' rmutils.write(ser, 'AT+UDELFILE="get.ffs"', verbose=verbose) return response
def stop_packet_session(self): ser = self.myserial rmutils.write(ser, 'AT#SGACT=1,0') # Deactivate context rmutils.wait_urc(ser, 2,self.com_port)
def lookup(self, host, verbose): ser = self.myserial self.create_packet_session() mycmd = 'AT#QDNS=\"' + host + '\"' rmutils.write(ser, mycmd) rmutils.wait_urc(ser, 2,self.com_port) # 4 seconds wait time
def ping(self, host, verbose): ser = self.myserial self.create_packet_session() mycmd = 'AT#PING=\"' + host + '\",3,100,300,200' rmutils.write(ser, mycmd, timeout=2) rmutils.wait_urc(ser, 10,self.com_port)
def wait_urc(self, timeout, returnonreset=False, returnonvalue=False, verbose=True): rmutils.wait_urc(self.myserial, timeout, self.com_port, returnonreset, returnonvalue, verbose=verbose) # Wait up to X seconds for URC