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