- add iSeekableService, implement it for serviceDvb
[enigma2.git] / lib / dvb / frontend.cpp
1 #include <config.h>
2 #include <lib/dvb/dvb.h>
3 #include <lib/base/eerror.h>
4 #include <errno.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8
9 #if HAVE_DVB_API_VERSION < 3
10 #include <ost/frontend.h>
11 #include <ost/sec.h>
12 #define QAM_AUTO                                (Modulation)6
13 #define TRANSMISSION_MODE_AUTO  (TransmitMode)2
14 #define BANDWIDTH_AUTO                  (BandWidth)3
15 #define GUARD_INTERVAL_AUTO             (GuardInterval)4
16 #define HIERARCHY_AUTO                  (Hierarchy)4
17 #define constellation Constellation
18 #define guard_interval guardInterval
19 #define hierarchy_information HierarchyInformation
20 #define code_rate_HP HP_CodeRate
21 #define code_rate_LP LP_CodeRate
22 #else
23 #include <linux/dvb/frontend.h>
24 #endif
25
26 #include <lib/dvb_si/satellite_delivery_system_descriptor.h>
27 #include <lib/dvb_si/cable_delivery_system_descriptor.h>
28 #include <lib/dvb_si/terrestrial_delivery_system_descriptor.h>
29
30 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
31 {
32         frequency    = descriptor.getFrequency() * 10;
33         symbol_rate  = descriptor.getSymbolRate() * 100;
34         switch (descriptor.getPolarization())
35         {
36         case 0:
37                 polarisation = Polarisation::Horizontal;
38                 break;
39         case 1:
40                 polarisation = Polarisation::Vertical;
41                 break;
42         case 2:
43                 polarisation = Polarisation::CircularLeft;
44                 break;
45         case 3:
46                 polarisation = Polarisation::CircularRight;
47                 break;
48         }
49         switch (descriptor.getFecInner())
50         {
51         case 1:
52                 fec = FEC::f1_2;
53                 break;
54         case 2:
55                 fec = FEC::f2_3;
56                 break;
57         case 3:
58                 fec = FEC::f3_4;
59                 break;
60         case 4:
61                 fec = FEC::f5_6;
62                 break;
63         case 5:
64                 fec = FEC::f7_8;
65                 break;
66         case 0xF:
67                 fec = FEC::fNone;
68                 break;
69         default:
70                 fec = FEC::fAuto;
71                 break;
72         }
73         inversion = Inversion::Unknown;
74         orbital_position  = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
75         orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
76         orbital_position += ((descriptor.getOrbitalPosition() >> 4) & 0xF) * 10;
77         orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
78         if (orbital_position && (!descriptor.getWestEastFlag()))
79                 orbital_position = 3600 - orbital_position;
80 }
81
82 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
83 {
84         eFatal("nyi");
85 }
86
87 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor  &)
88 {
89         eFatal("nyi");
90 }
91
92 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
93 {
94 }
95
96 DEFINE_REF(eDVBFrontendParameters);
97
98 RESULT eDVBFrontendParameters::getSystem(int &t) const
99 {
100         if (m_type == -1)
101                 return -1;
102         t = m_type;
103         return 0;
104 }
105
106 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
107 {
108         if (m_type != iDVBFrontend::feSatellite)
109                 return -1;
110         p = sat;
111         return 0;
112 }
113
114 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
115 {
116         if (m_type != iDVBFrontend::feCable)
117                 return -1;
118         p = cable;
119         return 0;
120 }
121
122 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
123 {
124         if (m_type != iDVBFrontend::feTerrestrial)
125                 return -1;
126         p = terrestrial;
127         return 0;
128 }
129
130 RESULT eDVBFrontendParameters::setDVBS(eDVBFrontendParametersSatellite &p)
131 {
132         sat = p;
133         m_type = iDVBFrontend::feSatellite;
134         return 0;
135 }
136
137 RESULT eDVBFrontendParameters::setDVBC(eDVBFrontendParametersCable &p)
138 {
139         cable = p;
140         m_type = iDVBFrontend::feCable;
141         return 0;
142 }
143
144 RESULT eDVBFrontendParameters::setDVBT(eDVBFrontendParametersTerrestrial &p)
145 {
146         terrestrial = p;
147         m_type = iDVBFrontend::feTerrestrial;
148         return 0;
149 }
150
151 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff) const
152 {
153         if (!parm)
154                 return -1;
155         int type;
156         if (parm->getSystem(type))
157                 return -1;
158         if (type != m_type)
159         {
160                 diff = 1<<30; // big difference
161                 return 0;
162         }
163         
164         switch (type)
165         {
166         case iDVBFrontend::feSatellite:
167         {
168                 eDVBFrontendParametersSatellite osat;
169                 if (parm->getDVBS(osat))
170                         return -2;
171                 
172                 if (sat.orbital_position != osat.orbital_position)
173                         diff = 1<<29;
174                 else if (sat.polarisation != osat.polarisation)
175                         diff = 1<<28;
176                 else 
177                         diff = abs(sat.frequency - osat.frequency);
178                 return 0;
179         }
180         case iDVBFrontend::feCable:
181         case iDVBFrontend::feTerrestrial:
182         default:
183                 return -1;
184         }
185         return 0;
186 }
187
188 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const 
189 {
190         switch (m_type)
191         {
192         case iDVBFrontend::feSatellite:
193         {
194                 hash  = sat.frequency & 0xFFFF;
195                 hash |= sat.orbital_position << 16;
196                 return 0;
197         }
198         case iDVBFrontend::feCable:
199         case iDVBFrontend::feTerrestrial:
200         default:
201                 return -1;
202         }
203 }
204
205 DEFINE_REF(eDVBFrontend);
206
207 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok): m_type(-1)
208 {
209 #if HAVE_DVB_API_VERSION < 3
210         char sec_filename[128];
211 #endif
212         char filename[128];
213
214         int result;
215
216         m_sn = 0;
217         m_timeout = 0;
218
219 #if HAVE_DVB_API_VERSION < 3
220         sprintf(sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
221         m_secfd = ::open(sec_filename, O_RDWR);
222         if (m_secfd < 0)
223         {
224                 eWarning("failed! (%s) %m", sec_filename);
225                 ok = 0;
226                 return;
227         }
228         else
229                 eDebug("m_secfd is %d", m_secfd);
230
231         FrontendInfo fe_info;
232         sprintf(filename, "/dev/dvb/card%d/frontend%d", adap, fe);
233 #else
234         dvb_frontend_info fe_info;      
235         sprintf(filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
236 #endif
237         eDebug("opening frontend.");
238         m_fd = ::open(filename, O_RDWR|O_NONBLOCK);
239         if (m_fd < 0)
240         {
241                 eWarning("failed! (%s) %m", filename);
242                 ok = 0;
243                 return;
244         }
245
246         result = ::ioctl(m_fd, FE_GET_INFO, &fe_info);
247         
248         if (result < 0) {
249                 eWarning("ioctl FE_GET_INFO failed");
250                 ::close(m_fd);
251                 m_fd = -1;
252                 ok = 0;
253                 return;
254         }
255
256         switch (fe_info.type) 
257         {
258         case FE_QPSK:
259                 m_type = feSatellite;
260                 break;
261         case FE_QAM:
262                 m_type = feCable;
263                 break;
264         case FE_OFDM:
265                 m_type = feTerrestrial;
266                 break;
267         default:
268                 eWarning("unknown frontend type.");
269                 ::close(m_fd);
270                 m_fd = -1;
271                 ok = 0;
272                 return;
273         }
274         eDebug("detected %s frontend", "satellite\0cable\0    terrestrial"+fe_info.type*9);
275         ok = 1;
276
277         m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
278         CONNECT(m_sn->activated, eDVBFrontend::feEvent);
279         m_sn->start();
280
281         m_timeout = new eTimer(eApp);
282         CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
283
284         m_tuneTimer = new eTimer(eApp);
285         CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
286
287         memset(m_data, 0xFFFF, sizeof(m_data));
288
289         return;
290 }
291
292 eDVBFrontend::~eDVBFrontend()
293 {
294         if (m_fd >= 0)
295                 ::close(m_fd);
296         if (m_sn)
297                 delete m_sn;
298         if (m_timeout)
299                 delete m_timeout;
300 }
301
302 void eDVBFrontend::feEvent(int w)
303 {
304         while (1)
305         {
306 #if HAVE_DVB_API_VERSION < 3
307                 FrontendEvent event;
308 #else
309                 dvb_frontend_event event;
310 #endif
311                 int res;
312                 int state;
313                 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
314                 
315                 if (res && (errno == EAGAIN))
316                         break;
317
318                 if (res)
319                 {
320                         eWarning("FE_GET_EVENT failed! %m");
321                         return;
322                 }
323                 
324                 if (w < 0)
325                         continue;
326
327 #if HAVE_DVB_API_VERSION < 3
328                 if (event.type == FE_COMPLETION_EV)
329 #else
330                 eDebug("fe event: status %x, inversion %s", event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
331                 if (event.status & FE_HAS_LOCK)
332 #endif
333                 {
334                         state = stateLock;
335                 } else
336                 {
337                         if (m_tuning)
338                                 state = stateTuning;
339                         else
340                                 state = stateFailed;
341                 }
342                 if (m_state != state)
343                 {
344                         m_state = state;
345                         m_stateChanged(this);
346                 }
347         }
348 }
349
350 void eDVBFrontend::timeout()
351 {
352         int state;
353         if (m_state == stateTuning)
354         {
355                 state = stateFailed;
356                 eDebug("DVBFrontend: timeout");
357                 if (m_state != state)
358                 {
359                         m_state = state;
360                         m_stateChanged(this);
361                 }
362         } else
363                 m_tuning = 0;
364 }
365
366 #ifndef FP_IOCTL_GET_ID
367 #define FP_IOCTL_GET_ID 0
368 #endif
369 int eDVBFrontend::readInputpower()
370 {
371         int power=0;
372 //      if ( eSystemInfo::getInstance()->canMeasureLNBCurrent() )
373         {
374 //              switch ( eSystemInfo::getInstance()->getHwType() )
375                 {
376 //                      case eSystemInfo::DM7000:
377 //                      case eSystemInfo::DM7020:
378                         {
379                                 // open front prozessor
380                                 int fp=::open("/dev/dbox/fp0", O_RDWR);
381                                 if (fp < 0)
382                                 {
383                                         eDebug("couldn't open fp");
384                                         return -1;
385                                 }
386                                 static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
387                                 if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
388                                 {
389                                         eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
390                                         return -1;
391                                 }
392                                 ::close(fp);
393 //                              break;
394                         }
395 //                      default:
396 //                              eDebug("Inputpower read for platform %d not yet implemented", eSystemInfo::getInstance()->getHwType());
397                 }
398         }
399         return power;
400 }
401
402 bool eDVBFrontend::setSecSequencePos(int steps)
403 {
404         eDebug("set sequence pos %d", steps);
405         if (!steps)
406                 return false;
407         while( steps > 0 )
408         {
409                 if (m_sec_sequence.current() != m_sec_sequence.end())
410                         ++m_sec_sequence.current();
411                 --steps;
412         }
413         while( steps < 0 )
414         {
415                 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
416                         --m_sec_sequence.current();
417                 ++steps;
418         }
419         return true;
420 }
421
422 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
423 {
424         int delay=0;
425         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
426         {
427                 switch (m_sec_sequence.current()->cmd)
428                 {
429                         case eSecCommand::SLEEP:
430                                 delay = m_sec_sequence.current()++->msec;
431                                 eDebug("sleep %dms", delay);
432                                 break;
433                         case eSecCommand::GOTO:
434                                 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
435                                         ++m_sec_sequence.current();
436                                 break;
437                         case eSecCommand::SET_VOLTAGE:
438                                 eDebug("setVoltage %d", m_sec_sequence.current()->voltage);
439                                 setVoltage(m_sec_sequence.current()++->voltage);
440                                 break;
441                         case eSecCommand::SET_TONE:
442                                 eDebug("setTone %d", m_sec_sequence.current()->tone);
443                                 setTone(m_sec_sequence.current()++->tone);
444                                 break;
445                         case eSecCommand::SEND_DISEQC:
446                                 sendDiseqc(m_sec_sequence.current()->diseqc);
447                                 eDebugNoNewLine("sendDiseqc: ");
448                                 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
449                                     eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
450                                 eDebug("");
451                                 ++m_sec_sequence.current();
452                                 break;
453                         case eSecCommand::SEND_TONEBURST:
454                                 eDebug("sendToneburst: %d", m_sec_sequence.current()->toneburst);
455                                 sendToneburst(m_sec_sequence.current()++->toneburst);
456                                 break;
457                         case eSecCommand::SET_FRONTEND:
458                                 eDebug("setFrontend");
459                                 setFrontend();
460                                 ++m_sec_sequence.current();
461                                 break;
462                         case eSecCommand::MEASURE_IDLE_INPUTPOWER:
463                                 m_idleInputpower = readInputpower();
464                                 eDebug("idleInputpower is %d", m_idleInputpower);
465                                 ++m_sec_sequence.current();
466                                 break;
467                         case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
468                                 m_runningInputpower = readInputpower();
469                                 eDebug("runningInputpower is %d", m_runningInputpower);
470                                 ++m_sec_sequence.current();
471                                 break;
472                         case eSecCommand::SET_TIMEOUT:
473                                 m_timeoutCount = m_sec_sequence.current()++->val;
474                                 eDebug("set timeout %d", m_timeoutCount);
475                                 break;
476                         case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
477                                 m_data[5] = m_data[3];
478                                 m_data[6] = m_data[4];
479                                 eDebug("update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
480                                 ++m_sec_sequence.current();
481                                 break;
482                         case eSecCommand::IF_TIMEOUT_GOTO:
483                                 if (!m_timeoutCount)
484                                         setSecSequencePos(m_sec_sequence.current()->steps);
485                                 else
486                                         ++m_sec_sequence.current();
487                                 break;
488                         case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
489                         {
490                                 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
491                                 const char *txt = cmd.direction ? "running" : "stopped";
492                                 eDebug("waiting for rotor %s", txt);
493                                 eDebug("%s %d, idle %d, delta %d",
494                                         txt,
495                                         m_runningInputpower,
496                                         m_idleInputpower,
497                                         cmd.deltaA);
498                                 if ( (cmd.direction && abs(m_runningInputpower - m_idleInputpower) >= cmd.deltaA)
499                                         || (!cmd.direction && abs(m_runningInputpower - m_idleInputpower) <= cmd.deltaA) )
500                                 {
501                                         ++cmd.okcount;
502                                         eDebug("rotor %s step %d ok", txt, cmd.okcount);
503                                         if ( cmd.okcount > 1 )
504                                         {
505                                                 eDebug("rotor is %s", txt);
506                                                 if (setSecSequencePos(cmd.steps))
507                                                         break;
508                                         }
509                                 }
510                                 else
511                                 {
512                                         eDebug("rotor not %s... reset counter.. increase timeout", txt);
513                                         --m_timeoutCount;
514                                         cmd.okcount=0;
515                                 }
516                                 ++m_sec_sequence.current();
517                                 break;
518                         }
519                         default:
520                                 ++m_sec_sequence.current();
521                                 eDebug("unhandled sec command");
522                 }
523                 m_tuneTimer->start(delay,true);
524         }
525 }
526
527 void eDVBFrontend::setFrontend()
528 {
529         eDebug("setting frontend..\n");
530         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
531         {
532                 perror("FE_SET_FRONTEND failed");
533                 return;
534         }
535
536         if (m_state != stateTuning)
537         {
538                 m_tuning = 1;
539                 m_state = stateTuning;
540                 m_stateChanged(this);
541         }
542         m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
543 }
544
545 RESULT eDVBFrontend::getFrontendType(int &t)
546 {
547         if (m_type == -1)
548                 return -ENODEV;
549         t = m_type;
550         return 0;
551 }
552
553 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
554 {
555         if (m_type == -1)
556                 return -ENODEV;
557
558         feEvent(-1);
559
560         m_sec_sequence.clear();
561
562         eDebug("eDVBFrontend::tune. type: %d", m_type);
563
564         switch (m_type)
565         {
566         case feSatellite:
567         {
568                 int res;
569                 eDVBFrontendParametersSatellite feparm;
570                 if (where.getDVBS(feparm))
571                 {
572                         eDebug("no dvbs data!");
573                         return -EINVAL;
574                 }
575                 if (!m_sec)
576                 {
577                         eWarning("no SEC module active!");
578                         return -ENOENT;
579                 }
580                 
581                 res = m_sec->prepare(*this, parm, feparm);
582                 if (res)
583                         return res;
584 #if HAVE_DVB_API_VERSION < 3
585                 eDebug("tuning to %d mhz", parm.Frequency/1000);
586 #else
587                 eDebug("tuning to %d mhz", parm.frequency/1000);
588 #endif
589                 break;
590         }
591         case feCable:
592         {
593                 eDVBFrontendParametersCable feparm;
594                 if (where.getDVBC(feparm))
595                         return -EINVAL;
596                 eFatal("cable tuning nyi");
597         }
598         case feTerrestrial:
599         {
600                 eDVBFrontendParametersTerrestrial feparm;
601                 if (where.getDVBT(feparm))
602                 {
603                         eDebug("no -T data");
604                         return -EINVAL;
605                 }
606 #if HAVE_DVB_API_VERSION < 3
607                 parm.Frequency = feparm.frequency;
608 #else
609                 parm.frequency = feparm.frequency;
610 #endif
611
612                 switch (feparm.bandwidth)
613                 {
614                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
615 #if HAVE_DVB_API_VERSION < 3
616                         parm.u.ofdm.bandWidth =
617 #else
618                         parm.u.ofdm.bandwidth =
619 #endif
620                                 BANDWIDTH_8_MHZ;
621                         break;
622                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
623 #if HAVE_DVB_API_VERSION < 3
624                         parm.u.ofdm.bandWidth =
625 #else
626                         parm.u.ofdm.bandwidth =
627 #endif
628                                 BANDWIDTH_7_MHZ;
629                         break;
630                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
631 #if HAVE_DVB_API_VERSION < 3
632                         parm.u.ofdm.bandWidth =
633 #else
634                         parm.u.ofdm.bandwidth =
635 #endif
636                                 BANDWIDTH_6_MHZ;
637                         break;
638                 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
639 #if HAVE_DVB_API_VERSION < 3
640                         parm.u.ofdm.bandWidth =
641 #else
642                         parm.u.ofdm.bandwidth =
643 #endif
644                                 BANDWIDTH_AUTO;
645                         break;
646                 default:
647                         eWarning("invalid OFDM bandwith");
648                         return -EINVAL;
649                 }
650                 
651                 parm.u.ofdm.code_rate_HP = FEC_AUTO;
652                 parm.u.ofdm.code_rate_LP = FEC_AUTO;
653                 
654                 switch (feparm.modulation)
655                 {
656                 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
657                         parm.u.ofdm.constellation = QPSK;
658                         break;
659                 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
660                         parm.u.ofdm.constellation = QAM_16;
661                         break;
662                 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
663                         parm.u.ofdm.constellation = QAM_AUTO;
664                         break;
665                 }
666                 
667                 switch (feparm.transmission_mode)
668                 {
669                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
670 #if HAVE_DVB_API_VERSION < 3
671                         parm.u.ofdm.TransmissionMode =
672 #else
673                         parm.u.ofdm.transmission_mode =
674 #endif
675                                 TRANSMISSION_MODE_2K;
676                         break;
677                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
678 #if HAVE_DVB_API_VERSION < 3
679                         parm.u.ofdm.TransmissionMode =
680 #else
681                         parm.u.ofdm.transmission_mode =
682 #endif
683                                 TRANSMISSION_MODE_8K;
684                         break;
685                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
686 #if HAVE_DVB_API_VERSION < 3
687                         parm.u.ofdm.TransmissionMode =
688 #else
689                         parm.u.ofdm.transmission_mode =
690 #endif
691                                 TRANSMISSION_MODE_AUTO;
692                         break;
693                 }
694                 
695                 parm.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
696                 parm.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
697 #if HAVE_DVB_API_VERSION < 3
698                 parm.Inversion =
699 #else
700                 parm.inversion =
701 #endif
702                         INVERSION_AUTO;
703                 break;
704         }
705         }
706
707         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
708         m_tuneTimer->start(0,true);
709         m_sec_sequence.current() = m_sec_sequence.begin();
710
711         return 0;
712 }
713
714 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
715 {
716         connection = new eConnection(this, m_stateChanged.connect(stateChange));
717         return 0;
718 }
719
720 RESULT eDVBFrontend::setVoltage(int voltage)
721 {
722 #if HAVE_DVB_API_VERSION < 3
723         secVoltage vlt;
724 #else
725         fe_sec_voltage_t vlt;
726 #endif
727
728         switch (voltage)
729         {
730         case voltageOff:
731                 vlt = SEC_VOLTAGE_OFF;
732                 break;
733         case voltage13:
734                 vlt = SEC_VOLTAGE_13;
735                 break;
736         case voltage18:
737                 vlt = SEC_VOLTAGE_18;
738                 break;
739         default:
740                 return -ENODEV;
741         }
742 #if HAVE_DVB_API_VERSION < 3
743         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
744 #else
745         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
746 #endif
747 }
748
749 RESULT eDVBFrontend::getState(int &state)
750 {
751         state = m_state;
752         return 0;
753 }
754
755 RESULT eDVBFrontend::setTone(int t)
756 {
757 #if HAVE_DVB_API_VERSION < 3
758         secToneMode_t tone;
759 #else
760         fe_sec_tone_mode_t tone;
761 #endif
762
763         switch (t)
764         {
765         case toneOn:
766                 tone = SEC_TONE_ON;
767                 break;
768         case toneOff:
769                 tone = SEC_TONE_OFF;
770                 break;
771         default:
772                 return -ENODEV;
773         }
774 #if HAVE_DVB_API_VERSION < 3    
775         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
776 #else   
777         return ::ioctl(m_fd, FE_SET_TONE, tone);
778 #endif
779 }
780
781 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
782         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
783 #endif
784
785 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
786 {
787 #if HAVE_DVB_API_VERSION < 3
788         struct secCommand cmd;
789         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
790         cmd.u.diseqc.cmdtype = diseqc.data[0];
791         cmd.u.diseqc.addr = diseqc.data[1];
792         cmd.u.diseqc.cmd = diseqc.data[2];
793         cmd.u.diseqc.numParams = diseqc.len-3;
794         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
795         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
796 #else
797         struct dvb_diseqc_master_cmd cmd;
798         memcpy(cmd.msg, diseqc.data, diseqc.len);
799         cmd.msg_len = diseqc.len;
800         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
801 #endif
802                 return -EINVAL;
803         return 0;
804 }
805
806 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
807         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
808 #endif
809 RESULT eDVBFrontend::sendToneburst(int burst)
810 {
811 #if HAVE_DVB_API_VERSION < 3
812         secMiniCmd cmd = SEC_MINI_NONE;
813         if ( burst == eDVBSatelliteDiseqcParameters::A )
814                 cmd = SEC_MINI_A;
815         else if ( burst == eDVBSatelliteDiseqcParameters::B )
816                 cmd = SEC_MINI_B;
817         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
818                 return -EINVAL;
819 #endif
820         return 0;
821 }
822
823 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
824 {
825         m_sec = sec;
826         return 0;
827 }
828
829 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
830 {
831         m_sec_sequence = list;
832         return 0;
833 }
834
835 RESULT eDVBFrontend::getData(int num, int &data)
836 {
837         if ( num < 7 )
838         {
839                 data = m_data[num];
840                 return 0;
841         }
842         return -EINVAL;
843 }
844
845 RESULT eDVBFrontend::setData(int num, int val)
846 {
847         if ( num < 7 )
848         {
849                 m_data[num] = val;
850                 return 0;
851         }
852         return -EINVAL;
853 }
854