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