def run_wifi(allow_timeout='True'): try: client = WifiClient(allow_timeout != 'False') client.join() except: LOG.exception('Error running wifi client') trigger_event('ap_error')
def connect(self, ssid, password=None): LOG.info('Connecting to ' + ssid + '...') connected = self.is_connected(ssid) if connected: LOG.warning("Device is already connected to %s" % ssid) else: self.disconnect() LOG.info("Connecting to: %s" % ssid) nid = wpa(self.wiface, 'add_network') wpa(self.wiface, 'set_network', nid, 'ssid', '"' + ssid + '"') if password: psk = '"' + password + '"' wpa(self.wiface, 'set_network', nid, 'psk', psk) else: wpa(self.wiface, 'set_network', nid, 'key_mgmt', 'NONE') wpa(self.wiface, 'enable', nid) connected = self.get_connected(ssid) if connected: wpa(self.wiface, 'save_config') trigger_event( 'ap_connection_success' if connected else 'ap_connection_failed') self.notify_server('connection.status', {'connected': connected}) LOG.info("Connection status for %s = %s" % (ssid, connected))
def scan(self): trigger_event('ap_scan') LOG.info("Scanning wifi connections...") networks = {} status = self.get_connection_info() for cell in Cell.all(self.wiface): if "x00" in cell.ssid: continue # ignore hidden networks # Fix UTF-8 characters ssid = literal_eval("b'" + cell.ssid + "'").decode('utf8') quality = self.get_quality(cell.quality) # If there are duplicate network IDs (e.g. repeaters) only # report the strongest signal update = True if ssid in networks: update = networks[ssid]["quality"] < quality if update and ssid: networks[ssid] = { 'quality': quality, 'encrypted': cell.encrypted, 'connected': self.is_connected(ssid, status), 'demo': False } LOG.info("Found wifi networks: %s" % networks) self.notify_server('wifi.scanned', {'networks': networks})
def close(self): trigger_event('ap_down') self.running = False LOG.info('Shutting down access point...') self.ap.close() LOG.info('Sending shutdown signal...') if self.server: self.server.shutdown() LOG.info('Closing websocket...') self.client.close() LOG.info("Wifi client stopped!")
def monitor_connection(self): trigger_event('ap_up') has_connected = False num_failures = 0 start_time = time.time() self.running = True while self.running: # do our monitoring... mod_time = self.get_last_lease_mod() if self.last_lease_mod != mod_time: # Something changed in the dnsmasq lease file - # presumably a (re)new lease if not has_connected: trigger_event('ap_device_connected') has_connected = True num_failures = 0 self.last_lease_mod = mod_time start_time = time.time() # reset start time after connection if time.time() - start_time > 60 * 5 and self.allow_timeout: # After 5 minutes, shut down the access point (unless the # system has never been setup, in which case we stay up # indefinitely) LOG.info("Auto-shutdown of access point after 5 minutes") self.cancel() continue if has_connected: # Flush the ARP entries associated with our access point # This will require all network hardware to re-register # with the ARP tables if still present. if num_failures == 0: #cli_no_output cli('ip', '-s', '-s', 'neigh', 'flush', self.ap.subnet + '.0/24') # now look at the hardware that has responded, if no entry # shows up on our access point after 2*5=10 seconds, the user # has disconnected if not self.is_ARP_filled(): num_failures += 1 LOG.info('Lost connection: ' + str(num_failures)) if num_failures > 5: trigger_event('ap_device_disconnected') has_connected = False else: num_failures = 0 sleep(5) # wait a bit to prevent thread from hogging CPU
def cancel(self): trigger_event('ap_cancel') self.close()