From 4b53fef5099e42240db57ce118dd9aa570b7815d Mon Sep 17 00:00:00 2001 From: Stefan Pluecken Date: Tue, 20 Mar 2007 21:38:37 +0000 Subject: [PATCH] some fixes for the new network configuration adding Reichi's WirelessLan configuration plugin (not yet fully functional) --- configure.ac | 1 + lib/python/Plugins/SystemPlugins/Makefile.am | 2 +- .../Plugins/SystemPlugins/WirelessLan/Makefile.am | 0 .../Plugins/SystemPlugins/WirelessLan/Wlan.py | 227 +++++++++++++++++++++ .../Plugins/SystemPlugins/WirelessLan/__init__.py | 0 .../Plugins/SystemPlugins/WirelessLan/key-blue.png | 0 .../SystemPlugins/WirelessLan/key-green.png | 0 .../Plugins/SystemPlugins/WirelessLan/key-red.png | 0 .../SystemPlugins/WirelessLan/key-yellow.png | 0 .../Plugins/SystemPlugins/WirelessLan/plugin.py | 191 +++++++++++++++++ lib/python/Screens/NetworkSetup.py | 5 +- 11 files changed, 422 insertions(+), 4 deletions(-) create mode 100644 lib/python/Plugins/SystemPlugins/WirelessLan/Makefile.am create mode 100644 lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py create mode 100644 lib/python/Plugins/SystemPlugins/WirelessLan/__init__.py create mode 100644 lib/python/Plugins/SystemPlugins/WirelessLan/key-blue.png create mode 100644 lib/python/Plugins/SystemPlugins/WirelessLan/key-green.png create mode 100644 lib/python/Plugins/SystemPlugins/WirelessLan/key-red.png create mode 100644 lib/python/Plugins/SystemPlugins/WirelessLan/key-yellow.png create mode 100644 lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py diff --git a/configure.ac b/configure.ac index ef22a6b..6198f55 100644 --- a/configure.ac +++ b/configure.ac @@ -69,6 +69,7 @@ lib/python/Components/Sources/Makefile lib/python/Screens/Makefile lib/python/Plugins/Makefile lib/python/Plugins/SystemPlugins/Makefile +lib/python/Plugins/SystemPlugins/WirelessLan/Makefile lib/python/Plugins/SystemPlugins/SoftwareUpdate/Makefile lib/python/Plugins/SystemPlugins/FrontprocessorUpgrade/Makefile lib/python/Plugins/SystemPlugins/PositionerSetup/Makefile diff --git a/lib/python/Plugins/SystemPlugins/Makefile.am b/lib/python/Plugins/SystemPlugins/Makefile.am index 80d8101..c9e2782 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 0000000..e69de29 diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py b/lib/python/Plugins/SystemPlugins/WirelessLan/Wlan.py new file mode 100644 index 0000000..5924c1e --- /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 0000000..e69de29 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 0000000..e69de29 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 0000000..e69de29 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 0000000..e69de29 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 0000000..e69de29 diff --git a/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py b/lib/python/Plugins/SystemPlugins/WirelessLan/plugin.py new file mode 100644 index 0000000..45c6944 --- /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 = """ + + + + + + + + + + + + + + """ + 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 = """ + + + + + + """ + + 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 diff --git a/lib/python/Screens/NetworkSetup.py b/lib/python/Screens/NetworkSetup.py index 94d9fcb..a2b15e9 100644 --- a/lib/python/Screens/NetworkSetup.py +++ b/lib/python/Screens/NetworkSetup.py @@ -137,6 +137,7 @@ class AdapterSetup(Screen, ConfigListScreen): self.list.append(getConfigListEntry(_('Gateway'), self.gatewayConfigEntry)) self.extended = None + self.extendedSetup = None for p in plugins.getPlugins(PluginDescriptor.WHERE_NETWORKSETUP): callFnc = p.__call__["ifaceSupported"](self.iface) if callFnc is not None: @@ -147,7 +148,7 @@ class AdapterSetup(Screen, ConfigListScreen): else: self.configStrings = None - if p.__call__.has_key(menuEntryName): + if p.__call__.has_key("menuEntryName"): menuEntryName = p.__call__["menuEntryName"](self.iface) else: menuEntryName = _('Extended Setup...') @@ -183,8 +184,6 @@ class AdapterSetup(Screen, ConfigListScreen): else: iNetwork.removeAdapterAttribute(self.iface, "gateway") - print "self.extended:", self.extended - print "self.configStrings:", self.configStrings if self.extended is not None and self.configStrings is not None: iNetwork.setAdapterAttribute(self.iface, "configStrings", self.configStrings(self.iface)) -- 2.7.4