small optimize
[enigma2.git] / lib / dvb / frontend.cpp
1 #include <lib/dvb/dvb.h>
2 #include <lib/base/eerror.h>
3 #include <lib/base/nconfig.h> // access to python config
4 #include <errno.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8
9 #ifndef I2C_SLAVE_FORCE
10 #define I2C_SLAVE_FORCE 0x0706
11 #endif
12
13 #if HAVE_DVB_API_VERSION < 3
14 #include <ost/frontend.h>
15 #include <ost/sec.h>
16 #define QAM_AUTO                                (Modulation)6
17 #define TRANSMISSION_MODE_AUTO  (TransmitMode)2
18 #define BANDWIDTH_AUTO                  (BandWidth)3
19 #define GUARD_INTERVAL_AUTO             (GuardInterval)4
20 #define HIERARCHY_AUTO                  (Hierarchy)4
21 #define parm_frequency parm.Frequency
22 #define parm_inversion parm.Inversion
23 #define parm_u_qpsk_symbol_rate parm.u.qpsk.SymbolRate
24 #define parm_u_qpsk_fec_inner parm.u.qpsk.FEC_inner
25 #define parm_u_qam_symbol_rate parm.u.qam.SymbolRate
26 #define parm_u_qam_fec_inner parm.u.qam.FEC_inner
27 #define parm_u_qam_modulation parm.u.qam.QAM
28 #define parm_u_ofdm_bandwidth parm.u.ofdm.bandWidth
29 #define parm_u_ofdm_code_rate_LP parm.u.ofdm.LP_CodeRate
30 #define parm_u_ofdm_code_rate_HP parm.u.ofdm.HP_CodeRate
31 #define parm_u_ofdm_constellation parm.u.ofdm.Constellation
32 #define parm_u_ofdm_transmission_mode parm.u.ofdm.TransmissionMode
33 #define parm_u_ofdm_guard_interval parm.u.ofdm.guardInterval
34 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.HierarchyInformation
35 #else
36 #include <linux/dvb/frontend.h>
37 #define parm_frequency parm.frequency
38 #define parm_inversion parm.inversion
39 #define parm_u_qpsk_symbol_rate parm.u.qpsk.symbol_rate
40 #define parm_u_qpsk_fec_inner parm.u.qpsk.fec_inner
41 #define parm_u_qam_symbol_rate parm.u.qam.symbol_rate
42 #define parm_u_qam_fec_inner parm.u.qam.fec_inner
43 #define parm_u_qam_modulation parm.u.qam.modulation
44 #define parm_u_ofdm_bandwidth parm.u.ofdm.bandwidth
45 #define parm_u_ofdm_code_rate_LP parm.u.ofdm.code_rate_LP
46 #define parm_u_ofdm_code_rate_HP parm.u.ofdm.code_rate_HP
47 #define parm_u_ofdm_constellation parm.u.ofdm.constellation
48 #define parm_u_ofdm_transmission_mode parm.u.ofdm.transmission_mode
49 #define parm_u_ofdm_guard_interval parm.u.ofdm.guard_interval
50 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.hierarchy_information
51 #ifdef FEC_9_10
52         #warning "FEC_9_10 already exist in dvb api ... it seems it is now ready for DVB-S2"
53 #else
54         #define FEC_S2_QPSK_1_2 (fe_code_rate_t)(FEC_AUTO+1)
55         #define FEC_S2_QPSK_2_3 (fe_code_rate_t)(FEC_S2_QPSK_1_2+1)
56         #define FEC_S2_QPSK_3_4 (fe_code_rate_t)(FEC_S2_QPSK_2_3+1)
57         #define FEC_S2_QPSK_5_6 (fe_code_rate_t)(FEC_S2_QPSK_3_4+1)
58         #define FEC_S2_QPSK_7_8 (fe_code_rate_t)(FEC_S2_QPSK_5_6+1)
59         #define FEC_S2_QPSK_8_9 (fe_code_rate_t)(FEC_S2_QPSK_7_8+1)
60         #define FEC_S2_QPSK_3_5 (fe_code_rate_t)(FEC_S2_QPSK_8_9+1)
61         #define FEC_S2_QPSK_4_5 (fe_code_rate_t)(FEC_S2_QPSK_3_5+1)
62         #define FEC_S2_QPSK_9_10 (fe_code_rate_t)(FEC_S2_QPSK_4_5+1)
63         #define FEC_S2_8PSK_1_2 (fe_code_rate_t)(FEC_S2_QPSK_9_10+1)
64         #define FEC_S2_8PSK_2_3 (fe_code_rate_t)(FEC_S2_8PSK_1_2+1)
65         #define FEC_S2_8PSK_3_4 (fe_code_rate_t)(FEC_S2_8PSK_2_3+1)
66         #define FEC_S2_8PSK_5_6 (fe_code_rate_t)(FEC_S2_8PSK_3_4+1)
67         #define FEC_S2_8PSK_7_8 (fe_code_rate_t)(FEC_S2_8PSK_5_6+1)
68         #define FEC_S2_8PSK_8_9 (fe_code_rate_t)(FEC_S2_8PSK_7_8+1)
69         #define FEC_S2_8PSK_3_5 (fe_code_rate_t)(FEC_S2_8PSK_8_9+1)
70         #define FEC_S2_8PSK_4_5 (fe_code_rate_t)(FEC_S2_8PSK_3_5+1)
71         #define FEC_S2_8PSK_9_10 (fe_code_rate_t)(FEC_S2_8PSK_4_5+1)
72 #endif
73 #endif
74
75 #include <dvbsi++/satellite_delivery_system_descriptor.h>
76 #include <dvbsi++/cable_delivery_system_descriptor.h>
77 #include <dvbsi++/terrestrial_delivery_system_descriptor.h>
78
79 void eDVBDiseqcCommand::setCommandString(const char *str)
80 {
81         if (!str)
82                 return;
83         len=0;
84         int slen = strlen(str);
85         if (slen % 2)
86         {
87                 eDebug("invalid diseqc command string length (not 2 byte aligned)");
88                 return;
89         }
90         if (slen > MAX_DISEQC_LENGTH*2)
91         {
92                 eDebug("invalid diseqc command string length (string is to long)");
93                 return;
94         }
95         unsigned char val=0;
96         for (int i=0; i < slen; ++i)
97         {
98                 unsigned char c = str[i];
99                 switch(c)
100                 {
101                         case '0' ... '9': c-=48; break;
102                         case 'a' ... 'f': c-=87; break;
103                         case 'A' ... 'F': c-=55; break;
104                         default:
105                                 eDebug("invalid character in hex string..ignore complete diseqc command !");
106                                 return;
107                 }
108                 if ( i % 2 )
109                 {
110                         val |= c;
111                         data[i/2] = val;
112                 }
113                 else
114                         val = c << 4;
115         }
116         len = slen/2;
117 }
118
119 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
120 {
121         frequency    = descriptor.getFrequency() * 10;
122         symbol_rate  = descriptor.getSymbolRate() * 100;
123         polarisation = descriptor.getPolarization();
124         fec = descriptor.getFecInner();
125         if ( fec != FEC::fNone && fec > FEC::f9_10 )
126                 fec = FEC::fAuto;
127         inversion = Inversion::Unknown;
128         orbital_position  = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
129         orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
130         orbital_position += ((descriptor.getOrbitalPosition() >> 4) & 0xF) * 10;
131         orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
132         if (orbital_position && (!descriptor.getWestEastFlag()))
133                 orbital_position = 3600 - orbital_position;
134         system = descriptor.getModulationSystem();
135         modulation = descriptor.getModulation();
136         if (system == System::DVB_S && modulation == Modulation::M8PSK)
137         {
138                 eDebug("satellite_delivery_descriptor non valid modulation type.. force QPSK");
139                 modulation=QPSK;
140         }
141         roll_off = descriptor.getRollOff();
142         if (system == System::DVB_S2)
143         {
144                 eDebug("SAT DVB-S2 freq %d, %s, pos %d, sr %d, fec %d, modulation %d, roll_off %d",
145                         frequency,
146                         polarisation ? "hor" : "vert",
147                         orbital_position,
148                         symbol_rate, fec,
149                         modulation,
150                         roll_off);
151         }
152         else
153         {
154                 eDebug("SAT DVB-S freq %d, %s, pos %d, sr %d, fec %d",
155                         frequency,
156                         polarisation ? "hor" : "vert",
157                         orbital_position,
158                         symbol_rate, fec);
159         }
160 }
161
162 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
163 {
164         frequency = descriptor.getFrequency() / 10;
165         symbol_rate = descriptor.getSymbolRate() * 100;
166         fec_inner = descriptor.getFecInner();
167         if ( fec_inner == 0xF )
168                 fec_inner = FEC::fNone;
169         modulation = descriptor.getModulation();
170         if ( modulation > 0x5 )
171                 modulation = Modulation::Auto;
172         inversion = Inversion::Unknown;
173         eDebug("Cable freq %d, mod %d, sr %d, fec %d",
174                 frequency,
175                 modulation, symbol_rate, fec_inner);
176 }
177
178 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor &descriptor)
179 {
180         frequency = descriptor.getCentreFrequency() * 10;
181         bandwidth = descriptor.getBandwidth();
182         if ( bandwidth > 2 ) // 5Mhz forced to auto
183                 bandwidth = Bandwidth::BwAuto;
184         code_rate_HP = descriptor.getCodeRateHpStream();
185         if (code_rate_HP > 4)
186                 code_rate_HP = FEC::fAuto;
187         code_rate_LP = descriptor.getCodeRateLpStream();
188         if (code_rate_LP > 4)
189                 code_rate_LP = FEC::fAuto;
190         transmission_mode = descriptor.getTransmissionMode();
191         if (transmission_mode > 1) // TM4k forced to auto
192                 transmission_mode = TransmissionMode::TMAuto;
193         guard_interval = descriptor.getGuardInterval();
194         if (guard_interval > 3)
195                 guard_interval = GuardInterval::GI_Auto;
196         hierarchy = descriptor.getHierarchyInformation()&3;
197         modulation = descriptor.getConstellation();
198         if (modulation > 2)
199                 modulation = Modulation::Auto;
200         inversion = Inversion::Unknown;
201         eDebug("Terr freq %d, bw %d, cr_hp %d, cr_lp %d, tm_mode %d, guard %d, hierarchy %d, const %d",
202                 frequency, bandwidth, code_rate_HP, code_rate_LP, transmission_mode,
203                 guard_interval, hierarchy, modulation);
204 }
205
206 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
207 {
208 }
209
210 DEFINE_REF(eDVBFrontendParameters);
211
212 RESULT eDVBFrontendParameters::getSystem(int &t) const
213 {
214         if (m_type == -1)
215                 return -1;
216         t = m_type;
217         return 0;
218 }
219
220 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
221 {
222         if (m_type != iDVBFrontend::feSatellite)
223                 return -1;
224         p = sat;
225         return 0;
226 }
227
228 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
229 {
230         if (m_type != iDVBFrontend::feCable)
231                 return -1;
232         p = cable;
233         return 0;
234 }
235
236 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
237 {
238         if (m_type != iDVBFrontend::feTerrestrial)
239                 return -1;
240         p = terrestrial;
241         return 0;
242 }
243
244 RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p, bool no_rotor_command_on_tune)
245 {
246         sat = p;
247         sat.no_rotor_command_on_tune = no_rotor_command_on_tune;
248         m_type = iDVBFrontend::feSatellite;
249         return 0;
250 }
251
252 RESULT eDVBFrontendParameters::setDVBC(const eDVBFrontendParametersCable &p)
253 {
254         cable = p;
255         m_type = iDVBFrontend::feCable;
256         return 0;
257 }
258
259 RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &p)
260 {
261         terrestrial = p;
262         m_type = iDVBFrontend::feTerrestrial;
263         return 0;
264 }
265
266 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff, bool exact) const
267 {
268         if (!parm)
269                 return -1;
270         int type;
271         if (parm->getSystem(type))
272                 return -1;
273         if (type != m_type)
274         {
275                 diff = 1<<30; // big difference
276                 return 0;
277         }
278
279         switch (type)
280         {
281         case iDVBFrontend::feSatellite:
282         {
283                 eDVBFrontendParametersSatellite osat;
284                 if (parm->getDVBS(osat))
285                         return -2;
286
287                 if (sat.orbital_position != osat.orbital_position)
288                         diff = 1<<29;
289                 else if (sat.polarisation != osat.polarisation)
290                         diff = 1<<28;
291                 else if (exact && sat.fec != osat.fec && sat.fec != eDVBFrontendParametersSatellite::FEC::fAuto && osat.fec != eDVBFrontendParametersSatellite::FEC::fAuto)
292                         diff = 1<<27;
293                 else if (exact && sat.modulation != osat.modulation && sat.modulation != eDVBFrontendParametersSatellite::Modulation::Auto && osat.modulation != eDVBFrontendParametersSatellite::Modulation::Auto)
294                         diff = 1<<27;
295                 else
296                 {
297                         diff = abs(sat.frequency - osat.frequency);
298                         diff += abs(sat.symbol_rate - osat.symbol_rate);
299                 }
300                 return 0;
301         }
302         case iDVBFrontend::feCable:
303                 eDVBFrontendParametersCable ocable;
304                 if (parm->getDVBC(ocable))
305                         return -2;
306
307                 if (exact && cable.modulation != ocable.modulation
308                         && cable.modulation != eDVBFrontendParametersCable::Modulation::Auto
309                         && ocable.modulation != eDVBFrontendParametersCable::Modulation::Auto)
310                         diff = 1 << 29;
311                 else if (exact && cable.fec_inner != ocable.fec_inner && cable.fec_inner != eDVBFrontendParametersCable::FEC::fAuto && ocable.fec_inner != eDVBFrontendParametersCable::FEC::fAuto)
312                         diff = 1 << 27;
313                 else
314                 {
315                         diff = abs(cable.frequency - ocable.frequency);
316                         diff += abs(cable.symbol_rate - ocable.symbol_rate);
317                 }
318                 return 0;
319         case iDVBFrontend::feTerrestrial:
320                 eDVBFrontendParametersTerrestrial oterrestrial;
321                 if (parm->getDVBT(oterrestrial))
322                         return -2;
323
324
325                 if (exact && oterrestrial.bandwidth != terrestrial.bandwidth &&
326                         oterrestrial.bandwidth != eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto &&
327                         terrestrial.bandwidth != eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto)
328                         diff = 1 << 30;
329                 else if (exact && oterrestrial.modulation != terrestrial.modulation &&
330                         oterrestrial.modulation != eDVBFrontendParametersTerrestrial::Modulation::Auto &&
331                         terrestrial.modulation != eDVBFrontendParametersTerrestrial::Modulation::Auto)
332                         diff = 1 << 30;
333                 else if (exact && oterrestrial.transmission_mode != terrestrial.transmission_mode &&
334                         oterrestrial.transmission_mode != eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto &&
335                         terrestrial.transmission_mode != eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto)
336                         diff = 1 << 30;
337                 else if (exact && oterrestrial.guard_interval != terrestrial.guard_interval &&
338                         oterrestrial.guard_interval != eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto &&
339                         terrestrial.guard_interval != eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto)
340                         diff = 1 << 30;
341                 else if (exact && oterrestrial.hierarchy != terrestrial.hierarchy &&
342                         oterrestrial.hierarchy != eDVBFrontendParametersTerrestrial::Hierarchy::HAuto &&
343                         terrestrial.hierarchy != eDVBFrontendParametersTerrestrial::Hierarchy::HAuto)
344                         diff = 1 << 30;
345                 else if (exact && oterrestrial.code_rate_LP != terrestrial.code_rate_LP &&
346                         oterrestrial.code_rate_LP != eDVBFrontendParametersTerrestrial::FEC::fAuto &&
347                         terrestrial.code_rate_LP != eDVBFrontendParametersTerrestrial::FEC::fAuto)
348                         diff = 1 << 30;
349                 else if (exact && oterrestrial.code_rate_HP != terrestrial.code_rate_HP &&
350                         oterrestrial.code_rate_HP != eDVBFrontendParametersTerrestrial::FEC::fAuto &&
351                         terrestrial.code_rate_HP != eDVBFrontendParametersTerrestrial::FEC::fAuto)
352                         diff = 1 << 30;
353                 else
354                         diff = abs(terrestrial.frequency - oterrestrial.frequency);
355                 return 0;
356         default:
357                 return -1;
358         }
359         return 0;
360 }
361
362 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
363 {
364         switch (m_type)
365         {
366         case iDVBFrontend::feSatellite:
367         {
368                 hash = (sat.orbital_position << 16);
369                 hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
370                 return 0;
371         }
372         case iDVBFrontend::feCable:
373                 hash = 0xFFFF0000;
374                 return 0;
375         case iDVBFrontend::feTerrestrial:
376                 hash = 0xEEEE0000;
377                 return 0;
378         default:
379                 return -1;
380         }
381 }
382
383 DEFINE_REF(eDVBFrontend);
384
385 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
386         :m_type(-1), m_fe(fe), m_fd(-1), m_sn(0), m_timeout(0), m_tuneTimer(0)
387 #if HAVE_DVB_API_VERSION < 3
388         ,m_secfd(-1)
389 #endif
390 {
391 #if HAVE_DVB_API_VERSION < 3
392         sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
393         sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
394 #else
395         sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
396 #endif
397         m_timeout = new eTimer(eApp);
398         CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
399
400         m_tuneTimer = new eTimer(eApp);
401         CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
402
403         for (int i=0; i<eDVBFrontend::NUM_DATA_ENTRIES; ++i)
404                 m_data[i] = -1;
405
406         m_idleInputpower[0]=m_idleInputpower[1]=0;
407
408         ok = !openFrontend();
409         closeFrontend();
410 }
411
412 int eDVBFrontend::openFrontend()
413 {
414         if (m_sn)
415                 return -1;  // already opened
416
417         m_state=0;
418         m_tuning=0;
419
420 #if HAVE_DVB_API_VERSION < 3
421         if (m_secfd < 0)
422         {
423                 m_secfd = ::open(m_sec_filename, O_RDWR);
424                 if (m_secfd < 0)
425                 {
426                         eWarning("failed! (%s) %m", m_sec_filename);
427                         return -1;
428                 }
429         }
430         else
431                 eWarning("sec %d already opened", m_fe);
432         FrontendInfo fe_info;
433 #else
434         dvb_frontend_info fe_info;
435 #endif
436         eDebug("opening frontend %d", m_fe);
437         if (m_fd < 0)
438         {
439                 m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
440                 if (m_fd < 0)
441                 {
442                         eWarning("failed! (%s) %m", m_filename);
443 #if HAVE_DVB_API_VERSION < 3
444                         ::close(m_secfd);
445                         m_secfd=-1;
446 #endif
447                         return -1;
448                 }
449         }
450         else
451                 eWarning("frontend %d already opened", m_fe);
452         if (m_type == -1)
453         {
454                 if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
455                 {
456                         eWarning("ioctl FE_GET_INFO failed");
457                         ::close(m_fd);
458                         m_fd = -1;
459 #if HAVE_DVB_API_VERSION < 3
460                         ::close(m_secfd);
461                         m_secfd=-1;
462 #endif
463                         return -1;
464                 }
465
466                 switch (fe_info.type)
467                 {
468                 case FE_QPSK:
469                         m_type = iDVBFrontend::feSatellite;
470                         break;
471                 case FE_QAM:
472                         m_type = iDVBFrontend::feCable;
473                         break;
474                 case FE_OFDM:
475                         m_type = iDVBFrontend::feTerrestrial;
476                         break;
477                 default:
478                         eWarning("unknown frontend type.");
479                         ::close(m_fd);
480                         m_fd = -1;
481 #if HAVE_DVB_API_VERSION < 3
482                         ::close(m_secfd);
483                         m_secfd=-1;
484 #endif
485                         return -1;
486                 }
487                 eDebug("detected %s frontend", "satellite\0cable\0    terrestrial"+fe_info.type*10);
488         }
489
490         setTone(iDVBFrontend::toneOff);
491         setVoltage(iDVBFrontend::voltageOff);
492
493         m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
494         CONNECT(m_sn->activated, eDVBFrontend::feEvent);
495
496         return 0;
497 }
498
499 int eDVBFrontend::closeFrontend()
500 {
501         eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[LINKED_NEXT_PTR];
502         while (linked_fe != (eDVBRegisteredFrontend*)-1)
503         {
504                 if (linked_fe->m_inuse)
505                 {
506                         eDebug("dont close frontend %d until the linked frontend %d is still in use",
507                                 m_fe, linked_fe->m_frontend->getID());
508                         return -1;
509                 }
510                 linked_fe->m_frontend->getData(LINKED_NEXT_PTR, (int&)linked_fe);
511         }
512         if (m_fd >= 0)
513         {
514                 eDebug("close frontend %d", m_fe);
515                 m_tuneTimer->stop();
516                 setTone(iDVBFrontend::toneOff);
517                 setVoltage(iDVBFrontend::voltageOff);
518                 if (m_sec)
519                         m_sec->setRotorMoving(false);
520                 if (!::close(m_fd))
521                         m_fd=-1;
522                 else
523                         eWarning("couldnt close frontend %d", m_fe);
524                 m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1;
525         }
526 #if HAVE_DVB_API_VERSION < 3
527         if (m_secfd >= 0)
528         {
529                 if (!::close(m_secfd))
530                         m_secfd=-1;
531                 else
532                         eWarning("couldnt close sec %d", m_fe);
533         }
534 #endif
535         delete m_sn;
536         m_sn=0;
537
538         return 0;
539 }
540
541 eDVBFrontend::~eDVBFrontend()
542 {
543         closeFrontend();
544         delete m_timeout;
545         delete m_tuneTimer;
546 }
547
548 void eDVBFrontend::feEvent(int w)
549 {
550         while (1)
551         {
552 #if HAVE_DVB_API_VERSION < 3
553                 FrontendEvent event;
554 #else
555                 dvb_frontend_event event;
556 #endif
557                 int res;
558                 int state;
559                 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
560
561                 if (res && (errno == EAGAIN))
562                         break;
563
564                 if (res)
565                 {
566                         eWarning("FE_GET_EVENT failed! %m");
567                         return;
568                 }
569
570                 if (w < 0)
571                         continue;
572
573 #if HAVE_DVB_API_VERSION < 3
574                 if (event.type == FE_COMPLETION_EV)
575 #else
576                 eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
577                 if (event.status & FE_HAS_LOCK)
578 #endif
579                 {
580                         state = stateLock;
581                 } else
582                 {
583                         if (m_tuning)
584                                 state = stateTuning;
585                         else
586                         {
587                                 eDebug("stateLostLock");
588                                 state = stateLostLock;
589                                 m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1; // reset diseqc
590                         }
591                 }
592                 if (m_state != state)
593                 {
594                         m_state = state;
595                         m_stateChanged(this);
596                 }
597         }
598 }
599
600 void eDVBFrontend::timeout()
601 {
602         m_tuning = 0;
603         if (m_state == stateTuning)
604         {
605                 m_state = stateFailed;
606                 m_stateChanged(this);
607         }
608 }
609
610 int eDVBFrontend::readFrontendData(int type)
611 {
612         switch(type)
613         {
614                 case bitErrorRate:
615                 {
616                         uint32_t ber=0;
617                         if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
618                                 eDebug("FE_READ_BER failed (%m)");
619                         return ber;
620                 }
621                 case signalPower:
622                 {
623                         uint16_t snr=0;
624                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
625                                 eDebug("FE_READ_SNR failed (%m)");
626                         return snr;
627                 }
628                 case signalQuality:
629                 {
630                         uint16_t strength=0;
631                         if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
632                                 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
633                         return strength;
634                 }
635                 case locked:
636                 {
637 #if HAVE_DVB_API_VERSION < 3
638                         FrontendStatus status=0;
639 #else
640                         fe_status_t status;
641 #endif
642                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
643                                 eDebug("FE_READ_STATUS failed (%m)");
644                         return !!(status&FE_HAS_LOCK);
645                 }
646                 case synced:
647                 {
648 #if HAVE_DVB_API_VERSION < 3
649                         FrontendStatus status=0;
650 #else
651                         fe_status_t status;
652 #endif
653                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
654                                 eDebug("FE_READ_STATUS failed (%m)");
655                         return !!(status&FE_HAS_SYNC);
656                 }
657                 case frontendNumber:
658                         return m_fe;
659         }
660         return 0;
661 }
662
663 void PutToDict(ePyObject &dict, const char*key, long value)
664 {
665         ePyObject item = PyInt_FromLong(value);
666         if (item)
667         {
668                 if (PyDict_SetItemString(dict, key, item))
669                         eDebug("put %s to dict failed", key);
670                 Py_DECREF(item);
671         }
672         else
673                 eDebug("could not create PyObject for %s", key);
674 }
675
676 void PutToDict(ePyObject &dict, const char*key, const char *value)
677 {
678         ePyObject item = PyString_FromString(value);
679         if (item)
680         {
681                 if (PyDict_SetItemString(dict, key, item))
682                         eDebug("put %s to dict failed", key);
683                 Py_DECREF(item);
684         }
685         else
686                 eDebug("could not create PyObject for %s", key);
687 }
688
689 void fillDictWithSatelliteData(ePyObject dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
690 {
691         int freq_offset=0;
692         int csw=0;
693         const char *tmp=0;
694         fe->getData(eDVBFrontend::CSW, csw);
695         fe->getData(eDVBFrontend::FREQ_OFFSET, freq_offset);
696         int frequency = parm_frequency + freq_offset;
697         PutToDict(dict, "frequency", frequency);
698         PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
699         switch(parm_u_qpsk_fec_inner)
700         {
701         case FEC_1_2:
702                 tmp = "FEC_1_2";
703                 break;
704         case FEC_2_3:
705                 tmp = "FEC_2_3";
706                 break;
707         case FEC_3_4:
708                 tmp = "FEC_3_4";
709                 break;
710         case FEC_5_6:
711                 tmp = "FEC_5_6";
712                 break;
713         case FEC_7_8:
714                 tmp = "FEC_7_8";
715                 break;
716         case FEC_NONE:
717                 tmp = "FEC_NONE";
718         default:
719         case FEC_AUTO:
720                 tmp = "FEC_AUTO";
721                 break;
722 #if HAVE_DVB_API_VERSION >=3
723         case FEC_S2_8PSK_1_2:
724         case FEC_S2_QPSK_1_2:
725                 tmp = "FEC_1_2";
726                 break;
727         case FEC_S2_8PSK_2_3:
728         case FEC_S2_QPSK_2_3:
729                 tmp = "FEC_2_3";
730                 break;
731         case FEC_S2_8PSK_3_4:
732         case FEC_S2_QPSK_3_4:
733                 tmp = "FEC_3_4";
734                 break;
735         case FEC_S2_8PSK_5_6:
736         case FEC_S2_QPSK_5_6:
737                 tmp = "FEC_5_6";
738                 break;
739         case FEC_S2_8PSK_7_8:
740         case FEC_S2_QPSK_7_8:
741                 tmp = "FEC_7_8";
742                 break;
743         case FEC_S2_8PSK_8_9:
744         case FEC_S2_QPSK_8_9:
745                 tmp = "FEC_8_9";
746                 break;
747         case FEC_S2_8PSK_3_5:
748         case FEC_S2_QPSK_3_5:
749                 tmp = "FEC_3_5";
750                 break;
751         case FEC_S2_8PSK_4_5:
752         case FEC_S2_QPSK_4_5:
753                 tmp = "FEC_4_5";
754                 break;
755         case FEC_S2_8PSK_9_10:
756         case FEC_S2_QPSK_9_10:
757                 tmp = "FEC_9_10";
758                 break;
759 #endif
760         }
761 #if HAVE_DVB_API_VERSION >=3
762         PutToDict(dict, "modulation",
763                 parm_u_qpsk_fec_inner > FEC_S2_QPSK_9_10 ? "8PSK": "QPSK" );
764 #else
765         PutToDict(dict, "modulation", "QPSK" );
766 #endif
767         PutToDict(dict, "fec_inner", tmp);
768         tmp = parm_u_qpsk_fec_inner > FEC_AUTO ?
769                 "DVB-S2" : "DVB-S";
770         PutToDict(dict, "system", tmp);
771 }
772
773 void fillDictWithCableData(ePyObject dict, const FRONTENDPARAMETERS &parm)
774 {
775         const char *tmp=0;
776         PutToDict(dict, "frequency", parm_frequency/1000);
777         PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
778         switch(parm_u_qam_fec_inner)
779         {
780         case FEC_NONE:
781                 tmp = "FEC_NONE";
782                 break;
783         case FEC_1_2:
784                 tmp = "FEC_1_2";
785                 break;
786         case FEC_2_3:
787                 tmp = "FEC_2_3";
788                 break;
789         case FEC_3_4:
790                 tmp = "FEC_3_4";
791                 break;
792         case FEC_5_6:
793                 tmp = "FEC_5_6";
794                 break;
795         case FEC_7_8:
796                 tmp = "FEC_7_8";
797                 break;
798 #if HAVE_DVB_API_VERSION >= 3
799         case FEC_8_9:
800                 tmp = "FEC_8_9";
801                 break;
802 #endif
803         default:
804         case FEC_AUTO:
805                 tmp = "FEC_AUTO";
806                 break;
807         }
808         PutToDict(dict, "fec_inner", tmp);
809         switch(parm_u_qam_modulation)
810         {
811         case QAM_16:
812                 tmp = "QAM_16";
813                 break;
814         case QAM_32:
815                 tmp = "QAM_32";
816                 break;
817         case QAM_64:
818                 tmp = "QAM_64";
819                 break;
820         case QAM_128:
821                 tmp = "QAM_128";
822                 break;
823         case QAM_256:
824                 tmp = "QAM_256";
825                 break;
826         default:
827         case QAM_AUTO:
828                 tmp = "QAM_AUTO";
829                 break;
830         }
831         PutToDict(dict, "modulation", tmp);
832 }
833
834 void fillDictWithTerrestrialData(ePyObject dict, const FRONTENDPARAMETERS &parm)
835 {
836         const char *tmp=0;
837         PutToDict(dict, "frequency", parm_frequency);
838         switch (parm_u_ofdm_bandwidth)
839         {
840         case BANDWIDTH_8_MHZ:
841                 tmp = "BANDWIDTH_8_MHZ";
842                 break;
843         case BANDWIDTH_7_MHZ:
844                 tmp = "BANDWIDTH_7_MHZ";
845                 break;
846         case BANDWIDTH_6_MHZ:
847                 tmp = "BANDWIDTH_6_MHZ";
848                 break;
849         default:
850         case BANDWIDTH_AUTO:
851                 tmp = "BANDWIDTH_AUTO";
852                 break;
853         }
854         PutToDict(dict, "bandwidth", tmp);
855         switch (parm_u_ofdm_code_rate_LP)
856         {
857         case FEC_1_2:
858                 tmp = "FEC_1_2";
859                 break;
860         case FEC_2_3:
861                 tmp = "FEC_2_3";
862                 break;
863         case FEC_3_4:
864                 tmp = "FEC_3_4";
865                 break;
866         case FEC_5_6:
867                 tmp = "FEC_5_6";
868                 break;
869         case FEC_7_8:
870                 tmp = "FEC_7_8";
871                 break;
872         default:
873         case FEC_AUTO:
874                 tmp = "FEC_AUTO";
875                 break;
876         }
877         PutToDict(dict, "code_rate_lp", tmp);
878         switch (parm_u_ofdm_code_rate_HP)
879         {
880         case FEC_1_2:
881                 tmp = "FEC_1_2";
882                 break;
883         case FEC_2_3:
884                 tmp = "FEC_2_3";
885                 break;
886         case FEC_3_4:
887                 tmp = "FEC_3_4";
888                 break;
889         case FEC_5_6:
890                 tmp = "FEC_5_6";
891                 break;
892         case FEC_7_8:
893                 tmp = "FEC_7_8";
894                 break;
895         default:
896         case FEC_AUTO:
897                 tmp = "FEC_AUTO";
898                 break;
899         }
900         PutToDict(dict, "code_rate_hp", tmp);
901         switch (parm_u_ofdm_constellation)
902         {
903         case QPSK:
904                 tmp = "QPSK";
905                 break;
906         case QAM_16:
907                 tmp = "QAM_16";
908                 break;
909         case QAM_64:
910                 tmp = "QAM_64";
911                 break;
912         default:
913         case QAM_AUTO:
914                 tmp = "QAM_AUTO";
915                 break;
916         }
917         PutToDict(dict, "constellation", tmp);
918         switch (parm_u_ofdm_transmission_mode)
919         {
920         case TRANSMISSION_MODE_2K:
921                 tmp = "TRANSMISSION_MODE_2K";
922                 break;
923         case TRANSMISSION_MODE_8K:
924                 tmp = "TRANSMISSION_MODE_8K";
925                 break;
926         default:
927         case TRANSMISSION_MODE_AUTO:
928                 tmp = "TRANSMISSION_MODE_AUTO";
929                 break;
930         }
931         PutToDict(dict, "transmission_mode", tmp);
932         switch (parm_u_ofdm_guard_interval)
933         {
934                 case GUARD_INTERVAL_1_32:
935                         tmp = "GUARD_INTERVAL_1_32";
936                         break;
937                 case GUARD_INTERVAL_1_16:
938                         tmp = "GUARD_INTERVAL_1_16";
939                         break;
940                 case GUARD_INTERVAL_1_8:
941                         tmp = "GUARD_INTERVAL_1_8";
942                         break;
943                 case GUARD_INTERVAL_1_4:
944                         tmp = "GUARD_INTERVAL_1_4";
945                         break;
946                 default:
947                 case GUARD_INTERVAL_AUTO:
948                         tmp = "GUARD_INTERVAL_AUTO";
949                         break;
950         }
951         PutToDict(dict, "guard_interval", tmp);
952         switch (parm_u_ofdm_hierarchy_information)
953         {
954                 case HIERARCHY_NONE:
955                         tmp = "HIERARCHY_NONE";
956                         break;
957                 case HIERARCHY_1:
958                         tmp = "HIERARCHY_1";
959                         break;
960                 case HIERARCHY_2:
961                         tmp = "HIERARCHY_2";
962                         break;
963                 case HIERARCHY_4:
964                         tmp = "HIERARCHY_4";
965                         break;
966                 default:
967                 case HIERARCHY_AUTO:
968                         tmp = "HIERARCHY_AUTO";
969                         break;
970         }
971         PutToDict(dict, "hierarchy_information", tmp);
972 }
973
974 PyObject *eDVBFrontend::readTransponderData(bool original)
975 {
976         ePyObject ret=PyDict_New();
977
978         if (ret)
979         {
980                 bool read=m_fd != -1;
981                 const char *tmp=0;
982
983                 PutToDict(ret, "tuner_number", m_fe);
984
985                 switch(m_type)
986                 {
987                         case feSatellite:
988                                 tmp = "DVB-S";
989                                 break;
990                         case feCable:
991                                 tmp = "DVB-C";
992                                 break;
993                         case feTerrestrial:
994                                 tmp = "DVB-T";
995                                 break;
996                         default:
997                                 tmp = "UNKNOWN";
998                                 read=false;
999                                 break;
1000                 }
1001                 PutToDict(ret, "tuner_type", tmp);
1002
1003                 if (read)
1004                 {
1005                         FRONTENDPARAMETERS front;
1006
1007                         tmp = "UNKNOWN";
1008                         switch(m_state)
1009                         {
1010                                 case stateIdle:
1011                                         tmp="IDLE";
1012                                         break;
1013                                 case stateTuning:
1014                                         tmp="TUNING";
1015                                         break;
1016                                 case stateFailed:
1017                                         tmp="FAILED";
1018                                         break;
1019                                 case stateLock:
1020                                         tmp="LOCKED";
1021                                         break;
1022                                 case stateLostLock:
1023                                         tmp="LOSTLOCK";
1024                                         break;
1025                                 default:
1026                                         break;
1027                         }
1028                         PutToDict(ret, "tuner_state", tmp);
1029
1030                         PutToDict(ret, "tuner_locked", readFrontendData(locked));
1031                         PutToDict(ret, "tuner_synced", readFrontendData(synced));
1032                         PutToDict(ret, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
1033                         PutToDict(ret, "tuner_signal_power", readFrontendData(signalPower));
1034                         PutToDict(ret, "tuner_signal_quality", readFrontendData(signalQuality));
1035
1036                         if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
1037                                 eDebug("FE_GET_FRONTEND (%m)");
1038                         else
1039                         {
1040                                 const FRONTENDPARAMETERS &parm = original ? this->parm : front;
1041                                 tmp = "INVERSION_AUTO";
1042                                 switch(parm_inversion)
1043                                 {
1044                                         case INVERSION_ON:
1045                                                 tmp = "INVERSION_ON";
1046                                                 break;
1047                                         case INVERSION_OFF:
1048                                                 tmp = "INVERSION_OFF";
1049                                                 break;
1050                                         default:
1051                                                 break;
1052                                 }
1053                                 if (tmp)
1054                                         PutToDict(ret, "inversion", tmp);
1055
1056                                 switch(m_type)
1057                                 {
1058                                         case feSatellite:
1059                                                 fillDictWithSatelliteData(ret, original?parm:front, this);
1060                                                 break;
1061                                         case feCable:
1062                                                 fillDictWithCableData(ret, original?parm:front);
1063                                                 break;
1064                                         case feTerrestrial:
1065                                                 fillDictWithTerrestrialData(ret, original?parm:front);
1066                                                 break;
1067                                 }
1068                         }
1069                 }
1070         }
1071         else
1072         {
1073                 Py_INCREF(Py_None);
1074                 ret = Py_None;
1075         }
1076         return ret;
1077 }
1078
1079 #ifndef FP_IOCTL_GET_ID
1080 #define FP_IOCTL_GET_ID 0
1081 #endif
1082 int eDVBFrontend::readInputpower()
1083 {
1084         int power=m_fe;  // this is needed for read inputpower from the correct tuner !
1085
1086         // open front prozessor
1087         int fp=::open("/dev/dbox/fp0", O_RDWR);
1088         if (fp < 0)
1089         {
1090                 eDebug("couldn't open fp");
1091                 return -1;
1092         }
1093         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
1094         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
1095         {
1096                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
1097                 return -1;
1098         }
1099         ::close(fp);
1100
1101         return power;
1102 }
1103
1104 bool eDVBFrontend::setSecSequencePos(int steps)
1105 {
1106         eDebug("set sequence pos %d", steps);
1107         if (!steps)
1108                 return false;
1109         while( steps > 0 )
1110         {
1111                 if (m_sec_sequence.current() != m_sec_sequence.end())
1112                         ++m_sec_sequence.current();
1113                 --steps;
1114         }
1115         while( steps < 0 )
1116         {
1117                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
1118                         --m_sec_sequence.current();
1119                 ++steps;
1120         }
1121         return true;
1122 }
1123
1124 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
1125 {
1126         int delay=0;
1127         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
1128         {
1129 //              eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
1130                 switch (m_sec_sequence.current()->cmd)
1131                 {
1132                         case eSecCommand::SLEEP:
1133                                 delay = m_sec_sequence.current()++->msec;
1134                                 eDebug("[SEC] sleep %dms", delay);
1135                                 break;
1136                         case eSecCommand::GOTO:
1137                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
1138                                         ++m_sec_sequence.current();
1139                                 break;
1140                         case eSecCommand::SET_VOLTAGE:
1141                         {
1142                                 int voltage = m_sec_sequence.current()++->voltage;
1143                                 eDebug("[SEC] setVoltage %d", voltage);
1144                                 setVoltage(voltage);
1145                                 break;
1146                         }
1147                         case eSecCommand::IF_VOLTAGE_GOTO:
1148                         {
1149                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1150                                 if ( compare.voltage == m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1151                                         break;
1152                                 ++m_sec_sequence.current();
1153                                 break;
1154                         }
1155                         case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1156                         {
1157                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1158                                 if ( compare.voltage != m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1159                                         break;
1160                                 ++m_sec_sequence.current();
1161                                 break;
1162                         }
1163                         case eSecCommand::IF_TONE_GOTO:
1164                         {
1165                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1166                                 if ( compare.tone == m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1167                                         break;
1168                                 ++m_sec_sequence.current();
1169                                 break;
1170                         }
1171                         case eSecCommand::IF_NOT_TONE_GOTO:
1172                         {
1173                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1174                                 if ( compare.tone != m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1175                                         break;
1176                                 ++m_sec_sequence.current();
1177                                 break;
1178                         }
1179                         case eSecCommand::SET_TONE:
1180                                 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1181                                 setTone(m_sec_sequence.current()++->tone);
1182                                 break;
1183                         case eSecCommand::SEND_DISEQC:
1184                                 sendDiseqc(m_sec_sequence.current()->diseqc);
1185                                 eDebugNoNewLine("[SEC] sendDiseqc: ");
1186                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1187                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1188                                 eDebug("");
1189                                 ++m_sec_sequence.current();
1190                                 break;
1191                         case eSecCommand::SEND_TONEBURST:
1192                                 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1193                                 sendToneburst(m_sec_sequence.current()++->toneburst);
1194                                 break;
1195                         case eSecCommand::SET_FRONTEND:
1196                                 eDebug("[SEC] setFrontend");
1197                                 setFrontend();
1198                                 ++m_sec_sequence.current();
1199                                 break;
1200                         case eSecCommand::START_TUNE_TIMEOUT:
1201                                 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1202                                 ++m_sec_sequence.current();
1203                                 break;
1204                         case eSecCommand::SET_TIMEOUT:
1205                                 m_timeoutCount = m_sec_sequence.current()++->val;
1206                                 eDebug("[SEC] set timeout %d", m_timeoutCount);
1207                                 break;
1208                         case eSecCommand::IF_TIMEOUT_GOTO:
1209                                 if (!m_timeoutCount)
1210                                 {
1211                                         eDebug("[SEC] rotor timout");
1212                                         m_sec->setRotorMoving(false);
1213                                         setSecSequencePos(m_sec_sequence.current()->steps);
1214                                 }
1215                                 else
1216                                         ++m_sec_sequence.current();
1217                                 break;
1218                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1219                         {
1220                                 int idx = m_sec_sequence.current()++->val;
1221                                 if ( idx == 0 || idx == 1 )
1222                                 {
1223                                         m_idleInputpower[idx] = readInputpower();
1224                                         eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1225                                 }
1226                                 else
1227                                         eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1228                                 break;
1229                         }
1230                         case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1231                         {
1232                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1233                                 int idx = compare.val;
1234                                 if ( idx == 0 || idx == 1 )
1235                                 {
1236                                         int idle = readInputpower();
1237                                         int diff = abs(idle-m_idleInputpower[idx]);
1238                                         if ( diff > 0)
1239                                         {
1240                                                 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1241                                                 setSecSequencePos(compare.steps);
1242                                                 break;
1243                                         }
1244                                 }
1245                                 ++m_sec_sequence.current();
1246                                 break;
1247                         }
1248                         case eSecCommand::IF_TUNER_LOCKED_GOTO:
1249                         {
1250                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1251                                 if (readFrontendData(locked))
1252                                 {
1253                                         eDebug("[SEC] locked step %d ok", cmd.okcount);
1254                                         ++cmd.okcount;
1255                                         if (cmd.okcount > 12)
1256                                         {
1257                                                 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1258                                                 setSecSequencePos(cmd.steps);
1259                                                 break;
1260                                         }
1261                                 }
1262                                 else
1263                                 {
1264                                         eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1265                                         --m_timeoutCount;
1266                                         if (!m_timeoutCount && m_retryCount > 0)
1267                                                 --m_retryCount;
1268                                         cmd.okcount=0;
1269                                 }
1270                                 ++m_sec_sequence.current();
1271                                 break;
1272                         }
1273                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1274                                 m_runningInputpower = readInputpower();
1275                                 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1276                                 ++m_sec_sequence.current();
1277                                 break;
1278                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1279                         {
1280                                 int idleInputpower = m_idleInputpower[ (m_data[CUR_VOLTAGE]&1) ? 0 : 1];
1281                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1282                                 const char *txt = cmd.direction ? "running" : "stopped";
1283                                 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1284                                         txt,
1285                                         m_runningInputpower,
1286                                         idleInputpower,
1287                                         cmd.deltaA);
1288                                 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1289                                         || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1290                                 {
1291                                         ++cmd.okcount;
1292                                         eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1293                                         if ( cmd.okcount > 6 )
1294                                         {
1295                                                 m_sec->setRotorMoving(cmd.direction);
1296                                                 eDebug("[SEC] rotor is %s", txt);
1297                                                 if (setSecSequencePos(cmd.steps))
1298                                                         break;
1299                                         }
1300                                 }
1301                                 else
1302                                 {
1303                                         eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1304                                         --m_timeoutCount;
1305                                         if (!m_timeoutCount && m_retryCount > 0)
1306                                                 --m_retryCount;
1307                                         cmd.okcount=0;
1308                                 }
1309                                 ++m_sec_sequence.current();
1310                                 break;
1311                         }
1312                         case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1313                                 if (m_data[ROTOR_CMD] != -1 && m_data[ROTOR_POS] != -1)
1314                                         setSecSequencePos(m_sec_sequence.current()->steps);
1315                                 else
1316                                         ++m_sec_sequence.current();
1317                                 break;
1318                         case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1319                                 m_data[ROTOR_CMD] = m_data[ROTOR_POS] = -1;
1320                                 eDebug("[SEC] invalidate current rotorparams");
1321                                 ++m_sec_sequence.current();
1322                                 break;
1323                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1324                                 m_data[ROTOR_CMD] = m_data[NEW_ROTOR_CMD];
1325                                 m_data[ROTOR_POS] = m_data[NEW_ROTOR_POS];
1326                                 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
1327                                 ++m_sec_sequence.current();
1328                                 break;
1329                         case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1330                                 m_retryCount = m_sec_sequence.current()++->val;
1331                                 eDebug("[SEC] set rotor retries %d", m_retryCount);
1332                                 break;
1333                         case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1334                                 if (!m_retryCount)
1335                                 {
1336                                         eDebug("[SEC] no more rotor retrys");
1337                                         setSecSequencePos(m_sec_sequence.current()->steps);
1338                                 }
1339                                 else
1340                                         ++m_sec_sequence.current();
1341                                 break;
1342                         case eSecCommand::SET_POWER_LIMITING_MODE:
1343                         {
1344                                 int fd = m_fe ?
1345                                         ::open("/dev/i2c/1", O_RDWR) :
1346                                         ::open("/dev/i2c/0", O_RDWR);
1347
1348                                 unsigned char data[2];
1349                                 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1350                                 if(::read(fd, data, 1) != 1)
1351                                         eDebug("[SEC] error read lnbp (%m)");
1352                                 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1353                                 {
1354                                         data[0] |= 0x80;  // enable static current limiting
1355                                         eDebug("[SEC] set static current limiting");
1356                                 }
1357                                 else
1358                                 {
1359                                         data[0] &= ~0x80;  // enable dynamic current limiting
1360                                         eDebug("[SEC] set dynamic current limiting");
1361                                 }
1362                                 if(::write(fd, data, 1) != 1)
1363                                         eDebug("[SEC] error write lnbp (%m)");
1364                                 ::close(fd);
1365                                 ++m_sec_sequence.current();
1366                                 break;
1367                         }
1368                         default:
1369                                 eDebug("[SEC] unhandled sec command %d",
1370                                         ++m_sec_sequence.current()->cmd);
1371                                 ++m_sec_sequence.current();
1372                 }
1373                 m_tuneTimer->start(delay,true);
1374         }
1375 }
1376
1377 void eDVBFrontend::setFrontend()
1378 {
1379         eDebug("setting frontend %d", m_fe);
1380         m_sn->start();
1381         feEvent(-1);
1382         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1383         {
1384                 perror("FE_SET_FRONTEND failed");
1385                 return;
1386         }
1387 }
1388
1389 RESULT eDVBFrontend::getFrontendType(int &t)
1390 {
1391         if (m_type == -1)
1392                 return -ENODEV;
1393         t = m_type;
1394         return 0;
1395 }
1396
1397 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1398 {
1399         int res;
1400         if (!m_sec)
1401         {
1402                 eWarning("no SEC module active!");
1403                 return -ENOENT;
1404         }
1405         res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
1406         if (!res)
1407         {
1408                 eDebug("prepare_sat System %d Freq %d Pol %d SR %d INV %d FEC %d orbpos %d",
1409                         feparm.system,
1410                         feparm.frequency,
1411                         feparm.polarisation,
1412                         feparm.symbol_rate,
1413                         feparm.inversion,
1414                         feparm.fec,
1415                         feparm.orbital_position);
1416                 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1417                 switch (feparm.inversion)
1418                 {
1419                         case eDVBFrontendParametersSatellite::Inversion::On:
1420                                 parm_inversion = INVERSION_ON;
1421                                 break;
1422                         case eDVBFrontendParametersSatellite::Inversion::Off:
1423                                 parm_inversion = INVERSION_OFF;
1424                                 break;
1425                         default:
1426                         case eDVBFrontendParametersSatellite::Inversion::Unknown:
1427                                 parm_inversion = INVERSION_AUTO;
1428                                 break;
1429                 }
1430                 if (feparm.system == eDVBFrontendParametersSatellite::System::DVB_S)
1431                         switch (feparm.fec)
1432                         {
1433                                 case eDVBFrontendParametersSatellite::FEC::fNone:
1434                                         parm_u_qpsk_fec_inner = FEC_NONE;
1435                                         break;
1436                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1437                                         parm_u_qpsk_fec_inner = FEC_1_2;
1438                                         break;
1439                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1440                                         parm_u_qpsk_fec_inner = FEC_2_3;
1441                                         break;
1442                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1443                                         parm_u_qpsk_fec_inner = FEC_3_4;
1444                                         break;
1445                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1446                                         parm_u_qpsk_fec_inner = FEC_5_6;
1447                                         break;
1448                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1449                                         parm_u_qpsk_fec_inner = FEC_7_8;
1450                                         break;
1451                                 default:
1452                                         eDebug("no valid fec for DVB-S set.. assume auto");
1453                                 case eDVBFrontendParametersSatellite::FEC::fAuto:
1454                                         parm_u_qpsk_fec_inner = FEC_AUTO;
1455                                         break;
1456                         }
1457 #if HAVE_DVB_API_VERSION >= 3
1458                 else // DVB_S2
1459                 {
1460                         switch (feparm.fec)
1461                         {
1462                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1463                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_1_2;
1464                                         break;
1465                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1466                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_2_3;
1467                                         break;
1468                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1469                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_4;
1470                                         break;
1471                                 case eDVBFrontendParametersSatellite::FEC::f3_5:
1472                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_5;
1473                                         break;
1474                                 case eDVBFrontendParametersSatellite::FEC::f4_5:
1475                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_4_5;
1476                                         break;
1477                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1478                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_5_6;
1479                                         break;
1480                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1481                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_7_8;
1482                                         break;
1483                                 case eDVBFrontendParametersSatellite::FEC::f8_9:
1484                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_8_9;
1485                                         break;
1486                                 case eDVBFrontendParametersSatellite::FEC::f9_10:
1487                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_9_10;
1488                                         break;
1489                                 default:
1490                                         eDebug("no valid fec for DVB-S2 set.. abort !!");
1491                                         return -EINVAL;
1492                         }
1493                         if (feparm.modulation == eDVBFrontendParametersSatellite::Modulation::M8PSK)
1494                                 parm_u_qpsk_fec_inner = (fe_code_rate_t)((int)parm_u_qpsk_fec_inner+9);
1495                                 // 8PSK fec driver values are decimal 9 bigger
1496                 }
1497 #endif
1498                 // FIXME !!! get frequency range from tuner
1499                 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1500                 {
1501                         eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1502                         return -EINVAL;
1503                 }
1504                 eDebug("tuning to %d mhz", parm_frequency/1000);
1505         }
1506         return res;
1507 }
1508
1509 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1510 {
1511         parm_frequency = feparm.frequency * 1000;
1512         parm_u_qam_symbol_rate = feparm.symbol_rate;
1513         switch (feparm.modulation)
1514         {
1515         case eDVBFrontendParametersCable::Modulation::QAM16:
1516                 parm_u_qam_modulation = QAM_16;
1517                 break;
1518         case eDVBFrontendParametersCable::Modulation::QAM32:
1519                 parm_u_qam_modulation = QAM_32;
1520                 break;
1521         case eDVBFrontendParametersCable::Modulation::QAM64:
1522                 parm_u_qam_modulation = QAM_64;
1523                 break;
1524         case eDVBFrontendParametersCable::Modulation::QAM128:
1525                 parm_u_qam_modulation = QAM_128;
1526                 break;
1527         case eDVBFrontendParametersCable::Modulation::QAM256:
1528                 parm_u_qam_modulation = QAM_256;
1529                 break;
1530         default:
1531         case eDVBFrontendParametersCable::Modulation::Auto:
1532                 parm_u_qam_modulation = QAM_AUTO;
1533                 break;
1534         }
1535         switch (feparm.inversion)
1536         {
1537         case eDVBFrontendParametersCable::Inversion::On:
1538                 parm_inversion = INVERSION_ON;
1539                 break;
1540         case eDVBFrontendParametersCable::Inversion::Off:
1541                 parm_inversion = INVERSION_OFF;
1542                 break;
1543         default:
1544         case eDVBFrontendParametersCable::Inversion::Unknown:
1545                 parm_inversion = INVERSION_AUTO;
1546                 break;
1547         }
1548         switch (feparm.fec_inner)
1549         {
1550         case eDVBFrontendParametersCable::FEC::fNone:
1551                 parm_u_qam_fec_inner = FEC_NONE;
1552                 break;
1553         case eDVBFrontendParametersCable::FEC::f1_2:
1554                 parm_u_qam_fec_inner = FEC_1_2;
1555                 break;
1556         case eDVBFrontendParametersCable::FEC::f2_3:
1557                 parm_u_qam_fec_inner = FEC_2_3;
1558                 break;
1559         case eDVBFrontendParametersCable::FEC::f3_4:
1560                 parm_u_qam_fec_inner = FEC_3_4;
1561                 break;
1562         case eDVBFrontendParametersCable::FEC::f5_6:
1563                 parm_u_qam_fec_inner = FEC_5_6;
1564                 break;
1565         case eDVBFrontendParametersCable::FEC::f7_8:
1566                 parm_u_qam_fec_inner = FEC_7_8;
1567                 break;
1568 #if HAVE_DVB_API_VERSION >= 3
1569         case eDVBFrontendParametersCable::FEC::f8_9:
1570                 parm_u_qam_fec_inner = FEC_8_9;
1571                 break;
1572 #endif
1573         default:
1574         case eDVBFrontendParametersCable::FEC::fAuto:
1575                 parm_u_qam_fec_inner = FEC_AUTO;
1576                 break;
1577         }
1578         eDebug("tuning to %d khz, sr %d, fec %d, modulation %d, inversion %d",
1579                 parm_frequency/1000,
1580                 parm_u_qam_symbol_rate,
1581                 parm_u_qam_fec_inner,
1582                 parm_u_qam_modulation,
1583                 parm_inversion);
1584         return 0;
1585 }
1586
1587 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1588 {
1589         parm_frequency = feparm.frequency;
1590
1591         switch (feparm.bandwidth)
1592         {
1593         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1594                 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1595                 break;
1596         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1597                 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1598                 break;
1599         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1600                 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1601                 break;
1602         default:
1603         case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1604                 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1605                 break;
1606         }
1607         switch (feparm.code_rate_LP)
1608         {
1609         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1610                 parm_u_ofdm_code_rate_LP = FEC_1_2;
1611                 break;
1612         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1613                 parm_u_ofdm_code_rate_LP = FEC_2_3;
1614                 break;
1615         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1616                 parm_u_ofdm_code_rate_LP = FEC_3_4;
1617                 break;
1618         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1619                 parm_u_ofdm_code_rate_LP = FEC_5_6;
1620                 break;
1621         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1622                 parm_u_ofdm_code_rate_LP = FEC_7_8;
1623                 break;
1624         default:
1625         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1626                 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1627                 break;
1628         }
1629         switch (feparm.code_rate_HP)
1630         {
1631         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1632                 parm_u_ofdm_code_rate_HP = FEC_1_2;
1633                 break;
1634         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1635                 parm_u_ofdm_code_rate_HP = FEC_2_3;
1636                 break;
1637         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1638                 parm_u_ofdm_code_rate_HP = FEC_3_4;
1639                 break;
1640         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1641                 parm_u_ofdm_code_rate_HP = FEC_5_6;
1642                 break;
1643         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1644                 parm_u_ofdm_code_rate_HP = FEC_7_8;
1645                 break;
1646         default:
1647         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1648                 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1649                 break;
1650         }
1651         switch (feparm.modulation)
1652         {
1653         case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1654                 parm_u_ofdm_constellation = QPSK;
1655                 break;
1656         case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1657                 parm_u_ofdm_constellation = QAM_16;
1658                 break;
1659         case eDVBFrontendParametersTerrestrial::Modulation::QAM64:
1660                 parm_u_ofdm_constellation = QAM_64;
1661                 break;
1662         default:
1663         case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1664                 parm_u_ofdm_constellation = QAM_AUTO;
1665                 break;
1666         }
1667         switch (feparm.transmission_mode)
1668         {
1669         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1670                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1671                 break;
1672         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1673                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1674                 break;
1675         default:
1676         case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1677                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1678                 break;
1679         }
1680         switch (feparm.guard_interval)
1681         {
1682                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1683                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1684                         break;
1685                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1686                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1687                         break;
1688                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1689                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1690                         break;
1691                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1692                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1693                         break;
1694                 default:
1695                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1696                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1697                         break;
1698         }
1699         switch (feparm.hierarchy)
1700         {
1701                 case eDVBFrontendParametersTerrestrial::Hierarchy::HNone:
1702                         parm_u_ofdm_hierarchy_information = HIERARCHY_NONE;
1703                         break;
1704                 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1705                         parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1706                         break;
1707                 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1708                         parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1709                         break;
1710                 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1711                         parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1712                         break;
1713                 default:
1714                 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1715                         parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1716                         break;
1717         }
1718         switch (feparm.inversion)
1719         {
1720         case eDVBFrontendParametersTerrestrial::Inversion::On:
1721                 parm_inversion = INVERSION_ON;
1722                 break;
1723         case eDVBFrontendParametersTerrestrial::Inversion::Off:
1724                 parm_inversion = INVERSION_OFF;
1725                 break;
1726         default:
1727         case eDVBFrontendParametersTerrestrial::Inversion::Unknown:
1728                 parm_inversion = INVERSION_AUTO;
1729                 break;
1730         }
1731         return 0;
1732 }
1733
1734 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1735 {
1736         eDebug("(%d)tune", m_fe);
1737
1738         m_timeout->stop();
1739
1740         int res=0;
1741
1742         if (!m_sn)
1743         {
1744                 eDebug("no frontend device opened... do not try to tune !!!");
1745                 res = -ENODEV;
1746                 goto tune_error;
1747         }
1748
1749         if (m_type == -1)
1750         {
1751                 res = -ENODEV;
1752                 goto tune_error;
1753         }
1754
1755         m_sn->stop();
1756         m_sec_sequence.clear();
1757
1758         switch (m_type)
1759         {
1760         case feSatellite:
1761         {
1762                 eDVBFrontendParametersSatellite feparm;
1763                 if (where.getDVBS(feparm))
1764                 {
1765                         eDebug("no dvbs data!");
1766                         res = -EINVAL;
1767                         goto tune_error;
1768                 }
1769                 m_sec->setRotorMoving(false);
1770                 res=prepare_sat(feparm);
1771                 if (res)
1772                         goto tune_error;
1773
1774                 break;
1775         }
1776         case feCable:
1777         {
1778                 eDVBFrontendParametersCable feparm;
1779                 if (where.getDVBC(feparm))
1780                 {
1781                         res = -EINVAL;
1782                         goto tune_error;
1783                 }
1784                 res=prepare_cable(feparm);
1785                 if (res)
1786                         goto tune_error;
1787
1788                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1789                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1790                 break;
1791         }
1792         case feTerrestrial:
1793         {
1794                 eDVBFrontendParametersTerrestrial feparm;
1795                 if (where.getDVBT(feparm))
1796                 {
1797                         eDebug("no -T data");
1798                         res = -EINVAL;
1799                         goto tune_error;
1800                 }
1801                 res=prepare_terrestrial(feparm);
1802                 if (res)
1803                         goto tune_error;
1804
1805                 std::string enable_5V;
1806                 char configStr[255];
1807                 snprintf(configStr, 255, "config.Nims.%d.terrestrial_5V", m_fe);
1808                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1809                 ePythonConfigQuery::getConfigValue(configStr, enable_5V);
1810                 if (enable_5V == "True")
1811                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
1812                 else
1813                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltageOff) );
1814                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1815
1816                 break;
1817         }
1818         }
1819
1820         m_tuneTimer->start(0,true);
1821         m_sec_sequence.current() = m_sec_sequence.begin();
1822
1823         if (m_state != stateTuning)
1824         {
1825                 m_tuning = 1;
1826                 m_state = stateTuning;
1827                 m_stateChanged(this);
1828         }
1829
1830         return res;
1831
1832 tune_error:
1833         m_tuneTimer->stop();
1834         return res;
1835 }
1836
1837 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1838 {
1839         connection = new eConnection(this, m_stateChanged.connect(stateChange));
1840         return 0;
1841 }
1842
1843 RESULT eDVBFrontend::setVoltage(int voltage)
1844 {
1845         if (m_type == feCable)
1846                 return -1;
1847 #if HAVE_DVB_API_VERSION < 3
1848         secVoltage vlt;
1849 #else
1850         bool increased=false;
1851         fe_sec_voltage_t vlt;
1852 #endif
1853         m_data[CUR_VOLTAGE]=voltage;
1854         switch (voltage)
1855         {
1856         case voltageOff:
1857                 for (int i=0; i < 3; ++i)  // reset diseqc
1858                         m_data[i]=-1;
1859                 vlt = SEC_VOLTAGE_OFF;
1860                 break;
1861         case voltage13_5:
1862 #if HAVE_DVB_API_VERSION < 3
1863                 vlt = SEC_VOLTAGE_13_5;
1864                 break;
1865 #else
1866                 increased = true;
1867 #endif
1868         case voltage13:
1869                 vlt = SEC_VOLTAGE_13;
1870                 break;
1871         case voltage18_5:
1872 #if HAVE_DVB_API_VERSION < 3
1873                 vlt = SEC_VOLTAGE_18_5;
1874                 break;
1875 #else
1876                 increased = true;
1877 #endif
1878         case voltage18:
1879                 vlt = SEC_VOLTAGE_18;
1880                 break;
1881         default:
1882                 return -ENODEV;
1883         }
1884 #if HAVE_DVB_API_VERSION < 3
1885         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1886 #else
1887         if (m_type == feSatellite && ::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1888                 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
1889         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
1890 #endif
1891 }
1892
1893 RESULT eDVBFrontend::getState(int &state)
1894 {
1895         state = m_state;
1896         return 0;
1897 }
1898
1899 RESULT eDVBFrontend::setTone(int t)
1900 {
1901         if (m_type != feSatellite)
1902                 return -1;
1903 #if HAVE_DVB_API_VERSION < 3
1904         secToneMode_t tone;
1905 #else
1906         fe_sec_tone_mode_t tone;
1907 #endif
1908         m_data[CUR_TONE]=t;
1909         switch (t)
1910         {
1911         case toneOn:
1912                 tone = SEC_TONE_ON;
1913                 break;
1914         case toneOff:
1915                 tone = SEC_TONE_OFF;
1916                 break;
1917         default:
1918                 return -ENODEV;
1919         }
1920 #if HAVE_DVB_API_VERSION < 3    
1921         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
1922 #else   
1923         return ::ioctl(m_fd, FE_SET_TONE, tone);
1924 #endif
1925 }
1926
1927 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
1928         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
1929 #endif
1930
1931 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
1932 {
1933 #if HAVE_DVB_API_VERSION < 3
1934         struct secCommand cmd;
1935         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
1936         cmd.u.diseqc.cmdtype = diseqc.data[0];
1937         cmd.u.diseqc.addr = diseqc.data[1];
1938         cmd.u.diseqc.cmd = diseqc.data[2];
1939         cmd.u.diseqc.numParams = diseqc.len-3;
1940         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
1941         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
1942 #else
1943         struct dvb_diseqc_master_cmd cmd;
1944         memcpy(cmd.msg, diseqc.data, diseqc.len);
1945         cmd.msg_len = diseqc.len;
1946         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
1947 #endif
1948                 return -EINVAL;
1949         return 0;
1950 }
1951
1952 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
1953         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
1954 #endif
1955 RESULT eDVBFrontend::sendToneburst(int burst)
1956 {
1957 #if HAVE_DVB_API_VERSION < 3
1958         secMiniCmd cmd = SEC_MINI_NONE;
1959 #else
1960         fe_sec_mini_cmd_t cmd = SEC_MINI_A;
1961 #endif
1962         if ( burst == eDVBSatelliteDiseqcParameters::A )
1963                 cmd = SEC_MINI_A;
1964         else if ( burst == eDVBSatelliteDiseqcParameters::B )
1965                 cmd = SEC_MINI_B;
1966 #if HAVE_DVB_API_VERSION < 3
1967         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
1968                 return -EINVAL;
1969 #else
1970         if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
1971                 return -EINVAL;
1972 #endif
1973         return 0;
1974 }
1975
1976 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
1977 {
1978         m_sec = sec;
1979         return 0;
1980 }
1981
1982 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
1983 {
1984         m_sec_sequence = list;
1985         return 0;
1986 }
1987
1988 RESULT eDVBFrontend::getData(int num, int &data)
1989 {
1990         if ( num < NUM_DATA_ENTRIES )
1991         {
1992                 data = m_data[num];
1993                 return 0;
1994         }
1995         return -EINVAL;
1996 }
1997
1998 RESULT eDVBFrontend::setData(int num, int val)
1999 {
2000         if ( num < NUM_DATA_ENTRIES )
2001         {
2002                 m_data[num] = val;
2003                 return 0;
2004         }
2005         return -EINVAL;
2006 }
2007
2008 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
2009 {
2010         int type;
2011         if (feparm->getSystem(type) || type != m_type)
2012                 return 0;
2013
2014         if (m_type == eDVBFrontend::feSatellite)
2015         {
2016                 ASSERT(m_sec);
2017                 eDVBFrontendParametersSatellite sat_parm;
2018                 ASSERT(!feparm->getDVBS(sat_parm));
2019                 return m_sec->canTune(sat_parm, this, 1 << m_fe);
2020         }
2021         else if (m_type == eDVBFrontend::feCable)
2022                 return 2;  // more prio for cable frontends
2023         return 1;
2024 }