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)
264 self.l = eListboxPythonMultiContent()
266 self.l.setFont(0, gFont("Regular", 32))
267 self.l.setFont(1, gFont("Regular", 18))
268 self.l.setFont(2, gFont("Regular", 16))
269 self.l.setBuildFunc(self.buildWlanListEntry)
273 def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate, signal):
275 res = [ (essid, encrypted, iface) ]
280 e = encrypted and _("Yes") or _("No")
281 res.append( MultiContentEntryText(pos=(0, 0), size=(470, 35), font=0, flags=RT_HALIGN_LEFT, text=essid) )
282 res.append( MultiContentEntryText(pos=(425, 0), size=(60, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Signal: ")))
283 res.append( MultiContentEntryText(pos=(480, 0), size=(70, 35), font=0, flags=RT_HALIGN_RIGHT, text="%s" %signal))
284 res.append( MultiContentEntryText(pos=(0, 40), size=(180, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Max. Bitrate: %s") %maxrate ))
285 res.append( MultiContentEntryText(pos=(190, 40), size=(180, 20), font=1, flags=RT_HALIGN_CENTER, text=_("Encrypted: %s") %e ))
286 res.append( MultiContentEntryText(pos=(345, 40), size=(190, 20), font=1, flags=RT_HALIGN_RIGHT, text=_("Interface: %s") %iface ))
291 aps = self.w.getNetworkList()
296 print "[Wlan.py] got Accespoints!"
301 # a['essid'] = a['bssid']
302 self.list.append( (a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate'], a['signal']) )
303 #self.aplist.append( a['essid'])
304 if self.oldlist is not None:
305 for entry in self.oldlist:
306 if entry not in self.list:
307 self.list.append(entry)
310 for entry in self.list:
311 self.aplist.append( entry[0])
312 self.length = len(self.list)
313 self.oldlist = self.list
315 self.l.setList(self.list)
317 GUI_WIDGET = eListbox
320 def getCurrent(self):
321 return self.l.getCurrentSelection()
324 def postWidgetCreate(self, instance):
325 instance.setContent(self.l)
326 instance.setItemHeight(60)
341 def writeConfig(self):
343 essid = config.plugins.wlan.essid.value
344 hiddenessid = config.plugins.wlan.hiddenessid.value
345 encrypted = config.plugins.wlan.encryption.enabled.value
346 encryption = config.plugins.wlan.encryption.type.value
347 wepkeytype = config.plugins.wlan.encryption.wepkeytype.value
348 psk = config.plugins.wlan.encryption.psk.value
349 fp = file('/etc/wpa_supplicant.conf', 'w')
350 fp.write('#WPA Supplicant Configuration by enigma2\n')
351 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
352 fp.write('eapol_version=1\n')
353 fp.write('fast_reauth=1\n')
354 if essid == 'hidden...':
355 fp.write('ap_scan=2\n')
357 fp.write('ap_scan=1\n')
358 fp.write('network={\n')
359 if essid == 'hidden...':
360 fp.write('\tssid="'+hiddenessid+'"\n')
362 fp.write('\tssid="'+essid+'"\n')
363 fp.write('\tscan_ssid=0\n')
365 if encryption == 'WPA' or encryption == 'WPA2' or encryption == 'WPA/WPA2' :
366 fp.write('\tkey_mgmt=WPA-PSK\n')
368 if encryption == 'WPA':
369 fp.write('\tproto=WPA\n')
370 fp.write('\tpairwise=TKIP\n')
371 fp.write('\tgroup=TKIP\n')
372 elif encryption == 'WPA2':
373 fp.write('\tproto=WPA RSN\n')
374 fp.write('\tpairwise=CCMP TKIP\n')
375 fp.write('\tgroup=CCMP TKIP\n')
377 fp.write('\tproto=WPA WPA2\n')
378 fp.write('\tpairwise=CCMP\n')
379 fp.write('\tgroup=TKIP\n')
380 fp.write('\tpsk="'+psk+'"\n')
382 elif encryption == 'WEP':
383 fp.write('\tkey_mgmt=NONE\n')
384 if wepkeytype == 'ASCII':
385 fp.write('\twep_key0="'+psk+'"\n')
387 fp.write('\twep_key0='+psk+'\n')
389 fp.write('\tkey_mgmt=NONE\n')
393 system("cat /etc/wpa_supplicant.conf")
395 def loadConfig(self):
397 #parse the wpasupplicant configfile
398 fp = file('/etc/wpa_supplicant.conf', 'r')
399 supplicant = fp.readlines()
405 split = s.strip().split('=',1)
406 if split[0] == 'ap_scan':
407 print "[Wlan.py] Got Hidden SSID Scan Value "+split[1]
413 elif split[0] == 'ssid':
414 print "[Wlan.py] Got SSID "+split[1][1:-1]
415 essid = split[1][1:-1]
417 elif split[0] == 'proto':
418 print "split[1]",split[1]
419 config.plugins.wlan.encryption.enabled.value = True
420 if split[1] == "WPA" :
422 if split[1] == "WPA WPA2" :
424 if split[1] == "WPA RSN" :
426 config.plugins.wlan.encryption.type.value = mode
427 print "[Wlan.py] Got Encryption: "+mode
430 #elif split[0] == 'key_mgmt':
431 # print "split[1]",split[1]
432 # if split[1] == "WPA-PSK" :
433 # config.plugins.wlan.encryption.enabled.value = True
434 # config.plugins.wlan.encryption.type.value = "WPA/WPA2"
435 # print "[Wlan.py] Got Encryption: "+ config.plugins.wlan.encryption.type.value
437 elif split[0] == 'wep_key0':
438 config.plugins.wlan.encryption.enabled.value = True
439 config.plugins.wlan.encryption.type.value = 'WEP'
440 if split[1].startswith('"') and split[1].endswith('"'):
441 config.plugins.wlan.encryption.wepkeytype.value = 'ASCII'
442 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
444 config.plugins.wlan.encryption.wepkeytype.value = 'HEX'
445 config.plugins.wlan.encryption.psk.value = split[1]
446 print "[Wlan.py] Got Encryption: WEP - keytype is: "+config.plugins.wlan.encryption.wepkeytype.value
447 print "[Wlan.py] Got Encryption: WEP - key0 is: "+config.plugins.wlan.encryption.psk.value
449 elif split[0] == 'psk':
450 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
451 print "[Wlan.py] Got PSK: "+split[1][1:-1]
456 config.plugins.wlan.hiddenessid.value = essid
457 config.plugins.wlan.essid.value = 'hidden...'
459 config.plugins.wlan.hiddenessid.value = essid
460 config.plugins.wlan.essid.value = essid
462 'hiddenessid': config.plugins.wlan.hiddenessid.value,
463 'ssid': config.plugins.wlan.essid.value,
464 'encryption': config.plugins.wlan.encryption.enabled.value,
465 'encryption_type': config.plugins.wlan.encryption.type.value,
466 'encryption_wepkeytype': config.plugins.wlan.encryption.wepkeytype.value,
467 'key': config.plugins.wlan.encryption.psk.value,
470 for (key, item) in wsconfig.items():
471 if item is "None" or item is "":
472 if key == 'hiddenessid':
473 wsconfig['hiddenessid'] = "home"
475 wsconfig['ssid'] = "home"
476 if key == 'encryption':
477 wsconfig['encryption'] = True
478 if key == 'encryption':
479 wsconfig['encryption_type'] = "WPA/WPA2"
480 if key == 'encryption':
481 wsconfig['encryption_wepkeytype'] = "ASCII"
482 if key == 'encryption':
483 wsconfig['key'] = "mysecurewlan"
486 print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
488 'hiddenessid': "home",
491 'encryption_type': "WPA/WPA2",
492 'encryption_wepkeytype': "ASCII",
493 'key': "mysecurewlan",
495 print "[Wlan.py] WS-CONFIG-->",wsconfig
499 def restart(self, iface):
500 system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
501 system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")
506 self.backupwlaniface = {}
507 self.WlanConsole = Console()
509 def stopWlanConsole(self):
510 if self.WlanConsole is not None:
511 print "killing self.WlanConsole"
512 self.WlanConsole = None
514 def getDataForInterface(self, iface, callback = None):
515 self.WlanConsole = Console()
516 cmd = "iwconfig " + iface
517 self.WlanConsole.ePopen(cmd, self.iwconfigFinished, [iface, callback])
519 def iwconfigFinished(self, result, retval, extra_args):
520 (iface, callback) = extra_args
521 data = { 'essid': False, 'frequency': False, 'acesspoint': False, 'bitrate': False, 'encryption': False, 'quality': False, 'signal': False }
522 for line in result.splitlines():
525 if "off/any" in line:
526 ssid = _("No Connection")
528 tmpssid=(line[line.index('ESSID')+7:len(line)-1])
530 ssid = _("Hidden networkname")
532 ssid = _("Hidden networkname")
538 if 'Frequency' in line:
539 frequency = line[line.index('Frequency')+10 :line.index(' GHz')]
540 #print "Frequency",frequency
541 if frequency is not None:
542 data['frequency'] = frequency
543 if "Access Point" in line:
544 ap=line[line.index('Access Point')+14:len(line)]
547 data['acesspoint'] = ap
548 if ap == "Not-Associated":
549 data['essid'] = _("No Connection")
550 if "Bit Rate" in line:
552 br = line[line.index('Bit Rate')+9 :line.index(' kb/s')]
554 br = _("Unsupported")
558 br = line[line.index('Bit Rate')+9 :line.index(' Mb/s')] + " Mb/s"
562 if 'Encryption key' in line:
564 if data['acesspoint'] is not "Not-Associated":
565 enc = _("Unsupported")
569 enc = line[line.index('Encryption key')+15 :line.index(' Security')]
572 #print "Encryption key",enc
574 data['encryption'] = enc
575 if 'Quality' in line:
577 qual = line[line.index('Quality')+8:line.index('/100')]
579 qual = line[line.index('Quality')+8:line.index('Sig')]
580 #print "Quality",qual
582 data['quality'] = qual
583 if 'Signal level' in line:
585 signal = line[line.index('Signal level')+14 :line.index(' dBm')]
588 signal = line[line.index('Signal level')+13:line.index('/100 Noise')]
591 signal = line[line.index('Signal level')+13:line.index(' Noise')]
593 #print "Signal level",signal
594 if signal is not None:
595 data['signal'] = signal
597 self.wlaniface[iface] = data
598 self.backupwlaniface = self.wlaniface
600 if self.WlanConsole is not None:
601 if len(self.WlanConsole.appContainers) == 0:
602 print "self.wlaniface after loading:", self.wlaniface
603 if callback is not None:
604 callback(True,self.wlaniface)
606 def getAdapterAttribute(self, iface, attribute):
607 print "im getAdapterAttribute"
609 if self.wlaniface.has_key(self.iface):
610 print "self.wlaniface.has_key",self.iface
611 if self.wlaniface[self.iface].has_key(attribute):
612 return self.wlaniface[self.iface][attribute]