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