Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # StdLib
- import operator
- import signal
- # import cupsext
- # Local
- from base.g import *
- from base import device, models, wifi, LedmWifi
- from base.codes import *
- from base.sixext import to_unicode
- PAGE_INTRO = 0 # Ask user to plugin temp USB connection
- PAGE_DEVICES = 1 # Select a probed USB device
- PAGE_NETWORK = 2 # Select a discovered SSID
- PAGE_CONFIGURE_WIFI = 3 # Configure USB device on network
- PAGE_EXIT = 4 # Tell user if successful, unplug USB onnection
- BUTTON_NEXT = 0
- BUTTON_FINISH = 1
- BUTTON_CONNECT = 3
- SUCCESS_NOT_CONNECTED = 0
- SUCCESS_AUTO_IP = 1
- SUCCESS_CONNECTED = 2
- ASSOCIATE_DELAY = 30
- REFRESH_INTERVAL = 20
- class WifiCmd():
- def __init__(self, device_uri=None):
- self.device_uri = device_uri
- self.device_uri = None
- self.devices = {}
- self.networks = {}
- self.ssid = ''
- self.directed = False
- self.show_extended = False
- self.bus = 'usb'
- self.search = ''
- self.max_page = PAGE_EXIT
- self.location_cache = {} # { 'bssid' : <location>, ... }
- self.dev = None
- self.success = SUCCESS_NOT_CONNECTED
- self.ip = '0.0.0.0'
- self.hn = ''
- self.adapterName = 'Wifi0'
- self.wifiObj = wifi
- if not self.devices:
- num_dev = 0
- self.device_infos = {}
- log.info("Searching on USB bus...")
- filter_dict = {'wifi-config' : (operator.gt, WIFI_CONFIG_NONE)}
- self.devices = device.probeDevices([self.bus], 0, 0, filter_dict, self.search)
- if self.devices:
- if len(self.devices) == 1:
- print("wireless capable device found.\n")
- for row, d in enumerate(self.devices):
- self.device_infos[num_dev] = d
- num_dev = num_dev + 1
- back_end, is_hp, bus, model, serial, dev_file, host, zc, port = device.parseDeviceURI(d)
- model_ui = models.normalizeModelUIName(model)
- print("%d) %s => %s\n" % (num_dev, str(model_ui), d))
- num_dev = 0
- if len(self.devices) > 1:
- text_input = ("%s wireless capable devices found. Select the device to install to continue.\n" % len(self.devices))
- num_dev = int(input(text_input)) - 1 # Python 3
- self.device_uri = self.device_infos[num_dev]
- self.mq = device.queryModelByURI(self.device_uri)
- self.getWifiObject(self.mq['wifi-config'])
- back_end, is_hp, bus, model, serial, dev_file, host, zc, port = device.parseDeviceURI(self.device_uri)
- self.model = models.normalizeModelName(model).lower()
- self.showNetworkPage()
- def showNetworkPage(self):
- if self.dev is None:
- try:
- self.dev = device.Device(self.device_uri)
- except Error as e:
- print("Error opening device: %s (%s)" % (self.device_uri, str(e.msg)))
- if self.dev is not None:
- self.dev.close()
- self.close()
- return
- self.networks.clear()
- self.num_networks = 0
- try:
- adaptor_list = self.wifiObj.getWifiAdaptorID(self.dev)
- except Error as e:
- self.showIOError(e)
- return
- if len(adaptor_list) == 0:
- print("Unable to locate wireless hardware on device.")
- if self.dev is not None:
- self.dev.close()
- self.close()
- print("Turning on wireless radio...")
- try:
- self.adaptor_id, self.adapterName, state, presence = self.wifiObj.setAdaptorPower(self.dev, adaptor_list )
- except Error as e:
- self.showIOError(e)
- return
- if self.adaptor_id == -1:
- print("Unable to turn on wireless adaptor.")
- if self.dev is not None:
- self.dev.close()
- print("Adaptor ID: %s" % self.adaptor_id)
- print("Adaptor name: %s" % self.adapterName)
- print("Adaptor state: %s" % state)
- print("Adaptor presence: %s" % presence)
- self.performScan()
- def performScan(self):
- # self.ssid = to_unicode(self.SSIDLineEdit.text())
- if self.directed and self.ssid:
- try:
- self.networks = self.wifiObj.performScan(self.dev, self.adapterName, self.ssid)
- except Error as e:
- self.showIOError(e)
- return
- else:
- try:
- self.networks = self.wifiObj.performScan(self.dev, self.adapterName)
- except Error as e:
- self.showIOError(e)
- return
- self.dev.close()
- self.num_networks = self.networks['numberofscanentries']
- if self.num_networks:
- if self.num_networks == 1:
- print("1 wireless network found.\n")
- else:
- print("%d wireless networks found.\n" % self.num_networks)
- self.loadNetworksTable()
- else:
- log.warning("No wireless networks found.")
- def loadNetworksTable(self):
- self.n, self.network = 0, to_unicode('')
- if self.num_networks:
- if self.show_extended:
- for n in range(self.num_networks):
- bssid = self.networks['bssid-%d' % n]
- ss = self.networks['signalstrength-%d' % n]
- try:
- self.location_cache[bssid]
- except KeyError:
- location = wifi.getLocation(bssid, ss)
- lat = self.networks['latitude-%d' % n] = location.get('latitude', 'Unknown')
- lng = self.networks['longitude-%d' % n] = location.get('longitude', 'Unknown')
- address = self.networks['address-%d' % n] = location.get('address', 'Unknown')
- self.location_cache[bssid] = (lat, lng, address)
- else:
- self.networks['latitude-%d' % n], self.networks['longitude-%d' % n], self.networks['address-%d' % n] = \
- self.location_cache[bssid]
- for n in range(self.num_networks):
- name = self.networks['ssid-%d' % n]
- ss = self.networks['signalstrength-%d' % n]
- sec = self.networks['encryptiontype-%d' % n]
- mode = self.networks['communicationmode-%d' % n]
- print("%d: Name=%s, strength=%s, security=%s, mode=%s" % #, channel=%d bssid=%s" %
- ((n + 1), name, ss, sec, mode))
- if self.show_extended:
- chn = self.networks['channel-%d' % n]
- dbm = self.networks['dbm-%d' % n]
- bssid = self.networks['bssid-%d' % n]
- address = self.networks['address-%d' % n]
- lat = self.networks['latitude-%d' % n]
- lng = self.networks['longitude-%d' % n]
- print("%d: channel=%d bssid=%s dbm=%s lat=%s long=%s address=%s" %
- ((n + 1), chn, bssid, dbm, lat, lng, address))
- self.current_network = 0
- if self.num_networks > 1:
- text_input = "\nSelect the wifi you want to connect to the printer: "
- self.current_network = int(input(text_input)) - 1 # Python 3
- self.select_wifi()
- def associate(self, key=to_unicode('')):
- try:
- alg, mode, secretid = self.wifiObj.getCryptoSuite(self.dev, self.adapterName)
- except Error as e:
- self.showIOError(e)
- return
- log.debug("Crypto algorithm: %s" % alg)
- log.debug("Crypto mode: %s" % mode)
- try:
- self.wifiObj.associate(self.dev, self.adapterName, self.network, self.mode, self.security, key)
- except Error as e:
- self.showIOError(e)
- return
- def select_wifi(self):
- self.security = 'None'
- self.mode = 'Infrastructure'
- self.ss = 0
- row = self.current_network
- if row != -1:
- self.network = to_unicode(self.networks['ssid-%d' % row])
- print("\tSelected network SSID: %s" % self.network)
- self.security = self.networks['encryptiontype-%d' % row]
- print("\tSecurity: %s" % self.security)
- self.mode = self.networks['communicationmode-%d' % self.n]
- print("\tMode: %s" % self.mode)
- self.ss = self.networks['signalstrength-%d' % self.n]
- print("\tSignal strength: %s" % self.ss)
- if self.security.lower() != 'none':
- text_input = input("Enter the wifi key: ")
- key = to_unicode(text_input)
- self.associate(key)
- else:
- # Skip config page if no security to setup
- self.associate()
- # The Wifi object here is not actual object, Dynamically relevant modules are selected based on
- # wifi-config value in the models file.
- def getWifiObject(self,wifiConfVal):
- if wifiConfVal == WIFI_CONFIG_LEDM:
- self.wifiObj = LedmWifi
- else:
- self.wifiObj = wifi
- # WifiCmd()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement