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
8 from string import maketrans, strip
9 from iwlibs import getNICnames, Wireless, Iwfreq
14 list.append(_("WPA2"))
16 config.plugins.wlan = ConfigSubsection()
17 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
19 config.plugins.wlan.encryption = ConfigSubsection()
20 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
21 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA")))
22 config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False))
25 def __init__(self, iface):
28 for i in range(0, 255):
36 self.asciitrans = maketrans(a, b)
39 def asciify(self, str):
40 return str.translate(self.asciitrans)
43 def getWirelessInterfaces(self):
46 iwifaces = getNICnames()
48 print "[Wlan.py] No Wireless Networkcards could be found"
53 def getNetworkList(self):
54 ifobj = Wireless(self.iface) # a Wireless NIC Object
55 print "ifobj.getStatistics(): ", ifobj.getStatistics()
58 stats, quality, discard, missed_beacon = ifobj.getStatistics()
59 snr = quality.signallevel - quality.noiselevel
60 system("ifconfig "+self.iface+" up")
63 scanresults = ifobj.scan()
66 print "[Wlan.py] No Wireless Networks could be found"
68 if scanresults is not None:
70 for result in scanresults:
74 encryption = map(lambda x: hex(ord(x)), result.encode)
76 if encryption[-1] == "0x8":
82 for element in result.custom:
83 element = element.encode()
84 extra.append( strip(self.asciify(element)) )
86 print result.quality.getSignallevel()
88 if result.quality.sl is 0 and len(extra) > 0:
89 begin = extra[0].find('SignalStrength=')+15
95 if extra[0][begin:end].isdigit():
101 signal = extra[0][begin:end]
102 print "[Wlan.py] signal is:" + str(signal)
105 signal = str(result.quality.sl)
109 'bssid': result.bssid,
110 'channel': result.frequency.getChannel(result.frequency.getFrequency()),
111 'encrypted': encryption,
112 'essid': strip(self.asciify(result.essid)),
114 'maxrate' : result.rate[-1],
115 'noise' : result.quality.getNoiselevel(),
116 'quality' : str(result.quality.quality),
125 ifobj = Wireless(self.iface)
129 'BSSID': str(ifobj.getAPaddr()),
130 'ESSID': str(ifobj.getEssid()),
131 'quality': str(ifobj.getStatistics()[1].quality),
132 'signal': str(ifobj.getStatistics()[1].sl),
133 'bitrate': str(ifobj.getBitrate()),
134 'channel': str(fq.getChannel(str(ifobj.getFrequency()[0:-3]))),
137 for (key, item) in status.items():
138 if item is "None" or item is "":
139 status[key] = _("N/A")
145 class WlanList(HTMLComponent, GUIComponent):
146 def __init__(self, session, iface):
148 GUIComponent.__init__(self)
155 self.l = eListboxPythonMultiContent()
157 self.l.setFont(0, gFont("Regular", 32))
158 self.l.setFont(1, gFont("Regular", 18))
159 self.l.setFont(2, gFont("Regular", 16))
160 self.l.setBuildFunc(self.buildWlanListEntry)
165 def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate, signal):
167 res = [ (essid, encrypted, iface) ]
172 e = encrypted and _("Yes") or _("No")
173 res.append( MultiContentEntryText(pos=(0, 0), size=(470, 35), font=0, flags=RT_HALIGN_LEFT, text=essid) )
174 res.append( MultiContentEntryText(pos=(425, 0), size=(60, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Signal: ")))
175 res.append( MultiContentEntryText(pos=(480, 0), size=(70, 35), font=0, flags=RT_HALIGN_RIGHT, text="%s" %signal))
176 res.append( MultiContentEntryText(pos=(0, 40), size=(180, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Max. Bitrate: %s") %maxrate ))
177 res.append( MultiContentEntryText(pos=(190, 40), size=(180, 20), font=1, flags=RT_HALIGN_CENTER, text=_("Encrypted: %s") %e ))
178 res.append( MultiContentEntryText(pos=(360, 40), size=(190, 20), font=1, flags=RT_HALIGN_RIGHT, text=_("Interface: %s") %iface ))
183 aps = self.w.getNetworkList()
187 print "[Wlan.py] got Accespoints!"
192 a['essid'] = a['bssid']
193 list.append( (a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate'], a['signal']) )
194 self.aplist.append( a['essid'])
196 self.length = len(list)
200 GUI_WIDGET = eListbox
203 def getCurrent(self):
204 return self.l.getCurrentSelection()
207 def postWidgetCreate(self, instance):
208 instance.setContent(self.l)
209 instance.setItemHeight(60)
224 def writeConfig(self):
226 essid = config.plugins.wlan.essid.value
227 encrypted = config.plugins.wlan.encryption.enabled.value
228 encryption = config.plugins.wlan.encryption.type.value
229 psk = config.plugins.wlan.encryption.psk.value
232 fp = file('/etc/wpa_supplicant.conf', 'w')
233 fp.write('#WPA Supplicant Configuration by enigma2\n\n')
234 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
235 fp.write('ctrl_interface_group=0\n')
236 fp.write('eapol_version=1\n')
237 fp.write('ap_scan=1\n')
238 fp.write('fast_reauth=1\n')
239 fp.write('network={\n')
240 fp.write('\tssid="'+essid+'"\n')
241 fp.write('\tscan_ssid=0\n')
245 if encryption == 'WPA' or encryption == 'WPA2':
246 fp.write('\tkey_mgmt=WPA-PSK\n')
248 if encryption == 'WPA':
249 fp.write('\tproto=WPA\n')
250 fp.write('\tpairwise=TKIP\n')
251 fp.write('\tgroup=TKIP\n')
253 fp.write('\tproto=WPA RSN\n')
254 fp.write('\tpairwise=CCMP TKIP\n')
255 fp.write('\tgroup=CCMP TKIP\n')
257 fp.write('\tpsk="'+psk+'"\n')
259 elif encryption == 'WEP':
260 fp.write('\tkey_mgmt=NONE\n')
261 fp.write('\twep_key0="'+psk+'"\n')
263 fp.write('\tkey_mgmt=NONE\n')
268 def loadConfig(self):
270 #parse the wpasupplicant configfile
271 fp = file('/etc/wpa_supplicant.conf', 'r')
272 supplicant = fp.readlines()
276 split = s.strip().split('=',1)
277 if split[0] == 'ssid':
278 print "[Wlan.py] Got SSID "+split[1][1:-1]
279 config.plugins.wlan.essid.value = split[1][1:-1]
281 elif split[0] == 'proto':
282 config.plugins.wlan.encryption.enabled.value = True
283 if split[1] == "WPA RSN" : split[1] = 'WPA2'
284 config.plugins.wlan.encryption.type.value = split[1]
285 print "[Wlan.py] Got Encryption: "+split[1]
287 elif split[0] == 'wep_key0':
288 config.plugins.wlan.encryption.enabled.value = True
289 config.plugins.wlan.encryption.type.value = 'WEP'
290 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
291 print "[Wlan.py] Got Encryption: WEP - key0 is: "+split[1][1:-1]
293 elif split[0] == 'psk':
294 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
295 print "[Wlan.py] Got PSK: "+split[1][1:-1]
300 'ssid': config.plugins.wlan.essid.value,
301 'encryption': config.plugins.wlan.encryption.enabled.value,
302 'encryption_type': config.plugins.wlan.encryption.type.value,
303 'key': config.plugins.wlan.encryption.psk.value,
306 for (key, item) in wsconfig.items():
307 if item is "None" or item is "":
309 wsconfig['ssid'] = "home"
310 if key == 'encryption':
311 wsconfig['encryption'] = False
312 if key == 'encryption':
313 wsconfig['encryption_type'] = "WPA"
314 if key == 'encryption':
315 wsconfig['key'] = "mysecurewlan"
318 print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
322 'encryption_type': "WPA",
323 'key': "mysecurewlan",
325 print "WS-CONFIG-->",wsconfig
329 def restart(self, iface):
330 system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
331 system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")