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