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