* fix makefile
[enigma2-plugins.git] / 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 = {}
49                 
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                 
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:
87                                         for result in scanresults:
88                                                 
89                                                         bssid = result.bssid
90         
91                                                         encryption = map(lambda x: hex(ord(x)), result.encode)
92         
93                                                         if encryption[-1] == "0x8":
94                                                                 encryption = True
95                                                         else:
96                                                                 encryption = False
97         
98                                                         extra = []
99                                                         for element in result.custom:
100                                                                 element = element.encode()
101                                                                 extra.append( string.strip(self.asciify(element)) )
102         
103                                                         self.aps[bssid] = {
104                                                                 'active' : True,
105                                                                 'bssid': result.bssid,
106                                                                 'channel': result.frequency.getChannel(result.frequency.getFrequency(), result.range),
107                                                                 'encrypted': encryption,
108                                                                 'essid': string.strip(self.asciify(result.essid)),
109                                                                 'iface': iface,
110                                                                 'maxrate' : result.rate[-1],
111                                                                 'noise' : result.quality.getNoiselevel(),
112                                                                 'quality' : result.quality.quality,
113                                                                 'signal' : result.quality.getSignallevel(),
114                                                                 'custom' : extra,
115                                                         }
116                 
117         
118         def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate):                                                                                                 
119                 
120                 res = [ (essid, encrypted, iface) ]
121                 e = encrypted and _("Yes") or _("No")
122                 res.append( MultiContentEntryText(pos=(0, 0), size=(570, 35), font=0, flags=RT_HALIGN_LEFT, text=essid) )
123                 res.append( MultiContentEntryText(pos=(0, 40), size=(180, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Max. Bitrate: ")+maxrate) )
124                 res.append( MultiContentEntryText(pos=(190, 40), size=(180, 20), font=1, flags=RT_HALIGN_CENTER, text=_("Encrypted: ")+e) )
125                 res.append( MultiContentEntryText(pos=(380, 40), size=(190, 20), font=1, flags=RT_HALIGN_RIGHT, text=_("Interface: ")+iface) )
126                 return res
127                         
128         def reload(self):
129                 self.getNetworkList()
130                 
131                 if self.aps:
132                         for ap in self.aps:
133                                 list = []
134                                 a = self.aps[ap]
135                                 if a['active']:
136                                         list.append((a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate']))
137                         self.l.setList(list)
138                 
139                         
140         GUI_WIDGET = eListbox
141
142         def getCurrent(self):
143                 return self.l.getCurrentSelection()
144         
145         def postWidgetCreate(self, instance):
146                 instance.setContent(self.l)
147                 instance.setItemHeight(60)
148
149 class wpaSupplicant:
150         def __init__(self):
151                 pass
152                 
153         def writeConfig(self):  
154                 if config.plugins.wlan.enabled.value:
155                         
156                         essid = config.plugins.wlan.essid.value
157                         encrypted = config.plugins.wlan.encryption.enabled.value
158                         encryption = config.plugins.wlan.encryption.type.value
159                         psk = config.plugins.wlan.encryption.psk.value
160
161                 
162                         fp = file('/etc/wpa_supplicant.conf', 'w')
163                         fp.write('#WPA Supplicant Configuration by enigma2\n\n')
164                         fp.write('ctrl_interface=/var/run/wpa_supplicant\n')
165                         fp.write('ctrl_interface_group=0\n')
166                         fp.write('network={\n')
167                         fp.write('\tssid="'+essid+'"\n')
168                         fp.write('\tscan_ssid=1\n')
169                         
170                         if encrypted:
171                                                         
172                                 if encryption == 'WPA' or encryption == 'WPA2':
173                                         fp.write('\tkey_mgmt=WPA-PSK\n')
174                                         
175                                         if encryption == 'WPA':
176                                                 fp.write('\tproto=WPA\n')
177                                                 fp.write('\tpairwise=TKIP\n')
178                                         else:
179                                                 fp.write('\tproto=WPA RSN\n')
180                                                 fp.write('\tpairwise=CCMP TKIP\n')
181                                         
182                                         fp.write('\tpsk="'+psk+'"\n')
183                                 
184                                 elif encryption == 'WEP':
185                                         fp.write('\tkey_mgmt=NONE\n')
186                                         fp.write('\twep_key0="'+psk+'"\n')
187                         
188                         fp.write('}')   
189                         fp.close()
190                                         
191                         
192         def loadConfig(self):
193
194                 try:
195                         #parse the wpasupplicant configfile
196                         fp = file('/etc/wpa_supplicant.conf', 'r')
197                         supplicant = fp.readlines()
198                         fp.close()
199                         
200                         for s in supplicant:
201                         
202                                 split = s.strip().split('=')
203                                 if split[0] == 'ssid':
204                                         print "[Wlan.py] Got SSID "+split[1][1:-1]
205                                         config.plugins.wlan.enabled.value = True
206                                         config.plugins.wlan.essid.value = split[1][1:-1]
207                                 elif split[0] == 'proto':
208                                         config.plugins.wlan.encryption.enabled.value = True
209                                         if split[1] == "WPA RSN" : split[1] = 'WPA2'
210                                         config.plugins.wlan.encryption.type.value = split[1]
211                                         print "[Wlan.py] Got Encryption "+split[1]
212                                 elif split[0] == 'psk':
213                                         config.plugins.wlan.encryption.psk.value = split[1][1:-1]
214                                         print "[Wlan.py] Got PSK "+split[1][1:-1]
215                                 else:
216                                         pass
217                 except:
218                         print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf"
219         
220         def restart(self, iface):
221                 import os
222                 os.system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant")
223                 os.system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf")
224         
225 def InitNetwork():
226         config.network = ConfigSubsection()
227         config.network.dhcp = NoSave(ConfigYesNo(default=True))
228         config.network.ip = NoSave(ConfigIP(default=[192,168,1,1]))
229         config.network.netmask = NoSave(ConfigIP(default=[255,255,255,0]))
230         config.network.gateway = NoSave(ConfigIP(default=[192,168,1,3]))
231         config.network.dns = NoSave(ConfigIP(default=[192,168,1,3]))
232         config.network.mac = NoSave(ConfigMAC(default=[00,11,22,33,44,55]))