Support turbo2.
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / WirelessLanSetup / plugin.py
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
14 import copy
15
16 import os
17
18 SHOW_HIDDEN_NETWORK = False
19 class WlanScan(Screen, HelpableScreen):
20         skin =  """
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" />
24
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" />
28
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" />
32
33                         <widget name="aplist" position="20,20" size="230,275" backgroundColor="#371e1c1a" transparent="1" zPosition="10" scrollbarMode="showOnDemand" />
34
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" />
43                 </screen>
44                 """
45         def __init__(self, session, iface):
46                 Screen.__init__(self,session)
47                 HelpableScreen.__init__(self)
48                 self.skinName = "WlanScanAp"
49                 self.session = session
50                 self.iface = iface
51                 self.wlanscanap = None
52                 self.apList = {}
53                 self.setApList = []
54
55                 self["WizardActions"] = HelpableActionMap(self, "WizardActions",
56                 {
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")),
61                 }, -2)
62
63                 self["OkCancelActions"] = HelpableActionMap(self, "OkCancelActions",
64                 {
65                         "cancel": (self.cancel, _("exit")),
66                         "ok": (self.ok, "select AP"),
67                 })
68
69                 self["ColorActions"] = HelpableActionMap(self, "ColorActions",
70                 {
71                         "red": (self.cancel, _("exit")),
72                         "green": (self.ok, "select AP"),
73                         "blue": (self.startWlanConfig, "Edit Wireless settings"),
74                 })
75
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")
89
90                 self.startupTimer = eTimer()
91                 self.startupTimer.callback.append(self.startup)
92
93                 self.activateIfaceTimer = eTimer()
94                 self.activateIfaceTimer.callback.append(self.activateIface)
95
96                 self.updateStatusTimer = eTimer()
97                 self.updateStatusTimer.callback.append(self.updateStatus)
98                 
99                 self.scanAplistTimer = eTimer()
100                 self.scanAplistTimer.callback.append(self.scanApList)
101                 
102                 self.onClose.append(self.__onClose)
103                 self.onShown.append(lambda: self.startupTimer.start(10, True))
104
105         def startup(self):
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)
110                 else:
111                         self.updateStatusTimer.start(10, True)
112
113         def activateIface(self):
114                 iWlan.activateIface()
115                 self.updateStatusTimer.start(10, True)
116
117         def updateStatus(self):
118                 self["Status"].setText(("Please wait for scanning AP..."))
119                 self.scanAplistTimer.stop()
120                 self.scanAplistTimer.start(10, True)
121
122         def updateAPList(self):
123                 self.updateStatusTimer.stop()
124                 self.updateStatusTimer.start(7000, True)
125
126         def left(self):
127                 self["aplist"].pageUp()
128                 self.displayApInfo()
129         
130         def right(self):
131                 self["aplist"].pageDown()
132                 self.displayApInfo()
133
134         def up(self):
135                 self["aplist"].up()
136                 self.displayApInfo()
137                 
138         def down(self):
139                 self["aplist"].down()
140                 self.displayApInfo()
141
142         def ok(self):
143                 essid = None
144                 if self["aplist"].getCurrent() is not None:
145                         essid = self["aplist"].getCurrent()[0]
146                 self.close(essid)
147
148         def cancel(self):
149                 self.close(None)
150
151         def startWlanConfig(self): # key blue
152                 if self["aplist"].getCurrent() is not None:
153                         essid = self["aplist"].getCurrent()[0]
154                         self.close(essid)
155
156         def scanApList(self):   
157                 self.apList = iWlan.getNetworkList()
158                 print "GET APLIST %d" % len(self.apList)
159                 old_aplist = self.setApList
160
161                 new_bssid_list = self.apList.keys()
162                 old_bssid_list = [x[1] for x in old_aplist]
163
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]
166
167                 for x in remove_bssid_list:
168                         self.setApList.remove(x)
169
170                 for bssid in add_bssid_list:
171                         essid = self.apList[bssid].get("ESSID", None)
172                         if essid is None:
173                                 global SHOW_HIDDEN_NETWORK
174                                 if SHOW_HIDDEN_NETWORK:
175                                         essid = "# Hidden Network"
176                                 else:
177                                         continue
178                         else:
179                                 essid = essid.strip('\x00').strip('\\x00')
180                                 if essid == "":
181                                         continue
182                         self.setApList.append( (essid, bssid) )
183
184                 self.setApList = sorted(self.setApList, key=lambda x: int(self.apList[x[1]]['Quality'].split('/')[0]), reverse=True)
185
186                 self["aplist"].setList(self.setApList)
187                 self["Status"].setText(("%d AP detected" % len(self.setApList)))
188                 self.displayApInfo()
189                 self.updateAPList()
190
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])
197                                 else:
198                                         value = "None"
199                                 self[key].setText(( "%s:  %s" % (key, value) ))
200
201         def __onClose(self):
202                 iWlan.deActivateIface()
203                 iWlan.setInterface()
204
205 class WlanStatus(Screen):
206         skin =  """
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" />
209
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" />
212                 </screen>
213                 """
214         def __init__(self, session, iface):
215                 Screen.__init__(self,session)
216                 self.skinName = "Wlanstatus"
217                 self.session = session
218                 self.iface = iface
219
220                 self["actions"] = ActionMap(["ShortcutActions", "SetupActions"],
221                 {
222                         "cancel": self.close,
223                         "ok": self.close,
224                         "red": self.close,
225                 }, -1)
226
227                 self["status"] = StaticText(_("Reading..."))
228                 self["key_red"] = StaticText(_("Close"))
229
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))
236
237         def startup(self):
238                 self.update()
239
240         def __onClose(self):
241                 self.updateTimer.stop()
242                 iStatus.stopWlanConsole()
243
244         def update(self):
245                 self.updateTimer.stop()
246                 iStatus.getDataForInterface(self.iface, self.getInfoCB)
247                 self.updateTimer.start(5000, True)
248
249         def getInfoCB(self, retval, data):
250                 #print "[getInfoCB] ", data
251                 _text = ""
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"]
259                         
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")
266                 else:
267                         _text += "No data\n"
268
269                 self["status"].setText(_text)
270
271 def getConfigStrings(iface):
272         contents = ''
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'
280         else:
281                 ws = wpaSupplicant()
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"
285
286         #print "[getConfigStrings] : ", contents
287         return contents
288
289 def Plugins(**kwargs):
290         fnc = {}
291         fnc ["ifaceSupported"] = lambda iface: iNetwork.isWirelessInterface(iface) or None
292         fnc ["configStrings"] = getConfigStrings
293         fnc ["WlanPluginEntry"] = None
294
295         return PluginDescriptor(name=_("Wireless LAN Setup"), description="Wireless LAN Setup", where = [PluginDescriptor.WHERE_NETWORKSETUP], fnc = fnc)