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);
287 memset(m_data, 0xFFFF, sizeof(m_data));
292 eDVBFrontend::~eDVBFrontend()
302 void eDVBFrontend::feEvent(int w)
306 #if HAVE_DVB_API_VERSION < 3
309 dvb_frontend_event event;
313 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
315 if (res && (errno == EAGAIN))
320 eWarning("FE_GET_EVENT failed! %m");
327 #if HAVE_DVB_API_VERSION < 3
328 if (event.type == FE_COMPLETION_EV)
330 eDebug("fe event: status %x, inversion %s", event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
331 if (event.status & FE_HAS_LOCK)
342 if (m_state != state)
345 m_stateChanged(this);
350 void eDVBFrontend::timeout()
353 if (m_state == stateTuning)
356 eDebug("DVBFrontend: timeout");
357 if (m_state != state)
360 m_stateChanged(this);
366 void eDVBFrontend::tuneLoop() // called by m_tuneTimer
369 if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
371 switch (m_sec_sequence.current()->cmd)
373 case eSecCommand::SLEEP:
374 delay = m_sec_sequence.current()->msec;
376 case eSecCommand::SET_VOLTAGE:
377 setVoltage(m_sec_sequence.current()->voltage);
379 case eSecCommand::SET_TONE:
380 setTone(m_sec_sequence.current()->tone);
382 case eSecCommand::SEND_DISEQC:
383 sendDiseqc(m_sec_sequence.current()->diseqc);
385 case eSecCommand::SEND_TONEBURST:
386 sendToneburst(m_sec_sequence.current()->toneburst);
388 case eSecCommand::SET_FRONTEND:
391 case eSecCommand::IF_LOCK_GOTO:
392 case eSecCommand::IF_NOT_LOCK_GOTO:
394 eDebug("unhandled sec command");
396 m_sec_sequence.current()++;
397 m_tuneTimer->start(delay,true);
401 void eDVBFrontend::setFrontend()
403 eDebug("setting frontend..\n");
404 if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
406 perror("FE_SET_FRONTEND failed");
410 if (m_state != stateTuning)
413 m_state = stateTuning;
414 m_stateChanged(this);
416 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
419 RESULT eDVBFrontend::getFrontendType(int &t)
427 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
434 m_sec_sequence.clear();
436 eDebug("eDVBFrontend::tune. type: %d", m_type);
443 eDVBFrontendParametersSatellite feparm;
444 if (where.getDVBS(feparm))
446 eDebug("no dvbs data!");
451 eWarning("no SEC module active!");
455 res = m_sec->prepare(*this, parm, feparm);
458 #if HAVE_DVB_API_VERSION < 3
459 eDebug("tuning to %d mhz", parm.Frequency/1000);
461 eDebug("tuning to %d mhz", parm.frequency/1000);
467 eDVBFrontendParametersCable feparm;
468 if (where.getDVBC(feparm))
470 eFatal("cable tuning nyi");
474 eDVBFrontendParametersTerrestrial feparm;
475 if (where.getDVBT(feparm))
477 eDebug("no -T data");
480 #if HAVE_DVB_API_VERSION < 3
481 parm.Frequency = feparm.frequency;
483 parm.frequency = feparm.frequency;
486 switch (feparm.bandwidth)
488 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
489 #if HAVE_DVB_API_VERSION < 3
490 parm.u.ofdm.bandWidth =
492 parm.u.ofdm.bandwidth =
496 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
497 #if HAVE_DVB_API_VERSION < 3
498 parm.u.ofdm.bandWidth =
500 parm.u.ofdm.bandwidth =
504 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
505 #if HAVE_DVB_API_VERSION < 3
506 parm.u.ofdm.bandWidth =
508 parm.u.ofdm.bandwidth =
512 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
513 #if HAVE_DVB_API_VERSION < 3
514 parm.u.ofdm.bandWidth =
516 parm.u.ofdm.bandwidth =
521 eWarning("invalid OFDM bandwith");
525 parm.u.ofdm.code_rate_HP = FEC_AUTO;
526 parm.u.ofdm.code_rate_LP = FEC_AUTO;
528 switch (feparm.modulation)
530 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
531 parm.u.ofdm.constellation = QPSK;
533 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
534 parm.u.ofdm.constellation = QAM_16;
536 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
537 parm.u.ofdm.constellation = QAM_AUTO;
541 switch (feparm.transmission_mode)
543 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
544 #if HAVE_DVB_API_VERSION < 3
545 parm.u.ofdm.TransmissionMode =
547 parm.u.ofdm.transmission_mode =
549 TRANSMISSION_MODE_2K;
551 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
552 #if HAVE_DVB_API_VERSION < 3
553 parm.u.ofdm.TransmissionMode =
555 parm.u.ofdm.transmission_mode =
557 TRANSMISSION_MODE_8K;
559 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
560 #if HAVE_DVB_API_VERSION < 3
561 parm.u.ofdm.TransmissionMode =
563 parm.u.ofdm.transmission_mode =
565 TRANSMISSION_MODE_AUTO;
569 parm.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
570 parm.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
571 #if HAVE_DVB_API_VERSION < 3
581 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND, 0) );
582 m_tuneTimer->start(0,true);
583 m_sec_sequence.current() = m_sec_sequence.begin();
588 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
590 connection = new eConnection(this, m_stateChanged.connect(stateChange));
594 RESULT eDVBFrontend::setVoltage(int voltage)
596 #if HAVE_DVB_API_VERSION < 3
599 fe_sec_voltage_t vlt;
605 vlt = SEC_VOLTAGE_OFF;
608 vlt = SEC_VOLTAGE_13;
611 vlt = SEC_VOLTAGE_18;
616 #if HAVE_DVB_API_VERSION < 3
617 return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
619 return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
623 RESULT eDVBFrontend::getState(int &state)
629 RESULT eDVBFrontend::setTone(int t)
631 #if HAVE_DVB_API_VERSION < 3
634 fe_sec_tone_mode_t tone;
648 #if HAVE_DVB_API_VERSION < 3
649 return ::ioctl(m_secfd, SEC_SET_TONE, tone);
651 return ::ioctl(m_fd, FE_SET_TONE, tone);
655 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
656 #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
659 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
661 eDebug("send %02x %02x %02x %02x",
662 diseqc.data[0], diseqc.data[1], diseqc.data[2], diseqc.data[3]);
663 #if HAVE_DVB_API_VERSION < 3
664 struct secCommand cmd;
665 cmd.type = SEC_CMDTYPE_DISEQC_RAW;
666 cmd.u.diseqc.cmdtype = diseqc.data[0];
667 cmd.u.diseqc.addr = diseqc.data[1];
668 cmd.u.diseqc.cmd = diseqc.data[2];
669 cmd.u.diseqc.numParams = diseqc.len-3;
670 memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
671 if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
673 struct dvb_diseqc_master_cmd cmd;
674 memcpy(cmd.msg, diseqc.data, diseqc.len);
675 cmd.msg_len = diseqc.len;
676 if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
682 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
683 #define SEC_DISEQC_SEND_BURST _IO('o', 96)
685 RESULT eDVBFrontend::sendToneburst(int burst)
687 #if HAVE_DVB_API_VERSION < 3
688 secMiniCmd cmd = SEC_MINI_NONE;
689 if ( burst == eDVBSatelliteDiseqcParameters::A )
691 else if ( burst == eDVBSatelliteDiseqcParameters::B )
693 if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
699 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
705 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
707 m_sec_sequence = list;
711 RESULT eDVBFrontend::getData(int num, int &data)
721 RESULT eDVBFrontend::setData(int num, int val)