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