fixes bug #436 (again)
[vuplus_dvbapp] / lib / python / Components / NimManager.py
index aa91593..3c96fbb 100644 (file)
@@ -1,4 +1,5 @@
 from Tools.HardwareInfo import HardwareInfo
+from Tools.BoundFunction import boundFunction
 
 from config import config, ConfigSubsection, ConfigSelection, ConfigFloat, \
        ConfigSatlist, ConfigYesNo, ConfigInteger, ConfigSubList, ConfigNothing, \
@@ -13,6 +14,7 @@ from enigma import eDVBSatelliteEquipmentControl as secClass, \
 
 from time import localtime, mktime
 from datetime import datetime
+from Tools.BoundFunction import boundFunction
 
 def getConfigSatlist(orbpos, satlist):
        default_orbpos = None
@@ -46,7 +48,7 @@ class SecConfigure:
                if self.equal.has_key(slotid):
                        for slot in self.equal[slotid]:
                                tunermask |= (1 << slot)
-               elif self.linked.has_key(slotid):
+               if self.linked.has_key(slotid):
                        for slot in self.linked[slotid]:
                                tunermask |= (1 << slot)
                sec.setLNBSatCR(-1)
@@ -262,7 +264,7 @@ class SecConfigure:
                                if self.equal.has_key(slotid):
                                        for slot in self.equal[slotid]:
                                                tunermask |= (1 << slot)
-                               elif self.linked.has_key(slotid):
+                               if self.linked.has_key(slotid):
                                        for slot in self.linked[slotid]:
                                                tunermask |= (1 << slot)
 
@@ -321,9 +323,9 @@ class SecConfigure:
                                elif dm == "1_2":
                                        sec.setDiSEqCMode(diseqcParam.V1_2)
 
-                               if self.satposdepends.has_key(slotid):
-                                       for slot in self.satposdepends[slotid]:
-                                               tunermask |= (1 << slot)
+                                       if self.satposdepends.has_key(slotid):
+                                               for slot in self.satposdepends[slotid]:
+                                                       tunermask |= (1 << slot)
 
                                if dm != "none":
                                        if currLnb.toneburst.value == "none":
@@ -444,7 +446,7 @@ class SecConfigure:
                self.update()
 
 class NIM(object):
-       def __init__(self, slot, type, description, has_outputs = True, internally_connectable = None):
+       def __init__(self, slot, type, description, has_outputs = True, internally_connectable = None, multi_type = {}):
                self.slot = slot
 
                if type not in ("DVB-S", "DVB-C", "DVB-T", "DVB-S2", None):
@@ -455,6 +457,7 @@ class NIM(object):
                self.description = description
                self.has_outputs = has_outputs
                self.internally_connectable = internally_connectable
+               self.multi_type = multi_type
 
        def isCompatible(self, what):
                compatible = {
@@ -466,6 +469,9 @@ class NIM(object):
                        }
                return what in compatible[self.type]
        
