enigma2 (20101216 rel31 -> 20110928 rel32)
[enigma2.git] / usr / lib / enigma2 / python / Components / TuneTest.py
1 from enigma import eDVBFrontendParametersSatellite, eDVBFrontendParameters, eDVBResourceManager, eTimer
2
3 class Tuner:
4         def __init__(self, frontend, ignore_rotor=False):
5                 self.frontend = frontend
6                 self.ignore_rotor = ignore_rotor
7
8         # transponder = (frequency, symbolrate, polarisation, fec, inversion, orbpos, system, modulation, rolloff, pilot, tsid, onid)
9         #                    0         1             2         3       4         5       6        7          8       9      10    11
10         def tune(self, transponder):
11                 if self.frontend:
12                         print "tuning to transponder with data", transponder
13                         parm = eDVBFrontendParametersSatellite()
14                         parm.frequency = transponder[0] * 1000
15                         parm.symbol_rate = transponder[1] * 1000
16                         parm.polarisation = transponder[2]
17                         parm.fec = transponder[3]
18                         parm.inversion = transponder[4]
19                         parm.orbital_position = transponder[5]
20                         parm.system = transponder[6]
21                         parm.modulation = transponder[7]
22                         parm.rolloff = transponder[8]
23                         parm.pilot = transponder[9]
24                         feparm = eDVBFrontendParameters()
25                         feparm.setDVBS(parm, self.ignore_rotor)
26                         self.lastparm = feparm
27                         self.frontend.tune(feparm)
28
29         def retune(self):
30                 if self.frontend:
31                         self.frontend.tune(self.lastparm)
32
33         def getTransponderData(self):
34                 ret = { }
35                 if self.frontend:
36                         self.frontend.getTransponderData(ret, True)
37                 return ret
38
39 # tunes a list of transponders and checks, if they lock and optionally checks the onid/tsid combination
40 # 1) add transponders with addTransponder()
41 # 2) call run(<checkPIDs = True>)
42 # 3) finishedChecking() is called, when the run is finished
43 class TuneTest:
44         def __init__(self, feid, stopOnSuccess = -1, stopOnError = -1):
45                 self.stopOnSuccess = stopOnSuccess
46                 self.stopOnError = stopOnError
47                 self.feid = feid
48                 self.transponderlist = []
49                 self.currTuned = None
50                 print "TuneTest for feid %d" % self.feid
51                 if not self.openFrontend():
52                         self.oldref = self.session.nav.getCurrentlyPlayingServiceReference()
53                         self.session.nav.stopService() # try to disable foreground service
54                         if not self.openFrontend():
55                                 if self.session.pipshown: # try to disable pip
56                                         self.session.pipshown = False
57                                         del self.session.pip
58                                         if not self.openFrontend():
59                                                 self.frontend = None # in normal case this should not happen
60                 self.tuner = Tuner(self.frontend)
61                 self.timer = eTimer()
62                 self.timer.callback.append(self.updateStatus)
63                 
64         def gotTsidOnid(self, tsid, onid):
65                 print "******** got tsid, onid:", tsid, onid
66                 if tsid is not None and onid is not None:
67                         self.pidStatus = self.INTERNAL_PID_STATUS_SUCCESSFUL
68                         self.tsid = tsid
69                         self.onid = onid
70                 else:
71                         self.pidStatus = self.INTERNAL_PID_STATUS_FAILED
72                         self.tsid = -1
73                         self.onid = -1
74                 self.timer.start(100, True)
75                         
76         def updateStatus(self):
77                 dict = {}
78                 self.frontend.getFrontendStatus(dict)
79                 stop = False
80                 
81                 print "status:", dict
82                 if dict["tuner_state"] == "TUNING":
83                         print "TUNING"
84                         self.timer.start(100, True)
85                         self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_TUNING, self.currTuned))
86                 elif self.checkPIDs and self.pidStatus == self.INTERNAL_PID_STATUS_NOOP:
87                         print "2nd choice"
88                         if dict["tuner_state"] == "LOCKED":
89                                 print "acquiring TSID/ONID"
90                                 self.raw_channel.requestTsidOnid(self.gotTsidOnid)
91                                 self.pidStatus = self.INTERNAL_PID_STATUS_WAITING
92                         else:
93                                 self.pidStatus = self.INTERNAL_PID_STATUS_FAILED
94                 elif self.checkPIDs and self.pidStatus == self.INTERNAL_PID_STATUS_WAITING:
95                         print "waiting for pids"                        
96                 else:
97                         if dict["tuner_state"] == "LOSTLOCK" or dict["tuner_state"] == "FAILED":
98                                 self.tuningtransponder = self.nextTransponder()
99                                 self.failedTune.append([self.currTuned, self.oldTuned, "tune_failed", dict])  # last parameter is the frontend status)
100                                 if self.stopOnError != -1 and self.stopOnError <= len(self.failedTune):
101                                         stop = True
102                         elif dict["tuner_state"] == "LOCKED":
103                                 pidsFailed = False
104                                 if self.checkPIDs:
105                                         if self.currTuned is not None:
106                                                 if self.tsid != self.currTuned[10] or self.onid != self.currTuned[11]:
107                                                         self.failedTune.append([self.currTuned, self.oldTuned, "pids_failed", {"real": (self.tsid, self.onid), "expected": (self.currTuned[10], self.currTuned[11])}, dict])  # last parameter is the frontend status
108                                                         pidsFailed = True
109                                                 else:
110                                                         self.successfullyTune.append([self.currTuned, self.oldTuned, dict])  # 3rd parameter is the frontend status
111                                                         if self.stopOnSuccess != -1 and self.stopOnSuccess <= len(self.successfullyTune):
112                                                                 stop = True
113                                 elif not self.checkPIDs or (self.checkPids and not pidsFailed):  
114                                         self.successfullyTune.append([self.currTuned, self.oldTuned, dict]) # 3rd parameter is the frontend status
115                                         if self.stopOnSuccess != -1 and self.stopOnSuccess <= len(self.successfullyTune):
116                                                                 stop = True
117                                 self.tuningtransponder = self.nextTransponder()
118                         else:
119                                 print "************* tuner_state:", dict["tuner_state"]
120                                 
121                         self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_NOOP, self.currTuned))
122                         
123                         if not stop:
124                                 self.tune()
125                 if self.tuningtransponder < len(self.transponderlist) and not stop:
126                         if self.pidStatus != self.INTERNAL_PID_STATUS_WAITING:
127                                 self.timer.start(100, True)
128                                 print "restart timer"
129                         else:
130                                 print "not restarting timers (waiting for pids)"
131                 else:
132                         self.progressCallback((self.getProgressLength(), len(self.transponderlist), self.STATUS_DONE, self.currTuned))
133                         print "finishedChecking"
134                         self.finishedChecking()
135                                 
136         def firstTransponder(self):
137                 print "firstTransponder:"
138                 index = 0
139                 if self.checkPIDs:
140                         print "checkPIDs-loop"
141                         # check for tsid != -1 and onid != -1 
142                         print "index:", index
143                         print "len(self.transponderlist):", len(self.transponderlist)
144                         while (index < len(self.transponderlist) and (self.transponderlist[index][10] == -1 or self.transponderlist[index][11] == -1)):
145                                 index += 1
146                 print "FirstTransponder final index:", index
147                 return index
148         
149         def nextTransponder(self):
150                 print "getting next transponder", self.tuningtransponder
151                 index = self.tuningtransponder + 1
152                 if self.checkPIDs:
153                         print "checkPIDs-loop"
154                         # check for tsid != -1 and onid != -1 
155                         print "index:", index
156                         print "len(self.transponderlist):", len(self.transponderlist)
157                         while (index < len(self.transponderlist) and (self.transponderlist[index][10] == -1 or self.transponderlist[index][11] == -1)):
158                                 index += 1
159
160                 print "next transponder index:", index
161                 return index
162         
163         def finishedChecking(self):
164                 print "finished testing"
165                 print "successfull:", self.successfullyTune
166                 print "failed:", self.failedTune
167         
168         def openFrontend(self):
169                 res_mgr = eDVBResourceManager.getInstance()
170                 if res_mgr:
171                         self.raw_channel = res_mgr.allocateRawChannel(self.feid)
172                         if self.raw_channel:
173                                 self.frontend = self.raw_channel.getFrontend()
174                                 if self.frontend:
175                                         return True
176                                 else:
177                                         print "getFrontend failed"
178                         else:
179                                 print "getRawChannel failed"
180                 else:
181                         print "getResourceManager instance failed"
182                 return False
183
184         def tune(self):
185                 print "tuning to", self.tuningtransponder
186                 if self.tuningtransponder < len(self.transponderlist):
187                         self.pidStatus = self.INTERNAL_PID_STATUS_NOOP
188                         self.oldTuned = self.currTuned
189                         self.currTuned = self.transponderlist[self.tuningtransponder]
190                         self.tuner.tune(self.transponderlist[self.tuningtransponder])           
191
192         INTERNAL_PID_STATUS_NOOP = 0
193         INTERNAL_PID_STATUS_WAITING = 1
194         INTERNAL_PID_STATUS_SUCCESSFUL = 2
195         INTERNAL_PID_STATUS_FAILED = 3
196         
197         def run(self, checkPIDs = False):
198                 self.checkPIDs = checkPIDs
199                 self.pidStatus = self.INTERNAL_PID_STATUS_NOOP
200                 self.failedTune = []
201                 self.successfullyTune = []
202                 self.tuningtransponder = self.firstTransponder()
203                 self.tune()
204                 self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_START, self.currTuned))
205                 self.timer.start(100, True)
206         
207         # transponder = (frequency, symbolrate, polarisation, fec, inversion, orbpos, <system>, <modulation>, <rolloff>, <pilot>, <tsid>, <onid>)
208         #                    0         1             2         3       4         5       6        7              8         9        10       11
209         def addTransponder(self, transponder):
210                 self.transponderlist.append(transponder)
211                 
212         def clearTransponder(self):
213                 self.transponderlist = []
214                 
215         def getProgressLength(self):
216                 count = 0
217                 if self.stopOnError == -1:
218                         count = len(self.transponderlist)
219                 else:
220                         if count < self.stopOnError:
221                                 count = self.stopOnError
222                 if self.stopOnSuccess == -1:
223                         count = len(self.transponderlist)
224                 else:
225                         if count < self.stopOnSuccess:
226                                 count = self.stopOnSuccess
227                 return count
228                 
229         STATUS_START = 0
230         STATUS_TUNING = 1
231         STATUS_DONE = 2
232         STATUS_NOOP = 3
233         # can be overwritten
234         # progress = (range, value, status, transponder)
235         def progressCallback(self, progress):
236                 pass