2 #include <lib/dvb/dvb.h>
3 #include <lib/base/eerror.h>
9 #if HAVE_DVB_API_VERSION < 3
10 #include <ost/frontend.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
23 #include <linux/dvb/frontend.h>
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>
30 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
32 frequency = descriptor.getFrequency() * 10;
33 symbol_rate = descriptor.getSymbolRate() * 100;
34 switch (descriptor.getPolarization())
37 polarisation = Polarisation::Horizontal;
40 polarisation = Polarisation::Vertical;
43 polarisation = Polarisation::CircularLeft;
46 polarisation = Polarisation::CircularRight;
49 switch (descriptor.getFecInner())
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;
82 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
87 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor &)
92 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
96 DEFINE_REF(eDVBFrontendParameters);
98 RESULT eDVBFrontendParameters::getSystem(int &t) const
106 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
108 if (m_type != iDVBFrontend::feSatellite)
114 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
116 if (m_type != iDVBFrontend::feCable)
122 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
124 if (m_type != iDVBFrontend::feTerrestrial)
130 RESULT eDVBFrontendParameters::setDVBS(eDVBFrontendParametersSatellite &p)
133 m_type = iDVBFrontend::feSatellite;
137 RESULT eDVBFrontendParameters::setDVBC(eDVBFrontendParametersCable &p)
140 m_type = iDVBFrontend::feCable;
144 RESULT eDVBFrontendParameters::setDVBT(eDVBFrontendParametersTerrestrial &p)
147 m_type = iDVBFrontend::feTerrestrial;
151 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff) const
156 if (parm->getSystem(type))
160 diff = 1<<30; // big difference
166 case iDVBFrontend::feSatellite:
168 eDVBFrontendParametersSatellite osat;
169 if (parm->getDVBS(osat))
172 if (sat.orbital_position != osat.orbital_position)
174 else if (sat.polarisation != osat.polarisation)
177 diff = abs(sat.frequency - osat.frequency);
180 case iDVBFrontend::feCable:
181 case iDVBFrontend::feTerrestrial:
188 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
192 case iDVBFrontend::feSatellite:
194 hash = sat.frequency & 0xFFFF;
195 hash |= sat.orbital_position << 16;
198 case iDVBFrontend::feCable:
199 case iDVBFrontend::feTerrestrial:
205 DEFINE_REF(eDVBFrontend);
207 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok): m_type(-1)
209 #if HAVE_DVB_API_VERSION < 3
210 char sec_filename[128];
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);
224 eWarning("failed! (%s) %m", sec_filename);
229 eDebug("m_secfd is %d", m_secfd);
231 FrontendInfo fe_info;
232 sprintf(filename, "/dev/dvb/card%d/frontend%d", adap, fe);
234 dvb_frontend_info fe_info;
235 sprintf(filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
237 eDebug("opening frontend.");
238 m_fd = ::open(filename, O_RDWR|O_NONBLOCK);
241 eWarning("failed! (%s) %m", filename);
246 result = ::ioctl(m_fd, FE_GET_INFO, &fe_info);
249 eWarning("ioctl FE_GET_INFO failed");
256 switch (fe_info.type)
259 m_type = feSatellite;
265 m_type = feTerrestrial;
268 eWarning("unknown frontend type.");
274 eDebug("detected %s frontend", "satellite\0cable\0 terrestrial"+fe_info.type*9);
277 m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
278 CONNECT(m_sn->activated, eDVBFrontend::feEvent);
281 m_timeout = new eTimer(eApp);
282 CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
284 m_tuneTimer = new eTimer(eApp);
285 CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
290 eDVBFrontend::~eDVBFrontend()
300 void eDVBFrontend::feEvent(int w)
304 #if HAVE_DVB_API_VERSION < 3
307 dvb_frontend_event event;
311 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
313 if (res && (errno == EAGAIN))
318 eWarning("FE_GET_EVENT failed! %m");
325 #if HAVE_DVB_API_VERSION < 3
326 if (event.type == FE_COMPLETION_EV)
328 eDebug("fe event: status %x, inversion %s", event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
329 if (event.status & FE_HAS_LOCK)
340 if (m_state != state)
343 m_stateChanged(this);
348 void eDVBFrontend::timeout()
351 if (m_state == stateTuning)
354 eDebug("DVBFrontend: timeout");
355 if (m_state != state)
358 m_stateChanged(this);
364 void eDVBFrontend::tuneLoop() // called by m_tuneTimer
367 if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
369 switch (m_sec_sequence.current()->cmd)
371 case eSecCommand::SLEEP:
372 delay = m_sec_sequence.current()->msec;
374 case eSecCommand::SET_VOLTAGE:
375 setVoltage(m_sec_sequence.current()->voltage);
377 case eSecCommand::SET_TONE:
378 setTone(m_sec_sequence.current()->tone);
380 case eSecCommand::SEND_DISEQC:
381 sendDiseqc(m_sec_sequence.current()->diseqc);
383 case eSecCommand::SEND_TONEBURST:
384 sendToneburst(m_sec_sequence.current()->toneburst);
386 case eSecCommand::SET_FRONTEND:
389 case eSecCommand::IF_LOCK_GOTO:
390 case eSecCommand::IF_NOT_LOCK_GOTO:
392 eDebug("unhandled sec command");
394 m_sec_sequence.current()++;
395 m_tuneTimer->start(delay,true);
399 void eDVBFrontend::setFrontend()
401 eDebug("setting frontend..\n");
402 if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
404 perror("FE_SET_FRONTEND failed");
408 if (m_state != stateTuning)
411 m_state = stateTuning;
412 m_stateChanged(this);
414 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
417 RESULT eDVBFrontend::getFrontendType(int &t)
425 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
432 m_sec_sequence.clear();
434 eDebug("eDVBFrontend::tune. type: %d", m_type);
441 eDVBFrontendParametersSatellite feparm;
442 if (where.getDVBS(feparm))
444 eDebug("no dvbs data!");
449 eWarning("no SEC module active!");
453 res = m_sec->prepare(*this, parm, feparm);
456 #if HAVE_DVB_API_VERSION < 3
457 eDebug("tuning to %d mhz", parm.Frequency/1000);
459 eDebug("tuning to %d mhz", parm.frequency/1000);
465 eDVBFrontendParametersCable feparm;
466 if (where.getDVBC(feparm))
468 eFatal("cable tuning nyi");
472 eDVBFrontendParametersTerrestrial feparm;
473 if (where.getDVBT(feparm))
475 eDebug("no -T data");
478 #if HAVE_DVB_API_VERSION < 3
479 parm.Frequency = feparm.frequency;
481 parm.frequency = feparm.frequency;
484 switch (feparm.bandwidth)
486 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
487 #if HAVE_DVB_API_VERSION < 3
488 parm.u.ofdm.bandWidth =
490 parm.u.ofdm.bandwidth =
494 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
495 #if HAVE_DVB_API_VERSION < 3
496 parm.u.ofdm.bandWidth =
498 parm.u.ofdm.bandwidth =
502 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
503 #if HAVE_DVB_API_VERSION < 3
504 parm.u.ofdm.bandWidth =
506 parm.u.ofdm.bandwidth =
510 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
511 #if HAVE_DVB_API_VERSION < 3
512 parm.u.ofdm.bandWidth =
514 parm.u.ofdm.bandwidth =
519 eWarning("invalid OFDM bandwith");
523 parm.u.ofdm.code_rate_HP = FEC_AUTO;
524 parm.u.ofdm.code_rate_LP = FEC_AUTO;
526 switch (feparm.modulation)
528 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
529 parm.u.ofdm.constellation = QPSK;
531 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
532 parm.u.ofdm.constellation = QAM_16;
534 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
535 parm.u.ofdm.constellation = QAM_AUTO;
539 switch (feparm.transmission_mode)
541 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
542 #if HAVE_DVB_API_VERSION < 3
543 parm.u.ofdm.TransmissionMode =
545 parm.u.ofdm.transmission_mode =
547 TRANSMISSION_MODE_2K;
549 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
550 #if HAVE_DVB_API_VERSION < 3
551 parm.u.ofdm.TransmissionMode =
553 parm.u.ofdm.transmission_mode =
555 TRANSMISSION_MODE_8K;
557 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
558 #if HAVE_DVB_API_VERSION < 3
559 parm.u.ofdm.TransmissionMode =
561 parm.u.ofdm.transmission_mode =
563 TRANSMISSION_MODE_AUTO;
567 parm.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
568 parm.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
569 #if HAVE_DVB_API_VERSION < 3
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();
586 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
588 connection = new eConnection(this, m_stateChanged.connect(stateChange));
592 RESULT eDVBFrontend::setVoltage(int voltage)
594 #if HAVE_DVB_API_VERSION < 3
597 fe_sec_voltage_t vlt;
603 vlt = SEC_VOLTAGE_OFF;
606 vlt = SEC_VOLTAGE_13;
609 vlt = SEC_VOLTAGE_18;
614 #if HAVE_DVB_API_VERSION < 3
615 return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
617 return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
621 RESULT eDVBFrontend::getState(int &state)
627 RESULT eDVBFrontend::setTone(int t)
629 #if HAVE_DVB_API_VERSION < 3
632 fe_sec_tone_mode_t tone;
646 #if HAVE_DVB_API_VERSION < 3
647 return ::ioctl(m_secfd, SEC_SET_TONE, tone);
649 return ::ioctl(m_fd, FE_SET_TONE, tone);
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 *)
657 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
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))
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))
678 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
679 #define SEC_DISEQC_SEND_BURST _IO('o', 96)
681 RESULT eDVBFrontend::sendToneburst(int burst)
683 #if HAVE_DVB_API_VERSION < 3
684 secMiniCmd cmd = SEC_MINI_NONE;
685 if ( burst == eDVBSatelliteDiseqcParameters::A )
687 else if ( burst == eDVBSatelliteDiseqcParameters::B )
689 if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
695 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
701 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
703 m_sec_sequence = list;