simplify mmi
[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 getConfigPrefix(self, slotid):
258                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
259                         
260         def __init__(self):
261                 #use as enum
262                 self.nimType = {                "empty/unknown": -1,
263                                                                                                 "DVB-S": 0,
264                                                                                                 "DVB-C": 1,
265                                                                                                 "DVB-T": 2}
266                 self.satList = [ ]
267                 self.cablesList = []
268                 self.terrestrialsList = []
269                                                                                                 
270                 self.parseProc()
271
272                 self.readSatsfromFile()                                                 
273                 
274                 self.nimCount = self.getNimSocketCount()
275                 
276                 self.nimslots = [ ]
277                 x = 0
278                 while x < self.nimCount:
279                         tType = self.getNimType(x)
280                         tName = self.getNimName(x)
281                         tNim = nimSlot(x, tType, tName)
282                         self.nimslots.append(tNim)
283                         x += 1
284                 
285                 InitNimManager(self)    #init config stuff
286
287         def nimList(self):
288                 list = [ ]
289                 for slot in self.nimslots:
290                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
291                         if slot.nimType == -1:
292                                 nimText += _("empty/unknown")
293                         else:
294                                 nimText += slot.name + " ("     
295                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
296                         list.append((nimText, slot))
297                 return list
298         
299         def getSatListForNim(self, slotid):
300                 list = []
301                 if (self.getNimType(slotid) != self.nimType["empty/unknown"]):
302                         #print "slotid:", slotid
303                         
304                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
305                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
306                         if (config.Nims[slotid].diseqcMode.value <= 3):
307                                 list.append(self.satList[config.Nims[slotid].diseqcA.value])
308                         if (0 < config.Nims[slotid].diseqcMode.value <= 3):
309                                 list.append(self.satList[config.Nims[slotid].diseqcB.value])
310                         if (config.Nims[slotid].diseqcMode.value == 3):
311                                 list.append(self.satList[config.Nims[slotid].diseqcC.value])
312                                 list.append(self.satList[config.Nims[slotid].diseqcD.value])
313                 return list
314
315         #callbacks for c++ config
316         def nimConfigModeChanged(self, slotid, mode):
317                 #print "nimConfigModeChanged set to " + str(mode)
318                 pass
319         def nimDiseqcModeChanged(self, slotid, mode):
320                 #print "nimDiseqcModeChanged set to " + str(mode)
321                 pass
322         def nimPortAChanged(self, slotid, val):
323                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
324                 pass
325         def nimPortBChanged(self, slotid, val):
326                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
327                 #print "nimDiseqcB set to " + str(val)
328                 pass
329         def nimPortCChanged(self, slotid, val):
330                 #print "nimDiseqcC set to " + str(val)
331                 pass
332         def nimPortDChanged(self, slotid, val):
333                 #print "nimDiseqcD set to " + str(val)
334                 pass
335
336
337 def InitNimManager(nimmgr):
338         config.Nims = []
339         for x in range(nimmgr.nimCount):
340                 config.Nims.append(ConfigSubsection())
341                 
342         def nimConfigModeChanged(slotid, configElement):
343                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
344         def nimDiseqcModeChanged(slotid, configElement):
345                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
346                 
347         def nimPortAChanged(slotid, configElement):
348                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
349         def nimPortBChanged(slotid, configElement):
350                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
351         def nimPortCChanged(slotid, configElement):
352                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
353         def nimPortDChanged(slotid, configElement):
354                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
355
356         for slot in nimmgr.nimslots:
357                 x = slot.slotid
358                 cname = nimmgr.getConfigPrefix(x)
359                 nim = config.Nims[x]
360                 
361                 if slot.nimType == nimmgr.nimType["DVB-S"]:
362                         nim.configMode = configElement(cname + "configMode", configSelection, 0, (_("Simple"), _("Advanced")));
363                         nim.diseqcMode = configElement(cname + "diseqcMode", configSelection, 2, (_("Single"), _("Toneburst A/B"), _("DiSEqC A/B"), _("DiSEqC A/B/C/D"), _("Positioner")));
364                         nim.diseqcA = configElement(cname + "diseqcA", configSatlist, 192, nimmgr.satList);
365                         nim.diseqcB = configElement(cname + "diseqcB", configSatlist, 130, nimmgr.satList);
366                         nim.diseqcC = configElement(cname + "diseqcC", configSatlist, 0, nimmgr.satList);
367                         nim.diseqcD = configElement(cname + "diseqcD", configSatlist, 0, nimmgr.satList);
368                         nim.longitude = configElement(cname + "longitude", configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
369                         nim.latitude = configElement(cname + "latitude", configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
370                         
371                         #perhaps the instance of the slot is more useful?
372                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
373                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
374                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
375                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
376                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
377                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
378                 elif slot.nimType == nimmgr.nimType["DVB-C"]:
379                         nim.cable = configElement(cname + "cable", configSelection, 0, nimmgr.cablesList);
380                 elif slot.nimType == nimmgr.nimType["DVB-T"]:
381                         nim.cable = configElement(cname + "terrestrial", configSelection, 0, nimmgr.terrestrialsList);
382                 else:
383                         print "pls add support for this frontend type!"         
384
385         nimmgr.sec = SecConfigure(nimmgr)
386
387 nimmanager = NimManager()