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