f04d076d29d50ef87428e6a63d82adbb10a4810f
[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                         return self.nimType["empty/unknown"]
136
137                 while 1:                
138                         line = nimfile.readline()
139                         if line == "":
140                                 break
141                         if line.startswith("NIM Socket"):
142                                 parts = line.strip().split(" ")
143                                 id = int(parts[2][:1])
144                                 if id == slotID:
145                                         line = nimfile.readline()
146                                         if line == "":
147                                                 break
148                                         if line.startswith("   Type:"):
149                                                 nimfile.close()
150                                                 return self.nimType["DVB-S"]
151                                         else:
152                                                 break   
153                 nimfile.close()
154                 return self.nimType["empty/unknown"]
155                 
156                 if slotID == 0:
157                         return self.nimType["DVB-S"]
158                 elif slotID == 1:
159                         return self.nimType["DVB-S"]
160                 else:
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                 #print "slotid:", slotid
211                 list = []
212                 #print "self.satellites:", self.satList[config.Nims[slotid].diseqcA.value]
213                 #print "diseqcA:", config.Nims[slotid].diseqcA.value
214                 if (config.Nims[slotid].diseqcMode.value <= 3):
215                         list.append(self.satList[config.Nims[slotid].diseqcA.value])
216                 if (0 < config.Nims[slotid].diseqcMode.value <= 3):
217                         list.append(self.satList[config.Nims[slotid].diseqcB.value])
218                 if (config.Nims[slotid].diseqcMode.value == 3):
219                         list.append(self.satList[config.Nims[slotid].diseqcC.value])
220                         list.append(self.satList[config.Nims[slotid].diseqcD.value])
221                 return list
222
223         #callbacks for c++ config
224         def nimConfigModeChanged(self, slotid, mode):
225                 #print "nimConfigModeChanged set to " + str(mode)
226                 pass
227         def nimDiseqcModeChanged(self, slotid, mode):
228                 #print "nimDiseqcModeChanged set to " + str(mode)
229                 pass
230         def nimPortAChanged(self, slotid, val):
231                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
232                 pass
233         def nimPortBChanged(self, slotid, val):
234                 #print "nimDiseqcA set to " + str(slotid) + " val:" + str(val)
235                 #print "nimDiseqcB set to " + str(val)
236                 pass
237         def nimPortCChanged(self, slotid, val):
238                 #print "nimDiseqcC set to " + str(val)
239                 pass
240         def nimPortDChanged(self, slotid, val):
241                 #print "nimDiseqcD set to " + str(val)
242                 pass
243
244
245 def InitNimManager(nimmgr):
246         config.Nims = []
247         for x in range(nimmgr.nimCount):
248                 config.Nims.append(ConfigSubsection())
249                 
250         def nimConfigModeChanged(slotid, configElement):
251                 nimmgr.nimConfigModeChanged(slotid, configElement.value)
252         def nimDiseqcModeChanged(slotid, configElement):
253                 nimmgr.nimDiseqcModeChanged(slotid, configElement.value)
254                 
255         def nimPortAChanged(slotid, configElement):
256                 nimmgr.nimPortAChanged(slotid, configElement.vals[configElement.value][1])
257         def nimPortBChanged(slotid, configElement):
258                 nimmgr.nimPortBChanged(slotid, configElement.vals[configElement.value][1])
259         def nimPortCChanged(slotid, configElement):
260                 nimmgr.nimPortCChanged(slotid, configElement.vals[configElement.value][1])
261         def nimPortDChanged(slotid, configElement):
262                 nimmgr.nimPortDChanged(slotid, configElement.vals[configElement.value][1])
263
264         for slot in nimmgr.nimslots:
265                 x = slot.slotid
266                 cname = nimmgr.getConfigPrefix(x)
267                 nim = config.Nims[x]
268                 
269                 if slot.nimType == nimmgr.nimType["DVB-S"]:
270                         nim.configMode = configElement(cname + "configMode",configSelection, 0, ("Simple", "Advanced"));
271                         nim.diseqcMode = configElement(cname + "diseqcMode",configSelection, 2, ("Single", "Toneburst A/B", "DiSEqC A/B", "DiSEqC A/B/C/D", "Positioner"));
272                         nim.diseqcA = configElement(cname + "diseqcA",configSatlist, 192, nimmgr.satList);
273                         nim.diseqcB = configElement(cname + "diseqcB",configSatlist, 130, nimmgr.satList);
274                         nim.diseqcC = configElement(cname + "diseqcC",configSatlist, 0, nimmgr.satList);
275                         nim.diseqcD = configElement(cname + "diseqcD",configSatlist, 0, nimmgr.satList);
276                         nim.longitude = configElement(cname + "longitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
277                         nim.latitude = configElement(cname + "latitude",configSequence, [0,0], configsequencearg.get("FLOAT", [(0,90),(0,999)]));
278                         
279                         #perhaps the instance of the slot is more useful?
280                         nim.configMode.addNotifier(boundFunction(nimConfigModeChanged,x))
281                         nim.diseqcMode.addNotifier(boundFunction(nimDiseqcModeChanged,x))
282                         nim.diseqcA.addNotifier(boundFunction(nimPortAChanged,int(x)))
283                         nim.diseqcB.addNotifier(boundFunction(nimPortBChanged,x))
284                         nim.diseqcC.addNotifier(boundFunction(nimPortCChanged,x))
285                         nim.diseqcD.addNotifier(boundFunction(nimPortDChanged,x))
286                 else:
287                         print "pls add support for this frontend type!"         
288
289         nimmgr.sec = SecConfigure(nimmgr)
290
291 nimmanager = NimManager()