invent proper signalPowerdB enums and use them
[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 signalPowerdB: /* this will move into the driver */
629                 {
630                         uint16_t snr=0;
631                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
632                                 eDebug("FE_READ_SNR failed (%m)");
633                         unsigned int SDS_SNRE = snr << 16;
634                         
635                         static float SNR_COEFF[6] = {
636                                 100.0 / 4194304.0,
637                                 -7136.0 / 4194304.0,
638                                 197418.0 / 4194304.0,
639                                 -2602183.0 / 4194304.0,
640                                 20377212.0 / 4194304.0,
641                                 -37791203.0 / 4194304.0,
642                         };
643                         
644                         float fval1, fval2, snr_in_db;
645                         int i;
646                         fval1 = 12.44714 - (2.0 * log10(SDS_SNRE / 256.0));
647                         fval2 = pow(10.0, fval1)-1;
648                         fval1 = 10.0 * log10(fval2);
649                         
650                         if (fval1 < 10.0)
651                         {
652                                 fval2 = SNR_COEFF[0];
653                                 for (i=0; i<6; ++i)
654                                 {
655                                         fval2 *= fval1;
656                                         fval2 += SNR_COEFF[i];
657                                 }
658                                 fval1 = fval2;
659                         }
660                         snr_in_db = fval1;
661                         
662                         return (int)(snr_in_db * 100.0);
663                 }
664                 case signalQuality:
665                 {
666                         uint16_t strength=0;
667                         if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
668                                 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
669                         return strength;
670                 }
671                 case locked:
672                 {
673 #if HAVE_DVB_API_VERSION < 3
674                         FrontendStatus status=0;
675 #else
676                         fe_status_t status;
677 #endif
678                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
679                                 eDebug("FE_READ_STATUS failed (%m)");
680                         return !!(status&FE_HAS_LOCK);
681                 }
682                 case synced:
683                 {
684 #if HAVE_DVB_API_VERSION < 3
685                         FrontendStatus status=0;
686 #else
687                         fe_status_t status;
688 #endif
689                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
690                                 eDebug("FE_READ_STATUS failed (%m)");
691                         return !!(status&FE_HAS_SYNC);
692                 }
693                 case frontendNumber:
694                         return m_fe;
695         }
696         return 0;
697 }
698
699 void PutToDict(ePyObject &dict, const char*key, long value)
700 {
701         ePyObject item = PyInt_FromLong(value);
702         if (item)
703         {
704                 if (PyDict_SetItemString(dict, key, item))
705                         eDebug("put %s to dict failed", key);
706                 Py_DECREF(item);
707         }
708         else
709                 eDebug("could not create PyObject for %s", key);
710 }
711
712 void PutToDict(ePyObject &dict, const char*key, const char *value)
713 {
714         ePyObject item = PyString_FromString(value);
715         if (item)
716         {
717                 if (PyDict_SetItemString(dict, key, item))
718                         eDebug("put %s to dict failed", key);
719                 Py_DECREF(item);
720         }
721         else
722                 eDebug("could not create PyObject for %s", key);
723 }
724
725 void fillDictWithSatelliteData(ePyObject dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
726 {
727         int freq_offset=0;
728         int csw=0;
729         const char *tmp=0;
730         fe->getData(eDVBFrontend::CSW, csw);
731         fe->getData(eDVBFrontend::FREQ_OFFSET, freq_offset);
732         int frequency = parm_frequency + freq_offset;
733         PutToDict(dict, "frequency", frequency);
734         PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
735         switch(parm_u_qpsk_fec_inner)
736         {
737         case FEC_1_2:
738                 tmp = "FEC_1_2";
739                 break;
740         case FEC_2_3:
741                 tmp = "FEC_2_3";
742                 break;
743         case FEC_3_4:
744                 tmp = "FEC_3_4";
745                 break;
746         case FEC_5_6:
747                 tmp = "FEC_5_6";
748                 break;
749         case FEC_7_8:
750                 tmp = "FEC_7_8";
751                 break;
752         case FEC_NONE:
753                 tmp = "FEC_NONE";
754         default:
755         case FEC_AUTO:
756                 tmp = "FEC_AUTO";
757                 break;
758 #if HAVE_DVB_API_VERSION >=3
759         case FEC_S2_8PSK_1_2:
760         case FEC_S2_QPSK_1_2:
761                 tmp = "FEC_1_2";
762                 break;
763         case FEC_S2_8PSK_2_3:
764         case FEC_S2_QPSK_2_3:
765                 tmp = "FEC_2_3";
766                 break;
767         case FEC_S2_8PSK_3_4:
768         case FEC_S2_QPSK_3_4:
769                 tmp = "FEC_3_4";
770                 break;
771         case FEC_S2_8PSK_5_6:
772         case FEC_S2_QPSK_5_6:
773                 tmp = "FEC_5_6";
774                 break;
775         case FEC_S2_8PSK_7_8:
776         case FEC_S2_QPSK_7_8:
777                 tmp = "FEC_7_8";
778                 break;
779         case FEC_S2_8PSK_8_9:
780         case FEC_S2_QPSK_8_9:
781                 tmp = "FEC_8_9";
782                 break;
783         case FEC_S2_8PSK_3_5:
784         case FEC_S2_QPSK_3_5:
785                 tmp = "FEC_3_5";
786                 break;
787         case FEC_S2_8PSK_4_5:
788         case FEC_S2_QPSK_4_5:
789                 tmp = "FEC_4_5";
790                 break;
791         case FEC_S2_8PSK_9_10:
792         case FEC_S2_QPSK_9_10:
793                 tmp = "FEC_9_10";
794                 break;
795 #endif
796         }
797 #if HAVE_DVB_API_VERSION >=3
798         PutToDict(dict, "modulation",
799                 parm_u_qpsk_fec_inner > FEC_S2_QPSK_9_10 ? "8PSK": "QPSK" );
800 #else
801         PutToDict(dict, "modulation", "QPSK" );
802 #endif
803         PutToDict(dict, "fec_inner", tmp);
804         tmp = parm_u_qpsk_fec_inner > FEC_AUTO ?
805                 "DVB-S2" : "DVB-S";
806         PutToDict(dict, "system", tmp);
807 }
808
809 void fillDictWithCableData(ePyObject dict, const FRONTENDPARAMETERS &parm)
810 {
811         const char *tmp=0;
812         PutToDict(dict, "frequency", parm_frequency/1000);
813         PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
814         switch(parm_u_qam_fec_inner)
815         {
816         case FEC_NONE:
817                 tmp = "FEC_NONE";
818                 break;
819         case FEC_1_2:
820                 tmp = "FEC_1_2";
821                 break;
822         case FEC_2_3:
823                 tmp = "FEC_2_3";
824                 break;
825         case FEC_3_4:
826                 tmp = "FEC_3_4";
827                 break;
828         case FEC_5_6:
829                 tmp = "FEC_5_6";
830                 break;
831         case FEC_7_8:
832                 tmp = "FEC_7_8";
833                 break;
834 #if HAVE_DVB_API_VERSION >= 3
835         case FEC_8_9:
836                 tmp = "FEC_8_9";
837                 break;
838 #endif
839         default:
840         case FEC_AUTO:
841                 tmp = "FEC_AUTO";
842                 break;
843         }
844         PutToDict(dict, "fec_inner", tmp);
845         switch(parm_u_qam_modulation)
846         {
847         case QAM_16:
848                 tmp = "QAM_16";
849                 break;
850         case QAM_32:
851                 tmp = "QAM_32";
852                 break;
853         case QAM_64:
854                 tmp = "QAM_64";
855                 break;
856         case QAM_128:
857                 tmp = "QAM_128";
858                 break;
859         case QAM_256:
860                 tmp = "QAM_256";
861                 break;
862         default:
863         case QAM_AUTO:
864                 tmp = "QAM_AUTO";
865                 break;
866         }
867         PutToDict(dict, "modulation", tmp);
868 }
869
870 void fillDictWithTerrestrialData(ePyObject dict, const FRONTENDPARAMETERS &parm)
871 {
872         const char *tmp=0;
873         PutToDict(dict, "frequency", parm_frequency);
874         switch (parm_u_ofdm_bandwidth)
875         {
876         case BANDWIDTH_8_MHZ:
877                 tmp = "BANDWIDTH_8_MHZ";
878                 break;
879         case BANDWIDTH_7_MHZ:
880                 tmp = "BANDWIDTH_7_MHZ";
881                 break;
882         case BANDWIDTH_6_MHZ:
883                 tmp = "BANDWIDTH_6_MHZ";
884                 break;
885         default:
886         case BANDWIDTH_AUTO:
887                 tmp = "BANDWIDTH_AUTO";
888                 break;
889         }
890         PutToDict(dict, "bandwidth", tmp);
891         switch (parm_u_ofdm_code_rate_LP)
892         {
893         case FEC_1_2:
894                 tmp = "FEC_1_2";
895                 break;
896         case FEC_2_3:
897                 tmp = "FEC_2_3";
898                 break;
899         case FEC_3_4:
900                 tmp = "FEC_3_4";
901                 break;
902         case FEC_5_6:
903                 tmp = "FEC_5_6";
904                 break;
905         case FEC_7_8:
906                 tmp = "FEC_7_8";
907                 break;
908         default:
909         case FEC_AUTO:
910                 tmp = "FEC_AUTO";
911                 break;
912         }
913         PutToDict(dict, "code_rate_lp", tmp);
914         switch (parm_u_ofdm_code_rate_HP)
915         {
916         case FEC_1_2:
917                 tmp = "FEC_1_2";
918                 break;
919         case FEC_2_3:
920                 tmp = "FEC_2_3";
921                 break;
922         case FEC_3_4:
923                 tmp = "FEC_3_4";
924                 break;
925         case FEC_5_6:
926                 tmp = "FEC_5_6";
927                 break;
928         case FEC_7_8:
929                 tmp = "FEC_7_8";
930                 break;
931         default:
932         case FEC_AUTO:
933                 tmp = "FEC_AUTO";
934                 break;
935         }
936         PutToDict(dict, "code_rate_hp", tmp);
937         switch (parm_u_ofdm_constellation)
938         {
939         case QPSK:
940                 tmp = "QPSK";
941                 break;
942         case QAM_16:
943                 tmp = "QAM_16";
944                 break;
945         case QAM_64:
946                 tmp = "QAM_64";
947                 break;
948         default:
949         case QAM_AUTO:
950                 tmp = "QAM_AUTO";
951                 break;
952         }
953         PutToDict(dict, "constellation", tmp);
954         switch (parm_u_ofdm_transmission_mode)
955         {
956         case TRANSMISSION_MODE_2K:
957                 tmp = "TRANSMISSION_MODE_2K";
958                 break;
959         case TRANSMISSION_MODE_8K:
960                 tmp = "TRANSMISSION_MODE_8K";
961                 break;
962         default:
963         case TRANSMISSION_MODE_AUTO:
964                 tmp = "TRANSMISSION_MODE_AUTO";
965                 break;
966         }
967         PutToDict(dict, "transmission_mode", tmp);
968         switch (parm_u_ofdm_guard_interval)
969         {
970                 case GUARD_INTERVAL_1_32:
971                         tmp = "GUARD_INTERVAL_1_32";
972                         break;
973                 case GUARD_INTERVAL_1_16:
974                         tmp = "GUARD_INTERVAL_1_16";
975                         break;
976                 case GUARD_INTERVAL_1_8:
977                         tmp = "GUARD_INTERVAL_1_8";
978                         break;
979                 case GUARD_INTERVAL_1_4:
980                         tmp = "GUARD_INTERVAL_1_4";
981                         break;
982                 default:
983                 case GUARD_INTERVAL_AUTO:
984                         tmp = "GUARD_INTERVAL_AUTO";
985                         break;
986         }
987         PutToDict(dict, "guard_interval", tmp);
988         switch (parm_u_ofdm_hierarchy_information)
989         {
990                 case HIERARCHY_NONE:
991                         tmp = "HIERARCHY_NONE";
992                         break;
993                 case HIERARCHY_1:
994                         tmp = "HIERARCHY_1";
995                         break;
996                 case HIERARCHY_2:
997                         tmp = "HIERARCHY_2";
998                         break;
999                 case HIERARCHY_4:
1000                         tmp = "HIERARCHY_4";
1001                         break;
1002                 default:
1003                 case HIERARCHY_AUTO:
1004                         tmp = "HIERARCHY_AUTO";
1005                         break;
1006         }
1007         PutToDict(dict, "hierarchy_information", tmp);
1008 }
1009
1010 void eDVBFrontend::getFrontendStatus(ePyObject dest)
1011 {
1012         if (dest && PyDict_Check(dest))
1013         {
1014                 const char *tmp = "UNKNOWN";
1015                 switch(m_state)
1016                 {
1017                         case stateIdle:
1018                                 tmp="IDLE";
1019                                 break;
1020                         case stateTuning:
1021                                 tmp="TUNING";
1022                                 break;
1023                         case stateFailed:
1024                                 tmp="FAILED";
1025                                 break;
1026                         case stateLock:
1027                                 tmp="LOCKED";
1028                                 break;
1029                         case stateLostLock:
1030                                 tmp="LOSTLOCK";
1031                                 break;
1032                         default:
1033                                 break;
1034                 }
1035                 PutToDict(dest, "tuner_state", tmp);
1036                 PutToDict(dest, "tuner_locked", readFrontendData(locked));
1037                 PutToDict(dest, "tuner_synced", readFrontendData(synced));
1038                 PutToDict(dest, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
1039                 PutToDict(dest, "tuner_signal_power", readFrontendData(signalPower));
1040                 PutToDict(dest, "tuner_signal_power_db", readFrontendData(signalPowerdB));
1041                 PutToDict(dest, "tuner_signal_quality", readFrontendData(signalQuality));
1042         }
1043 }
1044
1045 void eDVBFrontend::getTransponderData(ePyObject dest, bool original)
1046 {
1047         if (m_fd != -1 && dest && PyDict_Check(dest))
1048         {
1049                 switch(m_type)
1050                 {
1051                         case feSatellite:
1052                         case feCable:
1053                         case feTerrestrial:
1054                         {
1055                                 FRONTENDPARAMETERS front;
1056                                 if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
1057                                         eDebug("FE_GET_FRONTEND (%m)");
1058                                 else
1059                                 {
1060                                         const FRONTENDPARAMETERS &parm = original ? this->parm : front;
1061                                         const char *tmp = "INVERSION_AUTO";
1062                                         switch(parm_inversion)
1063                                         {
1064                                                 case INVERSION_ON:
1065                                                         tmp = "INVERSION_ON";
1066                                                         break;
1067                                                 case INVERSION_OFF:
1068                                                         tmp = "INVERSION_OFF";
1069                                                         break;
1070                                                 default:
1071                                                         break;
1072                                         }
1073                                         if (tmp)
1074                                                 PutToDict(dest, "inversion", tmp);
1075
1076                                         switch(m_type)
1077                                         {
1078                                                 case feSatellite:
1079                                                         fillDictWithSatelliteData(dest, original?parm:front, this);
1080                                                         break;
1081                                                 case feCable:
1082                                                         fillDictWithCableData(dest, original?parm:front);
1083                                                         break;
1084                                                 case feTerrestrial:
1085                                                         fillDictWithTerrestrialData(dest, original?parm:front);
1086                                                         break;
1087                                         }
1088                                 }
1089                         }
1090                         default:
1091                                 break;
1092                 }
1093         }
1094 }
1095
1096 void eDVBFrontend::getFrontendData(ePyObject dest)
1097 {
1098         if (dest && PyDict_Check(dest))
1099         {
1100                 const char *tmp=0;
1101                 PutToDict(dest, "tuner_number", m_fe);
1102                 switch(m_type)
1103                 {
1104                         case feSatellite:
1105                                 tmp = "DVB-S";
1106                                 break;
1107                         case feCable:
1108                                 tmp = "DVB-C";
1109                                 break;
1110                         case feTerrestrial:
1111                                 tmp = "DVB-T";
1112                                 break;
1113                         default:
1114                                 tmp = "UNKNOWN";
1115                                 break;
1116                 }
1117                 PutToDict(dest, "tuner_type", tmp);
1118         }
1119 }
1120
1121 #ifndef FP_IOCTL_GET_ID
1122 #define FP_IOCTL_GET_ID 0
1123 #endif
1124 int eDVBFrontend::readInputpower()
1125 {
1126         int power=m_fe;  // this is needed for read inputpower from the correct tuner !
1127
1128         // open front prozessor
1129         int fp=::open("/dev/dbox/fp0", O_RDWR);
1130         if (fp < 0)
1131         {
1132                 eDebug("couldn't open fp");
1133                 return -1;
1134         }
1135         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
1136         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
1137         {
1138                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
1139                 return -1;
1140         }
1141         ::close(fp);
1142
1143         return power;
1144 }
1145
1146 bool eDVBFrontend::setSecSequencePos(int steps)
1147 {
1148         eDebug("set sequence pos %d", steps);
1149         if (!steps)
1150                 return false;
1151         while( steps > 0 )
1152         {
1153                 if (m_sec_sequence.current() != m_sec_sequence.end())
1154                         ++m_sec_sequence.current();
1155                 --steps;
1156         }
1157         while( steps < 0 )
1158         {
1159                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
1160                         --m_sec_sequence.current();
1161                 ++steps;
1162         }
1163         return true;
1164 }
1165
1166 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
1167 {
1168         int delay=0;
1169         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
1170         {
1171 //              eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
1172                 switch (m_sec_sequence.current()->cmd)
1173                 {
1174                         case eSecCommand::SLEEP:
1175                                 delay = m_sec_sequence.current()++->msec;
1176                                 eDebug("[SEC] sleep %dms", delay);
1177                                 break;
1178                         case eSecCommand::GOTO:
1179                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
1180                                         ++m_sec_sequence.current();
1181                                 break;
1182                         case eSecCommand::SET_VOLTAGE:
1183                         {
1184                                 int voltage = m_sec_sequence.current()++->voltage;
1185                                 eDebug("[SEC] setVoltage %d", voltage);
1186                                 setVoltage(voltage);
1187                                 break;
1188                         }
1189                         case eSecCommand::IF_VOLTAGE_GOTO:
1190                         {
1191                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1192                                 if ( compare.voltage == m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1193                                         break;
1194                                 ++m_sec_sequence.current();
1195                                 break;
1196                         }
1197                         case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1198                         {
1199                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1200                                 if ( compare.voltage != m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1201                                         break;
1202                                 ++m_sec_sequence.current();
1203                                 break;
1204                         }
1205                         case eSecCommand::IF_TONE_GOTO:
1206                         {
1207                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1208                                 if ( compare.tone == m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1209                                         break;
1210                                 ++m_sec_sequence.current();
1211                                 break;
1212                         }
1213                         case eSecCommand::IF_NOT_TONE_GOTO:
1214                         {
1215                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1216                                 if ( compare.tone != m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1217                                         break;
1218                                 ++m_sec_sequence.current();
1219                                 break;
1220                         }
1221                         case eSecCommand::SET_TONE:
1222                                 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1223                                 setTone(m_sec_sequence.current()++->tone);
1224                                 break;
1225                         case eSecCommand::SEND_DISEQC:
1226                                 sendDiseqc(m_sec_sequence.current()->diseqc);
1227                                 eDebugNoNewLine("[SEC] sendDiseqc: ");
1228                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1229                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1230                                 eDebug("");
1231                                 ++m_sec_sequence.current();
1232                                 break;
1233                         case eSecCommand::SEND_TONEBURST:
1234                                 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1235                                 sendToneburst(m_sec_sequence.current()++->toneburst);
1236                                 break;
1237                         case eSecCommand::SET_FRONTEND:
1238                                 eDebug("[SEC] setFrontend");
1239                                 setFrontend();
1240                                 ++m_sec_sequence.current();
1241                                 break;
1242                         case eSecCommand::START_TUNE_TIMEOUT:
1243                                 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1244                                 ++m_sec_sequence.current();
1245                                 break;
1246                         case eSecCommand::SET_TIMEOUT:
1247                                 m_timeoutCount = m_sec_sequence.current()++->val;
1248                                 eDebug("[SEC] set timeout %d", m_timeoutCount);
1249                                 break;
1250                         case eSecCommand::IF_TIMEOUT_GOTO:
1251                                 if (!m_timeoutCount)
1252                                 {
1253                                         eDebug("[SEC] rotor timout");
1254                                         m_sec->setRotorMoving(false);
1255                                         setSecSequencePos(m_sec_sequence.current()->steps);
1256                                 }
1257                                 else
1258                                         ++m_sec_sequence.current();
1259                                 break;
1260                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1261                         {
1262                                 int idx = m_sec_sequence.current()++->val;
1263                                 if ( idx == 0 || idx == 1 )
1264                                 {
1265                                         m_idleInputpower[idx] = readInputpower();
1266                                         eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1267                                 }
1268                                 else
1269                                         eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1270                                 break;
1271                         }
1272                         case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1273                         {
1274                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1275                                 int idx = compare.val;
1276                                 if ( idx == 0 || idx == 1 )
1277                                 {
1278                                         int idle = readInputpower();
1279                                         int diff = abs(idle-m_idleInputpower[idx]);
1280                                         if ( diff > 0)
1281                                         {
1282                                                 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1283                                                 setSecSequencePos(compare.steps);
1284                                                 break;
1285                                         }
1286                                 }
1287                                 ++m_sec_sequence.current();
1288                                 break;
1289                         }
1290                         case eSecCommand::IF_TUNER_LOCKED_GOTO:
1291                         {
1292                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1293                                 if (readFrontendData(locked))
1294                                 {
1295                                         eDebug("[SEC] locked step %d ok", cmd.okcount);
1296                                         ++cmd.okcount;
1297                                         if (cmd.okcount > 12)
1298                                         {
1299                                                 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1300                                                 setSecSequencePos(cmd.steps);
1301                                                 break;
1302                                         }
1303                                 }
1304                                 else
1305                                 {
1306                                         eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1307                                         --m_timeoutCount;
1308                                         if (!m_timeoutCount && m_retryCount > 0)
1309                                                 --m_retryCount;
1310                                         cmd.okcount=0;
1311                                 }
1312                                 ++m_sec_sequence.current();
1313                                 break;
1314                         }
1315                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1316                                 m_runningInputpower = readInputpower();
1317                                 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1318                                 ++m_sec_sequence.current();
1319                                 break;
1320                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1321                         {
1322                                 int idleInputpower = m_idleInputpower[ (m_data[CUR_VOLTAGE]&1) ? 0 : 1];
1323                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1324                                 const char *txt = cmd.direction ? "running" : "stopped";
1325                                 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1326                                         txt,
1327                                         m_runningInputpower,
1328                                         idleInputpower,
1329                                         cmd.deltaA);
1330                                 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1331                                         || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1332                                 {
1333                                         ++cmd.okcount;
1334                                         eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1335                                         if ( cmd.okcount > 6 )
1336                                         {
1337                                                 m_sec->setRotorMoving(cmd.direction);
1338                                                 eDebug("[SEC] rotor is %s", txt);
1339                                                 if (setSecSequencePos(cmd.steps))
1340                                                         break;
1341                                         }
1342                                 }
1343                                 else
1344                                 {
1345                                         eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1346                                         --m_timeoutCount;
1347                                         if (!m_timeoutCount && m_retryCount > 0)
1348                                                 --m_retryCount;
1349                                         cmd.okcount=0;
1350                                 }
1351                                 ++m_sec_sequence.current();
1352                                 break;
1353                         }
1354                         case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1355                                 if (m_data[ROTOR_CMD] != -1 && m_data[ROTOR_POS] != -1)
1356                                         setSecSequencePos(m_sec_sequence.current()->steps);
1357                                 else
1358                                         ++m_sec_sequence.current();
1359                                 break;
1360                         case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1361                                 m_data[ROTOR_CMD] = m_data[ROTOR_POS] = -1;
1362                                 eDebug("[SEC] invalidate current rotorparams");
1363                                 ++m_sec_sequence.current();
1364                                 break;
1365                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1366                                 m_data[ROTOR_CMD] = m_data[NEW_ROTOR_CMD];
1367                                 m_data[ROTOR_POS] = m_data[NEW_ROTOR_POS];
1368                                 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
1369                                 ++m_sec_sequence.current();
1370                                 break;
1371                         case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1372                                 m_retryCount = m_sec_sequence.current()++->val;
1373                                 eDebug("[SEC] set rotor retries %d", m_retryCount);
1374                                 break;
1375                         case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1376                                 if (!m_retryCount)
1377                                 {
1378                                         eDebug("[SEC] no more rotor retrys");
1379                                         setSecSequencePos(m_sec_sequence.current()->steps);
1380                                 }
1381                                 else
1382                                         ++m_sec_sequence.current();
1383                                 break;
1384                         case eSecCommand::SET_POWER_LIMITING_MODE:
1385                         {
1386                                 int fd = m_fe ?
1387                                         ::open("/dev/i2c/1", O_RDWR) :
1388                                         ::open("/dev/i2c/0", O_RDWR);
1389
1390                                 unsigned char data[2];
1391                                 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1392                                 if(::read(fd, data, 1) != 1)
1393                                         eDebug("[SEC] error read lnbp (%m)");
1394                                 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1395                                 {
1396                                         data[0] |= 0x80;  // enable static current limiting
1397                                         eDebug("[SEC] set static current limiting");
1398                                 }
1399                                 else
1400                                 {
1401                                         data[0] &= ~0x80;  // enable dynamic current limiting
1402                                         eDebug("[SEC] set dynamic current limiting");
1403                                 }
1404                                 if(::write(fd, data, 1) != 1)
1405                                         eDebug("[SEC] error write lnbp (%m)");
1406                                 ::close(fd);
1407                                 ++m_sec_sequence.current();
1408                                 break;
1409                         }
1410                         default:
1411                                 eDebug("[SEC] unhandled sec command %d",
1412                                         ++m_sec_sequence.current()->cmd);
1413                                 ++m_sec_sequence.current();
1414                 }
1415                 m_tuneTimer->start(delay,true);
1416         }
1417 }
1418
1419 void eDVBFrontend::setFrontend()
1420 {
1421         eDebug("setting frontend %d", m_fe);
1422         m_sn->start();
1423         feEvent(-1);
1424         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1425         {
1426                 perror("FE_SET_FRONTEND failed");
1427                 return;
1428         }
1429 }
1430
1431 RESULT eDVBFrontend::getFrontendType(int &t)
1432 {
1433         if (m_type == -1)
1434                 return -ENODEV;
1435         t = m_type;
1436         return 0;
1437 }
1438
1439 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1440 {
1441         int res;
1442         if (!m_sec)
1443         {
1444                 eWarning("no SEC module active!");
1445                 return -ENOENT;
1446         }
1447         res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
1448         if (!res)
1449         {
1450                 eDebug("prepare_sat System %d Freq %d Pol %d SR %d INV %d FEC %d orbpos %d",
1451                         feparm.system,
1452                         feparm.frequency,
1453                         feparm.polarisation,
1454                         feparm.symbol_rate,
1455                         feparm.inversion,
1456                         feparm.fec,
1457                         feparm.orbital_position);
1458                 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1459                 switch (feparm.inversion)
1460                 {
1461                         case eDVBFrontendParametersSatellite::Inversion::On:
1462                                 parm_inversion = INVERSION_ON;
1463                                 break;
1464                         case eDVBFrontendParametersSatellite::Inversion::Off:
1465                                 parm_inversion = INVERSION_OFF;
1466                                 break;
1467                         default:
1468                         case eDVBFrontendParametersSatellite::Inversion::Unknown:
1469                                 parm_inversion = INVERSION_AUTO;
1470                                 break;
1471                 }
1472                 if (feparm.system == eDVBFrontendParametersSatellite::System::DVB_S)
1473                         switch (feparm.fec)
1474                         {
1475                                 case eDVBFrontendParametersSatellite::FEC::fNone:
1476                                         parm_u_qpsk_fec_inner = FEC_NONE;
1477                                         break;
1478                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1479                                         parm_u_qpsk_fec_inner = FEC_1_2;
1480                                         break;
1481                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1482                                         parm_u_qpsk_fec_inner = FEC_2_3;
1483                                         break;
1484                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1485                                         parm_u_qpsk_fec_inner = FEC_3_4;
1486                                         break;
1487                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1488                                         parm_u_qpsk_fec_inner = FEC_5_6;
1489                                         break;
1490                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1491                                         parm_u_qpsk_fec_inner = FEC_7_8;
1492                                         break;
1493                                 default:
1494                                         eDebug("no valid fec for DVB-S set.. assume auto");
1495                                 case eDVBFrontendParametersSatellite::FEC::fAuto:
1496                                         parm_u_qpsk_fec_inner = FEC_AUTO;
1497                                         break;
1498                         }
1499 #if HAVE_DVB_API_VERSION >= 3
1500                 else // DVB_S2
1501                 {
1502                         switch (feparm.fec)
1503                         {
1504                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1505                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_1_2;
1506                                         break;
1507                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1508                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_2_3;
1509                                         break;
1510                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1511                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_4;
1512                                         break;
1513                                 case eDVBFrontendParametersSatellite::FEC::f3_5:
1514                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_5;
1515                                         break;
1516                                 case eDVBFrontendParametersSatellite::FEC::f4_5:
1517                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_4_5;
1518                                         break;
1519                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1520                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_5_6;
1521                                         break;
1522                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1523                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_7_8;
1524                                         break;
1525                                 case eDVBFrontendParametersSatellite::FEC::f8_9:
1526                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_8_9;
1527                                         break;
1528                                 case eDVBFrontendParametersSatellite::FEC::f9_10:
1529                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_9_10;
1530                                         break;
1531                                 default:
1532                                         eDebug("no valid fec for DVB-S2 set.. abort !!");
1533                                         return -EINVAL;
1534                         }
1535                         if (feparm.modulation == eDVBFrontendParametersSatellite::Modulation::M8PSK)
1536                                 parm_u_qpsk_fec_inner = (fe_code_rate_t)((int)parm_u_qpsk_fec_inner+9);
1537                                 // 8PSK fec driver values are decimal 9 bigger
1538                 }
1539 #endif
1540                 // FIXME !!! get frequency range from tuner
1541                 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1542                 {
1543                         eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1544                         return -EINVAL;
1545                 }
1546                 eDebug("tuning to %d mhz", parm_frequency/1000);
1547         }
1548         return res;
1549 }
1550
1551 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1552 {
1553         parm_frequency = feparm.frequency * 1000;
1554         parm_u_qam_symbol_rate = feparm.symbol_rate;
1555         switch (feparm.modulation)
1556         {
1557         case eDVBFrontendParametersCable::Modulation::QAM16:
1558                 parm_u_qam_modulation = QAM_16;
1559                 break;
1560         case eDVBFrontendParametersCable::Modulation::QAM32:
1561                 parm_u_qam_modulation = QAM_32;
1562                 break;
1563         case eDVBFrontendParametersCable::Modulation::QAM64:
1564                 parm_u_qam_modulation = QAM_64;
1565                 break;
1566         case eDVBFrontendParametersCable::Modulation::QAM128:
1567                 parm_u_qam_modulation = QAM_128;
1568                 break;
1569         case eDVBFrontendParametersCable::Modulation::QAM256:
1570                 parm_u_qam_modulation = QAM_256;
1571                 break;
1572         default:
1573         case eDVBFrontendParametersCable::Modulation::Auto:
1574                 parm_u_qam_modulation = QAM_AUTO;
1575                 break;
1576         }
1577         switch (feparm.inversion)
1578         {
1579         case eDVBFrontendParametersCable::Inversion::On:
1580                 parm_inversion = INVERSION_ON;
1581                 break;
1582         case eDVBFrontendParametersCable::Inversion::Off:
1583                 parm_inversion = INVERSION_OFF;
1584                 break;
1585         default:
1586         case eDVBFrontendParametersCable::Inversion::Unknown:
1587                 parm_inversion = INVERSION_AUTO;
1588                 break;
1589         }
1590         switch (feparm.fec_inner)
1591         {
1592         case eDVBFrontendParametersCable::FEC::fNone:
1593                 parm_u_qam_fec_inner = FEC_NONE;
1594                 break;
1595         case eDVBFrontendParametersCable::FEC::f1_2:
1596                 parm_u_qam_fec_inner = FEC_1_2;
1597                 break;
1598         case eDVBFrontendParametersCable::FEC::f2_3:
1599                 parm_u_qam_fec_inner = FEC_2_3;
1600                 break;
1601         case eDVBFrontendParametersCable::FEC::f3_4:
1602                 parm_u_qam_fec_inner = FEC_3_4;
1603                 break;
1604         case eDVBFrontendParametersCable::FEC::f5_6:
1605                 parm_u_qam_fec_inner = FEC_5_6;
1606                 break;
1607         case eDVBFrontendParametersCable::FEC::f7_8:
1608                 parm_u_qam_fec_inner = FEC_7_8;
1609                 break;
1610 #if HAVE_DVB_API_VERSION >= 3
1611         case eDVBFrontendParametersCable::FEC::f8_9:
1612                 parm_u_qam_fec_inner = FEC_8_9;
1613                 break;
1614 #endif
1615         default:
1616         case eDVBFrontendParametersCable::FEC::fAuto:
1617                 parm_u_qam_fec_inner = FEC_AUTO;
1618                 break;
1619         }
1620         eDebug("tuning to %d khz, sr %d, fec %d, modulation %d, inversion %d",
1621                 parm_frequency/1000,
1622                 parm_u_qam_symbol_rate,
1623                 parm_u_qam_fec_inner,
1624                 parm_u_qam_modulation,
1625                 parm_inversion);
1626         return 0;
1627 }
1628
1629 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1630 {
1631         parm_frequency = feparm.frequency;
1632
1633         switch (feparm.bandwidth)
1634         {
1635         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1636                 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1637                 break;
1638         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1639                 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1640                 break;
1641         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1642                 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1643                 break;
1644         default:
1645         case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1646                 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1647                 break;
1648         }
1649         switch (feparm.code_rate_LP)
1650         {
1651         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1652                 parm_u_ofdm_code_rate_LP = FEC_1_2;
1653                 break;
1654         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1655                 parm_u_ofdm_code_rate_LP = FEC_2_3;
1656                 break;
1657         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1658                 parm_u_ofdm_code_rate_LP = FEC_3_4;
1659                 break;
1660         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1661                 parm_u_ofdm_code_rate_LP = FEC_5_6;
1662                 break;
1663         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1664                 parm_u_ofdm_code_rate_LP = FEC_7_8;
1665                 break;
1666         default:
1667         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1668                 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1669                 break;
1670         }
1671         switch (feparm.code_rate_HP)
1672         {
1673         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1674                 parm_u_ofdm_code_rate_HP = FEC_1_2;
1675                 break;
1676         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1677                 parm_u_ofdm_code_rate_HP = FEC_2_3;
1678                 break;
1679         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1680                 parm_u_ofdm_code_rate_HP = FEC_3_4;
1681                 break;
1682         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1683                 parm_u_ofdm_code_rate_HP = FEC_5_6;
1684                 break;
1685         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1686                 parm_u_ofdm_code_rate_HP = FEC_7_8;
1687                 break;
1688         default:
1689         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1690                 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1691                 break;
1692         }
1693         switch (feparm.modulation)
1694         {
1695         case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1696                 parm_u_ofdm_constellation = QPSK;
1697                 break;
1698         case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1699                 parm_u_ofdm_constellation = QAM_16;
1700                 break;
1701         case eDVBFrontendParametersTerrestrial::Modulation::QAM64:
1702                 parm_u_ofdm_constellation = QAM_64;
1703                 break;
1704         default:
1705         case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1706                 parm_u_ofdm_constellation = QAM_AUTO;
1707                 break;
1708         }
1709         switch (feparm.transmission_mode)
1710         {
1711         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1712                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1713                 break;
1714         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1715                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1716                 break;
1717         default:
1718         case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1719                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1720                 break;
1721         }
1722         switch (feparm.guard_interval)
1723         {
1724                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1725                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1726                         break;
1727                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1728                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1729                         break;
1730                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1731                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1732                         break;
1733                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1734                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1735                         break;
1736                 default:
1737                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1738                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1739                         break;
1740         }
1741         switch (feparm.hierarchy)
1742         {
1743                 case eDVBFrontendParametersTerrestrial::Hierarchy::HNone:
1744                         parm_u_ofdm_hierarchy_information = HIERARCHY_NONE;
1745                         break;
1746                 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1747                         parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1748                         break;
1749                 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1750                         parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1751                         break;
1752                 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1753                         parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1754                         break;
1755                 default:
1756                 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1757                         parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1758                         break;
1759         }
1760         switch (feparm.inversion)
1761         {
1762         case eDVBFrontendParametersTerrestrial::Inversion::On:
1763                 parm_inversion = INVERSION_ON;
1764                 break;
1765         case eDVBFrontendParametersTerrestrial::Inversion::Off:
1766                 parm_inversion = INVERSION_OFF;
1767                 break;
1768         default:
1769         case eDVBFrontendParametersTerrestrial::Inversion::Unknown:
1770                 parm_inversion = INVERSION_AUTO;
1771                 break;
1772         }
1773         return 0;
1774 }
1775
1776 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1777 {
1778         eDebug("(%d)tune", m_fe);
1779
1780         m_timeout->stop();
1781
1782         int res=0;
1783
1784         if (!m_sn)
1785         {
1786                 eDebug("no frontend device opened... do not try to tune !!!");
1787                 res = -ENODEV;
1788                 goto tune_error;
1789         }
1790
1791         if (m_type == -1)
1792         {
1793                 res = -ENODEV;
1794                 goto tune_error;
1795         }
1796
1797         m_sn->stop();
1798         m_sec_sequence.clear();
1799
1800         switch (m_type)
1801         {
1802         case feSatellite:
1803         {
1804                 eDVBFrontendParametersSatellite feparm;
1805                 if (where.getDVBS(feparm))
1806                 {
1807                         eDebug("no dvbs data!");
1808                         res = -EINVAL;
1809                         goto tune_error;
1810                 }
1811                 m_sec->setRotorMoving(false);
1812                 res=prepare_sat(feparm);
1813                 if (res)
1814                         goto tune_error;
1815
1816                 break;
1817         }
1818         case feCable:
1819         {
1820                 eDVBFrontendParametersCable feparm;
1821                 if (where.getDVBC(feparm))
1822                 {
1823                         res = -EINVAL;
1824                         goto tune_error;
1825                 }
1826                 res=prepare_cable(feparm);
1827                 if (res)
1828                         goto tune_error;
1829
1830                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1831                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1832                 break;
1833         }
1834         case feTerrestrial:
1835         {
1836                 eDVBFrontendParametersTerrestrial feparm;
1837                 if (where.getDVBT(feparm))
1838                 {
1839                         eDebug("no -T data");
1840                         res = -EINVAL;
1841                         goto tune_error;
1842                 }
1843                 res=prepare_terrestrial(feparm);
1844                 if (res)
1845                         goto tune_error;
1846
1847                 std::string enable_5V;
1848                 char configStr[255];
1849                 snprintf(configStr, 255, "config.Nims.%d.terrestrial_5V", m_fe);
1850                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1851                 ePythonConfigQuery::getConfigValue(configStr, enable_5V);
1852                 if (enable_5V == "True")
1853                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
1854                 else
1855                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltageOff) );
1856                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1857
1858                 break;
1859         }
1860         }
1861
1862         m_tuneTimer->start(0,true);
1863         m_sec_sequence.current() = m_sec_sequence.begin();
1864
1865         if (m_state != stateTuning)
1866         {
1867                 m_tuning = 1;
1868                 m_state = stateTuning;
1869                 m_stateChanged(this);
1870         }
1871
1872         return res;
1873
1874 tune_error:
1875         m_tuneTimer->stop();
1876         return res;
1877 }
1878
1879 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1880 {
1881         connection = new eConnection(this, m_stateChanged.connect(stateChange));
1882         return 0;
1883 }
1884
1885 RESULT eDVBFrontend::setVoltage(int voltage)
1886 {
1887         if (m_type == feCable)
1888                 return -1;
1889 #if HAVE_DVB_API_VERSION < 3
1890         secVoltage vlt;
1891 #else
1892         bool increased=false;
1893         fe_sec_voltage_t vlt;
1894 #endif
1895         m_data[CUR_VOLTAGE]=voltage;
1896         switch (voltage)
1897         {
1898         case voltageOff:
1899                 for (int i=0; i < 3; ++i)  // reset diseqc
1900                         m_data[i]=-1;
1901                 vlt = SEC_VOLTAGE_OFF;
1902                 break;
1903         case voltage13_5:
1904 #if HAVE_DVB_API_VERSION < 3
1905                 vlt = SEC_VOLTAGE_13_5;
1906                 break;
1907 #else
1908                 increased = true;
1909 #endif
1910         case voltage13:
1911                 vlt = SEC_VOLTAGE_13;
1912                 break;
1913         case voltage18_5:
1914 #if HAVE_DVB_API_VERSION < 3
1915                 vlt = SEC_VOLTAGE_18_5;
1916                 break;
1917 #else
1918                 increased = true;
1919 #endif
1920         case voltage18:
1921                 vlt = SEC_VOLTAGE_18;
1922                 break;
1923         default:
1924                 return -ENODEV;
1925         }
1926 #if HAVE_DVB_API_VERSION < 3
1927         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1928 #else
1929         if (m_type == feSatellite && ::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1930                 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
1931         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
1932 #endif
1933 }
1934
1935 RESULT eDVBFrontend::getState(int &state)
1936 {
1937         state = m_state;
1938         return 0;
1939 }
1940
1941 RESULT eDVBFrontend::setTone(int t)
1942 {
1943         if (m_type != feSatellite)
1944                 return -1;
1945 #if HAVE_DVB_API_VERSION < 3
1946         secToneMode_t tone;
1947 #else
1948         fe_sec_tone_mode_t tone;
1949 #endif
1950         m_data[CUR_TONE]=t;
1951         switch (t)
1952         {
1953         case toneOn:
1954                 tone = SEC_TONE_ON;
1955                 break;
1956         case toneOff:
1957                 tone = SEC_TONE_OFF;
1958                 break;
1959         default:
1960                 return -ENODEV;
1961         }
1962 #if HAVE_DVB_API_VERSION < 3    
1963         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
1964 #else   
1965         return ::ioctl(m_fd, FE_SET_TONE, tone);
1966 #endif
1967 }
1968
1969 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
1970         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
1971 #endif
1972
1973 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
1974 {
1975 #if HAVE_DVB_API_VERSION < 3
1976         struct secCommand cmd;
1977         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
1978         cmd.u.diseqc.cmdtype = diseqc.data[0];
1979         cmd.u.diseqc.addr = diseqc.data[1];
1980         cmd.u.diseqc.cmd = diseqc.data[2];
1981         cmd.u.diseqc.numParams = diseqc.len-3;
1982         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
1983         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
1984 #else
1985         struct dvb_diseqc_master_cmd cmd;
1986         memcpy(cmd.msg, diseqc.data, diseqc.len);
1987         cmd.msg_len = diseqc.len;
1988         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
1989 #endif
1990                 return -EINVAL;
1991         return 0;
1992 }
1993
1994 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
1995         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
1996 #endif
1997 RESULT eDVBFrontend::sendToneburst(int burst)
1998 {
1999 #if HAVE_DVB_API_VERSION < 3
2000         secMiniCmd cmd = SEC_MINI_NONE;
2001 #else
2002         fe_sec_mini_cmd_t cmd = SEC_MINI_A;
2003 #endif
2004         if ( burst == eDVBSatelliteDiseqcParameters::A )
2005                 cmd = SEC_MINI_A;
2006         else if ( burst == eDVBSatelliteDiseqcParameters::B )
2007                 cmd = SEC_MINI_B;
2008 #if HAVE_DVB_API_VERSION < 3
2009         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
2010                 return -EINVAL;
2011 #else
2012         if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
2013                 return -EINVAL;
2014 #endif
2015         return 0;
2016 }
2017
2018 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
2019 {
2020         m_sec = sec;
2021         return 0;
2022 }
2023
2024 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
2025 {
2026         m_sec_sequence = list;
2027         return 0;
2028 }
2029
2030 RESULT eDVBFrontend::getData(int num, int &data)
2031 {
2032         if ( num < NUM_DATA_ENTRIES )
2033         {
2034                 data = m_data[num];
2035                 return 0;
2036         }
2037         return -EINVAL;
2038 }
2039
2040 RESULT eDVBFrontend::setData(int num, int val)
2041 {
2042         if ( num < NUM_DATA_ENTRIES )
2043         {
2044                 m_data[num] = val;
2045                 return 0;
2046         }
2047         return -EINVAL;
2048 }
2049
2050 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
2051 {
2052         int type;
2053         if (feparm->getSystem(type) || type != m_type)
2054                 return 0;
2055
2056         if (m_type == eDVBFrontend::feSatellite)
2057         {
2058                 ASSERT(m_sec);
2059                 eDVBFrontendParametersSatellite sat_parm;
2060                 int ret = feparm->getDVBS(sat_parm);
2061                 ASSERT(!ret);
2062                 return m_sec->canTune(sat_parm, this, 1 << m_fe);
2063         }
2064         else if (m_type == eDVBFrontend::feCable)
2065                 return 2;  // more prio for cable frontends
2066         return 1;
2067 }