55b644f60eb5461c81e47af1025938adafea1480
[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 getNimType(self, slotID):
148                 #FIXME get it from /proc
149                 nimfile = tryOpen("/proc/bus/nim_sockets")
150
151                 if nimfile == "":
152                         # FIXME: remove this in the final version
153                         # check if we have a device for 7020 comp?atibility reasons
154                         try:
155                                 open("/dev/dvb/card0/frontend" + str(slotID))
156                                 return self.nimType["DVB-S"]
157                         except IOError:
158                                 return self.nimType["empty/unknown"]
159
160
161                 while 1:                
162                         line = nimfile.readline()
163                         if line == "":
164                                 break
165                         if line.startswith("NIM Socket"):
166                                 parts = line.strip().split(" ")
167                                 id = int(parts[2][:1])
168                                 if id == slotID:
169                                         line = nimfile.readline()
170                                         if line == "":
171                                                 break
172                                         if line.startswith("   Type:"):
173                                                 nimfile.close()
174                                                 return self.nimType["DVB-S"]
175                                         else:
176                                                 break   
177                 nimfile.close()
178                 return self.nimType["empty/unknown"]
179
180         def getNimName(self, slotID):
181                 #FIXME get it from /proc
182                 return "Alps BSBE1"
183
184         def getNimSocketCount(self):
185                 #FIXME get it from /proc
186                 return 2
187
188         def getConfigPrefix(self, slotid):
189                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
190                         
191         def __init__(self):
192                 #use as enum
193                 self.nimType = {                "empty/unknown": -1,
194                                                                                                 "DVB-S": 0,
195                                                                                                 "DVB-C": 1,
196                                                                                                 "DVB-T": 2}
197                 self.satList = [ ]                                                                              
198                                                                                                 
199                 self.readSatsfromFile()                                                                         
200                 
201                 self.nimCount = self.getNimSocketCount()
202                 
203                 self.nimslots = [ ]
204                 x = 0
205                 while x < self.nimCount:
206                         tType = self.getNimType(x)
207                         tName = self.getNimName(x)
208                         tNim = nimSlot(x, tType, tName)
209                         self.nimslots.append(tNim)
210                         x += 1
211                 
212                 InitNimManager(self)    #init config stuff
213
214         def nimList(self):
215                 list = [ ]
216                 for slot in self.nimslots:
217                         nimText = _("Socket ") + ("A", "B", "C", "D")[slot.slotid] + ": "
218                         if slot.nimType == -1:
219                                 nimText += _("empty/unknown")
220                         else:
221                                 nimText += slot.name + " ("     
222                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
223                         list.append((nimText, slot))
224                 return list
225         
226         def getSatListForNim(self, slotid):
227                 list = []
228                 if (self.getNimType(slotid) != self.nimType["empty/unknown"]):
229                         #print "slotid:", slotid
230                         
231                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
232                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
233                         if (config.Nims[slotid].diseqcMode.value <= 3):
234                                 list.append(self.satList[config.Nims[slotid].diseqcA.value])
235                         if (0 < config.Nims[slotid].diseqcMode.value <= 3):
236                                 list.append(self.satList[config.Nims[slotid].diseqcB.value])
237                         if (config.Nims[slotid].diseqcMode.value == 3):
238                                 list.append(self.satList[config.Nims[slotid].diseqcC.value])
239                                 list.append(self.satList[config.Nims[slotid].diseqcD.value])
240                 return list
241
242         #callbacks for c++ config
243         def nimConfigModeChanged(self, slotid, mode):
244                 #print "nimConfigModeChanged set to " + str(mode)
245                 pass
246         def nimDiseqcModeChanged(self, slotid, mode):
247                 #print "nimDiseqcModeChanged set to " + str(mode)
248                 pass
249         def nimPortAChanged(self, slotid, val):
250                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
251                 pass
252         def nimPortBChanged(self, slotid, val):
253                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
254                 #print "nimDiseqcB set to " + str(val)
255                 pass
256         def nimPortCChanged(self, slotid, val):
257                 #print "nimDiseqcC set to " + str(val)
258                 pass
259         def nimPortDChanged(self, slotid, val):
260                 #print "nimDiseqcD set to " + str(val)
261                 pass
262
263
264 def InitNimManager(nimmgr):
265         config.Nims = []
266         for x in range(nimmgr.nimCount):
267                 config.Nims.append(ConfigSubsection())
268                 
269         def nimConfigModeChanged(slotid, configElement):
270                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
271         def nimDiseqcModeChanged(slotid, configElement):
272                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
273                 
274         def nimPortAChanged(slotid, configElement):
275                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
276         def nimPortBChanged(slotid, configElement):
277                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
278         def nimPortCChanged(slotid, configElement):
279                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
280         def nimPortDChanged(slotid, configElement):
281                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
282
283         for slot in nimmgr.nimslots:
284                 x = slot.slotid
285                 cname = nimmgr.getConfigPrefix(x)
286                 nim = config.Nims[x]
287                 
288                 if slot.nimType == nimmgr.nimType["DVB-S"]:
289                         nim.configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
290                         nim.diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D", "Positioner"));
291                         nim.diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
292                         nim.diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
293                         nim.diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
294                         nim.diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
295                         nim.longitude = configElement(cname + "longitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
296                         nim.latitude = configElement(cname + "latitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
297                         
298                         #perhaps the instance of the slot is more useful?
299                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
300                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
301                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
302                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
303                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
304                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
305                 else:
306                         print "pls add support for this frontend type!"         
307
308         nimmgr.sec = SecConfigure(nimmgr)
309
310 nimmanager = NimManager()