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