6896110cb8ccb1a6b96d04e99a156e60f19b5372
[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_1:
807                         tmp = "HIERARCHY_1";
808                         break;
809                 case HIERARCHY_2:
810                         tmp = "HIERARCHY_2";
811                         break;
812                 case HIERARCHY_4:
813                         tmp = "HIERARCHY_4";
814                         break;
815                 default:
816                 case HIERARCHY_AUTO:
817                         tmp = "HIERARCHY_AUTO";
818                         break;
819         }
820         PutToDict(dict, "hierarchy_information", tmp);
821 }
822
823 PyObject *eDVBFrontend::readTransponderData(bool original)
824 {
825         PyObject *ret=PyDict_New();
826
827         if (ret)
828         {
829                 bool read=m_fd != -1;
830                 const char *tmp=0;
831
832                 PutToDict(ret, "tuner_number", m_fe);
833
834                 switch(m_type)
835                 {
836                         case feSatellite:
837                                 tmp = "DVB-S";
838                                 break;
839                         case feCable:
840                                 tmp = "DVB-C";
841                                 break;
842                         case feTerrestrial:
843                                 tmp = "DVB-T";
844                                 break;
845                         default:
846                                 tmp = "UNKNOWN";
847                                 read=false;
848                                 break;
849                 }
850                 PutToDict(ret, "tuner_type", tmp);
851
852                 if (read)
853                 {
854                         FRONTENDPARAMETERS front;
855
856                         tmp = "UNKNOWN";
857                         switch(m_state)
858                         {
859                                 case stateIdle:
860                                         tmp="IDLE";
861                                         break;
862                                 case stateTuning:
863                                         tmp="TUNING";
864                                         break;
865                                 case stateFailed:
866                                         tmp="FAILED";
867                                         break;
868                                 case stateLock:
869                                         tmp="LOCKED";
870                                         break;
871                                 case stateLostLock:
872                                         tmp="LOSTLOCK";
873                                         break;
874                                 default:
875                                         break;
876                         }
877                         PutToDict(ret, "tuner_state", tmp);
878
879                         PutToDict(ret, "tuner_locked", readFrontendData(Locked));
880                         PutToDict(ret, "tuner_synced", readFrontendData(Synced));
881                         PutToDict(ret, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
882                         PutToDict(ret, "tuner_signal_power", readFrontendData(signalPower));
883                         PutToDict(ret, "tuner_signal_quality", readFrontendData(signalQuality));
884
885                         if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
886                                 eDebug("FE_GET_FRONTEND (%m)");
887                         else
888                         {
889                                 tmp = "INVERSION_AUTO";
890                                 switch(parm_inversion)
891                                 {
892                                         case INVERSION_ON:
893                                                 tmp = "INVERSION_ON";
894                                                 break;
895                                         case INVERSION_OFF:
896                                                 tmp = "INVERSION_OFF";
897                                                 break;
898                                         default:
899                                                 break;
900                                 }
901                                 if (tmp)
902                                         PutToDict(ret, "inversion", tmp);
903
904                                 switch(m_type)
905                                 {
906                                         case feSatellite:
907                                                 fillDictWithSatelliteData(ret, original?parm:front, this);
908                                                 break;
909                                         case feCable:
910                                                 fillDictWithCableData(ret, original?parm:front);
911                                                 break;
912                                         case feTerrestrial:
913                                                 fillDictWithTerrestrialData(ret, original?parm:front);
914                                                 break;
915                                 }
916                         }
917                 }
918         }
919         else
920         {
921                 Py_INCREF(Py_None);
922                 ret = Py_None;
923         }
924         return ret;
925 }
926
927 #ifndef FP_IOCTL_GET_ID
928 #define FP_IOCTL_GET_ID 0
929 #endif
930 int eDVBFrontend::readInputpower()
931 {
932         int power=m_fe;  // this is needed for read inputpower from the correct tuner !
933
934         // open front prozessor
935         int fp=::open("/dev/dbox/fp0", O_RDWR);
936         if (fp < 0)
937         {
938                 eDebug("couldn't open fp");
939                 return -1;
940         }
941         static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
942         if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
943         {
944                 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
945                 return -1;
946         }
947         ::close(fp);
948
949         return power;
950 }
951
952 bool eDVBFrontend::setSecSequencePos(int steps)
953 {
954         eDebug("set sequence pos %d", steps);
955         if (!steps)
956                 return false;
957         while( steps > 0 )
958         {
959                 if (m_sec_sequence.current() != m_sec_sequence.end())
960                         ++m_sec_sequence.current();
961                 --steps;
962         }
963         while( steps < 0 )
964         {
965                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
966                         --m_sec_sequence.current();
967                 ++steps;
968         }
969         return true;
970 }
971
972 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
973 {
974         int delay=0;
975         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
976         {
977 //              eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
978                 switch (m_sec_sequence.current()->cmd)
979                 {
980                         case eSecCommand::SLEEP:
981                                 delay = m_sec_sequence.current()++->msec;
982                                 eDebug("[SEC] sleep %dms", delay);
983                                 break;
984                         case eSecCommand::GOTO:
985                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
986                                         ++m_sec_sequence.current();
987                                 break;
988                         case eSecCommand::SET_VOLTAGE:
989                         {
990                                 int voltage = m_sec_sequence.current()++->voltage;
991                                 eDebug("[SEC] setVoltage %d", voltage);
992                                 setVoltage(voltage);
993                                 break;
994                         }
995                         case eSecCommand::IF_VOLTAGE_GOTO:
996                         {
997                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
998                                 if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
999                                         break;
1000                                 ++m_sec_sequence.current();
1001                                 break;
1002                         }
1003                         case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1004                         {
1005                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1006                                 if ( compare.voltage != m_curVoltage && setSecSequencePos(compare.steps) )
1007                                         break;
1008                                 ++m_sec_sequence.current();
1009                                 break;
1010                         }
1011                         case eSecCommand::SET_TONE:
1012                                 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1013                                 setTone(m_sec_sequence.current()++->tone);
1014                                 break;
1015                         case eSecCommand::SEND_DISEQC:
1016                                 sendDiseqc(m_sec_sequence.current()->diseqc);
1017                                 eDebugNoNewLine("[SEC] sendDiseqc: ");
1018                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1019                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1020                                 eDebug("");
1021                                 ++m_sec_sequence.current();
1022                                 break;
1023                         case eSecCommand::SEND_TONEBURST:
1024                                 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1025                                 sendToneburst(m_sec_sequence.current()++->toneburst);
1026                                 break;
1027                         case eSecCommand::SET_FRONTEND:
1028                                 eDebug("[SEC] setFrontend");
1029                                 setFrontend();
1030                                 ++m_sec_sequence.current();
1031                                 break;
1032                         case eSecCommand::START_TUNE_TIMEOUT:
1033                                 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1034                                 ++m_sec_sequence.current();
1035                                 break;
1036                         case eSecCommand::SET_TIMEOUT:
1037                                 m_timeoutCount = m_sec_sequence.current()++->val;
1038                                 eDebug("[SEC] set timeout %d", m_timeoutCount);
1039                                 break;
1040                         case eSecCommand::IF_TIMEOUT_GOTO:
1041                                 if (!m_timeoutCount)
1042                                 {
1043                                         eDebug("[SEC] rotor timout");
1044                                         m_sec->setRotorMoving(false);
1045                                         setSecSequencePos(m_sec_sequence.current()->steps);
1046                                 }
1047                                 else
1048                                         ++m_sec_sequence.current();
1049                                 break;
1050                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1051                         {
1052                                 int idx = m_sec_sequence.current()++->val;
1053                                 if ( idx == 0 || idx == 1 )
1054                                 {
1055                                         m_idleInputpower[idx] = readInputpower();
1056                                         eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1057                                 }
1058                                 else
1059                                         eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1060                                 break;
1061                         }
1062                         case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1063                         {
1064                                 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1065                                 int idx = compare.voltage;
1066                                 if ( idx == 0 || idx == 1 )
1067                                 {
1068                                         int idle = readInputpower();
1069                                         int diff = abs(idle-m_idleInputpower[idx]);
1070                                         if ( diff > 0)
1071                                         {
1072                                                 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1073                                                 setSecSequencePos(compare.steps);
1074                                                 break;
1075                                         }
1076                                 }
1077                                 ++m_sec_sequence.current();
1078                                 break;
1079                         }
1080                         case eSecCommand::IF_TUNER_LOCKED_GOTO:
1081                         {
1082                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1083                                 if (readFrontendData(Locked))
1084                                 {
1085                                         eDebug("[SEC] locked step %d ok", cmd.okcount);
1086                                         ++cmd.okcount;
1087                                         if (cmd.okcount > 12)
1088                                         {
1089                                                 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1090                                                 setSecSequencePos(cmd.steps);
1091                                                 break;
1092                                         }
1093                                 }
1094                                 else
1095                                 {
1096                                         eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1097                                         --m_timeoutCount;
1098                                         if (!m_timeoutCount && m_retryCount > 0)
1099                                                 --m_retryCount;
1100                                         cmd.okcount=0;
1101                                 }
1102                                 ++m_sec_sequence.current();
1103                                 break;
1104                         }
1105                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1106                                 m_runningInputpower = readInputpower();
1107                                 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1108                                 ++m_sec_sequence.current();
1109                                 break;
1110                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1111                         {
1112                                 int idleInputpower = m_idleInputpower[ (m_curVoltage&1) ? 0 : 1];
1113                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1114                                 const char *txt = cmd.direction ? "running" : "stopped";
1115                                 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1116                                         txt,
1117                                         m_runningInputpower,
1118                                         idleInputpower,
1119                                         cmd.deltaA);
1120                                 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1121                                         || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1122                                 {
1123                                         ++cmd.okcount;
1124                                         eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1125                                         if ( cmd.okcount > 6 )
1126                                         {
1127                                                 m_sec->setRotorMoving(cmd.direction);
1128                                                 eDebug("[SEC] rotor is %s", txt);
1129                                                 if (setSecSequencePos(cmd.steps))
1130                                                         break;
1131                                         }
1132                                 }
1133                                 else
1134                                 {
1135                                         eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1136                                         --m_timeoutCount;
1137                                         if (!m_timeoutCount && m_retryCount > 0)
1138                                                 --m_retryCount;
1139                                         cmd.okcount=0;
1140                                 }
1141                                 ++m_sec_sequence.current();
1142                                 break;
1143                         }
1144                         case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1145                                 if (m_data[5] != -1 && m_data[6] != -1)
1146                                         setSecSequencePos(m_sec_sequence.current()->steps);
1147                                 else
1148                                         ++m_sec_sequence.current();
1149                                 break;
1150                         case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1151                                 m_data[5] = m_data[6] = -1;
1152                                 eDebug("[SEC] invalidate current rotorparams");
1153                                 ++m_sec_sequence.current();
1154                                 break;
1155                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1156                                 m_data[5] = m_data[3];
1157                                 m_data[6] = m_data[4];
1158                                 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
1159                                 ++m_sec_sequence.current();
1160                                 break;
1161                         case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1162                                 m_retryCount = m_sec_sequence.current()++->val;
1163                                 eDebug("[SEC] set rotor retries %d", m_retryCount);
1164                                 break;
1165                         case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1166                                 if (!m_retryCount)
1167                                 {
1168                                         eDebug("[SEC] no more rotor retrys");
1169                                         setSecSequencePos(m_sec_sequence.current()->steps);
1170                                 }
1171                                 else
1172                                         ++m_sec_sequence.current();
1173                                 break;
1174                         case eSecCommand::SET_POWER_LIMITING_MODE:
1175                         {
1176                                 int fd = m_fe ?
1177                                         ::open("/dev/i2c/1", O_RDWR) :
1178                                         ::open("/dev/i2c/0", O_RDWR);
1179
1180                                 unsigned char data[2];
1181                                 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1182                                 if(::read(fd, data, 1) != 1)
1183                                         eDebug("[SEC] error read lnbp (%m)");
1184                                 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1185                                 {
1186                                         data[0] |= 0x80;  // enable static current limiting
1187                                         eDebug("[SEC] set static current limiting");
1188                                 }
1189                                 else
1190                                 {
1191                                         data[0] &= ~0x80;  // enable dynamic current limiting
1192                                         eDebug("[SEC] set dynamic current limiting");
1193                                 }
1194                                 if(::write(fd, data, 1) != 1)
1195                                         eDebug("[SEC] error write lnbp (%m)");
1196                                 ::close(fd);
1197                                 ++m_sec_sequence.current();
1198                                 break;
1199                         }
1200                         default:
1201                                 ++m_sec_sequence.current();
1202                                 eDebug("[SEC] unhandled sec command");
1203                 }
1204                 m_tuneTimer->start(delay,true);
1205         }
1206 }
1207
1208 void eDVBFrontend::setFrontend()
1209 {
1210         eDebug("setting frontend %d", m_fe);
1211         m_sn->start();
1212         feEvent(-1);
1213         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1214         {
1215                 perror("FE_SET_FRONTEND failed");
1216                 return;
1217         }
1218 }
1219
1220 RESULT eDVBFrontend::getFrontendType(int &t)
1221 {
1222         if (m_type == -1)
1223                 return -ENODEV;
1224         t = m_type;
1225         return 0;
1226 }
1227
1228 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1229 {
1230         int res;
1231         if (!m_sec)
1232         {
1233                 eWarning("no SEC module active!");
1234                 return -ENOENT;
1235         }
1236         res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
1237         if (!res)
1238         {
1239                 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1240                 switch (feparm.inversion)
1241                 {
1242                         case eDVBFrontendParametersSatellite::Inversion::On:
1243                                 parm_inversion = INVERSION_ON;
1244                                 break;
1245                         case eDVBFrontendParametersSatellite::Inversion::Off:
1246                                 parm_inversion = INVERSION_OFF;
1247                                 break;
1248                         default:
1249                         case eDVBFrontendParametersSatellite::Inversion::Unknown:
1250                                 parm_inversion = INVERSION_AUTO;
1251                                 break;
1252                 }
1253                 switch (feparm.fec)
1254                 {
1255                         default:
1256                         case eDVBFrontendParametersSatellite::FEC::fNone:
1257                                 eDebug("no fec set.. assume auto");
1258                         case eDVBFrontendParametersSatellite::FEC::fAuto:
1259                                 parm_u_qpsk_fec_inner = FEC_AUTO;
1260                                 break;
1261                         case eDVBFrontendParametersSatellite::FEC::f1_2:
1262                                 parm_u_qpsk_fec_inner = FEC_1_2;
1263                                 break;
1264                         case eDVBFrontendParametersSatellite::FEC::f2_3:
1265                                 parm_u_qpsk_fec_inner = FEC_2_3;
1266                                 break;
1267                         case eDVBFrontendParametersSatellite::FEC::f3_4:
1268                                 parm_u_qpsk_fec_inner = FEC_3_4;
1269                                 break;
1270                         case eDVBFrontendParametersSatellite::FEC::f5_6:
1271                                 parm_u_qpsk_fec_inner = FEC_5_6;
1272                                 break;
1273                         case eDVBFrontendParametersSatellite::FEC::f7_8:
1274                                 parm_u_qpsk_fec_inner = FEC_7_8;
1275                                 break;
1276                 }
1277                 // FIXME !!! get frequency range from tuner
1278                 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1279                 {
1280                         eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1281                         return -EINVAL;
1282                 }
1283                 eDebug("tuning to %d mhz", parm_frequency/1000);
1284         }
1285         return res;
1286 }
1287
1288 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1289 {
1290         parm_frequency = feparm.frequency * 1000;
1291         parm_u_qam_symbol_rate = feparm.symbol_rate;
1292         switch (feparm.modulation)
1293         {
1294         case eDVBFrontendParametersCable::Modulation::QAM16:
1295                 parm_u_qam_modulation = QAM_16;
1296                 break;
1297         case eDVBFrontendParametersCable::Modulation::QAM32:
1298                 parm_u_qam_modulation = QAM_32;
1299                 break;
1300         case eDVBFrontendParametersCable::Modulation::QAM64:
1301                 parm_u_qam_modulation = QAM_64;
1302                 break;
1303         case eDVBFrontendParametersCable::Modulation::QAM128:
1304                 parm_u_qam_modulation = QAM_128;
1305                 break;
1306         case eDVBFrontendParametersCable::Modulation::QAM256:
1307                 parm_u_qam_modulation = QAM_256;
1308                 break;
1309         default:
1310         case eDVBFrontendParametersCable::Modulation::Auto:
1311                 parm_u_qam_modulation = QAM_AUTO;
1312                 break;
1313         }
1314         switch (feparm.inversion)
1315         {
1316         case eDVBFrontendParametersCable::Inversion::On:
1317                 parm_inversion = INVERSION_ON;
1318                 break;
1319         case eDVBFrontendParametersCable::Inversion::Off:
1320                 parm_inversion = INVERSION_OFF;
1321                 break;
1322         default:
1323         case eDVBFrontendParametersCable::Inversion::Unknown:
1324                 parm_inversion = INVERSION_AUTO;
1325                 break;
1326         }
1327         switch (feparm.fec_inner)
1328         {
1329         case eDVBFrontendParametersCable::FEC::fNone:
1330                 parm_u_qam_fec_inner = FEC_NONE;
1331                 break;
1332         case eDVBFrontendParametersCable::FEC::f1_2:
1333                 parm_u_qam_fec_inner = FEC_1_2;
1334                 break;
1335         case eDVBFrontendParametersCable::FEC::f2_3:
1336                 parm_u_qam_fec_inner = FEC_2_3;
1337                 break;
1338         case eDVBFrontendParametersCable::FEC::f3_4:
1339                 parm_u_qam_fec_inner = FEC_3_4;
1340                 break;
1341         case eDVBFrontendParametersCable::FEC::f5_6:
1342                 parm_u_qam_fec_inner = FEC_5_6;
1343                 break;
1344         case eDVBFrontendParametersCable::FEC::f7_8:
1345                 parm_u_qam_fec_inner = FEC_7_8;
1346                 break;
1347 #if HAVE_DVB_API_VERSION >= 3
1348         case eDVBFrontendParametersCable::FEC::f8_9:
1349                 parm_u_qam_fec_inner = FEC_8_9;
1350                 break;
1351 #endif
1352         default:
1353         case eDVBFrontendParametersCable::FEC::fAuto:
1354                 parm_u_qam_fec_inner = FEC_AUTO;
1355                 break;
1356         }
1357         return 0;
1358 }
1359
1360 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1361 {
1362         parm_frequency = feparm.frequency;
1363
1364         switch (feparm.bandwidth)
1365         {
1366         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1367                 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1368                 break;
1369         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1370                 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1371                 break;
1372         case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1373                 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1374                 break;
1375         default:
1376         case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1377                 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1378                 break;
1379         }
1380         switch (feparm.code_rate_LP)
1381         {
1382         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1383                 parm_u_ofdm_code_rate_LP = FEC_1_2;
1384                 break;
1385         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1386                 parm_u_ofdm_code_rate_LP = FEC_2_3;
1387                 break;
1388         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1389                 parm_u_ofdm_code_rate_LP = FEC_3_4;
1390                 break;
1391         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1392                 parm_u_ofdm_code_rate_LP = FEC_5_6;
1393                 break;
1394         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1395                 parm_u_ofdm_code_rate_LP = FEC_7_8;
1396                 break;
1397         default:
1398         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1399                 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1400                 break;
1401         }
1402         switch (feparm.code_rate_HP)
1403         {
1404         case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1405                 parm_u_ofdm_code_rate_HP = FEC_1_2;
1406                 break;
1407         case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1408                 parm_u_ofdm_code_rate_HP = FEC_2_3;
1409                 break;
1410         case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1411                 parm_u_ofdm_code_rate_HP = FEC_3_4;
1412                 break;
1413         case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1414                 parm_u_ofdm_code_rate_HP = FEC_5_6;
1415                 break;
1416         case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1417                 parm_u_ofdm_code_rate_HP = FEC_7_8;
1418                 break;
1419         default:
1420         case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1421                 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1422                 break;
1423         }
1424         switch (feparm.modulation)
1425         {
1426         case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1427                 parm_u_ofdm_constellation = QPSK;
1428                 break;
1429         case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1430                 parm_u_ofdm_constellation = QAM_16;
1431                 break;
1432         default:
1433         case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1434                 parm_u_ofdm_constellation = QAM_AUTO;
1435                 break;
1436         }
1437         switch (feparm.transmission_mode)
1438         {
1439         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1440                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1441                 break;
1442         case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1443                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1444                 break;
1445         default:
1446         case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1447                 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1448                 break;
1449         }
1450         switch (feparm.guard_interval)
1451         {
1452                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1453                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1454                         break;
1455                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1456                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1457                         break;
1458                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1459                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1460                         break;
1461                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1462                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1463                         break;
1464                 default:
1465                 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1466                         parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1467                         break;
1468         }
1469         switch (feparm.hierarchy)
1470         {
1471                 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1472                         parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1473                         break;
1474                 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1475                         parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1476                         break;
1477                 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1478                         parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1479                         break;
1480                 default:
1481                 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1482                         parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1483                         break;
1484         }
1485         return 0;
1486 }
1487
1488 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1489 {
1490         eDebug("(%d)tune", m_fe);
1491
1492         m_timeout->stop();
1493
1494         int res=0;
1495
1496         if (m_type == -1)
1497                 return -ENODEV;
1498
1499         m_sn->stop();
1500         m_sec_sequence.clear();
1501
1502         switch (m_type)
1503         {
1504         case feSatellite:
1505         {
1506                 eDVBFrontendParametersSatellite feparm;
1507                 if (where.getDVBS(feparm))
1508                 {
1509                         eDebug("no dvbs data!");
1510                         return -EINVAL;
1511                 }
1512                 res=prepare_sat(feparm);
1513                 m_sec->setRotorMoving(false);
1514                 break;
1515         }
1516         case feCable:
1517         {
1518                 eDVBFrontendParametersCable feparm;
1519                 if (where.getDVBC(feparm))
1520                         return -EINVAL;
1521                 res=prepare_cable(feparm);
1522                 if (!res)
1523                 {
1524                         m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1525                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1526                 }
1527                 break;
1528         }
1529         case feTerrestrial:
1530         {
1531                 eDVBFrontendParametersTerrestrial feparm;
1532                 if (where.getDVBT(feparm))
1533                 {
1534                         eDebug("no -T data");
1535                         return -EINVAL;
1536                 }
1537                 res=prepare_terrestrial(feparm);
1538                 if (!res)
1539                 {
1540                         m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1541                         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1542                 }
1543                 break;
1544         }
1545         }
1546
1547         if (!res)  // prepare ok
1548         {
1549                 m_tuneTimer->start(0,true);
1550                 m_sec_sequence.current() = m_sec_sequence.begin();
1551
1552                 if (m_state != stateTuning)
1553                 {
1554                         m_tuning = 1;
1555                         m_state = stateTuning;
1556                         m_stateChanged(this);
1557                 }
1558         }
1559
1560         return res;
1561 }
1562
1563 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1564 {
1565         connection = new eConnection(this, m_stateChanged.connect(stateChange));
1566         return 0;
1567 }
1568
1569 RESULT eDVBFrontend::setVoltage(int voltage)
1570 {
1571         if (m_type != feSatellite)
1572                 return -1;
1573 #if HAVE_DVB_API_VERSION < 3
1574         secVoltage vlt;
1575 #else
1576         bool increased=false;
1577         fe_sec_voltage_t vlt;
1578 #endif
1579         m_curVoltage=voltage;
1580         switch (voltage)
1581         {
1582         case voltageOff:
1583                 for (int i=0; i < 3; ++i)  // reset diseqc
1584                         m_data[i]=-1;
1585                 vlt = SEC_VOLTAGE_OFF;
1586                 break;
1587         case voltage13_5:
1588 #if HAVE_DVB_API_VERSION < 3
1589                 vlt = SEC_VOLTAGE_13_5;
1590                 break;
1591 #else
1592                 increased = true;
1593 #endif
1594         case voltage13:
1595                 vlt = SEC_VOLTAGE_13;
1596                 break;
1597         case voltage18_5:
1598 #if HAVE_DVB_API_VERSION < 3
1599                 vlt = SEC_VOLTAGE_18_5;
1600                 break;
1601 #else
1602                 increased = true;
1603 #endif
1604         case voltage18:
1605                 vlt = SEC_VOLTAGE_18;
1606                 break;
1607         default:
1608                 return -ENODEV;
1609         }
1610 #if HAVE_DVB_API_VERSION < 3
1611         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1612 #else
1613         if (::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1614                 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
1615         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
1616 #endif
1617 }
1618
1619 RESULT eDVBFrontend::getState(int &state)
1620 {
1621         state = m_state;
1622         return 0;
1623 }
1624
1625 RESULT eDVBFrontend::setTone(int t)
1626 {
1627         if (m_type != feSatellite)
1628                 return -1;
1629 #if HAVE_DVB_API_VERSION < 3
1630         secToneMode_t tone;
1631 #else
1632         fe_sec_tone_mode_t tone;
1633 #endif
1634
1635         switch (t)
1636         {
1637         case toneOn:
1638                 tone = SEC_TONE_ON;
1639                 break;
1640         case toneOff:
1641                 tone = SEC_TONE_OFF;
1642                 break;
1643         default:
1644                 return -ENODEV;
1645         }
1646 #if HAVE_DVB_API_VERSION < 3    
1647         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
1648 #else   
1649         return ::ioctl(m_fd, FE_SET_TONE, tone);
1650 #endif
1651 }
1652
1653 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
1654         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
1655 #endif
1656
1657 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
1658 {
1659 #if HAVE_DVB_API_VERSION < 3
1660         struct secCommand cmd;
1661         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
1662         cmd.u.diseqc.cmdtype = diseqc.data[0];
1663         cmd.u.diseqc.addr = diseqc.data[1];
1664         cmd.u.diseqc.cmd = diseqc.data[2];
1665         cmd.u.diseqc.numParams = diseqc.len-3;
1666         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
1667         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
1668 #else
1669         struct dvb_diseqc_master_cmd cmd;
1670         memcpy(cmd.msg, diseqc.data, diseqc.len);
1671         cmd.msg_len = diseqc.len;
1672         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
1673 #endif
1674                 return -EINVAL;
1675         return 0;
1676 }
1677
1678 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
1679         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
1680 #endif
1681 RESULT eDVBFrontend::sendToneburst(int burst)
1682 {
1683 #if HAVE_DVB_API_VERSION < 3
1684         secMiniCmd cmd = SEC_MINI_NONE;
1685 #else
1686         fe_sec_mini_cmd_t cmd = SEC_MINI_A;
1687 #endif
1688         if ( burst == eDVBSatelliteDiseqcParameters::A )
1689                 cmd = SEC_MINI_A;
1690         else if ( burst == eDVBSatelliteDiseqcParameters::B )
1691                 cmd = SEC_MINI_B;
1692 #if HAVE_DVB_API_VERSION < 3
1693         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
1694                 return -EINVAL;
1695 #else
1696         if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
1697                 return -EINVAL;
1698 #endif
1699         return 0;
1700 }
1701
1702 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
1703 {
1704         m_sec = sec;
1705         return 0;
1706 }
1707
1708 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
1709 {
1710         m_sec_sequence = list;
1711         return 0;
1712 }
1713
1714 RESULT eDVBFrontend::getData(int num, int &data)
1715 {
1716         if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1717         {
1718                 data = m_data[num];
1719                 return 0;
1720         }
1721         return -EINVAL;
1722 }
1723
1724 RESULT eDVBFrontend::setData(int num, int val)
1725 {
1726         if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1727         {
1728                 m_data[num] = val;
1729                 return 0;
1730         }
1731         return -EINVAL;
1732 }
1733
1734 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
1735 {
1736         int type;
1737         if (feparm->getSystem(type) || type != m_type)
1738                 return 0;
1739
1740         if (m_type == eDVBFrontend::feSatellite)
1741         {
1742                 ASSERT(m_sec);
1743                 eDVBFrontendParametersSatellite sat_parm;
1744                 ASSERT(!feparm->getDVBS(sat_parm));
1745                 return m_sec->canTune(sat_parm, this, 1 << m_fe);
1746         }
1747         return 1;
1748 }