1 from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER
3 from Components.MultiContent import MultiContentEntryText
4 from Components.GUIComponent import GUIComponent
5 from Components.HTMLComponent import HTMLComponent
6 from Components.config import config, ConfigYesNo, ConfigIP, NoSave, ConfigSubsection, ConfigMAC, ConfigEnableDisable, ConfigText, ConfigSelection
8 from pythonwifi import iwlibs
16 list.append(_("WPA2"))
18 config.plugins.wlan = ConfigSubsection()
19 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
21 config.plugins.wlan.encryption = ConfigSubsection()
22 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
23 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA")))
24 config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False))
30 for i in range(0, 255):
37 self.asciitrans = string.maketrans(a, b)
39 def asciify(self, str):
40 return str.translate(self.asciitrans)
42 def getWirelessInterfaces(self):
45 iwifaces = iwlibs.getNICnames()
48 "[Wlan.py] No Wireless Networkcards could be found"
52 def getNetworkList(self, iface):
54 ifobj = iwlibs.Wireless(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 os.system("ifconfig "+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( string.strip(self.asciify(element)) )
88 'bssid': result.bssid,
89 'channel': result.frequency.getChannel(result.frequency.getFrequency(), result.range),
90 'encrypted': encryption,
91 'essid': string.strip(self.asciify(result.essid)),
93 'maxrate' : result.rate[-1],
94 'noise' : result.quality.getNoiselevel(),
95 'quality' : result.quality.quality,
96 'signal' : result.quality.getSignallevel(),
103 class WlanList(HTMLComponent, GUIComponent):
105 def __init__(self, session, iface):
107 GUIComponent.__init__(self)
112 self.l = eListboxPythonMultiContent()
114 self.l.setFont(0, gFont("Regular", 32))
115 self.l.setFont(1, gFont("Regular", 18))
116 self.l.setFont(2, gFont("Regular", 16))
117 self.l.setBuildFunc(self.buildWlanListEntry)
121 def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate):
123 res = [ (essid, encrypted, iface) ]
124 e = encrypted and _("Yes") or _("No")
125 res.append( MultiContentEntryText(pos=(0, 0), size=(570, 35), font=0, flags=RT_HALIGN_LEFT, text=essid) )
126 res.append( MultiContentEntryText(pos=(0, 40), size=(180, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Max. Bitrate: ")+maxrate) )
127 res.append( MultiContentEntryText(pos=(190, 40), size=(180, 20), font=1, flags=RT_HALIGN_CENTER, text=_("Encrypted: ")+e) )
128 res.append( MultiContentEntryText(pos=(380, 40), size=(190, 20), font=1, flags=RT_HALIGN_RIGHT, text=_("Interface: ")+iface) )
132 aps = self.w.getNetworkList(self.iface)
135 print "[Wlan.py] got Accespoints!"
139 list.append((a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate']))
144 GUI_WIDGET = eListbox
146 def getCurrent(self):
147 return self.l.getCurrentSelection()
149 def postWidgetCreate(self, instance):
150 instance.setContent(self.l)
151 instance.setItemHeight(60)
158 def writeConfig(self):
160 essid = config.plugins.wlan.essid.value
161 encrypted = config.plugins.wlan.encryption.enabled.value
162 encryption = config.plugins.wlan.encryption.type.value
163 psk = config.plugins.wlan.encryption.psk.value
166 fp = file('/etc/wpa_supplicant.conf', 'w')
167 fp.write('#WPA Supplicant Configuration by enigma2\n\n')
168 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
169 fp.write('ctrl_interface_group=0\n')
170 fp.write('network={\n')
171 fp.write('\tssid="'+essid+'"\n')
172 fp.write('\tscan_ssid=1\n')
176 if encryption == 'WPA' or encryption == 'WPA2':
177 fp.write('\tkey_mgmt=WPA-PSK\n')
179 if encryption == 'WPA':
180 fp.write('\tproto=WPA\n')
181 fp.write('\tpairwise=TKIP\n')
183 fp.write('\tproto=WPA RSN\n')
184 fp.write('\tpairwise=CCMP TKIP\n')
186 fp.write('\tpsk="'+psk+'"\n')
188 elif encryption == 'WEP':
189 fp.write('\tkey_mgmt=NONE\n')
190 fp.write('\twep_key0="'+psk+'"\n')
196 def loadConfig(self):
199 #parse the wpasupplicant configfile
200 fp = file('/etc/wpa_supplicant.conf', 'r')
201 supplicant = fp.readlines()
206 split = s.strip().split('=')
207 if split[0] == 'ssid':
208 print "[Wlan.py] Got SSID "+split[1][1:-1]
209 config.plugins.wlan.essid.value = split[1][1:-1]
211 elif split[0] == 'proto':
212 config.plugins.wlan.encryption.enabled.value = True
213 if split[1] == "WPA RSN" : split[1] = 'WPA2'
214 config.plugins.wlan.encryption.type.value = split[1]
215 print "[Wlan.py] Got Encryption "+split[1]
217 elif split[0] == 'psk':
218 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
219 print "[Wlan.py] Got PSK "+split[1][1:-1]
224 print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
226 def restart(self, iface):
228 os.system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
229 os.system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")