1 #include <lib/dvb/dvb.h>
2 #include <lib/base/eerror.h>
8 #ifndef I2C_SLAVE_FORCE
9 #define I2C_SLAVE_FORCE 0x0706
12 #if HAVE_DVB_API_VERSION < 3
13 #include <ost/frontend.h>
15 #define QAM_AUTO (Modulation)6
16 #define TRANSMISSION_MODE_AUTO (TransmitMode)2
17 #define BANDWIDTH_AUTO (BandWidth)3
18 #define GUARD_INTERVAL_AUTO (GuardInterval)4
19 #define HIERARCHY_AUTO (Hierarchy)4
20 #define parm_frequency parm.Frequency
21 #define parm_inversion parm.Inversion
22 #define parm_u_qpsk_symbol_rate parm.u.qpsk.SymbolRate
23 #define parm_u_qpsk_fec_inner parm.u.qpsk.FEC_inner
24 #define parm_u_qam_symbol_rate parm.u.qam.SymbolRate
25 #define parm_u_qam_fec_inner parm.u.qam.FEC_inner
26 #define parm_u_qam_modulation parm.u.qam.QAM
27 #define parm_u_ofdm_bandwidth parm.u.ofdm.bandWidth
28 #define parm_u_ofdm_code_rate_LP parm.u.ofdm.LP_CodeRate
29 #define parm_u_ofdm_code_rate_HP parm.u.ofdm.HP_CodeRate
30 #define parm_u_ofdm_constellation parm.u.ofdm.Constellation
31 #define parm_u_ofdm_transmission_mode parm.u.ofdm.TransmissionMode
32 #define parm_u_ofdm_guard_interval parm.u.ofdm.guardInterval
33 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.HierarchyInformation
35 #include <linux/dvb/frontend.h>
36 #define parm_frequency parm.frequency
37 #define parm_inversion parm.inversion
38 #define parm_u_qpsk_symbol_rate parm.u.qpsk.symbol_rate
39 #define parm_u_qpsk_fec_inner parm.u.qpsk.fec_inner
40 #define parm_u_qam_symbol_rate parm.u.qam.symbol_rate
41 #define parm_u_qam_fec_inner parm.u.qam.fec_inner
42 #define parm_u_qam_modulation parm.u.qam.modulation
43 #define parm_u_ofdm_bandwidth parm.u.ofdm.bandwidth
44 #define parm_u_ofdm_code_rate_LP parm.u.ofdm.code_rate_LP
45 #define parm_u_ofdm_code_rate_HP parm.u.ofdm.code_rate_HP
46 #define parm_u_ofdm_constellation parm.u.ofdm.constellation
47 #define parm_u_ofdm_transmission_mode parm.u.ofdm.transmission_mode
48 #define parm_u_ofdm_guard_interval parm.u.ofdm.guard_interval
49 #define parm_u_ofdm_hierarchy_information parm.u.ofdm.hierarchy_information
52 #include <dvbsi++/satellite_delivery_system_descriptor.h>
53 #include <dvbsi++/cable_delivery_system_descriptor.h>
54 #include <dvbsi++/terrestrial_delivery_system_descriptor.h>
56 void eDVBDiseqcCommand::setCommandString(const char *str)
61 if (len > MAX_DISEQC_LENGTH)
62 len = MAX_DISEQC_LENGTH;
63 memcpy(data, str, len);
66 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
68 frequency = descriptor.getFrequency() * 10;
69 symbol_rate = descriptor.getSymbolRate() * 100;
70 polarisation = descriptor.getPolarization();
71 fec = descriptor.getFecInner();
74 inversion = Inversion::Unknown;
75 orbital_position = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
76 orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
77 orbital_position += ((descriptor.getOrbitalPosition() >> 4) & 0xF) * 10;
78 orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
79 if (orbital_position && (!descriptor.getWestEastFlag()))
80 orbital_position = 3600 - orbital_position;
81 eDebug("SAT freq %d, %s, pos %d, sr %d, fec %d",
83 polarisation ? "hor" : "vert",
88 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
90 frequency = descriptor.getFrequency() / 10;
91 symbol_rate = descriptor.getSymbolRate() * 100;
92 fec_inner = descriptor.getFecInner();
93 if ( fec_inner == 0xF )
94 fec_inner = FEC::fNone;
95 modulation = descriptor.getModulation();
96 if ( modulation > 0x5 )
97 modulation = Modulation::Auto;
98 inversion = Inversion::Unknown;
99 eDebug("Cable freq %d, mod %d, sr %d, fec %d",
101 modulation, symbol_rate, fec_inner);
104 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor &descriptor)
106 frequency = descriptor.getCentreFrequency() * 10;
107 bandwidth = descriptor.getBandwidth();
108 if ( bandwidth > 2 ) // 5Mhz forced to auto
109 bandwidth = Bandwidth::BwAuto;
110 code_rate_HP = descriptor.getCodeRateHpStream();
111 if (code_rate_HP > 4)
112 code_rate_HP = FEC::fAuto;
113 code_rate_LP = descriptor.getCodeRateLpStream();
114 if (code_rate_LP > 4)
115 code_rate_LP = FEC::fAuto;
116 transmission_mode = descriptor.getTransmissionMode();
117 if (transmission_mode > 2)
118 transmission_mode = TransmissionMode::TMAuto;
119 guard_interval = descriptor.getGuardInterval();
120 if (guard_interval > 3)
121 guard_interval = GuardInterval::GI_Auto;
122 hierarchy = descriptor.getHierarchyInformation()&3;
123 modulation = descriptor.getConstellation();
125 modulation = Modulation::Auto;
126 inversion = Inversion::Unknown;
127 eDebug("Terr freq %d, bw %d, cr_hp %d, cr_lp %d, tm_mode %d, guard %d, hierarchy %d, const %d",
128 frequency, bandwidth, code_rate_HP, code_rate_LP, transmission_mode,
129 guard_interval, hierarchy, modulation);
132 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
136 DEFINE_REF(eDVBFrontendParameters);
138 RESULT eDVBFrontendParameters::getSystem(int &t) const
146 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
148 if (m_type != iDVBFrontend::feSatellite)
154 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
156 if (m_type != iDVBFrontend::feCable)
162 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
164 if (m_type != iDVBFrontend::feTerrestrial)
170 RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p)
173 m_type = iDVBFrontend::feSatellite;
177 RESULT eDVBFrontendParameters::setDVBC(const eDVBFrontendParametersCable &p)
180 m_type = iDVBFrontend::feCable;
184 RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &p)
187 m_type = iDVBFrontend::feTerrestrial;
191 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff) const
196 if (parm->getSystem(type))
200 diff = 1<<30; // big difference
206 case iDVBFrontend::feSatellite:
208 eDVBFrontendParametersSatellite osat;
209 if (parm->getDVBS(osat))
212 if (sat.orbital_position != osat.orbital_position)
214 else if (sat.polarisation != osat.polarisation)
218 diff = abs(sat.frequency - osat.frequency);
219 diff += abs(sat.symbol_rate - osat.symbol_rate);
223 case iDVBFrontend::feCable:
224 eDVBFrontendParametersCable ocable;
225 if (parm->getDVBC(ocable))
228 if (cable.modulation != ocable.modulation && cable.modulation != eDVBFrontendParametersCable::Modulation::Auto && ocable.modulation != eDVBFrontendParametersCable::Modulation::Auto)
230 else if (cable.inversion != ocable.inversion && cable.inversion != eDVBFrontendParametersCable::Inversion::Unknown && ocable.inversion != eDVBFrontendParametersCable::Inversion::Unknown)
234 diff = abs(cable.frequency - ocable.frequency);
235 diff += abs(cable.symbol_rate - ocable.symbol_rate);
239 case iDVBFrontend::feTerrestrial:
240 eDVBFrontendParametersTerrestrial oterrestrial;
241 if (parm->getDVBT(oterrestrial))
244 diff = abs(terrestrial.frequency - oterrestrial.frequency);
253 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
257 case iDVBFrontend::feSatellite:
259 hash = (sat.orbital_position << 16);
260 hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
263 case iDVBFrontend::feCable:
264 case iDVBFrontend::feTerrestrial:
270 DEFINE_REF(eDVBFrontend);
272 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
273 :m_type(-1), m_fe(fe), m_fd(-1), m_timeout(0), m_tuneTimer(0)
274 #if HAVE_DVB_API_VERSION < 3
278 #if HAVE_DVB_API_VERSION < 3
279 sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
280 sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
282 sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
284 m_timeout = new eTimer(eApp);
285 CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
287 m_tuneTimer = new eTimer(eApp);
288 CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
290 int entries = sizeof(m_data) / sizeof(int);
291 for (int i=0; i<entries; ++i)
294 m_idleInputpower[0]=m_idleInputpower[1]=0;
296 ok = !openFrontend();
300 int eDVBFrontend::openFrontend()
303 return -1; // already opened
308 #if HAVE_DVB_API_VERSION < 3
309 m_secfd = ::open(m_sec_filename, O_RDWR);
312 eWarning("failed! (%s) %m", m_sec_filename);
315 FrontendInfo fe_info;
317 dvb_frontend_info fe_info;
319 eDebug("opening frontend %d", m_fe);
320 m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
323 eWarning("failed! (%s) %m", m_filename);
324 #if HAVE_DVB_API_VERSION < 3
333 if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
335 eWarning("ioctl FE_GET_INFO failed");
338 #if HAVE_DVB_API_VERSION < 3
345 switch (fe_info.type)
348 m_type = iDVBFrontend::feSatellite;
351 m_type = iDVBFrontend::feCable;
354 m_type = iDVBFrontend::feTerrestrial;
357 eWarning("unknown frontend type.");
360 #if HAVE_DVB_API_VERSION < 3
366 eDebug("detected %s frontend", "satellite\0cable\0 terrestrial"+fe_info.type*10);
369 setTone(iDVBFrontend::toneOff);
370 setVoltage(iDVBFrontend::voltageOff);
372 m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
373 CONNECT(m_sn->activated, eDVBFrontend::feEvent);
379 int eDVBFrontend::closeFrontend()
381 if (!m_fe && m_data[7] != -1)
383 // try to close the first frontend.. but the second is linked to the first
384 eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[7];
385 if (linked_fe->m_inuse)
387 eDebug("dont close frontend %d until the linked frontend %d is still in use",
388 m_fe, linked_fe->m_frontend->getID());
394 eDebug("close frontend %d", m_fe);
395 setTone(iDVBFrontend::toneOff);
396 setVoltage(iDVBFrontend::voltageOff);
399 m_data[0] = m_data[1] = m_data[2] = -1;
401 #if HAVE_DVB_API_VERSION < 3
414 eDVBFrontend::~eDVBFrontend()
421 void eDVBFrontend::feEvent(int w)
425 #if HAVE_DVB_API_VERSION < 3
428 dvb_frontend_event event;
432 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
434 if (res && (errno == EAGAIN))
439 eWarning("FE_GET_EVENT failed! %m");
446 #if HAVE_DVB_API_VERSION < 3
447 if (event.type == FE_COMPLETION_EV)
449 eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
450 if (event.status & FE_HAS_LOCK)
460 state = stateLostLock;
461 m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
464 if (m_state != state)
467 m_stateChanged(this);
472 void eDVBFrontend::timeout()
475 if (m_state == stateTuning)
477 m_state = stateFailed;
478 m_stateChanged(this);
482 int eDVBFrontend::readFrontendData(int type)
489 if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
490 eDebug("FE_READ_BER failed (%m)");
496 if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
497 eDebug("FE_READ_SNR failed (%m)");
503 if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
504 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
509 #if HAVE_DVB_API_VERSION < 3
510 FrontendStatus status=0;
514 if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
515 eDebug("FE_READ_STATUS failed (%m)");
516 return !!(status&FE_HAS_LOCK);
520 #if HAVE_DVB_API_VERSION < 3
521 FrontendStatus status=0;
525 if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
526 eDebug("FE_READ_STATUS failed (%m)");
527 return !!(status&FE_HAS_SYNC);
533 void PutToDict(PyObject *dict, const char*key, long value)
535 PyObject *item = PyInt_FromLong(value);
538 if (PyDict_SetItemString(dict, key, item))
539 eDebug("put %s to dict failed", key);
543 eDebug("could not create PyObject for %s", key);
546 void PutToDict(PyObject *dict, const char*key, const char *value)
548 PyObject *item = PyString_FromString(value);
551 if (PyDict_SetItemString(dict, key, item))
552 eDebug("put %s to dict failed", key);
556 eDebug("could not create PyObject for %s", key);
559 void fillDictWithSatelliteData(PyObject *dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
565 fe->getData(9, freq_offset);
566 int frequency = parm_frequency + freq_offset;
567 PutToDict(dict, "frequency", frequency);
568 PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
570 switch(parm_u_qpsk_fec_inner)
592 PutToDict(dict, "fec_inner", fec);
595 void fillDictWithCableData(PyObject *dict, const FRONTENDPARAMETERS &parm)
598 PutToDict(dict, "frequency", parm_frequency/1000);
599 PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
600 switch(parm_u_qam_fec_inner)
620 #if HAVE_DVB_API_VERSION >= 3
630 PutToDict(dict, "fec_inner", tmp);
631 switch(parm_u_qam_modulation)
653 PutToDict(dict, "modulation", tmp);
656 void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
659 PutToDict(dict, "frequency", parm_frequency);
660 switch (parm_u_ofdm_bandwidth)
662 case BANDWIDTH_8_MHZ:
663 tmp = "BANDWIDTH_8_MHZ";
665 case BANDWIDTH_7_MHZ:
666 tmp = "BANDWIDTH_7_MHZ";
668 case BANDWIDTH_6_MHZ:
669 tmp = "BANDWIDTH_6_MHZ";
673 tmp = "BANDWIDTH_AUTO";
676 PutToDict(dict, "bandwidth", tmp);
677 switch (parm_u_ofdm_code_rate_LP)
699 PutToDict(dict, "code_rate_lp", tmp);
700 switch (parm_u_ofdm_code_rate_HP)
722 PutToDict(dict, "code_rate_hp", tmp);
723 switch (parm_u_ofdm_constellation)
736 PutToDict(dict, "constellation", tmp);
737 switch (parm_u_ofdm_transmission_mode)
739 case TRANSMISSION_MODE_2K:
740 tmp = "TRANSMISSION_MODE_2K";
742 case TRANSMISSION_MODE_8K:
743 tmp = "TRANSMISSION_MODE_8K";
746 case TRANSMISSION_MODE_AUTO:
747 tmp = "TRANSMISSION_MODE_AUTO";
750 PutToDict(dict, "transmission_mode", tmp);
751 switch (parm_u_ofdm_guard_interval)
753 case GUARD_INTERVAL_1_32:
754 tmp = "GUARD_INTERVAL_1_32";
756 case GUARD_INTERVAL_1_16:
757 tmp = "GUARD_INTERVAL_1_16";
759 case GUARD_INTERVAL_1_8:
760 tmp = "GUARD_INTERVAL_1_8";
762 case GUARD_INTERVAL_1_4:
763 tmp = "GUARD_INTERVAL_1_4";
766 case GUARD_INTERVAL_AUTO:
767 tmp = "GUARD_INTERVAL_AUTO";
770 PutToDict(dict, "guard_interval", tmp);
771 switch (parm_u_ofdm_hierarchy_information)
784 tmp = "HIERARCHY_AUTO";
787 PutToDict(dict, "hierarchy_information", tmp);
790 PyObject *eDVBFrontend::readTransponderData(bool original)
792 PyObject *ret=PyDict_New();
796 bool read=m_fd != -1;
799 PutToDict(ret, "tuner_number", m_fe);
817 PutToDict(ret, "tuner_type", tmp);
821 FRONTENDPARAMETERS front;
844 PutToDict(ret, "tuner_state", tmp);
846 PutToDict(ret, "tuner_locked", readFrontendData(Locked));
847 PutToDict(ret, "tuner_synced", readFrontendData(Synced));
848 PutToDict(ret, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
849 PutToDict(ret, "tuner_signal_power", readFrontendData(signalPower));
850 PutToDict(ret, "tuner_signal_quality", readFrontendData(signalQuality));
852 if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
853 eDebug("FE_GET_FRONTEND (%m)");
856 tmp = "INVERSION_AUTO";
857 switch(parm_inversion)
860 tmp = "INVERSION_ON";
863 tmp = "INVERSION_OFF";
869 PutToDict(ret, "inversion", tmp);
874 fillDictWithSatelliteData(ret, original?parm:front, this);
877 fillDictWithCableData(ret, original?parm:front);
880 fillDictWithTerrestrialData(ret, original?parm:front);
894 #ifndef FP_IOCTL_GET_ID
895 #define FP_IOCTL_GET_ID 0
897 int eDVBFrontend::readInputpower()
899 int power=m_fe; // this is needed for read inputpower from the correct tuner !
901 // open front prozessor
902 int fp=::open("/dev/dbox/fp0", O_RDWR);
905 eDebug("couldn't open fp");
908 static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
909 if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
911 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
919 bool eDVBFrontend::setSecSequencePos(int steps)
921 eDebug("set sequence pos %d", steps);
926 if (m_sec_sequence.current() != m_sec_sequence.end())
927 ++m_sec_sequence.current();
932 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
933 --m_sec_sequence.current();
939 void eDVBFrontend::tuneLoop() // called by m_tuneTimer
942 if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
944 // eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
945 switch (m_sec_sequence.current()->cmd)
947 case eSecCommand::SLEEP:
948 delay = m_sec_sequence.current()++->msec;
949 eDebug("[SEC] sleep %dms", delay);
951 case eSecCommand::GOTO:
952 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
953 ++m_sec_sequence.current();
955 case eSecCommand::SET_VOLTAGE:
957 int voltage = m_sec_sequence.current()++->voltage;
958 eDebug("[SEC] setVoltage %d", voltage);
962 case eSecCommand::IF_VOLTAGE_GOTO:
964 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
965 if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
967 ++m_sec_sequence.current();
970 case eSecCommand::IF_NOT_VOLTAGE_GOTO:
972 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
973 if ( compare.voltage != m_curVoltage && setSecSequencePos(compare.steps) )
975 ++m_sec_sequence.current();
978 case eSecCommand::SET_TONE:
979 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
980 setTone(m_sec_sequence.current()++->tone);
982 case eSecCommand::SEND_DISEQC:
983 sendDiseqc(m_sec_sequence.current()->diseqc);
984 eDebugNoNewLine("[SEC] sendDiseqc: ");
985 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
986 eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
988 ++m_sec_sequence.current();
990 case eSecCommand::SEND_TONEBURST:
991 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
992 sendToneburst(m_sec_sequence.current()++->toneburst);
994 case eSecCommand::SET_FRONTEND:
995 eDebug("[SEC] setFrontend");
997 ++m_sec_sequence.current();
999 case eSecCommand::START_TUNE_TIMEOUT:
1000 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1001 ++m_sec_sequence.current();
1003 case eSecCommand::SET_TIMEOUT:
1004 m_timeoutCount = m_sec_sequence.current()++->val;
1005 eDebug("[SEC] set timeout %d", m_timeoutCount);
1007 case eSecCommand::IF_TIMEOUT_GOTO:
1008 if (!m_timeoutCount)
1010 eDebug("[SEC] rotor timout");
1011 m_sec->setRotorMoving(false);
1012 setSecSequencePos(m_sec_sequence.current()->steps);
1015 ++m_sec_sequence.current();
1017 case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1019 int idx = m_sec_sequence.current()++->val;
1020 if ( idx == 0 || idx == 1 )
1022 m_idleInputpower[idx] = readInputpower();
1023 eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1026 eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1029 case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1031 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1032 int idx = compare.voltage;
1033 if ( idx == 0 || idx == 1 )
1035 int idle = readInputpower();
1036 int diff = abs(idle-m_idleInputpower[idx]);
1039 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1040 setSecSequencePos(compare.steps);
1044 ++m_sec_sequence.current();
1047 case eSecCommand::IF_TUNER_LOCKED_GOTO:
1049 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1050 if (readFrontendData(Locked))
1052 eDebug("[SEC] locked step %d ok", cmd.okcount);
1054 if (cmd.okcount > 12)
1056 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1057 setSecSequencePos(cmd.steps);
1063 eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1065 if (!m_timeoutCount && m_retryCount > 0)
1069 ++m_sec_sequence.current();
1072 case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1073 m_runningInputpower = readInputpower();
1074 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1075 ++m_sec_sequence.current();
1077 case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1079 int idleInputpower = m_idleInputpower[ (m_curVoltage&1) ? 0 : 1];
1080 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1081 const char *txt = cmd.direction ? "running" : "stopped";
1082 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1084 m_runningInputpower,
1087 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1088 || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1091 eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1092 if ( cmd.okcount > 6 )
1094 m_sec->setRotorMoving(cmd.direction);
1095 eDebug("[SEC] rotor is %s", txt);
1096 if (setSecSequencePos(cmd.steps))
1102 eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1104 if (!m_timeoutCount && m_retryCount > 0)
1108 ++m_sec_sequence.current();
1111 case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1112 if (m_data[5] != -1 && m_data[6] != -1)
1113 setSecSequencePos(m_sec_sequence.current()->steps);
1115 ++m_sec_sequence.current();
1117 case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1118 m_data[5] = m_data[6] = -1;
1119 eDebug("[SEC] invalidate current rotorparams");
1120 ++m_sec_sequence.current();
1122 case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1123 m_data[5] = m_data[3];
1124 m_data[6] = m_data[4];
1125 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
1126 ++m_sec_sequence.current();
1128 case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1129 m_retryCount = m_sec_sequence.current()++->val;
1130 eDebug("[SEC] set rotor retries %d", m_retryCount);
1132 case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1135 eDebug("[SEC] no more rotor retrys");
1136 setSecSequencePos(m_sec_sequence.current()->steps);
1139 ++m_sec_sequence.current();
1141 case eSecCommand::SET_POWER_LIMITING_MODE:
1144 ::open("/dev/i2c/1", O_RDWR) :
1145 ::open("/dev/i2c/0", O_RDWR);
1147 unsigned char data[2];
1148 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1149 if(::read(fd, data, 1) != 1)
1150 eDebug("[SEC] error read lnbp (%m)");
1151 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1153 data[0] |= 0x80; // enable static current limiting
1154 eDebug("[SEC] set static current limiting");
1158 data[0] &= ~0x80; // enable dynamic current limiting
1159 eDebug("[SEC] set dynamic current limiting");
1161 if(::write(fd, data, 1) != 1)
1162 eDebug("[SEC] error write lnbp (%m)");
1164 ++m_sec_sequence.current();
1168 ++m_sec_sequence.current();
1169 eDebug("[SEC] unhandled sec command");
1171 m_tuneTimer->start(delay,true);
1175 void eDVBFrontend::setFrontend()
1177 eDebug("setting frontend %d", m_fe);
1178 if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1180 perror("FE_SET_FRONTEND failed");
1185 RESULT eDVBFrontend::getFrontendType(int &t)
1193 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1198 eWarning("no SEC module active!");
1201 res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
1204 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1205 switch (feparm.inversion)
1207 case eDVBFrontendParametersSatellite::Inversion::On:
1208 parm_inversion = INVERSION_ON;
1210 case eDVBFrontendParametersSatellite::Inversion::Off:
1211 parm_inversion = INVERSION_OFF;
1214 case eDVBFrontendParametersSatellite::Inversion::Unknown:
1215 parm_inversion = INVERSION_AUTO;
1221 case eDVBFrontendParametersSatellite::FEC::fNone:
1222 eDebug("no fec set.. assume auto");
1223 case eDVBFrontendParametersSatellite::FEC::fAuto:
1224 parm_u_qpsk_fec_inner = FEC_AUTO;
1226 case eDVBFrontendParametersSatellite::FEC::f1_2:
1227 parm_u_qpsk_fec_inner = FEC_1_2;
1229 case eDVBFrontendParametersSatellite::FEC::f2_3:
1230 parm_u_qpsk_fec_inner = FEC_2_3;
1232 case eDVBFrontendParametersSatellite::FEC::f3_4:
1233 parm_u_qpsk_fec_inner = FEC_3_4;
1235 case eDVBFrontendParametersSatellite::FEC::f5_6:
1236 parm_u_qpsk_fec_inner = FEC_5_6;
1238 case eDVBFrontendParametersSatellite::FEC::f7_8:
1239 parm_u_qpsk_fec_inner = FEC_7_8;
1242 // FIXME !!! get frequency range from tuner
1243 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1245 eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1248 eDebug("tuning to %d mhz", parm_frequency/1000);
1253 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1255 parm_frequency = feparm.frequency * 1000;
1256 parm_u_qam_symbol_rate = feparm.symbol_rate;
1257 switch (feparm.modulation)
1259 case eDVBFrontendParametersCable::Modulation::QAM16:
1260 parm_u_qam_modulation = QAM_16;
1262 case eDVBFrontendParametersCable::Modulation::QAM32:
1263 parm_u_qam_modulation = QAM_32;
1265 case eDVBFrontendParametersCable::Modulation::QAM64:
1266 parm_u_qam_modulation = QAM_64;
1268 case eDVBFrontendParametersCable::Modulation::QAM128:
1269 parm_u_qam_modulation = QAM_128;
1271 case eDVBFrontendParametersCable::Modulation::QAM256:
1272 parm_u_qam_modulation = QAM_256;
1275 case eDVBFrontendParametersCable::Modulation::Auto:
1276 parm_u_qam_modulation = QAM_AUTO;
1279 switch (feparm.inversion)
1281 case eDVBFrontendParametersCable::Inversion::On:
1282 parm_inversion = INVERSION_ON;
1284 case eDVBFrontendParametersCable::Inversion::Off:
1285 parm_inversion = INVERSION_OFF;
1288 case eDVBFrontendParametersCable::Inversion::Unknown:
1289 parm_inversion = INVERSION_AUTO;
1292 switch (feparm.fec_inner)
1294 case eDVBFrontendParametersCable::FEC::fNone:
1295 parm_u_qam_fec_inner = FEC_NONE;
1297 case eDVBFrontendParametersCable::FEC::f1_2:
1298 parm_u_qam_fec_inner = FEC_1_2;
1300 case eDVBFrontendParametersCable::FEC::f2_3:
1301 parm_u_qam_fec_inner = FEC_2_3;
1303 case eDVBFrontendParametersCable::FEC::f3_4:
1304 parm_u_qam_fec_inner = FEC_3_4;
1306 case eDVBFrontendParametersCable::FEC::f5_6:
1307 parm_u_qam_fec_inner = FEC_5_6;
1309 case eDVBFrontendParametersCable::FEC::f7_8:
1310 parm_u_qam_fec_inner = FEC_7_8;
1312 #if HAVE_DVB_API_VERSION >= 3
1313 case eDVBFrontendParametersCable::FEC::f8_9:
1314 parm_u_qam_fec_inner = FEC_8_9;
1318 case eDVBFrontendParametersCable::FEC::fAuto:
1319 parm_u_qam_fec_inner = FEC_AUTO;
1325 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1327 parm_frequency = feparm.frequency;
1329 switch (feparm.bandwidth)
1331 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1332 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1334 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1335 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1337 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1338 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1341 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1342 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1345 switch (feparm.code_rate_LP)
1347 case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1348 parm_u_ofdm_code_rate_LP = FEC_1_2;
1350 case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1351 parm_u_ofdm_code_rate_LP = FEC_2_3;
1353 case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1354 parm_u_ofdm_code_rate_LP = FEC_3_4;
1356 case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1357 parm_u_ofdm_code_rate_LP = FEC_5_6;
1359 case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1360 parm_u_ofdm_code_rate_LP = FEC_7_8;
1363 case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1364 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1367 switch (feparm.code_rate_HP)
1369 case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1370 parm_u_ofdm_code_rate_HP = FEC_1_2;
1372 case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1373 parm_u_ofdm_code_rate_HP = FEC_2_3;
1375 case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1376 parm_u_ofdm_code_rate_HP = FEC_3_4;
1378 case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1379 parm_u_ofdm_code_rate_HP = FEC_5_6;
1381 case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1382 parm_u_ofdm_code_rate_HP = FEC_7_8;
1385 case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1386 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1389 switch (feparm.modulation)
1391 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1392 parm_u_ofdm_constellation = QPSK;
1394 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1395 parm_u_ofdm_constellation = QAM_16;
1398 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1399 parm_u_ofdm_constellation = QAM_AUTO;
1402 switch (feparm.transmission_mode)
1404 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1405 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1407 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1408 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1411 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1412 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1415 switch (feparm.guard_interval)
1417 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1418 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1420 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1421 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1423 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1424 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1426 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1427 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1430 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1431 parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1434 switch (feparm.hierarchy)
1436 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1437 parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1439 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1440 parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1442 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1443 parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1446 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1447 parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1453 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1455 eDebug("(%d)tune", m_fe);
1466 m_sec_sequence.clear();
1472 eDVBFrontendParametersSatellite feparm;
1473 if (where.getDVBS(feparm))
1475 eDebug("no dvbs data!");
1478 res=prepare_sat(feparm);
1483 eDVBFrontendParametersCable feparm;
1484 if (where.getDVBC(feparm))
1486 res=prepare_cable(feparm);
1489 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1490 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1496 eDVBFrontendParametersTerrestrial feparm;
1497 if (where.getDVBT(feparm))
1499 eDebug("no -T data");
1502 res=prepare_terrestrial(feparm);
1505 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1506 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1512 if (!res) // prepare ok
1514 m_tuneTimer->start(0,true);
1515 m_sec_sequence.current() = m_sec_sequence.begin();
1517 if (m_state != stateTuning)
1520 m_state = stateTuning;
1521 m_stateChanged(this);
1528 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1530 connection = new eConnection(this, m_stateChanged.connect(stateChange));
1534 RESULT eDVBFrontend::setVoltage(int voltage)
1536 if (m_type != feSatellite)
1538 #if HAVE_DVB_API_VERSION < 3
1541 bool increased=false;
1542 fe_sec_voltage_t vlt;
1544 m_curVoltage=voltage;
1548 for (int i=0; i < 3; ++i) // reset diseqc
1550 vlt = SEC_VOLTAGE_OFF;
1553 #if HAVE_DVB_API_VERSION < 3
1554 vlt = SEC_VOLTAGE_13_5;
1560 vlt = SEC_VOLTAGE_13;
1563 #if HAVE_DVB_API_VERSION < 3
1564 vlt = SEC_VOLTAGE_18_5;
1570 vlt = SEC_VOLTAGE_18;
1575 #if HAVE_DVB_API_VERSION < 3
1576 return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1578 if (::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1579 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
1580 return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
1584 RESULT eDVBFrontend::getState(int &state)
1590 RESULT eDVBFrontend::setTone(int t)
1592 if (m_type != feSatellite)
1594 #if HAVE_DVB_API_VERSION < 3
1597 fe_sec_tone_mode_t tone;
1606 tone = SEC_TONE_OFF;
1611 #if HAVE_DVB_API_VERSION < 3
1612 return ::ioctl(m_secfd, SEC_SET_TONE, tone);
1614 return ::ioctl(m_fd, FE_SET_TONE, tone);
1618 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
1619 #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
1622 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
1624 #if HAVE_DVB_API_VERSION < 3
1625 struct secCommand cmd;
1626 cmd.type = SEC_CMDTYPE_DISEQC_RAW;
1627 cmd.u.diseqc.cmdtype = diseqc.data[0];
1628 cmd.u.diseqc.addr = diseqc.data[1];
1629 cmd.u.diseqc.cmd = diseqc.data[2];
1630 cmd.u.diseqc.numParams = diseqc.len-3;
1631 memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
1632 if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
1634 struct dvb_diseqc_master_cmd cmd;
1635 memcpy(cmd.msg, diseqc.data, diseqc.len);
1636 cmd.msg_len = diseqc.len;
1637 if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
1643 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
1644 #define SEC_DISEQC_SEND_BURST _IO('o', 96)
1646 RESULT eDVBFrontend::sendToneburst(int burst)
1648 #if HAVE_DVB_API_VERSION < 3
1649 secMiniCmd cmd = SEC_MINI_NONE;
1651 fe_sec_mini_cmd_t cmd = SEC_MINI_A;
1653 if ( burst == eDVBSatelliteDiseqcParameters::A )
1655 else if ( burst == eDVBSatelliteDiseqcParameters::B )
1657 #if HAVE_DVB_API_VERSION < 3
1658 if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
1661 if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
1667 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
1673 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
1675 m_sec_sequence = list;
1679 RESULT eDVBFrontend::getData(int num, int &data)
1681 if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1689 RESULT eDVBFrontend::setData(int num, int val)
1691 if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1699 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
1702 if (feparm->getSystem(type) || type != m_type)
1705 if (m_type == eDVBFrontend::feSatellite)
1708 eDVBFrontendParametersSatellite sat_parm;
1709 ASSERT(!feparm->getDVBS(sat_parm));
1710 return m_sec->canTune(sat_parm, this, 1 << m_fe);