Solve ScanSetup Issues with DVB-T2
[openblackhole/openblackhole-enigma2.git] / lib / python / Screens / ScanSetup.py
1 from Screen import Screen
2 from ServiceScan import ServiceScan
3 from Components.config import config, ConfigSubsection, ConfigSelection, \
4         ConfigYesNo, ConfigInteger, getConfigListEntry, ConfigSlider, ConfigEnableDisable
5 from Components.ActionMap import NumberActionMap, ActionMap
6 from Components.Sources.StaticText import StaticText
7 from Components.SystemInfo import SystemInfo
8 from Components.ConfigList import ConfigListScreen
9 from Components.NimManager import nimmanager, getConfigSatlist
10 from Components.Label import Label
11 from Tools.HardwareInfo import HardwareInfo
12 from Tools.Transponder import getChannelNumber, supportedChannels, channel2frequency
13 from Screens.InfoBar import InfoBar
14 from Screens.MessageBox import MessageBox
15 from enigma import eTimer, eDVBFrontendParametersSatellite, eComponentScan, \
16         eDVBSatelliteEquipmentControl, eDVBFrontendParametersTerrestrial, \
17         eDVBFrontendParametersCable, eConsoleAppContainer, eDVBResourceManager
18
19 def buildTerTransponder(frequency,
20                 inversion=2, bandwidth = 7000000, fechigh = 6, feclow = 6,
21                 modulation = 2, transmission = 2, guard = 4,
22                 hierarchy = 4, system = 0, plpid = 0):
23 #       print "freq", frequency, "inv", inversion, "bw", bandwidth, "fech", fechigh, "fecl", feclow, "mod", modulation, "tm", transmission, "guard", guard, "hierarchy", hierarchy
24         parm = eDVBFrontendParametersTerrestrial()
25         parm.frequency = frequency
26         parm.inversion = inversion
27         parm.bandwidth = bandwidth
28         parm.code_rate_HP = fechigh
29         parm.code_rate_LP = feclow
30         parm.modulation = modulation
31         parm.transmission_mode = transmission
32         parm.guard_interval = guard
33         parm.hierarchy = hierarchy
34         parm.system = system
35         parm.plpid = plpid
36         return parm
37
38 def getInitialTransponderList(tlist, pos):
39         list = nimmanager.getTransponders(pos)
40         for x in list:
41                 if x[0] == 0:           #SAT
42                         parm = eDVBFrontendParametersSatellite()
43                         parm.frequency = x[1]
44                         parm.symbol_rate = x[2]
45                         parm.polarisation = x[3]
46                         parm.fec = x[4]
47                         parm.inversion = x[7]
48                         parm.orbital_position = pos
49                         parm.system = x[5]
50                         parm.modulation = x[6]
51                         parm.rolloff = x[8]
52                         parm.pilot = x[9]
53                         tlist.append(parm)
54
55 def getInitialCableTransponderList(tlist, nim):
56         list = nimmanager.getTranspondersCable(nim)
57         for x in list:
58                 if x[0] == 1: #CABLE
59                         parm = eDVBFrontendParametersCable()
60                         parm.frequency = x[1]
61                         parm.symbol_rate = x[2]
62                         parm.modulation = x[3]
63                         parm.fec_inner = x[4]
64                         parm.inversion = x[5]
65                         parm.system = x[6]
66                         tlist.append(parm)
67
68 def getInitialTerrestrialTransponderList(tlist, region, tsystem = eDVBFrontendParametersTerrestrial.System_DVB_T_T2, skip_t2 = False):
69         list = nimmanager.getTranspondersTerrestrial(region)
70
71         #self.transponders[self.parsedTer].append((2,freq,bw,const,crh,crl,guard,transm,hierarchy,inv))
72
73         #def buildTerTransponder(frequency, inversion = 2, bandwidth = 3, fechigh = 6, feclow = 6,
74                                 #modulation = 2, transmission = 2, guard = 4, hierarchy = 4):
75
76         for x in list:
77                 if x[0] == 2: #TERRESTRIAL
78                         if skip_t2 and x[10] == eDVBFrontendParametersTerrestrial.System_DVB_T2:
79                                 # Should be searching on TerrestrialTransponderSearchSupport.
80                                 continue
81                         if tsystem == eDVBFrontendParametersTerrestrial.System_DVB_T_T2:
82                                 parm = buildTerTransponder(x[1], x[9], x[2], x[4], x[5], x[3], x[7], x[6], x[8], x[10], x[11])
83                         elif x[10] == eDVBFrontendParametersTerrestrial.System_DVB_T_T2 or x[10] == tsystem:
84                                 parm = buildTerTransponder(x[1], x[9], x[2], x[4], x[5], x[3], x[7], x[6], x[8], tsystem, x[11])
85                         else:
86                                 continue
87                         tlist.append(parm)
88
89 cable_bands = {
90         "DVBC_BAND_EU_VHF_I" : 1 << 0,
91         "DVBC_BAND_EU_MID" : 1 << 1,
92         "DVBC_BAND_EU_VHF_III" : 1 << 2,
93         "DVBC_BAND_EU_SUPER" : 1 << 3,
94         "DVBC_BAND_EU_HYPER" : 1 << 4,
95         "DVBC_BAND_EU_UHF_IV" : 1 << 5,
96         "DVBC_BAND_EU_UHF_V" : 1 << 6,
97         "DVBC_BAND_US_LO" : 1 << 7,
98         "DVBC_BAND_US_MID" : 1 << 8,
99         "DVBC_BAND_US_HI" : 1 << 9,
100         "DVBC_BAND_US_SUPER" : 1 << 10,
101         "DVBC_BAND_US_HYPER" : 1 << 11,
102 }
103
104 cable_autoscan_nimtype = {
105 'SSH108' : 'ssh108',
106 'TT3L10' : 'tt3l10',
107 'TURBO' : 'vuplus_turbo_c'
108 }
109
110 terrestrial_autoscan_nimtype = {
111 'SSH108' : 'ssh108_t2_scan',
112 'TT3L10' : 'tt3l10_t2_scan',
113 'TURBO' : 'vuplus_turbo_t'
114 }
115
116 def GetDeviceId(filter, nim_idx):
117         tuners={}
118         device_id = 0
119         socket_id = 0
120         for nim in nimmanager.nim_slots:
121                 name_token = nim.description.split(' ')
122                 name = name_token[-1][4:-1]
123                 if name == filter:
124                         if socket_id == nim_idx:
125                                 break
126                         if device_id:
127                                 device_id = 0
128                         else:
129                                 device_id = 1
130                 socket_id += 1
131         return device_id
132
133 class CableTransponderSearchSupport:
134
135         def __init__(self):
136                 pass
137
138         def tryGetRawFrontend(self, feid):
139                 res_mgr = eDVBResourceManager.getInstance()
140                 if res_mgr:
141                         raw_channel = res_mgr.allocateRawChannel(self.feid)
142                         if raw_channel:
143                                 frontend = raw_channel.getFrontend()
144                                 if frontend:
145                                         frontend.closeFrontend() # immediate close...
146                                         del frontend
147                                         del raw_channel
148                                         return True
149                 return False
150
151         def cableTransponderSearchSessionClosed(self, *val):
152                 print "cableTransponderSearchSessionClosed, val", val
153                 self.cable_search_container.appClosed.remove(self.cableTransponderSearchClosed)
154                 self.cable_search_container.dataAvail.remove(self.getCableTransponderData)
155                 if val and len(val):
156                         if val[0]:
157                                 self.setCableTransponderSearchResult(self.__tlist)
158                         else:
159                                 self.cable_search_container.sendCtrlC()
160                                 self.setCableTransponderSearchResult(None)
161                 self.cable_search_container = None
162                 self.cable_search_session = None
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                 #prepend any remaining data from the previous call
172                 str = self.remainingdata + str
173                 #split in lines
174                 lines = str.split('\n')
175                 #'str' should end with '\n', so when splitting, the last line should be empty. If this is not the case, we received an incomplete line
176                 if len(lines[-1]):
177                         #remember this data for next time
178                         self.remainingdata = lines[-1]
179                         lines = lines[0:-1]
180                 else:
181                         self.remainingdata = ""
182
183                 for line in lines:
184                         data = line.split()
185                         if len(data):
186                                 if data[0] == 'OK' and data[4] != 'NOT_IMPLEMENTED':
187                                         print str
188                                         parm = eDVBFrontendParametersCable()
189                                         qam = { "QAM16" : parm.Modulation_QAM16,
190                                                 "QAM32" : parm.Modulation_QAM32,
191                                                 "QAM64" : parm.Modulation_QAM64,
192                                                 "QAM128" : parm.Modulation_QAM128,
193                                                 "QAM256" : parm.Modulation_QAM256 }
194                                         inv = { "INVERSION_OFF" : parm.Inversion_Off,
195                                                 "INVERSION_ON" : parm.Inversion_On,
196                                                 "INVERSION_AUTO" : parm.Inversion_Unknown }
197                                         fec = { "FEC_AUTO" : parm.FEC_Auto,
198                                                 "FEC_1_2" : parm.FEC_1_2,
199                                                 "FEC_2_3" : parm.FEC_2_3,
200                                                 "FEC_3_4" : parm.FEC_3_4,
201                                                 "FEC_5_6" : parm.FEC_5_6,
202                                                 "FEC_7_8" : parm.FEC_7_8,
203                                                 "FEC_8_9" : parm.FEC_8_9,
204                                                 "FEC_3_5" : parm.FEC_3_5,
205                                                 "FEC_4_5" : parm.FEC_4_5,
206                                                 "FEC_9_10" : parm.FEC_9_10,
207                                                 "FEC_NONE" : parm.FEC_None }
208                                         parm.frequency = int(data[1])
209                                         parm.symbol_rate = int(data[2])
210                                         parm.fec_inner = fec[data[3]]
211                                         parm.modulation = qam[data[4]]
212                                         parm.inversion = inv[data[5]]
213                                         self.__tlist.append(parm)
214                                 tmpstr = _("Try to find used transponders in cable network.. please wait...")
215                                 tmpstr += "\n\n"
216                                 tmpstr += data[1].isdigit() and "%s MHz " % (int(data[1]) / 1000.) or data[1]
217                                 tmpstr += data[0]
218                                 self.cable_search_session["text"].setText(tmpstr)
219
220         def startCableTransponderSearch(self, nim_idx):
221                 def GetCommand(nim_idx):
222                         global cable_autoscan_nimtype
223                         try:
224                                 nim_name = nimmanager.getNimName(nim_idx)
225                                 if nim_name is not None and nim_name != "":
226                                         device_id = ""
227                                         nim_name = nim_name.split(' ')[-1][4:-1]
228                                         if nim_name == 'TT3L10':
229                                                 try:
230                                                         device_id = GetDeviceId('TT3L10', nim_idx)
231                                                         device_id = "--device=%s" % (device_id)
232                                                 except Exception, err:
233                                                         print "GetCommand ->", err
234                                                         device_id = "--device=0"
235                                         command = "%s %s" % (cable_autoscan_nimtype[nim_name], device_id)
236                                         return command
237                         except Exception, err:
238                                 print "GetCommand ->", err
239                         return "tda1002x"
240
241                 if not self.tryGetRawFrontend(nim_idx):
242                         self.session.nav.stopService()
243                         if not self.tryGetRawFrontend(nim_idx):
244                                 if self.session.pipshown:
245                                         self.session.infobar.showPiP()
246                                 if not self.tryGetRawFrontend(nim_idx):
247                                         self.cableTransponderSearchFinished()
248                                         return
249                 self.__tlist = []
250                 self.remainingdata = ""
251                 self.cable_search_container = eConsoleAppContainer()
252                 self.cable_search_container.appClosed.append(self.cableTransponderSearchClosed)
253                 self.cable_search_container.dataAvail.append(self.getCableTransponderData)
254                 cableConfig = config.Nims[nim_idx].cable
255                 tunername = nimmanager.getNimName(nim_idx)
256                 try:
257                         bus = nimmanager.getI2CDevice(nim_idx)
258                         if bus is None:
259                                 print "ERROR: could not get I2C device for nim", nim_idx, "for cable transponder search"
260                                 bus = 2
261                 except:
262                         # older API
263                         if nim_idx < 2:
264                                 if HardwareInfo().get_device_name() == "dm500hd":
265                                         bus = 2
266                                 else:
267                                         bus = nim_idx
268                         else:
269                                 if nim_idx == 2:
270                                         bus = 2 # DM8000 first nim is /dev/i2c/2
271                                 else:
272                                         bus = 4 # DM8000 second num is /dev/i2c/4
273
274                 bin_name = None
275                 if tunername == "CXD1981":
276                         bin_name = "CXD1981"
277                         cmd = "cxd1978 --init --scan --verbose --wakeup --inv 2 --bus %d" % bus
278                 elif tunername.startswith("Sundtek"):
279                         bin_name = "mediaclient"
280                         cmd = "/opt/bin/mediaclient --blindscan %d" % nim_idx
281                 else:
282                         bin_name = GetCommand(nim_idx)
283                         cmd = "%(BIN_NAME)s --init --scan --verbose --wakeup --inv 2 --bus %(BUS)d" % {'BIN_NAME':bin_name, 'BUS':bus}
284
285                 if cableConfig.scan_type.value == "bands":
286                         cmd += " --scan-bands "
287                         bands = 0
288                         if cableConfig.scan_band_EU_VHF_I.value:
289                                 bands |= cable_bands["DVBC_BAND_EU_VHF_I"]
290                         if cableConfig.scan_band_EU_MID.value:
291                                 bands |= cable_bands["DVBC_BAND_EU_MID"]
292                         if cableConfig.scan_band_EU_VHF_III.value:
293                                 bands |= cable_bands["DVBC_BAND_EU_VHF_III"]
294                         if cableConfig.scan_band_EU_UHF_IV.value:
295                                 bands |= cable_bands["DVBC_BAND_EU_UHF_IV"]
296                         if cableConfig.scan_band_EU_UHF_V.value:
297                                 bands |= cable_bands["DVBC_BAND_EU_UHF_V"]
298                         if cableConfig.scan_band_EU_SUPER.value:
299                                 bands |= cable_bands["DVBC_BAND_EU_SUPER"]
300                         if cableConfig.scan_band_EU_HYPER.value:
301                                 bands |= cable_bands["DVBC_BAND_EU_HYPER"]
302                         if cableConfig.scan_band_US_LOW.value:
303                                 bands |= cable_bands["DVBC_BAND_US_LO"]
304                         if cableConfig.scan_band_US_MID.value:
305                                 bands |= cable_bands["DVBC_BAND_US_MID"]
306                         if cableConfig.scan_band_US_HIGH.value:
307                                 bands |= cable_bands["DVBC_BAND_US_HI"]
308                         if cableConfig.scan_band_US_SUPER.value:
309                                 bands |= cable_bands["DVBC_BAND_US_SUPER"]
310                         if cableConfig.scan_band_US_HYPER.value:
311                                 bands |= cable_bands["DVBC_BAND_US_HYPER"]
312                         cmd += str(bands)
313                 else:
314                         cmd += " --scan-stepsize "
315                         cmd += str(cableConfig.scan_frequency_steps.value)
316                 if cableConfig.scan_mod_qam16.value:
317                         cmd += " --mod 16"
318                 if cableConfig.scan_mod_qam32.value:
319                         cmd += " --mod 32"
320                 if cableConfig.scan_mod_qam64.value:
321                         cmd += " --mod 64"
322                 if cableConfig.scan_mod_qam128.value:
323                         cmd += " --mod 128"
324                 if cableConfig.scan_mod_qam256.value:
325                         cmd += " --mod 256"
326                 if cableConfig.scan_sr_6900.value:
327                         cmd += " --sr 6900000"
328                 if cableConfig.scan_sr_6875.value:
329                         cmd += " --sr 6875000"
330                 if cableConfig.scan_sr_ext1.value > 450:
331                         cmd += " --sr "
332                         cmd += str(cableConfig.scan_sr_ext1.value)
333                         cmd += "000"
334                 if cableConfig.scan_sr_ext2.value > 450:
335                         cmd += " --sr "
336                         cmd += str(cableConfig.scan_sr_ext2.value)
337                         cmd += "000"
338                 print bin_name, " CMD is", cmd
339
340                 self.cable_search_container.execute(cmd)
341                 tmpstr = _("Try to find used transponders in cable network.. please wait...")
342                 tmpstr += "\n\n..."
343                 self.cable_search_session = self.session.openWithCallback(self.cableTransponderSearchSessionClosed, MessageBox, tmpstr, MessageBox.TYPE_INFO)
344
345 class TerrestrialTransponderSearchSupport:
346
347         def terrestrialTransponderSearchSessionClosed(self, *val):
348                 print "TerrestrialTransponderSearchSessionClosed, val", val
349                 self.terrestrial_search_container.appClosed.remove(self.terrestrialTransponderSearchClosed)
350                 self.terrestrial_search_container.dataAvail.remove(self.getTerrestrialTransponderData)
351                 if val and len(val):
352                         if val[0]:
353                                 self.setTerrestrialTransponderSearchResult(self.__tlist)
354                         else:
355                                 self.terrestrial_search_container.sendCtrlC()
356                                 self.setTerrestrialTransponderSearchResult(None)
357                 self.terrestrial_search_container = None
358                 self.terrestrial_search_session = None
359                 self.__tlist = None
360                 self.terrestrialTransponderSearchFinished()
361
362         def terrestrialTransponderSearchClosed(self, retval):
363                 if self.terrestrial_tunerName.startswith("Sundtek"):
364                         self.terrestrial_search_session.close(True)
365                 else:
366                         self.setTerrestrialTransponderData()
367                         opt = self.terrestrialTransponderGetOpt()
368                         if opt is None:
369                                 print "terrestrialTransponderSearch finished", retval
370                                 self.terrestrial_search_session.close(True)
371                         else:
372                                 (freq, bandWidth) = opt
373                                 self.terrestrialTransponderSearch(freq, bandWidth)
374
375         def getTerrestrialTransponderData(self, str):
376                 print str
377                 if self.terrestrial_tunerName.startswith("Sundtek"):
378                         str = self.remaining_data + str
379                         lines = str.split('\n')
380                         if len(lines[-1]):
381                                 self.remaining_data = lines[-1]
382                                 lines = lines[0:-1]
383                         else:
384                                 self.remaining_data = ""
385                         for line in lines:
386                                 data = line.split()
387                                 if len(data):
388                                         if 'MODE' in data[0]:
389                                                 parm = eDVBFrontendParametersTerrestrial()
390                                                 parm.frequency = int(data[3])
391                                                 parm.bandwidth = self.terrestrialTransponderconvBandwidth_P(int(data[5]))
392                                                 parm.inversion = parm.Inversion_Unknown
393                                                 parm.code_rate_HP = parm.FEC_Auto
394                                                 parm.code_rate_LP = parm.FEC_Auto
395                                                 parm.modulation = parm.Modulation_Auto
396                                                 parm.transmission_mode = parm.TransmissionMode_Auto
397                                                 parm.guard_interval = parm.GuardInterval_Auto
398                                                 parm.hierarchy = parm.Hierarchy_Auto
399                                                 parm.system = 'DVB-T2' in data[1] and parm.System_DVB_T_T2 or parm.System_DVB_T
400                                                 parm.plpid = 0
401                                                 self.__tlist.append(parm)
402                                         tmpstr = _("Try to find used transponders in terrestrial network... please wait...")
403                                         tmpstr += "\n\n"
404                                         if 'MODE' in line:
405                                                 tmpstr += data[3]
406                                                 tmpstr += " kHz "
407                                         else:
408                                                 tmpstr += _(line)
409                         self.terrestrial_search_session["text"].setText(tmpstr)
410                 else:
411                         self.terrestrial_search_data += str
412
413         def setTerrestrialTransponderData(self):
414                 data = self.terrestrial_search_data.split()
415                 if len(data):
416 #                       print "[setTerrestrialTransponderData] data : ", data
417                         if data[0] == 'OK':
418                                 # DVB-T : OK frequency bandwidth delivery system -1
419                                 # DVB-T2 : OK frequency bandwidth delivery system number_of_plp plp_id0:plp_type0
420                                 if data[3] == 1: # DVB-T
421                                         parm = eDVBFrontendParametersTerrestrial()
422                                         parm.frequency = int(data[1])
423                                         parm.bandwidth = int(data[2])
424                                         parm.inversion = parm.Inversion_Unknown
425                                         parm.code_rate_HP = parm.FEC_Auto
426                                         parm.code_rate_LP = parm.FEC_Auto
427                                         parm.modulation = parm.Modulation_Auto
428                                         parm.transmission_mode = parm.TransmissionMode_Auto
429                                         parm.guard_interval = parm.GuardInterval_Auto
430                                         parm.hierarchy = parm.Hierarchy_Auto
431                                         parm.system = parm.System_DVB_T
432                                         parm.plpid = 0
433                                         self.__tlist.append(parm)
434                                 else:
435                                         plp_list = data[5:]
436                                         plp_num = int(data[4])
437                                         if len(plp_list) > plp_num:
438                                                 plp_list = plp_list[:plp_num]
439                                         for plp in plp_list:
440                                                 (plp_id, plp_type) = plp.split(':')
441                                                 if plp_type == '0': # common PLP:
442                                                         continue
443                                                 parm = eDVBFrontendParametersTerrestrial()
444                                                 parm.frequency = int(data[1])
445                                                 parm.bandwidth = self.terrestrialTransponderconvBandwidth_P(int(data[2]))
446                                                 parm.inversion = parm.Inversion_Unknown
447                                                 parm.code_rate_HP = parm.FEC_Auto
448                                                 parm.code_rate_LP = parm.FEC_Auto
449                                                 parm.modulation = parm.Modulation_Auto
450                                                 parm.transmission_mode = parm.TransmissionMode_Auto
451                                                 parm.guard_interval = parm.GuardInterval_Auto
452                                                 parm.hierarchy = parm.Hierarchy_Auto
453                                                 parm.system = parm.System_DVB_T2
454                                                 parm.plpid = int(plp_id)
455                                                 self.__tlist.append(parm)
456                                 tmpstr = _("Try to find used transponders in terrestrial network... please wait...")
457                                 tmpstr += "\n\n"
458                                 tmpstr += data[1][:-3]
459                                 tmpstr += " kHz "
460                                 tmpstr += data[0]
461                                 self.terrestrial_search_session["text"].setText(tmpstr)
462
463         def terrestrialTransponderInitSearchList(self, searchList, region):
464                 tpList = nimmanager.getTranspondersTerrestrial(region)
465                 for x in tpList:
466                         if x[0] == 2: #TERRESTRIAL
467                                 freq = x[1] # frequency
468                                 bandWidth = self.terrestrialTransponderConvBandwidth_I(x[2]) # bandWidth
469                                 parm = (freq, bandWidth)
470                                 searchList.append(parm)
471
472         def terrestrialTransponderConvBandwidth_I(self, _bandWidth):
473                 bandWidth = {
474                         eDVBFrontendParametersTerrestrial.Bandwidth_8MHz : 8000000,
475                         eDVBFrontendParametersTerrestrial.Bandwidth_7MHz : 7000000,
476                         eDVBFrontendParametersTerrestrial.Bandwidth_6MHz : 6000000,
477                         eDVBFrontendParametersTerrestrial.Bandwidth_5MHz : 5000000,
478                         eDVBFrontendParametersTerrestrial.Bandwidth_1_712MHz : 1712000,
479                         eDVBFrontendParametersTerrestrial.Bandwidth_10MHz : 10000000,
480                 }.get(_bandWidth, 8000000)
481                 return bandWidth
482
483         def terrestrialTransponderconvBandwidth_P(self, _bandWidth):
484                 bandWidth = {
485                         8000000 : eDVBFrontendParametersTerrestrial.Bandwidth_8MHz,
486                         7000000 : eDVBFrontendParametersTerrestrial.Bandwidth_7MHz,
487                         6000000 : eDVBFrontendParametersTerrestrial.Bandwidth_6MHz,
488                         5000000 : eDVBFrontendParametersTerrestrial.Bandwidth_5MHz,
489                         1712000 : eDVBFrontendParametersTerrestrial.Bandwidth_1_712MHz,
490                         10000000 : eDVBFrontendParametersTerrestrial.Bandwidth_10MHz,
491                 }.get(_bandWidth, eDVBFrontendParametersTerrestrial.Bandwidth_8MHz)
492                 return bandWidth
493
494         def terrestrialTransponderGetOpt(self):
495                 if len(self.terrestrial_search_list) > 0:
496                         return self.terrestrial_search_list.pop(0)
497                 else:
498                         return None
499
500         def terrestrialTransponderGetCmd(self, nim_idx):
501                 global terrestrial_autoscan_nimtype
502                 try:
503                         if self.terrestrial_tunerName is not None and self.terrestrial_tunerName != "":
504                                 device_id = ""
505                                 tunerName = self.terrestrial_tunerName.split(' ')[-1][4:-1]
506                                 if tunerName == 'TT3L10':
507                                         try:
508                                                 device_id = GetDeviceId('TT3L10', nim_idx)
509                                                 device_id = "--device %s" % (device_id)
510                                         except Exception, err:
511                                                 print "terrestrialTransponderGetCmd ->", err
512                                                 device_id = "--device 0"
513                                 command = "%s %s" % (terrestrial_autoscan_nimtype[tunerName], device_id)
514                                 return command
515                 except Exception, err:
516                         print "terrestrialTransponderGetCmd ->", err
517                 return ""
518
519         def startTerrestrialTransponderSearch(self, nim_idx, region):
520                 if not self.tryGetRawFrontend(nim_idx):
521                         self.session.nav.stopService()
522                         if not self.tryGetRawFrontend(nim_idx):
523                                 if self.session.pipshown:
524                                         self.session.infobar.showPiP()
525                                 if not self.tryGetRawFrontend(nim_idx):
526                                         self.terrestrialTransponderSearchFinished()
527                                         return
528                 self.__tlist = [ ]
529                 self.remaining_data = ""
530                 self.terrestrial_search_container = eConsoleAppContainer()
531                 self.terrestrial_search_container.appClosed.append(self.terrestrialTransponderSearchClosed)
532                 self.terrestrial_search_container.dataAvail.append(self.getTerrestrialTransponderData)
533                 self.terrestrial_tunerName = nimmanager.getNimName(nim_idx)
534                 if self.terrestrial_tunerName.startswith("Sundtek"):
535                         cmd = "/opt/bin/mediaclient --blindscan /dev/dvb/adapter1/frontend0"
536                         print "SCAN CMD : ",cmd
537                         self.terrestrial_search_container.execute(cmd)
538                 else:
539                         self.terrestrial_search_binName = self.terrestrialTransponderGetCmd(nim_idx)
540                         self.terrestrial_search_bus = nimmanager.getI2CDevice(nim_idx)
541                         if self.terrestrial_search_bus is None:
542 #                       print "ERROR: could not get I2C device for nim", nim_idx, "for terrestrial transponder search"
543                                 self.terrestrial_search_bus = 2
544
545                         self.terrestrial_search_list = []
546                         self.terrestrialTransponderInitSearchList(self.terrestrial_search_list ,region)
547                         (freq, bandWidth) = self.terrestrialTransponderGetOpt()
548                         self.terrestrialTransponderSearch(freq, bandWidth)
549                 tmpstr = _("Try to find used transponders in terrestrial network... please wait...")
550                 tmpstr += "\n\n..."
551                 self.terrestrial_search_session = self.session.openWithCallback(self.terrestrialTransponderSearchSessionClosed, MessageBox, tmpstr, MessageBox.TYPE_INFO)
552
553         def terrestrialTransponderSearch(self, freq, bandWidth):
554                 self.terrestrial_search_data = ""
555                 cmd = "%s --freq %d --bw %d --bus %d --ds 2" % (self.terrestrial_search_binName, freq, bandWidth, self.terrestrial_search_bus)
556                 print "SCAN CMD : ",cmd
557                 self.terrestrial_search_container.execute(cmd)
558
559 class ScanSetup(ConfigListScreen, Screen, CableTransponderSearchSupport, TerrestrialTransponderSearchSupport):
560         def __init__(self, session):
561                 Screen.__init__(self, session)
562                 Screen.setTitle(self, _("Manual Scan"))
563
564                 self.finished_cb = None
565                 self.updateSatList()
566                 self.service = session.nav.getCurrentService()
567                 self.feinfo = None
568                 self.networkid = 0
569                 frontendData = None
570                 if self.service is not None:
571                         self.feinfo = self.service.frontendInfo()
572                         frontendData = self.feinfo and self.feinfo.getAll(True)
573
574                 self.ter_channel_input = False
575                 self.ter_tnumber = None
576                 self.createConfig(frontendData)
577
578                 del self.feinfo
579                 del self.service
580
581                 self.session.postScanService = session.nav.getCurrentlyPlayingServiceOrGroup()
582
583                 self["key_red"] = StaticText(_("Close"))
584                 self["key_green"] = StaticText(_("Scan"))
585
586                 self["actions"] = NumberActionMap(["SetupActions", "MenuActions", "ColorActions"],
587                 {
588                         "ok": self.keyGo,
589                         "save": self.keyGo,
590                         "cancel": self.keyCancel,
591                         "red": self.keyCancel,
592                         "green": self.keyGo,
593                         "menu": self.doCloseRecursive,
594                 }, -2)
595
596                 self.statusTimer = eTimer()
597                 self.statusTimer.callback.append(self.updateStatus)
598                 #self.statusTimer.start(5000, True)
599
600                 self.list = []
601                 ConfigListScreen.__init__(self, self.list)
602                 self["header"] = Label(_("Manual Scan"))
603                 if not self.scan_nims.value == "":
604                         self.createSetup()
605                         self["introduction"] = Label(_("Press OK to start the scan"))
606                 else:
607                         self["introduction"] = Label(_("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."))
608
609         def runAsync(self, finished_cb):
610                 self.finished_cb = finished_cb
611                 self.keyGo()
612
613         def updateSatList(self):
614                 self.satList = []
615                 for slot in nimmanager.nim_slots:
616                         if slot.isCompatible("DVB-S"):
617                                 self.satList.append(nimmanager.getSatListForNim(slot.slot))
618                         else:
619                                 self.satList.append(None)
620
621         def createSetup(self):
622                 self.list = []
623                 self.multiscanlist = []
624                 index_to_scan = int(self.scan_nims.value)
625                 print "ID: ", index_to_scan
626
627                 self.tunerEntry = getConfigListEntry(_("Tuner"), self.scan_nims)
628                 self.list.append(self.tunerEntry)
629
630                 if self.scan_nims == [ ]:
631                         return
632
633                 self.typeOfScanEntry = None
634                 self.typeOfInputEntry = None
635                 self.systemEntry = None
636                 self.modulationEntry = None
637                 self.preDefSatList = None
638                 self.TerrestrialTransponders = None
639                 self.TerrestrialRegionEntry = None
640                 self.TerrestrialCompleteEntry = None
641                 nim = nimmanager.nim_slots[index_to_scan]
642                 if nim.isCompatible("DVB-S"):
643                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_type)
644                         self.list.append(self.typeOfScanEntry)
645                 elif nim.isCompatible("DVB-C"):
646                         if config.Nims[index_to_scan].cable.scan_type.value != "provider": # only show predefined transponder if in provider mode
647                                 if self.scan_typecable.value == "predefined_transponder":
648                                         self.scan_typecable.value = self.cable_toggle[self.last_scan_typecable]
649                         self.last_scan_typecable = self.scan_typecable.value
650                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_typecable)
651                         self.list.append(self.typeOfScanEntry)
652                 elif nim.isCompatible("DVB-T"):
653                         self.typeOfScanEntry = getConfigListEntry(_("Type of scan"), self.scan_typeterrestrial)
654                         self.list.append(self.typeOfScanEntry)
655                         if self.scan_typeterrestrial.value == "single_transponder":
656                                 self.typeOfInputEntry = getConfigListEntry(_("Use frequency or channel"), self.scan_input_as)
657                                 if self.ter_channel_input:
658                                         self.list.append(self.typeOfInputEntry)
659                                 else:
660                                         self.scan_input_as.value = self.scan_input_as.choices[0]
661
662                 self.scan_networkScan.value = False
663                 if nim.isCompatible("DVB-S"):
664                         if self.scan_type.value == "single_transponder":
665                                 self.updateSatList()
666                                 if nim.isCompatible("DVB-S2"):
667                                         self.systemEntry = getConfigListEntry(_('System'), self.scan_sat.system)
668                                         self.list.append(self.systemEntry)
669                                 else:
670                                         # downgrade to dvb-s, in case a -s2 config was active
671                                         self.scan_sat.system.value = eDVBFrontendParametersSatellite.System_DVB_S
672                                 self.list.append(getConfigListEntry(_('Satellite'), self.scan_satselection[index_to_scan]))
673                                 self.list.append(getConfigListEntry(_('Frequency'), self.scan_sat.frequency))
674                                 self.list.append(getConfigListEntry(_('Inversion'), self.scan_sat.inversion))
675                                 self.list.append(getConfigListEntry(_('Symbol rate'), self.scan_sat.symbolrate))
676                                 self.list.append(getConfigListEntry(_('Polarization'), self.scan_sat.polarization))
677                                 if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
678                                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec))
679                                 elif self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
680                                         self.list.append(getConfigListEntry(_("FEC"), self.scan_sat.fec_s2))
681                                         self.modulationEntry = getConfigListEntry(_('Modulation'), self.scan_sat.modulation)
682                                         self.list.append(self.modulationEntry)
683                                         self.list.append(getConfigListEntry(_('Roll-off'), self.scan_sat.rolloff))
684                                         self.list.append(getConfigListEntry(_('Pilot'), self.scan_sat.pilot))
685                         elif self.scan_type.value == "predefined_transponder" and self.satList[index_to_scan]:
686                                 self.updateSatList()
687                                 self.preDefSatList = getConfigListEntry(_('Satellite'), self.scan_satselection[index_to_scan])
688                                 self.list.append(self.preDefSatList)
689                                 sat = self.satList[index_to_scan][self.scan_satselection[index_to_scan].index]
690                                 self.predefinedTranspondersList(sat[0])
691                                 self.list.append(getConfigListEntry(_('Transponder'), self.preDefTransponders))
692                         elif self.scan_type.value == "single_satellite":
693                                 self.updateSatList()
694                                 print self.scan_satselection[index_to_scan]
695                                 self.list.append(getConfigListEntry(_("Satellite"), self.scan_satselection[index_to_scan]))
696                                 self.scan_networkScan.value = True
697                         elif "multisat" in self.scan_type.value:
698                                 tlist = []
699                                 SatList = nimmanager.getSatListForNim(index_to_scan)
700                                 for x in SatList:
701                                         if self.Satexists(tlist, x[0]) == 0:
702                                                 tlist.append(x[0])
703                                                 sat = ConfigEnableDisable(default = "_yes" in self.scan_type.value and True or False)
704                                                 configEntry = getConfigListEntry(nimmanager.getSatDescription(x[0]), sat)
705                                                 self.list.append(configEntry)
706                                                 self.multiscanlist.append((x[0], sat))
707                                 self.scan_networkScan.value = True
708                 elif nim.isCompatible("DVB-C"):
709                         if self.scan_typecable.value == "single_transponder":
710                                 self.list.append(getConfigListEntry(_("Frequency"), self.scan_cab.frequency))
711                                 self.list.append(getConfigListEntry(_("Inversion"), self.scan_cab.inversion))
712                                 self.list.append(getConfigListEntry(_("Symbol rate"), self.scan_cab.symbolrate))
713                                 self.list.append(getConfigListEntry(_("Modulation"), self.scan_cab.modulation))
714                                 self.list.append(getConfigListEntry(_("FEC"), self.scan_cab.fec))
715                         elif self.scan_typecable.value == "predefined_transponder":
716                                 self.predefinedCabTranspondersList()
717                                 self.list.append(getConfigListEntry(_('Transponder'), self.CableTransponders))
718                         if config.Nims[index_to_scan].cable.scan_networkid.value:
719                                 self.networkid = config.Nims[index_to_scan].cable.scan_networkid.value
720                                 self.scan_networkScan.value = True
721                 elif nim.isCompatible("DVB-T"):
722                         if self.scan_typeterrestrial.value == "single_transponder":
723                                 if nim.isCompatible("DVB-T2"):
724                                         self.systemEntry = getConfigListEntry(_('System'), self.scan_ter.system)
725                                         self.list.append(self.systemEntry)
726                                 else:
727                                         self.scan_ter.system.value = eDVBFrontendParametersTerrestrial.System_DVB_T
728                                 if self.ter_channel_input and self.scan_input_as.value == "channel":
729                                         channel = getChannelNumber(self.scan_ter.frequency.value*1000, self.ter_tnumber)
730                                         if channel:
731                                                 self.scan_ter.channel.value = int(channel.replace("+","").replace("-",""))
732                                         self.list.append(getConfigListEntry(_("Channel"), self.scan_ter.channel))
733                                 else:
734                                         prev_val = self.scan_ter.frequency.value
735                                         self.scan_ter.frequency.value = channel2frequency(self.scan_ter.channel.value, self.ter_tnumber)/1000
736                                         if self.scan_ter.frequency.value == 474000:
737                                                 self.scan_ter.frequency.value = prev_val
738                                         self.list.append(getConfigListEntry(_("Frequency"), self.scan_ter.frequency))
739                                 self.list.append(getConfigListEntry(_("Inversion"), self.scan_ter.inversion))
740                                 self.list.append(getConfigListEntry(_("Bandwidth"), self.scan_ter.bandwidth))
741                                 self.list.append(getConfigListEntry(_("Code rate HP"), self.scan_ter.fechigh))
742                                 self.list.append(getConfigListEntry(_("Code rate LP"), self.scan_ter.feclow))
743                                 self.list.append(getConfigListEntry(_("Modulation"), self.scan_ter.modulation))
744                                 self.list.append(getConfigListEntry(_("Transmission mode"), self.scan_ter.transmission))
745                                 self.list.append(getConfigListEntry(_("Guard interval"), self.scan_ter.guard))
746                                 self.list.append(getConfigListEntry(_("Hierarchy info"), self.scan_ter.hierarchy))
747                                 if self.scan_ter.system.value == eDVBFrontendParametersTerrestrial.System_DVB_T2:
748                                         self.list.append(getConfigListEntry(_('PLP ID'), self.scan_ter.plp_id))
749                         elif self.scan_typeterrestrial.value == "predefined_transponder":
750                                 if nim.isCompatible("DVB-T2"):
751                                         self.systemEntry = getConfigListEntry(_('System'), self.scan_ter.system)
752                                         self.list.append(self.systemEntry)
753                                 else:
754                                         self.scan_ter.system.value = eDVBFrontendParametersTerrestrial.System_DVB_T
755                                 self.TerrestrialRegion = self.terrestrial_nims_regions[index_to_scan]
756                                 self.TerrestrialRegionEntry = getConfigListEntry(_('Region'), self.TerrestrialRegion)
757                                 self.list.append(self.TerrestrialRegionEntry)
758                                 self.predefinedTerrTranspondersList()
759                                 self.list.append(getConfigListEntry(_('Transponder'), self.TerrestrialTransponders))
760                         elif self.scan_typeterrestrial.value == "complete":
761                                 self.TerrestrialRegion = self.terrestrial_nims_regions[index_to_scan]
762                                 if nimmanager.getNimName(nim.slot).startswith("Sundtek"):
763                                         self.TerrestrialCompleteEntry = getConfigListEntry(_('Scan options'), self.scan_ter_complete_type)
764                                         self.list.append(self.TerrestrialCompleteEntry)
765                                 if self.TerrestrialCompleteEntry is None or self.scan_ter_complete_type.value == "extended":
766                                         if nim.isCompatible("DVB-T2"):
767                                                 self.systemEntry = getConfigListEntry(_('System'), self.scan_ter.system)
768                                                 self.list.append(self.systemEntry)
769                                         else:
770                                                 self.scan_ter.system.value = eDVBFrontendParametersTerrestrial.System_DVB_T
771                                         self.TerrestrialRegionEntry = getConfigListEntry(_('Region'), self.TerrestrialRegion)
772                                         self.list.append(self.TerrestrialRegionEntry)
773                 self.list.append(getConfigListEntry(_("Network scan"), self.scan_networkScan))
774                 self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
775                 self.list.append(getConfigListEntry(_("Only free scan"), self.scan_onlyfree))
776                 self["config"].list = self.list
777                 self["config"].l.setList(self.list)
778
779         def Satexists(self, tlist, pos):
780                 for x in tlist:
781                         if x == pos:
782                                 return 1
783                 return 0
784
785         def newConfig(self):
786                 cur = self["config"].getCurrent()
787                 print "cur is", cur
788                 if cur == self.typeOfScanEntry or \
789                         cur == self.typeOfInputEntry or \
790                         cur == self.tunerEntry or \
791                         cur == self.systemEntry or \
792                         cur == self.preDefSatList or \
793                         cur == self.TerrestrialRegionEntry or \
794                         cur == self.TerrestrialCompleteEntry or \
795                         (self.modulationEntry and self.systemEntry[1].value == eDVBFrontendParametersSatellite.System_DVB_S2 and cur == self.modulationEntry):
796                         self.createSetup()
797
798         def createConfig(self, frontendData):
799                 defaultSat = {
800                         "orbpos": 192,
801                         "system": eDVBFrontendParametersSatellite.System_DVB_S,
802                         "frequency": 11836,
803                         "inversion": eDVBFrontendParametersSatellite.Inversion_Unknown,
804                         "symbolrate": 27500,
805                         "polarization": eDVBFrontendParametersSatellite.Polarisation_Horizontal,
806                         "fec": eDVBFrontendParametersSatellite.FEC_Auto,
807                         "fec_s2": eDVBFrontendParametersSatellite.FEC_9_10,
808                         "modulation": eDVBFrontendParametersSatellite.Modulation_QPSK }
809                 defaultCab = {
810                         "frequency": 466,
811                         "inversion": eDVBFrontendParametersCable.Inversion_Unknown,
812                         "modulation": eDVBFrontendParametersCable.Modulation_QAM64,
813                         "fec": eDVBFrontendParametersCable.FEC_Auto,
814                         "symbolrate": 6900,
815                         "system": eDVBFrontendParametersCable.System_DVB_C_ANNEX_A }
816                 defaultTer = {
817                         "frequency" : 474000,
818                         "inversion" : eDVBFrontendParametersTerrestrial.Inversion_Unknown,
819                         "bandwidth" : 8000000,
820                         "fechigh" : eDVBFrontendParametersTerrestrial.FEC_Auto,
821                         "feclow" : eDVBFrontendParametersTerrestrial.FEC_Auto,
822                         "modulation" : eDVBFrontendParametersTerrestrial.Modulation_Auto,
823                         "transmission_mode" : eDVBFrontendParametersTerrestrial.TransmissionMode_Auto,
824                         "guard_interval" : eDVBFrontendParametersTerrestrial.GuardInterval_Auto,
825                         "hierarchy": eDVBFrontendParametersTerrestrial.Hierarchy_Auto,
826                         "system": eDVBFrontendParametersTerrestrial.System_DVB_T,
827                         "plp_id": 0 }
828
829                 if frontendData is not None:
830                         ttype = frontendData.get("tuner_type", "UNKNOWN")
831                         if ttype == "DVB-S":
832                                 defaultSat["system"] = frontendData.get("system", eDVBFrontendParametersSatellite.System_DVB_S)
833                                 defaultSat["frequency"] = frontendData.get("frequency", 0) / 1000
834                                 defaultSat["inversion"] = frontendData.get("inversion", eDVBFrontendParametersSatellite.Inversion_Unknown)
835                                 defaultSat["symbolrate"] = frontendData.get("symbol_rate", 0) / 1000
836                                 defaultSat["polarization"] = frontendData.get("polarization", eDVBFrontendParametersSatellite.Polarisation_Horizontal)
837                                 if defaultSat["system"] == eDVBFrontendParametersSatellite.System_DVB_S2:
838                                         defaultSat["fec_s2"] = frontendData.get("fec_inner", eDVBFrontendParametersSatellite.FEC_Auto)
839                                         defaultSat["rolloff"] = frontendData.get("rolloff", eDVBFrontendParametersSatellite.RollOff_alpha_0_35)
840                                         defaultSat["pilot"] = frontendData.get("pilot", eDVBFrontendParametersSatellite.Pilot_Unknown)
841                                 else:
842                                         defaultSat["fec"] = frontendData.get("fec_inner", eDVBFrontendParametersSatellite.FEC_Auto)
843                                 defaultSat["modulation"] = frontendData.get("modulation", eDVBFrontendParametersSatellite.Modulation_QPSK)
844                                 defaultSat["orbpos"] = frontendData.get("orbital_position", 0)
845                         elif ttype == "DVB-C":
846                                 defaultCab["frequency"] = frontendData.get("frequency", 0) / 1000
847                                 defaultCab["symbolrate"] = frontendData.get("symbol_rate", 0) / 1000
848                                 defaultCab["inversion"] = frontendData.get("inversion", eDVBFrontendParametersCable.Inversion_Unknown)
849                                 defaultCab["fec"] = frontendData.get("fec_inner", eDVBFrontendParametersCable.FEC_Auto)
850                                 defaultCab["modulation"] = frontendData.get("modulation", eDVBFrontendParametersCable.Modulation_QAM16)
851                                 defaultCab["system"] = frontendData.get("system", eDVBFrontendParametersCable.System_DVB_C_ANNEX_A)
852                         elif ttype == "DVB-T":
853                                 defaultTer["frequency"] = frontendData.get("frequency", 47400000) / 1000
854                                 defaultTer["inversion"] = frontendData.get("inversion", eDVBFrontendParametersTerrestrial.Inversion_Unknown)
855                                 defaultTer["bandwidth"] = frontendData.get("bandwidth", 8000000)
856                                 defaultTer["fechigh"] = frontendData.get("code_rate_hp", eDVBFrontendParametersTerrestrial.FEC_Auto)
857                                 defaultTer["feclow"] = frontendData.get("code_rate_lp", eDVBFrontendParametersTerrestrial.FEC_Auto)
858                                 defaultTer["modulation"] = frontendData.get("constellation", eDVBFrontendParametersTerrestrial.Modulation_Auto)
859                                 defaultTer["transmission_mode"] = frontendData.get("transmission_mode", eDVBFrontendParametersTerrestrial.TransmissionMode_Auto)
860                                 defaultTer["guard_interval"] = frontendData.get("guard_interval", eDVBFrontendParametersTerrestrial.GuardInterval_Auto)
861                                 defaultTer["hierarchy"] = frontendData.get("hierarchy_information", eDVBFrontendParametersTerrestrial.Hierarchy_Auto)
862                                 defaultTer["system"] = frontendData.get("system", eDVBFrontendParametersTerrestrial.System_DVB_T)
863                                 defaultTer["plp_id"] = frontendData.get("plp_id", 0)
864
865                 self.scan_sat = ConfigSubsection()
866                 self.scan_cab = ConfigSubsection()
867                 self.scan_ter = ConfigSubsection()
868
869                 nim_list = []
870                 # collect all nims which are *not* set to "nothing"
871                 for n in nimmanager.nim_slots:
872                         if n.config_mode == "nothing":
873                                 continue
874                         if n.config_mode == "advanced" and len(nimmanager.getSatListForNim(n.slot)) < 1:
875                                 continue
876                         if n.config_mode in ("loopthrough", "satposdepends"):
877                                 root_id = nimmanager.sec.getRoot(n.slot_id, int(n.config.connectedTo.value))
878                                 if n.type == nimmanager.nim_slots[root_id].type: # check if connected from a DVB-S to DVB-S2 Nim or vice versa
879                                         continue
880                         nim_list.append((str(n.slot), n.friendly_full_description))
881
882                 self.scan_nims = ConfigSelection(choices = nim_list)
883                 if frontendData is not None and len(nim_list) > 0:
884                         self.scan_nims.value = str(frontendData.get("tuner_number", nim_list[0][0]))
885
886                 for slot in nimmanager.nim_slots:
887                         if slot.isCompatible("DVB-T"):
888                                 self.ter_tnumber = slot.slot
889                 if self.ter_tnumber is not None:
890                         self.ter_channel_input = supportedChannels(self.ter_tnumber)
891
892                 # status
893                 self.scan_snr = ConfigSlider()
894                 self.scan_snr.enabled = False
895                 self.scan_agc = ConfigSlider()
896                 self.scan_agc.enabled = False
897                 self.scan_ber = ConfigSlider()
898                 self.scan_ber.enabled = False
899
900                 # sat
901                 self.scan_sat.system = ConfigSelection(default = defaultSat["system"], choices = [
902                         (eDVBFrontendParametersSatellite.System_DVB_S, _("DVB-S")),
903                         (eDVBFrontendParametersSatellite.System_DVB_S2, _("DVB-S2"))])
904                 self.scan_sat.frequency = ConfigInteger(default = defaultSat["frequency"], limits = (1, 99999))
905                 self.scan_sat.inversion = ConfigSelection(default = defaultSat["inversion"], choices = [
906                         (eDVBFrontendParametersSatellite.Inversion_Off, _("Off")),
907                         (eDVBFrontendParametersSatellite.Inversion_On, _("On")),
908                         (eDVBFrontendParametersSatellite.Inversion_Unknown, _("Auto"))])
909                 self.scan_sat.symbolrate = ConfigInteger(default = defaultSat["symbolrate"], limits = (1, 99999))
910                 self.scan_sat.polarization = ConfigSelection(default = defaultSat["polarization"], choices = [
911                         (eDVBFrontendParametersSatellite.Polarisation_Horizontal, _("horizontal")),
912                         (eDVBFrontendParametersSatellite.Polarisation_Vertical, _("vertical")),
913                         (eDVBFrontendParametersSatellite.Polarisation_CircularLeft, _("circular left")),
914                         (eDVBFrontendParametersSatellite.Polarisation_CircularRight, _("circular right"))])
915                 self.scan_sat.fec = ConfigSelection(default = defaultSat["fec"], choices = [
916                         (eDVBFrontendParametersSatellite.FEC_Auto, _("Auto")),
917                         (eDVBFrontendParametersSatellite.FEC_1_2, "1/2"),
918                         (eDVBFrontendParametersSatellite.FEC_2_3, "2/3"),
919                         (eDVBFrontendParametersSatellite.FEC_3_4, "3/4"),
920                         (eDVBFrontendParametersSatellite.FEC_5_6, "5/6"),
921                         (eDVBFrontendParametersSatellite.FEC_7_8, "7/8"),
922                         (eDVBFrontendParametersSatellite.FEC_None, _("None"))])
923                 self.scan_sat.fec_s2 = ConfigSelection(default = defaultSat["fec_s2"], choices = [
924                         (eDVBFrontendParametersSatellite.FEC_1_2, "1/2"),
925                         (eDVBFrontendParametersSatellite.FEC_2_3, "2/3"),
926                         (eDVBFrontendParametersSatellite.FEC_3_4, "3/4"),
927                         (eDVBFrontendParametersSatellite.FEC_3_5, "3/5"),
928                         (eDVBFrontendParametersSatellite.FEC_4_5, "4/5"),
929                         (eDVBFrontendParametersSatellite.FEC_5_6, "5/6"),
930                         (eDVBFrontendParametersSatellite.FEC_7_8, "7/8"),
931                         (eDVBFrontendParametersSatellite.FEC_8_9, "8/9"),
932                         (eDVBFrontendParametersSatellite.FEC_9_10, "9/10")])
933                 self.scan_sat.modulation = ConfigSelection(default = defaultSat["modulation"], choices = [
934                         (eDVBFrontendParametersSatellite.Modulation_QPSK, "QPSK"),
935                         (eDVBFrontendParametersSatellite.Modulation_8PSK, "8PSK")])
936                 self.scan_sat.rolloff = ConfigSelection(default = defaultSat.get("rolloff", eDVBFrontendParametersSatellite.RollOff_alpha_0_35), choices = [
937                         (eDVBFrontendParametersSatellite.RollOff_alpha_0_35, "0.35"),
938                         (eDVBFrontendParametersSatellite.RollOff_alpha_0_25, "0.25"),
939                         (eDVBFrontendParametersSatellite.RollOff_alpha_0_20, "0.20"),
940                         (eDVBFrontendParametersSatellite.RollOff_auto, _("Auto"))])
941                 self.scan_sat.pilot = ConfigSelection(default = defaultSat.get("pilot", eDVBFrontendParametersSatellite.Pilot_Unknown), choices = [
942                         (eDVBFrontendParametersSatellite.Pilot_Off, _("Off")),
943                         (eDVBFrontendParametersSatellite.Pilot_On, _("On")),
944                         (eDVBFrontendParametersSatellite.Pilot_Unknown, _("Auto"))])
945
946                 # cable
947                 self.scan_cab.frequency = ConfigInteger(default = defaultCab["frequency"], limits = (50, 999))
948                 self.scan_cab.inversion = ConfigSelection(default = defaultCab["inversion"], choices = [
949                         (eDVBFrontendParametersCable.Inversion_Off, _("Off")),
950                         (eDVBFrontendParametersCable.Inversion_On, _("On")),
951                         (eDVBFrontendParametersCable.Inversion_Unknown, _("Auto"))])
952                 self.scan_cab.modulation = ConfigSelection(default = defaultCab["modulation"], choices = [
953                         (eDVBFrontendParametersCable.Modulation_QAM16, "16-QAM"),
954                         (eDVBFrontendParametersCable.Modulation_QAM32, "32-QAM"),
955                         (eDVBFrontendParametersCable.Modulation_QAM64, "64-QAM"),
956                         (eDVBFrontendParametersCable.Modulation_QAM128, "128-QAM"),
957                         (eDVBFrontendParametersCable.Modulation_QAM256, "256-QAM")])
958                 self.scan_cab.fec = ConfigSelection(default = defaultCab["fec"], choices = [
959                         (eDVBFrontendParametersCable.FEC_Auto, _("Auto")),
960                         (eDVBFrontendParametersCable.FEC_1_2, "1/2"),
961                         (eDVBFrontendParametersCable.FEC_2_3, "2/3"),
962                         (eDVBFrontendParametersCable.FEC_3_4, "3/4"),
963                         (eDVBFrontendParametersCable.FEC_5_6, "5/6"),
964                         (eDVBFrontendParametersCable.FEC_7_8, "7/8"),
965                         (eDVBFrontendParametersCable.FEC_8_9, "8/9"),
966                         (eDVBFrontendParametersCable.FEC_3_5, "3/5"),
967                         (eDVBFrontendParametersCable.FEC_4_5, "4/5"),
968                         (eDVBFrontendParametersCable.FEC_9_10, "9/10"),
969                         (eDVBFrontendParametersCable.FEC_None, _("None"))])
970                 self.scan_cab.symbolrate = ConfigInteger(default = defaultCab["symbolrate"], limits = (1, 99999))
971                 self.scan_cab.system = ConfigSelection(default = defaultCab["system"], choices = [
972                         (eDVBFrontendParametersCable.System_DVB_C_ANNEX_A, _("DVB-C")),
973                         (eDVBFrontendParametersCable.System_DVB_C_ANNEX_C, _("DVB-C ANNEX C"))])
974
975                 # terrestial
976                 self.scan_ter.frequency = ConfigInteger(default = defaultTer["frequency"], limits = (50000, 999000))
977                 self.scan_ter.channel = ConfigInteger(default = 21, limits = (1, 99))
978                 self.scan_ter.inversion = ConfigSelection(default = defaultTer["inversion"], choices = [
979                         (eDVBFrontendParametersTerrestrial.Inversion_Off, _("Off")),
980                         (eDVBFrontendParametersTerrestrial.Inversion_On, _("On")),
981                         (eDVBFrontendParametersTerrestrial.Inversion_Unknown, _("Auto"))])
982                 # WORKAROUND: we can't use BW-auto
983                 self.scan_ter.bandwidth = ConfigSelection(default = defaultTer["bandwidth"], choices = [
984                         (1712000, "1.712MHz"),
985                         (5000000, "5MHz"),
986                         (6000000, "6MHz"),
987                         (7000000, "7MHz"),
988                         (8000000, "8MHz"),
989                         (10000000,"10MHz")
990                         ])
991                 #, (eDVBFrontendParametersTerrestrial.Bandwidth_Auto, _("Auto"))))
992                 self.scan_ter.fechigh = ConfigSelection(default = defaultTer["fechigh"], choices = [
993                         (eDVBFrontendParametersTerrestrial.FEC_1_2, "1/2"),
994                         (eDVBFrontendParametersTerrestrial.FEC_2_3, "2/3"),
995                         (eDVBFrontendParametersTerrestrial.FEC_3_4, "3/4"),
996                         (eDVBFrontendParametersTerrestrial.FEC_5_6, "5/6"),
997                         (eDVBFrontendParametersTerrestrial.FEC_6_7, "6/7"),
998                         (eDVBFrontendParametersTerrestrial.FEC_7_8, "7/8"),
999                         (eDVBFrontendParametersTerrestrial.FEC_8_9, "8/9"),
1000                         (eDVBFrontendParametersTerrestrial.FEC_Auto, _("Auto"))])
1001                 self.scan_ter.feclow = ConfigSelection(default = defaultTer["feclow"], choices = [
1002                         (eDVBFrontendParametersTerrestrial.FEC_1_2, "1/2"),
1003                         (eDVBFrontendParametersTerrestrial.FEC_2_3, "2/3"),
1004                         (eDVBFrontendParametersTerrestrial.FEC_3_4, "3/4"),
1005                         (eDVBFrontendParametersTerrestrial.FEC_5_6, "5/6"),
1006                         (eDVBFrontendParametersTerrestrial.FEC_6_7, "6/7"),
1007                         (eDVBFrontendParametersTerrestrial.FEC_7_8, "7/8"),
1008                         (eDVBFrontendParametersTerrestrial.FEC_8_9, "8/9"),
1009                         (eDVBFrontendParametersTerrestrial.FEC_Auto, _("Auto"))])
1010                 self.scan_ter.modulation = ConfigSelection(default = defaultTer["modulation"], choices = [
1011                         (eDVBFrontendParametersTerrestrial.Modulation_QPSK, "QPSK"),
1012                         (eDVBFrontendParametersTerrestrial.Modulation_QAM16, "QAM16"),
1013                         (eDVBFrontendParametersTerrestrial.Modulation_QAM64, "QAM64"),
1014                         (eDVBFrontendParametersTerrestrial.Modulation_QAM256, "QAM256"),
1015                         (eDVBFrontendParametersTerrestrial.Modulation_Auto, _("Auto"))])
1016                 self.scan_ter.transmission = ConfigSelection(default = defaultTer["transmission_mode"], choices = [
1017                         (eDVBFrontendParametersTerrestrial.TransmissionMode_1k, "1K"),
1018                         (eDVBFrontendParametersTerrestrial.TransmissionMode_2k, "2K"),
1019                         (eDVBFrontendParametersTerrestrial.TransmissionMode_4k, "4K"),
1020                         (eDVBFrontendParametersTerrestrial.TransmissionMode_8k, "8K"),
1021                         (eDVBFrontendParametersTerrestrial.TransmissionMode_16k, "16K"),
1022                         (eDVBFrontendParametersTerrestrial.TransmissionMode_32k, "32K"),
1023                         (eDVBFrontendParametersTerrestrial.TransmissionMode_Auto, _("Auto"))])
1024                 self.scan_ter.guard = ConfigSelection(default = defaultTer["guard_interval"], choices = [
1025                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_32, "1/32"),
1026                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_16, "1/16"),
1027                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_8, "1/8"),
1028                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_4, "1/4"),
1029                         (eDVBFrontendParametersTerrestrial.GuardInterval_1_128, "1/128"),
1030                         (eDVBFrontendParametersTerrestrial.GuardInterval_19_128, "19/128"),
1031                         (eDVBFrontendParametersTerrestrial.GuardInterval_19_256, "19/256"),
1032                         (eDVBFrontendParametersTerrestrial.GuardInterval_Auto, _("Auto"))])
1033                 self.scan_ter.hierarchy = ConfigSelection(default = defaultTer["hierarchy"], choices = [
1034                         (eDVBFrontendParametersTerrestrial.Hierarchy_None, _("None")),
1035                         (eDVBFrontendParametersTerrestrial.Hierarchy_1, "1"),
1036                         (eDVBFrontendParametersTerrestrial.Hierarchy_2, "2"),
1037                         (eDVBFrontendParametersTerrestrial.Hierarchy_4, "4"),
1038                         (eDVBFrontendParametersTerrestrial.Hierarchy_Auto, _("Auto"))])
1039                 self.scan_ter.system = ConfigSelection(default = defaultTer["system"], choices = [
1040                         (eDVBFrontendParametersTerrestrial.System_DVB_T_T2, _("Auto")),
1041                         (eDVBFrontendParametersTerrestrial.System_DVB_T, _("DVB-T")),
1042                         (eDVBFrontendParametersTerrestrial.System_DVB_T2, _("DVB-T2"))])
1043                 self.scan_ter.plp_id = ConfigInteger(default = defaultTer["plp_id"], limits = (0, 255))
1044
1045                 self.scan_scansat = {}
1046                 for sat in nimmanager.satList:
1047                         #print sat[1]
1048                         self.scan_scansat[sat[0]] = ConfigYesNo(default = False)
1049
1050                 self.scan_satselection = []
1051                 for slot in nimmanager.nim_slots:
1052                         if slot.isCompatible("DVB-S"):
1053                                 self.scan_satselection.append(getConfigSatlist(defaultSat["orbpos"], self.satList[slot.slot]))
1054                         else:
1055                                 self.scan_satselection.append(None)
1056
1057                 self.terrestrial_nims_regions = []
1058                 for slot in nimmanager.nim_slots:
1059                         if slot.isCompatible("DVB-T"):
1060                                 self.terrestrial_nims_regions.append(self.getTerrestrialRegionsList(slot.slot))
1061                         else:
1062                                 self.terrestrial_nims_regions.append(None)
1063
1064                 if frontendData is not None and ttype == "DVB-S" and self.predefinedTranspondersList(defaultSat["orbpos"]) is not None:
1065                         defaultSatSearchType = "predefined_transponder"
1066                 else:
1067                         defaultSatSearchType = "single_transponder"
1068                 if frontendData is not None and ttype == "DVB-T" and self.predefinedTerrTranspondersList() is not None:
1069                         defaultTerrSearchType = "predefined_transponder"
1070                 else:
1071                         defaultTerrSearchType = "single_transponder"
1072
1073                 if frontendData is not None and ttype == "DVB-C" and self.predefinedCabTranspondersList() is not None:
1074                         defaultCabSearchType = "predefined_transponder"
1075                 else:
1076                         defaultCabSearchType = "single_transponder"
1077
1078                 self.scan_type = ConfigSelection(default = defaultSatSearchType, choices = [("single_transponder", _("User defined transponder")), ("predefined_transponder", _("Predefined transponder")), ("single_satellite", _("Single satellite")), ("multisat", _("Multisat")), ("multisat_yes", _("Multisat all select"))])
1079                 self.scan_typecable = ConfigSelection(default = defaultCabSearchType, choices = [("single_transponder", _("User defined transponder")), ("predefined_transponder", _("Predefined transponder")), ("complete", _("Complete"))])
1080                 self.last_scan_typecable = "single_transponder"
1081                 self.cable_toggle = {"single_transponder":"complete", "complete":"single_transponder"}
1082                 self.scan_typeterrestrial = ConfigSelection(default = defaultTerrSearchType, choices = [("single_transponder", _("User defined transponder")), ("predefined_transponder", _("Predefined transponder")), ("complete", _("Complete"))])
1083                 self.scan_input_as = ConfigSelection(default = "channel", choices = [("frequency", _("Frequency")), ("channel", _("Channel"))])
1084                 self.scan_ter_complete_type = ConfigSelection(default = "all", choices = [("all", _("All frequency")), ("extended", _("Extended"))])
1085                 self.scan_clearallservices = ConfigSelection(default = "no", choices = [("no", _("no")), ("yes", _("yes")), ("yes_hold_feeds", _("yes (keep feeds)"))])
1086                 self.scan_onlyfree = ConfigYesNo(default = False)
1087                 self.scan_networkScan = ConfigYesNo(default = False)
1088
1089                 return True
1090
1091         def keyLeft(self):
1092                 ConfigListScreen.keyLeft(self)
1093                 self.newConfig()
1094
1095         def keyRight(self):
1096                 ConfigListScreen.keyRight(self)
1097                 self.newConfig()
1098
1099         def handleKeyFileCallback(self, answer):
1100                 ConfigListScreen.handleKeyFileCallback(self, answer)
1101                 self.newConfig()
1102
1103         def updateStatus(self):
1104                 print "updatestatus"
1105
1106         def addSatTransponder(self, tlist, frequency, symbol_rate, polarisation, fec, inversion, orbital_position, system, modulation, rolloff, pilot):
1107                 print "Add Sat: frequ: " + str(frequency) + " symbol: " + str(symbol_rate) + " pol: " + str(polarisation) + " fec: " + str(fec) + " inversion: " + str(inversion) + " modulation: " + str(modulation) + " system: " + str(system) + " rolloff" + str(rolloff) + " pilot" + str(pilot)
1108                 print "orbpos: " + str(orbital_position)
1109                 parm = eDVBFrontendParametersSatellite()
1110                 parm.modulation = modulation
1111                 parm.system = system
1112                 parm.frequency = frequency * 1000
1113                 parm.symbol_rate = symbol_rate * 1000
1114                 parm.polarisation = polarisation
1115                 parm.fec = fec
1116                 parm.inversion = inversion
1117                 parm.orbital_position = orbital_position
1118                 parm.rolloff = rolloff
1119                 parm.pilot = pilot
1120                 tlist.append(parm)
1121
1122         def addCabTransponder(self, tlist, frequency, symbol_rate, modulation, fec, inversion):
1123                 print "Add Cab: frequ: " + str(frequency) + " symbol: " + str(symbol_rate) + " pol: " + str(modulation) + " fec: " + str(fec) + " inversion: " + str(inversion)
1124                 parm = eDVBFrontendParametersCable()
1125                 parm.frequency = frequency
1126                 parm.symbol_rate = symbol_rate
1127                 parm.modulation = modulation
1128                 parm.fec_inner = fec
1129                 parm.inversion = inversion
1130                 tlist.append(parm)
1131
1132         def addTerTransponder(self, tlist, *args, **kwargs):
1133                 tlist.append(buildTerTransponder(*args, **kwargs))
1134
1135         def keyGo(self):
1136                 infoBarInstance = InfoBar.instance
1137                 if infoBarInstance:
1138                         infoBarInstance.checkTimeshiftRunning(self.keyGoCheckTimeshiftCallback)
1139                 else:
1140                         self.keyGoCheckTimeshiftCallback(True)
1141
1142         def keyGoCheckTimeshiftCallback(self, answer):
1143                 START_SCAN = 0
1144                 SEARCH_CABLE_TRANSPONDERS = 1
1145                 SEARCH_TERRESTRIAL2_TRANSPONDERS = 2
1146                 if not answer or self.scan_nims.value == "":
1147                         return
1148                 tlist = []
1149                 flags = None
1150                 removeAll = True
1151                 action = START_SCAN
1152                 index_to_scan = int(self.scan_nims.value)
1153
1154                 if self.scan_nims == [ ]:
1155                         self.session.open(MessageBox, _("No tuner is enabled!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1156                         return
1157
1158                 nim = nimmanager.nim_slots[index_to_scan]
1159                 print "nim", nim.slot
1160                 if nim.isCompatible("DVB-S"):
1161                         print "is compatible with DVB-S"
1162                         if self.scan_type.value == "single_transponder":
1163                                 # these lists are generated for each tuner, so this has work.
1164                                 assert len(self.satList) > index_to_scan
1165                                 assert len(self.scan_satselection) > index_to_scan
1166
1167                                 nimsats = self.satList[index_to_scan]
1168                                 selsatidx = self.scan_satselection[index_to_scan].index
1169
1170                                 # however, the satList itself could be empty. in that case, "index" is 0 (for "None").
1171                                 if len(nimsats):
1172                                         orbpos = nimsats[selsatidx][0]
1173                                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S:
1174                                                 fec = self.scan_sat.fec.value
1175                                         else:
1176                                                 fec = self.scan_sat.fec_s2.value
1177                                         print "add sat transponder"
1178                                         self.addSatTransponder(tlist, self.scan_sat.frequency.value,
1179                                                                 self.scan_sat.symbolrate.value,
1180                                                                 self.scan_sat.polarization.value,
1181                                                                 fec,
1182                                                                 self.scan_sat.inversion.value,
1183                                                                 orbpos,
1184                                                                 self.scan_sat.system.value,
1185                                                                 self.scan_sat.modulation.value,
1186                                                                 self.scan_sat.rolloff.value,
1187                                                                 self.scan_sat.pilot.value)
1188                                 removeAll = False
1189                         elif self.scan_type.value == "predefined_transponder":
1190                                 nimsats = self.satList[index_to_scan]
1191                                 selsatidx = self.scan_satselection[index_to_scan].index
1192                                 if len(nimsats):
1193                                         orbpos = nimsats[selsatidx][0]
1194                                         tps = nimmanager.getTransponders(orbpos)
1195                                         if len(tps) and len(tps) > self.preDefTransponders.index:
1196                                                 tp = tps[self.preDefTransponders.index]
1197                                                 self.addSatTransponder(tlist, tp[1] / 1000, tp[2] / 1000, tp[3], tp[4], tp[7], orbpos, tp[5], tp[6], tp[8], tp[9])
1198                                 removeAll = False
1199                         elif self.scan_type.value == "single_satellite":
1200                                 sat = self.satList[index_to_scan][self.scan_satselection[index_to_scan].index]
1201                                 getInitialTransponderList(tlist, sat[0])
1202                         elif "multisat" in self.scan_type.value:
1203                                 SatList = nimmanager.getSatListForNim(index_to_scan)
1204                                 for x in self.multiscanlist:
1205                                         if x[1].value:
1206                                                 print "   " + str(x[0])
1207                                                 getInitialTransponderList(tlist, x[0])
1208
1209                 elif nim.isCompatible("DVB-C"):
1210                         if self.scan_typecable.value == "single_transponder":
1211                                 self.addCabTransponder(tlist, self.scan_cab.frequency.value*1000,
1212                                                                                           self.scan_cab.symbolrate.value*1000,
1213                                                                                           self.scan_cab.modulation.value,
1214                                                                                           self.scan_cab.fec.value,
1215                                                                                           self.scan_cab.inversion.value)
1216                                 removeAll = False
1217                         elif self.scan_typecable.value == "predefined_transponder":
1218                                 tps = nimmanager.getTranspondersCable(index_to_scan)
1219                                 if len(tps) and len(tps) > self.CableTransponders.index :
1220                                         tp = tps[self.CableTransponders.index]
1221                                         # 0 transponder type, 1 freq, 2 sym, 3 mod, 4 fec, 5 inv, 6 sys
1222                                         self.addCabTransponder(tlist, tp[1], tp[2], tp[3], tp[4], tp[5])
1223                                 removeAll = False
1224                         elif self.scan_typecable.value == "complete":
1225                                 if config.Nims[index_to_scan].cable.scan_type.value == "provider":
1226                                         getInitialCableTransponderList(tlist, index_to_scan)
1227                                 else:
1228                                         action = SEARCH_CABLE_TRANSPONDERS
1229
1230                 elif nim.isCompatible("DVB-T"):
1231                         if self.scan_typeterrestrial.value == "single_transponder":
1232                                 if self.scan_input_as.value == "channel":
1233                                         frequency = channel2frequency(self.scan_ter.channel.value, self.ter_tnumber)
1234                                 else:
1235                                         frequency = self.scan_ter.frequency.value * 1000
1236                                 self.addTerTransponder(tlist,
1237                                                 frequency,
1238                                                 inversion = self.scan_ter.inversion.value,
1239                                                 bandwidth = self.scan_ter.bandwidth.value,
1240                                                 fechigh = self.scan_ter.fechigh.value,
1241                                                 feclow = self.scan_ter.feclow.value,
1242                                                 modulation = self.scan_ter.modulation.value,
1243                                                 transmission = self.scan_ter.transmission.value,
1244                                                 guard = self.scan_ter.guard.value,
1245                                                 hierarchy = self.scan_ter.hierarchy.value,
1246                                                 system = self.scan_ter.system.value,
1247                                                 plpid = self.scan_ter.plp_id.value)
1248                                 removeAll = False
1249                         elif self.scan_typeterrestrial.value == "predefined_transponder":
1250                                 if self.TerrestrialTransponders is not None:
1251                                         region = self.terrestrial_nims_regions[index_to_scan].value
1252                                         tps = nimmanager.getTranspondersTerrestrial(region)
1253                                         if len(tps) and len(tps) > self.TerrestrialTransponders.index:
1254                                                 tp = tps[self.TerrestrialTransponders.index]
1255                                                 tlist.append(buildTerTransponder(tp[1], tp[9], tp[2], tp[4], tp[5], tp[3], tp[7], tp[6], tp[8], self.scan_ter.system.value, tp[11]))
1256                                 removeAll = False
1257                         elif self.scan_typeterrestrial.value == "complete":
1258                                 skip_t2 = False
1259                                 if nimmanager.getNimName(nim.slot).startswith("Sundtek") and self.scan_ter_complete_type.value == "all":
1260                                         action = SEARCH_TERRESTRIAL2_TRANSPONDERS
1261                                 elif SystemInfo["Blindscan_t2_available"]:
1262                                         skip_t2 = True
1263                                         if nim.isCompatible("DVB-T2"):
1264                                                 if len(self.terrestrialTransponderGetCmd(nim.slot)):
1265                                                         action = SEARCH_TERRESTRIAL2_TRANSPONDERS
1266                                                 else:
1267                                                         skip_t2 = False
1268                                 getInitialTerrestrialTransponderList(tlist, self.TerrestrialRegion.value, int(self.scan_ter.system.value), skip_t2)
1269
1270                 flags = self.scan_networkScan.value and eComponentScan.scanNetworkSearch or 0
1271
1272                 tmp = self.scan_clearallservices.value
1273                 if tmp == "yes":
1274                         flags |= eComponentScan.scanRemoveServices
1275                 elif tmp == "yes_hold_feeds":
1276                         flags |= eComponentScan.scanRemoveServices
1277                         flags |= eComponentScan.scanDontRemoveFeeds
1278
1279                 if tmp != "no" and not removeAll:
1280                         flags |= eComponentScan.scanDontRemoveUnscanned
1281
1282                 if self.scan_onlyfree.value:
1283                         flags |= eComponentScan.scanOnlyFree
1284
1285                 for x in self["config"].list:
1286                         x[1].save()
1287
1288                 if action == START_SCAN:
1289                         self.startScan(tlist, flags, index_to_scan, self.networkid)
1290                 elif action == SEARCH_CABLE_TRANSPONDERS:
1291                         self.flags = flags
1292                         self.feid = index_to_scan
1293                         self.tlist = []
1294                         self.startCableTransponderSearch(self.feid)
1295                 elif action == SEARCH_TERRESTRIAL2_TRANSPONDERS:
1296                         self.flags = flags
1297                         self.feid = index_to_scan
1298                         self.tlist = tlist
1299                         self.startTerrestrialTransponderSearch(self.feid, nimmanager.getTerrestrialDescription(self.feid))
1300
1301         def setCableTransponderSearchResult(self, tlist):
1302                 self.tlist = tlist
1303
1304         def cableTransponderSearchFinished(self):
1305                 if self.tlist is None:
1306                         self.tlist = []
1307                 else:
1308                         self.startScan(self.tlist, self.flags, self.feid)
1309
1310         def setTerrestrialTransponderSearchResult(self, tlist):
1311                 self.tlist = tlist
1312
1313         def terrestrialTransponderSearchFinished(self):
1314                 if self.tlist is None:
1315                         self.tlist = []
1316                 else:
1317                         self.startScan(self.tlist, self.flags, self.feid)
1318
1319         def predefinedTranspondersList(self, orbpos):
1320                 default = None
1321                 if orbpos is not None:
1322                         list = []
1323                         if self.scan_sat.system.value == eDVBFrontendParametersSatellite.System_DVB_S2:
1324                                 fec = self.scan_sat.fec_s2.value
1325                         else:
1326                                 fec = self.scan_sat.fec.value
1327                         compare = [0, self.scan_sat.frequency.value*1000, self.scan_sat.symbolrate.value*1000, self.scan_sat.polarization.value, fec]
1328                         i = 0
1329                         tps = nimmanager.getTransponders(orbpos)
1330                         for tp in tps:
1331                                 if tp[0] == 0:
1332                                         if default is None and self.compareTransponders(tp, compare):
1333                                                 default = str(i)
1334                                         list.append((str(i), self.humanReadableTransponder(tp)))
1335                                         i += 1
1336                         self.preDefTransponders = ConfigSelection(choices = list, default = default)
1337                 return default
1338
1339         def humanReadableTransponder(self, tp):
1340                 if tp[3] in range (4) and tp[4] in range (11):
1341                         pol_list = ['H','V','L','R']
1342                         fec_list = ['Auto','1/2','2/3','3/4','5/6','7/8','8/9','3/5','4/5','9/10','None']
1343                         return str(tp[1] / 1000) + " " + pol_list[tp[3]] + " " + str(tp[2] / 1000) + " " + fec_list[tp[4]]
1344                 return _("Invalid transponder data")
1345
1346         def compareTransponders(self, tp, compare):
1347                 frequencyTolerance = 2000 #2 MHz
1348                 symbolRateTolerance = 10
1349                 return abs(tp[1] - compare[1]) <= frequencyTolerance and abs(tp[2] - compare[2]) <= symbolRateTolerance and tp[3] == compare[3] and (not tp[4] or tp[4] == compare[4])
1350
1351         def predefinedTerrTranspondersList(self):
1352                 default = None
1353                 list = []
1354                 compare = [2, self.scan_ter.frequency.value*1000]
1355                 i = 0
1356                 index_to_scan = int(self.scan_nims.value)
1357                 channels = supportedChannels(index_to_scan)
1358                 region = self.terrestrial_nims_regions[index_to_scan].value
1359                 tps = nimmanager.getTranspondersTerrestrial(region)
1360                 for tp in tps:
1361                         if tp[0] == 2: #TERRESTRIAL
1362                                 channel = ''
1363                                 if channels:
1364                                         channel = _(' (Channel %s)') % (getChannelNumber(tp[1], index_to_scan))
1365                                 if default is None and self.compareTerrTransponders(tp, compare):
1366                                         default = str(i)
1367                                 list.append((str(i), '%s MHz %s' % (str(tp[1] / 1000000), channel)))
1368                                 i += 1
1369                                 print "channel", channel
1370                 self.TerrestrialTransponders = ConfigSelection(choices = list, default = default)
1371                 return default
1372
1373         def compareTerrTransponders(self, tp, compare):
1374                 frequencyTolerance = 1000000 #1 MHz
1375                 return abs(tp[1] - compare[1]) <= frequencyTolerance
1376
1377         def getTerrestrialRegionsList(self, index_to_scan = None):
1378                 default = None
1379                 list = []
1380                 if index_to_scan is None:
1381                         index_to_scan = int(self.scan_nims.value)
1382                 defaultRegionForNIM = nimmanager.getTerrestrialDescription(index_to_scan)
1383                 for r in nimmanager.terrestrialsList:
1384                         if default is None and r[0] == defaultRegionForNIM:
1385                                 default = r[0]
1386                         list.append((r[0], r[0][:46]))
1387                 return ConfigSelection(choices = list, default = default)
1388
1389         def predefinedCabTranspondersList(self):
1390                 default = None
1391                 list = []
1392                 # 0 transponder type, 1 freq, 2 sym, 3 mod, 4 fec, 5 inv, 6 sys
1393                 compare = [1, self.scan_cab.frequency.value*1000, self.scan_cab.symbolrate.value*1000, self.scan_cab.modulation.value, self.scan_cab.fec.value, self.scan_cab.inversion.value, self.scan_cab.system.value]
1394                 i = 0
1395                 index_to_scan = int(self.scan_nims.value)
1396                 tps = nimmanager.getTranspondersCable(index_to_scan)
1397                 for tp in tps:
1398                         if tp[0] == 1: #CABLE
1399                                 if default is None and self.compareCabTransponders(tp, compare):
1400                                         default = str(i)
1401                                 list.append((str(i), self.humanReadableCabTransponder(tp)))
1402                                 i += 1
1403                 self.CableTransponders = ConfigSelection(choices = list, default = default)
1404                 return default
1405
1406         def humanReadableCabTransponder(self, tp):
1407                 if tp[3] in range (6) and (tp[4] in range (10) or tp[4] == 15):
1408                         mod_list = ['Auto', '16-QAM','32-QAM','64-QAM','128-QAM', '256-QAM']
1409                         fec_list = {0:"Auto", 1:'1/2', 2:'2/3', 3:'3/4', 4:'5/6', 5:'7/8', 6:'8/9', 7:'3/5', 8:'4/5', 9:'9/10', 15:'None'}
1410                         print str(tp[1]/1000) + " MHz " + fec_list[tp[4]] + " " + str(tp[2]/1000) + " " + mod_list[tp[3]]
1411                         return str(tp[1]/1000) + " MHz " + fec_list[tp[4]] + " " + str(tp[2]/1000) + " " + mod_list[tp[3]]
1412                 return _("Invalid transponder data")
1413
1414         def compareCabTransponders(self, tp, compare):
1415                 frequencyTolerance = 1000000 #1 MHz
1416                 symbolRateTolerance = 10
1417                 return abs(tp[1] - compare[1]) <= frequencyTolerance and abs(tp[2] - compare[2]) <= symbolRateTolerance and tp[3] == compare[3] and (not tp[4] or tp[4] == compare[4])
1418
1419         def startScan(self, tlist, flags, feid, networkid = 0):
1420                 if len(tlist):
1421                         # flags |= eComponentScan.scanSearchBAT
1422                         if self.finished_cb:
1423                                 self.session.openWithCallback(self.finished_cb, ServiceScan, [{"transponders": tlist, "feid": feid, "flags": flags, "networkid": networkid}])
1424                         else:
1425                                 self.session.openWithCallback(self.startScanCallback, ServiceScan, [{"transponders": tlist, "feid": feid, "flags": flags, "networkid": networkid}])
1426                 else:
1427                         if self.finished_cb:
1428                                 self.session.openWithCallback(self.finished_cb, MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1429                         else:
1430                                 self.session.open(MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1431
1432         def startScanCallback(self, answer=True):
1433                 if answer:
1434                         self.doCloseRecursive()
1435
1436         def keyCancel(self):
1437                 self.session.nav.playService(self.session.postScanService)
1438                 for x in self["config"].list:
1439                         x[1].cancel()
1440                 self.close()
1441
1442         def doCloseRecursive(self):
1443                 self.session.nav.playService(self.session.postScanService)
1444                 self.closeRecursive()
1445
1446 class ScanSimple(ConfigListScreen, Screen, CableTransponderSearchSupport, TerrestrialTransponderSearchSupport):
1447         def getNetworksForNim(self, nim):
1448                 if nim.isCompatible("DVB-S"):
1449                         networks = nimmanager.getSatListForNim(nim.slot)
1450                 elif nim.isCompatible("DVB-C"):
1451                         networks = nimmanager.getTranspondersCable(nim.slot)
1452                         if not networks and config.Nims[nim.slot].configMode.value == "enabled":
1453                                 networks = [ nim.type ]
1454                 elif nim.isCompatible("DVB-T"):
1455                         networks = nimmanager.getTerrestrialDescription(nim.slot)
1456                 elif not nim.empty:
1457                         networks = [ nim.type ] # "DVB-C" or "DVB-T". TODO: seperate networks for different C/T tuners, if we want to support that.
1458                 else:
1459                         # empty tuners provide no networks.
1460                         networks = [ ]
1461                 return networks
1462
1463         def __init__(self, session):
1464                 Screen.__init__(self, session)
1465                 Screen.setTitle(self, _("Automatic Scan"))
1466
1467                 self["key_red"] = StaticText(_("Close"))
1468                 self["key_green"] = StaticText(_("Scan"))
1469
1470                 self["actions"] = ActionMap(["SetupActions", "MenuActions", "ColorActions"],
1471                 {
1472                         "ok": self.keyGo,
1473                         "save": self.keyGo,
1474                         "cancel": self.keyCancel,
1475                         "menu": self.doCloseRecursive,
1476                         "red": self.keyCancel,
1477                         "green": self.keyGo,
1478                 }, -2)
1479
1480                 self.session.postScanService = session.nav.getCurrentlyPlayingServiceOrGroup()
1481
1482                 self.list = []
1483                 tlist = []
1484
1485                 known_networks = [ ]
1486                 nims_to_scan = [ ]
1487                 self.finished_cb = None
1488
1489                 for nim in nimmanager.nim_slots:
1490                         # collect networks provided by this tuner
1491
1492                         need_scan = False
1493                         networks = self.getNetworksForNim(nim)
1494
1495                         print "nim %d provides" % nim.slot, networks
1496                         print "known:", known_networks
1497
1498                         # we only need to scan on the first tuner which provides a network.
1499                         # this gives the first tuner for each network priority for scanning.
1500                         for x in networks:
1501                                 if x not in known_networks:
1502                                         need_scan = True
1503                                         print x, "not in ", known_networks
1504                                         known_networks.append(x)
1505
1506                         # don't offer to scan nims if nothing is connected
1507                         if not nimmanager.somethingConnected(nim.slot):
1508                                 need_scan = False
1509
1510                         if need_scan:
1511                                 nims_to_scan.append(nim)
1512
1513                 # we save the config elements to use them on keyGo
1514                 self.nim_enable = [ ]
1515
1516                 if len(nims_to_scan):
1517                         self.scan_networkScan = ConfigYesNo(default = True)
1518                         self.scan_clearallservices = ConfigSelection(default = "yes", choices = [("no", _("no")), ("yes", _("yes")), ("yes_hold_feeds", _("yes (keep feeds)"))])
1519                         self.list.append(getConfigListEntry(_("Network scan"), self.scan_networkScan))
1520                         self.list.append(getConfigListEntry(_("Clear before scan"), self.scan_clearallservices))
1521
1522                         for nim in nims_to_scan:
1523                                 nimconfig = ConfigYesNo(default = True)
1524                                 nimconfig.nim_index = nim.slot
1525                                 self.nim_enable.append(nimconfig)
1526                                 self.list.append(getConfigListEntry(_("Scan ") + nim.slot_name + " (" + nim.friendly_type + ")", nimconfig))
1527
1528                 ConfigListScreen.__init__(self, self.list)
1529                 self["header"] = Label(_("Automatic scan"))
1530                 self["footer"] = Label(_("Press OK to scan"))
1531
1532         def runAsync(self, finished_cb):
1533                 self.finished_cb = finished_cb
1534                 self.keyGo()
1535
1536         def keyGo(self):
1537                 InfoBarInstance = InfoBar.instance
1538                 if InfoBarInstance:
1539                         InfoBarInstance.checkTimeshiftRunning(self.keyGoCheckTimeshiftCallback)
1540                 else:
1541                         self.keyGoCheckTimeshiftCallback(True)
1542
1543         def keyGoCheckTimeshiftCallback(self, answer):
1544                 if answer:
1545                         self.scanList = []
1546                         self.known_networks = set()
1547                         self.nim_iter=0
1548                         self.buildTransponderList()
1549
1550         def buildTransponderList(self): # this method is called multiple times because of asynchronous stuff
1551                 APPEND_NOW = 0
1552                 SEARCH_CABLE_TRANSPONDERS = 1
1553                 SEARCH_TERRESTRIAL2_TRANSPONDERS = 2
1554                 action = APPEND_NOW
1555
1556                 n = self.nim_iter < len(self.nim_enable) and self.nim_enable[self.nim_iter] or None
1557                 self.nim_iter += 1
1558                 if n:
1559                         if n.value: # check if nim is enabled
1560                                 flags = 0
1561                                 nim = nimmanager.nim_slots[n.nim_index]
1562                                 networks = set(self.getNetworksForNim(nim))
1563                                 networkid = 0
1564
1565                                 # don't scan anything twice
1566                                 networks.discard(self.known_networks)
1567
1568                                 tlist = [ ]
1569                                 if nim.isCompatible("DVB-S"):
1570                                         # get initial transponders for each satellite to be scanned
1571                                         for sat in networks:
1572                                                 getInitialTransponderList(tlist, sat[0])
1573                                 elif nim.isCompatible("DVB-C"):
1574                                         if config.Nims[nim.slot].cable.scan_type.value == "provider":
1575                                                 getInitialCableTransponderList(tlist, nim.slot)
1576                                         else:
1577                                                 action = SEARCH_CABLE_TRANSPONDERS
1578                                                 networkid = config.Nims[nim.slot].cable.scan_networkid.value
1579                                 elif nim.isCompatible("DVB-T"):
1580                                         skip_t2 = False
1581                                         if SystemInfo["Blindscan_t2_available"]:
1582                                                 skip_t2 = True
1583                                                 if nim.isCompatible("DVB-T2"):
1584                                                         if len(self.terrestrialTransponderGetCmd(nim.slot)):
1585                                                                 action = SEARCH_TERRESTRIAL2_TRANSPONDERS
1586                                                         else:
1587                                                                 skip_t2 = False
1588                                         getInitialTerrestrialTransponderList(tlist, nimmanager.getTerrestrialDescription(nim.slot), skip_t2=skip_t2)
1589                                 else:
1590                                         assert False
1591
1592                                 flags = self.scan_networkScan.value and eComponentScan.scanNetworkSearch or 0
1593                                 tmp = self.scan_clearallservices.value
1594                                 if tmp == "yes":
1595                                         flags |= eComponentScan.scanRemoveServices
1596                                 elif tmp == "yes_hold_feeds":
1597                                         flags |= eComponentScan.scanRemoveServices
1598                                         flags |= eComponentScan.scanDontRemoveFeeds
1599
1600                                 if action == APPEND_NOW:
1601                                         self.scanList.append({"transponders": tlist, "feid": nim.slot, "flags": flags})
1602                                 elif action == SEARCH_CABLE_TRANSPONDERS:
1603                                         self.flags = flags
1604                                         self.feid = nim.slot
1605                                         self.networkid = networkid
1606                                         self.startCableTransponderSearch(nim.slot)
1607                                         return
1608                                 elif action == SEARCH_TERRESTRIAL2_TRANSPONDERS:
1609                                         self.tlist = tlist
1610                                         self.flags = flags
1611                                         self.feid = nim.slot
1612                                         self.startTerrestrialTransponderSearch(nim.slot, nimmanager.getTerrestrialDescription(nim.slot))
1613                                         return
1614                                 else:
1615                                         assert False
1616
1617                         self.buildTransponderList() # recursive call of this function !!!
1618                         return
1619                 # when we are here, then the recursion is finished and all enabled nims are checked
1620                 # so we now start the real transponder scan
1621                 self.startScan(self.scanList)
1622
1623         def startScan(self, scanList):
1624                 if len(scanList):
1625                         if self.finished_cb:
1626                                 self.session.openWithCallback(self.finished_cb, ServiceScan, scanList = scanList)
1627                         else:
1628                                 self.session.open(ServiceScan, scanList = scanList)
1629                 else:
1630                         if self.finished_cb:
1631                                 self.session.openWithCallback(self.finished_cb, MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1632                         else:
1633                                 self.session.open(MessageBox, _("Nothing to scan!\nPlease setup your tuner settings before you start a service scan."), MessageBox.TYPE_ERROR)
1634
1635         def setCableTransponderSearchResult(self, tlist):
1636                 if tlist is not None:
1637                         self.scanList.append({"transponders": tlist, "feid": self.feid, "flags": self.flags})
1638
1639         def cableTransponderSearchFinished(self):
1640                 self.buildTransponderList()
1641
1642         def setTerrestrialTransponderSearchResult(self, tlist):
1643                 if tlist is not None:
1644                         self.tlist.extend(tlist)
1645                 if self.tlist is not None:
1646                         self.scanList.append({"transponders": self.tlist, "feid": self.feid, "flags": self.flags})
1647
1648         def terrestrialTransponderSearchFinished(self):
1649                 self.buildTransponderList()
1650
1651         def keyCancel(self):
1652                 self.session.nav.playService(self.session.postScanService)
1653                 self.close()
1654
1655         def doCloseRecursive(self):
1656                 self.session.nav.playService(self.session.postScanService)
1657                 self.closeRecursive()
1658
1659         def Satexists(self, tlist, pos):
1660                 for x in tlist:
1661                         if x == pos:
1662                                 return 1
1663                 return 0