fix dvb-t scan, config
[enigma2.git] / lib / python / Components / NimManager.py
1 from config import config, ConfigSubsection, ConfigSelection, ConfigFloat, ConfigSatlist, ConfigYesNo, ConfigInteger, ConfigSubList, ConfigNothing, ConfigSubDict, ConfigOnOff
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))
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[0]
414         
415         def getCableTrustNit(self, nim):
416                 return (config.Nims[nim].cabletype.value == "quick")
417
418         def getTerrestrialDescription(self, nim):
419                 return self.terrestrialsList[config.Nims[nim].terrestrial.index][0]
420
421         def getTerrestrialFlags(self, nim):
422                 return self.terrestrialsList[config.Nims[nim].terrestrial.index][1]
423
424         def getConfiguredSats(self):
425                 return self.sec.getSatList()
426
427         def getSatDescription(self, pos):
428                 return self.satellites[pos]
429
430         def readSatsfromFile(self):
431                 self.satellites = { }
432                 self.transponders = { }
433                 self.transponderscable = { }
434                 self.transpondersterrestrial = { }              
435                 
436                 parser = make_parser()
437                 if (self.hasNimType(self.nimType["DVB-S"])):
438                         print "Reading satellites.xml"
439                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
440                         parser.setContentHandler(satHandler)
441                         parser.parse('/etc/tuxbox/satellites.xml')
442                 if (self.hasNimType(self.nimType["DVB-C"])):
443                         print "Reading cables.xml"
444                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
445                         parser.setContentHandler(cabHandler)
446                         parser.parse('/etc/tuxbox/cables.xml')
447
448                 if (self.hasNimType(self.nimType["DVB-T"])):
449                         print "Reading terrestrial.xml"
450                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
451                         parser.setContentHandler(terHandler)
452                         parser.parse('/etc/tuxbox/terrestrial.xml')
453                 
454         def parseProc(self):
455                 self.nimTypes = {}
456                 self.nimNames = {}              
457                 self.nimSocketCount = 0
458                 nimfile = tryOpen("/proc/bus/nim_sockets")
459
460                 if nimfile == "":
461                                 return self.nimType["empty/unknown"]
462                         
463                 lastsocket = -1
464
465                 while 1:                
466                         line = nimfile.readline()
467                         if line == "":
468                                 break
469                         if line.strip().startswith("NIM Socket"):
470                                 parts = line.strip().split(" ")
471                                 id = int(parts[2][:1])
472                                 lastsocket = int(id)
473                                 self.nimSocketCount += 1
474                         elif line.strip().startswith("Type:"):
475                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
476                                 #self.nimTypes[lastsocket] = str("DVB-T")
477                         elif line.strip().startswith("Name:"):
478                                 self.nimNames[lastsocket] = str(line.strip()[6:])
479                         elif line.strip().startswith("empty"):
480                                 self.nimNames[lastsocket] = _("N/A")
481                                 self.nimTypes[lastsocket] = "empty/unknown"
482
483                 nimfile.close()
484
485         def getNimType(self, slotID):
486                 if slotID >= self.nimCount:
487                         return self.nimType["empty/unknown"]
488                 else:   
489                         return self.nimType[self.nimTypes[slotID]]
490                         
491         def getNimTypeName(self, slotID):
492                 if slotID >= self.nimCount:
493                         return "empty/unknown"
494                 else:   
495                         return self.nimTypes[slotID]
496                 
497         def getNimName(self, slotID):
498                 return self.nimNames[slotID]
499
500         def getNimSocketCount(self):
501                 return self.nimSocketCount
502         
503         def hasNimType(self, chktype):
504                 for id, type in self.nimTypes.items():
505                         if (chktype == self.nimType[str(type)]):
506                                 return True
507                 return False
508         
509         def getNimListOfType(self, type, exception = -1):
510                 list = []
511                 for x in self.nimslots:
512                         if ((x.nimType == type) and (x.slotid != exception)):
513                                 list.append(x.slotid)
514                 return list
515
516         def getConfigPrefix(self, slotid):
517                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
518                         
519         def __init__(self):
520                 #use as enum
521                 self.nimType = {                "empty/unknown": -1,
522                                                                                                 "DVB-S": 0,
523                                                                                                 "DVB-C": 1,
524                                                                                                 "DVB-T": 2}
525                 self.satList = [ ]
526                 self.cablesList = []
527                 self.terrestrialsList = []
528                                                                                                 
529                 self.parseProc()
530
531                 self.readSatsfromFile()                                                 
532                 
533                 self.nimCount = self.getNimSocketCount()
534                 
535                 self.nimslots = [ ]
536                 x = 0
537                 while x < self.nimCount:
538                         tType = self.getNimType(x)
539                         tName = self.getNimName(x)
540                         tNim = nimSlot(x, tType, tName)
541                         self.nimslots.append(tNim)
542                         x += 1
543                 
544                 InitNimManager(self)    #init config stuff
545
546         def nimList(self):
547                 list = [ ]
548                 for slot in self.nimslots:
549                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
550                         if slot.nimType == -1:
551                                 nimText += _("empty/unknown")
552                         else:
553                                 nimText += slot.name + " ("     
554                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
555                         list.append((nimText, slot))
556                 return list
557
558         def getNimConfigMode(self, slotid):
559                 return config.Nims[slotid].configMode.value
560         
561         def getSatList(self):
562                 return self.satList
563
564         def getSatListForNim(self, slotid):
565                 list = []
566                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
567                         #print "slotid:", slotid
568                         
569                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
570                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
571                         configMode = config.Nims[slotid].configMode.value
572
573                         if configMode == "equal":
574                                 slotid=0 #FIXME add handling for more than two tuners !!!
575                                 configMode = config.Nims[slotid].configMode.value
576
577                         if configMode == "simple":
578                                 dm = config.Nims[slotid].diseqcMode.value
579                                 if dm in ["single", "toneburst", "diseqc_a_b", "diseqc_a_b_c_d"]:
580                                         list.append(self.satList[config.Nims[slotid].diseqcA.index])
581                                 if dm in ["toneburst", "diseqc_a_b", "diseqc_a_b_c_d"]:
582                                         list.append(self.satList[config.Nims[slotid].diseqcB.index])
583                                 if dm == "diseqc_a_b_c_d":
584                                         list.append(self.satList[config.Nims[slotid].diseqcC.index])
585                                         list.append(self.satList[config.Nims[slotid].diseqcD.index])
586                                 if dm == "positioner":
587                                         for x in self.satList:
588                                                 list.append(x)
589                         elif configMode == "advanced":
590                                 for x in self.satList:
591                                         if int(config.Nims[slotid].advanced.sat[x[0]].lnb.value) != 0:
592                                                 list.append(x)
593
594                 return list
595
596         def getRotorSatListForNim(self, slotid):
597                 list = []
598                 if (self.getNimType(slotid) == self.nimType["DVB-S"]):
599                         #print "slotid:", slotid
600
601                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
602                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
603                         configMode = config.Nims[slotid].configMode.value
604                         if configMode == "simple":
605                                 if config.Nims[slotid].diseqcMode.value == "positioner":
606                                         for x in self.satList:
607                                                 list.append(x)
608                         elif configMode == "advanced":
609                                 for x in self.satList:
610                                         nim = config.Nims[slotid]
611                                         lnbnum = int(nim.advanced.sat[x[0]].lnb.value)
612                                         if lnbnum != 0:
613                                                 lnb = nim.advanced.lnb[lnbnum]
614                                                 if lnb.diseqcMode.value == "1_2":
615                                                         list.append(x)
616                 return list
617
618         def nimDiseqcModeChanged(self, slotid, mode):
619                 #print "nimDiseqcModeChanged set to " + str(mode)
620                 pass
621         def nimPortAChanged(self, slotid, val):
622                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
623                 pass
624         def nimPortBChanged(self, slotid, val):
625                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
626                 #print "nimDiseqcB set to " + str(val)
627                 pass
628         def nimPortCChanged(self, slotid, val):
629                 #print "nimDiseqcC set to " + str(val)
630                 pass
631         def nimPortDChanged(self, slotid, val):
632                 #print "nimDiseqcD set to " + str(val)
633                 pass
634
635 def InitNimManager(nimmgr):
636         config.Nims = ConfigSubList()
637         for x in range(nimmgr.nimCount):
638                 config.Nims.append(ConfigSubsection())
639                 
640 #       def nimConfigModeChanged(slotid, configElement):
641 #               nimmgr.nimConfigModeChanged(slotid, configElement.value)
642         def nimDiseqcModeChanged(slotid, configElement):
643                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
644                 
645         def nimPortAChanged(slotid, configElement):
646                 nimmgr.nimPortAChanged(slotid, configElement.value)
647         def nimPortBChanged(slotid, configElement):
648                 nimmgr.nimPortBChanged(slotid, configElement.value)
649         def nimPortCChanged(slotid, configElement):
650                 nimmgr.nimPortCChanged(slotid, configElement.value)
651         def nimPortDChanged(slotid, configElement):
652                 nimmgr.nimPortDChanged(slotid, configElement.value)
653
654         for slot in nimmgr.nimslots:
655                 x = slot.slotid
656                 cname = nimmgr.getConfigPrefix(x)
657                 nim = config.Nims[x]
658                 
659                 if slot.nimType == nimmgr.nimType["DVB-S"]:
660                         if slot.slotid == 0:
661                                 nim.configMode = ConfigSelection(
662                                         choices = {
663                                                 "simple": _("simple"),
664                                                 "advanced": _("advanced")},
665                                         default = "simple")
666                         else:
667
668                                 nim.configMode = ConfigSelection(
669                                         choices = {
670                                                 "equal": _("equal to Socket A"),
671                                                 "looptrough": _("loopthrough to socket A"),
672                                                 "nothing": _("nothing connected"),
673                                                 "satposdepends": _("second cable of motorized LNB"),
674                                                 "simple": _("simple"),
675                                                 "advanced": _("advanced")},
676                                         default = "looptrough")
677
678                         #important - check if just the 2nd one is LT only and the first one is DVB-S
679                         if nim.configMode.value in ["loopthrough", "satposdepends", "equal"]:
680                                 if x == 0: # first one can never be linked to anything
681                                         # reset to simple
682                                         nim.configMode.value = "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                                                                 # reset to simple
690                                                                 nim.configMode.value = "simple"
691                                                                 nim.configMode.save()
692
693                         nim.diseqcMode = ConfigSelection(
694                                 choices = [
695                                         ("single", _("Single")),
696                                         ("toneburst_a_b", _("Toneburst A/B")),
697                                         ("diseqc_a_b", _("DiSEqC A/B")),
698                                         ("diseqc_a_b_c_d", _("DiSEqC A/B/C/D")),
699                                         ("positioner", _("Positioner"))],
700                                 default = "diseqc_a_b")
701                         nim.diseqcA = ConfigSatlist(default = 192, list = nimmgr.satList)
702                         nim.diseqcB = ConfigSatlist(default = 130, list = nimmgr.satList)
703                         nim.diseqcC = ConfigSatlist(list = nimmgr.satList)
704                         nim.diseqcD = ConfigSatlist(list = nimmgr.satList)
705                         nim.positionerMode = ConfigSelection(
706                                 choices = [
707                                         ("usals", _("USALS")),
708                                         ("manual", _("manual"))],
709                                 default = "usals")
710                         nim.longitude = ConfigFloat(default=[5,100], limits=[(0,359),(0,999)])
711                         nim.longitudeOrientation = ConfigSelection(choices={"east": _("East"), "west": _("West")}, default = "east")
712                         nim.latitude = ConfigFloat(default=[50,767], limits=[(0,359),(0,999)])
713                         nim.latitudeOrientation = ConfigSelection(choices={"north": _("North"), "south": _("South")}, default="north")
714                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
715                         satNimListNames = {}
716                         for x in satNimList:
717                                 satNimListNames["Slot_" + ("A", "B", "C", "D")[x] + "_" + nimmgr.getNimName(x)] = _("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x)
718                         if len(satNimListNames):
719                                 nim.equalTo = ConfigSelection(choices = satNimListNames)
720                                 nim.linkedTo = ConfigSelection(choices = satNimListNames)
721                                 nim.satposDependsTo = ConfigSelection(choices = satNimListNames)
722                         
723                         #perhaps the instance of the slot is more useful?
724 #                       nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
725                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
726                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
727                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
728                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
729                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
730                         
731                         # advanced config:
732                         nim.advanced = ConfigSubsection()
733                         nim.advanced.sats = ConfigSatlist(default = 192, list = nimmgr.satList)
734                         nim.advanced.sat = ConfigSubDict()
735                         lnbs = [("0", "not available")]
736                         for y in range(1, 33):
737                                 lnbs.append((str(y), "LNB " + str(y)))
738
739                         for x in nimmgr.satList:
740                                 nim.advanced.sat[x[0]] = ConfigSubsection()
741                                 nim.advanced.sat[x[0]].voltage = ConfigSelection(choices={"polarization": _("Polarization"), "13V": _("13 V"), "18V": _("18 V")}, default = "polarization")
742                                 nim.advanced.sat[x[0]].tonemode = ConfigSelection(choices={"band": _("Band"), "on": _("On"), "off": _("Off")}, default = "band")
743                                 nim.advanced.sat[x[0]].usals = ConfigYesNo(default=True)
744                                 nim.advanced.sat[x[0]].rotorposition = ConfigInteger(default=1, limits=(1, 255))
745                                 nim.advanced.sat[x[0]].lnb = ConfigSelection(choices = lnbs)
746
747                         csw = [("none", _("None")), ("AA", _("AA")), ("AB", _("AB")), ("BA", _("BA")), ("BB", _("BB"))]
748                         for y in range(0, 16):
749                                 csw.append((str(0xF0|y), "Input " + str(y+1)))
750                         
751                         ucsw = [("0", _("None"))]
752                         for y in range(1, 17):
753                                 ucsw.append((str(y), "Input " + str(y)))
754                         
755                         nim.advanced.lnb = ConfigSubList()
756                         nim.advanced.lnb.append(ConfigNothing())
757                         for x in range(1, 33):
758                                 nim.advanced.lnb.append(ConfigSubsection())
759                                 nim.advanced.lnb[x].lof = ConfigSelection(choices={"universal_lnb": _("Universal LNB"), "c_band": _("C-Band"), "user_defined": _("User defined")}, default="universal_lnb")
760                                 nim.advanced.lnb[x].lofl = ConfigInteger(default=9750, limits = (0, 99999))
761                                 nim.advanced.lnb[x].lofh = ConfigInteger(default=10600, limits = (0, 99999))
762                                 nim.advanced.lnb[x].threshold = ConfigInteger(default=11700, limits = (0, 99999))
763                                 nim.advanced.lnb[x].output_12v = ConfigSelection(choices = [("0V", _("0 V")), ("12V", _("12 V"))], default="0V")
764                                 nim.advanced.lnb[x].increased_voltage = ConfigYesNo(default=False)
765                                 nim.advanced.lnb[x].toneburst = ConfigSelection(choices = [("none", _("None")), ("A", _("A")), ("B", _("B"))], default = "none")
766                                 nim.advanced.lnb[x].diseqcMode = ConfigSelection(choices = [("none", _("None")), ("1_0", _("1.0")), ("1_1", _("1.1")), ("1_2", _("1.2"))], default = "none")
767                                 nim.advanced.lnb[x].commitedDiseqcCommand = ConfigSelection(choices = csw)
768                                 nim.advanced.lnb[x].fastDiseqc = ConfigYesNo(default=False)
769                                 nim.advanced.lnb[x].sequenceRepeat = ConfigYesNo(default=False)
770                                 nim.advanced.lnb[x].commandOrder1_0 = ConfigSelection(choices = [("ct", "committed, toneburst"), ("tc", "toneburst, committed")], default = "ct")
771                                 nim.advanced.lnb[x].commandOrder = ConfigSelection(choices = [
772                                                 ("ct", "committed, toneburst"), 
773                                                 ("tc", "toneburst, committed"), 
774                                                 ("cut", "committed, uncommitted, toneburst"), 
775                                                 ("tcu", "toneburst, committed, uncommitted"), 
776                                                 ("uct", "uncommitted, committed, toneburst"), 
777                                                 ("tuc", "toneburst, uncommitted, commmitted")],
778                                                 default="ct")
779                                 nim.advanced.lnb[x].uncommittedDiseqcCommand = ConfigSelection(choices = ucsw)
780                                 nim.advanced.lnb[x].diseqcRepeats = ConfigSelection(choices = [("none", _("None")), ("one", _("One")), ("two", _("Two")), ("three", _("Three"))], default = "none")
781                                 nim.advanced.lnb[x].longitude = ConfigFloat(default = [5,100], limits = [(0,359),(0,999)])
782                                 nim.advanced.lnb[x].longitudeOrientation = ConfigSelection(choices = [("east", _("East")), ("west", _("West"))], default = "east")
783                                 nim.advanced.lnb[x].latitude = ConfigFloat(default = [50,767], limits = [(0,359),(0,999)])
784                                 nim.advanced.lnb[x].latitudeOrientation = ConfigSelection(choices = [("north", _("North")), ("south", _("South"))], default = "north")
785                                 nim.advanced.lnb[x].powerMeasurement = ConfigYesNo(default=True)
786                                 nim.advanced.lnb[x].powerThreshold = ConfigInteger(default=50, limits=(0, 100))
787
788                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
789                         nim.cabletype = ConfigSelection(choices = [("quick", _("Quick")), ("complete", _("Complete"))])
790                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
791                         list = []
792                         n = 0
793                         for x in nimmgr.terrestrialsList:
794                                 list.append((str(n), x[0]))
795                                 n += 1
796                         nim.terrestrial = ConfigSelection(choices = list)
797                         nim.terrestrial_5V = ConfigOnOff()
798                 else:
799                         print "pls add support for this frontend type!"         
800 #                       assert False
801
802         nimmgr.sec = SecConfigure(nimmgr)
803
804
805 nimmanager = NimManager()