fix error handling
[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_enabled(false), m_type(-1), m_dvbid(fe), m_slotid(fe)
387         ,m_fd(-1), m_need_rotor_workaround(false), m_sn(0), m_timeout(0), m_tuneTimer(0)
388 #if HAVE_DVB_API_VERSION < 3
389         ,m_secfd(-1)
390 #endif
391 {
392 #if HAVE_DVB_API_VERSION < 3
393         sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
394         sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
395 #else
396         sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
397 #endif
398         m_timeout = new eTimer(eApp);
399         CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
400
401         m_tuneTimer = new eTimer(eApp);
402         CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
403
404         for (int i=0; i<eDVBFrontend::NUM_DATA_ENTRIES; ++i)
405                 m_data[i] = -1;
406
407         m_idleInputpower[0]=m_idleInputpower[1]=0;
408
409         ok = !openFrontend();
410         closeFrontend();
411 }
412
413 int eDVBFrontend::openFrontend()
414 {
415         if (m_sn)
416                 return -1;  // already opened
417
418         m_state=0;
419         m_tuning=0;
420
421 #if HAVE_DVB_API_VERSION < 3
422         FrontendInfo fe_info;
423 #else
424         dvb_frontend_info fe_info;
425 #endif
426         eDebug("opening frontend %d", m_dvbid);
427         if (m_fd < 0)
428         {
429                 m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
430                 if (m_fd < 0)
431                 {
432                         eWarning("failed! (%s) %m", m_filename);
433                         return -1;
434                 }
435         }
436         else
437                 eWarning("frontend %d already opened", m_dvbid);
438         if (m_type == -1)
439         {
440                 if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
441                 {
442                         eWarning("ioctl FE_GET_INFO failed");
443                         ::close(m_fd);
444                         m_fd = -1;
445                         return -1;
446                 }
447
448                 switch (fe_info.type)
449                 {
450                 case FE_QPSK:
451                         m_type = iDVBFrontend::feSatellite;
452                         break;
453                 case FE_QAM:
454                         m_type = iDVBFrontend::feCable;
455                         break;
456                 case FE_OFDM:
457                         m_type = iDVBFrontend::feTerrestrial;
458                         break;
459                 default:
460                         eWarning("unknown frontend type.");
461                         ::close(m_fd);
462                         m_fd = -1;
463                         return -1;
464                 }
465                 eDebug("detected %s frontend", "satellite\0cable\0    terrestrial"+fe_info.type*10);
466         }
467
468 #if HAVE_DVB_API_VERSION < 3
469         if (m_type == iDVBFrontend::feSatellite)
470         {
471                         if (m_secfd < 0)
472                         {
473                                 m_secfd = ::open(m_sec_filename, O_RDWR);
474                                 if (m_secfd < 0)
475                                 {
476                                         eWarning("failed! (%s) %m", m_sec_filename);
477                                         ::close(m_fd);
478                                         m_fd=-1;
479                                         return -1;
480                                 }
481                         }
482                         else
483                                 eWarning("sec %d already opened", m_dvbid);
484         }
485 #endif
486
487         setTone(iDVBFrontend::toneOff);
488         setVoltage(iDVBFrontend::voltageOff);
489
490         m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
491         CONNECT(m_sn->activated, eDVBFrontend::feEvent);
492
493         return 0;
494 }
495
496 int eDVBFrontend::closeFrontend()
497 {
498         eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[LINKED_NEXT_PTR];
499         while (linked_fe != (eDVBRegisteredFrontend*)-1)
500         {
501                 if (linked_fe->m_inuse)
502                 {
503                         eDebug("dont close frontend %d until the linked frontend %d in slot %d is still in use",
504                                 m_dvbid, linked_fe->m_frontend->getDVBID(), linked_fe->m_frontend->getSlotID());
505                         return -1;
506                 }
507                 linked_fe->m_frontend->getData(LINKED_NEXT_PTR, (int&)linked_fe);
508         }
509         if (m_fd >= 0)
510         {
511                 eDebug("close frontend %d", m_dvbid);
512                 m_tuneTimer->stop();
513                 setTone(iDVBFrontend::toneOff);
514                 setVoltage(iDVBFrontend::voltageOff);
515                 if (m_sec)
516                         m_sec->setRotorMoving(false);
517                 if (!::close(m_fd))
518                         m_fd=-1;
519                 else
520                         eWarning("couldnt close frontend %d", m_dvbid);
521                 m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1;
522         }
523 #if HAVE_DVB_API_VERSION < 3
524         if (m_secfd >= 0)
525         {
526                 if (!::close(m_secfd))
527                         m_secfd=-1;
528                 else
529                         eWarning("couldnt close sec %d", m_dvbid);
530         }
531 #endif
532         delete m_sn;
533         m_sn=0;
534
535         return 0;
536 }
537
538 eDVBFrontend::~eDVBFrontend()
539 {
540         closeFrontend();
541         delete m_timeout;
542         delete m_tuneTimer;
543 }
544
545 void eDVBFrontend::feEvent(int w)
546 {
547         while (1)
548         {
549 #if HAVE_DVB_API_VERSION < 3
550                 FrontendEvent event;
551 #else
552                 dvb_frontend_event event;
553 #endif
554                 int res;
555                 int state;
556                 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
557
558                 if (res && (errno == EAGAIN))
559                         break;
560
561                 if (res)
562                 {
563                         eWarning("FE_GET_EVENT failed! %m");
564                         return;
565                 }
566
567                 if (w < 0)
568                         continue;
569
570 #if HAVE_DVB_API_VERSION < 3
571                 if (event.type == FE_COMPLETION_EV)
572 #else
573                 eDebug("(%d)fe event: status %x, inversion %s", m_dvbid, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
574                 if (event.status & FE_HAS_LOCK)
575 #endif
576                 {
577                         state = stateLock;
578                 } else
579                 {
580                         if (m_tuning)
581                                 state = stateTuning;
582                         else
583                         {
584                                 eDebug("stateLostLock");
585                                 state = stateLostLock;
586                                 m_data[CSW] = m_data[UCSW] = m_data[TONEBURST] = -1; // reset diseqc
587                         }
588                 }
589                 if (m_state != state)
590                 {
591                         m_state = state;
592                         m_stateChanged(this);
593                 }
594         }
595 }
596
597 void eDVBFrontend::timeout()
598 {
599         m_tuning = 0;
600         if (m_state == stateTuning)
601         {
602                 m_state = stateFailed;
603                 m_stateChanged(this);
604         }
605 }
606
607 int eDVBFrontend::readFrontendData(int type)
608 {
609         switch(type)
610         {
611                 case bitErrorRate:
612                 {
613                         uint32_t ber=0;
614                         if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
615                                 eDebug("FE_READ_BER failed (%m)");
616                         return ber;
617                 }
618                 case signalPower:
619                 {
620                         uint16_t snr=0;
621                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
622                                 eDebug("FE_READ_SNR failed (%m)");
623                         return snr;
624                 }
625                 case signalPowerdB: /* this will move into the driver */
626                 {
627                         uint16_t snr=0;
628                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
629                                 eDebug("FE_READ_SNR failed (%m)");
630                         if (!strcmp(m_description, "BCM4501 (internal)"))
631                         {
632                                 unsigned int SDS_SNRE = snr << 16;
633
634                                 static float SNR_COEFF[6] = {
635                                         100.0 / 4194304.0,
636                                         -7136.0 / 4194304.0,
637                                         197418.0 / 4194304.0,
638                                         -2602183.0 / 4194304.0,
639                                         20377212.0 / 4194304.0,
640                                         -37791203.0 / 4194304.0,
641                                 };
642                         
643                                 float fval1, fval2, snr_in_db;
644                                 int i;
645                                 fval1 = 12.44714 - (2.0 * log10(SDS_SNRE / 256.0));
646                                 fval2 = pow(10.0, fval1)-1;
647                                 fval1 = 10.0 * log10(fval2);
648                         
649                                 if (fval1 < 10.0)
650                                 {
651                                         fval2 = SNR_COEFF[0];
652                                         for (i=0; i<6; ++i)
653                                         {
654                                                 fval2 *= fval1;
655                                                 fval2 += SNR_COEFF[i];
656                                         }
657                                         fval1 = fval2;
658                                 }
659                                 snr_in_db = fval1;
660                         
661                                 return (int)(snr_in_db * 100.0);
662                         }
663                         else if (!strcmp(m_description, "Alps BSBE1 702A") ||  // some frontends with STV0299
664                                 !strcmp(m_description, "Alps -S") ||
665                                 !strcmp(m_description, "Philips -S") ||
666                                 !strcmp(m_description, "LG -S") )
667                         {
668                                 float snr_in_db=(snr-39075)/1764.7;
669                                 return (int)(snr_in_db * 100.0);
670                         }
671                         else
672                                 eDebug("no SNR dB caluclation for frontendtype %s yet", m_description);
673                         return 0x12345678;
674                 }
675                 case signalQuality:
676                 {
677                         uint16_t strength=0;
678                         if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
679                                 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
680                         return strength;
681                 }
682                 case locked:
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_LOCK);
692                 }
693                 case synced:
694                 {
695 #if HAVE_DVB_API_VERSION < 3
696                         FrontendStatus status=0;
697 #else
698                         fe_status_t status;
699 #endif
700                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
701                                 eDebug("FE_READ_STATUS failed (%m)");
702                         return !!(status&FE_HAS_SYNC);
703                 }
704                 case frontendNumber:
705                         return m_slotid;
706         }
707         return 0;
708 }
709
710 void PutToDict(ePyObject &dict, const char*key, long value)
711 {
712         ePyObject item = PyInt_FromLong(value);
713         if (item)
714         {
715                 if (PyDict_SetItemString(dict, key, item))
716                         eDebug("put %s to dict failed", key);
717                 Py_DECREF(item);
718         }
719         else
720                 eDebug("could not create PyObject for %s", key);
721 }
722
723 void PutToDict(ePyObject &dict, const char*key, ePyObject item)
724 {
725         if (item)
726         {
727                 if (PyDict_SetItemString(dict, key, item))
728                         eDebug("put %s to dict failed", key);
729                 Py_DECREF(item);
730         }
731         else
732                 eDebug("invalid PyObject for %s", key);
733 }
734
735 void PutToDict(ePyObject &dict, const char*key, const char *value)
736 {
737         ePyObject item = PyString_FromString(value);
738         if (item)
739         {
740                 if (PyDict_SetItemString(dict, key, item))
741                         eDebug("put %s to dict failed", key);
742                 Py_DECREF(item);
743         }
744         else
745                 eDebug("could not create PyObject for %s", key);
746 }
747
748 void fillDictWithSatelliteData(ePyObject dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
749 {
750         int freq_offset=0;
751         int csw=0;
752         const char *tmp=0;
753         fe->getData(eDVBFrontend::CSW, csw);
754         fe->getData(eDVBFrontend::FREQ_OFFSET, freq_offset);
755         int frequency = parm_frequency + freq_offset;
756         PutToDict(dict, "frequency", frequency);
757         PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
758         switch(parm_u_qpsk_fec_inner)
759         {
760         case FEC_1_2:
761                 tmp = "FEC_1_2";
762                 break;
763         case FEC_2_3:
764                 tmp = "FEC_2_3";
765                 break;
766         case FEC_3_4:
767                 tmp = "FEC_3_4";
768                 break;
769         case FEC_5_6:
770                 tmp = "FEC_5_6";
771                 break;
772         case FEC_7_8:
773                 tmp = "FEC_7_8";
774                 break;
775         case FEC_NONE:
776                 tmp = "FEC_NONE";
777         default:
778         case FEC_AUTO:
779                 tmp = "FEC_AUTO";
780                 break;
781 #if HAVE_DVB_API_VERSION >=3
782         case FEC_S2_8PSK_1_2:
783         case FEC_S2_QPSK_1_2:
784                 tmp = "FEC_1_2";
785                 break;
786         case FEC_S2_8PSK_2_3:
787         case FEC_S2_QPSK_2_3:
788                 tmp = "FEC_2_3";
789                 break;
790         case FEC_S2_8PSK_3_4:
791         case FEC_S2_QPSK_3_4:
792                 tmp = "FEC_3_4";
793                 break;
794         case FEC_S2_8PSK_5_6:
795         case FEC_S2_QPSK_5_6:
796                 tmp = "FEC_5_6";
797                 break;
798         case FEC_S2_8PSK_7_8:
799         case FEC_S2_QPSK_7_8:
800                 tmp = "FEC_7_8";
801                 break;
802         case FEC_S2_8PSK_8_9:
803         case FEC_S2_QPSK_8_9:
804                 tmp = "FEC_8_9";
805                 break;
806         case FEC_S2_8PSK_3_5:
807         case FEC_S2_QPSK_3_5:
808                 tmp = "FEC_3_5";
809                 break;
810         case FEC_S2_8PSK_4_5:
811         case FEC_S2_QPSK_4_5:
812                 tmp = "FEC_4_5";
813                 break;
814         case FEC_S2_8PSK_9_10:
815         case FEC_S2_QPSK_9_10:
816                 tmp = "FEC_9_10";
817                 break;
818 #endif
819         }
820 #if HAVE_DVB_API_VERSION >=3
821         PutToDict(dict, "modulation",
822                 parm_u_qpsk_fec_inner > FEC_S2_QPSK_9_10 ? "8PSK": "QPSK" );
823 #else
824         PutToDict(dict, "modulation", "QPSK" );
825 #endif
826         PutToDict(dict, "fec_inner", tmp);
827         tmp = parm_u_qpsk_fec_inner > FEC_AUTO ?
828                 "DVB-S2" : "DVB-S";
829         PutToDict(dict, "system", tmp);
830 }
831
832 void fillDictWithCableData(ePyObject dict, const FRONTENDPARAMETERS &parm)
833 {
834         const char *tmp=0;
835         PutToDict(dict, "frequency", parm_frequency/1000);
836         PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
837         switch(parm_u_qam_fec_inner)
838         {
839         case FEC_NONE:
840                 tmp = "FEC_NONE";
841                 break;
842         case FEC_1_2:
843                 tmp = "FEC_1_2";
844                 break;
845         case FEC_2_3:
846                 tmp = "FEC_2_3";
847                 break;
848         case FEC_3_4:
849                 tmp = "FEC_3_4";
850                 break;
851         case FEC_5_6:
852                 tmp = "FEC_5_6";
853                 break;
854         case FEC_7_8:
855                 tmp = "FEC_7_8";
856                 break;
857 #if HAVE_DVB_API_VERSION >= 3
858         case FEC_8_9:
859                 tmp = "FEC_8_9";
860                 break;
861 #endif
862         default:
863         case FEC_AUTO:
864                 tmp = "FEC_AUTO";
865                 break;
866         }
867         PutToDict(dict, "fec_inner", tmp);
868         switch(parm_u_qam_modulation)
869         {
870         case QAM_16:
871                 tmp = "QAM_16";
872                 break;
873         case QAM_32:
874                 tmp = "QAM_32";
875                 break;
876         case QAM_64:
877                 tmp = "QAM_64";
878                 break;
879         case QAM_128:
880                 tmp = "QAM_128";
881                 break;
882         case QAM_256:
883                 tmp = "QAM_256";
884                 break;
885         default:
886         case QAM_AUTO:
887                 tmp = "QAM_AUTO";
888                 break;
889         }
890         PutToDict(dict, "modulation", tmp);
891 }
892
893 void fillDictWithTerrestrialData(ePyObject dict, const FRONTENDPARAMETERS &parm)
894 {
895         const char *tmp=0;
896         PutToDict(dict, "frequency", parm_frequency);
897         switch (parm_u_ofdm_bandwidth)
898         {
899         case BANDWIDTH_8_MHZ:
900                 tmp = "BANDWIDTH_8_MHZ";
901                 break;
902         case BANDWIDTH_7_MHZ:
903                 tmp = "BANDWIDTH_7_MHZ";
904                 break;
905         case BANDWIDTH_6_MHZ:
906                 tmp = "BANDWIDTH_6_MHZ";
907                 break;
908         default:
909         case BANDWIDTH_AUTO:
910                 tmp = "BANDWIDTH_AUTO";
911                 break;
912         }
913         PutToDict(dict, "bandwidth", tmp);
914         switch (parm_u_ofdm_code_rate_LP)
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_lp", tmp);
937         switch (parm_u_ofdm_code_rate_HP)
938         {
939         case FEC_1_2:
940                 tmp = "FEC_1_2";
941                 break;
942         case FEC_2_3:
943                 tmp = "FEC_2_3";
944                 break;
945         case FEC_3_4:
946                 tmp = "FEC_3_4";
947                 break;
948         case FEC_5_6:
949                 tmp = "FEC_5_6";
950                 break;
951         case FEC_7_8:
952                 tmp = "FEC_7_8";
953                 break;
954         default:
955         case FEC_AUTO:
956                 tmp = "FEC_AUTO";
957                 break;
958         }
959         PutToDict(dict, "code_rate_hp", tmp);
960         switch (parm_u_ofdm_constellation)
961         {
962         case QPSK:
963                 tmp = "QPSK";
964                 break;
965         case QAM_16:
966                 tmp = "QAM_16";
967                 break;
968         case QAM_64:
969                 tmp = "QAM_64";
970                 break;
971         default:
972         case QAM_AUTO:
973                 tmp = "QAM_AUTO";
974                 break;
975         }
976         PutToDict(dict, "constellation", tmp);
977         switch (parm_u_ofdm_transmission_mode)
978         {
979         case TRANSMISSION_MODE_2K:
980                 tmp = "TRANSMISSION_MODE_2K";
981                 break;
982         case TRANSMISSION_MODE_8K:
983                 tmp = "TRANSMISSION_MODE_8K";
984                 break;
985         default:
986         case TRANSMISSION_MODE_AUTO:
987                 tmp = "TRANSMISSION_MODE_AUTO";
988                 break;
989         }
990         PutToDict(dict, "transmission_mode", tmp);
991         switch (parm_u_ofdm_guard_interval)
992         {
993                 case GUARD_INTERVAL_1_32:
994                         tmp = "GUARD_INTERVAL_1_32";
995                         break;
996                 case GUARD_INTERVAL_1_16:
997                         tmp = "GUARD_INTERVAL_1_16";
998                         break;
999                 case GUARD_INTERVAL_1_8:
1000                         tmp = "GUARD_INTERVAL_1_8";
1001                         break;
1002                 case GUARD_INTERVAL_1_4:
1003                         tmp = "GUARD_INTERVAL_1_4";
1004                         break;
1005                 default:
1006                 case GUARD_INTERVAL_AUTO:
1007                         tmp = "GUARD_INTERVAL_AUTO";
1008                         break;
1009         }
1010         PutToDict(dict, "guard_interval", tmp);
1011         switch (parm_u_ofdm_hierarchy_information)
1012         {
1013                 case HIERARCHY_NONE:
1014                         tmp = "HIERARCHY_NONE";
1015                         break;
1016                 case HIERARCHY_1:
1017                         tmp = "HIERARCHY_1";
1018                         break;
1019                 case HIERARCHY_2:
1020                         tmp = "HIERARCHY_2";
1021                         break;
1022                 case HIERARCHY_4:
1023                         tmp = "HIERARCHY_4";
1024                         break;
1025                 default:
1026                 case HIERARCHY_AUTO:
1027                         tmp = "HIERARCHY_AUTO";
1028                         break;
1029         }
1030         PutToDict(dict, "hierarchy_information", tmp);
1031 }
1032
1033 void eDVBFrontend::getFrontendStatus(ePyObject dest)
1034 {
1035         if (dest && PyDict_Check(dest))
1036         {
1037                 const char *tmp = "UNKNOWN";
1038                 switch(m_state)
1039                 {
1040                         case stateIdle:
1041                                 tmp="IDLE";
1042                                 break;
1043                         case stateTuning:
1044                                 tmp="TUNING";
1045                                 break;
1046                         case stateFailed:
1047                                 tmp="FAILED";
1048                                 break;
1049                         case stateLock:
1050                                 tmp="LOCKED";
1051                                 break;
1052                         case stateLostLock:
1053                                 tmp="LOSTLOCK";
1054                                 break;
1055                         default:
1056                                 break;
1057                 }
1058                 PutToDict(dest, "tuner_state", tmp);
1059                 PutToDict(dest, "tuner_locked", readFrontendData(locked));
1060                 PutToDict(dest, "tuner_synced", readFrontendData(synced));
1061                 PutToDict(dest, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
1062                 PutToDict(dest, "tuner_signal_power", readFrontendData(signalPower));
1063                 int sigPowerdB = readFrontendData(signalPowerdB);
1064                 if (sigPowerdB == 0x12345678) // not support yet
1065                 {
1066                         ePyObject obj=Py_None;
1067                         Py_INCREF(obj);
1068                         PutToDict(dest, "tuner_signal_power_db", obj);
1069                 }
1070                 else
1071                         PutToDict(dest, "tuner_signal_power_db", sigPowerdB);
1072                 PutToDict(dest, "tuner_signal_quality", readFrontendData(signalQuality));
1073         }
1074 }
1075
1076 void eDVBFrontend::getTransponderData(ePyObject dest, bool original)
1077 {
1078         if (m_fd != -1 && dest && PyDict_Check(dest))
1079         {
1080                 switch(m_type)
1081                 {
1082                         case feSatellite:
1083                         case feCable:
1084                         case feTerrestrial:
1085                         {
1086                                 FRONTENDPARAMETERS front;
1087                                 if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
1088                                         eDebug("FE_GET_FRONTEND (%m)");
1089                                 else
1090                                 {
1091                                         const FRONTENDPARAMETERS &parm = original ? this->parm : front;
1092                                         const char *tmp = "INVERSION_AUTO";
1093                                         switch(parm_inversion)
1094                                         {
1095                                                 case INVERSION_ON:
1096                                                         tmp = "INVERSION_ON";
1097                                                         break;
1098                                                 case INVERSION_OFF:
1099                                                         tmp = "INVERSION_OFF";
1100                                                         break;
1101                                                 default:
1102                                                         break;
1103                                         }
1104                                         if (tmp)
1105                                                 PutToDict(dest, "inversion", tmp);
1106
1107                                         switch(m_type)
1108                                         {
1109                                                 case feSatellite:
1110                                                         fillDictWithSatelliteData(dest, original?parm:front, this);
1111                                                         break;
1112                                                 case feCable:
1113                                                         fillDictWithCableData(dest, original?parm:front);
1114                                                         break;
1115                                                 case feTerrestrial:
1116                                                         fillDictWithTerrestrialData(dest, original?parm:front);
1117                                                         break;
1118                                         }
1119                                 }
1120                         }
1121                         default:
1122                                 break;
1123                 }
1124         }
1125 }
1126
1127 void eDVBFrontend::getFrontendData(ePyObject dest)
1128 {
1129         if (dest && PyDict_Check(dest))
1130         {
1131                 const char *tmp=0;
1132                 PutToDict(dest, "tuner_number", m_dvbid);
1133                 switch(m_type)
1134                 {
1135                         case feSatellite:
1136                                 tmp = "DVB-S";
1137                                 break;
1138                         case feCable:
1139                                 tmp = "DVB-C";
1140                                 break;
1141                         case feTerrestrial:
1142                                 tmp = "DVB-T";
1143                                 break;
1144                         default:
1145                                 tmp = "UNKNOWN";
1146                                 break;
1147                 }
1148                 PutToDict(dest, "tuner_type", tmp);
1149         }
1150 }
1151
1152 #ifndef FP_IOCTL_GET_ID
1153 #define FP_IOCTL_GET_ID 0
1154 #endif
1155 int eDVBFrontend::readInputpower()
1156 {
1157         int power=m_slotid;  // this is needed for read inputpower from the correct tuner !
1158
1159         // open front prozessor
1160         int fp=::open("/dev/dbox/fp0", O_RDWR);
1161         if (fp < 0)
1162         {
1163                 eDebug("couldn't open fp");
1164                 return -1;
1165         }
1166         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
1167         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
1168         {
1169                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
1170                 return -1;
1171         }
1172         ::close(fp);
1173
1174         return power;
1175 }
1176
1177 bool eDVBFrontend::setSecSequencePos(int steps)
1178 {
1179         eDebug("set sequence pos %d", steps);
1180         if (!steps)
1181                 return false;
1182         while( steps > 0 )
1183         {
1184                 if (m_sec_sequence.current() != m_sec_sequence.end())
1185                         ++m_sec_sequence.current();
1186                 --steps;
1187         }
1188         while( steps < 0 )
1189         {
1190                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
1191                         --m_sec_sequence.current();
1192                 ++steps;
1193         }
1194         return true;
1195 }
1196
1197 void eDVBFrontend::setRotorData(int pos, int cmd)
1198 {
1199         m_data[ROTOR_CMD] = cmd;
1200         m_data[ROTOR_POS] = pos;
1201         if ( m_data[SATPOS_DEPENDS_PTR] != -1 )
1202         {
1203                 eDVBRegisteredFrontend *satpos_depends_to_fe = (eDVBRegisteredFrontend*) m_data[SATPOS_DEPENDS_PTR];
1204                 satpos_depends_to_fe->m_frontend->m_data[ROTOR_CMD] = cmd;
1205                 satpos_depends_to_fe->m_frontend->m_data[ROTOR_POS] = pos;
1206         }
1207         else
1208         {
1209                 eDVBRegisteredFrontend *next = (eDVBRegisteredFrontend *)m_data[LINKED_NEXT_PTR];
1210                 while ( (int)next != -1 )
1211                 {
1212                         next->m_frontend->m_data[ROTOR_CMD] = cmd;
1213                         next->m_frontend->m_data[ROTOR_POS] = pos;
1214                         next = (eDVBRegisteredFrontend *)next->m_frontend->m_data[LINKED_NEXT_PTR];
1215                 }
1216                 eDVBRegisteredFrontend *prev = (eDVBRegisteredFrontend *)m_data[LINKED_PREV_PTR];
1217                 while ( (int)prev != -1 )
1218                 {
1219                         prev->m_frontend->m_data[ROTOR_CMD] = cmd;
1220                         prev->m_frontend->m_data[ROTOR_POS] = pos;
1221                         prev = (eDVBRegisteredFrontend *)prev->m_frontend->m_data[LINKED_PREV_PTR];
1222                 }
1223         }
1224 }
1225
1226 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
1227 {
1228         int delay=0;
1229         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
1230         {
1231 //              eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
1232                 switch (m_sec_sequence.current()->cmd)
1233                 {
1234                         case eSecCommand::SLEEP:
1235                                 delay = m_sec_sequence.current()++->msec;
1236                                 eDebug("[SEC] sleep %dms", delay);
1237                                 break;
1238                         case eSecCommand::GOTO:
1239                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
1240                                         ++m_sec_sequence.current();
1241                                 break;
1242                         case eSecCommand::SET_VOLTAGE:
1243                         {
1244                                 int voltage = m_sec_sequence.current()++->voltage;
1245                                 eDebug("[SEC] setVoltage %d", voltage);
1246                                 setVoltage(voltage);
1247                                 break;
1248                         }
1249                         case eSecCommand::IF_VOLTAGE_GOTO:
1250                         {
1251                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1252                                 if ( compare.voltage == m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1253                                         break;
1254                                 ++m_sec_sequence.current();
1255                                 break;
1256                         }
1257                         case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1258                         {
1259                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1260                                 if ( compare.voltage != m_data[CUR_VOLTAGE] && setSecSequencePos(compare.steps) )
1261                                         break;
1262                                 ++m_sec_sequence.current();
1263                                 break;
1264                         }
1265                         case eSecCommand::IF_TONE_GOTO:
1266                         {
1267                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1268                                 if ( compare.tone == m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1269                                         break;
1270                                 ++m_sec_sequence.current();
1271                                 break;
1272                         }
1273                         case eSecCommand::IF_NOT_TONE_GOTO:
1274                         {
1275                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1276                                 if ( compare.tone != m_data[CUR_TONE] && setSecSequencePos(compare.steps) )
1277                                         break;
1278                                 ++m_sec_sequence.current();
1279                                 break;
1280                         }
1281                         case eSecCommand::SET_TONE:
1282                                 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1283                                 setTone(m_sec_sequence.current()++->tone);
1284                                 break;
1285                         case eSecCommand::SEND_DISEQC:
1286                                 sendDiseqc(m_sec_sequence.current()->diseqc);
1287                                 eDebugNoNewLine("[SEC] sendDiseqc: ");
1288                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1289                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1290                                 eDebug("");
1291                                 ++m_sec_sequence.current();
1292                                 break;
1293                         case eSecCommand::SEND_TONEBURST:
1294                                 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1295                                 sendToneburst(m_sec_sequence.current()++->toneburst);
1296                                 break;
1297                         case eSecCommand::SET_FRONTEND:
1298                                 eDebug("[SEC] setFrontend");
1299                                 setFrontend();
1300                                 ++m_sec_sequence.current();
1301                                 break;
1302                         case eSecCommand::START_TUNE_TIMEOUT:
1303                                 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1304                                 ++m_sec_sequence.current();
1305                                 break;
1306                         case eSecCommand::SET_TIMEOUT:
1307                                 m_timeoutCount = m_sec_sequence.current()++->val;
1308                                 eDebug("[SEC] set timeout %d", m_timeoutCount);
1309                                 break;
1310                         case eSecCommand::IF_TIMEOUT_GOTO:
1311                                 if (!m_timeoutCount)
1312                                 {
1313                                         eDebug("[SEC] rotor timout");
1314                                         m_sec->setRotorMoving(false);
1315                                         setSecSequencePos(m_sec_sequence.current()->steps);
1316                                 }
1317                                 else
1318                                         ++m_sec_sequence.current();
1319                                 break;
1320                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1321                         {
1322                                 int idx = m_sec_sequence.current()++->val;
1323                                 if ( idx == 0 || idx == 1 )
1324                                 {
1325                                         m_idleInputpower[idx] = readInputpower();
1326                                         eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1327                                 }
1328                                 else
1329                                         eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1330                                 break;
1331                         }
1332                         case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1333                         {
1334                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1335                                 int idx = compare.val;
1336                                 if ( idx == 0 || idx == 1 )
1337                                 {
1338                                         int idle = readInputpower();
1339                                         int diff = abs(idle-m_idleInputpower[idx]);
1340                                         if ( diff > 0)
1341                                         {
1342                                                 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1343                                                 setSecSequencePos(compare.steps);
1344                                                 break;
1345                                         }
1346                                 }
1347                                 ++m_sec_sequence.current();
1348                                 break;
1349                         }
1350                         case eSecCommand::IF_TUNER_LOCKED_GOTO:
1351                         {
1352                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1353                                 if (readFrontendData(locked))
1354                                 {
1355                                         eDebug("[SEC] locked step %d ok", cmd.okcount);
1356                                         ++cmd.okcount;
1357                                         if (cmd.okcount > 12)
1358                                         {
1359                                                 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1360                                                 setSecSequencePos(cmd.steps);
1361                                                 break;
1362                                         }
1363                                 }
1364                                 else
1365                                 {
1366                                         eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1367                                         --m_timeoutCount;
1368                                         if (!m_timeoutCount && m_retryCount > 0)
1369                                                 --m_retryCount;
1370                                         cmd.okcount=0;
1371                                 }
1372                                 ++m_sec_sequence.current();
1373                                 break;
1374                         }
1375                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1376                                 m_runningInputpower = readInputpower();
1377                                 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1378                                 ++m_sec_sequence.current();
1379                                 break;
1380                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1381                         {
1382                                 int idleInputpower = m_idleInputpower[ (m_data[CUR_VOLTAGE]&1) ? 0 : 1];
1383                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1384                                 const char *txt = cmd.direction ? "running" : "stopped";
1385                                 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1386                                         txt,
1387                                         m_runningInputpower,
1388                                         idleInputpower,
1389                                         cmd.deltaA);
1390                                 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1391                                         || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1392                                 {
1393                                         ++cmd.okcount;
1394                                         eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1395                                         if ( cmd.okcount > 6 )
1396                                         {
1397                                                 m_sec->setRotorMoving(cmd.direction);
1398                                                 eDebug("[SEC] rotor is %s", txt);
1399                                                 if (setSecSequencePos(cmd.steps))
1400                                                         break;
1401                                         }
1402                                 }
1403                                 else
1404                                 {
1405                                         eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1406                                         --m_timeoutCount;
1407                                         if (!m_timeoutCount && m_retryCount > 0)
1408                                                 --m_retryCount;
1409                                         cmd.okcount=0;
1410                                 }
1411                                 ++m_sec_sequence.current();
1412                                 break;
1413                         }
1414                         case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1415                                 if (m_data[ROTOR_CMD] != -1 && m_data[ROTOR_POS] != -1)
1416                                         setSecSequencePos(m_sec_sequence.current()->steps);
1417                                 else
1418                                         ++m_sec_sequence.current();
1419                                 break;
1420                         case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1421                                 eDebug("[SEC] invalidate current rotorparams");
1422                                 setRotorData(-1,-1);    
1423                                 ++m_sec_sequence.current();
1424                                 break;
1425                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1426                                 setRotorData(m_data[NEW_ROTOR_POS], m_data[NEW_ROTOR_CMD]);
1427                                 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[ROTOR_CMD], m_data[ROTOR_POS]);
1428                                 ++m_sec_sequence.current();
1429                                 break;
1430                         case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1431                                 m_retryCount = m_sec_sequence.current()++->val;
1432                                 eDebug("[SEC] set rotor retries %d", m_retryCount);
1433                                 break;
1434                         case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1435                                 if (!m_retryCount)
1436                                 {
1437                                         eDebug("[SEC] no more rotor retrys");
1438                                         setSecSequencePos(m_sec_sequence.current()->steps);
1439                                 }
1440                                 else
1441                                         ++m_sec_sequence.current();
1442                                 break;
1443                         case eSecCommand::SET_POWER_LIMITING_MODE:
1444                         {
1445                                 if (!m_need_rotor_workaround)
1446                                         break;
1447
1448                                 char dev[16];
1449
1450                                 // FIXMEEEEEE hardcoded i2c devices for dm7025 and dm8000
1451                                 if (m_slotid < 2)
1452                                         sprintf(dev, "/dev/i2c/%d", m_slotid);
1453                                 else if (m_slotid == 2)
1454                                         sprintf(dev, "/dev/i2c/2"); // first nim socket on DM8000 use /dev/i2c/2
1455                                 else if (m_slotid == 3)
1456                                         sprintf(dev, "/dev/i2c/4"); // second nim socket on DM8000 use /dev/i2c/4
1457                                 int fd = ::open(dev, O_RDWR);
1458
1459                                 unsigned char data[2];
1460                                 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1461                                 if(::read(fd, data, 1) != 1)
1462                                         eDebug("[SEC] error read lnbp (%m)");
1463                                 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1464                                 {
1465                                         data[0] |= 0x80;  // enable static current limiting
1466                                         eDebug("[SEC] set static current limiting");
1467                                 }
1468                                 else
1469                                 {
1470                                         data[0] &= ~0x80;  // enable dynamic current limiting
1471                                         eDebug("[SEC] set dynamic current limiting");
1472                                 }
1473                                 if(::write(fd, data, 1) != 1)
1474                                         eDebug("[SEC] error write lnbp (%m)");
1475                                 ::close(fd);
1476                                 ++m_sec_sequence.current();
1477                                 break;
1478                         }
1479                         default:
1480                                 eDebug("[SEC] unhandled sec command %d",
1481                                         ++m_sec_sequence.current()->cmd);
1482                                 ++m_sec_sequence.current();
1483                 }
1484                 m_tuneTimer->start(delay,true);
1485         }
1486 }
1487
1488 void eDVBFrontend::setFrontend()
1489 {
1490         eDebug("setting frontend %d", m_dvbid);
1491         m_sn->start();
1492         feEvent(-1);
1493         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1494         {
1495                 perror("FE_SET_FRONTEND failed");
1496                 return;
1497         }
1498 }
1499
1500 RESULT eDVBFrontend::getFrontendType(int &t)
1501 {
1502         if (m_type == -1)
1503                 return -ENODEV;
1504         t = m_type;
1505         return 0;
1506 }
1507
1508 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1509 {
1510         int res;
1511         if (!m_sec)
1512         {
1513                 eWarning("no SEC module active!");
1514                 return -ENOENT;
1515         }
1516         res = m_sec->prepare(*this, parm, feparm, 1 << m_slotid);
1517         if (!res)
1518         {
1519                 eDebug("prepare_sat System %d Freq %d Pol %d SR %d INV %d FEC %d orbpos %d",
1520                         feparm.system,
1521                         feparm.frequency,
1522                         feparm.polarisation,
1523                         feparm.symbol_rate,
1524                         feparm.inversion,
1525                         feparm.fec,
1526                         feparm.orbital_position);
1527                 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1528                 switch (feparm.inversion)
1529                 {
1530                         case eDVBFrontendParametersSatellite::Inversion::On:
1531                                 parm_inversion = INVERSION_ON;
1532                                 break;
1533                         case eDVBFrontendParametersSatellite::Inversion::Off:
1534                                 parm_inversion = INVERSION_OFF;
1535                                 break;
1536                         default:
1537                         case eDVBFrontendParametersSatellite::Inversion::Unknown:
1538                                 parm_inversion = INVERSION_AUTO;
1539                                 break;
1540                 }
1541                 if (feparm.system == eDVBFrontendParametersSatellite::System::DVB_S)
1542                         switch (feparm.fec)
1543                         {
1544                                 case eDVBFrontendParametersSatellite::FEC::fNone:
1545                                         parm_u_qpsk_fec_inner = FEC_NONE;
1546                                         break;
1547                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1548                                         parm_u_qpsk_fec_inner = FEC_1_2;
1549                                         break;
1550                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1551                                         parm_u_qpsk_fec_inner = FEC_2_3;
1552                                         break;
1553                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1554                                         parm_u_qpsk_fec_inner = FEC_3_4;
1555                                         break;
1556                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1557                                         parm_u_qpsk_fec_inner = FEC_5_6;
1558                                         break;
1559                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1560                                         parm_u_qpsk_fec_inner = FEC_7_8;
1561                                         break;
1562                                 default:
1563                                         eDebug("no valid fec for DVB-S set.. assume auto");
1564                                 case eDVBFrontendParametersSatellite::FEC::fAuto:
1565                                         parm_u_qpsk_fec_inner = FEC_AUTO;
1566                                         break;
1567                         }
1568 #if HAVE_DVB_API_VERSION >= 3
1569                 else // DVB_S2
1570                 {
1571                         switch (feparm.fec)
1572                         {
1573                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
1574                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_1_2;
1575                                         break;
1576                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
1577                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_2_3;
1578                                         break;
1579                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
1580                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_4;
1581                                         break;
1582                                 case eDVBFrontendParametersSatellite::FEC::f3_5:
1583                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_3_5;
1584                                         break;
1585                                 case eDVBFrontendParametersSatellite::FEC::f4_5:
1586                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_4_5;
1587                                         break;
1588                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
1589                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_5_6;
1590                                         break;
1591                                 case eDVBFrontendParametersSatellite::FEC::f7_8:
1592                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_7_8;
1593                                         break;
1594                                 case eDVBFrontendParametersSatellite::FEC::f8_9:
1595                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_8_9;
1596                                         break;
1597                                 case eDVBFrontendParametersSatellite::FEC::f9_10:
1598                                         parm_u_qpsk_fec_inner = FEC_S2_QPSK_9_10;
1599                                         break;
1600                                 default:
1601                                         eDebug("no valid fec for DVB-S2 set.. abort !!");
1602                                         return -EINVAL;
1603                         }
1604                         if (feparm.modulation == eDVBFrontendParametersSatellite::Modulation::M8PSK)
1605                                 parm_u_qpsk_fec_inner = (fe_code_rate_t)((int)parm_u_qpsk_fec_inner+9);
1606                                 // 8PSK fec driver values are decimal 9 bigger
1607                 }
1608 #endif
1609                 // FIXME !!! get frequency range from tuner
1610                 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1611                 {
1612                         eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1613                         return -EINVAL;
1614                 }
1615                 eDebug("tuning to %d mhz", parm_frequency/1000);
1616         }
1617         return res;
1618 }
1619
1620 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1621 {
1622         parm_frequency = feparm.frequency * 1000;
1623         parm_u_qam_symbol_rate = feparm.symbol_rate;
1624         switch (feparm.modulation)
1625         {
1626         case eDVBFrontendParametersCable::Modulation::QAM16:
1627                 parm_u_qam_modulation = QAM_16;
1628                 break;
1629         case eDVBFrontendParametersCable::Modulation::QAM32:
1630                 parm_u_qam_modulation = QAM_32;
1631                 break;
1632         case eDVBFrontendParametersCable::Modulation::QAM64:
1633                 parm_u_qam_modulation = QAM_64;
1634                 break;
1635         case eDVBFrontendParametersCable::Modulation::QAM128:
1636                 parm_u_qam_modulation = QAM_128;
1637                 break;
1638         case eDVBFrontendParametersCable::Modulation::QAM256:
1639                 parm_u_qam_modulation = QAM_256;
1640                 break;
1641         default:
1642         case eDVBFrontendParametersCable::Modulation::Auto:
1643                 parm_u_qam_modulation = QAM_AUTO;
1644                 break;
1645         }
1646         switch (feparm.inversion)
1647         {
1648         case eDVBFrontendParametersCable::Inversion::On:
1649                 parm_inversion = INVERSION_ON;
1650                 break;
1651         case eDVBFrontendParametersCable::Inversion::Off:
1652                 parm_inversion = INVERSION_OFF;
1653                 break;
1654         default:
1655         case eDVBFrontendParametersCable::Inversion::Unknown:
1656                 parm_inversion = INVERSION_AUTO;
1657                 break;
1658         }
1659         switch (feparm.fec_inner)
1660         {
1661         case eDVBFrontendParametersCable::FEC::fNone:
1662                 parm_u_qam_fec_inner = FEC_NONE;
1663                 break;
1664         case eDVBFrontendParametersCable::FEC::f1_2:
1665                 parm_u_qam_fec_inner = FEC_1_2;
1666                 break;
1667         case eDVBFrontendParametersCable::FEC::f2_3:
1668                 parm_u_qam_fec_inner = FEC_2_3;
1669                 break;
1670         case eDVBFrontendParametersCable::FEC::f3_4:
1671                 parm_u_qam_fec_inner = FEC_3_4;
1672                 break;
1673         case eDVBFrontendParametersCable::FEC::f5_6:
1674                 parm_u_qam_fec_inner = FEC_5_6;
1675                 break;
1676         case eDVBFrontendParametersCable::FEC::f7_8:
1677                 parm_u_qam_fec_inner = FEC_7_8;
1678                 break;
1679 #if HAVE_DVB_API_VERSION >= 3
1680         case eDVBFrontendParametersCable::FEC::f8_9:
1681                 parm_u_qam_fec_inner = FEC_8_9;
1682                 break;
1683 #endif
1684         default:
1685         case eDVBFrontendParametersCable::FEC::fAuto:
1686                 parm_u_qam_fec_inner = FEC_AUTO;
1687                 break;
1688         }
1689         eDebug("tuning to %d khz, sr %d, fec %d, modulation %d, inversion %d",
1690                 parm_frequency/1000,
1691                 parm_u_qam_symbol_rate,
1692                 parm_u_qam_fec_inner,
1693                 parm_u_qam_modulation,
1694                 parm_inversion);
1695         return 0;
1696 }
1697
1698 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1699 {
1700         parm_frequency = feparm.frequency;
1701
1702         switch (feparm.bandwidth)
1703         {
1704         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1705                 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1706                 break;
1707         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1708                 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1709                 break;
1710         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1711                 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1712                 break;
1713         default:
1714         case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1715                 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1716                 break;
1717         }
1718         switch (feparm.code_rate_LP)
1719         {
1720         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1721                 parm_u_ofdm_code_rate_LP = FEC_1_2;
1722                 break;
1723         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1724                 parm_u_ofdm_code_rate_LP = FEC_2_3;
1725                 break;
1726         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1727                 parm_u_ofdm_code_rate_LP = FEC_3_4;
1728                 break;
1729         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1730                 parm_u_ofdm_code_rate_LP = FEC_5_6;
1731                 break;
1732         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1733                 parm_u_ofdm_code_rate_LP = FEC_7_8;
1734                 break;
1735         default:
1736         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1737                 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1738                 break;
1739         }
1740         switch (feparm.code_rate_HP)
1741         {
1742         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1743                 parm_u_ofdm_code_rate_HP = FEC_1_2;
1744                 break;
1745         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1746                 parm_u_ofdm_code_rate_HP = FEC_2_3;
1747                 break;
1748         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1749                 parm_u_ofdm_code_rate_HP = FEC_3_4;
1750                 break;
1751         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1752                 parm_u_ofdm_code_rate_HP = FEC_5_6;
1753                 break;
1754         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1755                 parm_u_ofdm_code_rate_HP = FEC_7_8;
1756                 break;
1757         default:
1758         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1759                 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1760                 break;
1761         }
1762         switch (feparm.modulation)
1763         {
1764         case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1765                 parm_u_ofdm_constellation = QPSK;
1766                 break;
1767         case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1768                 parm_u_ofdm_constellation = QAM_16;
1769                 break;
1770         case eDVBFrontendParametersTerrestrial::Modulation::QAM64:
1771                 parm_u_ofdm_constellation = QAM_64;
1772                 break;
1773         default:
1774         case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1775                 parm_u_ofdm_constellation = QAM_AUTO;
1776                 break;
1777         }
1778         switch (feparm.transmission_mode)
1779         {
1780         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1781                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1782                 break;
1783         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1784                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1785                 break;
1786         default:
1787         case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1788                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1789                 break;
1790         }
1791         switch (feparm.guard_interval)
1792         {
1793                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1794                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1795                         break;
1796                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1797                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1798                         break;
1799                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1800                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1801                         break;
1802                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1803                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1804                         break;
1805                 default:
1806                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1807                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1808                         break;
1809         }
1810         switch (feparm.hierarchy)
1811         {
1812                 case eDVBFrontendParametersTerrestrial::Hierarchy::HNone:
1813                         parm_u_ofdm_hierarchy_information = HIERARCHY_NONE;
1814                         break;
1815                 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1816                         parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1817                         break;
1818                 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1819                         parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1820                         break;
1821                 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1822                         parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1823                         break;
1824                 default:
1825                 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1826                         parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1827                         break;
1828         }
1829         switch (feparm.inversion)
1830         {
1831         case eDVBFrontendParametersTerrestrial::Inversion::On:
1832                 parm_inversion = INVERSION_ON;
1833                 break;
1834         case eDVBFrontendParametersTerrestrial::Inversion::Off:
1835                 parm_inversion = INVERSION_OFF;
1836                 break;
1837         default:
1838         case eDVBFrontendParametersTerrestrial::Inversion::Unknown:
1839                 parm_inversion = INVERSION_AUTO;
1840                 break;
1841         }
1842         return 0;
1843 }
1844
1845 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1846 {
1847         eDebug("(%d)tune", m_dvbid);
1848
1849         m_timeout->stop();
1850
1851         int res=0;
1852
1853         if (!m_sn)
1854         {
1855                 eDebug("no frontend device opened... do not try to tune !!!");
1856                 res = -ENODEV;
1857                 goto tune_error;
1858         }
1859
1860         if (m_type == -1)
1861         {
1862                 res = -ENODEV;
1863                 goto tune_error;
1864         }
1865
1866         m_sn->stop();
1867         m_sec_sequence.clear();
1868
1869         switch (m_type)
1870         {
1871         case feSatellite:
1872         {
1873                 eDVBFrontendParametersSatellite feparm;
1874                 if (where.getDVBS(feparm))
1875                 {
1876                         eDebug("no dvbs data!");
1877                         res = -EINVAL;
1878                         goto tune_error;
1879                 }
1880                 m_sec->setRotorMoving(false);
1881                 res=prepare_sat(feparm);
1882                 if (res)
1883                         goto tune_error;
1884
1885                 break;
1886         }
1887         case feCable:
1888         {
1889                 eDVBFrontendParametersCable feparm;
1890                 if (where.getDVBC(feparm))
1891                 {
1892                         res = -EINVAL;
1893                         goto tune_error;
1894                 }
1895                 res=prepare_cable(feparm);
1896                 if (res)
1897                         goto tune_error;
1898
1899                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1900                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1901                 break;
1902         }
1903         case feTerrestrial:
1904         {
1905                 eDVBFrontendParametersTerrestrial feparm;
1906                 if (where.getDVBT(feparm))
1907                 {
1908                         eDebug("no -T data");
1909                         res = -EINVAL;
1910                         goto tune_error;
1911                 }
1912                 res=prepare_terrestrial(feparm);
1913                 if (res)
1914                         goto tune_error;
1915
1916                 std::string enable_5V;
1917                 char configStr[255];
1918                 snprintf(configStr, 255, "config.Nims.%d.terrestrial_5V", m_slotid);
1919                 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1920                 ePythonConfigQuery::getConfigValue(configStr, enable_5V);
1921                 if (enable_5V == "True")
1922                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
1923                 else
1924                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltageOff) );
1925                 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1926
1927                 break;
1928         }
1929         }
1930
1931         m_tuneTimer->start(0,true);
1932         m_sec_sequence.current() = m_sec_sequence.begin();
1933
1934         if (m_state != stateTuning)
1935         {
1936                 m_tuning = 1;
1937                 m_state = stateTuning;
1938                 m_stateChanged(this);
1939         }
1940
1941         return res;
1942
1943 tune_error:
1944         m_tuneTimer->stop();
1945         return res;
1946 }
1947
1948 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1949 {
1950         connection = new eConnection(this, m_stateChanged.connect(stateChange));
1951         return 0;
1952 }
1953
1954 RESULT eDVBFrontend::setVoltage(int voltage)
1955 {
1956         if (m_type == feCable)
1957                 return -1;
1958 #if HAVE_DVB_API_VERSION < 3
1959         secVoltage vlt;
1960 #else
1961         bool increased=false;
1962         fe_sec_voltage_t vlt;
1963 #endif
1964         m_data[CUR_VOLTAGE]=voltage;
1965         switch (voltage)
1966         {
1967         case voltageOff:
1968                 for (int i=0; i < 3; ++i)  // reset diseqc
1969                         m_data[i]=-1;
1970                 vlt = SEC_VOLTAGE_OFF;
1971                 break;
1972         case voltage13_5:
1973 #if HAVE_DVB_API_VERSION < 3
1974                 vlt = SEC_VOLTAGE_13_5;
1975                 break;
1976 #else
1977                 increased = true;
1978 #endif
1979         case voltage13:
1980                 vlt = SEC_VOLTAGE_13;
1981                 break;
1982         case voltage18_5:
1983 #if HAVE_DVB_API_VERSION < 3
1984                 vlt = SEC_VOLTAGE_18_5;
1985                 break;
1986 #else
1987                 increased = true;
1988 #endif
1989         case voltage18:
1990                 vlt = SEC_VOLTAGE_18;
1991                 break;
1992         default:
1993                 return -ENODEV;
1994         }
1995 #if HAVE_DVB_API_VERSION < 3
1996         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1997 #else
1998         if (m_type == feSatellite && ::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1999                 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
2000         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
2001 #endif
2002 }
2003
2004 RESULT eDVBFrontend::getState(int &state)
2005 {
2006         state = m_state;
2007         return 0;
2008 }
2009
2010 RESULT eDVBFrontend::setTone(int t)
2011 {
2012         if (m_type != feSatellite)
2013                 return -1;
2014 #if HAVE_DVB_API_VERSION < 3
2015         secToneMode_t tone;
2016 #else
2017         fe_sec_tone_mode_t tone;
2018 #endif
2019         m_data[CUR_TONE]=t;
2020         switch (t)
2021         {
2022         case toneOn:
2023                 tone = SEC_TONE_ON;
2024                 break;
2025         case toneOff:
2026                 tone = SEC_TONE_OFF;
2027                 break;
2028         default:
2029                 return -ENODEV;
2030         }
2031 #if HAVE_DVB_API_VERSION < 3    
2032         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
2033 #else   
2034         return ::ioctl(m_fd, FE_SET_TONE, tone);
2035 #endif
2036 }
2037
2038 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
2039         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
2040 #endif
2041
2042 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
2043 {
2044 #if HAVE_DVB_API_VERSION < 3
2045         struct secCommand cmd;
2046         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
2047         cmd.u.diseqc.cmdtype = diseqc.data[0];
2048         cmd.u.diseqc.addr = diseqc.data[1];
2049         cmd.u.diseqc.cmd = diseqc.data[2];
2050         cmd.u.diseqc.numParams = diseqc.len-3;
2051         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
2052         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
2053 #else
2054         struct dvb_diseqc_master_cmd cmd;
2055         memcpy(cmd.msg, diseqc.data, diseqc.len);
2056         cmd.msg_len = diseqc.len;
2057         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
2058 #endif
2059                 return -EINVAL;
2060         return 0;
2061 }
2062
2063 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
2064         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
2065 #endif
2066 RESULT eDVBFrontend::sendToneburst(int burst)
2067 {
2068 #if HAVE_DVB_API_VERSION < 3
2069         secMiniCmd cmd = SEC_MINI_NONE;
2070 #else
2071         fe_sec_mini_cmd_t cmd = SEC_MINI_A;
2072 #endif
2073         if ( burst == eDVBSatelliteDiseqcParameters::A )
2074                 cmd = SEC_MINI_A;
2075         else if ( burst == eDVBSatelliteDiseqcParameters::B )
2076                 cmd = SEC_MINI_B;
2077 #if HAVE_DVB_API_VERSION < 3
2078         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
2079                 return -EINVAL;
2080 #else
2081         if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
2082                 return -EINVAL;
2083 #endif
2084         return 0;
2085 }
2086
2087 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
2088 {
2089         m_sec = sec;
2090         return 0;
2091 }
2092
2093 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
2094 {
2095         m_sec_sequence = list;
2096         return 0;
2097 }
2098
2099 RESULT eDVBFrontend::getData(int num, int &data)
2100 {
2101         if ( num < NUM_DATA_ENTRIES )
2102         {
2103                 data = m_data[num];
2104                 return 0;
2105         }
2106         return -EINVAL;
2107 }
2108
2109 RESULT eDVBFrontend::setData(int num, int val)
2110 {
2111         if ( num < NUM_DATA_ENTRIES )
2112         {
2113                 m_data[num] = val;
2114                 return 0;
2115         }
2116         return -EINVAL;
2117 }
2118
2119 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
2120 {
2121         int type;
2122         if (feparm->getSystem(type) || type != m_type || !m_enabled)
2123                 return 0;
2124
2125         if (m_type == eDVBFrontend::feSatellite)
2126         {
2127                 ASSERT(m_sec);
2128                 eDVBFrontendParametersSatellite sat_parm;
2129                 int ret = feparm->getDVBS(sat_parm);
2130                 ASSERT(!ret);
2131                 return m_sec->canTune(sat_parm, this, 1 << m_slotid);
2132         }
2133         else if (m_type == eDVBFrontend::feCable)
2134                 return 2;  // more prio for cable frontends
2135         return 1;
2136 }
2137
2138 bool eDVBFrontend::setSlotInfo(ePyObject obj)
2139 {
2140         ePyObject Id, Descr, Enabled;
2141         if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 3)
2142                 goto arg_error;
2143         Id = PyTuple_GET_ITEM(obj, 0);
2144         Descr = PyTuple_GET_ITEM(obj, 1);
2145         Enabled = PyTuple_GET_ITEM(obj, 2);
2146         if (!PyInt_Check(Id) || !PyString_Check(Descr) || !PyBool_Check(Enabled))
2147                 goto arg_error;
2148         strcpy(m_description, PyString_AS_STRING(Descr));
2149         m_slotid = PyInt_AsLong(Id);
2150         m_enabled = Enabled == Py_True;
2151         // HACK.. the rotor workaround is neede for all NIMs with LNBP21 voltage regulator...
2152         m_need_rotor_workaround = !!strstr(m_description, "Alps BSBE1") || !!strstr(m_description, "Alps -S");
2153         eDebug("setSlotInfo for dvb frontend %d to slotid %d, descr %s, need rotorworkaround %s, enabled %s",
2154                 m_dvbid, m_slotid, m_description, m_need_rotor_workaround ? "Yes" : "No", m_enabled ? "Yes" : "No" );
2155         return true;
2156 arg_error:
2157         PyErr_SetString(PyExc_StandardError,
2158                 "eDVBFrontend::setSlotInfo must get a tuple with first param slotid, second param slot description and third param enabled boolean");
2159         return false;
2160 }