def initiate_ecm(self): logger.info("Checking the ECM initialization...") output = send_at_com(self.pdp_status_command, "OK") if output[2] == 0: if (output[0].find("0,1") != -1 or output[0].find("1,1") != -1): logger.info("ECM is already initiated.") time.sleep(10) return 0 logger.info("ECM Connection is initiating...") output = send_at_com(self.pdp_activate_command, "OK") if output[2] == 0: for i in range(60): output = send_at_com(self.pdp_status_command, "OK") if output[2] == 0: if (output[0].find("0,1") != -1 or output[0].find("1,1") != -1): logger.info("ECM is initiated.") time.sleep(10) return 0 else: time.sleep(1) else: time.sleep(1) raise PDPContextFailed("ECM initiation timeout!") else: raise PDPContextFailed("ECM initiation failed!")
def check_network(self): sim_ready = 0 network_reg = 0 network_ready = 0 # Check the network is ready logger.info("Checking the network is ready...") # SIM output = send_at_com("AT+CPIN?", "CPIN: READY") if output[2] == 0: logger.info("SIM is ready.") sim_ready = 1 else: logger.error(output[0]) raise SIMNotReady(output[0]) # Network Registeration output = send_at_com("AT+CREG?", "OK") if (output[2] == 0): if (output[0].find("+CREG: 0,1") != -1 or output[0].find("+CREG: 0,5") != -1): logger.info("Network is registered.") network_reg = 1 else: logger.error(output[0]) raise NetworkRegFailed(output[0]) else: logger.error(output[0]) raise NetworkRegFailed(output[0])
def set_modem_eps_data_centric(self): output = send_at_com(self.eps_mode_status_command, self.eps_data_centric_response) if output[2] == 0: logger.info("Modem mode for EPS is OK") else: output = send_at_com(self.eps_mode_setter_command, "OK") if output[2] == 0: logger.info("Modem mode for EPS updated succesfully") else: raise ModemNotReachable("Modem mode for EPS couldn't be set successfully!")
def configure_apn(self): apn_with_quotes = '"%s"' % conf.apn output = send_at_com("AT+CGDCONT?", apn_with_quotes) if output[2] == 0: logger.info("APN is up-to-date.") else: output = send_at_com(f'AT+CGDCONT=1,"IPV4V6","{conf.apn}"', "OK") if output[2] == 0: logger.info("APN is updated succesfully : %s", conf.apn) else: raise ModemNotReachable("APN couldn't be set successfully!")
def configure_apn(self): apn_with_quotes = '\"%s\"' % APN output = send_at_com("AT+CGDCONT?", apn_with_quotes) if output[2] == 0: logger.info("APN is up-to-date.") else: output = send_at_com("AT+CGDCONT=1,\"IPV4V6\",\"" + APN + "\"", "OK") if (output[2] == 0): logger.info("APN is updated succesfully : " + APN) else: raise ModemNotReachable("APN couldn't be set successfully!")
def wait_until_modem_started(self): result = 0 counter = 0 # Check modem USB interface for _ in range(120): output = shell_command("lsusb") if output[0].find(self.vid) != -1: logger.debug("Modem USB interface detected.") counter = 0 result += 1 break else: time.sleep(1) counter += 1 # Check modem AT FW for _ in range(10): output = send_at_com("AT", "OK") if output[2] == 0: logger.debug("Modem AT FW is working.") counter = 0 result += 1 break else: time.sleep(1) counter += 1 if result != 2: raise ModemNotFound("Modem couldn't be started!")
def get_roaming_operator(self): output = send_at_com("AT+COPS?", "OK") if output[2] == 0: data = self.get_significant_data(output, "+COPS:") return data[2] else: raise RuntimeError("Error occured on \"AT+CSQ\" command!")
def get_signal_quality(self): output = send_at_com("AT+CSQ", "OK") if output[2] == 0: data = self.get_significant_data(output, "+CSQ:") return int(data[0]) else: raise RuntimeError("Error occured on \"AT+CSQ\" command!")
def read_geoloc_data(self): """ Reads required data from modem in order to use at geolocation API """ logger.info("Getting raw geolocation data...") radio_type_id = 2 output = send_at_com('AT+QENG="servingcell"', "OK") if output[2] == 0: data = output[0].split(",") radio_type = data[radio_type_id].replace('"', '').casefold() try: for key in self.serving_cell_response_map: if key.find(radio_type) != -1: temp = self.serving_cell_response_map.get(key, {}) for key in temp: self.geolocation[key] = data[temp[key]].replace( '"', '').casefold() except: raise ValueError("Geolocation data is broken") else: raise RuntimeError(output[0]) # str/hex/int conversation try: for key in self.geolocation: if key in ["tac", "lac", "psc", "cid"]: self.geolocation[key] = int(self.geolocation[key], 16) elif key in ["mcc", "mnc"]: self.geolocation[key] = int(self.geolocation[key]) except: raise ValueError( "read_geoloc_data --> error occured converting hex to int")
def _turn_off_echo(): output = send_at_com("ATE0", "OK") if output[2] == 0: pass else: raise ModemNotReachable("Error occured turning of AT echo : send_at_com -> ATE0")
def get_active_lte_tech(self): techs = { "0": "GSM", "2": "UTRAN", "3": "GSM W/EGPRS", "4": "UTRAN W/HSDPA", "5": "UTRAN W/HSUPA", "6": "UTRAN W/HSDPA and HSUPA", "7": "E-UTRAN", "8": "CAT-M1", "9": "CAT-NB1", } output = send_at_com("AT+COPS?", "OK") if output[2] == 0: data = self.get_significant_data(output, "+COPS:") try: tech_id = data[3] except: return None else: return techs.get(tech_id, "Unknown") else: raise RuntimeError('Error occured on --> get_active_lte_tech')
def get_signal_quality(self): sq_lables = { "poor": range(0, 7), "fair": range(7, 12), "good": range(12, 20), "excellent": range(20, 33) } output = send_at_com("AT+CSQ", "OK") if output[2] == 0: data = self.get_significant_data(output, "+CSQ:") try: signal_quality = int(data[0]) except: signal_quality = None else: for key, value in sq_lables.items(): if signal_quality in value: signal_quality = key break else: signal_quality = "unknown" return signal_quality else: raise RuntimeError('Error occured on --> get_signal_quality')
def diag_sim_ready(self): logger.debug("[-] : Is the SIM ready?") output = send_at_com("AT+CPIN?", "READY") if output[2] == 0: self.diagnostic["sim_ready"] = True else: self.diagnostic["sim_ready"] = False
def diag_modem_reachable(self): logger.debug("[-] : Is modem reachable?") output = send_at_com("AT", "OK") if output[2] == 0: self.diagnostic["modem_reachable"] = True else: self.diagnostic["modem_reachable"] = False
def _identify_vendor_name(method=0): system_id["modem_vendor"] = "" step = 1 if method == 0 else method if method == 0 or step == 1: # [METHOD 1] By using lsusb output = shell_command("lsusb") if output[2] == 0: for vendor in ModemSupport.vendors: if output[0].find(vendor.name) != -1: system_id["modem_vendor"] = vendor.name step = 0 # Identification is successfull method = 1 # Identification is successfull #logger.debug("Modem vendor is detected with method 1!") if system_id["modem_vendor"] == "": logger.warning("Modem vendor couldn't be found with method 1!") step = 2 # Try next method else: raise RuntimeError("Error occured on lsusb command!") if method == 0 or step == 2: # [METHOD 2] By using usb-devices output = shell_command("usb-devices") if output[2] == 0: for vendor in ModemSupport.vendors: if output[0].find(vendor.name) != -1: system_id["modem_vendor"] = vendor.name step = 0 # Identification is successfull method = 1 # Identification is successfull #logger.debug("Modem vendor is detected with method 2!") if system_id["modem_vendor"] == "": logger.warning("Modem vendor couldn't be found with method 2!") step = 3 # Try next method else: raise RuntimeError("Error occured on usb-devices command!") if method == 0 or step == 3: # [METHOD 3] By using AT+GMI output = send_at_com("AT+GMI", "OK") if output[2] == 0: for vendor in ModemSupport.vendors: if output[0].find(vendor.name) != -1: system_id["modem_vendor"] = vendor.name if system_id["modem_vendor"] == "": logger.warning("Modem vendor couldn't be found with method 3!") else: logger.warning("Modem vendor couldn't be found with method 3!") raise RuntimeError( "Error occured on send_at_com --> AT+GMI command!") if system_id["modem_vendor"] == "": raise ModemNotSupported("Modem vendor couldn't be found!") else: return system_id["modem_vendor"]
def _identify_fw_version(): output = send_at_com("AT+CGMR", "OK") system_id["sw_version"] = output[0].split( "\n")[1] if output[2] == 0 else "" if system_id["sw_version"] != "": return system_id["sw_version"] else: raise ModemNotReachable("Firmware Ver. couldn't be detected!")
def enable_auto_network_registeration(self, register_delay=20): """ Enable network auto-registering """ output = send_at_com("AT+COPS?", "OK") if output[2] == 0: if output[0].find("+COPS: 0") != -1: logger.info("Network auto-registering is already enabled") else: logger.info("Network auto-registering is enabling") output = send_at_com("AT+COPS=0", "OK") if output[2] == 0: logger.info("Modem network auto-registering is enabled") time.sleep(register_delay) else: raise RuntimeError("Network auto-registering is failed!") else: raise RuntimeError("Network auto-registering check is failed!")
def diag_apn_set(self): logger.debug("[-] : Is the APN is OK?") apn_with_quotes = '"%s"' % conf.apn output = send_at_com("AT+CGDCONT?", apn_with_quotes) if output[2] == 0: self.diagnostic["modem_apn"] = True else: self.diagnostic["modem_apn"] = False
def diag_ecm_pdp_context(self): logger.debug("[-] : Is ECM PDP Context is active?") output = send_at_com(self.modem.pdp_status_command, self.modem.desired_pdp_status) if output[2] == 0: self.diagnostic["pdp_context"] = True else: self.diagnostic["pdp_context"] = False
def check_sim_ready(self): logger.info("Checking the SIM is ready...") output = send_at_com("AT+CPIN?", "CPIN: READY") if output[2] == 0: logger.info("SIM is ready.") else: logger.error(output[0]) raise SIMNotReady(output[0])
def diag_modem_mode(self): logger.debug("[-] : Is the modem mode OK?") output = send_at_com(self.modem.mode_status_command, self.modem.ecm_mode_response) if output[2] == 0: self.diagnostic["modem_mode"] = True else: self.diagnostic["modem_mode"] = False
def _identify_iccid(): output = send_at_com(identified_module.ccid_command, "OK") raw_iccid = output[0] if output[2] == 0 else "" if raw_iccid != "": iccid_filter = filter(str.isdigit, raw_iccid) system_id["iccid"] = "".join(iccid_filter) return system_id["iccid"] else: raise ModemNotReachable("ICCID couldn't be detected!")
def _identify_imei(): output = send_at_com("AT+CGSN", "OK") raw_imei = output[0] if output[2] == 0 else "" if raw_imei != "": imei_filter = filter(str.isdigit, raw_imei) system_id["imei"] = "".join(imei_filter) return system_id["imei"] else: raise ModemNotReachable("IMEI couldn't be detected!")
def deregister_network(self): """ Deregister from network and disable auto-registering """ output = send_at_com("AT+COPS=2", "OK") if output[2] == 0: logger.info("Modem is deregistered from network") else: raise RuntimeError("Network deregistering is failed!")
def check_network(self): logger.info("Checking the network is ready...") output = send_at_com("AT+CREG?", "OK") if output[2] == 0: if output[0].find("+CREG: 0,1") != -1 or output[0].find( "+CREG: 0,5") != -1: logger.info("Network is registered") else: raise NetworkRegFailed("Network not registered: ", output) else: raise NetworkRegFailed("Error occured sending AT+CREG?: ", output)
def reset_modem_softly(self): logger.info("Modem is resetting softly...") output = send_at_com(self.reboot_command, "OK") if output[2] == 0: try: self.wait_until_modem_turned_off() self.wait_until_modem_started() except Exception as error: raise error else: raise RuntimeError("Reboot command couldn't be reach to the modem!")
def _identify_product_name(): output = send_at_com("AT+GMM", "OK") if output[2] == 0: raw = output[0].split("\n") for _, value in enumerate(raw): if value != "": system_id["modem_name"] += " " + value break if system_id["modem_name"] == "": raise ModemNotSupported("Modem name couldn't be found!")
def get_signal_quality(self): output = send_at_com("AT+CSQ", "OK") if output[2] == 0: data = self.get_significant_data(output, "+CSQ:") try: signal_quality = int(data[0]) except: signal_quality = None else: return signal_quality else: raise RuntimeError('Error occured on --> get_signal_quality')
def check_network(self): logger.info("Checking the network is ready...") output = send_at_com("AT+CREG?", "OK") if output[2] == 0: if output[0].find("+CREG: 0,1") != -1 or output[0].find("+CREG: 0,5") != -1: logger.info("Network is registered.") else: logger.error(output[0]) raise NetworkRegFailed(output[0]) else: logger.error(output[0]) raise NetworkRegFailed(output[0])
def configure_modem(self): force_reset = 0 logger.info("Modem configuration started.") try: self.configure_apn() except Exception as e: raise e logger.info("Checking the mode of modem...") output = send_at_com(self.mode_status_command, self.ecm_mode_response) if output[2] != 0: logger.info( "Modem mode is not set. ECM mode will be activated soon.") output = send_at_com(self.ecm_mode_setter_command, "OK") if output[2] == 0: logger.info("ECM mode is activated.") logger.info("The modem will reboot to apply changes.") else: raise ModemNotReachable( "Error occured while setting mode configuration! " + output[0]) try: time.sleep(20) self.wait_until_modem_started() except Exception as e: logger.warning("wait_until_modem_started() -> " + str(e)) force_reset = 1 if force_reset == 1: force_reset = 0 try: self.reset_modem_softly() except Exception as e: raise e