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