def reset_modem_hardly(self): logger.info("Modem is resetting via hardware...") # Pin direction output = shell_command("gpio -g mode " + str(BASE_HAT_DISABLE_PIN) + " out") if output[2] == 0: pass else: raise RuntimeError("Error occured gpio command!") # Disable power output = shell_command("gpio -g write " + str(BASE_HAT_DISABLE_PIN) + " 1") if output[2] == 0: time.sleep(2) else: raise RuntimeError("Error occured gpio command!") # Enable power output = shell_command("gpio -g write " + str(BASE_HAT_DISABLE_PIN) + " 0") if output[2] == 0: time.sleep(2) else: raise RuntimeError("Error occured gpio command!")
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 find_active_interface(self): # Supported interfaces and locations interfaces = { "eth0": 10000, "wlan0": 10000, "usb0": 10000, "wwan0": 10000 } output = shell_command("route -n") if output[2] == 0: for key in interfaces: location = output[0].find(key) if location != -1: interfaces[key] = location else: raise RuntimeError("Error occured on \"route -n\" command!") # find interface has highest priority last_location = 10000 high = None for key in interfaces: if interfaces[key] < last_location: last_location = interfaces[key] high = key return high
def adjust_metric(self, interface, metric): output = shell_command(f"sudo ifmetric {interface} {metric}") if output[2] == 0: return 0 else: raise RuntimeError('Error occured on "route -n" command!')
def find_active_interface(self): interfaces = {} for ifs in self.interfaces: interfaces[ifs.name] = 10000 output = shell_command("route -n") if output[2] == 0: for key in interfaces: location = output[0].find(key) if location != -1: interfaces[key] = location else: raise RuntimeError('Error occured on "route -n" command!') # find interface has highest priority last_location = 10000 high = None for key in interfaces: if interfaces[key] < last_location: last_location = interfaces[key] high = key return high
def check_interface_health(self, interface, timeout): output = shell_command(f"ping -q -c 1 -s 8 -w {timeout} -I {interface} 8.8.8.8") if output[2] == 0: pass else: raise NoInternet("No internet!")
def _identify_board(): output = shell_command("cat /sys/firmware/devicetree/base/model") if output[2] == 0: system_id["board"] = output[0] else: raise RuntimeError("Board couldn't be detected!")
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 wait_until_modem_started(self): result = 0 counter = 0 # Check modem USB interface for i in range(120): output = shell_command("lsusb") if output[0].find(self.vendor) != -1: print("") # debug logger.debug("Modem USB interface detected.") counter = 0 result += 1 break else: time.sleep(1) counter += 1 print(str(counter) + " + ", end="", flush=True) # debug # Check modem AT FW for i in range(10): output = send_at_com("AT", "OK") if output[2] == 0: print("") # debug logger.debug("Modem AT FW is working.") counter = 0 result += 1 break else: time.sleep(1) counter += 1 print(str(counter) + " * ", end="", flush=True) # debug # Check modem connection interface for i in range(20): output = shell_command("route -n") if output[0].find(self.interface_name) != -1: print("") # debug logger.info("Modem started.") counter = 0 result += 1 break else: time.sleep(1) counter += 1 print(str(counter) + " : ", end="", flush=True) # debug if result != 3: raise ModemNotFound("Modem couldn't be started!")
def identify_modem(): global identified_module # Get old system setup if it is exist old_system_id = {} if os.path.isfile(SYSTEM_PATH): try: old_system_id = read_yaml_all(SYSTEM_PATH) except Exception as error: logger.warning("Old system_id in system.yaml file couln't be read!") system_id["modem_vendor"] = "" system_id["modem_name"] = "" system_id["modem_vendor_id"] = "" system_id["modem_product_id"] = "" logger.info("Tyring to detect modem...") output = shell_command("lsusb") if output[2] == 0: for module in modules: if output[0].find(module.pid) != -1: system_id["modem_vendor"] = module.vendor_name system_id["modem_name"] = module.module_name system_id["modem_vendor_id"] = module.vid system_id["modem_product_id"] = module.pid identified_module = module return identified_module logger.warning("Modem don't exist in list of supported modems!") for module in modules: if output[0].find(module.vid) != -1: system_id["modem_vendor"] = module.vendor_name system_id["modem_name"] = default_modules.get(str(module.vid)).module_name system_id["modem_vendor_id"] = module.vid system_id["modem_product_id"] = default_modules.get(str(module.vid)).pid identified_module = default_modules.get(str(module.vid)) return identified_module logger.warning("Modem vendor couldn't be found!") # clear modem identification data system_id["modem_vendor"] = None system_id["modem_name"] = None system_id["modem_vendor_id"] = None system_id["modem_product_id"] = None system_id["iccid"] = None system_id["imei"] = None system_id["sw_version"] = None if old_system_id.get("modem_vendor") is not None: try: write_yaml_all(SYSTEM_PATH, system_id) except Exception as error: raise RuntimeError("Save ID's to file") from error raise RuntimeError("Modem vendor couldn't be found!") else: raise RuntimeError("lsusb command error!")
def detect_modem(self): output = shell_command("lsusb") if output[2] == 0: if output[0].find(self.vendor) != -1: return self.vendor else: raise ModemNotFound("Modem couldn't be detected!") else: raise ModemNotFound("Modem couldn't be detected!")
def check_interface_health(self, interface): health_check = f"ping -q -c 1 -s 8 -w {conf.other_ping_timeout} -I {interface} 8.8.8.8" output = shell_command(health_check) if output[2] == 0: pass else: raise NoInternet("No internet!")
def check_internet(self): output = shell_command("ping -q -c 1 -s 0 -w " + str(PING_TIMEOUT) + " -I " + self.interface_name + " 8.8.8.8") #print(output) if output[2] == 0: return 0 else: raise NoInternet("No internet!")
def diag_connection_interface(self): logger.debug("[-] : Is connection interface exist?") output = shell_command("route -n") if output[2] == 0: if output[0].find(self.modem.interface_name) != -1: self.diagnostic["con_interface"] = True else: self.diagnostic["con_interface"] = False else: raise RuntimeError("Error occured processing shell command!")
def diag_usb_interface(self): logger.debug("[-] : Is USB interface exist?") output = shell_command("lsusb") if output[2] == 0: if output[0].find(self.modem.vid) != -1: self.diagnostic["usb_interface"] = True else: self.diagnostic["usb_interface"] = False else: raise RuntimeError("Error occured processing shell command!")
def wait_until_modem_turned_off(self): counter = 0 for _ in range(20): output = shell_command("lsusb") if output[0].find(self.vid) != -1: time.sleep(1) counter += 1 else: logger.debug("Modem turned off.") counter = 0 return 0 raise RuntimeError("Modem didn't turn off as expected!")
def reset_usb_interface(self): logger.info("USB interface is reset...") output = shell_command("sudo python3 " + reset_usb_script) if output[2] != 0: raise RuntimeError("Message: ", output) else: try: self.wait_until_modem_interface_up() except Exception as e: raise e
def find_usable_interfaces(self): # Supported interfaces interfaces = ["eth0", "wlan0", "usb0", "wwan0"] usable_interafaces = [] output = shell_command("route -n") if output[2] == 0: for i in interfaces: if output[0].find(i) != -1: usable_interafaces.append(i) return usable_interafaces
def get_interface_metrics(self): output = shell_command("ip route list") if output[2] != 0: raise RuntimeError('Error occured on "ip route list" command!') for line in output[0].splitlines(): for ifs in self.interfaces: if ifs.name in line and "default" in line: try: metric = parse_output(line, "metric", " ") ifs.actual_metric = int(metric) except Exception as error: logger.warning("Interface metrics couldn't be read! %s", error)
def debug_routes(self): if conf.debug_mode and conf.verbose_mode: output = shell_command("route -n") if output[2] == 0: print("") print("*****************************************************************") print("[?] NETWORK MANAGER REPORT") print("---------------------------") print(output[0]) print("*****************************************************************") print("") else: raise RuntimeError('Error occured on "route -n" command!')
def diag_usb_driver(self): logger.debug("[-] : Is USB driver exist?") output = shell_command("usb-devices") if output[2] == 0: if output[0].find("cdc_ether") != -1: if output[0].count("cdc_ether") >= 2: self.diagnostic["usb_driver"] = True else: self.diagnostic["usb_driver"] = False else: self.diagnostic["usb_driver"] = False else: raise RuntimeError("Error occured processing shell command!")
def wait_until_modem_turned_off(self): counter = 0 for i in range(20): output = shell_command("lsusb") if output[0].find(self.vendor) != -1: time.sleep(1) counter += 1 print(str(counter) + " - ", end="", flush=True) # debug else: print("") # debug logger.debug("Modem turned off.") counter = 0 return 0 raise RuntimeError("Modem didn't turn off as expected!")
def reset_connection_interface(self): down = "sudo ifconfig " + str(self.interface_name) + " down" up = "sudo ifconfig " + str(self.interface_name) + " up" logger.info("Connection interface is reset...") output = shell_command(down) if output[2] == 0: logger.info("Interface " + str(self.interface_name) + " is down.") else: raise RuntimeError("Error occured while interface getting down!") time.sleep(5) output = shell_command(up) if output[2] == 0: logger.info("Interface " + str(self.interface_name) + " is up.") else: raise RuntimeError("Error occured while interface getting up!") try: self.wait_until_modem_interface_up() except Exception as e: raise e
def find_usable_interfaces(self): ifs = [] output = shell_command("ip route list") if output[2] != 0: raise RuntimeError('Error occured on "ip route list" command!') for line in output[0].splitlines(): try: dev = parse_output(line, "dev", " ") if dev not in ifs: ifs.append(dev) except Exception as error: raise RuntimeError("Interface dev couldn't be read!") from error return ifs
def wait_until_modem_interface_up(self): counter = 0 logger.debug("Interface Name: %s", self.interface_name) # Check modem connection interface for _ in range(20): output = shell_command("route -n") if output[0].find(self.interface_name) != -1: logger.info("Modem interface is detected.") counter = 0 break else: time.sleep(1) counter += 1 if counter != 0: raise ModemNotFound("Modem interface couln't be detected.")
def _identify_product_name(method=0): system_id["modem_name"] = "" step = 1 if method == 0 else method if method == 0 or step == 1: # [METHOD 1] By using usb-devices output = shell_command("usb-devices") if output[2] == 0: for vendor in ModemSupport.vendors: for key in vendor.modules: product_name = key.split("_")[0] if output[0].find(product_name) != -1: system_id["modem_name"] = str(product_name) step = 0 # Identification is successfull method = 1 # Identification is successfull #logger.debug("Modem name is detected with method 1!") if system_id["modem_name"] == "": logger.warning("Modem name couldn't be found with method 1!") step = 2 # Try next method else: raise RuntimeError("Error occured on usb-devices command!") if method == 0 or step == 2: # [METHOD 2] By using AT+GMM output = send_at_com("AT+GMM", "OK") if output[2] == 0: for vendor in ModemSupport.vendors: for key in vendor.modules: product_name = key.split("_")[0] if output[0].find(product_name) != -1: system_id["modem_name"] = str(product_name) #logger.debug("Modem name is detected with method 2!") if system_id["modem_name"] == "": logger.warning("Modem name couldn't be found with method 2!") else: logger.warning("Modem name couldn't be found with method 2!") raise RuntimeError( "Error occured on send_at_com --> AT+GMM command!") if system_id["modem_name"] == "": raise ModemNotSupported("Modem name couldn't be found!") else: return system_id["modem_name"]
def wait_until_modem_interface_up(self): counter = 0 logger.debug("Interface Name: " + str(self.interface_name)) # Check modem connection interface for i in range(20): output = shell_command("route -n") if output[0].find(self.interface_name) != -1: print("") # debug logger.info("Modem interface is detected.") counter = 0 break else: time.sleep(1) counter += 1 print(str(counter) + " : ", end="", flush=True) # debug if counter != 0: raise ModemNotFound("Modem interface couln't be detected.")
def get_interface_type(self): output = shell_command("lshw -C Network") if output[2] == 0: networks = output[0].split("*-network") for network in networks: for interface in self.interfaces: if network.find(interface.name) >= 0: if network.find("Ethernet interface") >= 0: if network.find("driver=cdc_ether") >= 0: interface.if_type=InterfaceTypes.CELLULAR self.modem.interface_name = interface.name else: interface.if_type=InterfaceTypes.ETHERNET elif network.find("Wireless interface") >= 0: interface.if_type=InterfaceTypes.WIFI else: logger.warning("Error occured on --> get_interface_type")
def get_interface_type(self): output = shell_command("lshw -C Network") if output[2] == 0: networks = output[0].split("*-network") for network in networks: for interface in self.interfaces: if network.find(interface.name) >= 0: if network.find("Ethernet interface") >= 0: if network.find("driver=cdc_ether") >= 0: interface.if_type = "C" # cellular modem.interface_name = interface.name else: interface.if_type = "E" # ethernet elif network.find("Wireless interface") >= 0: interface.if_type = "W" # wifi else: logger.warning("Error occured on --> get_interface_type")
def _identify_usb_vid_pid(): system_id["modem_vendor_id"] = "" system_id["modem_product_id"] = "" output = shell_command("usb-devices") if output[2] == 0: for vendor in ModemSupport.vendors: if output[0].find(vendor.vendor_id) != -1: system_id["modem_vendor_id"] = vendor.vendor_id for vendor in ModemSupport.vendors: for key in vendor.modules: if output[0].find(vendor.modules[key]) != -1: system_id["modem_product_id"] = str(vendor.modules[key]) if system_id["modem_vendor_id"] == "" or system_id[ "modem_product_id"] == "": raise ModemNotSupported("Modem is not supported!") else: return (system_id["modem_vendor_id"], system_id["modem_product_id"]) else: raise RuntimeError("Error occured on usb-devices command!")