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