Enigma2-{Network,NetworkWizard,WirelessLan}: more work on wireless lan / networking...
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / WirelessLan / Wlan.py
index 4a7b1e8..a185157 100755 (executable)
@@ -6,29 +6,28 @@ from os import system, path as os_path
 from string import maketrans, strip
 import sys
 import types
-from re import compile as re_compile, search as re_search
+from re import compile as re_compile, search as re_search, escape as re_escape
 from pythonwifi.iwlibs import getNICnames, Wireless, Iwfreq, getWNICnames
 from pythonwifi import flags as wififlags
 
 list = []
+list.append("Unencrypted")
 list.append("WEP")
 list.append("WPA")
-list.append("WPA2")
 list.append("WPA/WPA2")
+list.append("WPA2")
 
 weplist = []
 weplist.append("ASCII")
 weplist.append("HEX")
 
 config.plugins.wlan = ConfigSubsection()
-config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
+config.plugins.wlan.essid = NoSave(ConfigText(default = "", fixed_size = False))
 config.plugins.wlan.hiddenessid = NoSave(ConfigYesNo(default = False))
+config.plugins.wlan.encryption = NoSave(ConfigSelection(list, default = "WPA2"))
+config.plugins.wlan.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
+config.plugins.wlan.psk = NoSave(ConfigPassword(default = "", fixed_size = False))
 
-config.plugins.wlan.encryption = ConfigSubsection()
-config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = True))
-config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WPA/WPA2"))
-config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
-config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False))
 
 def getWlanConfigName(iface):
        return '/etc/wpa_supplicant.' + iface + '.conf'
@@ -74,10 +73,9 @@ class Wlan:
                        scanresults = ifobj.scan()
                except:
                        scanresults = None
-                       print "[Wlan.py] No Wireless Networks could be found"
-               
+                       print "[Wlan.py] No wireless networks could be found"
+               aps = {}
                if scanresults is not None:
-                       aps = {}
                        (num_channels, frequencies) = ifobj.getChannelInfo()
                        index = 1
                        for result in scanresults:
@@ -116,9 +114,9 @@ class Wlan:
                                        'signal' : str(signal),
                                        'custom' : extra,
                                }
-                               #print "GOT APS ENTRY:",aps[bssid]
+
                                index = index + 1
-                       return aps
+               return aps
                
        def stopGetNetworkList(self):
                if self.oldInterfaceState is not None:
@@ -137,10 +135,9 @@ class wpaSupplicant:
        def writeConfig(self, iface):
                essid = config.plugins.wlan.essid.value
                hiddenessid = config.plugins.wlan.hiddenessid.value
-               encrypted = config.plugins.wlan.encryption.enabled.value
-               encryption = config.plugins.wlan.encryption.type.value
-               wepkeytype = config.plugins.wlan.encryption.wepkeytype.value
-               psk = config.plugins.wlan.encryption.psk.value
+               encryption = config.plugins.wlan.encryption.value
+               wepkeytype = config.plugins.wlan.wepkeytype.value
+               psk = config.plugins.wlan.psk.value
                fp = file(getWlanConfigName(iface), 'w')
                fp.write('#WPA Supplicant Configuration by enigma2\n')
                fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
@@ -154,31 +151,29 @@ class wpaSupplicant:
                fp.write('network={\n')
                fp.write('\tssid="'+essid+'"\n')
                fp.write('\tscan_ssid=0\n')                     
