switch to 4.2.x (initial add 4.2.0)
[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                                         del self.session.pip
63                                         if not self.openFrontend():
64                                                 self.frontend = None # in normal case this should not happen
65                 self.tuner = Tuner(self.frontend)
66                 self.timer = eTimer()
67                 self.timer_conn = self.timer.timeout.connect(self.updateStatus)
68
69         def gotTsidOnid(self, tsidonid):
70                 if tsidonid == -1:
71                         print "******** got tsidonid failed"
72                         self.pidStatus = self.INTERNAL_PID_STATUS_FAILED
73                         self.tsid = -1
74                         self.onid = -1
75                 else:
76                         self.pidStatus = self.INTERNAL_PID_STATUS_SUCCESSFUL
77                         self.tsid = (tsidonid>>16)&0xFFFF
78                         self.onid = tsidonid&0xFFFF
79                         print "******** got tsid %04x, onid %04x" %(self.tsid, self.onid)
80                 self.timer.start(100, True)
81                 self.__requestTsidOnid_conn = None
82                 
83         def updateStatus(self):
84                 fe_data = { }
85                 self.frontend.getFrontendStatus(fe_data)
86                 stop = False
87                 state = fe_data["tuner_state"]
88                 print "status:", state
89                 if state == stateTuning:
90                         print "TUNING"
91                         self.timer.start(100, True)
92                         self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_TUNING, self.currTuned))
93                 elif self.checkPIDs and self.pidStatus == self.INTERNAL_PID_STATUS_NOOP:
94                         print "2nd choice"
95                         if state == stateLock:
96                                 print "acquiring TSID/ONID"
97                                 class ePySlot1I(eSlot1I):
98                                         def __init__(self, func):
99                                                 eSlot1I.__init__(self)
100                                                 self.cb_func = func
101                                 self.__requestTsidOnid_conn = ePySlot1I(self.gotTsidOnid)
102                                 self.raw_channel.requestTsidOnid(self.__requestTsidOnid_conn)
103                                 self.pidStatus = self.INTERNAL_PID_STATUS_WAITING
104                         else:
105                                 self.pidStatus = self.INTERNAL_PID_STATUS_FAILED
106                 elif self.checkPIDs and self.pidStatus == self.INTERNAL_PID_STATUS_WAITING:
107                         print "waiting for pids"                        
108                 else:
109                         if state == stateLostLock or state == stateFailed:
110                                 self.tuningtransponder = self.nextTransponder()
111                                 self.failedTune.append([self.currTuned, self.oldTuned, "tune_failed", fe_data])  # last parameter is the frontend status)
112                                 if self.stopOnError != -1 and self.stopOnError <= len(self.failedTune):
113                                         stop = True
114                         elif state == stateLock:
115                                 pidsFailed = False
116                                 if self.checkPIDs:
117                                         if self.currTuned is not None:
118                                                 if self.tsid != self.currTuned[10] or self.onid != self.currTuned[11]:
119                                                         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
120                                                         pidsFailed = True
121                                                 else:
122                                                         self.successfullyTune.append([self.currTuned, self.oldTuned, fe_data])  # 3rd parameter is the frontend status
123                                                         if self.stopOnSuccess != -1 and self.stopOnSuccess <= len(self.successfullyTune):
124                                                                 stop = True
125                                 elif not self.checkPIDs or (self.checkPids and not pidsFailed):  
126                                         self.successfullyTune.append([self.currTuned, self.oldTuned, fe_data]) # 3rd parameter is the frontend status
127                                         if self.stopOnSuccess != -1 and self.stopOnSuccess <= len(self.successfullyTune):
128                                                                 stop = True
129                                 self.tuningtransponder = self.nextTransponder()
130                         else:
131                                 print "************* tuner_state:", state
132                                 
133                         self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_NOOP, self.currTuned))
134                         
135                         if not stop:
136                                 self.tune()
137                 if self.tuningtransponder < len(self.transponderlist) and not stop:
138                         if self.pidStatus != self.INTERNAL_PID_STATUS_WAITING:
139                                 self.timer.start(100, True)
140                                 print "restart timer"
141                         else:
142                                 print "not restarting timers (waiting for pids)"
143                 else:
144                         self.progressCallback((self.getProgressLength(), len(self.transponderlist), self.STATUS_DONE, self.currTuned))
145                         print "finishedChecking"
146                         self.finishedChecking()
147                                 
148         def firstTransponder(self):
149                 print "firstTransponder:"
150                 index = 0
151                 if self.checkPIDs:
152                         print "checkPIDs-loop"
153                         # check for tsid != -1 and onid != -1 
154                         print "index:", index
155                         print "len(self.transponderlist):", len(self.transponderlist)
156                         while (index < len(self.transponderlist) and (self.transponderlist[index][10] == -1 or self.transponderlist[index][11] == -1)):
157                                 index += 1
158                 print "FirstTransponder final index:", index
159                 return index
160         
161         def nextTransponder(self):
162                 print "getting next transponder", self.tuningtransponder
163                 index = self.tuningtransponder + 1
164                 if self.checkPIDs:
165                         print "checkPIDs-loop"
166                         # check for tsid != -1 and onid != -1 
167                         print "index:", index
168                         print "len(self.transponderlist):", len(self.transponderlist)
169                         while (index < len(self.transponderlist) and (self.transponderlist[index][10] == -1 or self.transponderlist[index][11] == -1)):
170                                 index += 1
171
172                 print "next transponder index:", index
173                 return index
174         
175         def finishedChecking(self):
176                 print "finished testing"
177                 print "successfull:", self.successfullyTune
178                 print "failed:", self.failedTune
179         
180         def openFrontend(self):
181                 res_mgr = eDVBResourceManager.getInstance()
182                 if res_mgr:
183                         self.raw_channel = res_mgr.allocateRawChannel(self.feid)
184                         if self.raw_channel:
185                                 self.frontend = self.raw_channel.getFrontend()
186                                 if self.frontend:
187                                         return True
188                                 else:
189                                         print "getFrontend failed"
190                         else:
191                                 print "getRawChannel failed"
192                 else:
193                         print "getResourceManager instance failed"
194                 return False
195
196         def tune(self):
197                 print "tuning to", self.tuningtransponder
198                 if self.tuningtransponder < len(self.transponderlist):
199                         self.pidStatus = self.INTERNAL_PID_STATUS_NOOP
200                         self.oldTuned = self.currTuned
201                         self.currTuned = self.transponderlist[self.tuningtransponder]
202                         self.tuner.tune(self.transponderlist[self.tuningtransponder])           
203
204         INTERNAL_PID_STATUS_NOOP = 0
205         INTERNAL_PID_STATUS_WAITING = 1
206         INTERNAL_PID_STATUS_SUCCESSFUL = 2
207         INTERNAL_PID_STATUS_FAILED = 3
208         
209         def run(self, checkPIDs = False):
210                 self.checkPIDs = checkPIDs
211                 self.pidStatus = self.INTERNAL_PID_STATUS_NOOP
212                 self.failedTune = []
213                 self.successfullyTune = []
214                 self.tuningtransponder = self.firstTransponder()
215                 self.tune()
216                 self.progressCallback((self.getProgressLength(), self.tuningtransponder, self.STATUS_START, self.currTuned))
217                 self.timer.start(100, True)
218         
219         # transponder = (frequency, symbolrate, polarisation, fec, inversion, orbpos, <system>, <modulation>, <rolloff>, <pilot>, <tsid>, <onid>)
220         #                    0         1             2         3       4         5       6        7              8         9        10       11
221         def addTransponder(self, transponder):
222                 self.transponderlist.append(transponder)
223                 
224         def clearTransponder(self):
225                 self.transponderlist = []
226                 
227         def getProgressLength(self):
228                 count = 0
229                 if self.stopOnError == -1:
230                         count = len(self.transponderlist)
231                 else:
232                         if count < self.stopOnError:
233                                 count = self.stopOnError
234                 if self.stopOnSuccess == -1:
235                         count = len(self.transponderlist)
236                 else:
237                         if count < self.stopOnSuccess:
238                                 count = self.stopOnSuccess
239                 return count
240                 
241         STATUS_START = 0
242         STATUS_TUNING = 1
243         STATUS_DONE = 2
244         STATUS_NOOP = 3
245         # can be overwritten
246         # progress = (range, value, status, transponder)
247         def progressCallback(self, progress):
248                 pass