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