* fix missing Makefile in configure.ac
[vuplus_dvbapp-plugin] / wirelesslan / src / Wlan.py
1 from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER
2
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
7
8 from pythonwifi import iwlibs
9
10 import os, string
11
12
13 list = []
14 list.append(_("WEP"))
15 list.append(_("WPA"))
16 list.append(_("WPA2"))
17
18 config.plugins = ConfigSubsection()
19 config.plugins.wlan = ConfigSubsection()
20
21
22 config.plugins.wlan.enabled = ConfigEnableDisable(default = False)
23 config.plugins.wlan.interface = ConfigText(default = "wlan0", fixed_size = False)
24 config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False))
25
26 config.plugins.wlan.encryption = ConfigSubsection()
27
28 config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False))
29 config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA")))
30 config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False))
31
32 class WlanList(HTMLComponent, GUIComponent):
33         
34         def __init__(self, session):
35                 
36                 GUIComponent.__init__(self)
37
38                 a = ''; b = ''
39                 for i in range(0, 255):
40                     a = a + chr(i)
41                     if i < 32 or i > 127:
42                         b = b + ' '
43                     else:
44                         b = b + chr(i)
45                 
46                 self.asciitrans = string.maketrans(a, b)
47                 
48                 self.aps = None
49                 self.l = None
50                 self.l = eListboxPythonMultiContent()
51                 
52                 self.l.setFont(0, gFont("Regular", 32))
53                 self.l.setFont(1, gFont("Regular", 18))
54                 self.l.setFont(2, gFont("Regular", 16))
55                 self.l.setBuildFunc(self.buildWlanListEntry)            
56                                 
57                 self.reload()
58         
59         def asciify(self, str):
60                 return str.translate(self.asciitrans)
61
62         def getNetworkList(self):
63                 iwifaces = None
64                 try:
65                         iwifaces = iwlibs.getNICnames()
66                 except:
67                         iwifaces = None
68                         "[Wlan.py] No Wireless Networkcards could be found"
69                 
70                 if iwifaces:
71         
72                         for iface in iwlibs.getNICnames():
73         
74                                 ifobj = iwlibs.Wireless(iface) # a Wireless NIC Object
75
76                                 #Association mappings
77                                 stats, quality, discard, missed_beacon = ifobj.getStatistics()
78                                 snr = quality.signallevel - quality.noiselevel
79                                 
80                                 try:
81                                         scanresults = ifobj.scan()
82                                 except:
83                                         scanresults = None
84                                         print "[Wlan.py] No Wireless Networks could be found"
85                                 
86                                 if scanresults is not None:
87                                         self.aps = {}
88                                         for result in scanresults:
89                                                 
90                                                 bssid = result.bssid
91
92                                                 encryption = map(lambda x: hex(ord(x)), result.encode)
93
94                                                 if encryption[-1] == "0x8":
95                                                         encryption = True
96                                                 else:
97                                                         encryption = False
98
99                                                 extra = []
100                                                 for element in result.custom:
101                                                         element = element.encode()
102                                                         extra.append( string.strip(self.asciify(element)) )
103
104                                                 self.aps[bssid] = {
105                                                         'active' : True,
106                                                         'bssid': result.bssid,
107                                                         'channel': result.frequency.getChannel(result.frequency.getFrequency(), result.range),
108                                                         'encrypted': encryption,
109                                                         'essid': string.strip(self.asciify(result.essid)),
110                                                         'iface': iface,
111                                                         'maxrate' : result.rate[-1],
112                                                         'noise' : result.quality.getNoiselevel(),
113                                                         'quality' : result.quality.quality,
114                                                         'signal' : result.quality.getSignallevel(),
115                                                         'custom' : extra,
116                                                 }
117                 
118         
119         def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate):                                                                                                 
120                 
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) )
127                 return res
128                         
129         def reload(self):
130                 self.getNetworkList()
131                 list = []
132                 if self.aps is not None:
133                         print "[Wlan.py] got Accespoints!"
134                         for ap in self.aps:
135                                 a = self.aps[ap]
136                                 if a['active']:
137                                         list.append((a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate']))
138                 
139                 self.l.setList([])
140                 self.l.setList(list)
141                         
142         GUI_WIDGET = eListbox
143
144         def getCurrent(self):
145                 return self.l.getCurrentSelection()
146         
147         def postWidgetCreate(self, instance):
148                 instance.setContent(self.l)
149                 instance.setItemHeight(60)
150
151 class wpaSupplicant:
152         def __init__(self):
153                 pass
154                 
155         def writeConfig(self):  
156                 if config.plugins.wlan.enabled.value:
157                         
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
162
163                 
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')
171                         
172                         if encrypted:
173                                                         
174                                 if encryption == 'WPA' or encryption == 'WPA2':
175                                         fp.write('\tkey_mgmt=WPA-PSK\n')
176                                         
177                                         if encryption == 'WPA':
178                                                 fp.write('\tproto=WPA\n')
179                                                 fp.write('\tpairwise=TKIP\n')
180                                         else:
181                                                 fp.write('\tproto=WPA RSN\n')
182                                                 fp.write('\tpairwise=CCMP TKIP\n')
183                                         
184                                         fp.write('\tpsk="'+psk+'"\n')
185                                 
186                                 elif encryption == 'WEP':
187                                         fp.write('\tkey_mgmt=NONE\n')
188                                         fp.write('\twep_key0="'+psk+'"\n')
189                         
190                         fp.write('}')   
191                         fp.close()
192                                         
193                         
194         def loadConfig(self):
195
196                 try:
197                         #parse the wpasupplicant configfile
198                         fp = file('/etc/wpa_supplicant.conf', 'r')
199                         supplicant = fp.readlines()
200                         fp.close()
201                         
202                         for s in supplicant:
203                         
204                                 split = s.strip().split('=')
205                                 if split[0] == 'ssid':
206                                         print "[Wlan.py] Got SSID "+split[1][1:-1]
207                                         config.plugins.wlan.enabled.value = True
208                                         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]
214                                 elif split[0] == 'psk':
215                                         config.plugins.wlan.encryption.psk.value = split[1][1:-1]
216                                         print "[Wlan.py] Got PSK "+split[1][1:-1]
217                                 else:
218                                         pass
219                 except:
220                         print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
221         
222         def restart(self, iface):
223                 import os
224                 os.system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
225                 os.system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")
226         
227 def InitNetwork():
228         config.network = ConfigSubsection()
229         config.network.dhcp = NoSave(ConfigYesNo(default=True))
230         config.network.ip = NoSave(ConfigIP(default=[192,168,1,1]))
231         config.network.netmask = NoSave(ConfigIP(default=[255,255,255,0]))
232         config.network.gateway = NoSave(ConfigIP(default=[192,168,1,3]))
233         config.network.dns = NoSave(ConfigIP(default=[192,168,1,3]))
234         config.network.mac = NoSave(ConfigMAC(default=[00,11,22,33,44,55]))