change parameters of diseqc-config to prepare the addition of positioners
[enigma2.git] / lib / python / Components / NimManager.py
1 from config import config       #global config instance
2
3 from config import configElement
4 from config import ConfigSubsection
5 from config import ConfigSlider
6 from config import configSelection
7 from config import configSequence
8 from config import configsequencearg
9 from config import configSatlist
10
11 from enigma import *
12
13 import xml.dom.minidom
14 from xml.dom import EMPTY_NAMESPACE
15 from skin import elementsWithTag
16 from Tools import XMLTools
17
18 from xml.sax import make_parser
19 from xml.sax.handler import ContentHandler
20
21 from Tools.BoundFunction import boundFunction
22
23 def tryOpen(filename):
24         try:
25                 procFile = open(filename)
26         except IOError:
27                 return ""
28         return procFile
29
30 class SecConfigure:
31         def addLNBSimple(self, slotid, diseqcmode, toneburstmode = 0, diseqcpos = 0, orbpos = 0, longitude = 0, latitude = 0, loDirection = 0, laDirection = 0, satList = None):
32                 #simple defaults
33                 sec = eDVBSatelliteEquipmentControl.getInstance()
34                 sec.addLNB()
35                 sec.setLNBTunerMask(1 << slotid)
36                 sec.setLNBLOFL(9750000)
37                 sec.setLNBLOFH(10600000)
38                 sec.setLNBThreshold(11750000)
39                 sec.setRepeats(0)
40                 sec.setFastDiSEqC(0)
41                 sec.setSeqRepeat(0)
42                 sec.setVoltageMode(0) #HV
43                 sec.setToneMode(0)              #HILO
44                 sec.setCommandOrder(0)
45                 #user values
46                 sec.setDiSEqCMode(diseqcmode)
47                 sec.setToneburst(toneburstmode)
48                 sec.setCommittedCommand(diseqcpos)
49                 #print "set orbpos to:" + str(orbpos)
50                 sec.addSatellite(orbpos)
51                 self.satList.append(orbpos)
52
53         def linkNIMs(self, nim1, nim2):
54                 eDVBSatelliteEquipmentControl.getInstance().setTunerLinked(nim1, nim2)
55                 
56         def getSatList(self):
57                 return self.satList
58
59         def update(self):
60                 eDVBSatelliteEquipmentControl.getInstance().clear()
61                 
62                 self.satList = []
63
64                 for slot in self.NimManager.nimslots:
65                         x = slot.slotid
66                         nim = config.Nims[x]
67                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
68                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
69                                 if nim.configMode.value == 1:
70                                         self.linkNIMs(x, nim.linkedTo.value)
71                                         nim = config.Nims[nim.linkedTo.value]
72                                 if nim.configMode.value == 0:           #simple config
73                                         if nim.diseqcMode.value == 0:                   #single
74                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = 0, diseqcmode = 0, diseqcpos = 4)
75                                         elif nim.diseqcMode.value == 1:         #Toneburst A/B
76                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = 1, diseqcmode = 0, diseqcpos = 4)
77                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = 1, diseqcmode = 0, diseqcpos = 4)
78                                         elif nim.diseqcMode.value == 2:         #DiSEqC A/B
79                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 0)
80                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 1)
81                                         elif nim.diseqcMode.value == 3:         #DiSEqC A/B/C/D
82                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcA.vals[nim.diseqcA.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 0)
83                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcB.vals[nim.diseqcB.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 1)
84                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcC.vals[nim.diseqcC.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 2)
85                                                 self.addLNBSimple(slotid = x, orbpos = int(nim.diseqcD.vals[nim.diseqcD.value][1]), toneburstmode = 0, diseqcmode = 1, diseqcpos = 3)
86                                         elif nim.diseqcMode.value == 4:         #Positioner
87                                                 print "FIXME: positioner suppport"
88                                         pass
89                                 else:                                                                                                                                   #advanced config
90                                         print "FIXME add support for advanced config"
91                 
92         def __init__(self, nimmgr):
93                 self.NimManager = nimmgr
94                 self.update()
95                 
96 class nimSlot:
97         def __init__(self, slotid, nimtype, name):
98                 self.slotid = slotid
99                 self.nimType = nimtype
100                 self.name = name
101
102 class NimManager:
103         class parseSats(ContentHandler):
104                 def __init__(self, satList, satellites, transponders):
105                         self.isPointsElement, self.isReboundsElement = 0, 0
106                         self.satList = satList
107                         self.satellites = satellites
108                         self.transponders = transponders
109         
110                 def startElement(self, name, attrs):
111                         if (name == "sat"):
112                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
113                                 tpos = attrs.get('position',"")
114                                 tname = attrs.get('name',"")
115                                 self.satellites[tpos] = tname
116                                 self.satList.append( (tname, tpos) )
117                                 self.parsedSat = int(tpos)
118                         elif (name == "transponder"):
119                                 freq = int(attrs.get('frequency',""))
120                                 sr = int(attrs.get('symbol_rate',""))
121                                 pol = int(attrs.get('polarization',""))
122                                 fec = int(attrs.get('fec_inner',""))
123                                 if self.parsedSat in self.transponders:
124                                         pass
125                                 else:
126                                         self.transponders[self.parsedSat] = [ ]
127
128                                 self.transponders[self.parsedSat].append((0, freq, sr, pol, fec))
129
130         class parseCables(ContentHandler):
131                 def __init__(self, cablesList, transponders):
132                         self.isPointsElement, self.isReboundsElement = 0, 0
133                         self.cablesList = cablesList
134                         self.transponders = transponders
135         
136                 def startElement(self, name, attrs):
137                         if (name == "cable"):
138                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
139                                 tname = attrs.get('name',"")
140                                 self.cablesList.append(str(tname))
141                                 self.parsedCab = str(tname)
142                         elif (name == "transponder"):
143                                 freq = int(attrs.get('frequency',""))
144                                 sr = int(attrs.get('symbol_rate',""))
145                                 mod = int(attrs.get('modulation',""))
146                                 fec = int(attrs.get('fec_inner',""))
147                                 if self.parsedCab in self.transponders:
148                                         pass
149                                 else:
150                                         self.transponders[self.parsedCab] = [ ]
151
152                                 self.transponders[self.parsedCab].append((0, freq, sr, mod, fec))
153
154         class parseTerrestrials(ContentHandler):
155                 def __init__(self, terrestrialsList, transponders):
156                         self.isPointsElement, self.isReboundsElement = 0, 0
157                         self.terrestrialsList = terrestrialsList
158                         self.transponders = transponders
159         
160                 def startElement(self, name, attrs):
161                         if (name == "terrestrial"):
162                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
163                                 tname = attrs.get('name',"")
164                                 tflags = attrs.get('flags',"")
165                                 self.terrestrialsList.append((tname, tflags))
166                                 self.parsedTer = str(tname)
167                         elif (name == "transponder"):
168                                 # TODO finish this!
169                                 freq = int(attrs.get('centre_frequency',""))
170                                 bw = int(attrs.get('bandwidth',""))
171                                 const = int(attrs.get('constellation',""))
172                                 crh = int(attrs.get('code_rate_hp',""))
173                                 crl = int(attrs.get('code_rate_lp',""))
174                                 guard = int(attrs.get('guard_interval',""))
175                                 transm = int(attrs.get('transmission_mode',""))
176                                 hierarchy = int(attrs.get('hierarchy_information',""))
177                                 inv = int(attrs.get('inversion',""))
178                                 if self.parsedTer in self.transponders:
179                                         pass
180                                 else:
181                                         self.transponders[self.parsedTer] = [ ]
182
183                                 self.transponders[self.parsedTer].append((0, freq, bw, const, crh, crl, guard, transm, hierarchy, inv))
184
185         def getTransponders(self, pos):
186                 return self.transponders[pos]
187
188         def getConfiguredSats(self):
189                 return self.sec.getSatList()
190
191         def getSatDescription(self, pos):
192                 return self.satellites[str(pos)]
193
194         def readSatsfromFile(self):
195                 self.satellites = { }
196                 self.transponders = { }
197                 self.transponderscable = { }
198                 self.transpondersterrestrial = { }              
199                 
200                 parser = make_parser()
201                 if (self.hasNimType(self.nimType["DVB-S"])):
202                         print "Reading satellites.xml"
203                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
204                         parser.setContentHandler(satHandler)
205                         parser.parse('/etc/tuxbox/satellites.xml')
206                 if (self.hasNimType(self.nimType["DVB-C"])):
207                         print "Reading cables.xml"
208                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
209                         parser.setContentHandler(cabHandler)
210                         parser.parse('/etc/tuxbox/cables.xml')
211
212                 if (self.hasNimType(self.nimType["DVB-T"])):
213                         print "Reading terrestrial.xml"
214                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
215                         parser.setContentHandler(terHandler)
216                         parser.parse('/etc/tuxbox/terrestrial.xml')
217                 
218         def parseProc(self):
219                 self.nimTypes = {}
220                 self.nimNames = {}              
221                 self.nimSocketCount = 0
222                 nimfile = tryOpen("/proc/bus/nim_sockets")
223
224                 if nimfile == "":
225                                 return self.nimType["empty/unknown"]
226                         
227                 lastsocket = -1
228
229                 while 1:                
230                         line = nimfile.readline()
231                         if line == "":
232                                 break
233                         if line.strip().startswith("NIM Socket"):
234                                 parts = line.strip().split(" ")
235                                 id = int(parts[2][:1])
236                                 lastsocket = int(id)
237                                 self.nimSocketCount += 1
238                         elif line.strip().startswith("Type:"):
239                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
240                         elif line.strip().startswith("Name:"):
241                                 self.nimNames[lastsocket] = str(line.strip()[6:])
242                         elif line.strip().startswith("empty"):
243                                 self.nimNames[lastsocket] = _("N/A")
244                                 self.nimTypes[lastsocket] = "empty/unknown"
245
246                 nimfile.close()
247
248         def getNimType(self, slotID):
249                 if slotID >= self.nimCount:
250                         return "empty/unknown"
251                 else:   
252                         return self.nimType[self.nimTypes[slotID]]
253                         
254         def getNimName(self, slotID):
255                 return self.nimNames[slotID]
256
257         def getNimSocketCount(self):
258                 return self.nimSocketCount
259         
260         def hasNimType(self, chktype):
261                 for id, type in self.nimTypes.items():
262                         if (chktype == self.nimType[str(type)]):
263                                 return True
264                 return False
265         
266         def getNimListOfType(self, type, exception = -1):
267                 list = []
268                 for x in self.nimslots:
269                         if ((x.nimType == type) and (x.slotid != exception)):
270                                 list.append(x.slotid)
271                 return list
272
273         def getConfigPrefix(self, slotid):
274                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
275                         
276         def __init__(self):
277                 #use as enum
278                 self.nimType = {                "empty/unknown": -1,
279                                                                                                 "DVB-S": 0,
280                                                                                                 "DVB-C": 1,
281                                                                                                 "DVB-T": 2}
282                 self.satList = [ ]
283                 self.cablesList = []
284                 self.terrestrialsList = []
285                                                                                                 
286                 self.parseProc()
287
288                 self.readSatsfromFile()                                                 
289                 
290                 self.nimCount = self.getNimSocketCount()
291                 
292                 self.nimslots = [ ]
293                 x = 0
294                 while x < self.nimCount:
295                         tType = self.getNimType(x)
296                         tName = self.getNimName(x)
297                         tNim = nimSlot(x, tType, tName)
298                         self.nimslots.append(tNim)
299                         x += 1
300                 
301                 InitNimManager(self)    #init config stuff
302
303         def nimList(self):
304                 list = [ ]
305                 for slot in self.nimslots:
306                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
307                         if slot.nimType == -1:
308                                 nimText += _("empty/unknown")
309                         else:
310                                 nimText += slot.name + " ("     
311                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
312                         list.append((nimText, slot))
313                 return list
314         
315         def getSatListForNim(self, slotid):
316                 list = []
317                 if (self.getNimType(slotid) != self.nimType["empty/unknown"]):
318                         #print "slotid:", slotid
319                         
320                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
321                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
322                         if (config.Nims[slotid].diseqcMode.value <= 3):
323                                 list.append(self.satList[config.Nims[slotid].diseqcA.value])
324                         if (0 < config.Nims[slotid].diseqcMode.value <= 3):
325                                 list.append(self.satList[config.Nims[slotid].diseqcB.value])
326                         if (config.Nims[slotid].diseqcMode.value == 3):
327                                 list.append(self.satList[config.Nims[slotid].diseqcC.value])
328                                 list.append(self.satList[config.Nims[slotid].diseqcD.value])
329                 return list
330
331         #callbacks for c++ config
332         def nimConfigModeChanged(self, slotid, mode):
333                 if (mode != 1): # not linked
334                         print "Unlinking slot " + str(slotid)
335                         # TODO call c++ to unlink nim in slot slotid
336                 if (mode == 1): # linked
337                         pass
338                         #FIXME!!!
339                         #if (len(self.getNimListOfType(self.nimType["DVB-S"], slotid)) > 0):
340                         #       print "Linking slot " + str(slotid) + " to " + str(nimmgr.getConfigPrefix(slotid).value)
341                         # TODO call c++ to link nim in slot slotid with nim in slot nimmgr.getConfigPrefix(slotid).value
342         def nimLinkedToChanged(self, slotid, val):
343                 print "Linking slot " + str(slotid) + " to " + str(val)
344
345         def nimDiseqcModeChanged(self, slotid, mode):
346                 #print "nimDiseqcModeChanged set to " + str(mode)
347                 pass
348         def nimPortAChanged(self, slotid, val):
349                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
350                 pass
351         def nimPortBChanged(self, slotid, val):
352                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
353                 #print "nimDiseqcB set to " + str(val)
354                 pass
355         def nimPortCChanged(self, slotid, val):
356                 #print "nimDiseqcC set to " + str(val)
357                 pass
358         def nimPortDChanged(self, slotid, val):
359                 #print "nimDiseqcD set to " + str(val)
360                 pass
361
362 def InitNimManager(nimmgr):
363         config.Nims = []
364         for x in range(nimmgr.nimCount):
365                 config.Nims.append(ConfigSubsection())
366                 
367         def nimConfigModeChanged(slotid, configElement):
368                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
369         def nimLinkedToChanged(slotid, configElement):
370                 nimmgr.nimLinkedToChanged(slotid, configElement.value)
371         def nimDiseqcModeChanged(slotid, configElement):
372                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
373                 
374         def nimPortAChanged(slotid, configElement):
375                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
376         def nimPortBChanged(slotid, configElement):
377                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
378         def nimPortCChanged(slotid, configElement):
379                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
380         def nimPortDChanged(slotid, configElement):
381                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
382
383         for slot in nimmgr.nimslots:
384                 x = slot.slotid
385                 cname = nimmgr.getConfigPrefix(x)
386                 nim = config.Nims[x]
387                 
388                 if slot.nimType == nimmgr.nimType["DVB-S"]:
389                         nim.configMode = configElement(cname + "configMode", configSelection, 0, (_("Simple"), _("Loopthrough to Socket A"))) # "Advanced"));
390                         
391                         #important - check if just the 2nd one is LT only and the first one is DVB-S
392                         if nim.configMode.value == 1: #linked
393                                 if x == 0:                                                                              #first one can never be linked to anything
394                                         nim.configMode.value = 0                #reset to simple
395                                         nim.configMode.save()
396                                 else:
397                                         #FIXME: make it better
398                                         for y in nimmgr.nimslots:
399                                                 if y.slotid == 0:
400                                                         if y.nimType != nimmgr.nimType["DVB-S"]:
401                                                                 nim.configMode.value = 0                #reset to simple
402                                                                 nim.configMode.save()
403
404                         nim.diseqcMode = configElement(cname + "diseqcMode", configSelection, 2, (_("Single"), _("Toneburst A/B"), _("DiSEqC A/B"), _("DiSEqC A/B/C/D")))#, _("Positioner")));
405                         nim.diseqcA = configElement(cname + "diseqcA", configSatlist, 192, nimmgr.satList);
406                         nim.diseqcB = configElement(cname + "diseqcB", configSatlist, 130, nimmgr.satList);
407                         nim.diseqcC = configElement(cname + "diseqcC", configSatlist, 0, nimmgr.satList);
408                         nim.diseqcD = configElement(cname + "diseqcD", configSatlist, 0, nimmgr.satList);
409                         nim.positionerMode = configElement(cname + "positionerMode", configSelection, 0, (_("USALS"), _("manual")));
410                         nim.longitude = configElement(cname + "longitude", configSequence, [5,100], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
411                         nim.longitudeOrientation = configElement(cname + "longitudeOrientation", configSelection, 0, (_("East"), _("West")))
412                         nim.latitude = configElement(cname + "latitude", configSequence, [50,767], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
413                         nim.latitudeOrientation = configElement(cname + "latitudeOrientation", configSelection, 0, (_("North"), _("South")))
414                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
415                         satNimListNames = []
416                         for x in satNimList:
417                                 satNimListNames.append(_("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x))
418                         nim.linkedTo = configElement(cname + "linkedTo", configSelection, 0, satNimListNames);
419                         
420                         #perhaps the instance of the slot is more useful?
421                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
422                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
423                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
424                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
425                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
426                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
427                         nim.linkedTo.addNotifier(boundFunction(nimLinkedToChanged,x))
428                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
429                         nim.cable = configElement(cname + "cable", configSelection, 0, nimmgr.cablesList);
430                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
431                         nim.cable = configElement(cname + "terrestrial", configSelection, 0, nimmgr.terrestrialsList);
432                 else:
433                         print "pls add support for this frontend type!"         
434
435         nimmgr.sec = SecConfigure(nimmgr)
436
437 nimmanager = NimManager()