update network setup
[enigma2.git] / lib / python / Components / Network.py
index 06e81ea..3abfc34 100644 (file)
@@ -1,7 +1,7 @@
-import os
-import re
+from os import system, popen
+from re import compile as re_compile
 from socket import *
-from enigma import eTimer, eConsoleAppContainer
+from enigma import eConsoleAppContainer
 
 class Network:
        def __init__(self):
@@ -13,7 +13,7 @@ class Network:
                self.container = eConsoleAppContainer()
 
        def getInterfaces(self):
-               devicesPattern = re.compile('[a-z]+[0-9]+')
+               devicesPattern = re_compile('[a-z]+[0-9]+')
 
                fp = file('/proc/net/dev', 'r')
                result = fp.readlines()
@@ -23,8 +23,8 @@ class Network:
                                device = devicesPattern.search(line).group()
                                self.ifaces[device] = self.getDataForInterface(device)
                                # Show only UP Interfaces in E2
-                               if self.getAdapterAttribute(device, 'up') is False:
-                                       del self.ifaces[device]
+                               #if self.getAdapterAttribute(device, 'up') is False:
+                               #       del self.ifaces[device]
                        except AttributeError:
                                pass
 
@@ -55,14 +55,14 @@ class Network:
        def getDataForInterface(self, iface):
                #ipRegexp = '[0-9]{1,2,3}\.[0-9]{1,2,3}\.[0-9]{1,2,3}\.[0-9]{1,2,3}'
                ipRegexp = '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
-               ipLinePattern = re.compile('inet addr:' + ipRegexp)
-               netmaskLinePattern = re.compile('Mask:' + ipRegexp)
-               bcastLinePattern = re.compile('Bcast:' + ipRegexp)
-               ipPattern = re.compile(ipRegexp)
-               upPattern = re.compile('UP ')
-               macPattern = re.compile('[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}')
-
-               fp = os.popen("ifconfig " + iface)
+               ipLinePattern = re_compile('inet addr:' + ipRegexp)
+               netmaskLinePattern = re_compile('Mask:' + ipRegexp)
+               bcastLinePattern = re_compile('Bcast:' + ipRegexp)
+               ipPattern = re_compile(ipRegexp)
+               upPattern = re_compile('UP ')
+               macPattern = re_compile('[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}\:[0-9]{2}')
+               
+               fp = popen("ifconfig " + iface)
                result = fp.readlines()
                fp.close()
                data = { 'up': False, 'dhcp': False }
@@ -87,8 +87,8 @@ class Network:
                        data['ip'] = [192, 168, 1, 2]
                        data['netmask'] = [255, 255, 255, 0]
                        data['gateway'] = [192, 168, 1, 1]
-
-               fp = os.popen("route -n | grep  " + iface)
+                       
+               fp = popen("route -n | grep  " + iface)
                result = fp.readlines()
                fp.close()
                for line in result:
@@ -104,24 +104,22 @@ class Network:
                fp.write("# automatically generated by enigma 2\n# do NOT change manually!\n\n")
                fp.write("auto lo\n")
                fp.write("iface lo inet loopback\n\n")
-
                for ifacename, iface in self.ifaces.items():
                        if iface['up'] == True:
                                fp.write("auto " + ifacename + "\n")
