1 from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER
2 from Components.MultiContent import MultiContentEntryText
3 from Components.GUIComponent import GUIComponent
4 from Components.HTMLComponent import HTMLComponent
5 from Components.config import config, ConfigYesNo, NoSave, ConfigSubsection, ConfigText, ConfigSelection, ConfigPassword
6 from Components.Console import Console
9 from string import maketrans, strip
12 from re import compile as re_compile, search as re_search
13 from iwlibs import getNICnames, Wireless, Iwfreq
19 list.append("WPA/WPA2")
22 weplist.append("ASCII")
25 config.plugins.wlan = ConfigSubsection()
26 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
27 config.plugins.wlan.hiddenessid = NoSave(ConfigText(default = "home", fixed_size = False))
29 config.plugins.wlan.encryption = ConfigSubsection()
30 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = True))
31 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = "WPA/WPA2"))
32 config.plugins.wlan.encryption.wepkeytype = NoSave(ConfigSelection(weplist, default = "ASCII"))
33 config.plugins.wlan.encryption.psk = NoSave(ConfigPassword(default = "mysecurewlan", fixed_size = False))
36 def __init__(self, iface):
39 for i in range(0, 255):
48 self.WlanConsole = Console()
49 self.asciitrans = maketrans(a, b)
51 def stopWlanConsole(self):
52 if self.WlanConsole is not None:
53 print "killing self.WlanConsole"
54 self.WlanConsole = None
57 def getDataForInterface(self, callback = None):
58 #get ip out of ip addr, as avahi sometimes overrides it in ifconfig.
59 print "self.iface im getDataForInterface",self.iface
60 if len(self.WlanConsole.appContainers) == 0:
61 self.WlanConsole = Console()
62 cmd = "iwconfig " + self.iface
63 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, callback)
65 def iwconfigFinished(self, result, retval, extra_args):
66 print "self.iface im iwconfigFinished",self.iface
68 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
69 #print "result im iwconfigFinished",result
71 for line in result.splitlines():
76 ssid = _("No Connection")
78 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
80 ssid = _("Hidden networkname")
82 ssid = _("Hidden networkname")
88 if 'Frequency' in line:
89 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
90 #print "Frequency",frequency
91 if frequency is not None:
92 data['frequency'] = frequency
93 if "Access Point" in line:
94 ap=line[line.index('Access Point')+14:len(line)-1]
97 data['acesspoint'] = ap
98 if "Bit Rate" in line:
99 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')]
103 if 'Encryption key' in line:
107 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
108 #print "Encryption key",enc
110 data['encryption'] = _("Enabled")
111 if 'Quality' in line:
113 qual = line[line.index('Quality')+8:line.index('/100')]
115 qual = line[line.index('Quality')+8:line.index('Sig')]
116 #print "Quality",qual
118 data['quality'] = qual
119 if 'Signal level' in line:
120 signal = line[line.index('Signal level')+14 :line.index(' dBm')]
121 #print "Signal level",signal
122 if signal is not None:
123 data['signal'] = signal
125 self.wlaniface[self.iface] = data
127 if len(self.WlanConsole.appContainers) == 0:
128 print "self.wlaniface after loading:", self.wlaniface
129 self.WlanConsole = None
130 if callback is not None:
131 callback(True,self.wlaniface)
133 def getAdapterAttribute(self, attribute):
134 print "im getAdapterAttribute"
135 if self.wlaniface.has_key(self.iface):
136 print "self.wlaniface.has_key",self.iface
137 if self.wlaniface[self.iface].has_key(attribute):
138 return self.wlaniface[self.iface][attribute]
141 def asciify(self, str):
142 return str.translate(self.asciitrans)
145 def getWirelessInterfaces(self):
148 iwifaces = getNICnames()
150 print "[Wlan.py] No Wireless Networkcards could be found"
155 def getNetworkList(self):
156 system("ifconfig "+self.iface+" up")
157 ifobj = Wireless(self.iface) # a Wireless NIC Object
159 #Association mappings
160 stats, quality, discard, missed_beacon = ifobj.getStatistics()
161 snr = quality.signallevel - quality.noiselevel
164 scanresults = ifobj.scan()
167 print "[Wlan.py] No Wireless Networks could be found"
169 if scanresults is not None:
171 for result in scanresults:
175 encryption = map(lambda x: hex(ord(x)), result.encode)
177 if encryption[-1] == "0x8":
183 for element in result.custom:
184 element = element.encode()
185 extra.append( strip(self.asciify(element)) )
187 if result.quality.sl is 0 and len(extra) > 0:
188 begin = extra[0].find('SignalStrength=')+15
194 if extra[0][begin:end].isdigit():
200 signal = extra[0][begin:end]
201 #print "[Wlan.py] signal is:" + str(signal)
204 signal = str(result.quality.sl)
208 'bssid': result.bssid,
209 'channel': result.frequency.getChannel(result.frequency.getFrequency()),
210 'encrypted': encryption,
211 'essid': strip(self.asciify(result.essid)),
213 'maxrate' : result.rate[-1],
214 'noise' : result.quality.getNoiselevel(),
215 'quality' : str(result.quality.quality),
224 ifobj = Wireless(self.iface)
227 self.channel = str(fq.getChannel(str(ifobj.getFrequency()[0:-3])))
230 #print ifobj.getStatistics()
232 'BSSID': str(ifobj.getAPaddr()),
233 'ESSID': str(ifobj.getEssid()),
234 'quality': str(ifobj.getStatistics()[1].quality),
235 'signal': str(ifobj.getStatistics()[1].sl),
236 'bitrate': str(ifobj.getBitrate()),
237 'channel': str(self.channel),
238 #'channel': str(fq.getChannel(str(ifobj.getFrequency()[0:-3]))),
241 for (key, item) in status.items():
242 if item is "None" or item is "":
243 status[key] = _("N/A")
249 class WlanList(HTMLComponent, GUIComponent):
250 def __init__(self, session, iface):
252 GUIComponent.__init__(self)
261 self.l = eListboxPythonMultiContent()
263 self.l.setFont(0, gFont("Regular", 32))
264 self.l.setFont(1, gFont("Regular", 18))
265 self.l.setFont(2, gFont("Regular", 16))
266 self.l.setBuildFunc(self.buildWlanListEntry)
270 def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate, signal):
272 res = [ (essid, encrypted, iface) ]
277 e = encrypted and _("Yes") or _("No")
278 res.append( MultiContentEntryText(pos=(0, 0), size=(470, 35), font=0, flags=RT_HALIGN_LEFT, text=essid) )
279 res.append( MultiContentEntryText(pos=(425, 0), size=(60, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Signal: ")))
280 res.append( MultiContentEntryText(pos=(480, 0), size=(70, 35), font=0, flags=RT_HALIGN_RIGHT, text="%s" %signal))
281 res.append( MultiContentEntryText(pos=(0, 40), size=(180, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Max. Bitrate: %s") %maxrate ))
282 res.append( MultiContentEntryText(pos=(190, 40), size=(180, 20), font=1, flags=RT_HALIGN_CENTER, text=_("Encrypted: %s") %e ))
283 res.append( MultiContentEntryText(pos=(345, 40), size=(190, 20), font=1, flags=RT_HALIGN_RIGHT, text=_("Interface: %s") %iface ))
288 aps = self.w.getNetworkList()
293 print "[Wlan.py] got Accespoints!"
298 # a['essid'] = a['bssid']
299 self.list.append( (a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate'], a['signal']) )
300 #self.aplist.append( a['essid'])
301 if self.oldlist is not None:
302 for entry in self.oldlist:
303 if entry not in self.list:
304 self.list.append(entry)
307 for entry in self.list:
308 self.aplist.append( entry[0])
309 self.length = len(self.list)
310 self.oldlist = self.list
312 self.l.setList(self.list)
314 GUI_WIDGET = eListbox
317 def getCurrent(self):
318 return self.l.getCurrentSelection()
321 def postWidgetCreate(self, instance):
322 instance.setContent(self.l)
323 instance.setItemHeight(60)
338 def writeConfig(self):
340 essid = config.plugins.wlan.essid.value
341 hiddenessid = config.plugins.wlan.hiddenessid.value
342 encrypted = config.plugins.wlan.encryption.enabled.value
343 encryption = config.plugins.wlan.encryption.type.value
344 wepkeytype = config.plugins.wlan.encryption.wepkeytype.value
345 psk = config.plugins.wlan.encryption.psk.value
346 fp = file('/etc/wpa_supplicant.conf', 'w')
347 fp.write('#WPA Supplicant Configuration by enigma2\n')
348 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
349 fp.write('eapol_version=1\n')
350 fp.write('fast_reauth=1\n')
351 if essid == 'hidden...':
352 fp.write('ap_scan=2\n')
354 fp.write('ap_scan=1\n')
355 fp.write('network={\n')
356 if essid == 'hidden...':
357 fp.write('\tssid="'+hiddenessid+'"\n')
359 fp.write('\tssid="'+essid+'"\n')
360 fp.write('\tscan_ssid=0\n')
362 if encryption == 'WPA' or encryption == 'WPA2' or encryption == 'WPA/WPA2' :
363 fp.write('\tkey_mgmt=WPA-PSK\n')
365 if encryption == 'WPA':
366 fp.write('\tproto=WPA\n')
367 fp.write('\tpairwise=TKIP\n')
368 fp.write('\tgroup=TKIP\n')
369 elif encryption == 'WPA2':
370 fp.write('\tproto=WPA RSN\n')
371 fp.write('\tpairwise=CCMP TKIP\n')
372 fp.write('\tgroup=CCMP TKIP\n')
374 fp.write('\tproto=WPA WPA2\n')
375 fp.write('\tpairwise=CCMP\n')
376 fp.write('\tgroup=TKIP\n')
377 fp.write('\tpsk="'+psk+'"\n')
379 elif encryption == 'WEP':
380 fp.write('\tkey_mgmt=NONE\n')
381 if wepkeytype == 'ASCII':
382 fp.write('\twep_key0="'+psk+'"\n')
384 fp.write('\twep_key0='+psk+'\n')
386 fp.write('\tkey_mgmt=NONE\n')
390 system("cat /etc/wpa_supplicant.conf")
392 def loadConfig(self):
394 #parse the wpasupplicant configfile
395 fp = file('/etc/wpa_supplicant.conf', 'r')
396 supplicant = fp.readlines()
402 split = s.strip().split('=',1)
403 if split[0] == 'ap_scan':
404 print "[Wlan.py] Got Hidden SSID Scan Value "+split[1]
410 elif split[0] == 'ssid':
411 print "[Wlan.py] Got SSID "+split[1][1:-1]
412 essid = split[1][1:-1]
414 elif split[0] == 'proto':
415 print "split[1]",split[1]
416 config.plugins.wlan.encryption.enabled.value = True
417 if split[1] == "WPA" :
419 if split[1] == "WPA WPA2" :
421 if split[1] == "WPA RSN" :
423 config.plugins.wlan.encryption.type.value = mode
424 print "[Wlan.py] Got Encryption: "+mode
427 #elif split[0] == 'key_mgmt':
428 # print "split[1]",split[1]
429 # if split[1] == "WPA-PSK" :
430 # config.plugins.wlan.encryption.enabled.value = True
431 # config.plugins.wlan.encryption.type.value = "WPA/WPA2"
432 # print "[Wlan.py] Got Encryption: "+ config.plugins.wlan.encryption.type.value
434 elif split[0] == 'wep_key0':
435 config.plugins.wlan.encryption.enabled.value = True
436 config.plugins.wlan.encryption.type.value = 'WEP'
437 if split[1].startswith('"') and split[1].endswith('"'):
438 config.plugins.wlan.encryption.wepkeytype.value = 'ASCII'
439 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
441 config.plugins.wlan.encryption.wepkeytype.value = 'HEX'
442 config.plugins.wlan.encryption.psk.value = split[1]
443 print "[Wlan.py] Got Encryption: WEP - keytype is: "+config.plugins.wlan.encryption.wepkeytype.value
444 print "[Wlan.py] Got Encryption: WEP - key0 is: "+config.plugins.wlan.encryption.psk.value
446 elif split[0] == 'psk':
447 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
448 print "[Wlan.py] Got PSK: "+split[1][1:-1]
453 config.plugins.wlan.hiddenessid.value = essid
454 config.plugins.wlan.essid.value = 'hidden...'
456 config.plugins.wlan.hiddenessid.value = essid
457 config.plugins.wlan.essid.value = essid
459 'hiddenessid': config.plugins.wlan.hiddenessid.value,
460 'ssid': config.plugins.wlan.essid.value,
461 'encryption': config.plugins.wlan.encryption.enabled.value,
462 'encryption_type': config.plugins.wlan.encryption.type.value,
463 'encryption_wepkeytype': config.plugins.wlan.encryption.wepkeytype.value,
464 'key': config.plugins.wlan.encryption.psk.value,
467 for (key, item) in wsconfig.items():
468 if item is "None" or item is "":
469 if key == 'hiddenessid':
470 wsconfig['hiddenessid'] = "home"
472 wsconfig['ssid'] = "home"
473 if key == 'encryption':
474 wsconfig['encryption'] = True
475 if key == 'encryption':
476 wsconfig['encryption_type'] = "WPA/WPA2"
477 if key == 'encryption':
478 wsconfig['encryption_wepkeytype'] = "ASCII"
479 if key == 'encryption':
480 wsconfig['key'] = "mysecurewlan"
483 print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
485 'hiddenessid': "home",
488 'encryption_type': "WPA/WPA2",
489 'encryption_wepkeytype': "ASCII",
490 'key': "mysecurewlan",
492 print "[Wlan.py] WS-CONFIG-->",wsconfig
496 def restart(self, iface):
497 system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
498 system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")
503 self.backupwlaniface = {}
504 self.WlanConsole = Console()
506 def stopWlanConsole(self):
507 if self.WlanConsole is not None:
508 print "killing self.WlanConsole"
509 self.WlanConsole = None
511 def getDataForInterface(self, iface, callback = None):
512 self.WlanConsole = Console()
513 cmd = "iwconfig " + iface
514 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, [iface, callback])
516 def iwconfigFinished(self, result, retval, extra_args):
517 (iface, callback) = extra_args
518 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
519 for line in result.splitlines():
522 if "off/any" in line:
523 ssid = _("No Connection")
525 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
527 ssid = _("Hidden networkname")
529 ssid = _("Hidden networkname")
535 if 'Frequency' in line:
536 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
537 #print "Frequency",frequency
538 if frequency is not None:
539 data['frequency'] = frequency
540 if "Access Point" in line:
541 ap=line[line.index('Access Point')+14:len(line)]
544 data['acesspoint'] = ap
545 if ap == "Not-Associated":
546 data['essid'] = _("No Connection")
547 if "Bit Rate" in line:
549 br = line[line.index('Bit Rate')+9 :line.index(' kb/s')]
551 br = _("Unsupported")
555 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] + " Mb/s"
559 if 'Encryption key' in line:
561 if data['acesspoint'] is not "Not-Associated":
562 enc = _("Unsupported")
566 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
569 #print "Encryption key",enc
571 data['encryption'] = enc
572 if 'Quality' in line:
574 qual = line[line.index('Quality')+8:line.index('/100')]
576 qual = line[line.index('Quality')+8:line.index('Sig')]
577 #print "Quality",qual
579 data['quality'] = qual
580 if 'Signal level' in line:
582 signal = line[line.index('Signal level')+14 :line.index(' dBm')]
585 signal = line[line.index('Signal level')+13:line.index('/100 Noise')]
588 signal = line[line.index('Signal level')+13:line.index(' Noise')]
590 #print "Signal level",signal
591 if signal is not None:
592 data['signal'] = signal
594 self.wlaniface[iface] = data
595 self.backupwlaniface = self.wlaniface
597 if self.WlanConsole is not None:
598 if len(self.WlanConsole.appContainers) == 0:
599 print "self.wlaniface after loading:", self.wlaniface
600 if callback is not None:
601 callback(True,self.wlaniface)
603 def getAdapterAttribute(self, iface, attribute):
604 print "im getAdapterAttribute"
606 if self.wlaniface.has_key(self.iface):
607 print "self.wlaniface.has_key",self.iface
608 if self.wlaniface[self.iface].has_key(attribute):
609 return self.wlaniface[self.iface][attribute]