1 # for localized messages
4 from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER
5 from Components.MultiContent import MultiContentEntryText
6 from Components.GUIComponent import GUIComponent
7 from Components.HTMLComponent import HTMLComponent
8 from Components.config import config, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection, ConfigPassword
9 from Components.Console import Console
12 from string import maketrans, strip
15 from re import compile as re_compile, search as re_search
16 from iwlibs import getNICnames, Wireless, Iwfreq
22 list.append("WPA/WPA2")
25 weplist.append("ASCII")
28 config.plugins.wlan = ConfigSubsection()
29 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
30 config.plugins.wlan.hiddenessid = NoSave(ConfigText(default = "home", fixed_size = False))
32 config.plugins.wlan.encryption = ConfigSubsection()
33 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = True))
34 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WPA/WPA2"))
35 config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
36 config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False))
39 def __init__(self, iface):
42 for i in range(0, 255):
51 self.WlanConsole = Console()
52 self.asciitrans = maketrans(a, b)
54 def stopWlanConsole(self):
55 if self.WlanConsole is not None:
56 print "killing self.WlanConsole"
57 self.WlanConsole = None
60 def getDataForInterface(self, callback = None):
61 #get ip out of ip addr, as avahi sometimes overrides it in ifconfig.
62 print "self.iface im getDataForInterface",self.iface
63 if len(self.WlanConsole.appContainers) == 0:
64 self.WlanConsole = Console()
65 cmd = "iwconfig " + self.iface
66 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, callback)
68 def iwconfigFinished(self, result, retval, extra_args):
69 print "self.iface im iwconfigFinished",self.iface
71 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
72 #print "result im iwconfigFinished",result
74 for line in result.splitlines():
79 ssid = _("No Connection")
81 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
83 ssid = _("Hidden networkname")
85 ssid = _("Hidden networkname")
91 if 'Frequency' in line:
92 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
93 #print "Frequency",frequency
94 if frequency is not None:
95 data['frequency'] = frequency
96 if "Access Point" in line:
97 ap=line[line.index('Access Point')+14:len(line)-1]
100 data['acesspoint'] = ap
101 if "Bit Rate" in line:
102 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')]
106 if 'Encryption key' in line:
110 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
111 #print "Encryption key",enc
113 data['encryption'] = _("Enabled")
114 if 'Quality' in line:
116 qual = line[line.index('Quality')+8:line.index('/100')]
118 qual = line[line.index('Quality')+8:line.index('Sig')]
119 #print "Quality",qual
121 data['quality'] = qual
122 if 'Signal level' in line:
123 signal = line[line.index('Signal level')+14 :line.index(' dBm')]
124 #print "Signal level",signal
125 if signal is not None:
126 data['signal'] = signal
128 self.wlaniface[self.iface] = data
130 if len(self.WlanConsole.appContainers) == 0:
131 print "self.wlaniface after loading:", self.wlaniface
132 self.WlanConsole = None
133 if callback is not None:
134 callback(True,self.wlaniface)
136 def getAdapterAttribute(self, attribute):
137 print "im getAdapterAttribute"
138 if self.wlaniface.has_key(self.iface):
139 print "self.wlaniface.has_key",self.iface
140 if self.wlaniface[self.iface].has_key(attribute):
141 return self.wlaniface[self.iface][attribute]
144 def asciify(self, str):
145 return str.translate(self.asciitrans)
148 def getWirelessInterfaces(self):
151 iwifaces = getNICnames()
153 print "[Wlan.py] No Wireless Networkcards could be found"
158 def getNetworkList(self):
159 system("ifconfig "+self.iface+" up")
160 ifobj = Wireless(self.iface) # a Wireless NIC Object
162 #Association mappings
163 stats, quality, discard, missed_beacon = ifobj.getStatistics()
164 snr = quality.signallevel - quality.noiselevel
167 scanresults = ifobj.scan()
170 print "[Wlan.py] No Wireless Networks could be found"
172 if scanresults is not None:
174 for result in scanresults:
178 encryption = map(lambda x: hex(ord(x)), result.encode)
180 if encryption[-1] == "0x8":
186 for element in result.custom:
187 element = element.encode()
188 extra.append( strip(self.asciify(element)) )
190 if result.quality.sl is 0 and len(extra) > 0:
191 begin = extra[0].find('SignalStrength=')+15
197 if extra[0][begin:end].isdigit():
203 signal = extra[0][begin:end]
204 #print "[Wlan.py] signal is:" + str(signal)
207 signal = str(result.quality.sl)
211 'bssid': result.bssid,
212 'channel': result.frequency.getChannel(result.frequency.getFrequency()),
213 'encrypted': encryption,
214 'essid': strip(self.asciify(result.essid)),
216 'maxrate' : result.rate[-1],
217 'noise' : result.quality.getNoiselevel(),
218 'quality' : str(result.quality.quality),
227 ifobj = Wireless(self.iface)
230 self.channel = str(fq.getChannel(str(ifobj.getFrequency()[0:-3])))
233 #print ifobj.getStatistics()
235 'BSSID': str(ifobj.getAPaddr()),
236 'ESSID': str(ifobj.getEssid()),
237 'quality': str(ifobj.getStatistics()[1].quality),
238 'signal': str(ifobj.getStatistics()[1].sl),
239 'bitrate': str(ifobj.getBitrate()),
240 'channel': str(self.channel),
241 #'channel': str(fq.getChannel(str(ifobj.getFrequency()[0:-3]))),
244 for (key, item) in status.items():
245 if item is "None" or item is "":
246 status[key] = _("N/A")
252 class WlanList(HTMLComponent, GUIComponent):
253 def __init__(self, session, iface):
255 GUIComponent.__init__(self)
262 self.l = eListboxPythonMultiContent()
264 self.l.setFont(0, gFont("Regular", 32))
265 self.l.setFont(1, gFont("Regular", 18))
266 self.l.setFont(2, gFont("Regular", 16))
267 self.l.setBuildFunc(self.buildWlanListEntry)
271 def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate, signal):
273 res = [ (essid, encrypted, iface) ]
278 e = encrypted and _("Yes") or _("No")
279 res.append( MultiContentEntryText(pos=(0, 0), size=(470, 35), font=0, flags=RT_HALIGN_LEFT, text=essid) )
280 res.append( MultiContentEntryText(pos=(425, 0), size=(60, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Signal: ")))
281 res.append( MultiContentEntryText(pos=(480, 0), size=(70, 35), font=0, flags=RT_HALIGN_RIGHT, text="%s" %signal))
282 res.append( MultiContentEntryText(pos=(0, 40), size=(180, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Max. Bitrate: %s") %maxrate ))
283 res.append( MultiContentEntryText(pos=(190, 40), size=(180, 20), font=1, flags=RT_HALIGN_CENTER, text=_("Encrypted: %s") %e ))
284 res.append( MultiContentEntryText(pos=(360, 40), size=(190, 20), font=1, flags=RT_HALIGN_RIGHT, text=_("Interface: %s") %iface ))
289 aps = self.w.getNetworkList()
293 print "[Wlan.py] got Accespoints!"
298 a['essid'] = a['bssid']
299 list.append( (a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate'], a['signal']) )
300 self.aplist.append( a['essid'])
302 self.length = len(list)
306 GUI_WIDGET = eListbox
309 def getCurrent(self):
310 return self.l.getCurrentSelection()
313 def postWidgetCreate(self, instance):
314 instance.setContent(self.l)
315 instance.setItemHeight(60)
330 def writeConfig(self):
332 essid = config.plugins.wlan.essid.value
333 hiddenessid = config.plugins.wlan.hiddenessid.value
334 encrypted = config.plugins.wlan.encryption.enabled.value
335 encryption = config.plugins.wlan.encryption.type.value
336 wepkeytype = config.plugins.wlan.encryption.wepkeytype.value
337 psk = config.plugins.wlan.encryption.psk.value
338 fp = file('/etc/wpa_supplicant.conf', 'w')
339 fp.write('#WPA Supplicant Configuration by enigma2\n')
340 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
341 fp.write('eapol_version=1\n')
342 fp.write('fast_reauth=1\n')
343 if essid == 'hidden...':
344 fp.write('ap_scan=2\n')
346 fp.write('ap_scan=1\n')
347 fp.write('network={\n')
348 if essid == 'hidden...':
349 fp.write('\tssid="'+hiddenessid+'"\n')
351 fp.write('\tssid="'+essid+'"\n')
352 fp.write('\tscan_ssid=0\n')
354 if encryption == 'WPA' or encryption == 'WPA2' or encryption == 'WPA/WPA2' :
356 if encryption == 'WPA':
357 fp.write('\tproto=WPA\n')
358 fp.write('\tpairwise=TKIP\n')
359 fp.write('\tgroup=TKIP\n')
360 elif encryption == 'WPA2':
361 fp.write('\tproto=WPA RSN\n')
362 fp.write('\tpairwise=CCMP TKIP\n')
363 fp.write('\tgroup=CCMP TKIP\n')
365 fp.write('\tproto=WPA WPA2\n')
366 fp.write('\tpairwise=CCMP\n')
367 fp.write('\tgroup=TKIP\n')
368 fp.write('\tpsk="'+psk+'"\n')
370 elif encryption == 'WEP':
371 fp.write('\tkey_mgmt=NONE\n')
372 if wepkeytype == 'ASCII':
373 fp.write('\twep_key0="'+psk+'"\n')
375 fp.write('\twep_key0='+psk+'\n')
377 fp.write('\tkey_mgmt=NONE\n')
381 system("cat /etc/wpa_supplicant.conf")
383 def loadConfig(self):
385 #parse the wpasupplicant configfile
386 fp = file('/etc/wpa_supplicant.conf', 'r')
387 supplicant = fp.readlines()
393 split = s.strip().split('=',1)
394 if split[0] == 'ap_scan':
395 print "[Wlan.py] Got Hidden SSID Scan Value "+split[1]
401 elif split[0] == 'ssid':
402 print "[Wlan.py] Got SSID "+split[1][1:-1]
403 essid = split[1][1:-1]
405 elif split[0] == 'proto':
406 print "split[1]",split[1]
407 config.plugins.wlan.encryption.enabled.value = True
408 if split[1] == "WPA" :
410 if split[1] == "WPA WPA2" :
412 if split[1] == "WPA RSN" :
414 config.plugins.wlan.encryption.type.value = mode
415 print "[Wlan.py] Got Encryption: "+mode
418 #elif split[0] == 'key_mgmt':
419 # print "split[1]",split[1]
420 # if split[1] == "WPA-PSK" :
421 # config.plugins.wlan.encryption.enabled.value = True
422 # config.plugins.wlan.encryption.type.value = "WPA/WPA2"
423 # print "[Wlan.py] Got Encryption: "+ config.plugins.wlan.encryption.type.value
425 elif split[0] == 'wep_key0':
426 config.plugins.wlan.encryption.enabled.value = True
427 config.plugins.wlan.encryption.type.value = 'WEP'
428 if split[1].startswith('"') and split[1].endswith('"'):
429 config.plugins.wlan.encryption.wepkeytype.value = 'ASCII'
430 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
432 config.plugins.wlan.encryption.wepkeytype.value = 'HEX'
433 config.plugins.wlan.encryption.psk.value = split[1]
434 print "[Wlan.py] Got Encryption: WEP - keytype is: "+config.plugins.wlan.encryption.wepkeytype.value
435 print "[Wlan.py] Got Encryption: WEP - key0 is: "+config.plugins.wlan.encryption.psk.value
437 elif split[0] == 'psk':
438 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
439 print "[Wlan.py] Got PSK: "+split[1][1:-1]
444 config.plugins.wlan.hiddenessid.value = essid
445 config.plugins.wlan.essid.value = 'hidden...'
447 config.plugins.wlan.hiddenessid.value = essid
448 config.plugins.wlan.essid.value = essid
450 'hiddenessid': config.plugins.wlan.hiddenessid.value,
451 'ssid': config.plugins.wlan.essid.value,
452 'encryption': config.plugins.wlan.encryption.enabled.value,
453 'encryption_type': config.plugins.wlan.encryption.type.value,
454 'encryption_wepkeytype': config.plugins.wlan.encryption.wepkeytype.value,
455 'key': config.plugins.wlan.encryption.psk.value,
458 for (key, item) in wsconfig.items():
459 if item is "None" or item is "":
460 if key == 'hiddenessid':
461 wsconfig['hiddenessid'] = "home"
463 wsconfig['ssid'] = "home"
464 if key == 'encryption':
465 wsconfig['encryption'] = True
466 if key == 'encryption':
467 wsconfig['encryption_type'] = "WPA/WPA2"
468 if key == 'encryption':
469 wsconfig['encryption_wepkeytype'] = "ASCII"
470 if key == 'encryption':
471 wsconfig['key'] = "mysecurewlan"
474 print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
476 'hiddenessid': "home",
479 'encryption_type': "WPA/WPA2",
480 'encryption_wepkeytype': "ASCII",
481 'key': "mysecurewlan",
483 print "[Wlan.py] WS-CONFIG-->",wsconfig
487 def restart(self, iface):
488 system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
489 system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")
494 self.backupwlaniface = {}
495 self.WlanConsole = Console()
497 def stopWlanConsole(self):
498 if self.WlanConsole is not None:
499 print "killing self.WlanConsole"
500 self.WlanConsole = None
502 def getDataForInterface(self, iface, callback = None):
503 self.WlanConsole = Console()
504 cmd = "iwconfig " + iface
505 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, [iface, callback])
507 def iwconfigFinished(self, result, retval, extra_args):
508 (iface, callback) = extra_args
509 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
510 for line in result.splitlines():
513 if "off/any" in line:
514 ssid = _("No Connection")
516 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
518 ssid = _("Hidden networkname")
520 ssid = _("Hidden networkname")
526 if 'Frequency' in line:
527 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
528 #print "Frequency",frequency
529 if frequency is not None:
530 data['frequency'] = frequency
531 if "Access Point" in line:
532 ap=line[line.index('Access Point')+14:len(line)]
535 data['acesspoint'] = ap
536 if ap == "Not-Associated":
537 data['essid'] = _("No Connection")
538 if "Bit Rate" in line:
540 br = line[line.index('Bit Rate')+9 :line.index(' kb/s')]
542 br = _("Unsupported")
546 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] + " Mb/s"
550 if 'Encryption key' in line:
552 if data['acesspoint'] is not "Not-Associated":
553 enc = _("Unsupported")
557 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
560 #print "Encryption key",enc
562 data['encryption'] = enc
563 if 'Quality' in line:
565 qual = line[line.index('Quality')+8:line.index('/100')]
567 qual = line[line.index('Quality')+8:line.index('Sig')]
568 #print "Quality",qual
570 data['quality'] = qual
571 if 'Signal level' in line:
573 signal = line[line.index('Signal level')+14 :line.index(' dBm')]
576 signal = line[line.index('Signal level')+13:line.index('/100 Noise')]
579 signal = line[line.index('Signal level')+13:line.index(' Noise')]
581 #print "Signal level",signal
582 if signal is not None:
583 data['signal'] = signal
585 self.wlaniface[iface] = data
586 self.backupwlaniface = self.wlaniface
588 if self.WlanConsole is not None:
589 if len(self.WlanConsole.appContainers) == 0:
590 print "self.wlaniface after loading:", self.wlaniface
591 if callback is not None:
592 callback(True,self.wlaniface)
594 def getAdapterAttribute(self, iface, attribute):
595 print "im getAdapterAttribute"
597 if self.wlaniface.has_key(self.iface):
598 print "self.wlaniface.has_key",self.iface
599 if self.wlaniface[self.iface].has_key(attribute):
600 return self.wlaniface[self.iface][attribute]