+       def getType(self):
+               return self.type
+       
        def connectableTo(self):
                connectable = {
                                "DVB-S": ("DVB-S", "DVB-S2"),
@@ -491,6 +497,13 @@ class NIM(object):
        
        def internallyConnectableTo(self):
                return self.internally_connectable
+       
+       def isMultiType(self):
+               return (len(self.multi_type) > 0)
+       
+       # returns dict {<slotid>: <type>}
+       def getMultiTypeList(self):
+               return self.multi_type
 
        slot_id = property(getSlotID)
 
@@ -554,6 +567,13 @@ class NimManager:
        def getSatDescription(self, pos):
                return self.satellites[pos]
 
+       def sortFunc(self, x):
+               orbpos = x[0]
+               if orbpos > 1800:
+                       return orbpos - 3600
+               else:
+                       return orbpos + 1800
+
        def readTransponders(self):
                # read initial networks from file. we only read files which we are interested in,
                # which means only these where a compatible tuner exists.
@@ -565,9 +585,10 @@ class NimManager:
                if self.hasNimType("DVB-S"):
                        print "Reading satellites.xml"
                        db.readSatellites(self.satList, self.satellites, self.transponders)
-#                      print "SATLIST", self.satList
-#                      print "SATS", self.satellites
-#                      print "TRANSPONDERS", self.transponders
+                       self.satList.sort(key = self.sortFunc) # sort by orbpos
+                       #print "SATLIST", self.satList
+                       #print "SATS", self.satellites
+                       #print "TRANSPONDERS", self.transponders
 
                if self.hasNimType("DVB-C"):
                        print "Reading cables.xml"
@@ -628,7 +649,15 @@ class NimManager:
                                entries[current_slot]["has_outputs"] = (input == "yes")
                        elif line.strip().startswith("Internally_Connectable:"):
                                input = int(line.strip()[len("Internally_Connectable:") + 1:])
-                               entries[current_slot]["internally_connectable"] = input 
+                               entries[current_slot]["internally_connectable"] = input
+                       elif  line.strip().startswith("Mode"):
+                               # "Mode 0: DVB-T" -> ["Mode 0", " DVB-T"]
+                               split = line.strip().split(":")
+                               # "Mode 0" -> ["Mode, "0"]
+                               split2 = split[0].split(" ")
+                               modes = entries[current_slot].get("multi_type", {})
+                               modes[split2[1]] = split[1].strip()
+                               entries[current_slot]["multi_type"] = modes
                        elif line.strip().startswith("empty"):
                                entries[current_slot]["type"] = None
                                entries[current_slot]["name"] = _("N/A")
@@ -642,12 +671,17 @@ class NimManager:
                                entry["has_outputs"] = True
                        if not (entry.has_key("internally_connectable")):
                                entry["internally_connectable"] = None
-                       self.nim_slots.append(NIM(slot = id, description = entry["name"], type = entry["type"], has_outputs = entry["has_outputs"], internally_connectable = entry["internally_connectable"]))
+                       if not (entry.has_key("multi_type")):
+                               entry["multi_type"] = {}
+                       self.nim_slots.append(NIM(slot = id, description = entry["name"], type = entry["type"], has_outputs = entry["has_outputs"], internally_connectable = entry["internally_connectable"], multi_type = entry["multi_type"]))
 
        def hasNimType(self, chktype):
                for slot in self.nim_slots:
                        if slot.isCompatible(chktype):
                                return True
+                       for type in slot.getMultiTypeList().values():
+                               if chktype == type:
+                                       return True
                return False
        
        def getNimType(self, slotid):
@@ -655,6 +689,9 @@ class NimManager:
        
        def getNimDescription(self, slotid):
                return self.nim_slots[slotid].friendly_full_description
+       
+       def getNimName(self, slotid):
+               return self.nim_slots[slotid].description
 
        def getNimListOfType(self, type, exception = -1):
                # returns a list of indexes for NIMs compatible to the given type, except for 'exception'
@@ -759,6 +796,22 @@ class NimManager:
 
        def getSatList(self):
                return self.satList
+       
+       # returns True if something is configured to be connected to this nim
+       # if slotid == -1, returns if something is connected to ANY nim
+       def somethingConnected(self, slotid = -1):
+               if (slotid == -1):
+                       connected = False
+                       for id in range(self.getSlotCount()):
+                               if self.somethingConnected(id):
+                                       connected = True
+                       return connected
+               else:
+                       nim = config.Nims[slotid]
+                       configMode = nim.configMode.value
+               
+                       if self.nim_slots[slotid].isCompatible("DVB-S") or self.nim_slots[slotid].isCompatible("DVB-T") or self.nim_slots[slotid].isCompatible("DVB-C"):
+                               return not (configMode == "nothing")            
 
        def getSatListForNim(self, slotid):
                list = []
@@ -875,7 +928,7 @@ def InitSecParams():
        x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MEASURE_IDLE_INPUTPOWER, configElement.value))
        config.sec.delay_after_voltage_change_before_measure_idle_inputpower = x
 
-       x = ConfigInteger(default=750, limits = (0, 9999))
+       x = ConfigInteger(default=900, limits = (0, 9999))
        x.addNotifier(lambda configElement: secClass.setParam(secClass.DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_MOTOR_CMD, configElement.value))
        config.sec.delay_after_enable_voltage_before_motor_command = x
 
@@ -914,16 +967,22 @@ def InitSecParams():
 # the configElement should be only visible when diseqc 1.2 is disabled
 
 def InitNimManager(nimmgr):
-       InitSecParams()
        hw = HardwareInfo()
+       addNimConfig = False
+       try:
+               config.Nims
+       except:
+               addNimConfig = True
 
