add possibility to invalidate configList entrys other than the current one
[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):
103                         self.isPointsElement, self.isReboundsElement = 0, 0
104                         self.satList = satList
105                         self.satellites = satellites
106         
107                 def startElement(self, name, attrs):
108                         if (name == "sat"):
109                                 #print "found sat " + attrs.get('name',"") + " " + str(attrs.get('position',""))
110                                 tpos = attrs.get('position',"")
111                                 tname = attrs.get('name',"")
112                                 self.satellites[tpos] = tname
113                                 self.satList.append( (tname, tpos) )
114
115         def getConfiguredSats(self):
116                 return self.sec.getSatList()
117
118         def getSatDescription(self, pos):
119                 return self.satellites[str(pos)]
120
121         def readSatsfromFile(self):
122                 self.satellites = { }
123
124                 print "Reading satellites.xml"
125                 parser = make_parser()
126                 satHandler = self.parseSats(self.satList, self.satellites)
127                 parser.setContentHandler(satHandler)
128                 parser.parse('/etc/tuxbox/satellites.xml')
129
130         def getNimType(self, slotID):
131                 #FIXME get it from /proc
132                 nimfile = tryOpen("/proc/bus/nim_sockets")
133
134                 if nimfile == "":
135                         # FIXME: remove this in the final version
136                         # check if we have a device for 7020 comp?atibility reasons
137                         try:
138                                 open("/dev/dvb/card0/frontend" + str(slotID))
139                                 return self.nimType["DVB-S"]
140                         except IOError:
141                                 return self.nimType["empty/unknown"]
142
143
144                 while 1:                
145                         line = nimfile.readline()
146                         if line == "":
147                                 break
148                         if line.startswith("NIM Socket"):
149                                 parts = line.strip().split(" ")
150                                 id = int(parts[2][:1])
151                                 if id == slotID:
152                                         line = nimfile.readline()
153                                         if line == "":
154                                                 break
155                                         if line.startswith("   Type:"):
156                                                 nimfile.close()
157                                                 return self.nimType["DVB-S"]
158                                         else:
159                                                 break   
160                 nimfile.close()
161                 return self.nimType["empty/unknown"]
162
163         def getNimName(self, slotID):
164                 #FIXME get it from /proc
165                 return "Alps BSBE1"
166
167         def getNimSocketCount(self):
168                 #FIXME get it from /proc
169                 return 2
170
171         def getConfigPrefix(self, slotid):
172                 return "config.Nim" + ("A","B","C","D")[slotid] + "."
173                         
174         def __init__(self):
175                 #use as enum
176                 self.nimType = {                "empty/unknown": -1,
177                                                                                                 "DVB-S": 0,
178                                                                                                 "DVB-C": 1,
179                                                                                                 "DVB-T": 2}
180                 self.satList = [ ]                                                                              
181                                                                                                 
182                 self.readSatsfromFile()                                                                         
183                                                                                                 
184                 self.nimCount = self.getNimSocketCount()
185                 
186                 self.nimslots = [ ]
187                 x = 0
188                 while x < self.nimCount:
189                         tType = self.getNimType(x)
190                         tName = self.getNimName(x)
191                         tNim = nimSlot(x, tType, tName)
192                         self.nimslots.append(tNim)
193                         x += 1
194                 
195                 InitNimManager(self)    #init config stuff
196
197         def nimList(self):
198                 list = [ ]
199                 for slot in self.nimslots:
200                         nimText = "Socket " + ("A", "B", "C", "D")[slot.slotid] + ": "
201                         if slot.nimType == -1:
202                                 nimText += "empty/unknown"
203                         else:
204                                 nimText += slot.name + " ("     
205                                 nimText += ("DVB-S", "DVB-C", "DVB-T")[slot.nimType] + ")"
206                         list.append((nimText, slot))
207                 return list
208         
209         def getSatListForNim(self, slotid):
210                 list = []
211                 if (self.getNimType(slotid) != self.nimType["empty/unknown"]):
212                         #print "slotid:", slotid
213                         
214                         #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
215                         #print "diseqcA:", config.Nims[slotid].diseqcA.value
216                         if (config.Nims[slotid].diseqcMode.value <= 3):
217                                 list.append(self.satList[config.Nims[slotid].diseqcA.value])
218                         if (0 < config.Nims[slotid].diseqcMode.value <= 3):
219                                 list.append(self.satList[config.Nims[slotid].diseqcB.value])
220                         if (config.Nims[slotid].diseqcMode.value == 3):
221                                 list.append(self.satList[config.Nims[slotid].diseqcC.value])
222                                 list.append(self.satList[config.Nims[slotid].diseqcD.value])
223                 return list
224
225         #callbacks for c++ config
226         def nimConfigModeChanged(self, slotid, mode):
227                 #print "nimConfigModeChanged set to " + str(mode)
228                 pass
229         def nimDiseqcModeChanged(self, slotid, mode):
230                 #print "nimDiseqcModeChanged set to " + str(mode)
231                 pass
232         def nimPortAChanged(self, slotid, val):
233                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
234                 pass
235         def nimPortBChanged(self, slotid, val):
236                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
237                 #print "nimDiseqcB set to " + str(val)
238                 pass
239         def nimPortCChanged(self, slotid, val):
240                 #print "nimDiseqcC set to " + str(val)
241                 pass
242         def nimPortDChanged(self, slotid, val):
243                 #print "nimDiseqcD set to " + str(val)
244                 pass
245
246
247 def InitNimManager(nimmgr):
248         config.Nims = []
249         for x in range(nimmgr.nimCount):
250                 config.Nims.append(ConfigSubsection())
251                 
252         def nimConfigModeChanged(slotid, configElement):
253                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
254         def nimDiseqcModeChanged(slotid, configElement):
255                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
256                 
257         def nimPortAChanged(slotid, configElement):
258                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
259         def nimPortBChanged(slotid, configElement):
260                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
261         def nimPortCChanged(slotid, configElement):
262                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
263         def nimPortDChanged(slotid, configElement):
264                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
265
266         for slot in nimmgr.nimslots:
267                 x = slot.slotid
268                 cname = nimmgr.getConfigPrefix(x)
269                 nim = config.Nims[x]
270                 
271                 if slot.nimType == nimmgr.nimType["DVB-S"]:
272                         nim.configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
273                         nim.diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D", "Positioner"));
274                         nim.diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
275                         nim.diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
276                         nim.diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
277                         nim.diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
278                         nim.longitude = configElement(cname + "longitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
279                         nim.latitude = configElement(cname + "latitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
280                         
281                         #perhaps the instance of the slot is more useful?
282                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
283                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
284                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
285                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
286                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
287                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
288                 else:
289                         print "pls add support for this frontend type!"         
290
291         nimmgr.sec = SecConfigure(nimmgr)
292
293 nimmanager = NimManager()