4b2f309dbfeca62c2706bd3ac04bb68022ae3e86
[enigma2.git] / lib / python / Screens / ScanSetup.py
1 from Screen import Screen
2 from ServiceScan import *
3 from Components.config import config, ConfigSubsection, ConfigSelection, \
4         ConfigYesNo, ConfigInteger, getConfigListEntry, ConfigSlider, ConfigSatlist, ConfigEnableDisable
5 from Components.ActionMap import NumberActionMap
6 from Components.ConfigList import ConfigList, ConfigListScreen
7 from Components.NimManager import nimmanager, getConfigSatlist
8 from Components.Label import Label
9 from Screens.MessageBox import MessageBox
10 from enigma import eTimer, eDVBFrontendParametersSatellite, eComponentScan, \
11         eDVBSatelliteEquipmentControl, eDVBFrontendParametersTerrestrial, \
12         eDVBFrontendParametersCable, eConsoleAppContainer, eDVBResourceManager
13
14 def buildTerTransponder(frequency, 
15                 inversion=2, bandwidth = 3, fechigh = 6, feclow = 6,
16                 modulation = 2, transmission = 2, guard = 4,
17                 hierarchy = 4):
18
19 #       print "freq", frequency, "inv", inversion, "bw", bandwidth, "fech", fechigh, "fecl", feclow, "mod", modulation, "tm", transmission, "guard", guard, "hierarchy", hierarchy
20
21         # WARNING: normally, enums are working directly.
22         # Don't copy this (very bad)!! Instead either fix swig (good) or
23         # move this into a central place.
24         Bw8MHz = 0
25         Bw7MHz = 1
26         Bw6MHz = 2
27         #Bw5MHz = 3 #not implemented for e1 compatibilty
28         BwAuto = 3
29         
30         f1_2 = 0
31         f2_3 = 1
32         f3_4 = 2
33         f5_6 = 3
34         f7_8 = 4
35         fAuto = 5
36         
37         TM2k = 0
38         TM8k = 1
39         #TM4k = 2  #not implemented for e1 compatibilty
40         TMAuto = 2
41         
42         GI_1_32 = 0
43         GI_1_16 = 1
44         GI_1_8 = 2
45         GI_1_4 = 3
46         GI_Auto = 4
47         
48         HNone = 0
49         H1 = 1
50         H2 = 2
51         H4 = 3
52         HAuto = 4
53
54         QPSK = 0
55         QAM16 = 1
56         QAM64 = 2
57         Auto = 3
58         
59         Off = 0
60         On = 1
61         Unknown = 2
62
63         parm = eDVBFrontendParametersTerrestrial()
64
65         parm.frequency = frequency
66
67         parm.inversion = [Off, On, Unknown][inversion]
68         parm.bandwidth = [Bw8MHz, Bw7MHz, Bw6MHz, BwAuto][bandwidth] # Bw5MHz unsupported
69         parm.code_rate_HP = [f1_2, f2_3, f3_4, f5_6, f7_8, fAuto][fechigh]
70         parm.code_rate_LP = [f1_2, f2_3, f3_4, f5_6, f7_8, fAuto][feclow]
71         parm.modulation = [QPSK, QAM16, QAM64, Auto][modulation]
72         parm.transmission_mode = [TM2k, TM8k, TMAuto][transmission] # TM4k unsupported
73         parm.guard_interval = [GI_1_32, GI_1_16, GI_1_8, GI_1_4, GI_Auto][guard]
74         parm.hierarchy = [HNone, H1, H2, H4, HAuto][hierarchy]
75         
76         return parm
77
78 def getInitialTransponderList(tlist, pos):
79         list = nimmanager.getTransponders(pos)
80         for x in list:
81                 if x[0] == 0:           #SAT
82                         parm = eDVBFrontendParametersSatellite()
83                         parm.frequency = x[1]
84                         parm.symbol_rate = x[2]
85                         parm.polarisation = x[3]
86                         parm.fec = x[4]
87                         parm.inversion = 2 # AUTO
88                         parm.orbital_position = pos
89                         parm.system = x[5]
90                         parm.modulation = x[6]
91                         tlist.append(parm)
92
93 def getInitialCableTransponderList(tlist, nim):
94         list = nimmanager.getTranspondersCable(nim)
95         for x in list:
96                 if x[0] == 1: #CABLE
97                         parm = eDVBFrontendParametersCable()
98                         parm.frequency = x[1]
99                         parm.symbol_rate = x[2]
100                         parm.modulation = x[3]
101                         parm.fec_inner = x[4]
102                         parm.inversion = 2 # AUTO
103                         #print "frequency:", x[1]
104                         #print "symbol_rate:", x[2]
105                         #print "modulation:", x[3]
106                         #print "fec_inner:", x[4]
107                         #print "inversion:", 2
108                         tlist.append(parm)
109
110 def getInitialTerrestrialTransponderList(tlist, region):
111         list = nimmanager.getTranspondersTerrestrial(region)
112
113         #self.transponders[self.parsedTer].append((2,freq,bw,const,crh,crl,guard,transm,hierarchy,inv))
114
115         #def buildTerTransponder(frequency, inversion = 2, bandwidth = 3, fechigh = 6, feclow = 6,
116                                 #modulation = 2, transmission = 2, guard = 4, hierarchy = 4):
117
118         for x in list:
119                 if x[0] == 2: #TERRESTRIAL
120                         parm = buildTerTransponder(x[1], x[9], x[2], x[4], x[5], x[3], x[7], x[6], x[8])
121                         tlist.append(parm)
122
123 cable_bands = {
124         "DVBC_BAND_EU_VHF_I" : 1 << 0,
125         "DVBC_BAND_EU_MID" : 1 << 1,
126         "DVBC_BAND_EU_VHF_III" : 1 << 2,
127         "DVBC_BAND_EU_SUPER" : 1 << 3,
128         "DVBC_BAND_EU_HYPER" : 1 << 4,
129         "DVBC_BAND_EU_UHF_IV" : 1 << 5,
130         "DVBC_BAND_EU_UHF_V" : 1 << 6,
131         "DVBC_BAND_US_LO" : 1 << 7,
132         "DVBC_BAND_US_MID" : 1 << 8,
133         "DVBC_BAND_US_HI" : 1 << 9,
134         "DVBC_BAND_US_SUPER" : 1 << 10,
135         "DVBC_BAND_US_HYPER" : 1 << 11,
136 }
137
138 class CableTransponderSearchSupport:
139 #       def setCableTransponderSearchResult(self, tlist):
140 #               pass
141
142 #       def cableTransponderSearchFinished(self):
143 #               pass
144
145         def tryGetRawFrontend(self, feid):
146                 res_mgr = eDVBResourceManager.getInstance()
147                 if res_mgr:
148                         raw_channel = res_mgr.allocateRawChannel(self.feid)
149                         if raw_channel:
150                                 frontend = raw_channel.getFrontend()
151                                 if frontend:
152                                         del frontend
153                                         del raw_channel
154                                         return True
155                 return False
156
157         def cableTransponderSearchSessionClosed(self, *val):
158                 print "cableTransponderSearchSessionClosed, val", val
159                 self.cable_search_container = None
160                 self.cable_search_session = None
161                 if val and len(val) and val[0]:
162                         self.setCableTransponderSearchResult(self.__tlist)
163                 self.__tlist = None
164                 self.cableTransponderSearchFinished()
165
166         def cableTransponderSearchClosed(self, retval):
167                 print "cableTransponderSearch finished", retval
168                 self.cable_search_session.close(True)
169
170         def getCableTransponderData(self, str):
171                 data = str.split()
172                 if len(data):
173                         if data[0] == 'OK':
174                                 print str
175                                 qam = { "QAM16" : 1, "QAM32" : 2, "QAM64" : 3, "QAM128" : 4, "QAM256" : 5 }
176                                 inv = { "INVERSION_OFF" : 0, "INVERSION_ON" : 1 }
177                                 fec = { "FEC_AUTO" : 0, "FEC_1_2" : 1, "FEC_2_3" : 2, "FEC_3_4" : 3, "FEC_5_6": 4, "FEC_7_8" : 5, "FEC_8_9" : 6, "FEC_NONE" : 15 }
178                                 parm = eDVBFrontendParametersCable()
179                                 parm.frequency = int(data[1])
180                                 parm.symbol_rate = int(data[2])
181                                 parm.fec_inner = fec[data[3]]
182                                 parm.modulation = qam[data[4]]
183                                 parm.inversion = inv[data[5]]
184                                 self.__tlist.append(parm)
185                 tmpstr = _("Try to find used Transponders in cable network.. please wait...")
186                 tmpstr += "\n\n"
187                 tmpstr += data[1]
188                 tmpstr += " kHz "
189                 tmpstr += data[0]
190                 self.cable_search_session["text"].setText(tmpstr)
191
192         def startCableTransponderSearch(self, nim_idx):
193                 if not self.tryGetRawFrontend(nim_idx):
194                         self.session.nav.stopService()
195                         if not self.tryGetRawFrontend(nim_idx):
196                                 if self.session.pipshown: # try to disable pip
197                                         self.session.pipshown = False
198                                         del self.session.pip
199                                 if not self.tryGetRawFrontend(nim_idx):
200                                         self.cableTransponderSearchFinished()
201                                         return
202                 self.__tlist = [ ]
203                 self.cable_search_container = eConsoleAppContainer()
204                 self.cable_search_container.appClosed.get().append(self.cableTransponderSearchClosed)
205                 self.cable_search_container.dataAvail.get().append(self.getCableTransponderData)
206                 cableConfig = config.Nims[nim_idx].cable
207                 cmd = "tda1002x --init --scan --verbose --wakeup --inv 2 --bus "
208                 if nim_idx < 2:
209                         cmd += str(nim_idx)
210                 else: # FIXMEE DM8000
211                         if nim_idx == 2:
212                                 cmd += "2" # first nim socket on DM8000 use /dev/i2c/2
213                         else:
214                                 cmd += "4" # second nim socket on DM8000 use /dev/i2c/4
215                 if cableConfig.scan_type.value == "bands":
216                         cmd += " --scan-bands "
217                         bands = 0
218                         if cableConfig.scan_band_EU_VHF_I.value:
219                                 bands |= cable_bands["DVBC_BAND_EU_VHF_I"]
220                         if cableConfig.scan_band_EU_MID.value:
221                                 bands |= cable_bands["DVBC_BAND_EU_MID"]
222                         if cableConfig.scan_band_EU_VHF_III.value:
223                                 bands |= cable_bands["DVBC_BAND_EU_VHF_III"]
224                         if cableConfig.scan_band_EU_UHF_IV.value:
225                                 bands |= cable_bands["DVBC_BAND_EU_UHF_IV"]
226                         if cableConfig.scan_band_EU_UHF_V.value:
227                                 bands |= cable_bands["DVBC_BAND_EU_UHF_V"]
228                         if cableConfig.scan_band_EU_SUPER.value:
229                                 bands |= cable_bands["DVBC_BAND_EU_SUPER"]
230                         if cableConfig.scan_band_EU_HYPER.value:
231                                 bands |= cable_bands["DVBC_BAND_EU_HYPER"]
232                         if cableConfig.scan_band_US_LOW.value:
233                                 bands |= cable_bands["DVBC_BAND_US_LO"]
234                         if cableConfig.scan_band_US_MID.value:
235                                 bands |= cable_bands["DVBC_BAND_US_MID"]
236                         if cableConfig.scan_band_US_HIGH.value:
237                                 bands |= cable_bands["DVBC_BAND_US_HI"]
238                         if cableConfig.scan_band_US_SUPER.value:
239                                 bands |= cable_bands["DVBC_BAND_US_SUPER"]
240                         if cableConfig.scan_band_US_HYPER.value:
241                                 bands |= cable_bands["DVBC_BAND_US_HYPER"]
242                         cmd += str(bands)
243                 else:
244                         cmd += " --scan-stepsize "
245                         cmd += str(cableConfig.scan_frequency_steps.value)
246                 if cableConfig.scan_mod_qam16.value:
247                         cmd += " --mod 16"
248                 if cableConfig.scan_mod_qam32.value:
249                         cmd += " --mod 32"
250                 if cableConfig.scan_mod_qam64.value:
251                         cmd += " --mod 64"
252                 if cableConfig.scan_mod_qam128.value:
253                         cmd += " --mod 128"
254                 if cableConfig.scan_mod_qam256.value:
255                         cmd += " --mod 256"
256                 if cableConfig.scan_sr_6900.value:
257                         cmd += " --sr 6900000"
258                 if cableConfig.scan_sr_6875.value:
259                         cmd += " --sr 6875000"
260                 if cableConfig.scan_sr_ext1.value > 450:
261                         cmd += " --sr "
262                         cmd += str(cableConfig.scan_sr_ext1.value)
263                         cmd += "000"
264                 if cableConfig.scan_sr_ext2.value > 450:
265                         cmd += " --sr "
266                         cmd += str(cableConfig.scan_sr_ext2.value)
267                         cmd += "000"
268                 print "TDA1002x CMD is", cmd
269
270                 self.cable_search_container.execute(cmd)
271                 tmpstr = _("Try to find used transponders in cable network.. please wait...")
272                 tmpstr += "\n\n..."
273                 self.cable_search_session = self.session.openWithCallback(self.cableTransponderSearchSessionClosed, MessageBox, tmpstr, MessageBox.TYPE_INFO)
274
275 class ScanSetup(ConfigListScreen, Screen, CableTransponderSearchSupport):
276         def __init__(self, session):
277                 Screen.__init__(self, session)
278
279                 self.updateSatList()
280                 self.service = session.nav.getCurrentService()
281                 self.feinfo = None
282                 frontendData = None
283                 if self.service is not None:
284                         self.feinfo = self.service.frontendInfo()
285                         frontendData = self.feinfo and self.feinfo.getAll(True)
286                 
287                 self.createConfig(frontendData)
288
289                 del self.feinfo
290                 del self.service
291
292                 self["actions"] = NumberActionMap(["SetupActions"],
293                 {
294                         "ok": self.keyGo,
295                         "cancel": self.keyCancel,
296                 }, -2)
297
298                 self.statusTimer = eTimer()
299                 self.statusTimer.timeout.get().append(self.updateStatus)
300                 #self.statusTimer.start(5000, True)
301
302                 self.list = []
303                 ConfigListScreen.__init__(self, self.list)
304                 self.createSetup()
305
306                 self["introduction"] = Label(_("Press OK to start the scan"))
307
308         def run(self):
309                 self.keyGo()
310
311         def updateSatList(self):
312                 self.satList = []
313                 for slot in nimmanager.nim_slots:
314                         if slot.isCompatible("DVB-S"):
315                                 self.satList.append(nimmanager.getSatListForNim(slot.slot))
316                         else:
317                                 self.satList.append(None)
318
319         def createSetup(self):
320                 self.list = []
321                 self.multiscanlist = []
322                 index_to_scan = int(self.scan_nims.value)
323                 print "ID: ", index_to_scan
324
325                 self.tunerEntry = getConfigListEntry(_("Tuner"), self.scan_nims)
326                 self.list.append(self.tunerEntry)
327                 
328                 if self.scan_nims == [ ]:
329                         return
330                 
331                 self.typeOfScanEntry = None
332                 self.systemEntry = None
333                 nim = nimmanager.nim_slots[index_to_scan]
334                 if nim.isCompatible("DVB-S"):
335                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_type)
336                         self.list.append(self.typeOfScanEntry)
337                 elif nim.isCompatible("DVB-C"):
338                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_typecable)
339                         self.list.append(self.typeOfScanEntry)
340                 elif nim.isCompatible("DVB-T"):
341                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_typeterrestrial)
342                         self.list.append(self.typeOfScanEntry)
343
344                 if nim.isCompatible("DVB-S"):
345                         if self.scan_type.value == "single_transponder":
346                                 self.updateSatList()
347                                 if nim.isCompatible("DVB-S2"):
348                                         self.systemEntry = getConfigListEntry(_('System'), self.scan_sat.system)
349                                         self.list.append(self.systemEntry)
350                                 else:
351                                         # downgrade to dvb-s, in case a -s2 config was active
352                                         self.scan_sat.system.value = "dvb-s"
353                                 self.list.append(getConfigListEntry(_('Satellite'), self.scan_satselection[index_to_scan]))
354                                 self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
355                                 self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
356                                 self.list.append(getConfigListEntry(_('Symbol Rate'), self.scan_sat.symbolrate))
357                                 self.list.append(getConfigListEntry(_("Polarity"), self.scan_sat.polarization))
358                                 if self.scan_sat.system.value == "dvb-s":
359                                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
360                                 elif self.scan_sat.system.value == "dvb-s2":
361                                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec_s2))
362                                         self.list.append(getConfigListEntry(_('Modulation'), self.scan_sat.modulation))
363                                 self.list.append(getConfigListEntry(_("Network scan"), self.scan_networkScan))
364                         elif self.scan_type.value == "single_satellite":
365                                 self.updateSatList()
366                                 print self.scan_satselection[index_to_scan]
367                                 self.list.append(getConfigListEntry(_("Satellite"), self.scan_satselection[index_to_scan]))
368                                 self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
369                         elif self.scan_type.value == "multisat":
370                                 # if (norotor)
371                                 tlist = []
372                                 SatList = nimmanager.getSatListForNim(index_to_scan)
373                                 self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
374                                 for x in SatList:
375                                         if self.Satexists(tlist, x[0]) == 0:
376                                                 tlist.append(x[0])
377                                                 sat = ConfigEnableDisable(default = True)
378                                                 configEntry = getConfigListEntry(nimmanager.getSatDescription(x[0]), sat)
379                                                 self.list.append(configEntry)
380                                                 self.multiscanlist.append((x[0], sat))
381                                 # if (rotor):
382                            # for sat in nimmanager.satList:
383                                 #       self.list.append(getConfigListEntry(sat[1], self.scan_scansat[sat[0]]))
384                 elif nim.isCompatible("DVB-C"):
385                         if self.scan_typecable.value == "single_transponder":
386                                 self.list.append(getConfigListEntry(_("Frequency"), self.scan_cab.frequency))
387                                 self.list.append(getConfigListEntry(_("Inversions"), self.scan_cab.inversion))
388                                 self.list.append(getConfigListEntry(_("Symbol Rate"), self.scan_cab.symbolrate))
389                                 self.list.append(getConfigListEntry(_("Modulation"), self.scan_cab.modulation))
390                                 self.list.append(getConfigListEntry(_("FEC"), self.scan_cab.fec))
391                                 self.list.append(getConfigListEntry(_("Network scan"), self.scan_networkScan))
392                         elif self.scan_typecable.value == "complete":
393                                 self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
394                 elif nim.isCompatible("DVB-T"):
395                         if self.scan_typeterrestrial.value == "single_transponder":
396                                 self.list.append(getConfigListEntry(_("Frequency"), self.scan_ter.frequency))
397                                 self.list.append(getConfigListEntry(_("Inversion"), self.scan_ter.inversion))
398                                 self.list.append(getConfigListEntry(_("Bandwidth"), self.scan_ter.bandwidth))
399                                 self.list.append(getConfigListEntry(_("Code rate high"), self.scan_ter.fechigh))
400                                 self.list.append(getConfigListEntry(_("Code rate low"), self.scan_ter.feclow))
401                                 self.list.append(getConfigListEntry(_("Modulation"), self.scan_ter.modulation))
402                                 self.list.append(getConfigListEntry(_("Transmission mode"), self.scan_ter.transmission))
403                                 self.list.append(getConfigListEntry(_("Guard interval mode"), self.scan_ter.guard))
404                                 self.list.append(getConfigListEntry(_("Hierarchy mode"), self.scan_ter.hierarchy))
405                                 self.list.append(getConfigListEntry(_("Network scan"), self.scan_networkScan))
406                         elif self.scan_typeterrestrial.value == "complete":
407                                 self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
408
409 #               if (nim.isCompatible("DVB-S") and self.scan_type.type == "single_transponder") or \
410 #                       (nim.isCompatbile("DVB-C") and self.scan_typecable.type == "single_transponder") or \
411 #                       (nim.isCompatible("DVB-T") and self.scan_typeterrestrial.type == "single_transponder"):
412 #                               self.configElementSNR = getConfigListEntry(_("SNR"), self.scan_snr)
413 #                               self.list.append(self.configElementSNR)
414 #                               self.configElementACG = getConfigListEntry(_("AGC"), self.scan_agc)
415 #                               self.list.append(self.configElementACG)
416 #                               self.configElementBER = getConfigListEntry(_("BER"), self.scan_ber)
417 #                               self.list.append(self.configElementBER)
418 #                               self.statusTimer.start(500, False)
419 #               else:
420 #                       self.statusTimer.stop()
421
422                 self["config"].list = self.list
423                 self["config"].l.setList(self.list)
424
425         def Satexists(self, tlist, pos):
426                 for x in tlist:
427                         if x == pos:
428                                 return 1
429                 return 0
430
431         def newConfig(self):
432                 cur = self["config"].getCurrent()
433                 print "cur is", cur
434                 if cur == self.typeOfScanEntry or \
435                         cur == self.tunerEntry or \
436                         cur == self.systemEntry:
437                         self.createSetup()
438
439         def createConfig(self, frontendData):
440                                                            #("Type", frontendData["system"], TYPE_TEXT),
441                                            #("Modulation", frontendData["modulation"], TYPE_TEXT),
442                                            #("Orbital position", frontendData["orbital_position"], TYPE_VALUE_DEC),
443                                            #("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
444                                            #("Symbolrate", frontendData["symbol_rate"], TYPE_VALUE_DEC),
445                                            #("Polarization", frontendData["polarization"], TYPE_TEXT),
446                                            #("Inversion", frontendData["inversion"], TYPE_TEXT),
447                                            #("FEC inner", frontendData["fec_inner"], TYPE_TEXT),
448                                                 #)
449                 #elif frontendData["tuner_type"] == "DVB-C":
450                         #return ( ("NIM", ['A', 'B', 'C', 'D'][frontendData["tuner_number"]], TYPE_TEXT),
451                                            #("Type", frontendData["tuner_type"], TYPE_TEXT),
452                                            #("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
453                                            #("Symbolrate", frontendData["symbol_rate"], TYPE_VALUE_DEC),
454                                            #("Modulation", frontendData["modulation"], TYPE_TEXT),
455                                            #("Inversion", frontendData["inversion"], TYPE_TEXT),
456                         #                  ("FEC inner", frontendData["fec_inner"], TYPE_TEXT),
457                                                 #)
458                 #elif frontendData["tuner_type"] == "DVB-T":
459                         #return ( ("NIM", ['A', 'B', 'C', 'D'][frontendData["tuner_number"]], TYPE_TEXT),
460                                            #("Type", frontendData["tuner_type"], TYPE_TEXT),
461                                            #("Frequency", frontendData["frequency"], TYPE_VALUE_DEC),
462                                            #("Inversion", frontendData["inversion"], TYPE_TEXT),
463                                            #("Bandwidth", frontendData["bandwidth"], TYPE_VALUE_DEC),
464                                            #("CodeRateLP", frontendData["code_rate_lp"], TYPE_TEXT),
465                                            #("CodeRateHP", frontendData["code_rate_hp"], TYPE_TEXT),
466                                            #("Constellation", frontendData["constellation"], TYPE_TEXT),
467                                            #("Transmission Mode", frontendData["transmission_mode"], TYPE_TEXT),
468                                            #("Guard Interval", frontendData["guard_interval"], TYPE_TEXT),
469                                            #("Hierarchy Inform.", frontendData["hierarchy_information"], TYPE_TEXT),
470                         defaultSat = { "orbpos": 192, "system": "dvb-s", "frequency": 11836, "inversion": "auto", "symbolrate": 27500, "polarization": "horizontal", "fec": "auto", "fec_s2": "9_10", "modulation": "qpsk" }
471                         defaultCab = {"frequency": 466, "inversion": "auto", "modulation": "64qam", "fec": "auto", "symbolrate": 6900}
472                         if frontendData is not None:
473                                 ttype = frontendData.get("tuner_type", "UNKNOWN")
474                                 if ttype == "DVB-S":
475                                         defaultSat["system"] = {"DVB-S": "dvb-s", "DVB-S2": "dvb-s2"}[frontendData.get("system", "DVB-S")]
476                                         defaultSat["frequency"] = int(frontendData.get("frequency", 0) / 1000)
477                                         defaultSat["inversion"] = {"INVERSION_OFF": "off", "INVERSION_ON": "on", "INVERSION_AUTO": "auto"}[frontendData.get("inversion", "INVERSION_AUTO")]
478                                         defaultSat["symbolrate"] = int(frontendData.get("symbol_rate", 0) / 1000)
479                                         defaultSat["polarization"] = {"HORIZONTAL": "horizontal", "VERTICAL": "vertical", "CIRCULAR_LEFT": "circular_left", "CIRCULAR_RIGHT": "circular_right", "UNKNOWN": None}[frontendData.get("polarization", "HORIZONTAL")]
480                                         
481                                         if frontendData.get("system", "DVB-S") == "DVB-S2":
482                                                 defaultSat["fec_s2"] = {"FEC_1_2": "1_2", "FEC_2_3": "2_3", "FEC_3_4": "3_4", "FEC_4_5": "4_5", "FEC_5_6": "5_6", "FEC_7_8": "7_8", "FEC_8_9": "8_9", "FEC_9_10": "9_10"} \
483                                                                                         [frontendData.get("fec_inner", "FEC_AUTO")]
484                                         else:
485                                                 defaultSat["fec"] = {"FEC_AUTO": "auto", "FEC_1_2": "1_2", "FEC_2_3": "2_3", "FEC_3_4": "3_4", "FEC_5_6": "5_6", "FEC_7_8": "7_8", "FEC_NONE": "none"} \
486                                                         [frontendData.get("fec_inner", "FEC_AUTO")]
487
488                                         defaultSat["modulation"] = {"QPSK": "qpsk", "8PSK": "8psk"}[frontendData.get("modulation", "QPSK")]
489                                         defaultSat["orbpos"] = frontendData.get("orbital_position", 0)
490                                 elif ttype == "DVB-C":
491                                         defaultCab["frequency"] = int(frontendData.get("frequency", 0) / 1000)
492                                         defaultCab["symbolrate"] = int(frontendData.get("symbol_rate", 0) / 1000)
493                                         defaultCab["inversion"] = {"INVERSION_OFF": "off", "INVERSION_ON": "on", "INVERSION_AUTO": "auto"}[frontendData.get("inversion", "INVERSION_AUTO")]
494                                         defaultCab["fec"] = {"FEC_AUTO": "auto", "FEC_1_2": "1_2", "FEC_2_3": "2_3", "FEC_3_4": "3_4", "FEC_5_6": "5_6", "FEC_7_8": "7_8", "FEC_8_9": "8_9", "FEC_NONE": "none"}[frontendData.get("fec_inner", "FEC_AUTO")]
495                                         defaultCab["modulation"] = {"QAM_AUTO": "auto", "QAM_16": "16qam", "QAM_32": "32qam", "QAM_64": "64qam", "QAM_128": "128qam", "QAM_256": "256qam"}[frontendData.get("modulation", "QAM_16")]
496
497                         self.scan_sat = ConfigSubsection()
498                         self.scan_cab = ConfigSubsection()
499                         self.scan_ter = ConfigSubsection()
500
501                         self.scan_type = ConfigSelection(default = "single_transponder", choices = [("single_transponder", _("Single transponder")), ("single_satellite", _("Single satellite")), ("multisat", _("Multisat"))])
502                         self.scan_typecable = ConfigSelection(default = "single_transponder", choices = [("single_transponder", _("Single transponder")), ("complete", _("Complete"))])
503                         self.scan_typeterrestrial = ConfigSelection(default = "single_transponder", choices = [("single_transponder", _("Single transponder")), ("complete", _("Complete"))])
504                         self.scan_clearallservices = ConfigSelection(default = "no", choices = [("no", _("no")), ("yes", _("yes")), ("yes_hold_feeds", _("yes (keep feeds)"))])
505                         self.scan_networkScan = ConfigYesNo(default = False)
506
507                         nim_list = []
508                         # collect all nims which are *not* set to "nothing"
509                         for n in nimmanager.nim_slots:
510                                 if n.config_mode != "nothing":
511                                         nim_list.append((str(n.slot), n.friendly_full_description))
512
513                         self.scan_nims = ConfigSelection(choices = nim_list)
514
515                         # status
516                         self.scan_snr = ConfigSlider()
517                         self.scan_snr.enabled = False
518                         self.scan_agc = ConfigSlider()
519                         self.scan_agc.enabled = False
520                         self.scan_ber = ConfigSlider()
521                         self.scan_ber.enabled = False
522
523                         # sat
524                         self.scan_sat.system = ConfigSelection(default = defaultSat["system"], choices = [("dvb-s", _("DVB-S")), ("dvb-s2", _("DVB-S2"))])
525                         self.scan_sat.frequency = ConfigInteger(default = defaultSat["frequency"], limits = (1, 99999))
526                         self.scan_sat.inversion = ConfigSelection(default = defaultSat["inversion"], choices = [("off", _("off")), ("on", _("on")), ("auto", _("Auto"))])
527                         self.scan_sat.symbolrate = ConfigInteger(default = defaultSat["symbolrate"], limits = (1, 99999))
528                         self.scan_sat.polarization = ConfigSelection(default = defaultSat["polarization"], choices = [("horizontal", _("horizontal")), ("vertical", _("vertical")),  ("circular_left", _("circular left")), ("circular_right", _("circular right"))])
529                         self.scan_sat.fec = ConfigSelection(default = defaultSat["fec"], choices = [("auto", _("Auto")), ("1_2", "1/2"), ("2_3", "2/3"), ("3_4", "3/4"), ("5_6", "5/6"), ("7_8", "7/8"), ("none", _("None"))])
530                         self.scan_sat.fec_s2 = ConfigSelection(default = defaultSat["fec_s2"], choices = [("1_2", "1/2"), ("2_3", "2/3"), ("3_4", "3/4"), ("3_5", "3/5"), ("4_5", "4/5"), ("5_6", "5/6"), ("7_8", "7/8"), ("8_9", "8/9"), ("9_10", "9/10")])
531                         self.scan_sat.modulation = ConfigSelection(default = defaultSat["modulation"], choices = [("qpsk", "QPSK"), ("8psk", "8PSK")])
532
533                         # cable
534                         self.scan_cab.frequency = ConfigInteger(default = defaultCab["frequency"], limits = (50, 999))
535                         self.scan_cab.inversion = ConfigSelection(default = defaultCab["inversion"], choices = [("off", _("off")), ("on", _("on")), ("auto", _("Auto"))])
536                         self.scan_cab.modulation = ConfigSelection(default = defaultCab["modulation"], choices = [("16qam", "16-QAM"), ("32qam", "32-QAM"), ("64qam", "64-QAM"), ("128qam", "128-QAM"), ("256qam", "256-QAM")])
537                         self.scan_cab.fec = ConfigSelection(default = defaultCab["fec"], choices = [("auto", _("Auto")), ("1_2", "1/2"), ("2_3", "2/3"), ("3_4", "3/4"), ("5_6", "5/6"), ("7_8", "7/8"), ("8_9", "8/9"), ("none", _("None"))])
538                         self.scan_cab.symbolrate = ConfigInteger(default = defaultCab["symbolrate"], limits = (1, 99999))
539
540                         # terrestial
541                         self.scan_ter.frequency = ConfigInteger(default = 466000, limits = (50000, 999000))
542                         self.scan_ter.inversion = ConfigSelection(default = "auto", choices = [("off", _("off")), ("on", _("on")), ("auto", _("Auto"))])
543                         # WORKAROUND: we can't use BW-auto
544                         self.scan_ter.bandwidth = ConfigSelection(default = "8MHz", choices = [("8MHz", "8MHz"), ("7MHz", "7MHz"), ("6MHz", "6MHz")])
545                         #, ("auto", _("Auto"))))
546                         self.scan_ter.fechigh = ConfigSelection(default = "auto", choices = [("1_2", "1/2"), ("2_3", "2/3"), ("3_4", "3/4"), ("5_6", "5/6"), ("7_8", "7/8"), ("auto", _("Auto"))])
547                         self.scan_ter.feclow = ConfigSelection(default = "auto", choices = [("1_2", "1/2"), ("2_3", "2/3"), ("3_4", "3/4"), ("5_6", "5/6"), ("7_8", "7/8"), ("auto", _("Auto"))])
548                         self.scan_ter.modulation = ConfigSelection(default = "auto", choices = [("qpsk", "QPSK"), ("qam16", "QAM16"), ("qam64", "QAM64"), ("auto", _("Auto"))])
549                         self.scan_ter.transmission = ConfigSelection(default = "auto", choices = [("2k", "2K"), ("8k", "8K"), ("auto", _("Auto"))])
550                         self.scan_ter.guard = ConfigSelection(default = "auto", choices = [("1_32", "1/32"), ("1_16", "1/16"), ("1_8", "1/8"), ("1_4", "1/4"), ("auto", _("Auto"))])
551                         self.scan_ter.hierarchy = ConfigSelection(default = "auto", choices = [("none", _("None")), ("1", "1"), ("2", "2"), ("4", "4"), ("auto", _("Auto"))])
552
553                         self.scan_scansat = {}
554                         for sat in nimmanager.satList:
555                                 #print sat[1]
556                                 self.scan_scansat[sat[0]] = ConfigYesNo(default = False)
557
558                         self.scan_satselection = []
559                         for slot in nimmanager.nim_slots:
560                                 if slot.isCompatible("DVB-S"):
561                                         self.scan_satselection.append(getConfigSatlist(int(defaultSat["orbpos"]), self.satList[slot.slot]))
562                                 else:
563                                         self.scan_satselection.append(None)
564
565                         return True
566
567         def keyLeft(self):
568                 ConfigListScreen.keyLeft(self)
569                 self.newConfig()
570
571         def keyRight(self):
572                 ConfigListScreen.keyRight(self)
573                 self.newConfig()
574
575         def updateStatus(self):
576                 print "updatestatus"
577
578         fecmap = { "auto": 0,
579                            "1_2": 1,
580                            "2_3": 2,
581                            "3_4": 3,
582                            "5_6": 4,
583                            "7_8": 5,
584                            "8_9": 6,
585                            "3_5": 7,
586                            "4_5": 8,
587                            "9_10": 9,
588                            "none": 15
589                            }
590
591         def addSatTransponder(self, tlist, frequency, symbol_rate, polarisation, fec, inversion, orbital_position, system, modulation):
592                 print "Add Sat: frequ: " + str(frequency) + " symbol: " + str(symbol_rate) + " pol: " + str(polarisation) + " fec: " + str(self.fecmap[fec]) + " inversion: " + str(inversion) + " modulation: " + str(modulation) + " system: " + str(system)
593                 print "orbpos: " + str(orbital_position)
594                 parm = eDVBFrontendParametersSatellite()
595                 if modulation == 1:
596                         parm.modulation = 2 # eDVBFrontendParametersSatellite.Modulation.8PSK
597                 else:
598                         parm.modulation = 1 # eDVBFrontendParametersSatellite.Modulation.QPSK
599                 parm.system = system
600                 parm.frequency = frequency * 1000
601                 parm.symbol_rate = symbol_rate * 1000
602                 parm.polarisation = polarisation
603                 parm.fec = self.fecmap[fec]
604                 parm.inversion = inversion
605                 parm.orbital_position = int(orbital_position)
606                 tlist.append(parm)
607
608         def addCabTransponder(self, tlist, frequency, symbol_rate, modulation, fec, inversion):
609                 print "Add Cab: frequ: " + str(frequency) + " symbol: " + str(symbol_rate) + " pol: " + str(modulation) + " fec: " + str(fec) + " inversion: " + str(inversion)
610                 parm = eDVBFrontendParametersCable()
611                 parm.frequency = frequency * 1000
612                 parm.symbol_rate = symbol_rate * 1000
613                 parm.modulation = modulation
614                 parm.fec = self.fecmap[fec]
615                 parm.inversion = inversion
616                 tlist.append(parm)
617
618         def addTerTransponder(self, tlist, *args, **kwargs):
619                 tlist.append(buildTerTransponder(*args, **kwargs))
620
621         def keyGo(self):
622                 tlist = []
623                 flags = None
624                 extFlags = True
625                 
626                 startScan = True
627                 index_to_scan = int(self.scan_nims.value)
628                 
629                 if self.scan_nims == [ ]:
630                         self.session.open(MessageBox, _("No tuner is enabled!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
631                         return
632
633                 nim = nimmanager.nim_slots[index_to_scan]
634                 print "nim", nim.slot
635                 if nim.isCompatible("DVB-S"):
636                         print "is compatible with DVB-S"
637                         if self.scan_type.value == "single_transponder":
638                                 # these lists are generated for each tuner, so this has work.
639                                 assert len(self.satList) > index_to_scan
640                                 assert len(self.scan_satselection) > index_to_scan
641                                 
642                                 nimsats = self.satList[index_to_scan]
643                                 selsatidx = self.scan_satselection[index_to_scan].index
644
645                                 # however, the satList itself could be empty. in that case, "index" is 0 (for "None").
646                                 if len(nimsats):
647                                         orbpos = nimsats[selsatidx][0]
648                                         if self.scan_sat.system.value == "dvb-s":
649                                                 fec = self.scan_sat.fec.value
650                                         else:
651                                                 fec = self.scan_sat.fec_s2.value
652                                         print "add sat transponder"
653                                         self.addSatTransponder(tlist, self.scan_sat.frequency.value,
654                                                                 self.scan_sat.symbolrate.value,
655                                                                 self.scan_sat.polarization.index,
656                                                                 fec,
657                                                                 self.scan_sat.inversion.index,
658                                                                 orbpos,
659                                                                 self.scan_sat.system.index,
660                                                                 self.scan_sat.modulation.index)
661                                 flags = self.scan_networkScan.value and eComponentScan.scanNetworkSearch or 0
662                                 extFlags = False
663                         elif self.scan_type.value == "single_satellite":
664                                 sat = self.satList[index_to_scan][self.scan_satselection[index_to_scan].index]
665                                 getInitialTransponderList(tlist, sat[0])
666                         elif self.scan_type.value == "multisat":
667                                 SatList = nimmanager.getSatListForNim(index_to_scan)
668                                 for x in self.multiscanlist:
669                                         if x[1].value:
670                                                 print "   " + str(x[0])
671                                                 getInitialTransponderList(tlist, x[0])
672
673                 elif nim.isCompatible("DVB-C"):
674                         if self.scan_typecable.value == "single_transponder":
675                                 fec = self.scan_cab.fec.value
676                                 self.addCabTransponder(tlist, self.scan_cab.frequency.value,
677                                                                                           self.scan_cab.symbolrate.value,
678                                                                                           self.scan_cab.modulation.index + 1,
679                                                                                           fec,
680                                                                                           self.scan_cab.inversion.index)
681                                 flags = self.scan_networkScan.value and eComponentScan.scanNetworkSearch or 0
682                                 extFlags = False
683                         elif self.scan_typecable.value == "complete":
684                                 if config.Nims[index_to_scan].cable.scan_type.value == "provider":
685                                         getInitialCableTransponderList(tlist, index_to_scan)
686                                 else:
687                                         startScan = False
688
689                 elif nim.isCompatible("DVB-T"):
690                         if self.scan_typeterrestrial.value == "single_transponder":
691                                 self.addTerTransponder(tlist,
692                                                 self.scan_ter.frequency.value * 1000,
693                                                 inversion = self.scan_ter.inversion.index,
694                                                 bandwidth = self.scan_ter.bandwidth.index,
695                                                 fechigh = self.scan_ter.fechigh.index,
696                                                 feclow = self.scan_ter.feclow.index,
697                                                 modulation = self.scan_ter.modulation.index,
698                                                 transmission = self.scan_ter.transmission.index,
699                                                 guard = self.scan_ter.guard.index,
700                                                 hierarchy = self.scan_ter.hierarchy.index)
701                                 flags = self.scan_networkScan.value and eComponentScan.scanNetworkSearch or 0
702                                 extFlags = False
703                         elif self.scan_typeterrestrial.value == "complete":
704                                 getInitialTerrestrialTransponderList(tlist, nimmanager.getTerrestrialDescription(index_to_scan))
705
706                 if flags is None:
707                         flags = eComponentScan.scanNetworkSearch
708
709                 if extFlags:
710                         tmp = self.scan_clearallservices.value
711                         if tmp == "yes":
712                                 flags |= eComponentScan.scanRemoveServices
713                         elif tmp == "yes_hold_feeds":
714                                 flags |= eComponentScan.scanRemoveServices
715                                 flags |= eComponentScan.scanDontRemoveFeeds
716
717                 for x in self["config"].list:
718                         x[1].save()
719
720                 if startScan:
721                         self.startScan(tlist, flags, index_to_scan)
722                 else:
723                         self.flags = flags
724                         self.feid = index_to_scan
725                         self.tlist = []
726                         self.startCableTransponderSearch(self.feid)
727
728         def setCableTransponderSearchResult(self, tlist):
729                 self.tlist = tlist
730
731         def cableTransponderSearchFinished(self):
732                 self.startScan(self.tlist, self.flags, self.feid)
733
734         def startScan(self, tlist, flags, feid):
735                 if len(tlist):
736                         # flags |= eComponentScan.scanSearchBAT
737                         self.session.open(ServiceScan, [{"transponders": tlist, "feid": feid, "flags": flags}])
738                 else:
739                         self.session.open(MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
740
741         def keyCancel(self):
742                 for x in self["config"].list:
743                         x[1].cancel()
744                 self.close()
745
746 class ScanSimple(ConfigListScreen, Screen, CableTransponderSearchSupport):
747         def getNetworksForNim(self, nim):
748                 if nim.isCompatible("DVB-S"):
749                         networks = nimmanager.getSatListForNim(nim.slot)
750 # the original code took "loopthrough" etc. into account. Do we need this?
751 #                       if nimmanager.getNimConfigMode(1) in ["loopthrough", "satposdepends", "equal", "nothing"]:
752 #                               return False
753 #                       sec = eDVBSatelliteEquipmentControl.getInstance()
754 #                       if sec is not None:
755 #                               exclusive_satellites = sec.get_exclusive_satellites(0,1)
756 #                               if len(exclusive_satellites) == 2:
757 #                                       return False
758 #                               idx = exclusive_satellites[0]+1
759 #                               exclusive_nim_sats = exclusive_satellites[idx+1:idx+1+exclusive_satellites[idx]]
760 #                               if len(exclusive_nim_sats):
761 #                                       return True
762                 elif not nim.empty:
763                         networks = [ nim.type ] # "DVB-C" or "DVB-T". TODO: seperate networks for different C/T tuners, if we want to support that.
764                 else:
765                         # empty tuners provide no networks.
766                         networks = [ ]
767                 return networks
768
769         def __init__(self, session):
770                 Screen.__init__(self, session)
771
772                 self["actions"] = ActionMap(["SetupActions"],
773                 {
774                         "ok": self.keyGo,
775                         "cancel": self.keyCancel,
776                 }, -2)
777
778                 self.list = []
779                 tlist = []
780
781                 known_networks = [ ]
782                 nims_to_scan = [ ]
783
784                 for nim in nimmanager.nim_slots:
785                         # collect networks provided by this tuner
786
787                         need_scan = False
788                         networks = self.getNetworksForNim(nim)
789                         
790                         print "nim %d provides" % nim.slot, networks
791                         print "known:", known_networks
792
793                         # we only need to scan on the first tuner which provides a network.
794                         # this gives the first tuner for each network priority for scanning.
795                         for x in networks:
796                                 if x not in known_networks:
797                                         need_scan = True
798                                         print x, "not in ", known_networks
799                                         known_networks.append(x)
800
801                         if need_scan:
802                                 nims_to_scan.append(nim)
803
804                 # we save the config elements to use them on keyGo
805                 self.nim_enable = [ ]
806
807                 if len(nims_to_scan):
808                         self.scan_clearallservices = ConfigSelection(default = "yes", choices = [("no", _("no")), ("yes", _("yes")), ("yes_hold_feeds", _("yes (keep feeds)"))])
809                         self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
810
811                         for nim in nims_to_scan:
812                                 nimconfig = ConfigYesNo(default = True)
813                                 nimconfig.nim_index = nim.slot
814                                 self.nim_enable.append(nimconfig)
815                                 self.list.append(getConfigListEntry(_("Scan ") + nim.slot_name + " (" + nim.friendly_type + ")", nimconfig))
816
817                 ConfigListScreen.__init__(self, self.list)
818                 self["header"] = Label(_("Automatic Scan"))
819                 self["footer"] = Label(_("Press OK to scan"))
820
821         def run(self):
822                 self.keyGo()
823
824         def keyGo(self):
825                 self.scanList = []
826                 self.known_networks = set()
827                 self.nim_iter=0
828                 self.buildTransponderList()
829
830         def buildTransponderList(self): # this method is called multiple times because of asynchronous stuff
831                 print "buildTransponderList"
832                 APPEND_NOW = 0
833                 SEARCH_CABLE_TRANSPONDERS = 1
834                 action = APPEND_NOW
835
836                 n = self.nim_iter < len(self.nim_enable) and self.nim_enable[self.nim_iter] or None
837                 self.nim_iter += 1
838                 if n:
839                         if n.value: # check if nim is enabled
840                                 flags = 0
841                                 nim = nimmanager.nim_slots[n.nim_index]
842                                 networks = set(self.getNetworksForNim(nim))
843
844                                 # don't scan anything twice
845                                 networks.discard(self.known_networks)
846
847                                 tlist = [ ]
848                                 if nim.isCompatible("DVB-S"):
849                                         # get initial transponders for each satellite to be scanned
850                                         for sat in networks:
851                                                 getInitialTransponderList(tlist, sat[0])
852                                 elif nim.isCompatible("DVB-C"):
853                                         if config.Nims[nim.slot].cable.scan_type.value == "provider":
854                                                 getInitialCableTransponderList(tlist, nim.slot)
855                                         else:
856                                                 action = SEARCH_CABLE_TRANSPONDERS
857                                 elif nim.isCompatible("DVB-T"):
858                                         getInitialTerrestrialTransponderList(tlist, nimmanager.getTerrestrialDescription(nim.slot))
859                                 else:
860                                         assert False
861
862                                 flags |= eComponentScan.scanNetworkSearch #FIXMEEE.. use flags from cables / satellites / terrestrial.xml
863                                 tmp = self.scan_clearallservices.value
864                                 if tmp == "yes":
865                                         flags |= eComponentScan.scanRemoveServices
866                                 elif tmp == "yes_hold_feeds":
867                                         flags |= eComponentScan.scanRemoveServices
868                                         flags |= eComponentScan.scanDontRemoveFeeds
869
870                                 if action == APPEND_NOW:
871                                         self.scanList.append({"transponders": tlist, "feid": nim.slot, "flags": flags})
872                                 elif action == SEARCH_CABLE_TRANSPONDERS:
873                                         self.flags = flags
874                                         self.feid = nim.slot
875                                         self.startCableTransponderSearch(nim.slot)
876                                         return
877                                 else:
878                                         assert False
879
880                         self.buildTransponderList() # recursive call of this function !!!
881                         return
882                 # when we are here, then the recursion is finished and all enabled nims are checked
883                 # so we now start the real transponder scan
884                 self.startScan(self.scanList)
885
886         def startScan(self, scanList):
887                 if len(scanList):
888                         self.session.open(ServiceScan, scanList = scanList)
889                 else:
890                         self.session.open(MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
891
892         def setCableTransponderSearchResult(self, tlist):
893                 self.scanList.append({"transponders": tlist, "feid": self.feid, "flags": self.flags})
894
895         def cableTransponderSearchFinished(self):
896                 self.buildTransponderList()
897
898         def keyCancel(self):
899                 self.close()
900
901         def Satexists(self, tlist, pos):
902                 for x in tlist:
903                         if x == pos:
904                                 return 1
905                 return 0
906