1 from Plugins.Plugin import PluginDescriptor
2 from Components.Network import iNetwork
3 from Components.config import config
4 from Screens.Screen import Screen
5 from Screens.HelpMenu import HelpableScreen
6 from Components.ActionMap import ActionMap
7 from Components.ActionMap import HelpableActionMap
8 from Components.Sources.StaticText import StaticText
9 from Components.MenuList import MenuList
10 from enigma import eTimer
11 from Wlan import iWlan, iStatus, wpaSupplicant
12 from pythonwifi.iwlibs import Wireless
13 from pythonwifi import flags as wifi_flags
18 SHOW_HIDDEN_NETWORK = False
19 class WlanScan(Screen, HelpableScreen):
21 <screen position="center,center" size="510,400" title="Wireless Network AP Scan..." >
22 <ePixmap pixmap="skin_default/div-h.png" position="0,350" zPosition="1" size="560,2" />
23 <ePixmap pixmap="skin_default/border_menu.png" position="10,10" zPosition="1" size="250,300" transparent="1" alphatest="on" />
25 <ePixmap pixmap="skin_default/buttons/red.png" position="10,360" size="140,40" alphatest="on" />
26 <ePixmap pixmap="skin_default/buttons/green.png" position="185,360" size="140,40" alphatest="on" />
27 <ePixmap pixmap="skin_default/buttons/blue.png" position="360,360" size="140,40" alphatest="on" />
29 <widget source="key_red" render="Label" position="10,360" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
30 <widget source="key_green" render="Label" position="185,360" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
31 <widget source="key_blue" render="Label" position="360,360" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#18188b" foregroundColor="#ffffff" transparent="1" />
33 <widget name="aplist" position="20,20" size="230,275" backgroundColor="#371e1c1a" transparent="1" zPosition="10" scrollbarMode="showOnDemand" />
35 <widget source="ESSID" render="Label" position="265,70" zPosition="1" size="240,30" font="Regular;18" halign="center" valign="center" />
36 <widget source="Address" render="Label" position="265,100" zPosition="1" size="240,30" font="Regular;18" halign="center" valign="center" />
37 <widget source="Protocol" render="Label" position="265,130" zPosition="1" size="240,30" font="Regular;18" halign="center" valign="center" />
38 <widget source="Frequency" render="Label" position="265,160" zPosition="1" size="240,30" font="Regular;18" halign="center" valign="center" />
39 <widget source="Channel" render="Label" position="265,190" zPosition="1" size="240,30" font="Regular;18" halign="center" valign="center" />
40 <widget source="Encryption key" render="Label" position="265,220" zPosition="1" size="240,30" font="Regular;18" halign="center" valign="center" />
41 <widget source="BitRate" render="Label" position="265,250" zPosition="1" size="240,30" font="Regular;18" halign="center" valign="center" />
42 <widget source="Status" render="Label" position="115,310" zPosition="1" size="300,30" font="Regular;18" halign="center" valign="center" />
45 def __init__(self, session, iface):
46 Screen.__init__(self,session)
47 HelpableScreen.__init__(self)
48 self.skinName = "WlanScanAp"
49 self.session = session
51 self.wlanscanap = None
55 self["WizardActions"] = HelpableActionMap(self, "WizardActions",
57 "up": (self.up, _("move up to previous entry")),
58 "down": (self.down, _("move down to next entry")),
59 "left": (self.left, _("move up to first entry")),
60 "right": (self.right, _("move down to last entry")),
63 self["OkCancelActions"] = HelpableActionMap(self, "OkCancelActions",
65 "cancel": (self.cancel, _("exit")),
66 "ok": (self.ok, "select AP"),
69 self["ColorActions"] = HelpableActionMap(self, "ColorActions",
71 "red": (self.cancel, _("exit")),
72 "green": (self.ok, "select AP"),
73 "blue": (self.startWlanConfig, "Edit Wireless settings"),
76 self["aplist"] = MenuList([])
77 self["key_red"] = StaticText(_("Close"))
78 self["key_green"] = StaticText(_("Select"))
79 self["key_blue"] = StaticText(_("EditSetting"))
80 self["Status"] = StaticText(_(" "))
81 self["ESSID"] = StaticText(" ")
82 self["Address"] = StaticText(" ")
83 self["Protocol"] = StaticText(" ")
84 self["Frequency"] = StaticText(" ")
85 self["Channel"] = StaticText(" ")
86 self["Encryption key"] = StaticText(" ")
87 self["BitRate"] = StaticText(" ")
88 self.oldInterfaceState = iNetwork.getAdapterAttribute(self.iface, "up")
90 self.startupTimer = eTimer()
91 self.startupTimer.callback.append(self.startup)
93 self.activateIfaceTimer = eTimer()
94 self.activateIfaceTimer.callback.append(self.activateIface)
96 self.updateStatusTimer = eTimer()
97 self.updateStatusTimer.callback.append(self.updateStatus)
99 self.scanAplistTimer = eTimer()
100 self.scanAplistTimer.callback.append(self.scanApList)
102 self.onClose.append(self.__onClose)
103 self.onShown.append(lambda: self.startupTimer.start(10, True))
106 iWlan.setInterface(self.iface)
107 if self.oldInterfaceState is not True:
108 self["Status"].setText(("Please wait for activating interface..."))
109 self.activateIfaceTimer.start(10, True)
111 self.updateStatusTimer.start(10, True)
113 def activateIface(self):
114 iWlan.activateIface()
115 self.updateStatusTimer.start(10, True)
117 def updateStatus(self):
118 self["Status"].setText(("Please wait for scanning AP..."))
119 self.scanAplistTimer.stop()
120 self.scanAplistTimer.start(10, True)
122 def updateAPList(self):
123 self.updateStatusTimer.stop()
124 self.updateStatusTimer.start(7000, True)
127 self["aplist"].pageUp()
131 self["aplist"].pageDown()
139 self["aplist"].down()
144 if self["aplist"].getCurrent() is not None:
145 essid = self["aplist"].getCurrent()[0]
151 def startWlanConfig(self): # key blue
152 if self["aplist"].getCurrent() is not None:
153 essid = self["aplist"].getCurrent()[0]
156 def scanApList(self):
157 self.apList = iWlan.getNetworkList()
158 print "GET APLIST %d" % len(self.apList)
159 old_aplist = self.setApList
161 new_bssid_list = self.apList.keys()
162 old_bssid_list = [x[1] for x in old_aplist]
164 remove_bssid_list = [x for x in old_aplist if x[1] not in new_bssid_list]
165 add_bssid_list = [x for x in new_bssid_list if x not in old_bssid_list]
167 for x in remove_bssid_list:
168 self.setApList.remove(x)
170 for bssid in add_bssid_list:
171 essid = self.apList[bssid].get("ESSID", None)
173 global SHOW_HIDDEN_NETWORK
174 if SHOW_HIDDEN_NETWORK:
175 essid = "# Hidden Network"
179 essid = essid.strip('\x00').strip('\\x00')
182 self.setApList.append( (essid, bssid) )
184 self.setApList = sorted(self.setApList, key=lambda x: int(self.apList[x[1]]['Quality'].split('/')[0]), reverse=True)
186 self["aplist"].setList(self.setApList)
187 self["Status"].setText(("%d AP detected" % len(self.setApList)))
191 def displayApInfo(self):
192 if self["aplist"].getCurrent() is not None:
193 bssid = self["aplist"].getCurrent()[1]
194 for key in ["Address", "ESSID", "Protocol", "Frequency", "Encryption key", "BitRate", "Channel"]:
195 if self.apList[bssid].has_key(key) and self.apList[bssid][key] is not None:
196 value = str(self.apList[bssid][key])
199 self[key].setText(( "%s: %s" % (key, value) ))
202 iWlan.deActivateIface()
205 class WlanStatus(Screen):
207 <screen position="center,center" size="510,400" title="Wireless Network Status..." >
208 <widget source="status" render="Label" position="5,15" size="500,350" font="Regular;18" zPosition="1" />
210 <ePixmap pixmap="skin_default/buttons/red.png" position="185,360" size="140,40" alphatest="on" />
211 <widget source="key_red" render="Label" position="185,360" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
214 def __init__(self, session, iface):
215 Screen.__init__(self,session)
216 self.skinName = "Wlanstatus"
217 self.session = session
220 self["actions"] = ActionMap(["ShortcutActions", "SetupActions"],
222 "cancel": self.close,
227 self["status"] = StaticText(_("Reading..."))
228 self["key_red"] = StaticText(_("Close"))
230 self.startupTimer = eTimer()
231 self.startupTimer.callback.append(self.startup)
232 self.updateTimer = eTimer()
233 self.updateTimer.callback.append(self.update)
234 self.onClose.append(self.__onClose)
235 self.onShown.append(lambda: self.startupTimer.start(10, True))
241 self.updateTimer.stop()
242 iStatus.stopWlanConsole()
245 self.updateTimer.stop()
246 iStatus.getDataForInterface(self.iface, self.getInfoCB)
247 self.updateTimer.start(5000, True)
249 def getInfoCB(self, retval, data):
250 #print "[getInfoCB] ", data
252 if retval and data and self.iface:
253 data = data[self.iface]
254 _text += "Interface : %s\n" % self.iface
255 if data["frequency"]:
256 _text += "Frequency : %s\n" % data["frequency"]
257 elif data["channel"]:
258 _text += "Channel : %s\n" % data["channel"]
260 _text += "Access point : %s\n" % data["accesspoint"]
261 _text += "Bit Rate : %s\n" % data["bitrate"]
262 _text += "Link Quality : %s\n" % data["link_quality"]
263 _text += "Signal level : %s\n" % data["signal_level"]
264 _text += "Noise level : %s\n" % data["noise_level"]
265 _text += "Ip address : %s\n" % (data["ip_addr"] or "None")
269 self["status"].setText(_text)
271 def getConfigStrings(iface):
273 if iNetwork.useWlCommand(iface):
274 essid = config.plugins.wlan.essid.value
275 encryption = config.plugins.wlan.encryption.value
276 key = config.plugins.wlan.psk.value
277 encryption = {"Unencrypted" : "None", "WEP" : "wep", "WPA" : "wpa", "WPA2" : "wpa2"}.get(encryption, "wpa2")
278 contents = '\tpre-up wl-config.sh -m %s -k "%s" -s "%s" \n' % (encryption, key, essid)
279 contents += '\tpost-down wl-down.sh\n'
282 wpaSupplicantName = ws.getWpaSupplicantName(iface)
283 contents = "\tpre-up wpa_supplicant -i"+iface+" -c%s -B -D" % (wpaSupplicantName) +iNetwork.detectWlanModule(iface)+"\n"
284 contents += "\tpost-down wpa_cli terminate\n"
286 #print "[getConfigStrings] : ", contents
289 def Plugins(**kwargs):
291 fnc ["ifaceSupported"] = lambda iface: iNetwork.isWirelessInterface(iface) or None
292 fnc ["configStrings"] = getConfigStrings
293 fnc ["WlanPluginEntry"] = None
295 return PluginDescriptor(name=_("Wireless LAN Setup"), description="Wireless LAN Setup", where = [PluginDescriptor.WHERE_NETWORKSETUP], fnc = fnc)