Thierry70

Untitled

Sep 30th, 2020
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # StdLib
  2. import operator
  3. import signal
  4.  
  5. # import cupsext
  6.  
  7. # Local
  8. from base.g import *
  9. from base import device, models, wifi, LedmWifi
  10. from base.codes import *
  11. from base.sixext import  to_unicode
  12.  
  13. PAGE_INTRO = 0 # Ask user to plugin temp USB connection
  14. PAGE_DEVICES = 1 # Select a probed USB device
  15. PAGE_NETWORK = 2 # Select a discovered SSID
  16. PAGE_CONFIGURE_WIFI = 3 # Configure USB device on network
  17. PAGE_EXIT = 4 #  Tell user if successful, unplug USB onnection
  18.  
  19.  
  20. BUTTON_NEXT = 0
  21. BUTTON_FINISH = 1
  22. BUTTON_CONNECT = 3
  23.  
  24. SUCCESS_NOT_CONNECTED = 0
  25. SUCCESS_AUTO_IP = 1
  26. SUCCESS_CONNECTED = 2
  27.  
  28. ASSOCIATE_DELAY = 30
  29. REFRESH_INTERVAL = 20
  30.  
  31. class WifiCmd():
  32.     def __init__(self, device_uri=None):
  33.         self.device_uri = device_uri
  34.  
  35.         self.device_uri = None
  36.         self.devices = {}
  37.         self.networks = {}
  38.         self.ssid = ''
  39.         self.directed = False
  40.         self.show_extended = False
  41.         self.bus = 'usb'
  42.         self.search = ''
  43.         self.max_page = PAGE_EXIT
  44.         self.location_cache = {} # { 'bssid' : <location>, ... }
  45.         self.dev = None
  46.         self.success = SUCCESS_NOT_CONNECTED
  47.         self.ip = '0.0.0.0'
  48.         self.hn = ''
  49.         self.adapterName = 'Wifi0'
  50.         self.wifiObj = wifi
  51.  
  52.         if not self.devices:
  53.             num_dev = 0
  54.             self.device_infos = {}
  55.             log.info("Searching on USB bus...")
  56.             filter_dict = {'wifi-config' : (operator.gt, WIFI_CONFIG_NONE)}
  57.  
  58.             self.devices = device.probeDevices([self.bus], 0, 0, filter_dict, self.search)
  59.             if self.devices:
  60.                 if len(self.devices) == 1:
  61.                     print("wireless capable device found.\n")
  62.                 for row, d in enumerate(self.devices):
  63.                     self.device_infos[num_dev] = d
  64.                     num_dev = num_dev + 1
  65.                     back_end, is_hp, bus, model, serial, dev_file, host, zc, port = device.parseDeviceURI(d)
  66.                     model_ui = models.normalizeModelUIName(model)
  67.                     print("%d) %s => %s\n" % (num_dev, str(model_ui), d))
  68.  
  69.                 num_dev = 0
  70.                 if len(self.devices) > 1:
  71.                     text_input = ("%s wireless capable devices found. Select the device to install to continue.\n" % len(self.devices))
  72.                     num_dev = int(input(text_input)) - 1 # Python 3
  73.                 self.device_uri = self.device_infos[num_dev]
  74.                 self.mq = device.queryModelByURI(self.device_uri)
  75.  
  76.                 self.getWifiObject(self.mq['wifi-config'])
  77.                 back_end, is_hp, bus, model, serial, dev_file, host, zc, port = device.parseDeviceURI(self.device_uri)
  78.                 self.model = models.normalizeModelName(model).lower()
  79.                 self.showNetworkPage()
  80.  
  81.  
  82.     def showNetworkPage(self):
  83.         if self.dev is None:
  84.             try:
  85.                 self.dev = device.Device(self.device_uri)
  86.             except Error as e:
  87.                 print("Error opening device: %s (%s)" % (self.device_uri, str(e.msg)))
  88.  
  89.                 if self.dev is not None:
  90.                     self.dev.close()
  91.  
  92.                 self.close()
  93.                 return
  94.  
  95.         self.networks.clear()
  96.         self.num_networks = 0
  97.  
  98.         try:
  99.             adaptor_list = self.wifiObj.getWifiAdaptorID(self.dev)
  100.         except Error as e:
  101.             self.showIOError(e)
  102.             return
  103.  
  104.         if len(adaptor_list) == 0:
  105.             print("Unable to locate wireless hardware on device.")
  106.             if self.dev is not None:
  107.                 self.dev.close()
  108.  
  109.             self.close()
  110.  
  111.         print("Turning on wireless radio...")
  112.         try:
  113.             self.adaptor_id, self.adapterName, state, presence =  self.wifiObj.setAdaptorPower(self.dev, adaptor_list )
  114.         except Error as e:
  115.             self.showIOError(e)
  116.             return
  117.         if self.adaptor_id == -1:
  118.             print("Unable to turn on wireless adaptor.")
  119.             if self.dev is not None:
  120.                 self.dev.close()
  121.  
  122.         print("Adaptor ID: %s" % self.adaptor_id)
  123.         print("Adaptor name: %s" % self.adapterName)
  124.         print("Adaptor state: %s" % state)
  125.         print("Adaptor presence: %s" % presence)
  126.  
  127.         self.performScan()
  128.  
  129.     def performScan(self):
  130.         # self.ssid = to_unicode(self.SSIDLineEdit.text())
  131.         if self.directed and self.ssid:
  132.             try:
  133.                 self.networks = self.wifiObj.performScan(self.dev, self.adapterName, self.ssid)
  134.             except Error as e:
  135.                 self.showIOError(e)
  136.                 return
  137.         else:
  138.             try:
  139.                 self.networks = self.wifiObj.performScan(self.dev, self.adapterName)
  140.             except Error as e:
  141.                 self.showIOError(e)
  142.                 return
  143.         self.dev.close()
  144.         self.num_networks = self.networks['numberofscanentries']
  145.  
  146.         if self.num_networks:
  147.             if self.num_networks == 1:
  148.                 print("1 wireless network found.\n")
  149.             else:
  150.                 print("%d wireless networks found.\n" % self.num_networks)
  151.  
  152.             self.loadNetworksTable()
  153.  
  154.         else:
  155.             log.warning("No wireless networks found.")
  156.  
  157.     def loadNetworksTable(self):
  158.         self.n, self.network = 0, to_unicode('')
  159.         if self.num_networks:
  160.             if self.show_extended:
  161.                 for n in range(self.num_networks):
  162.                     bssid = self.networks['bssid-%d' % n]
  163.                     ss = self.networks['signalstrength-%d' % n]
  164.                     try:
  165.                         self.location_cache[bssid]
  166.                     except KeyError:
  167.                         location = wifi.getLocation(bssid, ss)
  168.                         lat = self.networks['latitude-%d' % n] = location.get('latitude', 'Unknown')
  169.                         lng  = self.networks['longitude-%d' % n] = location.get('longitude', 'Unknown')
  170.                         address = self.networks['address-%d' % n] = location.get('address', 'Unknown')
  171.                         self.location_cache[bssid] = (lat, lng, address)
  172.                     else:
  173.                         self.networks['latitude-%d' % n], self.networks['longitude-%d' % n], self.networks['address-%d' % n] = \
  174.                             self.location_cache[bssid]
  175.  
  176.             for n in range(self.num_networks):
  177.                 name = self.networks['ssid-%d' % n]
  178.  
  179.                 ss = self.networks['signalstrength-%d' % n]
  180.                 sec = self.networks['encryptiontype-%d' % n]
  181.                 mode = self.networks['communicationmode-%d' % n]
  182.  
  183.                 print("%d: Name=%s, strength=%s, security=%s, mode=%s" % #, channel=%d bssid=%s" %
  184.                     ((n + 1), name, ss, sec, mode))
  185.  
  186.                 if self.show_extended:
  187.                     chn = self.networks['channel-%d' % n]
  188.                     dbm = self.networks['dbm-%d' % n]
  189.                     bssid = self.networks['bssid-%d' % n]
  190.                     address = self.networks['address-%d' % n]
  191.                     lat = self.networks['latitude-%d' % n]
  192.                     lng = self.networks['longitude-%d' % n]
  193.  
  194.                     print("%d: channel=%d bssid=%s dbm=%s lat=%s long=%s address=%s" %
  195.                         ((n + 1), chn, bssid, dbm, lat, lng, address))
  196.             self.current_network = 0
  197.             if self.num_networks > 1:
  198.                 text_input = "\nSelect the wifi you want to connect to the printer: "
  199.                 self.current_network = int(input(text_input)) - 1 # Python 3
  200.             self.select_wifi()
  201.  
  202.  
  203.     def associate(self, key=to_unicode('')):
  204.         try:
  205.             alg, mode, secretid = self.wifiObj.getCryptoSuite(self.dev, self.adapterName)
  206.         except Error as e:
  207.             self.showIOError(e)
  208.             return
  209.  
  210.         log.debug("Crypto algorithm: %s" % alg)
  211.         log.debug("Crypto mode: %s" % mode)
  212.  
  213.         try:
  214.             self.wifiObj.associate(self.dev, self.adapterName, self.network, self.mode, self.security, key)
  215.         except Error as e:
  216.             self.showIOError(e)
  217.             return
  218.  
  219.     def select_wifi(self):
  220.         self.security = 'None'
  221.         self.mode = 'Infrastructure'
  222.         self.ss = 0
  223.         row = self.current_network
  224.         if row != -1:
  225.             self.network = to_unicode(self.networks['ssid-%d' % row])
  226.             print("\tSelected network SSID: %s" % self.network)
  227.             self.security = self.networks['encryptiontype-%d' % row]
  228.             print("\tSecurity: %s" % self.security)
  229.             self.mode = self.networks['communicationmode-%d' % self.n]
  230.             print("\tMode: %s" % self.mode)
  231.             self.ss = self.networks['signalstrength-%d' % self.n]
  232.             print("\tSignal strength: %s" % self.ss)
  233.  
  234.             if self.security.lower() != 'none':
  235.                 text_input = input("Enter the wifi key: ")
  236.                 key = to_unicode(text_input)
  237.                 self.associate(key)
  238.             else:
  239.                 # Skip config page if no security to setup
  240.                 self.associate()
  241.                
  242.     # The Wifi object here is not actual object, Dynamically relevant modules are selected based on
  243.     # wifi-config value in the models file.
  244.     def getWifiObject(self,wifiConfVal):
  245.         if wifiConfVal == WIFI_CONFIG_LEDM:
  246.             self.wifiObj = LedmWifi
  247.         else:
  248.             self.wifiObj = wifi
  249.  
  250. # WifiCmd()
RAW Paste Data