Esempio n. 1
0
    def clean(self):
        if tools.remove_networkd_file(self.__class__.__name__):
            tools.systemd_restart('systemd-networkd')
        self._terminate_wpasupplicant()
        tools.iface_down(self._status['ifname'])

        if self._wpacli_lock.acquire(block=False):
            self._wpacli_lock.release()
Esempio n. 2
0
 def clean(self):
     modem = self._get_modem()
     if modem is not None and 'wwan' in modem:
         try:
             self._at_modem.ndis_disconnect(modem['port_control'])
         except Exception:
             pass
         if tools.remove_networkd_file(self.__class__.__name__):
             tools.systemd_restart('systemd-networkd')
         os.system('/bin/ip link set dev %s down' % modem['wwan'])
     else:
         self._terminate_pppd()
Esempio n. 3
0
    def _loop(self, config, status):
        """Overloaded method: Main loop taking care of link connection.
        """
        log = logging.getLogger(__name__)
        self._status['status'] = 'NOT_CONNECTED'
        operstate = None

        while True:
            iface = tools.get_iface(config)

            if iface is not None:
                try:
                    self._status['ifname'] = iface.ifname
                except Exception as e:
                    log.error('Error: ' + str(e))
                    time.sleep(1)
                    continue

                if tools.gen_systemd_networkd(self.__class__.__name__, config['ipv4'], mac=iface.mac,
                                              metric=1024):
                    self._status['status'] = 'CONNECTING'
                    tools.systemd_restart('systemd-networkd')
                    log.info('Created systemd-networkd configuration for %s (%s)' % (iface.ifname, iface.mac))

                if tools.get_operstate(self._status['ifname']) == 'UP':
                    self._status['status'] = 'CONNECTED'

            else:
                self._status['error'] = 'NO_DEVICE_DETECTED'
                self._status['ifname'] = None

                if tools.remove_networkd_file(self.__class__.__name__):
                    tools.systemd_restart('systemd-networkd')
                    log.info('Removed systemd-networkd configuration')
                    self._ev_conn.set()  # update online status

            ops = tools.get_operstate(self._status['ifname'])
            if ops == 'UP' and operstate != 'UP':
                log.info('Link UP')
                self._ev_conn.set()
            if ops != 'UP' and operstate == 'UP':
                log.info('Link DOWN')
                self._ev_conn.set()

            operstate = ops
            time.sleep(5)
