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 #ifndef FP_IOCTL_GET_ID
367 #define FP_IOCTL_GET_ID 0
369 int eDVBFrontend::readInputpower()
372 // if ( eSystemInfo::getInstance()->canMeasureLNBCurrent() )
374 // switch ( eSystemInfo::getInstance()->getHwType() )
376 // case eSystemInfo::DM7000:
377 // case eSystemInfo::DM7020:
379 // open front prozessor
380 int fp=::open("/dev/dbox/fp0", O_RDWR);
383 eDebug("couldn't open fp");
386 static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
387 if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
389 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
396 // eDebug("Inputpower read for platform %d not yet implemented", eSystemInfo::getInstance()->getHwType());
402 bool eDVBFrontend::setSecSequencePos(int steps)
404 eDebug("set sequence pos %d", steps);
409 if (m_sec_sequence.current() != m_sec_sequence.end())
410 ++m_sec_sequence.current();
415 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
416 --m_sec_sequence.current();
422 void eDVBFrontend::tuneLoop() // called by m_tuneTimer
425 if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
427 switch (m_sec_sequence.current()->cmd)
429 case eSecCommand::SLEEP:
430 delay = m_sec_sequence.current()++->msec;
431 eDebug("sleep %dms", delay);
433 case eSecCommand::GOTO:
434 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
435 ++m_sec_sequence.current();
437 case eSecCommand::SET_VOLTAGE:
438 eDebug("setVoltage %d", m_sec_sequence.current()->voltage);
439 setVoltage(m_sec_sequence.current()++->voltage);
441 case eSecCommand::SET_TONE:
442 eDebug("setTone %d", m_sec_sequence.current()->tone);
443 setTone(m_sec_sequence.current()++->tone);
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]);
451 ++m_sec_sequence.current();
453 case eSecCommand::SEND_TONEBURST:
454 eDebug("sendToneburst: %d", m_sec_sequence.current()->toneburst);
455 sendToneburst(m_sec_sequence.current()++->toneburst);
457 case eSecCommand::SET_FRONTEND:
458 eDebug("setFrontend");
460 ++m_sec_sequence.current();
462 case eSecCommand::MEASURE_IDLE_INPUTPOWER:
463 m_idleInputpower = readInputpower();
464 eDebug("idleInputpower is %d", m_idleInputpower);
465 ++m_sec_sequence.current();
467 case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
468 m_runningInputpower = readInputpower();
469 eDebug("runningInputpower is %d", m_runningInputpower);
470 ++m_sec_sequence.current();
472 case eSecCommand::SET_TIMEOUT:
473 m_timeoutCount = m_sec_sequence.current()++->val;
474 eDebug("set timeout %d", m_timeoutCount);
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();
482 case eSecCommand::IF_TIMEOUT_GOTO:
484 setSecSequencePos(m_sec_sequence.current()->steps);
486 ++m_sec_sequence.current();
488 case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
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",
498 if ( (cmd.direction && abs(m_runningInputpower - m_idleInputpower) >= cmd.deltaA)
499 || (!cmd.direction && abs(m_runningInputpower - m_idleInputpower) <= cmd.deltaA) )
502 eDebug("rotor %s step %d ok", txt, cmd.okcount);
503 if ( cmd.okcount > 1 )
505 eDebug("rotor is %s", txt);
506 if (setSecSequencePos(cmd.steps))
512 eDebug("rotor not %s... reset counter.. increase timeout", txt);
516 ++m_sec_sequence.current();
520 ++m_sec_sequence.current();
521 eDebug("unhandled sec command");
523 m_tuneTimer->start(delay,true);
527 void eDVBFrontend::setFrontend()
529 eDebug("setting frontend..\n");
530 if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
532 perror("FE_SET_FRONTEND failed");
536 if (m_state != stateTuning)
539 m_state = stateTuning;
540 m_stateChanged(this);
542 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
545 RESULT eDVBFrontend::getFrontendType(int &t)
553 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
560 m_sec_sequence.clear();
562 eDebug("eDVBFrontend::tune. type: %d", m_type);
569 eDVBFrontendParametersSatellite feparm;
570 if (where.getDVBS(feparm))
572 eDebug("no dvbs data!");
577 eWarning("no SEC module active!");
581 res = m_sec->prepare(*this, parm, feparm);
584 #if HAVE_DVB_API_VERSION < 3
585 eDebug("tuning to %d mhz", parm.Frequency/1000);
587 eDebug("tuning to %d mhz", parm.frequency/1000);
593 eDVBFrontendParametersCable feparm;
594 if (where.getDVBC(feparm))
596 eFatal("cable tuning nyi");
600 eDVBFrontendParametersTerrestrial feparm;
601 if (where.getDVBT(feparm))
603 eDebug("no -T data");
606 #if HAVE_DVB_API_VERSION < 3
607 parm.Frequency = feparm.frequency;
609 parm.frequency = feparm.frequency;
612 switch (feparm.bandwidth)
614 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
615 #if HAVE_DVB_API_VERSION < 3
616 parm.u.ofdm.bandWidth =
618 parm.u.ofdm.bandwidth =
622 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
623 #if HAVE_DVB_API_VERSION < 3
624 parm.u.ofdm.bandWidth =
626 parm.u.ofdm.bandwidth =
630 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
631 #if HAVE_DVB_API_VERSION < 3
632 parm.u.ofdm.bandWidth =
634 parm.u.ofdm.bandwidth =
638 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
639 #if HAVE_DVB_API_VERSION < 3
640 parm.u.ofdm.bandWidth =
642 parm.u.ofdm.bandwidth =
647 eWarning("invalid OFDM bandwith");
651 parm.u.ofdm.code_rate_HP = FEC_AUTO;
652 parm.u.ofdm.code_rate_LP = FEC_AUTO;
654 switch (feparm.modulation)
656 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
657 parm.u.ofdm.constellation = QPSK;
659 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
660 parm.u.ofdm.constellation = QAM_16;
662 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
663 parm.u.ofdm.constellation = QAM_AUTO;
667 switch (feparm.transmission_mode)
669 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
670 #if HAVE_DVB_API_VERSION < 3
671 parm.u.ofdm.TransmissionMode =
673 parm.u.ofdm.transmission_mode =
675 TRANSMISSION_MODE_2K;
677 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
678 #if HAVE_DVB_API_VERSION < 3
679 parm.u.ofdm.TransmissionMode =
681 parm.u.ofdm.transmission_mode =
683 TRANSMISSION_MODE_8K;
685 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
686 #if HAVE_DVB_API_VERSION < 3
687 parm.u.ofdm.TransmissionMode =
689 parm.u.ofdm.transmission_mode =
691 TRANSMISSION_MODE_AUTO;
695 parm.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
696 parm.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
697 #if HAVE_DVB_API_VERSION < 3
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();
714 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
716 connection = new eConnection(this, m_stateChanged.connect(stateChange));
720 RESULT eDVBFrontend::setVoltage(int voltage)
722 #if HAVE_DVB_API_VERSION < 3
725 fe_sec_voltage_t vlt;
731 vlt = SEC_VOLTAGE_OFF;
734 vlt = SEC_VOLTAGE_13;
737 vlt = SEC_VOLTAGE_18;
742 #if HAVE_DVB_API_VERSION < 3
743 return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
745 return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
749 RESULT eDVBFrontend::getState(int &state)
755 RESULT eDVBFrontend::setTone(int t)
757 #if HAVE_DVB_API_VERSION < 3
760 fe_sec_tone_mode_t tone;
774 #if HAVE_DVB_API_VERSION < 3
775 return ::ioctl(m_secfd, SEC_SET_TONE, tone);
777 return ::ioctl(m_fd, FE_SET_TONE, tone);
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 *)
785 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
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))
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))
806 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
807 #define SEC_DISEQC_SEND_BURST _IO('o', 96)
809 RESULT eDVBFrontend::sendToneburst(int burst)
811 #if HAVE_DVB_API_VERSION < 3
812 secMiniCmd cmd = SEC_MINI_NONE;
813 if ( burst == eDVBSatelliteDiseqcParameters::A )
815 else if ( burst == eDVBSatelliteDiseqcParameters::B )
817 if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
823 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
829 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
831 m_sec_sequence = list;
835 RESULT eDVBFrontend::getData(int num, int &data)
845 RESULT eDVBFrontend::setData(int num, int val)