-               if encrypted:
-                       if encryption in ('WPA', 'WPA2', 'WPA/WPA2'):
-                               fp.write('\tkey_mgmt=WPA-PSK\n')
-               
-                               if encryption == 'WPA':
-                                       fp.write('\tproto=WPA\n')
-                                       fp.write('\tpairwise=TKIP\n')
-                                       fp.write('\tgroup=TKIP\n')
-                               elif encryption == 'WPA2':
-                                       fp.write('\tproto=RSN\n')
-                                       fp.write('\tpairwise=CCMP\n')
-                                       fp.write('\tgroup=CCMP\n')
-                               else:
-                                       fp.write('\tproto=WPA RSN\n')
-                                       fp.write('\tpairwise=CCMP TKIP\n')
-                                       fp.write('\tgroup=CCMP TKIP\n')
-                               fp.write('\tpsk="'+psk+'"\n')
-                       elif encryption == 'WEP':
-                               fp.write('\tkey_mgmt=NONE\n')
-                               if wepkeytype == 'ASCII':
-                                       fp.write('\twep_key0="'+psk+'"\n')
-                               else:
-                                       fp.write('\twep_key0='+psk+'\n')
+               if encryption in ('WPA', 'WPA2', 'WPA/WPA2'):
+                       fp.write('\tkey_mgmt=WPA-PSK\n')
+                       if encryption == 'WPA':
+                               fp.write('\tproto=WPA\n')
+                               fp.write('\tpairwise=TKIP\n')
+                               fp.write('\tgroup=TKIP\n')
+                       elif encryption == 'WPA2':
+                               fp.write('\tproto=RSN\n')
+                               fp.write('\tpairwise=CCMP\n')
+                               fp.write('\tgroup=CCMP\n')
+                       else:
+                               fp.write('\tproto=WPA RSN\n')
+                               fp.write('\tpairwise=CCMP TKIP\n')
+                               fp.write('\tgroup=CCMP TKIP\n')
+                       fp.write('\tpsk="'+psk+'"\n')
+               elif encryption == 'WEP':
+                       fp.write('\tkey_mgmt=NONE\n')
+                       if wepkeytype == 'ASCII':
+                               fp.write('\twep_key0="'+psk+'"\n')
+                       else:
+                               fp.write('\twep_key0='+psk+'\n')
                else:
-                       fp.write('\tkey_mgmt=NONE\n')                   
+                       fp.write('\tkey_mgmt=NONE\n')
                fp.write('}')
                fp.write('\n')
                fp.close()
@@ -195,54 +190,51 @@ class wpaSupplicant:
                        supplicant = fp.readlines()
                        fp.close()
                        essid = None
+                       encryption = "Unencrypted"
 
                        for s in supplicant:
                                split = s.strip().split('=',1)
                                if split[0] == 'ap_scan':
-                                       #print "[Wlan.py] Got Hidden SSID Scan Value ",split[1]
                                        if split[1] == '2':
                                                config.plugins.wlan.hiddenessid.value = True
                                        else:
                                                config.plugins.wlan.hiddenessid.value = False
-                                       
+
                                elif split[0] == 'ssid':
-                                       #print "[Wlan.py] Got SSID ",split[1][1:-1]
                                        essid = split[1][1:-1]
                                        config.plugins.wlan.essid.value = essid
-                               
+
                                elif split[0] == 'proto':
-                                       config.plugins.wlan.encryption.enabled.value = True
                                        if split[1] == 'WPA' :
                                                mode = 'WPA'
                                        if split[1] == 'RSN':
                                                mode = 'WPA2'
                                        if split[1] in ('WPA RSN', 'WPA WPA2'):
                                                mode = 'WPA/WPA2'
-                                       #print "[Wlan.py] Got Encryption: ",mode
-                                       config.plugins.wlan.encryption.type.value = mode
+                                       encryption = mode
                                        
                                elif split[0] == 'wep_key0':
-                                       config.plugins.wlan.encryption.enabled.value = True
-                                       config.plugins.wlan.encryption.type.value = 'WEP'
+                                       encryption = 'WEP'
                                        if split[1].startswith('"') and split[1].endswith('"'):
-                                               config.plugins.wlan.encryption.wepkeytype.value = 'ASCII'
-                                               config.plugins.wlan.encryption.psk.value = split[1][1:-1]
+                                               config.plugins.wlan.wepkeytype.value = 'ASCII'
+                                               config.plugins.wlan.psk.value = split[1][1:-1]
                                        else:
-                                               config.plugins.wlan.encryption.wepkeytype.value = 'HEX'
-                                               config.plugins.wlan.encryption.psk.value = split[1]                                             
+                                               config.plugins.wlan.wepkeytype.value = 'HEX'
+                                               config.plugins.wlan.psk.value = split[1]                                                
                                        
                                elif split[0] == 'psk':
-                                       config.plugins.wlan.encryption.psk.value = split[1][1:-1]
+                                       config.plugins.wlan.psk.value = split[1][1:-1]
                                else:
                                        pass
+
+                       config.plugins.wlan.encryption.value = encryption
                                
                        wsconfig = {
                                        'hiddenessid': config.plugins.wlan.hiddenessid.value,
                                        'ssid': config.plugins.wlan.essid.value,
-                                       'encryption': config.plugins.wlan.encryption.enabled.value,
-                                       'encryption_type': config.plugins.wlan.encryption.type.value,
-                                       'encryption_wepkeytype': config.plugins.wlan.encryption.wepkeytype.value,
-                                       'key': config.plugins.wlan.encryption.psk.value,
+                                       'encryption': config.plugins.wlan.encryption.value,
+                                       'wepkeytype': config.plugins.wlan.wepkeytype.value,
+                                       'key': config.plugins.wlan.psk.value,
                                }
                
                        for (key, item) in wsconfig.items():
@@ -250,24 +242,21 @@ class wpaSupplicant:
                                        if key == 'hiddenessid':
                                                wsconfig['hiddenessid'] = False
                                        if key == 'ssid':
-                                               wsconfig['ssid'] = "home"
+                                               wsconfig['ssid'] = ""
                                        if key == 'encryption':
-                                               wsconfig['encryption'] = True                           
-                                       if key == 'encryption_type':
-                                               wsconfig['encryption_type'] = "WPA/WPA2"
-                                       if key == 'encryption_wepkeytype':
-                                               wsconfig['encryption_wepkeytype'] = "ASCII"
+                                               wsconfig['encryption'] = "WPA2"                 
+                                       if key == 'wepkeytype':
+                                               wsconfig['wepkeytype'] = "ASCII"
                                        if key == 'key':
-                                               wsconfig['key'] = "mysecurewlan"
+                                               wsconfig['key'] = ""
                except:
                        print "[Wlan.py] Error parsing ",configfile
                        wsconfig = {
                                        'hiddenessid': False,
-                                       'ssid': "home",
-                                       'encryption': True,
-                                       'encryption_type': "WPA/WPA2",
-                                       'encryption_wepkeytype': "ASCII",
-                                       'key': "mysecurewlan",
+                                       'ssid': "",
+                                       'encryption': "WPA2",
+                                       'wepkeytype': "ASCII",
+                                       'key': "",
                                }
                #print "[Wlan.py] WS-CONFIG-->",wsconfig
                return wsconfig
@@ -277,6 +266,7 @@ class Status:
        def __init__(self):
                self.wlaniface = {}
                self.backupwlaniface = {}
+               self.statusCallback = None
                self.WlanConsole = Console()
 
        def stopWlanConsole(self):
@@ -288,66 +278,54 @@ class Status:
        def getDataForInterface(self, iface, callback = None):
                self.WlanConsole = Console()
                cmd = "iwconfig " + iface
-               self.WlanConsole.ePopen(cmd, self.iwconfigFinished, [iface, callback])
+               if callback is not None:
+                       self.statusCallback = callback
+               self.WlanConsole.ePopen(cmd, self.iwconfigFinished, iface)
 
        def iwconfigFinished(self, result, retval, extra_args):