-                       if iface['dhcp'] == True:
-                               fp.write("iface "+ ifacename +" inet dhcp\n")
-                       else:
-                               fp.write("iface "+ ifacename +" inet static\n")
-                               if iface.has_key('ip'):
-                                       print tuple(iface['ip'])
-                                       fp.write("      address %d.%d.%d.%d\n" % tuple(iface['ip']))
-                                       fp.write("      netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
-                                       if iface.has_key('gateway'):
-                                               fp.write("      gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
-
-                       if iface.has_key("configStrings"):
-                               fp.write("\n" + iface["configStrings"] + "\n")
-                       fp.write("\n")
+                               if iface['dhcp'] == True:
+                                       fp.write("iface "+ ifacename +" inet dhcp\n")
+                               if iface['dhcp'] == False:
+                                       fp.write("iface "+ ifacename +" inet static\n")
+                                       if iface.has_key('ip'):
+                                               print tuple(iface['ip'])
+                                               fp.write("      address %d.%d.%d.%d\n" % tuple(iface['ip']))
+                                               fp.write("      netmask %d.%d.%d.%d\n" % tuple(iface['netmask']))
+                                               if iface.has_key('gateway'):
+                                                       fp.write("      gateway %d.%d.%d.%d\n" % tuple(iface['gateway']))
+                               if iface.has_key("configStrings"):
+                                       fp.write("\n" + iface["configStrings"] + "\n")
+                               fp.write("\n")                          
                fp.close()
                self.writeNameserverConfig()
 
@@ -171,8 +169,8 @@ class Network:
 
        def loadNameserverConfig(self):
                ipRegexp = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
-               nameserverPattern = re.compile("nameserver +" + ipRegexp)
-               ipPattern = re.compile(ipRegexp)
+               nameserverPattern = re_compile("nameserver +" + ipRegexp)
+               ipPattern = re_compile(ipRegexp)
 
                resolv = []
                try:
@@ -193,13 +191,13 @@ class Network:
 
        def deactivateNetworkConfig(self):
                for iface in self.ifaces.keys():
-                       os.system("ip addr flush " + iface)
-               os.system("/etc/init.d/networking stop")
-               os.system("killall -9 udhcpc")
-               os.system("rm /var/run/udhcpc*")
+                       system("ip addr flush " + iface)
+               system("/etc/init.d/networking stop")
+               system("killall -9 udhcpc")
+               system("rm /var/run/udhcpc*")
 
        def activateNetworkConfig(self):
-               os.system("/etc/init.d/networking start")
+               system("/etc/init.d/networking start")
                self.getInterfaces()
 
        def getNumberOfAdapters(self):
@@ -209,7 +207,8 @@ class Network:
                # maybe this needs to be replaced by an external list.
                friendlyNames = {
                        "eth0": _("Integrated Ethernet"),
-                       "wlan0": _("Wireless")
+                       "wlan0": _("Wireless"),
+                       "ath0": _("Integrated Wireless")
                }
                return friendlyNames.get(x, x) # when we have no friendly name, use adapter name
 
@@ -266,7 +265,10 @@ class Network:
                if mode == 'wlan':
                        fp.write("auto wlan0\n")
                        fp.write("iface wlan0 inet dhcp\n")
-               else:
+               if mode == 'wlan-mpci':
+                       fp.write("auto ath0\n")
+                       fp.write("iface eth0 inet dhcp\n")
+               if mode == 'lan':
                        fp.write("auto eth0\n")
                        fp.write("iface eth0 inet dhcp\n")
                fp.write("\n")
@@ -276,21 +278,28 @@ class Network:
                self.deactivateNetworkConfig()
                self.writeDefaultNetworkConfig(mode)
                if mode == 'wlan':
-                       os.system("ifconfig eth0 down")
-                       os.system("ifconfig wlan0 up")
-               else:
-                       os.system("ifconfig eth0 up")
-                       os.system("ifconfig wlan0 down")
+                       system("ifconfig eth0 down")
+                       system("ifconfig ath0 down")
+                       system("ifconfig wlan0 up")
+               if mode == 'wlan-mpci':
+                       system("ifconfig eth0 down")
+                       system("ifconfig wlan0 down")
+                       system("ifconfig ath0 up")              
+               if mode == 'lan':                       
+                       system("ifconfig eth0 up")
+                       system("ifconfig wlan0 down")
+                       system("ifconfig ath0 down")
+               self.getInterfaces()    
 
        def checkNetworkState(self):
                ok_counter = 0
-               ret1=os.system("ping -c 1 www.dream-multimedia-tv.de")
+               ret1=system("ping -c 1 www.dream-multimedia-tv.de")
                if ret1 != 0:
                        ok_counter = ok_counter + 1
-               ret2=os.system("ping -c 1 www.heise.de")
+               ret2=system("ping -c 1 www.heise.de")
                if ret2 != 0:
                        ok_counter = ok_counter + 1
-               ret3=os.system("ping -c 1 www.google.de")
+               ret3=system("ping -c 1 www.google.de")
                if ret2 != 0:
                        ok_counter = ok_counter + 1             
                if ok_counter <= 2:
@@ -314,21 +323,25 @@ class Network:
                self.container.dataAvail.get().remove(self.dataAvail)
 
        def checkforInterface(self,iface):
-               ret=os.system("ifconfig " + iface + " up")
-               if ret == 0:
+               if self.getAdapterAttribute(iface, 'up') is True:
                        return True
                else:
-                       return False
-
+                       ret=system("ifconfig " + iface + " up")
+                       system("ifconfig " + iface + " down")
+                       if ret == 0:
+                               return True
+                       else:
+                               return False
+               
        def checkDNSLookup(self):
                ok_counter = 0
-               ret1=os.system("nslookup www.dream-multimedia-tv.de")
+               ret1=system("nslookup www.dream-multimedia-tv.de")
                if ret1 != 0:
                        ok_counter = ok_counter + 1
-               ret2=os.system("nslookup www.heise.de")
+               ret2=system("nslookup www.heise.de")
                if ret2 != 0:
                        ok_counter = ok_counter + 1
-               ret3=os.system("nslookup www.google.de")
+               ret3=system("nslookup www.google.de")
                if ret2 != 0:
                        ok_counter = ok_counter + 1             
                if ok_counter <= 2:
@@ -336,6 +349,26 @@ class Network:
                else:
                        return False
                
+       def deactivateInterface(self,iface):
+               system("ifconfig " + iface + " down")
+               
+       def detectWlanModule(self):
+               self.wlanmodule = None
+               rt73_dir = "/sys/bus/usb/drivers/rt73/"
+               zd1211b_dir = "/sys/bus/usb/drivers/zd1211b/"
+               if os_path.exists(rt73_dir):
+                       files = []
+                       files = listdir(rt73_dir)
+                       if len(files) >= 1:
+                               self.wlanmodule = 'ralink'
+               if os_path.exists(zd1211b_dir):
+                       files = []
+                       files = listdir(zd1211b_dir)
+                       if len(files) != 0:
+                               self.wlanmodule = 'zydas'
+               return self.wlanmodule
+       
+       
 iNetwork = Network()
 
 def InitNetwork():