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