-               (iface, callback) = extra_args
-               data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
+               iface = extra_args
+               data = { 'essid': False, 'frequency': False, 'accesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
                for line in result.splitlines():
                        line = line.strip()
                        if "ESSID" in line:
                                if "off/any" in line:
-                                       ssid = _("No Connection")
+                                       ssid = "off"
                                else:
                                        if "Nickname" in line:
-                                               tmpssid=(line[line.index('ESSID')+7:line.index('"  Nickname')])
-                                               if tmpssid in ('', ' '):
-                                                       ssid = _("Hidden networkname")
-                                               else:
-                                                       ssid = tmpssid
+                                               ssid=(line[line.index('ESSID')+7:line.index('"  Nickname')])
                                        else:
-                                               tmpssid=(line[line.index('ESSID')+7:len(line)-1])
-                                               if tmpssid in ('', ' '):
-                                                       ssid = _("Hidden networkname")
-                                               else:
-                                                       ssid = tmpssid                                          
+                                               ssid=(line[line.index('ESSID')+7:len(line)-1])
                                if ssid is not None:
                                        data['essid'] = ssid
-                       if 'Frequency' in line:
+                       if "Frequency" in line:
                                frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
                                if frequency is not None:
                                        data['frequency'] = frequency
                        if "Access Point" in line:
-                               ap=line[line.index('Access Point')+14:len(line)]
+                               if "Sensitivity" in line:
+                                       ap=line[line.index('Access Point')+14:line.index('   Sensitivity')]
+                               else:
+                                       ap=line[line.index('Access Point')+14:len(line)]
                                if ap is not None:
-                                       data['acesspoint'] = ap
-                                       if ap == "Not-Associated":
-                                               data['essid'] = _("No Connection")
+                                       data['accesspoint'] = ap
                        if "Bit Rate" in line:
                                if "kb" in line:
                                        br = line[line.index('Bit Rate')+9 :line.index(' kb/s')]
-                                       if br == '0':
-                                               br = _("Unsupported")
-                                       else:
-                                               br += " Mb/s"
                                else:
-                                       br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] + " Mb/s"
+                                       br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')]
                                if br is not None:
                                        data['bitrate'] = br
-                       if 'Encryption key' in line:
+                       if "Encryption key" in line:
                                if ":off" in line:
-                                       if data['acesspoint'] is not "Not-Associated":
-                                               enc = _("Unsupported")
-                                       else:
-                                               enc = _("Disabled")
+                                       enc = "off"
                                elif "Security" in line:
                                        enc = line[line.index('Encryption key')+15 :line.index('   Security')]
                                        if enc is not None:
-                                               enc = _("Enabled")
+                                               enc = "on"
                                else:
                                        enc = line[line.index('Encryption key')+15 :len(line)]
                                        if enc is not None:
-                                               enc = _("Enabled")                                      
+                                               enc = "on"
                                if enc is not None:
                                        data['encryption'] = enc
                        if 'Quality' in line:
@@ -359,8 +337,7 @@ class Status:
                                        data['quality'] = qual
                        if 'Signal level' in line:
                                if "dBm" in line:
-                                       signal = line[line.index('Signal level')+13 :line.index(' dBm')]
-                                       signal += " dBm"
+                                       signal = line[line.index('Signal level')+13 :line.index(' dBm')] + " dBm"
                                elif "/100" in line:
                                        if "Noise" in line:
                                                signal = line[line.index('Signal level')+13:line.index('  Noise')]
@@ -380,8 +357,9 @@ class Status:
                if self.WlanConsole is not None:
                        if len(self.WlanConsole.appContainers) == 0:
                                print "[Wlan.py] self.wlaniface after loading:", self.wlaniface
-                               if callback is not None:
-                                       callback(True,self.wlaniface)
+                               if self.statusCallback is not None:
+                                               self.statusCallback(True,self.wlaniface)
+                                               self.statusCallback = None
 
        def getAdapterAttribute(self, iface, attribute):
                self.iface = iface