Esempio n. 4
0
    def _loop(self, config, status):
        """Overloaded method: Main loop taking care of link connection.
        """
        log = logging.getLogger(__name__)
        self._status['status'] = 'NOT_CONNECTED'
        self._modem = None
        pppdconn = None
        error_status = None

        while True:
            if self._status['status'] != 'CONNECTED':

                self._status['ifname'] = None
                modem = None
                try:
                    modem = self._get_modem()
                except Exception:
                    pass

                if modem is not None and self._at_modem.registered(modem['port_control']) is True:

                    # create neccessary files and connect
                    lte_cfg = config['lte']
                    self._status['status'] = 'CONNECTING'

                    if 'wwan' in modem:
                        error = None
                        connected = False
                        try:
                            self._at_modem.ndis_connect(lte_cfg['apn'], modem['port_control'])
                        except Exception as e:
                            error = 'Cannot connect: ' + str(e)

                        time.sleep(5)  # wait for connected
                        if error is None:
                            try:
                                connected = self._at_modem.ndis_connected(modem['port_control'])
                            except Exception as e:
                                error = 'Cannot get connection status: ' + str(e)

                        if connected:
                            if tools.gen_systemd_networkd(self.__class__.__name__, {'dhcp': True},
                                                          ifname=modem['wwan']):
                                os.system('/bin/ip link set dev %s up' % modem['wwan'])
                                tools.systemd_restart('systemd-networkd')
                                log.info('Created systemd-networkd configuration for %s' % modem['wwan'])


                            log.info('Link layer of LTE connected')
                            log.info('Network info: %s' % str(self._at_modem.network_info(modem['port_control'])))
                            log.info('Signal: %s' % str(self._at_modem.signal(modem['port_control'])))
                            self._status['status'] = 'CONNECTED'
                            self._status['error'] = None
                            self._ev_conn.set()  # update online status
                            self._status['ifname'] = modem['wwan']
                            #self._status['dns'] = pppdconn.dns()

                        else:
                            self._status['error'] = 'Cannot connect (NDIS)'

                    else:
                        # ppp connection
                        pppd_params = self._pppd_params(lte_cfg['apn'],
                                                        lte_cfg['number'],
                                                        lte_cfg['user'],
                                                        lte_cfg['password'])

                        pppd_params = shlex.split(pppd_params)

                        self._terminate_pppd()
                        time.sleep(1)
                        try:
                            pppdconn = PPPConnection(*pppd_params)
                        except Exception as e:
                            self._status['error'] = 'Cannot connect: ' + str(e)
                            if hasattr(e, 'output') and e.output is not None:
                                self._status['error'] += ' pppd output: ' + e.output[-500:]
                        else:
                            log.info('Link layer of LTE connected')
                            log.info('Network info: %s' % str(self._at_modem.network_info(modem['port_control'])))
                            log.info('Signal: %s' % str(self._at_modem.signal(modem['port_control'])))
                            self._status['status'] = 'CONNECTED'
                            self._status['error'] = None
                            self._ev_conn.set()  # update online status
                            self._status['ifname'] = 'ppp0'
                            self._status['dns'] = pppdconn.dns()

                else:
                    self._status['error'] = 'NO_DEVICE_DETECTED'

            if self._status['status'] == 'CONNECTED':

                if 'wwan' in modem:
                    try:
                        if not self._at_modem.ndis_connected(modem['port_control']):
                            self._status['error'] = 'Connection interrupted'
                            self._status['status'] = 'NOT_CONNECTED'
                    except Exception as e:
                        self._status['error'] = 'Connection interrupted: ' + str(e)
                        self._status['status'] = 'NOT_CONNECTED'
                else:
                    try:
                        ret = pppdconn.connected()
                    except Exception as e:
                        self._status['error'] = 'Connection interrupted: ' + str(e)
                        if hasattr(e, 'output') and e.output is not None:
                            self._status['error'] += ' pppd output: ' + e.output[-500:]
                        self._status['status'] = 'NOT_CONNECTED'
                    else:
                        self._status['error'] = None
                        if ret is False:
                            self._status['error'] = 'Connection interrupted'
                            self._status['status'] = 'NOT_CONNECTED'

            if error_status != self._status['error']:
                if self._status['error'] is not None:
                    log.info('Error: %s' % (self._status['error']))
                    self._ev_conn.set()  # update online status
                error_status = self._status['error']
                if modem is not None:
                    log.info('Network info: %s' % str(self._at_modem.network_info(modem['port_control'])))
                    log.info('Signal: %s' % str(self._at_modem.signal(modem['port_control'])))

            time.sleep(10)
