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