05afcbd2dc8cf5e7e0307167dedaaa4ba26590d1
[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 def tryOpen(filename):
22         try:
23                 procFile = open(filename)
24         except IOError:
25                 return ""
26         return procFile
27
28 class SecConfigure:
29         def addLNBSimple(self, slotid, orbpos, toneburstmode, diseqcmode, diseqcpos):
30                 #simple defaults
31                 sec = eDVBSatelliteEquipmentControl.getInstance()
32                 sec.addLNB()
33                 sec.setLNBTunerMask(1 << slotid)
34                 sec.setLNBLOFL(9750000)
35                 sec.setLNBLOFH(10600000)
36                 sec.setLNBThreshold(11750000)
37                 sec.setRepeats(0)
38                 sec.setFastDiSEqC(0)
39                 sec.setSeqRepeat(0)
40                 sec.setVoltageMode(0) #HV
41                 sec.setToneMode(0)              #HILO
42                 sec.setCommandOrder(0)
43                 #user values
44                 sec.setDiSEqCMode(diseqcmode)
45                 sec.setToneburst(toneburstmode)
46                 sec.setCommittedCommand(diseqcpos)
47                 #print "set orbpos to:" + str(orbpos)
48                 sec.addSatellite(orbpos)
49                 self.satList.append(orbpos)
50
51         def getSatList(self):
52                 return self.satList
53
54         def update(self):
55                 eDVBSatelliteEquipmentControl.getInstance().clear()
56                 self.satList = []
57
58                 for slot in self.NimManager.nimslots:
59                         x = slot.slotid
60                         nim = config.Nims[x]
61                         if slot.nimType == self.NimManager.nimType["DVB-S"]:
62                                 print "slot: " + str(x) + " configmode: " + str(nim.configMode.value)
63                                 if nim.configMode.value == 0:           #simple config
64                                         if nim.diseqcMode.value == 0:                   #single
65                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 0, 4)
66                                         elif nim.diseqcMode.value == 1:         #Toneburst A/B
67                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 1, 0, 4)
68                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 1, 0, 4)
69                                         elif nim.diseqcMode.value == 2:         #DiSEqC A/B
70                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
71                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
72                                         elif nim.diseqcMode.value == 3:         #DiSEqC A/B/C/D
73                                                 self.addLNBSimple(x, int(nim.diseqcA.vals[nim.diseqcA.value][1]), 0, 1, 0)
74                                                 self.addLNBSimple(x, int(nim.diseqcB.vals[nim.diseqcB.value][1]), 0, 1, 1)
75                                                 self.addLNBSimple(x, int(nim.diseqcC.vals[nim.diseqcC.value][1]), 0, 1, 2)
76                                                 self.addLNBSimple(x, int(nim.diseqcD.vals[nim.diseqcD.value][1]), 0, 1, 3)
77                                         elif nim.diseqcMode.value == 4:         #Positioner
78                                                 print "FIXME: positioner suppport"
79                                         pass
80                                 else:                                                                                                                                   #advanced config
81                                         print "FIXME add support for advanced config"
82                 
83         def __init__(self, nimmgr):
84                 self.NimManager = nimmgr
85                 self.update()
86                 
87 class boundFunction:
88         def __init__(self, fnc, *args):
89                 self.fnc = fnc
90                 self.args = args
91         def __call__(self, *args):
92                 self.fnc(*self.args + args)
93
94 class nimSlot:
95         def __init__(self, slotid, nimtype, name):
96                 self.slotid = slotid
97                 self.nimType = nimtype
98                 self.name = name
99
100 class NimManager:
101         class parseSats(ContentHandler):
102                 def __init__(self, satList, satellites, transponders):
103                         self.isPointsElement, self.isReboundsElement = 0, 0
104                         self.satList = satList
105                         self.satellites = satellites
106                         self.transponders = transponders
107         
108                 def startElement(self, name, attrs):
109                         if (name == "sat"):
110                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
111                                 tpos = attrs.get('position',"")
112                                 tname = attrs.get('name',"")
113                                 self.satellites[tpos] = tname
114                                 self.satList.append( (tname, tpos) )
115                                 self.parsedSat = int(tpos)
116                         elif (name == "transponder"):
117                                 freq = int(attrs.get('frequency',""))
118                                 sr = int(attrs.get('symbol_rate',""))
119                                 pol = int(attrs.get('polarization',""))
120                                 fec = int(attrs.get('fec_inner',""))
121                                 if self.parsedSat in self.transponders:
122                                         pass
123                                 else:
124                                         self.transponders[self.parsedSat] = [ ]
125
126                                 self.transponders[self.parsedSat].append((0, freq, sr, pol, fec))
127
128         def getTransponders(self, pos):
129                 return self.transponders[pos]
130
131         def getConfiguredSats(self):
132                 return self.sec.getSatList()
133
134         def getSatDescription(self, pos):
135                 return self.satellites[str(pos)]
136
137         def readSatsfromFile(self):
138                 self.satellites = { }
139                 self.transponders = { }
140
141                 print "Reading satellites.xml"
142                 parser = make_parser()
143                 satHandler = self.parseSats(self.satList, self.satellites, self.transponders)
144                 parser.setContentHandler(satHandler)
145                 parser.parse('/etc/tuxbox/satellites.xml')
146                 
147         def parseProc(self):
148                 self.nimTypes = {}
149                 self.nimNames = {}              
150                 nimfile = tryOpen("/proc/bus/nim_sockets")
151
152                 if nimfile == "":
153                                 return self.nimType["empty/unknown"]
154                         
155                 lastsocket = -1
156
157                 while 1:                
158                         line = nimfile.readline()
159                         if line == "":
160                                 break
161                         if line.strip().startswith("NIM Socket"):
162                                 parts = line.strip().split(" ")
163                                 id = int(parts[2][:1])
164                                 lastsocket = int(id)
165                         elif line.strip().startswith("Type:"):
166                                 self.nimTypes[lastsocket] = str(line.strip()[6:])
167                         elif line.strip().startswith("Name:"):
168                                 self.nimNames[lastsocket] = str(line.strip()[6:])
169
170                 nimfile.close()
171                 
172
173         def getNimType(self, slotID):
174                 return self.nimType[self.nimTypes[slotID]]
175
176         def getNimName(self, slotID):
177                 return self.nimNames[slotID]
178
179         def getNimSocketCount(self):
180                 #FIXME get it from /proc
181                 return 2
182
183         def getConfigPrefix(self, slotid):
184                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
185                         
186         def __init__(self):
187                 #use as enum
188                 self.nimType = {                "empty/unknown": -1,
189                                                                                                 "DVB-S": 0,
190                                                                                                 "DVB-C": 1,
191                                                                                                 "DVB-T": 2}
192                 self.satList = [ ]                                                                              
193                                                                                                 
194                 self.readSatsfromFile()                                                                         
195                 
196                 self.nimCount = self.getNimSocketCount()
197                 
198                 self.parseProc()
199                 
200                 self.nimslots = [ ]
201                 x = 0
202                 while x < self.nimCount:
203                         tType = self.getNimType(x)
204                         tName = self.getNimName(x)
205                         tNim = nimSlot(x, tType, tName)
206                         self.nimslots.append(tNim)
207                         x += 1
208                 
209                 InitNimManager(self)    #init config stuff
210
211         def nimList(self):
212                 list = [ ]
213                 for slot in self.nimslots:
214                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
215                         if slot.nimType == -1:
216                                 nimText += _("empty/unknown")
217                         else:
218                                 nimText += slot.name + " ("     
219                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
220                         list.append((nimText, slot))
221                 return list
222         
223         def getSatListForNim(self, slotid):
224                 list = []
225                 if (self.getNimType(slotid) != self.nimType["empty/unknown"]):
226                         #print "slotid:", slotid
227                         
228                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
229                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
230                         if (config.Nims[slotid].diseqcMode.value <= 3):
231                                 list.append(self.satList[config.Nims[slotid].diseqcA.value])
232                         if (0 < config.Nims[slotid].diseqcMode.value <= 3):
233                                 list.append(self.satList[config.Nims[slotid].diseqcB.value])
234                         if (config.Nims[slotid].diseqcMode.value == 3):
235                                 list.append(self.satList[config.Nims[slotid].diseqcC.value])
236                                 list.append(self.satList[config.Nims[slotid].diseqcD.value])
237                 return list
238
239         #callbacks for c++ config
240         def nimConfigModeChanged(self, slotid, mode):
241                 #print "nimConfigModeChanged set to " + str(mode)
242                 pass
243         def nimDiseqcModeChanged(self, slotid, mode):
244                 #print "nimDiseqcModeChanged set to " + str(mode)
245                 pass
246         def nimPortAChanged(self, slotid, val):
247                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
248                 pass
249         def nimPortBChanged(self, slotid, val):
250                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
251                 #print "nimDiseqcB set to " + str(val)
252                 pass
253         def nimPortCChanged(self, slotid, val):
254                 #print "nimDiseqcC set to " + str(val)
255                 pass
256         def nimPortDChanged(self, slotid, val):
257                 #print "nimDiseqcD set to " + str(val)
258                 pass
259
260
261 def InitNimManager(nimmgr):
262         config.Nims = []
263         for x in range(nimmgr.nimCount):
264                 config.Nims.append(ConfigSubsection())
265                 
266         def nimConfigModeChanged(slotid, configElement):
267                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
268         def nimDiseqcModeChanged(slotid, configElement):
269                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
270                 
271         def nimPortAChanged(slotid, configElement):
272                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
273         def nimPortBChanged(slotid, configElement):
274                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
275         def nimPortCChanged(slotid, configElement):
276                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
277         def nimPortDChanged(slotid, configElement):
278                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
279
280         for slot in nimmgr.nimslots:
281                 x = slot.slotid
282                 cname = nimmgr.getConfigPrefix(x)
283                 nim = config.Nims[x]
284                 
285                 if slot.nimType == nimmgr.nimType["DVB-S"]:
286                         nim.configMode = configElement(cname + "configMode",configSelection, 0, (_("Simple"), _("Advanced")));
287                         nim.diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, (_("Single"), _("Toneburst A/B"), _("DiSEqC A/B"), _("DiSEqC A/B/C/D"), _("Positioner")));
288                         nim.diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
289                         nim.diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
290                         nim.diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
291                         nim.diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
292                         nim.longitude = configElement(cname + "longitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
293                         nim.latitude = configElement(cname + "latitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
294                         
295                         #perhaps the instance of the slot is more useful?
296                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
297                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
298                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
299                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
300                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
301                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
302                 else:
303                         print "pls add support for this frontend type!"         
304
305         nimmgr.sec = SecConfigure(nimmgr)
306
307 nimmanager = NimManager()