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