ServiceList: allow setMode before GUIcreate
[enigma2.git] / lib / python / Components / NimManager.py
1 from config import config       #global config instance
2
3 from config import configElement
4 from config import ConfigSubsection
5 from config import ConfigSlider
6 from config import configSelection
7 from config import configSequence
8 from config import configsequencearg
9 from config import configSatlist
10
11 from enigma import *
12
13 import xml.dom.minidom
14 from xml.dom import EMPTY_NAMESPACE
15 from skin import elementsWithTag
16 from Tools import XMLTools
17
18 from xml.sax import make_parser
19 from xml.sax.handler import ContentHandler
20
21 from Tools.BoundFunction import boundFunction
22
23 def tryOpen(filename):
24         try:
25                 procFile = open(filename)
26         except IOError:
27                 return ""
28         return procFile
29
30 class SecConfigure:
31         def addLNBSimple(self, slotid, orbpos, toneburstmode, diseqcmode, diseqcpos):
32                 #simple defaults
33                 sec = eDVBSatelliteEquipmentControl.getInstance()
34                 sec.addLNB()
35                 sec.setLNBTunerMask(1 << slotid)
36                 sec.setLNBLOFL(9750000)
37                 sec.setLNBLOFH(10600000)
38                 sec.setLNBThreshold(11750000)
39                 sec.setRepeats(0)
40                 sec.setFastDiSEqC(0)
41                 sec.setSeqRepeat(0)
42                 sec.setVoltageMode(0) #HV
43                 sec.setToneMode(0)              #HILO
44                 sec.setCommandOrder(0)
45                 #user values
46                 sec.setDiSEqCMode(diseqcmode)
47                 sec.setToneburst(toneburstmode)
48                 sec.setCommittedCommand(diseqcpos)
49                 #print "set orbpos to:" + str(orbpos)
50                 sec.addSatellite(orbpos)
51                 self.satList.append(orbpos)
52
53         def linkNIMs(self, nim1, nim2):
54                 eDVBSatelliteEquipmentControl.getInstance().setTunerLinked(nim1, nim2)
55                 
56         def getSatList(self):
57                 return self.satList
58
59         def update(self):
60                 eDVBSatelliteEquipmentControl.getInstance().clear()
61                 
62                 self.satList = []
63
64                 for slot in self.NimManager.nimslots:
65                         x = slot.slotid
66                         nim = config.Nims[x]
67                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
68                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
69                                 if nim.configMode.value == 1:
70                                         self.linkNIMs(x, nim.linkedTo.value)
71                                         nim = config.Nims[nim.linkedTo.value]
72                                 if nim.configMode.value == 0:           #simple config
73                                         if nim.diseqcMode.value == 0:                   #single
74                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 0, 4)
75                                         elif nim.diseqcMode.value == 1:         #Toneburst A/B
76                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 1, 0, 4)
77                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 1, 0, 4)
78                                         elif nim.diseqcMode.value == 2:         #DiSEqC A/B
79                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
80                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
81                                         elif nim.diseqcMode.value == 3:         #DiSEqC A/B/C/D
82                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
83                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
84                                                 self.addLNBSimple(x, int(nim.diseqcC.vals[nim.diseqcC.value][1]), 0, 1, 2)
85                                                 self.addLNBSimple(x, int(nim.diseqcD.vals[nim.diseqcD.value][1]), 0, 1, 3)
86                                         elif nim.diseqcMode.value == 4:         #Positioner
87                                                 print "FIXME: positioner suppport"
88                                         pass
89                                 else:                                                                                                                                   #advanced config
90                                         print "FIXME add support for advanced config"
91                 
92         def __init__(self, nimmgr):
93                 self.NimManager = nimmgr
94                 self.update()
95                 
96 class nimSlot:
97         def __init__(self, slotid, nimtype, name):
98                 self.slotid = slotid
99                 self.nimType = nimtype
100                 self.name = name
101
102 class NimManager:
103         class parseSats(ContentHandler):
104                 def __init__(self, satList, satellites, transponders):
105                         self.isPointsElement, self.isReboundsElement = 0, 0
106                         self.satList = satList
107                         self.satellites = satellites
108                         self.transponders = transponders
109         
110                 def startElement(self, name, attrs):
111                         if (name == "sat"):
112                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
113                                 tpos = attrs.get('position',"")
114                                 tname = attrs.get('name',"")
115                                 self.satellites[tpos] = tname
116                                 self.satList.append( (tname, tpos) )
117                                 self.parsedSat = int(tpos)
118                         elif (name == "transponder"):
119                                 freq = int(attrs.get('frequency',""))
120                                 sr = int(attrs.get('symbol_rate',""))
121                                 pol = int(attrs.get('polarization',""))
122                                 fec = int(attrs.get('fec_inner',""))
123                                 if self.parsedSat in self.transponders:
124                                         pass
125                                 else:
126                                         self.transponders[self.parsedSat] = [ ]
127
128                                 self.transponders[self.parsedSat].append((0, freq, sr, pol, fec))
129
130         class parseCables(ContentHandler):
131                 def __init__(self, cablesList, transponders):
132                         self.isPointsElement, self.isReboundsElement = 0, 0
133                         self.cablesList = cablesList
134                         self.transponders = transponders
135         
136                 def startElement(self, name, attrs):
137                         if (name == "cable"):
138                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
139                                 tname = attrs.get('name',"")
140                                 self.cablesList.append(str(tname))
141                                 self.parsedCab = str(tname)
142                         elif (name == "transponder"):
143                                 freq = int(attrs.get('frequency',""))
144                                 sr = int(attrs.get('symbol_rate',""))
145                                 mod = int(attrs.get('modulation',""))
146                                 fec = int(attrs.get('fec_inner',""))
147                                 if self.parsedCab in self.transponders:
148                                         pass
149                                 else:
150                                         self.transponders[self.parsedCab] = [ ]
151
152                                 self.transponders[self.parsedCab].append((0, freq, sr, mod, fec))
153
154         class parseTerrestrials(ContentHandler):
155                 def __init__(self, terrestrialsList, transponders):
156                         self.isPointsElement, self.isReboundsElement = 0, 0
157                         self.terrestrialsList = terrestrialsList
158                         self.transponders = transponders
159         
160                 def startElement(self, name, attrs):
161                         if (name == "terrestrial"):
162                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
163                                 tname = attrs.get('name',"")
164                                 tflags = attrs.get('flags',"")
165                                 self.terrestrialsList.append((tname, tflags))
166                                 self.parsedTer = str(tname)
167                         elif (name == "transponder"):
168                                 # TODO finish this!
169                                 freq = int(attrs.get('centre_frequency',""))
170                                 bw = int(attrs.get('bandwidth',""))
171                                 const = int(attrs.get('constellation',""))
172                                 crh = int(attrs.get('code_rate_hp',""))
173                                 crl = int(attrs.get('code_rate_lp',""))
174                                 guard = int(attrs.get('guard_interval',""))
175                                 transm = int(attrs.get('transmission_mode',""))
176                                 hierarchy = int(attrs.get('hierarchy_information',""))
177                                 inv = int(attrs.get('inversion',""))
178                                 if self.parsedTer in self.transponders:
179                                         pass
180                                 else:
181                                         self.transponders[self.parsedTer] = [ ]
182
183                                 self.transponders[self.parsedTer].append((0, freq, bw, const, crh, crl, guard, transm, hierarchy, inv))
184
185         def getTransponders(self, pos):
186                 return self.transponders[pos]
187
188         def getConfiguredSats(self):
189                 return self.sec.getSatList()
190
191         def getSatDescription(self, pos):
192                 return self.satellites[str(pos)]
193
194         def readSatsfromFile(self):
195                 self.satellites = { }
196                 self.transponders = { }
197                 self.transponderscable = { }
198                 self.transpondersterrestrial = { }              
199                 
200                 parser = make_parser()
201                 if (self.hasNimType(self.nimType["DVB-S"])):
202                         print "Reading satellites.xml"
203                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
204                         parser.setContentHandler(satHandler)
205                         parser.parse('/etc/tuxbox/satellites.xml')
206                 if (self.hasNimType(self.nimType["DVB-C"])):
207                         print "Reading cables.xml"
208                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
209                         parser.setContentHandler(cabHandler)
210                         parser.parse('/etc/tuxbox/cables.xml')
211
212                 if (self.hasNimType(self.nimType["DVB-T"])):
213                         print "Reading terrestrial.xml"
214                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
215                         parser.setContentHandler(terHandler)
216                         parser.parse('/etc/tuxbox/terrestrial.xml')
217                 
218         def parseProc(self):
219                 self.nimTypes = {}
220                 self.nimNames = {}              
221                 self.nimSocketCount = 0
222                 nimfile = tryOpen("/proc/bus/nim_sockets")
223
224                 if nimfile == "":
225                                 return self.nimType["empty/unknown"]
226                         
227                 lastsocket = -1
228
229                 while 1:                
230                         line = nimfile.readline()
231                         if line == "":
232                                 break
233                         if line.strip().startswith("NIM Socket"):
234                                 parts = line.strip().split(" ")
235                                 id = int(parts[2][:1])
236                                 lastsocket = int(id)
237                                 self.nimSocketCount += 1
238                         elif line.strip().startswith("Type:"):
239                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
240                         elif line.strip().startswith("Name:"):
241                                 self.nimNames[lastsocket] = str(line.strip()[6:])
242                         elif line.strip().startswith("empty"):
243                                 self.nimNames[lastsocket] = _("N/A")
244                                 self.nimTypes[lastsocket] = "empty/unknown"
245
246                 nimfile.close()
247                 
248
249         def getNimType(self, slotID):
250                 return self.nimType[self.nimTypes[slotID]]
251
252         def getNimName(self, slotID):
253                 return self.nimNames[slotID]
254
255         def getNimSocketCount(self):
256                 return self.nimSocketCount
257         
258         def hasNimType(self, chktype):
259                 for id, type in self.nimTypes.items():
260                         if (chktype == self.nimType[str(type)]):
261                                 return True
262                 return False
263         
264         def getNimListOfType(self, type, exception = -1):
265                 list = []
266                 for x in self.nimslots:
267                         if ((x.nimType == type) and (x.slotid != exception)):
268                                 list.append(x.slotid)
269                 return list
270
271         def getConfigPrefix(self, slotid):
272                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
273                         
274         def __init__(self):
275                 #use as enum
276                 self.nimType = {                "empty/unknown": -1,
277                                                                                                 "DVB-S": 0,
278                                                                                                 "DVB-C": 1,
279                                                                                                 "DVB-T": 2}
280                 self.satList = [ ]
281                 self.cablesList = []
282                 self.terrestrialsList = []
283                                                                                                 
284                 self.parseProc()
285
286                 self.readSatsfromFile()                                                 
287                 
288                 self.nimCount = self.getNimSocketCount()
289                 
290                 self.nimslots = [ ]
291                 x = 0
292                 while x < self.nimCount:
293                         tType = self.getNimType(x)
294                         tName = self.getNimName(x)
295                         tNim = nimSlot(x, tType, tName)
296                         self.nimslots.append(tNim)
297                         x += 1
298                 
299                 InitNimManager(self)    #init config stuff
300
301         def nimList(self):
302                 list = [ ]
303                 for slot in self.nimslots:
304                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
305                         if slot.nimType == -1:
306                                 nimText += _("empty/unknown")
307                         else:
308                                 nimText += slot.name + " ("     
309                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
310                         list.append((nimText, slot))
311                 return list
312         
313         def getSatListForNim(self, slotid):
314                 list = []
315                 if (self.getNimType(slotid) != self.nimType["empty/unknown"]):
316                         #print "slotid:", slotid
317                         
318                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
319                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
320                         if (config.Nims[slotid].diseqcMode.value <= 3):
321                                 list.append(self.satList[config.Nims[slotid].diseqcA.value])
322                         if (0 < config.Nims[slotid].diseqcMode.value <= 3):
323                                 list.append(self.satList[config.Nims[slotid].diseqcB.value])
324                         if (config.Nims[slotid].diseqcMode.value == 3):
325                                 list.append(self.satList[config.Nims[slotid].diseqcC.value])
326                                 list.append(self.satList[config.Nims[slotid].diseqcD.value])
327                 return list
328
329         #callbacks for c++ config
330         def nimConfigModeChanged(self, slotid, mode):
331                 if (mode != 1): # not linked
332                         print "Unlinking slot " + str(slotid)
333                         # TODO call c++ to unlink nim in slot slotid
334                 if (mode == 1): # linked
335                         pass
336                         #FIXME!!!
337                         #if (len(self.getNimListOfType(self.nimType["DVB-S"], slotid)) > 0):
338                         #       print "Linking slot " + str(slotid) + " to " + str(nimmgr.getConfigPrefix(slotid).value)
339                         # TODO call c++ to link nim in slot slotid with nim in slot nimmgr.getConfigPrefix(slotid).value
340         def nimLinkedToChanged(self, slotid, val):
341                 print "Linking slot " + str(slotid) + " to " + str(val)
342
343         def nimDiseqcModeChanged(self, slotid, mode):
344                 #print "nimDiseqcModeChanged set to " + str(mode)
345                 pass
346         def nimPortAChanged(self, slotid, val):
347                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
348                 pass
349         def nimPortBChanged(self, slotid, val):
350                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
351                 #print "nimDiseqcB set to " + str(val)
352                 pass
353         def nimPortCChanged(self, slotid, val):
354                 #print "nimDiseqcC set to " + str(val)
355                 pass
356         def nimPortDChanged(self, slotid, val):
357                 #print "nimDiseqcD set to " + str(val)
358                 pass
359
360 def InitNimManager(nimmgr):
361         config.Nims = []
362         for x in range(nimmgr.nimCount):
363                 config.Nims.append(ConfigSubsection())
364                 
365         def nimConfigModeChanged(slotid, configElement):
366                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
367         def nimLinkedToChanged(slotid, configElement):
368                 nimmgr.nimLinkedToChanged(slotid, configElement.value)
369         def nimDiseqcModeChanged(slotid, configElement):
370                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
371                 
372         def nimPortAChanged(slotid, configElement):
373                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
374         def nimPortBChanged(slotid, configElement):
375                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
376         def nimPortCChanged(slotid, configElement):
377                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
378         def nimPortDChanged(slotid, configElement):
379                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
380
381         for slot in nimmgr.nimslots:
382                 x = slot.slotid
383                 cname = nimmgr.getConfigPrefix(x)
384                 nim = config.Nims[x]
385                 
386                 if slot.nimType == nimmgr.nimType["DVB-S"]:
387                         nim.configMode = configElement(cname + "configMode", configSelection, 0, (_("Simple"), _("Loopthrough to Socket A"))) # "Advanced"));
388                         
389                         #important - check if just the 2nd one is LT only and the first one is DVB-S
390                         if nim.configMode.value == 1: #linked
391                                 if x == 0:                                                                              #first one can never be linked to anything
392                                         nim.configMode.value = 0                #reset to simple
393                                         nim.configMode.save()
394                                 else:
395                                         #FIXME: make it better
396                                         for y in nimmgr.nimslots:
397                                                 if y.slotid == 0:
398                                                         if y.nimType != nimmgr.nimType["DVB-S"]:
399                                                                 nim.configMode.value = 0                #reset to simple
400                                                                 nim.configMode.save()
401
402                         nim.diseqcMode = configElement(cname + "diseqcMode", configSelection, 2, (_("Single"), _("Toneburst A/B"), _("DiSEqC A/B"), _("DiSEqC A/B/C/D"), _("Positioner")));
403                         nim.diseqcA = configElement(cname + "diseqcA", configSatlist, 192, nimmgr.satList);
404                         nim.diseqcB = configElement(cname + "diseqcB", configSatlist, 130, nimmgr.satList);
405                         nim.diseqcC = configElement(cname + "diseqcC", configSatlist, 0, nimmgr.satList);
406                         nim.diseqcD = configElement(cname + "diseqcD", configSatlist, 0, nimmgr.satList);
407                         nim.positionerMode = configElement(cname + "positionerMode", configSelection, 0, (_("USALS"), _("manual")));
408                         nim.longitude = configElement(cname + "longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
409                         nim.longitudeOrientation = configElement(cname + "longitudeOrientation", configSelection, 0, (_("East"), _("West")))
410                         nim.latitude = configElement(cname + "latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
411                         nim.latitudeOrientation = configElement(cname + "latitudeOrientation", configSelection, 0, (_("North"), _("South")))
412                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
413                         satNimListNames = []
414                         for x in satNimList:
415                                 satNimListNames.append(_("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x))
416                         nim.linkedTo = configElement(cname + "linkedTo", configSelection, 0, satNimListNames);
417                         
418                         #perhaps the instance of the slot is more useful?
419                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
420                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
421                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
422                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
423                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
424                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
425                         nim.linkedTo.addNotifier(boundFunction(nimLinkedToChanged,x))
426                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
427                         nim.cable = configElement(cname + "cable", configSelection, 0, nimmgr.cablesList);
428                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
429                         nim.cable = configElement(cname + "terrestrial", configSelection, 0, nimmgr.terrestrialsList);
430                 else:
431                         print "pls add support for this frontend type!"         
432
433         nimmgr.sec = SecConfigure(nimmgr)
434
435 nimmanager = NimManager()