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
57 stats, quality, discard, missed_beacon = ifobj.getStatistics()
58 snr = quality.signallevel - quality.noiselevel
61 scanresults = ifobj.scan()
64 print "[Wlan.py] No Wireless Networks could be found"
66 if scanresults is not None:
68 for result in scanresults:
72 encryption = map(lambda x: hex(ord(x)), result.encode)
74 if encryption[-1] == "0x8":
80 for element in result.custom:
81 element = element.encode()
82 extra.append( string.strip(self.asciify(element)) )
86 'bssid': result.bssid,
87 'channel': result.frequency.getChannel(result.frequency.getFrequency(), result.range),
88 'encrypted': encryption,
89 'essid': string.strip(self.asciify(result.essid)),
91 'maxrate' : result.rate[-1],
92 'noise' : result.quality.getNoiselevel(),
93 'quality' : result.quality.quality,
94 'signal' : result.quality.getSignallevel(),
101 class WlanList(HTMLComponent, GUIComponent):
103 def __init__(self, session, iface = 'wlan0'):
105 GUIComponent.__init__(self)
110 self.l = eListboxPythonMultiContent()
112 self.l.setFont(0, gFont("Regular", 32))
113 self.l.setFont(1, gFont("Regular", 18))
114 self.l.setFont(2, gFont("Regular", 16))
115 self.l.setBuildFunc(self.buildWlanListEntry)
119 def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate):
121 res = [ (essid, encrypted, iface) ]
122 e = encrypted and _("Yes") or _("No")
123 res.append( MultiContentEntryText(pos=(0, 0), size=(570, 35), font=0, flags=RT_HALIGN_LEFT, text=essid) )
124 res.append( MultiContentEntryText(pos=(0, 40), size=(180, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Max. Bitrate: ")+maxrate) )
125 res.append( MultiContentEntryText(pos=(190, 40), size=(180, 20), font=1, flags=RT_HALIGN_CENTER, text=_("Encrypted: ")+e) )
126 res.append( MultiContentEntryText(pos=(380, 40), size=(190, 20), font=1, flags=RT_HALIGN_RIGHT, text=_("Interface: ")+iface) )
130 aps = self.w.getNetworkList(self.iface)
133 print "[Wlan.py] got Accespoints!"
137 list.append((a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate']))
142 GUI_WIDGET = eListbox
144 def getCurrent(self):
145 return self.l.getCurrentSelection()
147 def postWidgetCreate(self, instance):
148 instance.setContent(self.l)
149 instance.setItemHeight(60)
156 def writeConfig(self):
158 essid = config.plugins.wlan.essid.value
159 encrypted = config.plugins.wlan.encryption.enabled.value
160 encryption = config.plugins.wlan.encryption.type.value
161 psk = config.plugins.wlan.encryption.psk.value
164 fp = file('/etc/wpa_supplicant.conf', 'w')
165 fp.write('#WPA Supplicant Configuration by enigma2\n\n')
166 fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
167 fp.write('ctrl_interface_group=0\n')
168 fp.write('network={\n')
169 fp.write('\tssid="'+essid+'"\n')
170 fp.write('\tscan_ssid=1\n')
174 if encryption == 'WPA' or encryption == 'WPA2':
175 fp.write('\tkey_mgmt=WPA-PSK\n')
177 if encryption == 'WPA':
178 fp.write('\tproto=WPA\n')
179 fp.write('\tpairwise=TKIP\n')
181 fp.write('\tproto=WPA RSN\n')
182 fp.write('\tpairwise=CCMP TKIP\n')
184 fp.write('\tpsk="'+psk+'"\n')
186 elif encryption == 'WEP':
187 fp.write('\tkey_mgmt=NONE\n')
188 fp.write('\twep_key0="'+psk+'"\n')
194 def loadConfig(self):
197 #parse the wpasupplicant configfile
198 fp = file('/etc/wpa_supplicant.conf', 'r')
199 supplicant = fp.readlines()
204 split = s.strip().split('=')
205 if split[0] == 'ssid':
206 print "[Wlan.py] Got SSID "+split[1][1:-1]
207 config.plugins.wlan.essid.value = split[1][1:-1]
209 elif split[0] == 'proto':
210 config.plugins.wlan.encryption.enabled.value = True
211 if split[1] == "WPA RSN" : split[1] = 'WPA2'
212 config.plugins.wlan.encryption.type.value = split[1]
213 print "[Wlan.py] Got Encryption "+split[1]
215 elif split[0] == 'psk':
216 config.plugins.wlan.encryption.psk.value = split[1][1:-1]
217 print "[Wlan.py] Got PSK "+split[1][1:-1]
222 print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
224 def restart(self, iface):
226 os.system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
227 os.system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")