824f9f71df253ee1581db16ffe9c4ef9a2c333d2
[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(11750000)
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(11750000)
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',"")
347                                 self.satellites[tpos] = tname
348                                 self.satList.append( (tname, tpos) )
349                                 self.parsedSat = int(tpos)
350                         elif (name == "transponder"):
351                                 freq = int(attrs.get('frequency',""))
352                                 sr = int(attrs.get('symbol_rate',""))
353                                 pol = int(attrs.get('polarization',""))
354                                 fec = int(attrs.get('fec_inner',""))
355                                 if self.parsedSat in self.transponders:
356                                         pass
357                                 else:
358                                         self.transponders[self.parsedSat] = [ ]
359
360                                 self.transponders[self.parsedSat].append((0, freq, sr, pol, fec))
361
362         class parseCables(ContentHandler):
363                 def __init__(self, cablesList, transponders):
364                         self.isPointsElement, self.isReboundsElement = 0, 0
365                         self.cablesList = cablesList
366                         self.transponders = transponders
367         
368                 def startElement(self, name, attrs):
369                         if (name == "cable"):
370                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
371                                 tname = attrs.get('name',"")
372                                 self.cablesList.append(str(tname))
373                                 self.parsedCab = str(tname)
374                         elif (name == "transponder"):
375                                 freq = int(attrs.get('frequency',""))
376                                 sr = int(attrs.get('symbol_rate',""))
377                                 mod = int(attrs.get('modulation',""))
378                                 fec = int(attrs.get('fec_inner',""))
379                                 if self.parsedCab in self.transponders:
380                                         pass
381                                 else:
382                                         self.transponders[self.parsedCab] = [ ]
383                                 self.transponders[self.parsedCab].append((1, freq, sr, mod, fec))
384
385         class parseTerrestrials(ContentHandler):
386                 def __init__(self, terrestrialsList, transponders):
387                         self.isPointsElement, self.isReboundsElement = 0, 0
388                         self.terrestrialsList = terrestrialsList
389                         self.transponders = transponders
390         
391                 def startElement(self, name, attrs):
392                         if (name == "terrestrial"):
393                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
394                                 tname = attrs.get('name',"").encode("UTF-8")
395                                 tflags = attrs.get('flags',"")
396                                 self.terrestrialsList.append((tname, tflags))
397                                 self.parsedTer = str(tname)
398                         elif (name == "transponder"):
399                                 # TODO finish this!
400                                 freq = int(attrs.get('centre_frequency',""))
401                                 bw = int(attrs.get('bandwidth',""))
402                                 const = int(attrs.get('constellation',""))
403                                 crh = int(attrs.get('code_rate_hp',""))
404                                 crl = int(attrs.get('code_rate_lp',""))
405                                 guard = int(attrs.get('guard_interval',""))
406                                 transm = int(attrs.get('transmission_mode',""))
407                                 hierarchy = int(attrs.get('hierarchy_information',""))
408                                 inv = int(attrs.get('inversion',""))
409                                 if self.parsedTer in self.transponders:
410                                         pass
411                                 else:
412                                         self.transponders[self.parsedTer] = [ ]
413
414                                 self.transponders[self.parsedTer].append((2, freq, bw, const, crh, crl, guard, transm, hierarchy, inv))
415
416         def getTransponders(self, pos):
417                 if self.transponders.has_key(pos):
418                         return self.transponders[pos]
419                 else:
420                         return []
421
422         def getTranspondersCable(self, cable):
423                 return self.transponderscable[cable]
424
425         def getTranspondersTerrestrial(self, region):
426                 return self.transpondersterrestrial[region]
427         
428         def getCableDescription(self, nim):
429                 return self.cablesList[config.Nims[nim].cable.value]
430
431         def getTerrestrialDescription(self, nim):
432                 return self.terrestrialsList[config.Nims[nim].terrestrial.value][0]
433
434         def getTerrestrialFlags(self, nim):
435                 return self.terrestrialsList[config.Nims[nim].terrestrial.value][1]
436
437         def getConfiguredSats(self):
438                 return self.sec.getSatList()
439
440         def getSatDescription(self, pos):
441                 return self.satellites[pos]
442
443         def readSatsfromFile(self):
444                 self.satellites = { }
445                 self.transponders = { }
446                 self.transponderscable = { }
447                 self.transpondersterrestrial = { }              
448                 
449                 parser = make_parser()
450                 if (self.hasNimType(self.nimType["DVB-S"])):
451                         print "Reading satellites.xml"
452                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
453                         parser.setContentHandler(satHandler)
454                         parser.parse('/etc/tuxbox/satellites.xml')
455                 if (self.hasNimType(self.nimType["DVB-C"])):
456                         print "Reading cables.xml"
457                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
458                         parser.setContentHandler(cabHandler)
459                         parser.parse('/etc/tuxbox/cables.xml')
460
461                 if (self.hasNimType(self.nimType["DVB-T"])):
462                         print "Reading terrestrial.xml"
463                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
464                         parser.setContentHandler(terHandler)
465                         parser.parse('/etc/tuxbox/terrestrial.xml')
466                 
467         def parseProc(self):
468                 self.nimTypes = {}
469                 self.nimNames = {}              
470                 self.nimSocketCount = 0
471                 nimfile = tryOpen("/proc/bus/nim_sockets")
472
473                 if nimfile == "":
474                                 return self.nimType["empty/unknown"]
475                         
476                 lastsocket = -1
477
478                 while 1:                
479                         line = nimfile.readline()
480                         if line == "":
481                                 break
482                         if line.strip().startswith("NIM Socket"):
483                                 parts = line.strip().split(" ")
484                                 id = int(parts[2][:1])
485                                 lastsocket = int(id)
486                                 self.nimSocketCount += 1
487                         elif line.strip().startswith("Type:"):
488                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
489                                 #self.nimTypes[lastsocket] = str("DVB-T")
490                         elif line.strip().startswith("Name:"):
491                                 self.nimNames[lastsocket] = str(line.strip()[6:])
492                         elif line.strip().startswith("empty"):
493                                 self.nimNames[lastsocket] = _("N/A")
494                                 self.nimTypes[lastsocket] = "empty/unknown"
495
496                 nimfile.close()
497
498         def getNimType(self, slotID):
499                 if slotID >= self.nimCount:
500                         return self.nimType["empty/unknown"]
501                 else:   
502                         return self.nimType[self.nimTypes[slotID]]
503                         
504         def getNimTypeName(self, slotID):
505                 if slotID >= self.nimCount:
506                         return "empty/unknown"
507                 else:   
508                         return self.nimTypes[slotID]
509                 
510         def getNimName(self, slotID):
511                 return self.nimNames[slotID]
512
513         def getNimSocketCount(self):
514                 return self.nimSocketCount
515         
516         def hasNimType(self, chktype):
517                 for id, type in self.nimTypes.items():
518                         if (chktype == self.nimType[str(type)]):
519                                 return True
520                 return False
521         
522         def getNimListOfType(self, type, exception = -1):
523                 list = []
524                 for x in self.nimslots:
525                         if ((x.nimType == type) and (x.slotid != exception)):
526                                 list.append(x.slotid)
527                 return list
528
529         def getConfigPrefix(self, slotid):
530                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
531                         
532         def __init__(self):
533                 #use as enum
534                 self.nimType = {                "empty/unknown": -1,
535                                                                                                 "DVB-S": 0,
536                                                                                                 "DVB-C": 1,
537                                                                                                 "DVB-T": 2}
538                 self.satList = [ ]
539                 self.cablesList = []
540                 self.terrestrialsList = []
541                                                                                                 
542                 self.parseProc()
543
544                 self.readSatsfromFile()                                                 
545                 
546                 self.nimCount = self.getNimSocketCount()
547                 
548                 self.nimslots = [ ]
549                 x = 0
550                 while x < self.nimCount:
551                         tType = self.getNimType(x)
552                         tName = self.getNimName(x)
553                         tNim = nimSlot(x, tType, tName)
554                         self.nimslots.append(tNim)
555                         x += 1
556                 
557                 InitNimManager(self)    #init config stuff
558
559         def nimList(self):
560                 list = [ ]
561                 for slot in self.nimslots:
562                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
563                         if slot.nimType == -1:
564                                 nimText += _("empty/unknown")
565                         else:
566                                 nimText += slot.name + " ("     
567                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
568                         list.append((nimText, slot))
569                 return list
570
571         def getNimConfigMode(self, slotid):
572                 return currentConfigSelectionElement(config.Nims[slotid].configMode)
573         
574         def getSatList(self):
575                 return self.satList
576
577         def getSatListForNim(self, slotid):
578                 list = []
579                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
580                         #print "slotid:", slotid
581                         
582                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
583                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
584                         configMode = currentConfigSelectionElement(config.Nims[slotid].configMode)
585                         if configMode == "simple":
586                                 if (config.Nims[slotid].diseqcMode.value <= 3):
587                                         list.append(self.satList[config.Nims[slotid].diseqcA.value])
588                                 if (0 < config.Nims[slotid].diseqcMode.value <= 3):
589                                         list.append(self.satList[config.Nims[slotid].diseqcB.value])
590                                 if (config.Nims[slotid].diseqcMode.value == 3):
591                                         list.append(self.satList[config.Nims[slotid].diseqcC.value])
592                                         list.append(self.satList[config.Nims[slotid].diseqcD.value])
593                                 if (config.Nims[slotid].diseqcMode.value == 4):
594                                         for x in self.satList:
595                                                 list.append(x)
596                         elif configMode == "advanced":
597                                 for x in self.satList:
598                                         if config.Nims[slotid].advanced.sat[x[1]].lnb.value != 0:
599                                                 list.append(x)
600                 return list
601
602         def getRotorSatListForNim(self, slotid):
603                 list = []
604                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
605                         #print "slotid:", slotid
606
607                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
608                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
609                         configMode = currentConfigSelectionElement(config.Nims[slotid].configMode)
610                         if configMode == "simple":
611                                 if (config.Nims[slotid].diseqcMode.value == 4):
612                                         for x in self.satList:
613                                                 list.append(x)
614                         elif configMode == "advanced":
615                                 for x in self.satList:
616                                         nim = config.Nims[slotid]
617                                         lnbnum = nim.advanced.sat[x[1]].lnb.value
618                                         if lnbnum != 0:
619                                                 lnb = nim.advanced.lnb[lnbnum]
620                                                 if lnb.diseqcMode.value == 3: # diseqc 1.2
621                                                         list.append(x)
622                 return list
623
624         def nimDiseqcModeChanged(self, slotid, mode):
625                 #print "nimDiseqcModeChanged set to " + str(mode)
626                 pass
627         def nimPortAChanged(self, slotid, val):
628                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
629                 pass
630         def nimPortBChanged(self, slotid, val):
631                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
632                 #print "nimDiseqcB set to " + str(val)
633                 pass
634         def nimPortCChanged(self, slotid, val):
635                 #print "nimDiseqcC set to " + str(val)
636                 pass
637         def nimPortDChanged(self, slotid, val):
638                 #print "nimDiseqcD set to " + str(val)
639                 pass
640
641 def InitNimManager(nimmgr):
642         config.Nims = []
643         for x in range(nimmgr.nimCount):
644                 config.Nims.append(ConfigSubsection())
645                 
646 #       def nimConfigModeChanged(slotid, configElement):
647 #               nimmgr.nimConfigModeChanged(slotid, configElement.value)
648         def nimDiseqcModeChanged(slotid, configElement):
649                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
650                 
651         def nimPortAChanged(slotid, configElement):
652                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
653         def nimPortBChanged(slotid, configElement):
654                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
655         def nimPortCChanged(slotid, configElement):
656                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
657         def nimPortDChanged(slotid, configElement):
658                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
659
660         for slot in nimmgr.nimslots:
661                 x = slot.slotid
662                 cname = nimmgr.getConfigPrefix(x)
663                 nim = config.Nims[x]
664                 
665                 if slot.nimType == nimmgr.nimType["DVB-S"]:
666                         if slot.slotid == 0:
667                                 nim.configMode = configElement(cname + "configMode", configSelection, 0, (
668                                 ("simple", _("Simple")), ("advanced", _("Advanced"))), False)
669                         else:
670                                 nim.configMode = configElement(cname + "configMode", configSelection, 0, (
671                                 ("equal", _("Equal to Socket A")),
672                                 ("loopthrough", _("Loopthrough to Socket A")),
673                                 ("nothing", _("Nothing connected")),
674                                 ("satposdepends", _("Secondary cable from motorized LNB")),
675                                 ("simple", _("Simple")),
676                                 ("advanced", _("Advanced"))), False)
677                         #important - check if just the 2nd one is LT only and the first one is DVB-S
678                         if currentConfigSelectionElement(nim.configMode) in ["loopthrough", "satposdepends", "equal"]:
679                                 if x == 0:                                                                              #first one can never be linked to anything
680                                         nim.configMode.value = getConfigSelectionElement(nim.configMode, "simple")              #reset to simple
681                                         nim.configMode.save()
682                                 else:
683                                         #FIXME: make it better
684                                         for y in nimmgr.nimslots:
685                                                 if y.slotid == 0:
686                                                         if y.nimType != nimmgr.nimType["DVB-S"]:
687                                                                 nim.configMode.value = getConfigSelectionElement(nim.configMode, "simple")              #reset to simple
688                                                                 nim.configMode.save()
689
690                         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);
691                         nim.diseqcA = configElement(cname + "diseqcA", configSatlist, 192, nimmgr.satList, False);
692                         nim.diseqcB = configElement(cname + "diseqcB", configSatlist, 130, nimmgr.satList, False);
693                         nim.diseqcC = configElement(cname + "diseqcC", configSatlist, 0, nimmgr.satList, False);
694                         nim.diseqcD = configElement(cname + "diseqcD", configSatlist, 0, nimmgr.satList, False);
695                         nim.positionerMode = configElement(cname + "positionerMode", configSelection, 0, (("usals", _("USALS")), ("manual", _("manual"))), False);
696                         nim.longitude = configElement(cname + "longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,90),(0,999)]), False);
697                         nim.longitudeOrientation = configElement(cname + "longitudeOrientation", configSelection, 0, (("east", _("East")), ("west", _("West"))), False)
698                         nim.latitude = configElement(cname + "latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,90),(0,999)]), False);
699                         nim.latitudeOrientation = configElement(cname + "latitudeOrientation", configSelection, 0, (("north", _("North")), ("south", _("South"))), False)
700                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
701                         satNimListNames = []
702                         for x in satNimList:
703                                 satNimListNames.append((("Slot_" + ("A", "B", "C", "D")[x] + "_" + nimmgr.getNimName(x)), _("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x)))
704                         nim.equalTo = configElement(cname + "equalTo", configSelection, 0, satNimListNames, False);
705                         nim.linkedTo = configElement(cname + "linkedTo", configSelection, 0, satNimListNames, False);
706                         nim.satposDependsTo = configElement(cname + "satposDependsTo", configSelection, 0, satNimListNames, False);
707                         
708                         #perhaps the instance of the slot is more useful?
709 #                       nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
710                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
711                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
712                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
713                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
714                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
715                         
716                         # advanced config:
717                         nim.advanced = ConfigSubsection()
718                         nim.advanced.sats = configElement(cname + "advanced.sats", configSatlist, 192, nimmgr.satList, False);
719                         nim.advanced.sat = {}
720                         lnbs = ["not available"]
721                         for y in range(1, 33):
722                                 lnbs.append("LNB " + str(y))
723                         for x in nimmgr.satList:
724                                 nim.advanced.sat[x[1]] = ConfigSubsection()
725                                 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)
726                                 nim.advanced.sat[x[1]].tonemode = configElement(cname + "advanced.sat" + str(x[1]) + ".tonemode", configSelection, 0, (("band", _("Band")), ("on", _("On")), ("off", _("Off"))), False)
727                                 nim.advanced.sat[x[1]].usals = configElement(cname + "advanced.sat" + str(x[1]) + ".usals", configSelection, 0, (("yes", _("Yes")), ("no", _("No"))), False)
728                                 nim.advanced.sat[x[1]].rotorposition = configElement(cname + "advanced.sat" + str(x[1]) + ".rotorposition", configSequence, [1], configsequencearg.get("INTEGER", (1, 255)), False)
729                                 nim.advanced.sat[x[1]].lnb = configElement(cname + "advanced.sat" + str(x[1]) + ".lnb", configSelection, 0, lnbs, False)
730
731                         csw = [("none", _("None")), ("AA", _("AA")), ("AB", _("AB")), ("BA", _("BA")), ("BB", _("BB"))]
732                         for y in range(0, 16):
733                                 csw.append((str(0xF0|y), "Input " + str(y+1)))
734
735                         ucsw = [("none", _("None"))]
736                         for y in range(1, 17):
737                                 ucsw.append("Input " + str(y))
738
739                         nim.advanced.lnb = [0]
740                         for x in range(1, 33):
741                                 nim.advanced.lnb.append(ConfigSubsection())
742                                 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)
743                                 nim.advanced.lnb[x].lofl = configElement(cname + "advanced.lnb" + str(x) + ".lofl", configSequence, [9750], configsequencearg.get("INTEGER", (0, 99999)), False)
744                                 nim.advanced.lnb[x].lofh = configElement(cname + "advanced.lnb" + str(x) + ".lofh", configSequence, [10600], configsequencearg.get("INTEGER", (0, 99999)), False)
745                                 nim.advanced.lnb[x].threshold = configElement(cname + "advanced.lnb" + str(x) + ".threshold", configSequence, [11750], configsequencearg.get("INTEGER", (0, 99999)), False)
746                                 nim.advanced.lnb[x].output_12v = configElement(cname + "advanced.lnb" + str(x) + ".output_12v", configSelection, 0, (("0V", _("0 V")), ("12V", _("12 V"))), False)
747                                 nim.advanced.lnb[x].increased_voltage = configElement(cname + "advanced.lnb" + str(x) + ".increased_voltage", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
748                                 nim.advanced.lnb[x].toneburst = configElement(cname + "advanced.lnb" + str(x) + ".toneburst", configSelection, 0, (("none", _("None")), ("A", _("A")), ("B", _("B"))), False)
749                                 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)
750                                 nim.advanced.lnb[x].commitedDiseqcCommand = configElement(cname + "advanced.lnb" + str(x) + ".commitedDiseqcCommand", configSelection, 0, csw, False)
751                                 nim.advanced.lnb[x].fastDiseqc = configElement(cname + "advanced.lnb" + str(x) + ".fastDiseqc", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
752                                 nim.advanced.lnb[x].sequenceRepeat = configElement(cname + "advanced.lnb" + str(x) + ".sequenceRepeat", configSelection, 0, (("no", _("No")), ("yes", _("Yes"))), False)
753                                 nim.advanced.lnb[x].commandOrder1_0 = configElement(cname + "advanced.lnb" + str(x) + ".commandOrder1_0", configSelection, 0, ("committed, toneburst", "toneburst, committed"), False)
754                                 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)
755                                 nim.advanced.lnb[x].uncommittedDiseqcCommand = configElement(cname + "advanced.lnb" + str(x) + ".uncommittedDiseqcCommand", configSelection, 0, ucsw, False)
756                                 nim.advanced.lnb[x].diseqcRepeats = configElement(cname + "advanced.lnb" + str(x) + ".diseqcRepeats", configSelection, 0, (("none", _("None")), ("one", _("One")), ("two", _("Two")), ("three", _("Three"))), False)
757                                 nim.advanced.lnb[x].longitude = configElement(cname + "advanced.lnb" + str(x) + ".longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,90),(0,999)]), False)
758                                 nim.advanced.lnb[x].longitudeOrientation = configElement(cname + "advanced.lnb" + str(x) + ".longitudeOrientation", configSelection, 0, (("east", _("East")), ("west", _("West"))), False)
759                                 nim.advanced.lnb[x].latitude = configElement(cname + "advanced.lnb" + str(x) + ".latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,90),(0,999)]), False)
760                                 nim.advanced.lnb[x].latitudeOrientation = configElement(cname + "advanced.lnb" + str(x) + ".latitudeOrientation", configSelection, 0, (("north", _("North")), ("south", _("South"))), False)
761                                 nim.advanced.lnb[x].powerMeasurement = configElement(cname + "advanced.lnb" + str(x) + ".powerMeasurement", configSelection, 0, (("yes", _("Yes")), ("no", _("No"))), False)
762                                 nim.advanced.lnb[x].powerThreshold = configElement(cname + "advanced.lnb" + str(x) + ".powerThreshold", configSequence, [50], configsequencearg.get("INTEGER", (0, 100)), False)
763                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
764                         nim.cable = configElement(cname + "cable", configSelection, 0, nimmgr.cablesList, False);
765                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
766                         list = []
767                         for x in nimmgr.terrestrialsList:
768                                 list.append(x[0])
769                         nim.terrestrial = configElement(cname + "terrestrial", configSelection, 0, list, False);
770                 else:
771                         print "pls add support for this frontend type!"         
772
773         nimmgr.sec = SecConfigure(nimmgr)
774
775 nimmanager = NimManager()