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