-       config.Nims = ConfigSubList()
-       for x in range(len(nimmgr.nim_slots)):
-               config.Nims.append(ConfigSubsection())
+       if addNimConfig:
+               InitSecParams()
+               config.Nims = ConfigSubList()
+               for x in range(len(nimmgr.nim_slots)):
+                       config.Nims.append(ConfigSubsection())
 
        lnb_choices = {
                "universal_lnb": _("Universal LNB"),
-               "unicable": _("Unicable"),
+#              "unicable": _("Unicable"),
                "c_band": _("C-Band"),
                "user_defined": _("User defined")}
 
@@ -1189,10 +1248,59 @@ def InitNimManager(nimmgr):
                                tmp.lnb = lnb
                                nim.advanced.sat[x] = tmp
 
+       def toneAmplitudeChanged(configElement):
+               fe_id = configElement.fe_id
+               slot_id = configElement.slot_id
+               if nimmgr.nim_slots[slot_id].description == 'Alps BSBE2':
+                       open("/proc/stb/frontend/%d/tone_amplitude" %(fe_id), "w").write(configElement.value)
+                       
+       def tunerTypeChanged(nimmgr, configElement):
+               fe_id = configElement.fe_id
+               print "tunerTypeChanged feid %d to mode %s" % (fe_id, configElement.value)
+               try:
+                       oldvalue = open("/sys/module/dvb_core/parameters/dvb_shutdown_timeout", "r").readline()
+                       open("/sys/module/dvb_core/parameters/dvb_shutdown_timeout", "w").write("0")
+               except:
+                       print "[info] no /sys/module/dvb_core/parameters/dvb_shutdown_timeout available"
+               frontend = eDVBResourceManager.getInstance().allocateRawChannel(fe_id).getFrontend()
+               frontend.closeFrontend()
+               open("/proc/stb/frontend/%d/mode" % (fe_id), "w").write(configElement.value)
+               frontend.reopenFrontend()
+               try:
+                       open("/sys/module/dvb_core/parameters/dvb_shutdown_timeout", "w").write(oldvalue)
+               except:
+                       print "[info] no /sys/module/dvb_core/parameters/dvb_shutdown_timeout available"
+               nimmgr.enumerateNIMs()
+       
+       empty_slots = 0
        for slot in nimmgr.nim_slots:
                x = slot.slot
                nim = config.Nims[x]
+               addMultiType = False
+               try:
+                       nim.multiType
+               except:
+                       addMultiType = True
+               if slot.isMultiType() and addMultiType:
+                       typeList = []
+                       for id in slot.getMultiTypeList().keys():
+                               type = slot.getMultiTypeList()[id]
+                               typeList.append((id, type))
+                       nim.multiType = ConfigSelection(typeList, "0")
+                       
+                       nim.multiType.fe_id = x - empty_slots
+                       nim.multiType.addNotifier(boundFunction(tunerTypeChanged, nimmgr))
+               
+       empty_slots = 0
+       for slot in nimmgr.nim_slots:
+               x = slot.slot
+               nim = config.Nims[x]
+
                if slot.isCompatible("DVB-S"):
+                       nim.toneAmplitude = ConfigSelection([("9", "600mV"), ("8", "700mV"), ("7", "800mV"), ("6", "900mV"), ("5", "1100mV")], "7")
+                       nim.toneAmplitude.fe_id = x - empty_slots
+                       nim.toneAmplitude.slot_id = x
+                       nim.toneAmplitude.addNotifier(toneAmplitudeChanged)
                        nim.diseqc13V = ConfigYesNo(False)
                        nim.diseqcMode = ConfigSelection(diseqc_mode_choices, "diseqc_a_b")
                        nim.connectedTo = ConfigSelection([(str(id), nimmgr.getNimDescription(id)) for id in nimmgr.getNimListOfType("DVB-S") if id != x])
@@ -1282,6 +1390,7 @@ def InitNimManager(nimmgr):
                        nim.terrestrial = ConfigSelection(choices = list)
                        nim.terrestrial_5V = ConfigOnOff()
                else:
+                       empty_slots += 1
                        nim.configMode = ConfigSelection(choices = { "nothing": _("disabled") }, default="nothing");
                        if slot.type is not None:
                                print "pls add support for this frontend type!", slot.type