better display for linked nim-names
[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, orbpos, toneburstmode, diseqcmode, diseqcpos):
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 getSatList(self):
54                 return self.satList
55
56         def update(self):
57                 eDVBSatelliteEquipmentControl.getInstance().clear()
58                 self.satList = []
59
60                 for slot in self.NimManager.nimslots:
61                         x = slot.slotid
62                         nim = config.Nims[x]
63                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
64                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
65                                 if nim.configMode.value == 0:           #simple config
66                                         if nim.diseqcMode.value == 0:                   #single
67                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 0, 4)
68                                         elif nim.diseqcMode.value == 1:         #Toneburst A/B
69                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 1, 0, 4)
70                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 1, 0, 4)
71                                         elif nim.diseqcMode.value == 2:         #DiSEqC A/B
72                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
73                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
74                                         elif nim.diseqcMode.value == 3:         #DiSEqC A/B/C/D
75                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
76                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
77                                                 self.addLNBSimple(x, int(nim.diseqcC.vals[nim.diseqcC.value][1]), 0, 1, 2)
78                                                 self.addLNBSimple(x, int(nim.diseqcD.vals[nim.diseqcD.value][1]), 0, 1, 3)
79                                         elif nim.diseqcMode.value == 4:         #Positioner
80                                                 print "FIXME: positioner suppport"
81                                         pass
82                                 else:                                                                                                                                   #advanced config
83                                         print "FIXME add support for advanced config"
84                 
85         def __init__(self, nimmgr):
86                 self.NimManager = nimmgr
87                 self.update()
88                 
89 class nimSlot:
90         def __init__(self, slotid, nimtype, name):
91                 self.slotid = slotid
92                 self.nimType = nimtype
93                 self.name = name
94
95 class NimManager:
96         class parseSats(ContentHandler):
97                 def __init__(self, satList, satellites, transponders):
98                         self.isPointsElement, self.isReboundsElement = 0, 0
99                         self.satList = satList
100                         self.satellites = satellites
101                         self.transponders = transponders
102         
103                 def startElement(self, name, attrs):
104                         if (name == "sat"):
105                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
106                                 tpos = attrs.get('position',"")
107                                 tname = attrs.get('name',"")
108                                 self.satellites[tpos] = tname
109                                 self.satList.append( (tname, tpos) )
110                                 self.parsedSat = int(tpos)
111                         elif (name == "transponder"):
112                                 freq = int(attrs.get('frequency',""))
113                                 sr = int(attrs.get('symbol_rate',""))
114                                 pol = int(attrs.get('polarization',""))
115                                 fec = int(attrs.get('fec_inner',""))
116                                 if self.parsedSat in self.transponders:
117                                         pass
118                                 else:
119                                         self.transponders[self.parsedSat] = [ ]
120
121                                 self.transponders[self.parsedSat].append((0, freq, sr, pol, fec))
122
123         class parseCables(ContentHandler):
124                 def __init__(self, cablesList, transponders):
125                         self.isPointsElement, self.isReboundsElement = 0, 0
126                         self.cablesList = cablesList
127                         self.transponders = transponders
128         
129                 def startElement(self, name, attrs):
130                         if (name == "cable"):
131                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
132                                 tname = attrs.get('name',"")
133                                 self.cablesList.append(str(tname))
134                                 self.parsedCab = str(tname)
135                         elif (name == "transponder"):
136                                 freq = int(attrs.get('frequency',""))
137                                 sr = int(attrs.get('symbol_rate',""))
138                                 mod = int(attrs.get('modulation',""))
139                                 fec = int(attrs.get('fec_inner',""))
140                                 if self.parsedCab in self.transponders:
141                                         pass
142                                 else:
143                                         self.transponders[self.parsedCab] = [ ]
144
145                                 self.transponders[self.parsedCab].append((0, freq, sr, mod, fec))
146
147         class parseTerrestrials(ContentHandler):
148                 def __init__(self, terrestrialsList, transponders):
149                         self.isPointsElement, self.isReboundsElement = 0, 0
150                         self.terrestrialsList = terrestrialsList
151                         self.transponders = transponders
152         
153                 def startElement(self, name, attrs):
154                         if (name == "terrestrial"):
155                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
156                                 tname = attrs.get('name',"")
157                                 tflags = attrs.get('flags',"")
158                                 self.terrestrialsList.append((tname, tflags))
159                                 self.parsedTer = str(tname)
160                         elif (name == "transponder"):
161                                 # TODO finish this!
162                                 freq = int(attrs.get('centre_frequency',""))
163                                 bw = int(attrs.get('bandwidth',""))
164                                 const = int(attrs.get('constellation',""))
165                                 crh = int(attrs.get('code_rate_hp',""))
166                                 crl = int(attrs.get('code_rate_lp',""))
167                                 guard = int(attrs.get('guard_interval',""))
168                                 transm = int(attrs.get('transmission_mode',""))
169                                 hierarchy = int(attrs.get('hierarchy_information',""))
170                                 inv = int(attrs.get('inversion',""))
171                                 if self.parsedTer in self.transponders:
172                                         pass
173                                 else:
174                                         self.transponders[self.parsedTer] = [ ]
175
176                                 self.transponders[self.parsedTer].append((0, freq, bw, const, crh, crl, guard, transm, hierarchy, inv))
177
178         def getTransponders(self, pos):
179                 return self.transponders[pos]
180
181         def getConfiguredSats(self):
182                 return self.sec.getSatList()
183
184         def getSatDescription(self, pos):
185                 return self.satellites[str(pos)]
186
187         def readSatsfromFile(self):
188                 self.satellites = { }
189                 self.transponders = { }
190                 self.transponderscable = { }
191                 self.transpondersterrestrial = { }              
192                 
193                 parser = make_parser()
194                 if (self.hasNimType(self.nimType["DVB-S"])):
195                         print "Reading satellites.xml"
196                         satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
197                         parser.setContentHandler(satHandler)
198                         parser.parse('/etc/tuxbox/satellites.xml')
199                 if (self.hasNimType(self.nimType["DVB-C"])):
200                         print "Reading cables.xml"
201                         cabHandler = self.parseCables(self.cablesList, self.transponderscable)
202                         parser.setContentHandler(cabHandler)
203                         parser.parse('/etc/tuxbox/cables.xml')
204
205                 if (self.hasNimType(self.nimType["DVB-T"])):
206                         print "Reading terrestrial.xml"
207                         terHandler = self.parseTerrestrials(self.terrestrialsList, self.transpondersterrestrial)
208                         parser.setContentHandler(terHandler)
209                         parser.parse('/etc/tuxbox/terrestrial.xml')
210                 
211         def parseProc(self):
212                 self.nimTypes = {}
213                 self.nimNames = {}              
214                 self.nimSocketCount = 0
215                 nimfile = tryOpen("/proc/bus/nim_sockets")
216
217                 if nimfile == "":
218                                 return self.nimType["empty/unknown"]
219                         
220                 lastsocket = -1
221
222                 while 1:                
223                         line = nimfile.readline()
224                         if line == "":
225                                 break
226                         if line.strip().startswith("NIM Socket"):
227                                 parts = line.strip().split(" ")
228                                 id = int(parts[2][:1])
229                                 lastsocket = int(id)
230                                 self.nimSocketCount += 1
231                         elif line.strip().startswith("Type:"):
232                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
233                         elif line.strip().startswith("Name:"):
234                                 self.nimNames[lastsocket] = str(line.strip()[6:])
235                         elif line.strip().startswith("empty"):
236                                 self.nimNames[lastsocket] = _("N/A")
237                                 self.nimTypes[lastsocket] = "empty/unknown"
238
239                 nimfile.close()
240                 
241
242         def getNimType(self, slotID):
243                 return self.nimType[self.nimTypes[slotID]]
244
245         def getNimName(self, slotID):
246                 return self.nimNames[slotID]
247
248         def getNimSocketCount(self):
249                 return self.nimSocketCount
250         
251         def hasNimType(self, chktype):
252                 for id, type in self.nimTypes.items():
253                         if (chktype == self.nimType[str(type)]):
254                                 return True
255                 return False
256         
257         def getNimListOfType(self, type, exception = -1):
258                 list = []
259                 for x in self.nimslots:
260                         if ((x.nimType == type) and (x.slotid != exception)):
261                                 list.append(x.slotid)
262                 return list
263
264         def getConfigPrefix(self, slotid):
265                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
266                         
267         def __init__(self):
268                 #use as enum
269                 self.nimType = {                "empty/unknown": -1,
270                                                                                                 "DVB-S": 0,
271                                                                                                 "DVB-C": 1,
272                                                                                                 "DVB-T": 2}
273                 self.satList = [ ]
274                 self.cablesList = []
275                 self.terrestrialsList = []
276                                                                                                 
277                 self.parseProc()
278
279                 self.readSatsfromFile()                                                 
280                 
281                 self.nimCount = self.getNimSocketCount()
282                 
283                 self.nimslots = [ ]
284                 x = 0
285                 while x < self.nimCount:
286                         tType = self.getNimType(x)
287                         tName = self.getNimName(x)
288                         tNim = nimSlot(x, tType, tName)
289                         self.nimslots.append(tNim)
290                         x += 1
291                 
292                 InitNimManager(self)    #init config stuff
293
294         def nimList(self):
295                 list = [ ]
296                 for slot in self.nimslots:
297                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
298                         if slot.nimType == -1:
299                                 nimText += _("empty/unknown")
300                         else:
301                                 nimText += slot.name + " ("     
302                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
303                         list.append((nimText, slot))
304                 return list
305         
306         def getSatListForNim(self, slotid):
307                 list = []
308                 if (self.getNimType(slotid) != self.nimType["empty/unknown"]):
309                         #print "slotid:", slotid
310                         
311                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
312                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
313                         if (config.Nims[slotid].diseqcMode.value <= 3):
314                                 list.append(self.satList[config.Nims[slotid].diseqcA.value])
315                         if (0 < config.Nims[slotid].diseqcMode.value <= 3):
316                                 list.append(self.satList[config.Nims[slotid].diseqcB.value])
317                         if (config.Nims[slotid].diseqcMode.value == 3):
318                                 list.append(self.satList[config.Nims[slotid].diseqcC.value])
319                                 list.append(self.satList[config.Nims[slotid].diseqcD.value])
320                 return list
321
322         #callbacks for c++ config
323         def nimConfigModeChanged(self, slotid, mode):
324                 #print "nimConfigModeChanged set to " + str(mode)
325                 pass
326         def nimDiseqcModeChanged(self, slotid, mode):
327                 #print "nimDiseqcModeChanged set to " + str(mode)
328                 pass
329         def nimPortAChanged(self, slotid, val):
330                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
331                 pass
332         def nimPortBChanged(self, slotid, val):
333                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
334                 #print "nimDiseqcB set to " + str(val)
335                 pass
336         def nimPortCChanged(self, slotid, val):
337                 #print "nimDiseqcC set to " + str(val)
338                 pass
339         def nimPortDChanged(self, slotid, val):
340                 #print "nimDiseqcD set to " + str(val)
341                 pass
342
343
344 def InitNimManager(nimmgr):
345         config.Nims = []
346         for x in range(nimmgr.nimCount):
347                 config.Nims.append(ConfigSubsection())
348                 
349         def nimConfigModeChanged(slotid, configElement):
350                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
351         def nimDiseqcModeChanged(slotid, configElement):
352                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
353                 
354         def nimPortAChanged(slotid, configElement):
355                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
356         def nimPortBChanged(slotid, configElement):
357                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
358         def nimPortCChanged(slotid, configElement):
359                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
360         def nimPortDChanged(slotid, configElement):
361                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
362
363         for slot in nimmgr.nimslots:
364                 x = slot.slotid
365                 cname = nimmgr.getConfigPrefix(x)
366                 nim = config.Nims[x]
367                 
368                 if slot.nimType == nimmgr.nimType["DVB-S"]:
369                         nim.configMode = configElement(cname + "configMode", configSelection, 0, (_("Simple"), _("Linked tuner"))) # _("Advanced")));
370                         nim.diseqcMode = configElement(cname + "diseqcMode", configSelection, 2, (_("Single"), _("Toneburst A/B"), _("DiSEqC A/B"), _("DiSEqC A/B/C/D"), _("Positioner")));
371                         nim.diseqcA = configElement(cname + "diseqcA", configSatlist, 192, nimmgr.satList);
372                         nim.diseqcB = configElement(cname + "diseqcB", configSatlist, 130, nimmgr.satList);
373                         nim.diseqcC = configElement(cname + "diseqcC", configSatlist, 0, nimmgr.satList);
374                         nim.diseqcD = configElement(cname + "diseqcD", configSatlist, 0, nimmgr.satList);
375                         nim.longitude = configElement(cname + "longitude", configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
376                         nim.latitude = configElement(cname + "latitude", configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
377                         
378                         satNimList = nimmgr.getNimListOfType(nimmgr.nimType["DVB-S"], slot.slotid)
379                         satNimListNames = []
380                         for x in satNimList:
381                                 satNimListNames.append(_("Slot ") + ("A", "B", "C", "D")[x] + ": " + nimmgr.getNimName(x))
382                         nim.linkedTo = configElement(cname + "linkedTo", configSelection, 0, satNimListNames);
383                         
384                         #perhaps the instance of the slot is more useful?
385                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
386                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
387                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
388                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
389                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
390                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
391                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
392                         nim.cable = configElement(cname + "cable", configSelection, 0, nimmgr.cablesList);
393                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
394                         nim.cable = configElement(cname + "terrestrial", configSelection, 0, nimmgr.terrestrialsList);
395                 else:
396                         print "pls add support for this frontend type!"         
397
398         nimmgr.sec = SecConfigure(nimmgr)
399
400 nimmanager = NimManager()