diff options
| author | Stefan Pluecken <stefan.pluecken@multimedia-labs.de> | 2007-03-20 21:38:37 +0000 |
|---|---|---|
| committer | Stefan Pluecken <stefan.pluecken@multimedia-labs.de> | 2007-03-20 21:38:37 +0000 |
| commit | 4b53fef5099e42240db57ce118dd9aa570b7815d (patch) | |
| tree | f0afc726fc6c412da236f5e48de8f488df1ec08c /lib/python/Plugins | |
| parent | eba98a3843d8358cd66d3d8a31b08771899ce705 (diff) | |
| download | enigma2-4b53fef5099e42240db57ce118dd9aa570b7815d.tar.gz enigma2-4b53fef5099e42240db57ce118dd9aa570b7815d.zip | |
some fixes for the new network configuration
adding Reichi's WirelessLan configuration plugin (not yet fully functional)
Diffstat (limited to 'lib/python/Plugins')
9 files changed, 419 insertions, 1 deletions
diff --git a/lib/python/Plugins/SystemPlugins/Makefile.am b/lib/python/Plugins/SystemPlugins/Makefile.am index 80d8101a..c9e27827 100644 --- a/lib/python/Plugins/SystemPlugins/Makefile.am +++ b/lib/python/Plugins/SystemPlugins/Makefile.am @@ -1 +1 @@ -SUBDIRS = SoftwareUpdate FrontprocessorUpgrade PositionerSetup ConfigurationBackup Satfinder SkinSelector SatelliteEquipmentControl +SUBDIRS = SoftwareUpdate FrontprocessorUpgrade PositionerSetup ConfigurationBackup Satfinder SkinSelector SatelliteEquipmentControl WirelessLan diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/Makefile.am b/lib/python/Plugins/SystemPlugins/WirelessLan/Makefile.am new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/Makefile.am diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py b/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py new file mode 100644 index 00000000..5924c1e7 --- /dev/null +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py @@ -0,0 +1,227 @@ +from enigma import eListboxPythonMultiContent, eListbox, gFont, RT_HALIGN_LEFT, RT_HALIGN_RIGHT, RT_HALIGN_CENTER + +from Components.MultiContent import MultiContentEntryText +from Components.GUIComponent import GUIComponent +from Components.HTMLComponent import HTMLComponent +from Components.config import config, ConfigYesNo, ConfigIP, NoSave, ConfigSubsection, ConfigMAC, ConfigEnableDisable, ConfigText, ConfigSelection + +from pythonwifi import iwlibs + +import os, string + + +list = [] +list.append(_("WEP")) +list.append(_("WPA")) +list.append(_("WPA2")) + +config.plugins.wlan = ConfigSubsection() +config.plugins.wlan.essid = NoSave(ConfigText(default = "home", fixed_size = False)) + +config.plugins.wlan.encryption = ConfigSubsection() +config.plugins.wlan.encryption.enabled = NoSave(ConfigYesNo(default = False)) +config.plugins.wlan.encryption.type = NoSave(ConfigSelection(list, default = _("WPA"))) +config.plugins.wlan.encryption.psk = NoSave(ConfigText(default = "mysecurewlan", fixed_size = False)) + +class Wlan: + def __init__(self): + a = ''; b = '' + + for i in range(0, 255): + a = a + chr(i) + if i < 32 or i > 127: + b = b + ' ' + else: + b = b + chr(i) + + self.asciitrans = string.maketrans(a, b) + + def asciify(self, str): + return str.translate(self.asciitrans) + + def getWirelessInterfaces(self): + iwifaces = None + try: + iwifaces = iwlibs.getNICnames() + except: + iwifaces = None + "[Wlan.py] No Wireless Networkcards could be found" + + return iwifaces + + def getNetworkList(self, iface): + + ifobj = iwlibs.Wireless(iface) # a Wireless NIC Object + + #Association mappings + stats, quality, discard, missed_beacon = ifobj.getStatistics() + snr = quality.signallevel - quality.noiselevel + + try: + scanresults = ifobj.scan() + except: + scanresults = None + print "[Wlan.py] No Wireless Networks could be found" + + if scanresults is not None: + aps = {} + for result in scanresults: + + bssid = result.bssid + + encryption = map(lambda x: hex(ord(x)), result.encode) + + if encryption[-1] == "0x8": + encryption = True + else: + encryption = False + + extra = [] + for element in result.custom: + element = element.encode() + extra.append( string.strip(self.asciify(element)) ) + + aps[bssid] = { + 'active' : True, + 'bssid': result.bssid, + 'channel': result.frequency.getChannel(result.frequency.getFrequency(), result.range), + 'encrypted': encryption, + 'essid': string.strip(self.asciify(result.essid)), + 'iface': iface, + 'maxrate' : result.rate[-1], + 'noise' : result.quality.getNoiselevel(), + 'quality' : result.quality.quality, + 'signal' : result.quality.getSignallevel(), + 'custom' : extra, + } + + return aps + + +class WlanList(HTMLComponent, GUIComponent): + + def __init__(self, session, iface = 'wlan0'): + + GUIComponent.__init__(self) + self.w = Wlan() + self.iface = iface + + self.l = None + self.l = eListboxPythonMultiContent() + + self.l.setFont(0, gFont("Regular", 32)) + self.l.setFont(1, gFont("Regular", 18)) + self.l.setFont(2, gFont("Regular", 16)) + self.l.setBuildFunc(self.buildWlanListEntry) + + self.reload() + + def buildWlanListEntry(self, essid, bssid, encrypted, iface, maxrate): + + res = [ (essid, encrypted, iface) ] + e = encrypted and _("Yes") or _("No") + res.append( MultiContentEntryText(pos=(0, 0), size=(570, 35), font=0, flags=RT_HALIGN_LEFT, text=essid) ) + res.append( MultiContentEntryText(pos=(0, 40), size=(180, 20), font=1, flags=RT_HALIGN_LEFT, text=_("Max. Bitrate: ")+maxrate) ) + res.append( MultiContentEntryText(pos=(190, 40), size=(180, 20), font=1, flags=RT_HALIGN_CENTER, text=_("Encrypted: ")+e) ) + res.append( MultiContentEntryText(pos=(380, 40), size=(190, 20), font=1, flags=RT_HALIGN_RIGHT, text=_("Interface: ")+iface) ) + return res + + def reload(self): + aps = self.w.getNetworkList(self.iface) + list = [] + if aps is not None: + print "[Wlan.py] got Accespoints!" + for ap in aps: + a = aps[ap] + if a['active']: + list.append((a['essid'], a['bssid'], a['encrypted'], a['iface'], a['maxrate'])) + + self.l.setList([]) + self.l.setList(list) + + GUI_WIDGET = eListbox + + def getCurrent(self): + return self.l.getCurrentSelection() + + def postWidgetCreate(self, instance): + instance.setContent(self.l) + instance.setItemHeight(60) + + +class wpaSupplicant: + def __init__(self): + pass + + def writeConfig(self): + + essid = config.plugins.wlan.essid.value + encrypted = config.plugins.wlan.encryption.enabled.value + encryption = config.plugins.wlan.encryption.type.value + psk = config.plugins.wlan.encryption.psk.value + + + fp = file('/etc/wpa_supplicant.conf', 'w') + fp.write('#WPA Supplicant Configuration by enigma2\n\n') + fp.write('ctrl_interface=/var/run/wpa_supplicant\n') + fp.write('ctrl_interface_group=0\n') + fp.write('network={\n') + fp.write('\tssid="'+essid+'"\n') + fp.write('\tscan_ssid=1\n') + + if encrypted: + + if encryption == 'WPA' or encryption == 'WPA2': + fp.write('\tkey_mgmt=WPA-PSK\n') + + if encryption == 'WPA': + fp.write('\tproto=WPA\n') + fp.write('\tpairwise=TKIP\n') + else: + fp.write('\tproto=WPA RSN\n') + fp.write('\tpairwise=CCMP TKIP\n') + + fp.write('\tpsk="'+psk+'"\n') + + elif encryption == 'WEP': + fp.write('\tkey_mgmt=NONE\n') + fp.write('\twep_key0="'+psk+'"\n') + + fp.write('}') + fp.close() + + + def loadConfig(self): + + try: + #parse the wpasupplicant configfile + fp = file('/etc/wpa_supplicant.conf', 'r') + supplicant = fp.readlines() + fp.close() + + for s in supplicant: + + split = s.strip().split('=') + if split[0] == 'ssid': + print "[Wlan.py] Got SSID "+split[1][1:-1] + config.plugins.wlan.essid.value = split[1][1:-1] + + elif split[0] == 'proto': + config.plugins.wlan.encryption.enabled.value = True + if split[1] == "WPA RSN" : split[1] = 'WPA2' + config.plugins.wlan.encryption.type.value = split[1] + print "[Wlan.py] Got Encryption "+split[1] + + elif split[0] == 'psk': + config.plugins.wlan.encryption.psk.value = split[1][1:-1] + print "[Wlan.py] Got PSK "+split[1][1:-1] + else: + pass + + except: + print "[Wlan.py] Error parsing /etc/wpa_supplicant.conf" + + def restart(self, iface): + import os + os.system("start-stop-daemon -K -x /usr/sbin/wpa_supplicant") + os.system("start-stop-daemon -S -x /usr/sbin/wpa_supplicant -- -B -i"+iface+" -c/etc/wpa_supplicant.conf") diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/__init__.py b/lib/python/Plugins/SystemPlugins/WirelessLan/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/__init__.py diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/key-blue.png b/lib/python/Plugins/SystemPlugins/WirelessLan/key-blue.png new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/key-blue.png diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/key-green.png b/lib/python/Plugins/SystemPlugins/WirelessLan/key-green.png new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/key-green.png diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/key-red.png b/lib/python/Plugins/SystemPlugins/WirelessLan/key-red.png new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/key-red.png diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/key-yellow.png b/lib/python/Plugins/SystemPlugins/WirelessLan/key-yellow.png new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/key-yellow.png diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py b/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py new file mode 100644 index 00000000..45c69447 --- /dev/null +++ b/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py @@ -0,0 +1,191 @@ +from Screens.Screen import Screen +from Screens.MessageBox import MessageBox + +from Components.ActionMap import ActionMap, NumberActionMap +from Components.Pixmap import Pixmap +from Components.Label import Label +from Components.GUIComponent import * +from Components.MenuList import MenuList +from Components.MultiContent import MultiContentEntryText + + +from Components.config import config, getConfigListEntry +from Components.ConfigList import ConfigList, ConfigListScreen +from Components.Network import Network + +from Plugins.Plugin import PluginDescriptor + +from Wlan import Wlan, WlanList, wpaSupplicant + +plugin_path = "/usr/lib/enigma2/python/Plugins/SystemPlugins/WirelessLAN" + +class WlanSelection(Screen): + skin = """ + <screen position="70,138" size="610,300" title="Choose a Wireless Network" > + <widget name="list" position="10,10" size="580,200" scrollbarMode="showOnDemand" /> + + <widget name="cancel" position="10,255" size="140,40" pixmap="~/key-red.png" zPosition="1" transparent="1" alphatest="on" /> + <widget name="select" position="160,255" size="140,40" pixmap="~/key-green.png" zPosition="1" transparent="1" alphatest="on" /> + <widget name="rescan" position="310,255" size="140,40" pixmap="~/key-yellow.png" zPosition="1" transparent="1" alphatest="on" /> + <widget name="skip" position="460,255" size="140,40" pixmap="~/key-blue.png" zPosition="1" transparent="1" alphatest="on" /> + + <widget name="canceltext" position="10,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" /> + <widget name="selecttext" position="160,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" /> + <widget name="rescantext" position="310,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" /> + <widget name="skiptext" position="460,255" size="140,40" valign="center" halign="center" zPosition="2" font="Regular;20" transparent="1" foregroundColor="#FFFFFF" /> + </screen> + """ + def __init__(self, session, args = None): + + self.skin = WlanSelection.skin + self.session = session + Screen.__init__(self, session) + + self.list = [] + + self["list"] = WlanList(self.session) + self.skin_path = plugin_path + + self["cancel"] = Pixmap() + self["select"] = Pixmap() + self["rescan"] = Pixmap() + self["skip"] = Pixmap() + + + self["canceltext"] = Label(_("Cancel")) + self["selecttext"] = Label(_("Select")) + self["rescantext"] = Label(_("Rescan")) + self["skiptext"] = Label(_("Skip")) + + self["actions"] = NumberActionMap(["WizardActions", "InputActions", "EPGSelectActions"], + { + "ok": self.select, + "back": self.exit, +# "up": self.up, +# "down": self.down, + }, -1) + + self["shortcuts"] = ActionMap(["ShortcutActions"], + { + "red": self.exit, + "green": self.select, + "yellow": self.rescan, + "blue": self.skip, + }) + + def select(self): + cur = self["list"].getCurrent() + if cur: + ret = (self.session, cur) + else: + ret = (self.session, None) + self.close(ret) + + def rescan(self): + self["list"].reload() + + def skip(self): + self.close( (self.session, None) ) + + def exit(self): + self.close( (None ,) ) + +class WlanConfiguration(ConfigListScreen, Screen): + skin = """ + <screen position="76,138" size="600,300" title="Wireless Network Configuration" > + <widget name="interface" position="10,10" size="580,30" font="Regular;24" valign="center" /> + <widget name="config" position="10,60" size="580,150" scrollbarMode="showOnDemand" /> + <widget name="introduction" position="100,260" size="400,30" font="Regular;23" valign="center" halign="center" /> + </screen> + """ + + def __init__(self, session, essid = None, encrypted = False, iface = "wlan0"): + + Screen.__init__(self, session) + self.skin = WlanConfiguration.skin + + self.iface = iface + self.list = [] + self.ws = wpaSupplicant() + + self["introduction"] = Label(_("Press OK to activate the settings.")) + self["interface"] = Label(_("Interface: ")+self.iface) + + if essid is None: + self.ws.loadConfig() + + else: + config.plugins.wlan.essid.value = essid + config.plugins.wlan.encryption.enabled.value = True + + self["actions"] = ActionMap(["SetupActions"], + { + "ok": self.ok, + "cancel": self.cancel, + }, -2) + + ConfigListScreen.__init__(self, self.list) + self.createSetup() + + def createSetup(self): + + self.list = [ ] + + self.list.append(getConfigListEntry(_("Network SSID"), config.plugins.wlan.essid)) + self.list.append(getConfigListEntry(_("Encryption"), config.plugins.wlan.encryption.enabled)) + + if config.plugins.wlan.encryption.enabled.value: + self.list.append(getConfigListEntry(_("Encryption Type"), config.plugins.wlan.encryption.type)) + self.list.append(getConfigListEntry(_("Encryption Key"), config.plugins.wlan.encryption.psk)) + + self["config"].list = self.list + self["config"].l.setList(self.list) + + def keyLeft(self): + ConfigListScreen.keyLeft(self) + self.createSetup() + + def keyRight(self): + ConfigListScreen.keyRight(self) + self.createSetup() + + def ok(self): + self.ws.writeConfig() + self.ws.restart(self.iface) + self.close() + + def cancel(self): + self.close() + +def EntryChosen(parms): + if parms[0]: + session = parms[0] + if parms[1] is not None: + val = parms[1] + essid = val[0] + encrypted = val[2] + iface = val[3] + session.open(WlanConfiguration, essid, encrypted, iface) + else: + session.open(WlanConfiguration) + +def WlanSelectionMain(session, iface): + session.openWithCallback(EntryChosen, WlanSelection) + +def WlanConfigurationMain(session, **kwargs): + session.open(WlanConfiguration) + +def callFunction(iface): + w = Wlan() + + if iface in w.getWirelessInterfaces(): + return WlanSelectionMain + else: + return None + +def configStrings(iface): + return "#Custom Configstring for "+iface + +def Plugins(**kwargs): + return PluginDescriptor(name=_("Wireless LAN"), description=_("Connect to a Wireless Network"), where = PluginDescriptor.WHERE_NETWORKSETUP, fnc={"ifaceSupported": callFunction, "configStrings": configStrings, "menuEntryName": lambda x: _("Wlan Configuartion Utility")}) +
\ No newline at end of file |
