non blocking diseqc and sec stuff
[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         return;
288 }
289
290 eDVBFrontend::~eDVBFrontend()
291 {
292         if (m_fd >= 0)
293                 ::close(m_fd);
294         if (m_sn)
295                 delete m_sn;
296         if (m_timeout)
297                 delete m_timeout;
298 }
299
300 void eDVBFrontend::feEvent(int w)
301 {
302         while (1)
303         {
304 #if HAVE_DVB_API_VERSION < 3
305                 FrontendEvent event;
306 #else
307                 dvb_frontend_event event;
308 #endif
309                 int res;
310                 int state;
311                 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
312                 
313                 if (res && (errno == EAGAIN))
314                         break;
315
316                 if (res)
317                 {
318                         eWarning("FE_GET_EVENT failed! %m");
319                         return;
320                 }
321                 
322                 if (w < 0)
323                         continue;
324
325 #if HAVE_DVB_API_VERSION < 3
326                 if (event.type == FE_COMPLETION_EV)
327 #else
328                 eDebug("fe event: status %x, inversion %s", event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
329                 if (event.status & FE_HAS_LOCK)
330 #endif
331                 {
332                         state = stateLock;
333                 } else
334                 {
335                         if (m_tuning)
336                                 state = stateTuning;
337                         else
338                                 state = stateFailed;
339                 }
340                 if (m_state != state)
341                 {
342                         m_state = state;
343                         m_stateChanged(this);
344                 }
345         }
346 }
347
348 void eDVBFrontend::timeout()
349 {
350         int state;
351         if (m_state == stateTuning)
352         {
353                 state = stateFailed;
354                 eDebug("DVBFrontend: timeout");
355                 if (m_state != state)
356                 {
357                         m_state = state;
358                         m_stateChanged(this);
359                 }
360         } else
361                 m_tuning = 0;
362 }
363
364 void eDVBFrontend::tuneLoop()  // called by m_tuneTimer
365 {
366         int delay=0;
367         if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
368         {
369                 switch (m_sec_sequence.current()->cmd)
370                 {
371                         case eSecCommand::SLEEP:
372                                 delay = m_sec_sequence.current()->msec;
373                                 break;
374                         case eSecCommand::SET_VOLTAGE:
375                                 setVoltage(m_sec_sequence.current()->voltage);
376                                 break;
377                         case eSecCommand::SET_TONE:
378                                 setTone(m_sec_sequence.current()->tone);
379                                 break;
380                         case eSecCommand::SEND_DISEQC:
381                                 sendDiseqc(m_sec_sequence.current()->diseqc);
382                                 break;
383                         case eSecCommand::SEND_TONEBURST:
384                                 sendToneburst(m_sec_sequence.current()->toneburst);
385                                 break;
386                         case eSecCommand::SET_FRONTEND:
387                                 setFrontend();
388                                 break;
389                         case eSecCommand::IF_LOCK_GOTO:
390                         case eSecCommand::IF_NOT_LOCK_GOTO:
391                         default:
392                                 eDebug("unhandled sec command");
393                 }
394                 m_sec_sequence.current()++;
395                 m_tuneTimer->start(delay,true);
396         }
397 }
398
399 void eDVBFrontend::setFrontend()
400 {
401         eDebug("setting frontend..\n");
402         if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
403         {
404                 perror("FE_SET_FRONTEND failed");
405                 return;
406         }
407
408         if (m_state != stateTuning)
409         {
410                 m_tuning = 1;
411                 m_state = stateTuning;
412                 m_stateChanged(this);
413         }
414         m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
415 }
416
417 RESULT eDVBFrontend::getFrontendType(int &t)
418 {
419         if (m_type == -1)
420                 return -ENODEV;
421         t = m_type;
422         return 0;
423 }
424
425 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
426 {
427         if (m_type == -1)
428                 return -ENODEV;
429
430         feEvent(-1);
431
432         m_sec_sequence.clear();
433
434         eDebug("eDVBFrontend::tune. type: %d", m_type);
435
436         switch (m_type)
437         {
438         case feSatellite:
439         {
440                 int res;
441                 eDVBFrontendParametersSatellite feparm;
442                 if (where.getDVBS(feparm))
443                 {
444                         eDebug("no dvbs data!");
445                         return -EINVAL;
446                 }
447                 if (!m_sec)
448                 {
449                         eWarning("no SEC module active!");
450                         return -ENOENT;
451                 }
452                 
453                 res = m_sec->prepare(*this, parm, feparm);
454                 if (res)
455                         return res;
456 #if HAVE_DVB_API_VERSION < 3
457                 eDebug("tuning to %d mhz", parm.Frequency/1000);
458 #else
459                 eDebug("tuning to %d mhz", parm.frequency/1000);
460 #endif
461                 break;
462         }
463         case feCable:
464         {
465                 eDVBFrontendParametersCable feparm;
466                 if (where.getDVBC(feparm))
467                         return -EINVAL;
468                 eFatal("cable tuning nyi");
469         }
470         case feTerrestrial:
471         {
472                 eDVBFrontendParametersTerrestrial feparm;
473                 if (where.getDVBT(feparm))
474                 {
475                         eDebug("no -T data");
476                         return -EINVAL;
477                 }
478 #if HAVE_DVB_API_VERSION < 3
479                 parm.Frequency = feparm.frequency;
480 #else
481                 parm.frequency = feparm.frequency;
482 #endif
483
484                 switch (feparm.bandwidth)
485                 {
486                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
487 #if HAVE_DVB_API_VERSION < 3
488                         parm.u.ofdm.bandWidth =
489 #else
490                         parm.u.ofdm.bandwidth =
491 #endif
492                                 BANDWIDTH_8_MHZ;
493                         break;
494                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
495 #if HAVE_DVB_API_VERSION < 3
496                         parm.u.ofdm.bandWidth =
497 #else
498                         parm.u.ofdm.bandwidth =
499 #endif
500                                 BANDWIDTH_7_MHZ;
501                         break;
502                 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
503 #if HAVE_DVB_API_VERSION < 3
504                         parm.u.ofdm.bandWidth =
505 #else
506                         parm.u.ofdm.bandwidth =
507 #endif
508                                 BANDWIDTH_6_MHZ;
509                         break;
510                 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
511 #if HAVE_DVB_API_VERSION < 3
512                         parm.u.ofdm.bandWidth =
513 #else
514                         parm.u.ofdm.bandwidth =
515 #endif
516                                 BANDWIDTH_AUTO;
517                         break;
518                 default:
519                         eWarning("invalid OFDM bandwith");
520                         return -EINVAL;
521                 }
522                 
523                 parm.u.ofdm.code_rate_HP = FEC_AUTO;
524                 parm.u.ofdm.code_rate_LP = FEC_AUTO;
525                 
526                 switch (feparm.modulation)
527                 {
528                 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
529                         parm.u.ofdm.constellation = QPSK;
530                         break;
531                 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
532                         parm.u.ofdm.constellation = QAM_16;
533                         break;
534                 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
535                         parm.u.ofdm.constellation = QAM_AUTO;
536                         break;
537                 }
538                 
539                 switch (feparm.transmission_mode)
540                 {
541                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
542 #if HAVE_DVB_API_VERSION < 3
543                         parm.u.ofdm.TransmissionMode =
544 #else
545                         parm.u.ofdm.transmission_mode =
546 #endif
547                                 TRANSMISSION_MODE_2K;
548                         break;
549                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
550 #if HAVE_DVB_API_VERSION < 3
551                         parm.u.ofdm.TransmissionMode =
552 #else
553                         parm.u.ofdm.transmission_mode =
554 #endif
555                                 TRANSMISSION_MODE_8K;
556                         break;
557                 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
558 #if HAVE_DVB_API_VERSION < 3
559                         parm.u.ofdm.TransmissionMode =
560 #else
561                         parm.u.ofdm.transmission_mode =
562 #endif
563                                 TRANSMISSION_MODE_AUTO;
564                         break;
565                 }
566                 
567                 parm.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
568                 parm.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
569 #if HAVE_DVB_API_VERSION < 3
570                 parm.Inversion =
571 #else
572                 parm.inversion =
573 #endif
574                         INVERSION_AUTO;
575                 break;
576         }
577         }
578
579         m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND, 0) );
580         m_tuneTimer->start(0,true);
581         m_sec_sequence.current() = m_sec_sequence.begin();
582
583         return 0;
584 }
585
586 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
587 {
588         connection = new eConnection(this, m_stateChanged.connect(stateChange));
589         return 0;
590 }
591
592 RESULT eDVBFrontend::setVoltage(int voltage)
593 {
594 #if HAVE_DVB_API_VERSION < 3
595         secVoltage vlt;
596 #else
597         fe_sec_voltage_t vlt;
598 #endif
599
600         switch (voltage)
601         {
602         case voltageOff:
603                 vlt = SEC_VOLTAGE_OFF;
604                 break;
605         case voltage13:
606                 vlt = SEC_VOLTAGE_13;
607                 break;
608         case voltage18:
609                 vlt = SEC_VOLTAGE_18;
610                 break;
611         default:
612                 return -ENODEV;
613         }
614 #if HAVE_DVB_API_VERSION < 3
615         return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
616 #else
617         return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
618 #endif
619 }
620
621 RESULT eDVBFrontend::getState(int &state)
622 {
623         state = m_state;
624         return 0;
625 }
626
627 RESULT eDVBFrontend::setTone(int t)
628 {
629 #if HAVE_DVB_API_VERSION < 3
630         secToneMode_t tone;
631 #else
632         fe_sec_tone_mode_t tone;
633 #endif
634
635         switch (t)
636         {
637         case toneOn:
638                 tone = SEC_TONE_ON;
639                 break;
640         case toneOff:
641                 tone = SEC_TONE_OFF;
642                 break;
643         default:
644                 return -ENODEV;
645         }
646 #if HAVE_DVB_API_VERSION < 3    
647         return ::ioctl(m_secfd, SEC_SET_TONE, tone);
648 #else   
649         return ::ioctl(m_fd, FE_SET_TONE, tone);
650 #endif
651 }
652
653 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
654         #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
655 #endif
656
657 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
658 {
659 #if HAVE_DVB_API_VERSION < 3
660         struct secCommand cmd;
661         cmd.type = SEC_CMDTYPE_DISEQC_RAW;
662         cmd.u.diseqc.cmdtype = diseqc.data[0];
663         cmd.u.diseqc.addr = diseqc.data[1];
664         cmd.u.diseqc.cmd = diseqc.data[2];
665         cmd.u.diseqc.numParams = diseqc.len-3;
666         memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
667         if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
668 #else
669         struct dvb_diseqc_master_cmd cmd;
670         memcpy(cmd.msg, diseqc.data, diseqc.len);
671         cmd.msg_len = diseqc.len;
672         if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
673 #endif
674                 return -EINVAL;
675         return 0;
676 }
677
678 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
679         #define SEC_DISEQC_SEND_BURST _IO('o', 96)
680 #endif
681 RESULT eDVBFrontend::sendToneburst(int burst)
682 {
683 #if HAVE_DVB_API_VERSION < 3
684         secMiniCmd cmd = SEC_MINI_NONE;
685         if ( burst == eDVBSatelliteDiseqcParameters::A )
686                 cmd = SEC_MINI_A;
687         else if ( burst == eDVBSatelliteDiseqcParameters::B )
688                 cmd = SEC_MINI_B;
689         if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
690                 return -EINVAL;
691 #endif
692         return 0;
693 }
694
695 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
696 {
697         m_sec = sec;
698         return 0;
699 }
700
701 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
702 {
703         m_sec_sequence = list;
704         return 0;
705 }