def function(task): cmd = 'AT+CIMI' regex = '^(\+CIMI:\ +|)(?P<cimi>.+)' r_values = ["cimi"] for i in range(0, 3): res = self.io.com.send_query({ "type": "regex", "cmd": cmd, "task": task, "regex": regex, "r_values": r_values }) if res == None: return "" try: # Test it is a valid integer number int(res["cimi"]) # If the IMSI seems valid store it in a cache self.cache["imsi"] = res["cimi"] return self.cache["imsi"] except ValueError: warning("Got erroneous IMSI, trying again") error("Got erroneous IMSI for three times, desisting")
def __on_usb_event(self, client, action, device): if action == "add": vid = device.get_property("ID_VENDOR_ID") if vid in self.__devtable.vids: pid = device.get_property("ID_MODEL_ID") if pid not in self.__devtable.pids(vid): vendor = self.__devtable.vendor(vid) warning("New USB parent %s:%s | Vendor %s | Pending 'usb_modeswitch' rule?" % (vid, pid, vendor.name()))
def __close_serial_port(self): try: tcflush(self._fd, TCIOFLUSH) except: warning("Got error flushing port") try: os.close(self._fd) except Exception as err: error("Got error closing port: %s" % str(err))
def __on_usb_event(self, client, action, device): if action == "add": vid = device.get_property("ID_VENDOR_ID") if vid in self.__devtable.vids: pid = device.get_property("ID_MODEL_ID") if pid not in self.__devtable.pids(vid): vendor = self.__devtable.vendor(vid) warning( "New USB parent %s:%s | Vendor %s | Pending 'usb_modeswitch' rule?" % (vid, pid, vendor.name()))
def filters(self, commands=None): if (commands is not None): if not hasattr(commands, '__iter__'): commands = [ commands ] self.__task_filters = re.compile(r"%s" % '|'.join(map(lambda cmd: ".*%s.*" % cmd, commands))) self.__commands = commands if self.__filters_enabled is True: warning("IMPORTANT: Tasks filter(s) enabled: %s" % repr(commands)) return self.__commands
def AT_test(self, retries, timeout, write=None, read=None): retval = self.AT_TEST_ERROR write = "AT" if write is None else write read = "AT|OK" if read is None else read for retry in range(0, retries): try: self.open(flush=True) self.write(write + "\r") # -- Wait for the response from the port if self.expect(read, timeout) is True: retval = self.AT_TEST_OK #self.write("ATZ\r") except Exception, err: warning("Got error testing port %s, %s" % (self.__devnode, err)) finally:
def __init__(self): self.vids = [ ] self.__cfg = ConfigFile(os.path.join(mobilemanager.DEVICE_TABLE_DIR(), self.DEVICE_TABLE_XML)) try: self.__vendors = self.__cfg.read() info("Using XML devices table '%s'" % self.__cfg.fpath()) except Exception, err: warning("Error reading '%s', %s" % (self.__cfg.fpath(), err)) info("Using default vendors table") self.__vendors = { } self.__add( "Huawei" , "huawei" , HUAWEI_VID , [ ( '1003', 1, 0, None ) ] ) self.__add( "Novatel" , "novatel" , NOVATEL_VID , [ ] ) self.__add( "Sierra" , "sierra" , SIERRA_VID , [ ] ) self.__add( "ZTE" , "zte" , ZTE_VID , [ ] ) self.__add( "Option" , "option" , OPTION_VID , [ ] ) self.__add( "Alcatel" , "alcatel" , ALCATEL_VID , [ ] ) self.__save()
def expect(self, pattern, timeout, event=None): select_timeout = 0.010 loops = int(timeout / select_timeout) msg = "" while loops: ready = select.select([ self.__fd ], [ ], [ ], select_timeout) loops = loops - 1 if self.__fd in ready[0]: msg += os.read(self.__fd, 1) if re.search(pattern, msg) is not None: return True if (event is not None) and event.isSet(): return False if msg != "": warning("Got unexpected data '%s' testing '%s'" % (repr(msg), self.__devnode)) return False
def __init__(self): self.vids = [] self.__cfg = ConfigFile( os.path.join(mobilemanager.DEVICE_TABLE_DIR(), self.DEVICE_TABLE_XML)) try: self.__vendors = self.__cfg.read() info("Using XML devices table '%s'" % self.__cfg.fpath()) except Exception, err: warning("Error reading '%s', %s" % (self.__cfg.fpath(), err)) info("Using default vendors table") self.__vendors = {} self.__add("Huawei", "huawei", HUAWEI_VID, [('1003', 1, 0, None)]) self.__add("Novatel", "novatel", NOVATEL_VID, []) self.__add("Sierra", "sierra", SIERRA_VID, []) self.__add("ZTE", "zte", ZTE_VID, []) self.__add("Option", "option", OPTION_VID, []) self.__add("Alcatel", "alcatel", ALCATEL_VID, []) self.__save()
def function(task): cmd = "AT+CIMI" regex = "^(\+CIMI:\ +|)(?P<cimi>.+)" r_values = ["cimi"] for i in range(0, 3): res = self.io.com.send_query( {"type": "regex", "cmd": cmd, "task": task, "regex": regex, "r_values": r_values} ) if res == None: return "" try: # Test it is a valid integer number int(res["cimi"]) # If the IMSI seems valid store it in a cache self.cache["imsi"] = res["cimi"] return self.cache["imsi"] except ValueError: warning("Got erroneous IMSI, trying again") error("Got erroneous IMSI for three times, desisting")
def warning(self, msg): warning("%s: %s" % (self.__vid_pid, msg))
class DeviceManager(gobject.GObject): ''' DeviceManager Class ''' __metaclass__ = Singleton __gsignals__ = { 'device-added': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, )), 'device-removed': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, )), } # -- Internal device class for assuring that some resources (like the Gobject signals) are # -- freed after the modem was removed class _Device(): def __init__(self, parent, busname, mm_path, id, signals): self.__dev = DeviceDriverBuilder(parent, busname, mm_path, id) # -- Methods we need to link from the device driver class self.mm_device_path = self.__dev.mm_device_path self.remove = self.__dev.remove self.devpath = self.__dev.devpath self.remove = self.__dev.remove self.vid = self.__dev.vid self.pid = self.__dev.pid self.ports_nr = self.__dev.ports_nr # -- Connect to the Gobject signals of the device self.__signals = [] for [name, cb] in signals[:]: signal = self.__dev.connect(name, cb) self.__signals.append(signal) # -- Disconnect the already connected Gobject signals def disconnect(self): for signal in self.__signals[:]: self.__dev.disconnect(signal) self.__signals = [] def __init__(self, bus_name): gobject.GObject.__init__(self) self.bus_name = bus_name self.device_list = {} self.__devtable = DeviceTable.DeviceTable() self.udev_client = gudev.Client(["tty"]) self.udev_client_usb = gudev.Client(["usb/usb_device"]) thread.start_new_thread(self.__check_available_devices, ()) def enumerate_devices(self): ret = [] for dev in self.device_list.values(): ret.append(dev.mm_device_path()) return ret def __check_available_devices(self): info("Checking tty's availables") for device in self.udev_client.query_by_subsystem("tty"): self.__on_tty_event(self.udev_client, "add", device) info("Checking tty's availables finished") for device in self.udev_client_usb.query_by_subsystem("usb"): self.__on_usb_event(self.udev_client_usb, "add", device) self.udev_client.connect("uevent", self.__on_tty_event) self.udev_client_usb.connect("uevent", self.__on_usb_event) # -- This function checks for devices that are not handled by the MM as they are not # -- included in the devices table def __on_usb_event(self, client, action, device): if action == "add": vid = device.get_property("ID_VENDOR_ID") if vid in self.__devtable.vids: pid = device.get_property("ID_MODEL_ID") if pid not in self.__devtable.pids(vid): vendor = self.__devtable.vendor(vid) warning( "New USB parent %s:%s | Vendor %s | Pending 'usb_modeswitch' rule?" % (vid, pid, vendor.name())) # -- Wait for state changes of TTY ports def __on_tty_event(self, client, action, device): # -- By new TTY ports only create the object and store the corresponding ID if action == "add": _parent = parent_from_device(device) _id = create_parent_id(_parent) if _id is None: return if self.device_list.has_key(_id) is False: try: # -- @XXX: Correct place for defining the Gobject signals with the callbacks? DEVICE_SIGNALS = [ ["modem-ready", self.__modem_ready_cb], ["modem-removed", self.__modem_removed_cb], ["modem-failure", self.__modem_failure_cb], ["device-removed", self.__device_removed_cb], ] dev = self._Device(_parent, self.bus_name, MM_DEVICE_PATH, _id, DEVICE_SIGNALS) self.device_list[_id] = dev except Exception, err: error("Unexpected failure adding device, %s" % err) elif action == "remove": # -- @FIXME: As the parent could be already destroyed during this callback is # -- executed, we use another mechanism for obtaining the parent that corresponds # -- to this device found = False path = device.get_property("DEVPATH") for dev in self.device_list.values(): if path.startswith(dev.devpath() + "/"): # -- We must assure that the device is gone, in some cases it doesn't (why?) for bus in usb.busses(): for x in bus.devices: if x.idVendor == dev.vid( int) and x.idProduct == dev.pid(int): return dev.remove() warning("Removed USB parent %s:%s" % (dev.vid(), dev.pid())) found = True break if found is False: warning("Unknown remove event of device '%s'" % (path))
def warning(self, msg): warning(msg)