Esempio n. 5
0
    def _loop(self, config, status):
        """Overloaded method: Main loop taking care of link connection.
        """
        log = logging.getLogger(__name__)
        self._status['status'] = 'NOT_CONNECTED'
        proc = None
        link_status = {}
        error_status = None

        while True:
            iface = tools.get_iface(config)

            if iface is not None:
                self._status['ifname'] = iface.ifname

                if tools.gen_systemd_networkd(self.__class__.__name__,
                                              config['ipv4'],
                                              mac=iface.mac,
                                              metric=512):
                    self._status['status'] = 'CONNECTING'
                    tools.systemd_restart('systemd-networkd')
                    log.info(
                        'Created systemd-networkd configuration for %s (%s)' %
                        (iface.ifname, iface.mac))

                if self._wpasupplicant_conf(config['wifi_client']):
                    self._status['status'] = 'CONNECTING'
                    log.info('Created wpa_supplicant configuration: %s' %
                             (WPASUPPLICANT_CONF))

                # wpasupplicant up
                if proc is None:
                    self._status['status'] = 'CONNECTING'
                    log.info("Starting wpa_supplicant")
                    self._terminate_wpasupplicant()
                    wpasupplicant_cmd = WPASUPPLICANT_CMD % (
                        iface.ifname, WPASUPPLICANT_CONF)
                    proc = Popen(shlex.split(wpasupplicant_cmd),
                                 stdin=DEVNULL,
                                 stderr=DEVNULL,
                                 stdout=DEVNULL)

                status = self._wifi_status()
                if status is not None and status['status'] == 'COMPLETED':
                    self._status['status'] = 'CONNECTED'

                self._status['error'] = None

            else:
                self._status['error'] = 'NO_DEVICE_DETECTED'
                self._status['ifname'] = None

                if tools.remove_networkd_file(self.__class__.__name__):
                    tools.systemd_restart('systemd-networkd')
                    log.info('Removed systemd-networkd configuration')
                    self._ev_conn.set()

                if proc is not None:
                    log.info('Terminating wpasupplicant process')
                    proc.terminate()
                    proc.wait()
                    proc = None

            if proc is not None and proc.poll() is not None:
                # wpasupplicant interrupted
                self._status['status'] = 'NOT_CONNECTED'
                proc = None

            # log important status changes
            ret = self._wifi_status()
            if ret is not None and link_status != ret:

                if ret['status'] != link_status.get('status', None):
                    log.info('Status: %s <%s> (%s dbm)' %
                             (ret['status'], ret['ssid'], ret['rssi']))

                # update online status when status change from/to COMPLETED - wifi is connected to AP
                if ret['status'] == 'COMPLETED' and link_status.get(
                        'status', None) != 'COMPLETED':
                    self._ev_conn.set()
                if ret['status'] != 'COMPLETED' and link_status.get(
                        'status', None) == 'COMPLETED':
                    self._ev_conn.set()

                link_status = ret

            if error_status != self._status['error']:
                if self._status['error'] is not None:
                    log.info('Error: %s' % (self._status['error']))
                error_status = self._status['error']

            time.sleep(5)
Esempio n. 6
0
 def clean(self):
     if tools.remove_networkd_file(self.__class__.__name__):
         tools.systemd_restart('systemd-networkd')
     tools.iface_down(self._status['ifname'])
Esempio n. 7
0
    def _loop(self, config, status):
        """Overloaded method: Main loop taking care of link connection.
        """
        log = logging.getLogger(__name__)
        self._status['status'] = 'NOT_CONNECTED'
        proc = None

        while True:
            if self._status['status'] != 'CONNECTED':
                iface = tools.get_iface(config)

            if iface is not None:
                self._status['ifname'] = iface.ifname

                if tools.gen_systemd_networkd(self.__class__.__name__,
                                              config['ipv4'],
                                              mac=iface.mac,
                                              dhcp_server=True):
                    self._status['status'] = 'CONNECTING'
                    tools.systemd_restart('systemd-networkd')
                    log.info(
                        'Created systemd-networkd configuration for %s (%s)' %
                        (iface.ifname, iface.mac))

                if self._hostapd_conf(iface.ifname, config['wifi_ap']):
                    self._status['status'] = 'CONNECTING'
                    log.info('Created hostapd configuration: %s' %
                             (HOSTAPD_CONF))

                if proc is None:
                    self._status['status'] = 'CONNECTING'
                    self._terminate_hostapd()
                    hostapd_cmd = HOSTAPD_CMD % (HOSTAPD_CONF)
                    proc = Popen(shlex.split(hostapd_cmd),
                                 stdin=DEVNULL,
                                 stderr=DEVNULL,
                                 stdout=DEVNULL)

                time.sleep(1)
                if proc.poll() is None:
                    self._status['status'] = 'CONNECTED'

            else:
                self._status['error'] = 'NO_DEVICE_DETECTED'
                self._status['ifname'] = None

                if tools.remove_networkd_file(self.__class__.__name__):
                    tools.systemd_restart('systemd-networkd')
                    log.info('Removed systemd-networkd configuration')

                if proc is not None:
                    log.info('Terminating wpasupplicant process')
                    proc.terminate()
                    proc.wait()
                    proc = None

            if proc is not None and proc.poll() is not None:
                # wpasupplicant interrupted
                self._status['status'] = 'NOT_CONNECTED'
                proc = None

            time.sleep(5)