show terrestrial frontend infos in ServiceInfo dialog
[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                 if (m_sec)
429                         m_sec->setRotorMoving(false);
430                 ::close(m_fd);
431                 m_fd=-1;
432                 m_data[0] = m_data[1] = m_data[2] = -1;
433         }
434 #if HAVE_DVB_API_VERSION < 3
435         if (m_secfd >= 0)
436         {
437                 ::close(m_secfd);
438                 m_secfd=-1;
439         }
440 #endif
441         delete m_sn;
442         m_sn=0;
443
444         return 0;
445 }
446
447 eDVBFrontend::~eDVBFrontend()
448 {
449         closeFrontend();
450         delete m_timeout;
451         delete m_tuneTimer;
452 }
453
454 void eDVBFrontend::feEvent(int w)
455 {
456         while (1)
457         {
458 #if HAVE_DVB_API_VERSION < 3
459                 FrontendEvent event;
460 #else
461                 dvb_frontend_event event;
462 #endif
463                 int res;
464                 int state;
465                 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
466                 
467                 if (res && (errno == EAGAIN))
468                         break;
469
470                 if (res)
471                 {
472                         eWarning("FE_GET_EVENT failed! %m");
473                         return;
474                 }
475                 
476                 if (w < 0)
477                         continue;
478
479 #if HAVE_DVB_API_VERSION < 3
480                 if (event.type == FE_COMPLETION_EV)
481 #else
482                 eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
483                 if (event.status & FE_HAS_LOCK)
484 #endif
485                 {
486                         state = stateLock;
487                 } else
488                 {
489                         if (m_tuning)
490                                 state = stateTuning;
491                         else
492                         {
493                                 state = stateLostLock;
494                                 m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
495                         }
496                 }
497                 if (m_state != state)
498                 {
499                         m_state = state;
500                         m_stateChanged(this);
501                 }
502         }
503 }
504
505 void eDVBFrontend::timeout()
506 {
507         m_tuning = 0;
508         if (m_state == stateTuning)
509         {
510                 m_state = stateFailed;
511                 m_stateChanged(this);
512         }
513 }
514
515 int eDVBFrontend::readFrontendData(int type)
516 {
517         switch(type)
518         {
519                 case bitErrorRate:
520                 {
521                         uint32_t ber=0;
522                         if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
523                                 eDebug("FE_READ_BER failed (%m)");
524                         return ber;
525                 }
526                 case signalPower:
527                 {
528                         uint16_t snr=0;
529                         if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
530                                 eDebug("FE_READ_SNR failed (%m)");
531                         return snr;
532                 }
533                 case signalQuality:
534                 {
535                         uint16_t strength=0;
536                         if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
537                                 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
538                         return strength;
539                 }
540                 case Locked:
541                 {
542 #if HAVE_DVB_API_VERSION < 3
543                         FrontendStatus status=0;
544 #else
545                         fe_status_t status;
546 #endif
547                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
548                                 eDebug("FE_READ_STATUS failed (%m)");
549                         return !!(status&FE_HAS_LOCK);
550                 }
551                 case Synced:
552                 {
553 #if HAVE_DVB_API_VERSION < 3
554                         FrontendStatus status=0;
555 #else
556                         fe_status_t status;
557 #endif
558                         if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
559                                 eDebug("FE_READ_STATUS failed (%m)");
560                         return !!(status&FE_HAS_SYNC);
561                 }
562         }
563         return 0;
564 }
565
566 void PutToDict(PyObject *dict, const char*key, long value)
567 {
568         PyObject *item = PyInt_FromLong(value);
569         if (item)
570         {
571                 if (PyDict_SetItemString(dict, key, item))
572                         eDebug("put %s to dict failed", key);
573                 Py_DECREF(item);
574         }
575         else
576                 eDebug("could not create PyObject for %s", key);
577 }
578
579 void PutToDict(PyObject *dict, const char*key, const char *value)
580 {
581         PyObject *item = PyString_FromString(value);
582         if (item)
583         {
584                 if (PyDict_SetItemString(dict, key, item))
585                         eDebug("put %s to dict failed", key);
586                 Py_DECREF(item);
587         }
588         else
589                 eDebug("could not create PyObject for %s", key);
590 }
591
592 void fillDictWithSatelliteData(PyObject *dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
593 {
594         int freq_offset=0;
595         int csw=0;
596         const char *fec=0;
597         fe->getData(0, csw);
598         fe->getData(9, freq_offset);
599         int frequency = parm_frequency + freq_offset;
600         PutToDict(dict, "frequency", frequency);
601         PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
602
603         switch(parm_u_qpsk_fec_inner)
604         {
605         case FEC_1_2:
606                 fec = "FEC_1_2";
607                 break;
608         case FEC_2_3:
609                 fec = "FEC_2_3";
610                 break;
611         case FEC_3_4:
612                 fec = "FEC_3_4";
613                 break;
614         case FEC_5_6:
615                 fec = "FEC_5_6";
616                 break;
617         case FEC_7_8:
618                 fec = "FEC_7_8";
619                 break;
620         default:
621         case FEC_AUTO:
622                 fec = "FEC_AUTO";
623                 break;
624         }
625         PutToDict(dict, "fec_inner", fec);
626 }
627
628 void fillDictWithCableData(PyObject *dict, const FRONTENDPARAMETERS &parm)
629 {
630         const char *tmp=0;
631         PutToDict(dict, "frequency", parm_frequency/1000);
632         PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
633         switch(parm_u_qam_fec_inner)
634         {
635         case FEC_NONE:
636                 tmp = "FEC_NONE";
637                 break;
638         case FEC_1_2:
639                 tmp = "FEC_1_2";
640                 break;
641         case FEC_2_3:
642                 tmp = "FEC_2_3";
643                 break;
644         case FEC_3_4:
645                 tmp = "FEC_3_4";
646                 break;
647         case FEC_5_6:
648                 tmp = "FEC_5_6";
649                 break;
650         case FEC_7_8:
651                 tmp = "FEC_7_8";
652                 break;
653 #if HAVE_DVB_API_VERSION >= 3
654         case FEC_8_9:
655                 tmp = "FEC_8_9";
656                 break;
657 #endif
658         default:
659         case FEC_AUTO:
660                 tmp = "FEC_AUTO";
661                 break;
662         }
663         PutToDict(dict, "fec_inner", tmp);
664         switch(parm_u_qam_modulation)
665         {
666         case QAM_16:
667                 tmp = "QAM_16";
668                 break;
669         case QAM_32:
670                 tmp = "QAM_32";
671                 break;
672         case QAM_64:
673                 tmp = "QAM_64";
674                 break;
675         case QAM_128:
676                 tmp = "QAM_128";
677                 break;
678         case QAM_256:
679                 tmp = "QAM_256";
680                 break;
681         default:
682         case QAM_AUTO:
683                 tmp = "QAM_AUTO";
684                 break;
685         }
686         PutToDict(dict, "modulation", tmp);
687 }
688
689 void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
690 {
691         const char *tmp=0;
692         PutToDict(dict, "frequency", parm_frequency);
693         switch (parm_u_ofdm_bandwidth)
694         {
695         case BANDWIDTH_8_MHZ:
696                 tmp = "BANDWIDTH_8_MHZ";
697                 break;
698         case BANDWIDTH_7_MHZ:
699                 tmp = "BANDWIDTH_7_MHZ";
700                 break;
701         case BANDWIDTH_6_MHZ:
702                 tmp = "BANDWIDTH_6_MHZ";
703                 break;
704         default:
705         case BANDWIDTH_AUTO:
706                 tmp = "BANDWIDTH_AUTO";
707                 break;
708         }
709         PutToDict(dict, "bandwidth", tmp);
710         switch (parm_u_ofdm_code_rate_LP)
711         {
712         case FEC_1_2:
713                 tmp = "FEC_1_2";
714                 break;
715         case FEC_2_3:
716                 tmp = "FEC_2_3";
717                 break;
718         case FEC_3_4:
719                 tmp = "FEC_3_4";
720                 break;
721         case FEC_5_6:
722                 tmp = "FEC_5_6";
723                 break;
724         case FEC_7_8:
725                 tmp = "FEC_7_8";
726                 break;
727         default:
728         case FEC_AUTO:
729                 tmp = "FEC_AUTO";
730                 break;
731         }
732         PutToDict(dict, "code_rate_lp", tmp);
733         switch (parm_u_ofdm_code_rate_HP)
734         {
735         case FEC_1_2:
736                 tmp = "FEC_1_2";
737                 break;
738         case FEC_2_3:
739                 tmp = "FEC_2_3";
740                 break;
741         case FEC_3_4:
742                 tmp = "FEC_3_4";
743                 break;
744         case FEC_5_6:
745                 tmp = "FEC_5_6";
746                 break;
747         case FEC_7_8:
748                 tmp = "FEC_7_8";
749                 break;
750         default:
751         case FEC_AUTO:
752                 tmp = "FEC_AUTO";
753                 break;
754         }
755         PutToDict(dict, "code_rate_hp", tmp);
756         switch (parm_u_ofdm_constellation)
757         {
758         case QPSK:
759                 tmp = "QPSK";
760                 break;
761         case QAM_16:
762                 tmp = "QAM_16";
763                 break;
764         default:
765         case QAM_AUTO:
766                 tmp = "QAM_AUTO";
767                 break;
768         }
769         PutToDict(dict, "constellation", tmp);
770         switch (parm_u_ofdm_transmission_mode)
771         {
772         case TRANSMISSION_MODE_2K:
773                 tmp = "TRANSMISSION_MODE_2K";
774                 break;
775         case TRANSMISSION_MODE_8K:
776                 tmp = "TRANSMISSION_MODE_8K";
777                 break;
778         default:
779         case TRANSMISSION_MODE_AUTO:
780                 tmp = "TRANSMISSION_MODE_AUTO";
781                 break;
782         }
783         PutToDict(dict, "transmission_mode", tmp);
784         switch (parm_u_ofdm_guard_interval)
785         {
786                 case GUARD_INTERVAL_1_32:
787                         tmp = "GUARD_INTERVAL_1_32";
788                         break;
789                 case GUARD_INTERVAL_1_16:
790                         tmp = "GUARD_INTERVAL_1_16";
791                         break;
792                 case GUARD_INTERVAL_1_8:
793                         tmp = "GUARD_INTERVAL_1_8";
794                         break;
795                 case GUARD_INTERVAL_1_4:
796                         tmp = "GUARD_INTERVAL_1_4";
797                         break;
798                 default:
799                 case GUARD_INTERVAL_AUTO:
800                         tmp = "GUARD_INTERVAL_AUTO";
801                         break;
802         }
803         PutToDict(dict, "guard_interval", tmp);
804         switch (parm_u_ofdm_hierarchy_information)
805         {
806                 case HIERARCHY_NONE:
807                         tmp = "HIERARCHY_NONE";
808                         break;
809                 case HIERARCHY_1:
810                         tmp = "HIERARCHY_1";
811                         break;
812                 case HIERARCHY_2:
813                         tmp = "HIERARCHY_2";
814                         break;
815                 case HIERARCHY_4:
816                         tmp = "HIERARCHY_4";
817                         break;
818                 default:
819                 case HIERARCHY_AUTO:
820                         tmp = "HIERARCHY_AUTO";
821                         break;
822         }
823         PutToDict(dict, "hierarchy_information", tmp);
824 }
825
826 PyObject *eDVBFrontend::readTransponderData(bool original)
827 {
828         PyObject *ret=PyDict_New();
829
830         if (ret)
831         {
832                 bool read=m_fd != -1;
833                 const char *tmp=0;
834
835                 PutToDict(ret, "tuner_number", m_fe);
836
837                 switch(m_type)
838                 {
839                         case feSatellite:
840                                 tmp = "DVB-S";
841                                 break;
842                         case feCable:
843                                 tmp = "DVB-C";
844                                 break;
845                         case feTerrestrial:
846                                 tmp = "DVB-T";
847                                 break;
848                         default:
849                                 tmp = "UNKNOWN";
850                                 read=false;
851                                 break;
852                 }
853                 PutToDict(ret, "tuner_type", tmp);
854
855                 if (read)
856                 {
857                         FRONTENDPARAMETERS front;
858
859                         tmp = "UNKNOWN";
860                         switch(m_state)
861                         {
862                                 case stateIdle:
863                                         tmp="IDLE";
864                                         break;
865                                 case stateTuning:
866                                         tmp="TUNING";
867                                         break;
868                                 case stateFailed:
869                                         tmp="FAILED";
870                                         break;
871                                 case stateLock:
872                                         tmp="LOCKED";
873                                         break;
874                                 case stateLostLock:
875                                         tmp="LOSTLOCK";
876                                         break;
877                                 default:
878                                         break;
879                         }
880                         PutToDict(ret, "tuner_state", tmp);
881
882                         PutToDict(ret, "tuner_locked", readFrontendData(Locked));
883                         PutToDict(ret, "tuner_synced", readFrontendData(Synced));
884                         PutToDict(ret, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
885                         PutToDict(ret, "tuner_signal_power", readFrontendData(signalPower));
886                         PutToDict(ret, "tuner_signal_quality", readFrontendData(signalQuality));
887
888                         if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
889                                 eDebug("FE_GET_FRONTEND (%m)");
890                         else
891                         {
892                                 tmp = "INVERSION_AUTO";
893                                 switch(parm_inversion)
894                                 {
895                                         case INVERSION_ON:
896                                                 tmp = "INVERSION_ON";
897                                                 break;
898                                         case INVERSION_OFF:
899                                                 tmp = "INVERSION_OFF";
900                                                 break;
901                                         default:
902                                                 break;
903                                 }
904                                 if (tmp)
905                                         PutToDict(ret, "inversion", tmp);
906
907                                 switch(m_type)
908                                 {
909                                         case feSatellite:
910                                                 fillDictWithSatelliteData(ret, original?parm:front, this);
911                                                 break;
912                                         case feCable:
913                                                 fillDictWithCableData(ret, original?parm:front);
914                                                 break;
915                                         case feTerrestrial:
916                                                 fillDictWithTerrestrialData(ret, original?parm:front);
917                                                 break;
918                                 }
919                         }
920                 }
921         }
922         else
923         {
924                 Py_INCREF(Py_None);
925                 ret = Py_None;
926         }
927         return ret;
928 }
929
930 #ifndef FP_IOCTL_GET_ID
931 #define FP_IOCTL_GET_ID 0
932 #endif
933 int eDVBFrontend::readInputpower()
934 {
935         int power=m_fe;  // this is needed for read inputpower from the correct tuner !
936
937         // open front prozessor
938         int fp=::open("/dev/dbox/fp0", O_RDWR);
939         if (fp < 0)
940         {
941                 eDebug("couldn't open fp");
942                 return -1;
943         }
944         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
945         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
946         {
947                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
948                 return -1;
949         }
950         ::close(fp);
951
952         return power;
953 }
954
955 bool eDVBFrontend::setSecSequencePos(int steps)
956 {
957         eDebug("set sequence pos %d", steps);
958         if (!steps)
959                 return false;
960         while( steps > 0 )
961         {
962                 if (m_sec_sequence.current() != m_sec_sequence.end())
963                         ++m_sec_sequence.current();
964                 --steps;
965         }
966         while( steps < 0 )
967         {
968                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
969                         --m_sec_sequence.current();
970                 ++steps;
971         }
972         return true;
973 }
974
975 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
976 {
977         int delay=0;
978         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
979         {
980 //              eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
981                 switch (m_sec_sequence.current()->cmd)
982                 {
983                         case eSecCommand::SLEEP:
984                                 delay = m_sec_sequence.current()++->msec;
985                                 eDebug("[SEC] sleep %dms", delay);
986                                 break;
987                         case eSecCommand::GOTO:
988                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
989                                         ++m_sec_sequence.current();
990                                 break;
991                         case eSecCommand::SET_VOLTAGE:
992                         {
993                                 int voltage = m_sec_sequence.current()++->voltage;
994                                 eDebug("[SEC] setVoltage %d", voltage);
995                                 setVoltage(voltage);
996                                 break;
997                         }
998                         case eSecCommand::IF_VOLTAGE_GOTO:
999                         {
1000                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1001                                 if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
1002                                         break;
1003                                 ++m_sec_sequence.current();
1004                                 break;
1005                         }
1006                         case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1007                         {
1008                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1009                                 if ( compare.voltage != m_curVoltage && setSecSequencePos(compare.steps) )
1010                                         break;
1011                                 ++m_sec_sequence.current();
1012                                 break;
1013                         }
1014                         case eSecCommand::SET_TONE:
1015                                 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1016                                 setTone(m_sec_sequence.current()++->tone);
1017                                 break;
1018                         case eSecCommand::SEND_DISEQC:
1019                                 sendDiseqc(m_sec_sequence.current()->diseqc);
1020                                 eDebugNoNewLine("[SEC] sendDiseqc: ");
1021                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1022                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1023                                 eDebug("");
1024                                 ++m_sec_sequence.current();
1025                                 break;
1026                         case eSecCommand::SEND_TONEBURST:
1027                                 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1028                                 sendToneburst(m_sec_sequence.current()++->toneburst);
1029                                 break;
1030                         case eSecCommand::SET_FRONTEND:
1031                                 eDebug("[SEC] setFrontend");
1032                                 setFrontend();
1033                                 ++m_sec_sequence.current();
1034                                 break;
1035                         case eSecCommand::START_TUNE_TIMEOUT:
1036                                 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1037                                 ++m_sec_sequence.current();
1038                                 break;
1039                         case eSecCommand::SET_TIMEOUT:
1040                                 m_timeoutCount = m_sec_sequence.current()++->val;
1041                                 eDebug("[SEC] set timeout %d", m_timeoutCount);
1042                                 break;
1043                         case eSecCommand::IF_TIMEOUT_GOTO:
1044                                 if (!m_timeoutCount)
1045                                 {
1046                                         eDebug("[SEC] rotor timout");
1047                                         m_sec->setRotorMoving(false);
1048                                         setSecSequencePos(m_sec_sequence.current()->steps);
1049                                 }
1050                                 else
1051                                         ++m_sec_sequence.current();
1052                                 break;
1053                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1054                         {
1055                                 int idx = m_sec_sequence.current()++->val;
1056                                 if ( idx == 0 || idx == 1 )
1057                                 {
1058                                         m_idleInputpower[idx] = readInputpower();
1059                                         eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1060                                 }
1061                                 else
1062                                         eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1063                                 break;
1064                         }
1065                         case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1066                         {
1067                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1068                                 int idx = compare.voltage;
1069                                 if ( idx == 0 || idx == 1 )
1070                                 {
1071                                         int idle = readInputpower();
1072                                         int diff = abs(idle-m_idleInputpower[idx]);
1073                                         if ( diff > 0)
1074                                         {
1075                                                 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1076                                                 setSecSequencePos(compare.steps);
1077                                                 break;
1078                                         }
1079                                 }
1080                                 ++m_sec_sequence.current();
1081                                 break;
1082                         }
1083                         case eSecCommand::IF_TUNER_LOCKED_GOTO:
1084                         {
1085                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1086                                 if (readFrontendData(Locked))
1087                                 {
1088                                         eDebug("[SEC] locked step %d ok", cmd.okcount);
1089                                         ++cmd.okcount;
1090                                         if (cmd.okcount > 12)
1091                                         {
1092                                                 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1093                                                 setSecSequencePos(cmd.steps);
1094                                                 break;
1095                                         }
1096                                 }
1097                                 else
1098                                 {
1099                                         eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1100                                         --m_timeoutCount;
1101                                         if (!m_timeoutCount && m_retryCount > 0)
1102                                                 --m_retryCount;
1103                                         cmd.okcount=0;
1104                                 }
1105                                 ++m_sec_sequence.current();
1106                                 break;
1107                         }
1108                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1109                                 m_runningInputpower = readInputpower();
1110                                 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1111                                 ++m_sec_sequence.current();
1112                                 break;
1113                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1114                         {
1115                                 int idleInputpower = m_idleInputpower[ (m_curVoltage&1) ? 0 : 1];
1116                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1117                                 const char *txt = cmd.direction ? "running" : "stopped";
1118                                 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1119                                         txt,
1120                                         m_runningInputpower,
1121                                         idleInputpower,
1122                                         cmd.deltaA);
1123                                 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1124                                         || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1125                                 {
1126                                         ++cmd.okcount;
1127                                         eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1128                                         if ( cmd.okcount > 6 )
1129                                         {
1130                                                 m_sec->setRotorMoving(cmd.direction);
1131                                                 eDebug("[SEC] rotor is %s", txt);
1132                                                 if (setSecSequencePos(cmd.steps))
1133                                                         break;
1134                                         }
1135                                 }
1136                                 else
1137                                 {
1138                                         eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1139                                         --m_timeoutCount;
1140                                         if (!m_timeoutCount && m_retryCount > 0)
1141                                                 --m_retryCount;
1142                                         cmd.okcount=0;
1143                                 }
1144                                 ++m_sec_sequence.current();
1145                                 break;
1146                         }
1147                         case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1148                                 if (m_data[5] != -1 && m_data[6] != -1)
1149                                         setSecSequencePos(m_sec_sequence.current()->steps);
1150                                 else
1151                                         ++m_sec_sequence.current();
1152                                 break;
1153                         case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1154                                 m_data[5] = m_data[6] = -1;
1155                                 eDebug("[SEC] invalidate current rotorparams");
1156                                 ++m_sec_sequence.current();
1157                                 break;
1158                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1159                                 m_data[5] = m_data[3];
1160                                 m_data[6] = m_data[4];
1161                                 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
1162                                 ++m_sec_sequence.current();
1163                                 break;
1164                         case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1165                                 m_retryCount = m_sec_sequence.current()++->val;
1166                                 eDebug("[SEC] set rotor retries %d", m_retryCount);
1167                                 break;
1168                         case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1169                                 if (!m_retryCount)
1170                                 {
1171                                         eDebug("[SEC] no more rotor retrys");
1172                                         setSecSequencePos(m_sec_sequence.current()->steps);
1173                                 }
1174                                 else
1175                                         ++m_sec_sequence.current();
1176                                 break;
1177                         case eSecCommand::SET_POWER_LIMITING_MODE:
1178                         {
1179                                 int fd = m_fe ?
1180                                         ::open("/dev/i2c/1", O_RDWR) :
1181                                         ::open("/dev/i2c/0", O_RDWR);
1182
1183                                 unsigned char data[2];
1184                                 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1185                                 if(::read(fd, data, 1) != 1)
1186                                         eDebug("[SEC] error read lnbp (%m)");
1187                                 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1188                                 {
1189                                         data[0] |= 0x80;  // enable static current limiting
1190                                         eDebug("[SEC] set static current limiting");
1191                                 }
1192                                 else
1193                                 {
1194                                         data[0] &= ~0x80;  // enable dynamic current limiting
1195                                         eDebug("[SEC] set dynamic current limiting");
1196                                 }
1197                                 if(::write(fd, data, 1) != 1)
1198                                         eDebug("[SEC] error write lnbp (%m)");
1199                                 ::close(fd);
1200                                 ++m_sec_sequence.current();
1201                                 break;
1202                         }
1203                         default:
1204                                 ++m_sec_sequence.current();
1205                                 eDebug("[SEC] unhandled sec command");
1206                 }
1207                 m_tuneTimer->start(delay,true);
1208         }
1209 }
1210
1211 void eDVBFrontend::setFrontend()
1212 {
1213         eDebug("setting frontend %d", m_fe);
1214         m_sn->start();
1215         feEvent(-1);
1216         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1217         {
1218                 perror("FE_SET_FRONTEND failed");
1219                 return;
1220         }
1221 }
1222
1223 RESULT eDVBFrontend::getFrontendType(int &t)
1224 {
1225         if (m_type == -1)
1226                 return -ENODEV;
1227         t = m_type;
1228         return 0;
1229 }
1230
1231 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1232 {
1233         int res;
1234         if (!m_sec)
1235         {
1236                 eWarning("no SEC module active!");
1237                 return -ENOENT;
1238         }
1239         res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
1240         if (!res)
1241         {
1242                 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1243                 switch (feparm.inversion)
1244                 {
1245                         case eDVBFrontendParametersSatellite::Inversion::On:
1246                                 parm_inversion = INVERSION_ON;
1247                                 break;
1248                         case eDVBFrontendParametersSatellite::Inversion::Off:
1249                                 parm_inversion = INVERSION_OFF;
1250                                 break;
1251                         default:
1252                         case eDVBFrontendParametersSatellite::Inversion::Unknown:
1253                                 parm_inversion = INVERSION_AUTO;
1254                                 break;
1255                 }
1256                 switch (feparm.fec)
1257                 {
1258                         default:
1259                         case eDVBFrontendParametersSatellite::FEC::fNone:
1260                                 eDebug("no fec set.. assume auto");
1261                         case eDVBFrontendParametersSatellite::FEC::fAuto:
1262                                 parm_u_qpsk_fec_inner = FEC_AUTO;
1263                                 break;
1264                         case eDVBFrontendParametersSatellite::FEC::f1_2:
1265                                 parm_u_qpsk_fec_inner = FEC_1_2;
1266                                 break;
1267                         case eDVBFrontendParametersSatellite::FEC::f2_3:
1268                                 parm_u_qpsk_fec_inner = FEC_2_3;
1269                                 break;
1270                         case eDVBFrontendParametersSatellite::FEC::f3_4:
1271                                 parm_u_qpsk_fec_inner = FEC_3_4;
1272                                 break;
1273                         case eDVBFrontendParametersSatellite::FEC::f5_6:
1274                                 parm_u_qpsk_fec_inner = FEC_5_6;
1275                                 break;
1276                         case eDVBFrontendParametersSatellite::FEC::f7_8:
1277                                 parm_u_qpsk_fec_inner = FEC_7_8;
1278                                 break;
1279                 }
1280                 // FIXME !!! get frequency range from tuner
1281                 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1282                 {
1283                         eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1284                         return -EINVAL;
1285                 }
1286                 eDebug("tuning to %d mhz", parm_frequency/1000);
1287         }
1288         return res;
1289 }
1290
1291 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1292 {
1293         parm_frequency = feparm.frequency * 1000;
1294         parm_u_qam_symbol_rate = feparm.symbol_rate;
1295         switch (feparm.modulation)
1296         {
1297         case eDVBFrontendParametersCable::Modulation::QAM16:
1298                 parm_u_qam_modulation = QAM_16;
1299                 break;
1300         case eDVBFrontendParametersCable::Modulation::QAM32:
1301                 parm_u_qam_modulation = QAM_32;
1302                 break;
1303         case eDVBFrontendParametersCable::Modulation::QAM64:
1304                 parm_u_qam_modulation = QAM_64;
1305                 break;
1306         case eDVBFrontendParametersCable::Modulation::QAM128:
1307                 parm_u_qam_modulation = QAM_128;
1308                 break;
1309         case eDVBFrontendParametersCable::Modulation::QAM256:
1310                 parm_u_qam_modulation = QAM_256;
1311                 break;
1312         default:
1313         case eDVBFrontendParametersCable::Modulation::Auto:
1314                 parm_u_qam_modulation = QAM_AUTO;
1315                 break;
1316         }
1317         switch (feparm.inversion)
1318         {
1319         case eDVBFrontendParametersCable::Inversion::On:
1320                 parm_inversion = INVERSION_ON;
1321                 break;
1322         case eDVBFrontendParametersCable::Inversion::Off:
1323                 parm_inversion = INVERSION_OFF;
1324                 break;
1325         default:
1326         case eDVBFrontendParametersCable::Inversion::Unknown:
1327                 parm_inversion = INVERSION_AUTO;
1328                 break;
1329         }
1330         switch (feparm.fec_inner)
1331         {
1332         case eDVBFrontendParametersCable::FEC::fNone:
1333                 parm_u_qam_fec_inner = FEC_NONE;
1334                 break;
1335         case eDVBFrontendParametersCable::FEC::f1_2:
1336                 parm_u_qam_fec_inner = FEC_1_2;
1337                 break;
1338         case eDVBFrontendParametersCable::FEC::f2_3:
1339                 parm_u_qam_fec_inner = FEC_2_3;
1340                 break;
1341         case eDVBFrontendParametersCable::FEC::f3_4:
1342                 parm_u_qam_fec_inner = FEC_3_4;
1343                 break;
1344         case eDVBFrontendParametersCable::FEC::f5_6:
1345                 parm_u_qam_fec_inner = FEC_5_6;
1346                 break;
1347         case eDVBFrontendParametersCable::FEC::f7_8:
1348                 parm_u_qam_fec_inner = FEC_7_8;
1349                 break;
1350 #if HAVE_DVB_API_VERSION >= 3
1351         case eDVBFrontendParametersCable::FEC::f8_9:
1352                 parm_u_qam_fec_inner = FEC_8_9;
1353                 break;
1354 #endif
1355         default:
1356         case eDVBFrontendParametersCable::FEC::fAuto:
1357                 parm_u_qam_fec_inner = FEC_AUTO;
1358                 break;
1359         }
1360         return 0;
1361 }
1362
1363 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1364 {
1365         parm_frequency = feparm.frequency;
1366
1367         switch (feparm.bandwidth)
1368         {
1369         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1370                 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1371                 break;
1372         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1373                 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1374                 break;
1375         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1376                 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1377                 break;
1378         default:
1379         case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1380                 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1381                 break;
1382         }
1383         switch (feparm.code_rate_LP)
1384         {
1385         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1386                 parm_u_ofdm_code_rate_LP = FEC_1_2;
1387                 break;
1388         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1389                 parm_u_ofdm_code_rate_LP = FEC_2_3;
1390                 break;
1391         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1392                 parm_u_ofdm_code_rate_LP = FEC_3_4;
1393                 break;
1394         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1395                 parm_u_ofdm_code_rate_LP = FEC_5_6;
1396                 break;
1397         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1398                 parm_u_ofdm_code_rate_LP = FEC_7_8;
1399                 break;
1400         default:
1401         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1402                 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1403                 break;
1404         }
1405         switch (feparm.code_rate_HP)
1406         {
1407         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1408                 parm_u_ofdm_code_rate_HP = FEC_1_2;
1409                 break;
1410         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1411                 parm_u_ofdm_code_rate_HP = FEC_2_3;
1412                 break;
1413         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1414                 parm_u_ofdm_code_rate_HP = FEC_3_4;
1415                 break;
1416         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1417                 parm_u_ofdm_code_rate_HP = FEC_5_6;
1418                 break;
1419         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1420                 parm_u_ofdm_code_rate_HP = FEC_7_8;
1421                 break;
1422         default:
1423         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1424                 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1425                 break;
1426         }
1427         switch (feparm.modulation)
1428         {
1429         case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1430                 parm_u_ofdm_constellation = QPSK;
1431                 break;
1432         case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1433                 parm_u_ofdm_constellation = QAM_16;
1434                 break;
1435         default:
1436         case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1437                 parm_u_ofdm_constellation = QAM_AUTO;
1438                 break;
1439         }
1440         switch (feparm.transmission_mode)
1441         {
1442         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1443                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1444                 break;
1445         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1446                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1447                 break;
1448         default:
1449         case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1450                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1451                 break;
1452         }
1453         switch (feparm.guard_interval)
1454         {
1455                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1456                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1457                         break;
1458                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1459                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1460                         break;
1461                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1462                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1463                         break;
1464                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1465                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1466                         break;
1467                 default:
1468                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1469                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1470                         break;
1471         }
1472         switch (feparm.hierarchy)
1473         {
1474                 case eDVBFrontendParametersTerrestrial::Hierarchy::HNone:
1475                         parm_u_ofdm_hierarchy_information = HIERARCHY_NONE;
1476                         break;
1477                 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1478                         parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1479                         break;
1480                 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1481                         parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1482                         break;
1483                 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1484                         parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1485                         break;
1486                 default:
1487                 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1488                         parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1489                         break;
1490         }
1491         switch (feparm.inversion)
1492         {
1493         case eDVBFrontendParametersTerrestrial::Inversion::On:
1494                 parm_inversion = INVERSION_ON;
1495                 break;
1496         case eDVBFrontendParametersTerrestrial::Inversion::Off:
1497                 parm_inversion = INVERSION_OFF;
1498                 break;
1499         default:
1500         case eDVBFrontendParametersTerrestrial::Inversion::Unknown:
1501                 parm_inversion = INVERSION_AUTO;
1502                 break;
1503         }
1504         return 0;
1505 }
1506
1507 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1508 {
1509         eDebug("(%d)tune", m_fe);
1510
1511         m_timeout->stop();
1512
1513         int res=0;
1514
1515         if (m_type == -1)
1516                 return -ENODEV;
1517
1518         m_sn->stop();
1519         m_sec_sequence.clear();
1520
1521         switch (m_type)
1522         {
1523         case feSatellite:
1524         {
1525                 eDVBFrontendParametersSatellite feparm;
1526                 if (where.getDVBS(feparm))
1527                 {
1528                         eDebug("no dvbs data!");
1529                         return -EINVAL;
1530                 }
1531                 res=prepare_sat(feparm);
1532                 m_sec->setRotorMoving(false);
1533                 break;
1534         }
1535         case feCable:
1536         {
1537                 eDVBFrontendParametersCable feparm;
1538                 if (where.getDVBC(feparm))
1539                         return -EINVAL;
1540                 res=prepare_cable(feparm);
1541                 if (!res)
1542                 {
1543                         m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1544                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1545                 }
1546                 break;
1547         }
1548         case feTerrestrial:
1549         {
1550                 eDVBFrontendParametersTerrestrial feparm;
1551                 if (where.getDVBT(feparm))
1552                 {
1553                         eDebug("no -T data");
1554                         return -EINVAL;
1555                 }
1556                 res=prepare_terrestrial(feparm);
1557                 if (!res)
1558                 {
1559                         m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1560                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1561                 }
1562                 break;
1563         }
1564         }
1565
1566         if (!res)  // prepare ok
1567         {
1568                 m_tuneTimer->start(0,true);
1569                 m_sec_sequence.current() = m_sec_sequence.begin();
1570
1571                 if (m_state != stateTuning)
1572                 {
1573                         m_tuning = 1;
1574                         m_state = stateTuning;
1575                         m_stateChanged(this);
1576                 }
1577         }
1578
1579         return res;
1580 }
1581
1582 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1583 {
1584         connection = new eConnection(this, m_stateChanged.connect(stateChange));
1585         return 0;
1586 }
1587
1588 RESULT eDVBFrontend::setVoltage(int voltage)
1589 {
1590         if (m_type != feSatellite)
1591                 return -1;
1592 #if HAVE_DVB_API_VERSION < 3
1593         secVoltage vlt;
1594 #else
1595         bool increased=false;
1596         fe_sec_voltage_t vlt;
1597 #endif
1598         m_curVoltage=voltage;
1599         switch (voltage)
1600         {
1601         case voltageOff:
1602                 for (int i=0; i < 3; ++i)  // reset diseqc
1603                         m_data[i]=-1;
1604                 vlt = SEC_VOLTAGE_OFF;
1605                 break;
1606         case voltage13_5:
1607 #if HAVE_DVB_API_VERSION < 3
1608                 vlt = SEC_VOLTAGE_13_5;
1609                 break;
1610 #else
1611                 increased = true;
1612 #endif
1613         case voltage13:
1614                 vlt = SEC_VOLTAGE_13;
1615                 break;
1616         case voltage18_5:
1617 #if HAVE_DVB_API_VERSION < 3
1618                 vlt = SEC_VOLTAGE_18_5;
1619                 break;
1620 #else
1621                 increased = true;
1622 #endif
1623         case voltage18:
1624                 vlt = SEC_VOLTAGE_18;
1625                 break;
1626         default:
1627                 return -ENODEV;
1628         }
1629 #if HAVE_DVB_API_VERSION < 3
1630         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1631 #else
1632         if (::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1633                 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
1634         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
1635 #endif
1636 }
1637
1638 RESULT eDVBFrontend::getState(int &state)
1639 {
1640         state = m_state;
1641         return 0;
1642 }
1643
1644 RESULT eDVBFrontend::setTone(int t)
1645 {
1646         if (m_type != feSatellite)
1647                 return -1;
1648 #if HAVE_DVB_API_VERSION < 3
1649         secToneMode_t tone;
1650 #else
1651         fe_sec_tone_mode_t tone;
1652 #endif
1653
1654         switch (t)
1655         {
1656         case toneOn:
1657                 tone = SEC_TONE_ON;
1658                 break;
1659         case toneOff:
1660                 tone = SEC_TONE_OFF;
1661                 break;
1662         default:
1663                 return -ENODEV;
1664         }
1665 #if HAVE_DVB_API_VERSION < 3    
1666         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
1667 #else   
1668         return ::ioctl(m_fd, FE_SET_TONE, tone);
1669 #endif
1670 }
1671
1672 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
1673         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
1674 #endif
1675
1676 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
1677 {
1678 #if HAVE_DVB_API_VERSION < 3
1679         struct secCommand cmd;
1680         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
1681         cmd.u.diseqc.cmdtype = diseqc.data[0];
1682         cmd.u.diseqc.addr = diseqc.data[1];
1683         cmd.u.diseqc.cmd = diseqc.data[2];
1684         cmd.u.diseqc.numParams = diseqc.len-3;
1685         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
1686         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
1687 #else
1688         struct dvb_diseqc_master_cmd cmd;
1689         memcpy(cmd.msg, diseqc.data, diseqc.len);
1690         cmd.msg_len = diseqc.len;
1691         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
1692 #endif
1693                 return -EINVAL;
1694         return 0;
1695 }
1696
1697 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
1698         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
1699 #endif
1700 RESULT eDVBFrontend::sendToneburst(int burst)
1701 {
1702 #if HAVE_DVB_API_VERSION < 3
1703         secMiniCmd cmd = SEC_MINI_NONE;
1704 #else
1705         fe_sec_mini_cmd_t cmd = SEC_MINI_A;
1706 #endif
1707         if ( burst == eDVBSatelliteDiseqcParameters::A )
1708                 cmd = SEC_MINI_A;
1709         else if ( burst == eDVBSatelliteDiseqcParameters::B )
1710                 cmd = SEC_MINI_B;
1711 #if HAVE_DVB_API_VERSION < 3
1712         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
1713                 return -EINVAL;
1714 #else
1715         if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
1716                 return -EINVAL;
1717 #endif
1718         return 0;
1719 }
1720
1721 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
1722 {
1723         m_sec = sec;
1724         return 0;
1725 }
1726
1727 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
1728 {
1729         m_sec_sequence = list;
1730         return 0;
1731 }
1732
1733 RESULT eDVBFrontend::getData(int num, int &data)
1734 {
1735         if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1736         {
1737                 data = m_data[num];
1738                 return 0;
1739         }
1740         return -EINVAL;
1741 }
1742
1743 RESULT eDVBFrontend::setData(int num, int val)
1744 {
1745         if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1746         {
1747                 m_data[num] = val;
1748                 return 0;
1749         }
1750         return -EINVAL;
1751 }
1752
1753 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
1754 {
1755         int type;
1756         if (feparm->getSystem(type) || type != m_type)
1757                 return 0;
1758
1759         if (m_type == eDVBFrontend::feSatellite)
1760         {
1761                 ASSERT(m_sec);
1762                 eDVBFrontendParametersSatellite sat_parm;
1763                 ASSERT(!feparm->getDVBS(sat_parm));
1764                 return m_sec->canTune(sat_parm, this, 1 << m_fe);
1765         }
1766         return 1;
1767 }