fix caching
[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 configSelection
6 from config import currentConfigSelectionElement
7 from config import getConfigSelectionElement
8 from config import configSequence
9 from config import configsequencearg
10 from config import configSatlist
11
12 from enigma import eDVBSatelliteEquipmentControl, \
13         eDVBSatelliteLNBParameters as lnbParam, \
14         eDVBSatelliteDiseqcParameters as diseqcParam, \
15         eDVBSatelliteSwitchParameters as switchParam, \
16         eDVBSatelliteRotorParameters as rotorParam
17
18 import xml.dom.minidom
19 from xml.dom import EMPTY_NAMESPACE
20 from skin import elementsWithTag
21 from Tools import XMLTools
22
23 from xml.sax import make_parser
24 from xml.sax.handler import ContentHandler
25
26 from Tools.BoundFunction import boundFunction
27
28 def tryOpen(filename):
29         try:
30                 procFile = open(filename)
31         except IOError:
32                 return ""
33         return procFile
34
35 class SecConfigure:
36         def addLNBSimple(self, sec, slotid, diseqcmode, toneburstmode = 0, diseqcpos = 0, orbpos = 0, longitude = 0, latitude = 0, loDirection = 0, laDirection = 0):
37                 #simple defaults
38                 sec.addLNB()
39                 tunermask = 1 << slotid
40                 if self.equal.has_key(slotid):
41                         tunermask |= (1 << self.equal[slotid])
42                 elif self.linked.has_key(slotid):
43                         tunermask |= (1 << self.linked[slotid])
44                 sec.setLNBTunerMask(tunermask)
45                 sec.setLNBLOFL(9750000)
46                 sec.setLNBLOFH(10600000)
47                 sec.setLNBThreshold(11700000)
48                 sec.setLNBIncreasedVoltage(lnbParam.OFF)
49                 sec.setRepeats(0)
50                 sec.setFastDiSEqC(0)
51                 sec.setSeqRepeat(0)
52                 sec.setVoltageMode(switchParam.HV)
53                 sec.setToneMode(switchParam.HILO)
54                 sec.setCommandOrder(0)
55
56                 #user values
57                 sec.setDiSEqCMode(diseqcmode)
58                 sec.setToneburst(toneburstmode)
59                 sec.setCommittedCommand(diseqcpos)
60                 #print "set orbpos to:" + str(orbpos)
61
62                 if (0 <= diseqcmode < 3):
63                         sec.addSatellite(orbpos)
64                         self.satList.append(orbpos)
65                 elif (diseqcmode == 3): # diseqc 1.2
66                         if self.satposdepends.has_key(slotid):
67                                 tunermask |= (1 << self.satposdepends[slotid])
68                                 sec.setLNBTunerMask(tunermask)
69                         sec.setLatitude(latitude)
70                         sec.setLaDirection(laDirection)
71                         sec.setLongitude(longitude)
72                         sec.setLoDirection(loDirection)
73                         sec.setUseInputpower(True)
74                         sec.setInputpowerDelta(50)
75
76                         for x in self.NimManager.satList:
77                                 print "Add sat " + str(x[1])
78                                 sec.addSatellite(int(x[1]))
79                                 sec.setVoltageMode(0)
80                                 sec.setToneMode(0)
81                                 sec.setRotorPosNum(0) # USALS
82                                 self.satList.append(int(x[1]))
83
84         def setSatposDepends(self, sec, nim1, nim2):
85                 print "tuner", nim1, "depends on satpos of", nim2
86                 sec.setTunerDepends(nim1, nim2)
87
88         def linkNIMs(self, sec, nim1, nim2):
89                 print "link tuner", nim1, "to tuner", nim2
90                 sec.setTunerLinked(nim1, nim2)
91
92         def getSatList(self):
93                 return self.satList
94
95         def update(self):
96                 sec = eDVBSatelliteEquipmentControl.getInstance()
97                 sec.clear() ## this do unlinking NIMs too !!
98                 print "sec config cleared"
99                 self.satList = []
100
101                 self.linked = { }
102                 self.satposdepends = { }
103                 self.equal = { }
104                 for slot in self.NimManager.nimslots:
105                         x = slot.slotid
106                         nim = config.Nims[x]
107                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
108                                 if currentConfigSelectionElement(nim.configMode) == "equal":
109                                         self.equal[nim.equalTo.value]=x
110                                 if currentConfigSelectionElement(nim.configMode) == "loopthrough":
111                                         self.linkNIMs(sec, x, nim.linkedTo.value)
112                                         self.linked[nim.linkedTo.value]=x
113                                 elif currentConfigSelectionElement(nim.configMode) == "satposdepends":
114                                         self.setSatposDepends(sec, x, nim.satposDependsTo.value)
115                                         self.satposdepends[nim.satposDependsTo.value]=x
116
117                 for slot in self.NimManager.nimslots:
118                         x = slot.slotid
119                         nim = config.Nims[x]
120                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
121                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
122                                 if currentConfigSelectionElement(nim.configMode) in [ "loopthrough", "satposdepends", "equal", "nothing" ]:
123                                         pass
124                                 elif currentConfigSelectionElement(nim.configMode) == "simple":         #simple config
125                                         if currentConfigSelectionElement(nim.diseqcMode) == "single":                   #single
126                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.NONE, diseqcpos = diseqcParam.SENDNO)
127                                         elif currentConfigSelectionElement(nim.diseqcMode) == "toneburst_a_b":          #Toneburst A/B
128                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = diseqcParam.A, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.SENDNO)
129                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = diseqcParam.B, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.SENDNO)
130                                         elif currentConfigSelectionElement(nim.diseqcMode) == "diseqc_a_b":             #DiSEqC A/B
131                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.AA)
132                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.AB)
133                                         elif currentConfigSelectionElement(nim.diseqcMode) == "diseqc_a_b_c_d":         #DiSEqC A/B/C/D
134                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.AA)
135                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.AB)
136                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcC.vals[nim.diseqcC.value][1]), toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.BA)
137                                                 self.addLNBSimple(sec, slotid = x, orbpos = int(nim.diseqcD.vals[nim.diseqcD.value][1]), toneburstmode = diseqcParam.NO, diseqcmode = diseqcParam.V1_0, diseqcpos = diseqcParam.BB)
138                                         elif currentConfigSelectionElement(nim.diseqcMode) == "positioner":             #Positioner
139                                                 if currentConfigSelectionElement(nim.latitudeOrientation) == "north":
140                                                         laValue = rotorParam.NORTH
141                                                 else:
142                                                         laValue = rotorParam.SOUTH
143                                                 if currentConfigSelectionElement(nim.longitudeOrientation) == "east":
144                                                         loValue = rotorParam.EAST
145                                                 else:
146                                                         loValue = rotorParam.WEST
147                                                 self.addLNBSimple(sec, slotid = x, diseqcmode = 3,
148                                                         longitude = configsequencearg.getFloat(nim.longitude),
149                                                         loDirection = loValue,
150                                                         latitude = configsequencearg.getFloat(nim.latitude),
151                                                         laDirection = laValue)
152                                 elif currentConfigSelectionElement(nim.configMode) == "advanced": #advanced config
153                                         self.updateAdvanced(sec, x)
154                 print "sec config completed"
155
156         def updateAdvanced(self, sec, slotid):
157                 lnbSat = {}
158                 for x in range(1,33):
159                         lnbSat[x] = []
160                 for x in self.NimManager.satList:
161                         lnb = config.Nims[slotid].advanced.sat[x[1]].lnb.value
162                         if lnb != 0:
163                                 print "add", x[1], "to", lnb
164                                 lnbSat[lnb].append(x[1])
165                 for x in range(1,33):
166                         if len(lnbSat[x]) > 0:
167                                 currLnb = config.Nims[slotid].advanced.lnb[x]
168                                 sec.addLNB()
169
170                                 tunermask = 1 << slotid
171                                 if self.equal.has_key(slotid):
172                                         tunermask |= (1 << self.equal[slotid])
173                                 elif self.linked.has_key(slotid):
174                                         tunermask |= (1 << self.linked[slotid])
175
176                                 if currentConfigSelectionElement(currLnb.lof) == "universal_lnb":
177                                         sec.setLNBLOFL(9750000)
178                                         sec.setLNBLOFH(10600000)
179                                         sec.setLNBThreshold(11700000)
180                                 elif currentConfigSelectionElement(currLnb.lof) == "c_band":
181                                         sec.setLNBLOFL(5150000)
182                                         sec.setLNBLOFH(5150000)
183                                         sec.setLNBThreshold(5150000)
184                                 elif currentConfigSelectionElement(currLnb.lof) == "user_defined":
185                                         sec.setLNBLOFL(currLnb.lofl.value[0] * 1000)
186                                         sec.setLNBLOFH(currLnb.lofh.value[0] * 1000)
187                                         sec.setLNBThreshold(currLnb.threshold.value[0] * 1000)
188                                         
189                                 if currentConfigSelectionElement(currLnb.output_12v) == "0V":
190                                         pass # nyi in drivers
191                                 elif currentConfigSelectionElement(currLnb.output_12v) == "12V":
192                                         pass # nyi in drivers
193                                         
194                                 if currentConfigSelectionElement(currLnb.increased_voltage) == "yes":
195                                         sec.setLNBIncreasedVoltage(lnbParam.ON)
196                                 else:
197                                         sec.setLNBIncreasedVoltage(lnbParam.OFF)
198                                         
199                                 if currentConfigSelectionElement(currLnb.diseqcMode) == "none":
200                                         sec.setDiSEqCMode(diseqcParam.NONE)
201                                 elif currentConfigSelectionElement(currLnb.diseqcMode) == "1_0":
202                                         sec.setDiSEqCMode(diseqcParam.V1_0)
203                                 elif currentConfigSelectionElement(currLnb.diseqcMode) == "1_1":
204                                         sec.setDiSEqCMode(diseqcParam.V1_1)
205                                 elif currentConfigSelectionElement(currLnb.diseqcMode) == "1_2":
206                                         sec.setDiSEqCMode(diseqcParam.V1_2)
207
208                                         if self.satposdepends.has_key(slotid):  # only useable with rotors
209                                                 tunermask |= (1 << self.satposdepends[slotid])
210
211                                 if currentConfigSelectionElement(currLnb.diseqcMode) != "none":
212                                         if currentConfigSelectionElement(currLnb.toneburst) == "none":
213                                                 sec.setToneburst(diseqcParam.NO)
214                                         elif currentConfigSelectionElement(currLnb.toneburst) == "A":
215                                                 sec.setToneburst(diseqcParam.A)
216                                         elif currentConfigSelectionElement(currLnb.toneburst) == "B":
217                                                 sec.setToneburst(diseqcParam.B)
218                                                 
219                                         if currentConfigSelectionElement(currLnb.commitedDiseqcCommand) == "none":
220                                                 sec.setCommittedCommand(diseqcParam.SENDNO)
221                                         elif currentConfigSelectionElement(currLnb.commitedDiseqcCommand) == "AA":
222                                                 sec.setCommittedCommand(diseqcParam.AA)
223                                         elif currentConfigSelectionElement(currLnb.commitedDiseqcCommand) == "AB":
224                                                 sec.setCommittedCommand(diseqcParam.AB)
225                                         elif currentConfigSelectionElement(currLnb.commitedDiseqcCommand) == "BA":
226                                                 sec.setCommittedCommand(diseqcParam.BA)
227                                         elif currentConfigSelectionElement(currLnb.commitedDiseqcCommand) == "BB":
228                                                 sec.setCommittedCommand(diseqcParam.BB)
229                                         else:
230                                                 sec.setCommittedCommand(long(currentConfigSelectionElement(currLnb.commitedDiseqcCommand)))
231
232                                         if currentConfigSelectionElement(currLnb.fastDiseqc) == "yes":
233                                                 sec.setFastDiSEqC(True)
234                                         else:
235                                                 sec.setFastDiSEqC(False)
236                                                 
237                                         if currentConfigSelectionElement(currLnb.sequenceRepeat) == "yes":
238                                                 sec.setSeqRepeat(True)
239                                         else:
240                                                 sec.setSeqRepeat(False)
241                                                 
242                                         if currentConfigSelectionElement(currLnb.diseqcMode) == "1_0":
243                                                 currCO = currLnb.commandOrder1_0.value
244                                         else:
245                                                 currCO = currLnb.commandOrder.value
246                                                 
247                                                 if currLnb.uncommittedDiseqcCommand.value > 0:
248                                                         sec.setUncommittedCommand(0xF0|(currLnb.uncommittedDiseqcCommand.value-1))
249                                                 else:
250                                                         sec.setUncommittedCommand(0) # SENDNO
251                                                 
252                                                 if currentConfigSelectionElement(currLnb.diseqcRepeats) == "none":
253                                                         sec.setRepeats(0)
254                                                 elif currentConfigSelectionElement(currLnb.diseqcRepeats) == "one":
255                                                         sec.setRepeats(1)
256                                                 elif currentConfigSelectionElement(currLnb.diseqcRepeats) == "two":
257                                                         sec.setRepeats(2)
258                                                 elif currentConfigSelectionElement(currLnb.diseqcRepeats) == "three":
259                                                         sec.setRepeats(3)
260                                                 
261                                         setCommandOrder=False
262                                         if currCO == 0: # committed, toneburst
263                                                 setCommandOrder=True
264                                         elif currCO == 1: # toneburst, committed
265                                                 setCommandOrder=True
266                                         elif currCO == 2: # committed, uncommitted, toneburst
267                                                 setCommandOrder=True
268                                         elif currCO == 3: # toneburst, committed, uncommitted
269                                                 setCommandOrder=True
270                                         elif currCO == 4: # uncommitted, committed, toneburst
271                                                 setCommandOrder=True
272                                         elif currCO == 5: # toneburst, uncommitted, commmitted
273                                                 setCommandOrder=True
274                                         if setCommandOrder:
275                                                 sec.setCommandOrder(currCO)
276                                                 
277                                 if currentConfigSelectionElement(currLnb.diseqcMode) == "1_2":
278                                         latitude = configsequencearg.getFloat(currLnb.latitude)
279                                         sec.setLatitude(latitude)
280                                         longitude = configsequencearg.getFloat(currLnb.longitude)
281                                         sec.setLongitude(longitude)
282                                         if currentConfigSelectionElement(currLnb.latitudeOrientation) == "north":
283                                                 sec.setLaDirection(rotorParam.NORTH)
284                                         else:
285                                                 sec.setLaDirection(rotorParam.SOUTH)
286                                         if currentConfigSelectionElement(currLnb.longitudeOrientation) == "east":
287                                                 sec.setLoDirection(rotorParam.EAST)
288                                         else:
289                                                 sec.setLoDirection(rotorParam.WEST)
290                                                 
291                                 if currentConfigSelectionElement(currLnb.powerMeasurement) == "yes":
292                                         sec.setUseInputpower(True)
293                                         sec.setInputpowerDelta(currLnb.powerThreshold.value[0])
294                                 else:
295                                         sec.setUseInputpower(False)
296
297                                 sec.setLNBTunerMask(tunermask)
298
299                                 # finally add the orbital positions
300                                 for y in lnbSat[x]:
301                                         sec.addSatellite(y)
302                                         currSat = config.Nims[slotid].advanced.sat[y]
303                                         if currentConfigSelectionElement(currSat.voltage) == "polarization":
304                                                 sec.setVoltageMode(switchParam.HV)
305                                         elif currentConfigSelectionElement(currSat.voltage) == "13V":
306                                                 sec.setVoltageMode(switchParam._14V)
307                                         elif currentConfigSelectionElement(currSat.voltage) == "18V":
308                                                 sec.setVoltageMode(switchParam._18V)
309                                                 
310                                         if currentConfigSelectionElement(currSat.tonemode) == "band":
311                                                 sec.setToneMode(switchParam.HILO)
312                                         elif currentConfigSelectionElement(currSat.tonemode) == "on":
313                                                 sec.setToneMode(switchParam.ON)
314                                         elif currentConfigSelectionElement(currSat.tonemode) == "off":
315                                                 sec.setToneMode(switchParam.OFF)
316                                                 
317                                         if  currentConfigSelectionElement(currSat.usals) == "no":
318                                                 sec.setRotorPosNum(currSat.rotorposition.value[0])
319                                         else:
320                                                 sec.setRotorPosNum(0) #USALS
321
322         def __init__(self, nimmgr):
323                 self.NimManager = nimmgr
324                 self.update()
325
326 class nimSlot:
327         def __init__(self, slotid, nimtype, name):
328                 self.slotid = slotid
329                 self.nimType = nimtype
330                 self.name = name
331
332 class NimManager:
333         class parseSats(ContentHandler):
334                 def __init__(self, satList, satellites, transponders):
335                         self.isPointsElement, self.isReboundsElement = 0, 0
336                         self.satList = satList
337                         self.satellites = satellites
338                         self.transponders = transponders
339         
340                 def startElement(self, name, attrs):
341                         if (name == "sat"):
342                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
343                                 tpos = int(attrs.get('position',""))
344                                 if tpos < 0:
345                                         tpos = 3600 + tpos
346                                 tname = attrs.get('name',"").encode("UTF-8")
347                                 self.satellites[tpos] = tname
348                                 self.satList.append( (tname, tpos) )
349                                 self.parsedSat = int(tpos)
350                         elif (name == "transponder"):
351                                 modulation = int(attrs.get('modulation',"1")) # QPSK default
352                                 system = int(attrs.get('system',"0")) # DVB-S default
353                                 freq = int(attrs.get('frequency',""))
354                                 sr = int(attrs.get('symbol_rate',""))
355                                 pol = int(attrs.get('polarization',""))
356                                 fec = int(attrs.get('fec_inner',"0")) # AUTO default
357                                 if self.parsedSat in self.transponders:
358                                         pass
359                                 else:
360                                         self.transponders[self.parsedSat] = [ ]
361
362                                 self.transponders[self.parsedSat].append((0, freq, sr, pol, fec, system, modulation))
363
364         class parseCables(ContentHandler):
365                 def __init__(self, cablesList, transponders):
366                         self.isPointsElement, self.isReboundsElement = 0, 0
367                         self.cablesList = cablesList
368                         self.transponders = transponders
369         
370                 def startElement(self, name, attrs):
371                         if (name == "cable"):
372                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
373                                 tname = attrs.get('name',"").encode("UTF-8")
374                                 self.cablesList.append(str(tname))
375                                 self.parsedCab = str(tname)
376                         elif (name == "transponder"):
377                                 freq = int(attrs.get('frequency',""))
378                                 sr = int(attrs.get('symbol_rate',""))
379                                 mod = int(attrs.get('modulation',"3")) # QAM64 default
380                                 fec = int(attrs.get('fec_inner',"0")) # AUTO default
381                                 if self.parsedCab in self.transponders:
382                                         pass
383                                 else:
384                                         self.transponders[self.parsedCab] = [ ]
385                                 self.transponders[self.parsedCab].append((1, freq, sr, mod, fec))
386
387         class parseTerrestrials(ContentHandler):
388                 def __init__(self, terrestrialsList, transponders):
389                         self.isPointsElement, self.isReboundsElement = 0, 0
390                         self.terrestrialsList = terrestrialsList
391                         self.transponders = transponders
392         
393                 def startElement(self, name, attrs):
394                         if (name == "terrestrial"):
395                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
396                                 tname = attrs.get('name',"").encode("UTF-8")
397                                 tflags = attrs.get('flags',"")
398                                 self.terrestrialsList.append((tname, tflags))
399                                 self.parsedTer = str(tname)
400                         elif (name == "transponder"):
401                                 # TODO finish this!
402                                 freq = int(attrs.get('centre_frequency',""))
403                                 bw = int(attrs.get('bandwidth',"3")) # AUTO
404                                 const = int(attrs.get('constellation',"1")) # AUTO
405                                 crh = int(attrs.get('code_rate_hp',"5")) # AUTO
406                                 if crh > 5: # our terrestrial.xml is buggy... 6 for AUTO
407                                         crh = 5
408                                 crl = int(attrs.get('code_rate_lp',"5")) # AUTO
409                                 if crl > 5: # our terrestrial.xml is buggy... 6 for AUTO
410                                         crl = 5
411                                 guard = int(attrs.get('guard_interval',"4")) # AUTO
412                                 transm = int(attrs.get('transmission_mode',"2")) # AUTO
413                                 hierarchy = int(attrs.get('hierarchy_information',"4")) # AUTO
414                                 inv = int(attrs.get('inversion',"2")) # AUTO
415                                 if self.parsedTer in self.transponders:
416                                         pass
417                                 else:
418                                         self.transponders[self.parsedTer] = [ ]
419
420                                 self.transponders[self.parsedTer].append((2, freq, bw, const, crh, crl, guard, transm, hierarchy, inv))
421
422         def getTransponders(self, pos):
423                 if self.transponders.has_key(pos):
424                         return self.transponders[pos]
425                 else:
426                         return []
427
428         def getTranspondersCable(self, cable):
429                 return self.transponderscable[cable]
430
431         def getTranspondersTerrestrial(self, region):
432                 return self.transpondersterrestrial[region]
433         
434         def getCableDescription(self, nim):
435                 return self.cablesList[config.Nims[nim].cable.value]
436
437         def getTerrestrialDescription(self, nim):
438                 return self.terrestrialsList[config.Nims[nim].terrestrial.value][0]
439
440         def getTerrestrialFlags(self, nim):
441                 return self.terrestrialsList[config.Nims[nim].terrestrial.value][1]
442
443         def getConfiguredSats(self):
444                 return self.sec.getSatList()
445
446         def getSatDescription(self, pos):
447                 return self.satellites[pos]
448
449         def readSatsfromFile(self):
450                 self.satellites = { }
451                 self.transponders = { }
452                 self.transponderscable = { }
453                 self.transpondersterrestrial = { }              
454                 
455                 parser = make_parser()
456                 if (self.hasNimType(self.nimType["DVB-S"])):
457                         print "Reading satellites.xml"
458                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
459                         parser.setContentHandler(satHandler)
460                         parser.parse('/etc/tuxbox/satellites.xml')
461                 if (self.hasNimType(self.nimType["DVB-C"])):
462                         print "Reading cables.xml"
463                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
464                         parser.setContentHandler(cabHandler)
465                         parser.parse('/etc/tuxbox/cables.xml')
466
467                 if (self.hasNimType(self.nimType["DVB-T"])):
468                         print "Reading terrestrial.xml"
469                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
470                         parser.setContentHandler(terHandler)
471                         parser.parse('/etc/tuxbox/terrestrial.xml')
472                 
473         def parseProc(self):
474                 self.nimTypes = {}
475                 self.nimNames = {}              
476                 self.nimSocketCount = 0
477                 nimfile = tryOpen("/proc/bus/nim_sockets")
478
479                 if nimfile == "":
480                                 return self.nimType["empty/unknown"]
481                         
482                 lastsocket = -1
483
484                 while 1:                
485                         line = nimfile.readline()
486                         if line == "":
487                                 break
488                         if line.strip().startswith("NIM Socket"):
489                                 parts = line.strip().split(" ")
490                                 id = int(parts[2][:1])
491                                 lastsocket = int(id)
492                                 self.nimSocketCount += 1
493                         elif line.strip().startswith("Type:"):
494                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
495                                 #self.nimTypes[lastsocket] = str("DVB-T")
496                         elif line.strip().startswith("Name:"):
497                                 self.nimNames[lastsocket] = str(line.strip()[6:])
498                         elif line.strip().startswith("empty"):
499                                 self.nimNames[lastsocket] = _("N/A")
500                                 self.nimTypes[lastsocket] = "empty/unknown"
501
502                 nimfile.close()
503
504         def getNimType(self, slotID):
505                 if slotID >= self.nimCount:
506                         return self.nimType["empty/unknown"]
507                 else:   
508                         return self.nimType[self.nimTypes[slotID]]
509                         
510         def getNimTypeName(self, slotID):
511                 if slotID >= self.nimCount:
512                         return "empty/unknown"
513                 else:   
514                         return self.nimTypes[slotID]
515                 
516         def getNimName(self, slotID):
517                 return self.nimNames[slotID]
518
519         def getNimSocketCount(self):
520                 return self.nimSocketCount
521         
522         def hasNimType(self, chktype):
523                 for id, type in self.nimTypes.items():
524                         if (chktype == self.nimType[str(type)]):
525                                 return True
526                 return False
527         
528         def getNimListOfType(self, type, exception = -1):
529                 list = []
530                 for x in self.nimslots:
531                         if ((x.nimType == type) and (x.slotid != exception)):
532                                 list.append(x.slotid)
533                 return list
534
535         def getConfigPrefix(self, slotid):
536                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
537                         
538         def __init__(self):
539                 #use as enum
540                 self.nimType = {                "empty/unknown": -1,
541                                                                                                 "DVB-S": 0,
542                                                                                                 "DVB-C": 1,
543                                                                                                 "DVB-T": 2}
544                 self.satList = [ ]
545                 self.cablesList = []
546                 self.terrestrialsList = []
547                                                                                                 
548                 self.parseProc()
549
550                 self.readSatsfromFile()                                                 
551                 
552                 self.nimCount = self.getNimSocketCount()
553                 
554                 self.nimslots = [ ]
555                 x = 0
556                 while x < self.nimCount:
557                         tType = self.getNimType(x)
558                         tName = self.getNimName(x)
559                         tNim = nimSlot(x, tType, tName)
560                         self.nimslots.append(tNim)
561                         x += 1
562                 
563                 InitNimManager(self)    #init config stuff
564
565         def nimList(self):
566                 list = [ ]
567                 for slot in self.nimslots:
568                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
569                         if slot.nimType == -1:
570                                 nimText += _("empty/unknown")
571                         else:
572                                 nimText += slot.name + " ("     
573                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
574                         list.append((nimText, slot))
575                 return list
576
577         def getNimConfigMode(self, slotid):
578                 return currentConfigSelectionElement(config.Nims[slotid].configMode)
579         
580         def getSatList(self):
581                 return self.satList
582
583         def getSatListForNim(self, slotid):
584                 list = []
585                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
586                         #print "slotid:", slotid
587                         
588                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
589                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
590                         configMode = currentConfigSelectionElement(config.Nims[slotid].configMode)
591
592                         if configMode == "equal":
593                                 slotid=0 #FIXME add handling for more than two tuners !!!
594                                 configMode = currentConfigSelectionElement(config.Nims[slotid].configMode)
595
596                         if configMode == "simple":
597                                 if (config.Nims[slotid].diseqcMode.value <= 3):
598                                         list.append(self.satList[config.Nims[slotid].diseqcA.value])
599                                 if (0 < config.Nims[slotid].diseqcMode.value <= 3):
600                                         list.append(self.satList[config.Nims[slotid].diseqcB.value])
601                                 if (config.Nims[slotid].diseqcMode.value == 3):
602                                         list.append(self.satList[config.Nims[slotid].diseqcC.value])
603                                         list.append(self.satList[config.Nims[slotid].diseqcD.value])
604                                 if (config.Nims[slotid].diseqcMode.value == 4):
605                                         for x in self.satList:
606                                                 list.append(x)
607                         elif configMode == "advanced":
608                                 for x in self.satList:
609                                         if config.Nims[slotid].advanced.sat[x[1]].lnb.value != 0:
610                                                 list.append(x)
611
612                 return list
613
614         def getRotorSatListForNim(self, slotid):
615                 list = []
616                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
617                         #print "slotid:", slotid
618
619                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
620                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
621                         configMode = currentConfigSelectionElement(config.Nims[slotid].configMode)
622                         if configMode == "simple":
623                                 if (config.Nims[slotid].diseqcMode.value == 4):
624                                         for x in self.satList:
625                                                 list.append(x)
626                         elif configMode == "advanced":
627                                 for x in self.satList:
628                                         nim = config.Nims[slotid]
629                                         lnbnum = nim.advanced.sat[x[1]].lnb.value
630                                         if lnbnum != 0:
631                                                 lnb = nim.advanced.lnb[lnbnum]
632                                                 if lnb.diseqcMode.value == 3: # diseqc 1.2
633                                                         list.append(x)
634                 return list
635
636         def nimDiseqcModeChanged(self, slotid, mode):
637                 #print "nimDiseqcModeChanged set to " + str(mode)
638                 pass
639         def nimPortAChanged(self, slotid, val):
640                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
641                 pass
642         def nimPortBChanged(self, slotid, val):
643                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
644                 #print "nimDiseqcB set to " + str(val)
645                 pass
646         def nimPortCChanged(self, slotid, val):
647                 #print "nimDiseqcC set to " + str(val)
648                 pass
649         def nimPortDChanged(self, slotid, val):
650                 #print "nimDiseqcD set to " + str(val)
651                 pass
652
653 def InitNimManager(nimmgr):
654         config.Nims = []
655         for x in range(nimmgr.nimCount):
656                 config.Nims.append(ConfigSubsection())
657                 
658 #       def nimConfigModeChanged(slotid, configElement):
659 #               nimmgr.nimConfigModeChanged(slotid, configElement.value)
660         def nimDiseqcModeChanged(slotid, configElement):
661                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
662                 
663         def nimPortAChanged(slotid, configElement):
664                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
665         def nimPortBChanged(slotid, configElement):
666                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
667         def nimPortCChanged(slotid, configElement):
668                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
669         def nimPortDChanged(slotid, configElement):
670                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
671
672         for slot in nimmgr.nimslots:
673                 x = slot.slotid
674                 cname = nimmgr.getConfigPrefix(x)
675                 nim = config.Nims[x]
676                 
677                 if slot.nimType == nimmgr.nimType["DVB-S"]:
678                         if slot.slotid == 0:
679                                 nim.configMode = configElement(cname + "configMode", configSelection, 0, (
680                                 ("simple", _("Simple")), ("advanced", _("Advanced"))), False)
681                         else:
682                                 nim.configMode = configElement(cname + "configMode", configSelection, 0, (
683                                 ("equal", _("Equal to Socket A")),
684                                 ("loopthrough", _("Loopthrough to Socket A")),
685                                 ("nothing", _("Nothing connected")),
686                                 ("satposdepends", _("Secondary cable from motorized LNB")),
687                                 ("simple", _("Simple")),
688                                 ("advanced", _("Advanced"))), False)
689                         #important - check if just the 2nd one is LT only and the first one is DVB-S
690                         if currentConfigSelectionElement(nim.configMode) in ["loopthrough", "satposdepends", "equal"]:
691                                 if x == 0:                                                                              #first one can never be linked to anything
692                                         nim.configMode.value = getConfigSelectionElement(nim.configMode, "simple")              #reset to simple
693                                         nim.configMode.save()
694                                 else:
695                                         #FIXME: make it better
696                                         for y in nimmgr.nimslots:
697                                                 if y.slotid == 0:
698                                                         if y.nimType != nimmgr.nimType["DVB-S"]:
699                                                                 nim.configMode.value = getConfigSelectionElement(nim.configMode, "simple")              #reset to simple
700                                                                 nim.configMode.save()
701
702                         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"))), False);
703                         nim.diseqcA = configElement(cname + "diseqcA", configSatlist, 192, nimmgr.satList, False);
704                         nim.diseqcB = configElement(cname + "diseqcB", configSatlist, 130, nimmgr.satList, False);
705                         nim.diseqcC = configElement(cname + "diseqcC", configSatlist, 0, nimmgr.satList, False);
706                         nim.diseqcD = configElement(cname + "diseqcD", configSatlist, 0, nimmgr.satList, False);
707                         nim.positionerMode = configElement(cname + "positionerMode", configSelection, 0, (("usals", _("USALS")), ("manual", _("manual"))), False);
708                         nim.longitude = configElement(cname + "longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,359),(0,999)]), False);
709                         nim.longitudeOrientation = configElement(cname + "longitudeOrientation", configSelection, 0, (("east", _("East")), ("west", _("West"))), False)
710                         nim.latitude = configElement(cname + "latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,359),(0,999)]), False);
711                         nim.latitudeOrientation = configElement(cname + "latitudeOrientation", configSelection, 0, (("north", _("North")), ("south", _("South"))), False)
712                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
713                         satNimListNames = []
714                         for x in satNimList:
715                                 satNimListNames.append((("Slot_" + ("A", "B", "C", "D")[x] + "_" + nimmgr.getNimName(x)), _("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x)))
716                         nim.equalTo = configElement(cname + "equalTo", configSelection, 0, satNimListNames, False);
717                         nim.linkedTo = configElement(cname + "linkedTo", configSelection, 0, satNimListNames, False);
718                         nim.satposDependsTo = configElement(cname + "satposDependsTo", configSelection, 0, satNimListNames, False);
719                         
720                         #perhaps the instance of the slot is more useful?
721 #                       nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
722                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
723                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
724                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
725                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
726                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
727                         
728                         # advanced config:
729                         nim.advanced = ConfigSubsection()
730                         nim.advanced.sats = configElement(cname + "advanced.sats", configSatlist, 192, nimmgr.satList, False);
731                         nim.advanced.sat = {}
732                         lnbs = ["not available"]
733                         for y in range(1, 33):
734                                 lnbs.append("LNB " + str(y))
735                         for x in nimmgr.satList:
736                                 nim.advanced.sat[x[1]] = ConfigSubsection()
737                                 nim.advanced.sat[x[1]].voltage = configElement(cname + "advanced.sat" + str(x[1]) + ".voltage", configSelection, 0, (("polarization", _("Polarization")), ("13V", _("13 V")), ("18V", _("18 V"))), False)
738                                 nim.advanced.sat[x[1]].tonemode = configElement(cname + "advanced.sat" + str(x[1]) + ".tonemode", configSelection, 0, (("band", _("Band")), ("on", _("On")), ("off", _("Off"))), False)
739                                 nim.advanced.sat[x[1]].usals = configElement(cname + "advanced.sat" + str(x[1]) + ".usals", configSelection, 0, (("yes", _("Yes")), ("no", _("No"))), False)
740                                 nim.advanced.sat[x[1]].rotorposition = configElement(cname + "advanced.sat" + str(x[1]) + ".rotorposition", configSequence, [1], configsequencearg.get("INTEGER", (1, 255)), False)
741                                 nim.advanced.sat[x[1]].lnb = configElement(cname + "advanced.sat" + str(x[1]) + ".lnb", configSelection, 0, lnbs, False)
742
743                         csw = [("none", _("None")), ("AA", _("AA")), ("AB", _("AB")), ("BA", _("BA")), ("BB", _("BB"))]
744                         for y in range(0, 16):
745                                 csw.append((str(0xF0|y), "Input " + str(y+1)))
746
747                         ucsw = [("none", _("None"))]
748                         for y in range(1, 17):
749                                 ucsw.append("Input " + str(y))
750
751                         nim.advanced.lnb = [0]
752                         for x in range(1, 33):
753                                 nim.advanced.lnb.append(ConfigSubsection())
754                                 nim.advanced.lnb[x].lof = configElement(cname + "advanced.lnb" + str(x) + ".lof", configSelection, 0, (("universal_lnb", _("Universal LNB")), ("c_band", _("C-Band")), ("user_defined", _("User defined"))), False)
755                                 nim.advanced.lnb[x].lofl = configElement(cname + "advanced.lnb" + str(x) + ".lofl", configSequence, [9750], configsequencearg.get("INTEGER", (0, 99999)), False)
756                                 nim.advanced.lnb[x].lofh = configElement(cname + "advanced.lnb" + str(x) + ".lofh", configSequence, [10600], configsequencearg.get("INTEGER", (0, 99999)), False)
757                                 nim.advanced.lnb[x].threshold = configElement(cname + "advanced.lnb" + str(x) + ".threshold", configSequence, [11700], configsequencearg.get("INTEGER", (0, 99999)), False)
758                                 nim.advanced.lnb[x].output_12v = configElement(cname + "advanced.lnb" + str(x) + ".output_12v", configSelection, 0, (("0V", _("0 V")), ("12V", _("12 V"))), False)
759                                 nim.advanced.lnb[x].increased_voltage = configElement(cname + "advanced.lnb" + str(x) + ".increased_voltage", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
760                                 nim.advanced.lnb[x].toneburst = configElement(cname + "advanced.lnb" + str(x) + ".toneburst", configSelection, 0, (("none", _("None")), ("A", _("A")), ("B", _("B"))), False)
761                                 nim.advanced.lnb[x].diseqcMode = configElement(cname + "advanced.lnb" + str(x) + ".diseqcMode", configSelection, 0, (("none", _("None")), ("1_0", _("1.0")), ("1_1", _("1.1")), ("1_2", _("1.2"))), False)
762                                 nim.advanced.lnb[x].commitedDiseqcCommand = configElement(cname + "advanced.lnb" + str(x) + ".commitedDiseqcCommand", configSelection, 0, csw, False)
763                                 nim.advanced.lnb[x].fastDiseqc = configElement(cname + "advanced.lnb" + str(x) + ".fastDiseqc", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
764                                 nim.advanced.lnb[x].sequenceRepeat = configElement(cname + "advanced.lnb" + str(x) + ".sequenceRepeat", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
765                                 nim.advanced.lnb[x].commandOrder1_0 = configElement(cname + "advanced.lnb" + str(x) + ".commandOrder1_0", configSelection, 0, ("committed, toneburst", "toneburst, committed"), False)
766                                 nim.advanced.lnb[x].commandOrder = configElement(cname + "advanced.lnb" + str(x) + ".commandOrder", configSelection, 0, ("committed, toneburst", "toneburst, committed", "committed, uncommitted, toneburst", "toneburst, committed, uncommitted", "uncommitted, committed, toneburst", "toneburst, uncommitted, commmitted"), False)
767                                 nim.advanced.lnb[x].uncommittedDiseqcCommand = configElement(cname + "advanced.lnb" + str(x) + ".uncommittedDiseqcCommand", configSelection, 0, ucsw, False)
768                                 nim.advanced.lnb[x].diseqcRepeats = configElement(cname + "advanced.lnb" + str(x) + ".diseqcRepeats", configSelection, 0, (("none", _("None")), ("one", _("One")), ("two", _("Two")), ("three", _("Three"))), False)
769                                 nim.advanced.lnb[x].longitude = configElement(cname + "advanced.lnb" + str(x) + ".longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,359),(0,999)]), False)
770                                 nim.advanced.lnb[x].longitudeOrientation = configElement(cname + "advanced.lnb" + str(x) + ".longitudeOrientation", configSelection, 0, (("east", _("East")), ("west", _("West"))), False)
771                                 nim.advanced.lnb[x].latitude = configElement(cname + "advanced.lnb" + str(x) + ".latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,359),(0,999)]), False)
772                                 nim.advanced.lnb[x].latitudeOrientation = configElement(cname + "advanced.lnb" + str(x) + ".latitudeOrientation", configSelection, 0, (("north", _("North")), ("south", _("South"))), False)
773                                 nim.advanced.lnb[x].powerMeasurement = configElement(cname + "advanced.lnb" + str(x) + ".powerMeasurement", configSelection, 0, (("yes", _("Yes")), ("no", _("No"))), False)
774                                 nim.advanced.lnb[x].powerThreshold = configElement(cname + "advanced.lnb" + str(x) + ".powerThreshold", configSequence, [50], configsequencearg.get("INTEGER", (0, 100)), False)
775                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
776                         nim.cable = configElement(cname + "cable", configSelection, 0, nimmgr.cablesList, False);
777                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
778                         list = []
779                         for x in nimmgr.terrestrialsList:
780                                 list.append(x[0])
781                         nim.terrestrial = configElement(cname + "terrestrial", configSelection, 0, list, False);
782                         nim.terrestrial_5V = configElement(cname + "terrestrial_5V", configSelection, 1, (("on", _("On")), ("off", _("Off"))), True);
783                 else:
784                         print "pls add support for this frontend type!"         
785
786         nimmgr.sec = SecConfigure(nimmgr)
787
788
789 nimmanager = NimManager()