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 int slen = strlen(str);
64 eDebug("invalid diseqc command string length (not 2 byte aligned)");
67 if (slen > MAX_DISEQC_LENGTH*2)
69 eDebug("invalid diseqc command string length (string is to long)");
73 for (int i=0; i < slen; ++i)
75 unsigned char c = str[i];
78 case '0' ... '9': c-=48; break;
79 case 'a' ... 'f': c-=87; break;
80 case 'A' ... 'F': c-=55; break;
82 eDebug("invalid character in hex string..ignore complete diseqc command !");
96 void eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
98 frequency = descriptor.getFrequency() * 10;
99 symbol_rate = descriptor.getSymbolRate() * 100;
100 polarisation = descriptor.getPolarization();
101 fec = descriptor.getFecInner();
104 inversion = Inversion::Unknown;
105 orbital_position = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
106 orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
107 orbital_position += ((descriptor.getOrbitalPosition() >> 4) & 0xF) * 10;
108 orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
109 if (orbital_position && (!descriptor.getWestEastFlag()))
110 orbital_position = 3600 - orbital_position;
111 eDebug("SAT freq %d, %s, pos %d, sr %d, fec %d",
113 polarisation ? "hor" : "vert",
118 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
120 frequency = descriptor.getFrequency() / 10;
121 symbol_rate = descriptor.getSymbolRate() * 100;
122 fec_inner = descriptor.getFecInner();
123 if ( fec_inner == 0xF )
124 fec_inner = FEC::fNone;
125 modulation = descriptor.getModulation();
126 if ( modulation > 0x5 )
127 modulation = Modulation::Auto;
128 inversion = Inversion::Unknown;
129 eDebug("Cable freq %d, mod %d, sr %d, fec %d",
131 modulation, symbol_rate, fec_inner);
134 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor &descriptor)
136 frequency = descriptor.getCentreFrequency() * 10;
137 bandwidth = descriptor.getBandwidth();
138 if ( bandwidth > 2 ) // 5Mhz forced to auto
139 bandwidth = Bandwidth::BwAuto;
140 code_rate_HP = descriptor.getCodeRateHpStream();
141 if (code_rate_HP > 4)
142 code_rate_HP = FEC::fAuto;
143 code_rate_LP = descriptor.getCodeRateLpStream();
144 if (code_rate_LP > 4)
145 code_rate_LP = FEC::fAuto;
146 transmission_mode = descriptor.getTransmissionMode();
147 if (transmission_mode > 2)
148 transmission_mode = TransmissionMode::TMAuto;
149 guard_interval = descriptor.getGuardInterval();
150 if (guard_interval > 3)
151 guard_interval = GuardInterval::GI_Auto;
152 hierarchy = descriptor.getHierarchyInformation()&3;
153 modulation = descriptor.getConstellation();
155 modulation = Modulation::Auto;
156 inversion = Inversion::Unknown;
157 eDebug("Terr freq %d, bw %d, cr_hp %d, cr_lp %d, tm_mode %d, guard %d, hierarchy %d, const %d",
158 frequency, bandwidth, code_rate_HP, code_rate_LP, transmission_mode,
159 guard_interval, hierarchy, modulation);
162 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
166 DEFINE_REF(eDVBFrontendParameters);
168 RESULT eDVBFrontendParameters::getSystem(int &t) const
176 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
178 if (m_type != iDVBFrontend::feSatellite)
184 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
186 if (m_type != iDVBFrontend::feCable)
192 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
194 if (m_type != iDVBFrontend::feTerrestrial)
200 RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p, bool no_rotor_command_on_tune)
203 sat.no_rotor_command_on_tune = no_rotor_command_on_tune;
204 m_type = iDVBFrontend::feSatellite;
208 RESULT eDVBFrontendParameters::setDVBC(const eDVBFrontendParametersCable &p)
211 m_type = iDVBFrontend::feCable;
215 RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &p)
218 m_type = iDVBFrontend::feTerrestrial;
222 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff) const
227 if (parm->getSystem(type))
231 diff = 1<<30; // big difference
237 case iDVBFrontend::feSatellite:
239 eDVBFrontendParametersSatellite osat;
240 if (parm->getDVBS(osat))
243 if (sat.orbital_position != osat.orbital_position)
245 else if (sat.polarisation != osat.polarisation)
249 diff = abs(sat.frequency - osat.frequency);
250 diff += abs(sat.symbol_rate - osat.symbol_rate);
254 case iDVBFrontend::feCable:
255 eDVBFrontendParametersCable ocable;
256 if (parm->getDVBC(ocable))
259 if (cable.modulation != ocable.modulation && cable.modulation != eDVBFrontendParametersCable::Modulation::Auto && ocable.modulation != eDVBFrontendParametersCable::Modulation::Auto)
261 else if (cable.inversion != ocable.inversion && cable.inversion != eDVBFrontendParametersCable::Inversion::Unknown && ocable.inversion != eDVBFrontendParametersCable::Inversion::Unknown)
265 diff = abs(cable.frequency - ocable.frequency);
266 diff += abs(cable.symbol_rate - ocable.symbol_rate);
270 case iDVBFrontend::feTerrestrial:
271 eDVBFrontendParametersTerrestrial oterrestrial;
272 if (parm->getDVBT(oterrestrial))
275 diff = abs(terrestrial.frequency - oterrestrial.frequency);
284 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
288 case iDVBFrontend::feSatellite:
290 hash = (sat.orbital_position << 16);
291 hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
294 case iDVBFrontend::feCable:
295 case iDVBFrontend::feTerrestrial:
301 DEFINE_REF(eDVBFrontend);
303 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
304 :m_type(-1), m_fe(fe), m_fd(-1), m_timeout(0), m_tuneTimer(0)
305 #if HAVE_DVB_API_VERSION < 3
309 #if HAVE_DVB_API_VERSION < 3
310 sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
311 sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
313 sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
315 m_timeout = new eTimer(eApp);
316 CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
318 m_tuneTimer = new eTimer(eApp);
319 CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
321 int entries = sizeof(m_data) / sizeof(int);
322 for (int i=0; i<entries; ++i)
325 m_idleInputpower[0]=m_idleInputpower[1]=0;
327 ok = !openFrontend();
331 int eDVBFrontend::openFrontend()
334 return -1; // already opened
339 #if HAVE_DVB_API_VERSION < 3
340 m_secfd = ::open(m_sec_filename, O_RDWR);
343 eWarning("failed! (%s) %m", m_sec_filename);
346 FrontendInfo fe_info;
348 dvb_frontend_info fe_info;
350 eDebug("opening frontend %d", m_fe);
351 m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
354 eWarning("failed! (%s) %m", m_filename);
355 #if HAVE_DVB_API_VERSION < 3
364 if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
366 eWarning("ioctl FE_GET_INFO failed");
369 #if HAVE_DVB_API_VERSION < 3
376 switch (fe_info.type)
379 m_type = iDVBFrontend::feSatellite;
382 m_type = iDVBFrontend::feCable;
385 m_type = iDVBFrontend::feTerrestrial;
388 eWarning("unknown frontend type.");
391 #if HAVE_DVB_API_VERSION < 3
397 eDebug("detected %s frontend", "satellite\0cable\0 terrestrial"+fe_info.type*10);
400 setTone(iDVBFrontend::toneOff);
401 setVoltage(iDVBFrontend::voltageOff);
403 m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
404 CONNECT(m_sn->activated, eDVBFrontend::feEvent);
409 int eDVBFrontend::closeFrontend()
411 if (!m_fe && m_data[7] != -1)
413 // try to close the first frontend.. but the second is linked to the first
414 eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[7];
415 if (linked_fe->m_inuse)
417 eDebug("dont close frontend %d until the linked frontend %d is still in use",
418 m_fe, linked_fe->m_frontend->getID());
424 eDebug("close frontend %d", m_fe);
426 setTone(iDVBFrontend::toneOff);
427 setVoltage(iDVBFrontend::voltageOff);
429 m_sec->setRotorMoving(false);
432 m_data[0] = m_data[1] = m_data[2] = -1;
434 #if HAVE_DVB_API_VERSION < 3
447 eDVBFrontend::~eDVBFrontend()
454 void eDVBFrontend::feEvent(int w)
458 #if HAVE_DVB_API_VERSION < 3
461 dvb_frontend_event event;
465 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
467 if (res && (errno == EAGAIN))
472 eWarning("FE_GET_EVENT failed! %m");
479 #if HAVE_DVB_API_VERSION < 3
480 if (event.type == FE_COMPLETION_EV)
482 eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
483 if (event.status & FE_HAS_LOCK)
493 state = stateLostLock;
494 m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
497 if (m_state != state)
500 m_stateChanged(this);
505 void eDVBFrontend::timeout()
508 if (m_state == stateTuning)
510 m_state = stateFailed;
511 m_stateChanged(this);
515 int eDVBFrontend::readFrontendData(int type)
522 if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
523 eDebug("FE_READ_BER failed (%m)");
529 if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
530 eDebug("FE_READ_SNR failed (%m)");
536 if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
537 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
542 #if HAVE_DVB_API_VERSION < 3
543 FrontendStatus status=0;
547 if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
548 eDebug("FE_READ_STATUS failed (%m)");
549 return !!(status&FE_HAS_LOCK);
553 #if HAVE_DVB_API_VERSION < 3
554 FrontendStatus status=0;
558 if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
559 eDebug("FE_READ_STATUS failed (%m)");
560 return !!(status&FE_HAS_SYNC);
566 void PutToDict(PyObject *dict, const char*key, long value)
568 PyObject *item = PyInt_FromLong(value);
571 if (PyDict_SetItemString(dict, key, item))
572 eDebug("put %s to dict failed", key);
576 eDebug("could not create PyObject for %s", key);
579 void PutToDict(PyObject *dict, const char*key, const char *value)
581 PyObject *item = PyString_FromString(value);
584 if (PyDict_SetItemString(dict, key, item))
585 eDebug("put %s to dict failed", key);
589 eDebug("could not create PyObject for %s", key);
592 void fillDictWithSatelliteData(PyObject *dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
598 fe->getData(9, freq_offset);
599 int frequency = parm_frequency + freq_offset;
600 PutToDict(dict, "frequency", frequency);
601 PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
603 switch(parm_u_qpsk_fec_inner)
625 PutToDict(dict, "fec_inner", fec);
628 void fillDictWithCableData(PyObject *dict, const FRONTENDPARAMETERS &parm)
631 PutToDict(dict, "frequency", parm_frequency/1000);
632 PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
633 switch(parm_u_qam_fec_inner)
653 #if HAVE_DVB_API_VERSION >= 3
663 PutToDict(dict, "fec_inner", tmp);
664 switch(parm_u_qam_modulation)
686 PutToDict(dict, "modulation", tmp);
689 void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
692 PutToDict(dict, "frequency", parm_frequency);
693 switch (parm_u_ofdm_bandwidth)
695 case BANDWIDTH_8_MHZ:
696 tmp = "BANDWIDTH_8_MHZ";
698 case BANDWIDTH_7_MHZ:
699 tmp = "BANDWIDTH_7_MHZ";
701 case BANDWIDTH_6_MHZ:
702 tmp = "BANDWIDTH_6_MHZ";
706 tmp = "BANDWIDTH_AUTO";
709 PutToDict(dict, "bandwidth", tmp);
710 switch (parm_u_ofdm_code_rate_LP)
732 PutToDict(dict, "code_rate_lp", tmp);
733 switch (parm_u_ofdm_code_rate_HP)
755 PutToDict(dict, "code_rate_hp", tmp);
756 switch (parm_u_ofdm_constellation)
769 PutToDict(dict, "constellation", tmp);
770 switch (parm_u_ofdm_transmission_mode)
772 case TRANSMISSION_MODE_2K:
773 tmp = "TRANSMISSION_MODE_2K";
775 case TRANSMISSION_MODE_8K:
776 tmp = "TRANSMISSION_MODE_8K";
779 case TRANSMISSION_MODE_AUTO:
780 tmp = "TRANSMISSION_MODE_AUTO";
783 PutToDict(dict, "transmission_mode", tmp);
784 switch (parm_u_ofdm_guard_interval)
786 case GUARD_INTERVAL_1_32:
787 tmp = "GUARD_INTERVAL_1_32";
789 case GUARD_INTERVAL_1_16:
790 tmp = "GUARD_INTERVAL_1_16";
792 case GUARD_INTERVAL_1_8:
793 tmp = "GUARD_INTERVAL_1_8";
795 case GUARD_INTERVAL_1_4:
796 tmp = "GUARD_INTERVAL_1_4";
799 case GUARD_INTERVAL_AUTO:
800 tmp = "GUARD_INTERVAL_AUTO";
803 PutToDict(dict, "guard_interval", tmp);
804 switch (parm_u_ofdm_hierarchy_information)
807 tmp = "HIERARCHY_NONE";
820 tmp = "HIERARCHY_AUTO";
823 PutToDict(dict, "hierarchy_information", tmp);
826 PyObject *eDVBFrontend::readTransponderData(bool original)
828 PyObject *ret=PyDict_New();
832 bool read=m_fd != -1;
835 PutToDict(ret, "tuner_number", m_fe);
853 PutToDict(ret, "tuner_type", tmp);
857 FRONTENDPARAMETERS front;
880 PutToDict(ret, "tuner_state", tmp);
882 PutToDict(ret, "tuner_locked", readFrontendData(Locked));
883 PutToDict(ret, "tuner_synced", readFrontendData(Synced));
884 PutToDict(ret, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
885 PutToDict(ret, "tuner_signal_power", readFrontendData(signalPower));
886 PutToDict(ret, "tuner_signal_quality", readFrontendData(signalQuality));
888 if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
889 eDebug("FE_GET_FRONTEND (%m)");
892 tmp = "INVERSION_AUTO";
893 switch(parm_inversion)
896 tmp = "INVERSION_ON";
899 tmp = "INVERSION_OFF";
905 PutToDict(ret, "inversion", tmp);
910 fillDictWithSatelliteData(ret, original?parm:front, this);
913 fillDictWithCableData(ret, original?parm:front);
916 fillDictWithTerrestrialData(ret, original?parm:front);
930 #ifndef FP_IOCTL_GET_ID
931 #define FP_IOCTL_GET_ID 0
933 int eDVBFrontend::readInputpower()
935 int power=m_fe; // this is needed for read inputpower from the correct tuner !
937 // open front prozessor
938 int fp=::open("/dev/dbox/fp0", O_RDWR);
941 eDebug("couldn't open fp");
944 static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
945 if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
947 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
955 bool eDVBFrontend::setSecSequencePos(int steps)
957 eDebug("set sequence pos %d", steps);
962 if (m_sec_sequence.current() != m_sec_sequence.end())
963 ++m_sec_sequence.current();
968 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
969 --m_sec_sequence.current();
975 void eDVBFrontend::tuneLoop() // called by m_tuneTimer
978 if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
980 // eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
981 switch (m_sec_sequence.current()->cmd)
983 case eSecCommand::SLEEP:
984 delay = m_sec_sequence.current()++->msec;
985 eDebug("[SEC] sleep %dms", delay);
987 case eSecCommand::GOTO:
988 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
989 ++m_sec_sequence.current();
991 case eSecCommand::SET_VOLTAGE:
993 int voltage = m_sec_sequence.current()++->voltage;
994 eDebug("[SEC] setVoltage %d", voltage);
998 case eSecCommand::IF_VOLTAGE_GOTO:
1000 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1001 if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
1003 ++m_sec_sequence.current();
1006 case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1008 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1009 if ( compare.voltage != m_curVoltage && setSecSequencePos(compare.steps) )
1011 ++m_sec_sequence.current();
1014 case eSecCommand::SET_TONE:
1015 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1016 setTone(m_sec_sequence.current()++->tone);
1018 case eSecCommand::SEND_DISEQC:
1019 sendDiseqc(m_sec_sequence.current()->diseqc);
1020 eDebugNoNewLine("[SEC] sendDiseqc: ");
1021 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1022 eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1024 ++m_sec_sequence.current();
1026 case eSecCommand::SEND_TONEBURST:
1027 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1028 sendToneburst(m_sec_sequence.current()++->toneburst);
1030 case eSecCommand::SET_FRONTEND:
1031 eDebug("[SEC] setFrontend");
1033 ++m_sec_sequence.current();
1035 case eSecCommand::START_TUNE_TIMEOUT:
1036 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1037 ++m_sec_sequence.current();
1039 case eSecCommand::SET_TIMEOUT:
1040 m_timeoutCount = m_sec_sequence.current()++->val;
1041 eDebug("[SEC] set timeout %d", m_timeoutCount);
1043 case eSecCommand::IF_TIMEOUT_GOTO:
1044 if (!m_timeoutCount)
1046 eDebug("[SEC] rotor timout");
1047 m_sec->setRotorMoving(false);
1048 setSecSequencePos(m_sec_sequence.current()->steps);
1051 ++m_sec_sequence.current();
1053 case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1055 int idx = m_sec_sequence.current()++->val;
1056 if ( idx == 0 || idx == 1 )
1058 m_idleInputpower[idx] = readInputpower();
1059 eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1062 eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1065 case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1067 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1068 int idx = compare.voltage;
1069 if ( idx == 0 || idx == 1 )
1071 int idle = readInputpower();
1072 int diff = abs(idle-m_idleInputpower[idx]);
1075 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1076 setSecSequencePos(compare.steps);
1080 ++m_sec_sequence.current();
1083 case eSecCommand::IF_TUNER_LOCKED_GOTO:
1085 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1086 if (readFrontendData(Locked))
1088 eDebug("[SEC] locked step %d ok", cmd.okcount);
1090 if (cmd.okcount > 12)
1092 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1093 setSecSequencePos(cmd.steps);
1099 eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1101 if (!m_timeoutCount && m_retryCount > 0)
1105 ++m_sec_sequence.current();
1108 case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1109 m_runningInputpower = readInputpower();
1110 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1111 ++m_sec_sequence.current();
1113 case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1115 int idleInputpower = m_idleInputpower[ (m_curVoltage&1) ? 0 : 1];
1116 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1117 const char *txt = cmd.direction ? "running" : "stopped";
1118 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1120 m_runningInputpower,
1123 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1124 || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1127 eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1128 if ( cmd.okcount > 6 )
1130 m_sec->setRotorMoving(cmd.direction);
1131 eDebug("[SEC] rotor is %s", txt);
1132 if (setSecSequencePos(cmd.steps))
1138 eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1140 if (!m_timeoutCount && m_retryCount > 0)
1144 ++m_sec_sequence.current();
1147 case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1148 if (m_data[5] != -1 && m_data[6] != -1)
1149 setSecSequencePos(m_sec_sequence.current()->steps);
1151 ++m_sec_sequence.current();
1153 case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1154 m_data[5] = m_data[6] = -1;
1155 eDebug("[SEC] invalidate current rotorparams");
1156 ++m_sec_sequence.current();
1158 case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1159 m_data[5] = m_data[3];
1160 m_data[6] = m_data[4];
1161 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
1162 ++m_sec_sequence.current();
1164 case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1165 m_retryCount = m_sec_sequence.current()++->val;
1166 eDebug("[SEC] set rotor retries %d", m_retryCount);
1168 case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1171 eDebug("[SEC] no more rotor retrys");
1172 setSecSequencePos(m_sec_sequence.current()->steps);
1175 ++m_sec_sequence.current();
1177 case eSecCommand::SET_POWER_LIMITING_MODE:
1180 ::open("/dev/i2c/1", O_RDWR) :
1181 ::open("/dev/i2c/0", O_RDWR);
1183 unsigned char data[2];
1184 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1185 if(::read(fd, data, 1) != 1)
1186 eDebug("[SEC] error read lnbp (%m)");
1187 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1189 data[0] |= 0x80; // enable static current limiting
1190 eDebug("[SEC] set static current limiting");
1194 data[0] &= ~0x80; // enable dynamic current limiting
1195 eDebug("[SEC] set dynamic current limiting");
1197 if(::write(fd, data, 1) != 1)
1198 eDebug("[SEC] error write lnbp (%m)");
1200 ++m_sec_sequence.current();
1204 ++m_sec_sequence.current();
1205 eDebug("[SEC] unhandled sec command");
1207 m_tuneTimer->start(delay,true);
1211 void eDVBFrontend::setFrontend()
1213 eDebug("setting frontend %d", m_fe);
1216 if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1218 perror("FE_SET_FRONTEND failed");
1223 RESULT eDVBFrontend::getFrontendType(int &t)
1231 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1236 eWarning("no SEC module active!");
1239 res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
1242 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1243 switch (feparm.inversion)
1245 case eDVBFrontendParametersSatellite::Inversion::On:
1246 parm_inversion = INVERSION_ON;
1248 case eDVBFrontendParametersSatellite::Inversion::Off:
1249 parm_inversion = INVERSION_OFF;
1252 case eDVBFrontendParametersSatellite::Inversion::Unknown:
1253 parm_inversion = INVERSION_AUTO;
1259 case eDVBFrontendParametersSatellite::FEC::fNone:
1260 eDebug("no fec set.. assume auto");
1261 case eDVBFrontendParametersSatellite::FEC::fAuto:
1262 parm_u_qpsk_fec_inner = FEC_AUTO;
1264 case eDVBFrontendParametersSatellite::FEC::f1_2:
1265 parm_u_qpsk_fec_inner = FEC_1_2;
1267 case eDVBFrontendParametersSatellite::FEC::f2_3:
1268 parm_u_qpsk_fec_inner = FEC_2_3;
1270 case eDVBFrontendParametersSatellite::FEC::f3_4:
1271 parm_u_qpsk_fec_inner = FEC_3_4;
1273 case eDVBFrontendParametersSatellite::FEC::f5_6:
1274 parm_u_qpsk_fec_inner = FEC_5_6;
1276 case eDVBFrontendParametersSatellite::FEC::f7_8:
1277 parm_u_qpsk_fec_inner = FEC_7_8;
1280 // FIXME !!! get frequency range from tuner
1281 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1283 eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1286 eDebug("tuning to %d mhz", parm_frequency/1000);
1291 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1293 parm_frequency = feparm.frequency * 1000;
1294 parm_u_qam_symbol_rate = feparm.symbol_rate;
1295 switch (feparm.modulation)
1297 case eDVBFrontendParametersCable::Modulation::QAM16:
1298 parm_u_qam_modulation = QAM_16;
1300 case eDVBFrontendParametersCable::Modulation::QAM32:
1301 parm_u_qam_modulation = QAM_32;
1303 case eDVBFrontendParametersCable::Modulation::QAM64:
1304 parm_u_qam_modulation = QAM_64;
1306 case eDVBFrontendParametersCable::Modulation::QAM128:
1307 parm_u_qam_modulation = QAM_128;
1309 case eDVBFrontendParametersCable::Modulation::QAM256:
1310 parm_u_qam_modulation = QAM_256;
1313 case eDVBFrontendParametersCable::Modulation::Auto:
1314 parm_u_qam_modulation = QAM_AUTO;
1317 switch (feparm.inversion)
1319 case eDVBFrontendParametersCable::Inversion::On:
1320 parm_inversion = INVERSION_ON;
1322 case eDVBFrontendParametersCable::Inversion::Off:
1323 parm_inversion = INVERSION_OFF;
1326 case eDVBFrontendParametersCable::Inversion::Unknown:
1327 parm_inversion = INVERSION_AUTO;
1330 switch (feparm.fec_inner)
1332 case eDVBFrontendParametersCable::FEC::fNone:
1333 parm_u_qam_fec_inner = FEC_NONE;
1335 case eDVBFrontendParametersCable::FEC::f1_2:
1336 parm_u_qam_fec_inner = FEC_1_2;
1338 case eDVBFrontendParametersCable::FEC::f2_3:
1339 parm_u_qam_fec_inner = FEC_2_3;
1341 case eDVBFrontendParametersCable::FEC::f3_4:
1342 parm_u_qam_fec_inner = FEC_3_4;
1344 case eDVBFrontendParametersCable::FEC::f5_6:
1345 parm_u_qam_fec_inner = FEC_5_6;
1347 case eDVBFrontendParametersCable::FEC::f7_8:
1348 parm_u_qam_fec_inner = FEC_7_8;
1350 #if HAVE_DVB_API_VERSION >= 3
1351 case eDVBFrontendParametersCable::FEC::f8_9:
1352 parm_u_qam_fec_inner = FEC_8_9;
1356 case eDVBFrontendParametersCable::FEC::fAuto:
1357 parm_u_qam_fec_inner = FEC_AUTO;
1363 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1365 parm_frequency = feparm.frequency;
1367 switch (feparm.bandwidth)
1369 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1370 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1372 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1373 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1375 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1376 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1379 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1380 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1383 switch (feparm.code_rate_LP)
1385 case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1386 parm_u_ofdm_code_rate_LP = FEC_1_2;
1388 case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1389 parm_u_ofdm_code_rate_LP = FEC_2_3;
1391 case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1392 parm_u_ofdm_code_rate_LP = FEC_3_4;
1394 case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1395 parm_u_ofdm_code_rate_LP = FEC_5_6;
1397 case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1398 parm_u_ofdm_code_rate_LP = FEC_7_8;
1401 case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1402 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1405 switch (feparm.code_rate_HP)
1407 case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1408 parm_u_ofdm_code_rate_HP = FEC_1_2;
1410 case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1411 parm_u_ofdm_code_rate_HP = FEC_2_3;
1413 case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1414 parm_u_ofdm_code_rate_HP = FEC_3_4;
1416 case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1417 parm_u_ofdm_code_rate_HP = FEC_5_6;
1419 case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1420 parm_u_ofdm_code_rate_HP = FEC_7_8;
1423 case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1424 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1427 switch (feparm.modulation)
1429 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1430 parm_u_ofdm_constellation = QPSK;
1432 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1433 parm_u_ofdm_constellation = QAM_16;
1436 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1437 parm_u_ofdm_constellation = QAM_AUTO;
1440 switch (feparm.transmission_mode)
1442 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1443 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1445 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1446 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1449 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1450 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1453 switch (feparm.guard_interval)
1455 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1456 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1458 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1459 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1461 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1462 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1464 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1465 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1468 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1469 parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1472 switch (feparm.hierarchy)
1474 case eDVBFrontendParametersTerrestrial::Hierarchy::HNone:
1475 parm_u_ofdm_hierarchy_information = HIERARCHY_NONE;
1477 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1478 parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1480 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1481 parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1483 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1484 parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1487 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1488 parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1491 switch (feparm.inversion)
1493 case eDVBFrontendParametersTerrestrial::Inversion::On:
1494 parm_inversion = INVERSION_ON;
1496 case eDVBFrontendParametersTerrestrial::Inversion::Off:
1497 parm_inversion = INVERSION_OFF;
1500 case eDVBFrontendParametersTerrestrial::Inversion::Unknown:
1501 parm_inversion = INVERSION_AUTO;
1507 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1509 eDebug("(%d)tune", m_fe);
1519 m_sec_sequence.clear();
1525 eDVBFrontendParametersSatellite feparm;
1526 if (where.getDVBS(feparm))
1528 eDebug("no dvbs data!");
1531 res=prepare_sat(feparm);
1532 m_sec->setRotorMoving(false);
1537 eDVBFrontendParametersCable feparm;
1538 if (where.getDVBC(feparm))
1540 res=prepare_cable(feparm);
1543 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1544 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1550 eDVBFrontendParametersTerrestrial feparm;
1551 if (where.getDVBT(feparm))
1553 eDebug("no -T data");
1556 res=prepare_terrestrial(feparm);
1559 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1560 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1566 if (!res) // prepare ok
1568 m_tuneTimer->start(0,true);
1569 m_sec_sequence.current() = m_sec_sequence.begin();
1571 if (m_state != stateTuning)
1574 m_state = stateTuning;
1575 m_stateChanged(this);
1582 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1584 connection = new eConnection(this, m_stateChanged.connect(stateChange));
1588 RESULT eDVBFrontend::setVoltage(int voltage)
1590 if (m_type != feSatellite)
1592 #if HAVE_DVB_API_VERSION < 3
1595 bool increased=false;
1596 fe_sec_voltage_t vlt;
1598 m_curVoltage=voltage;
1602 for (int i=0; i < 3; ++i) // reset diseqc
1604 vlt = SEC_VOLTAGE_OFF;
1607 #if HAVE_DVB_API_VERSION < 3
1608 vlt = SEC_VOLTAGE_13_5;
1614 vlt = SEC_VOLTAGE_13;
1617 #if HAVE_DVB_API_VERSION < 3
1618 vlt = SEC_VOLTAGE_18_5;
1624 vlt = SEC_VOLTAGE_18;
1629 #if HAVE_DVB_API_VERSION < 3
1630 return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1632 if (::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1633 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
1634 return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
1638 RESULT eDVBFrontend::getState(int &state)
1644 RESULT eDVBFrontend::setTone(int t)
1646 if (m_type != feSatellite)
1648 #if HAVE_DVB_API_VERSION < 3
1651 fe_sec_tone_mode_t tone;
1660 tone = SEC_TONE_OFF;
1665 #if HAVE_DVB_API_VERSION < 3
1666 return ::ioctl(m_secfd, SEC_SET_TONE, tone);
1668 return ::ioctl(m_fd, FE_SET_TONE, tone);
1672 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
1673 #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
1676 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
1678 #if HAVE_DVB_API_VERSION < 3
1679 struct secCommand cmd;
1680 cmd.type = SEC_CMDTYPE_DISEQC_RAW;
1681 cmd.u.diseqc.cmdtype = diseqc.data[0];
1682 cmd.u.diseqc.addr = diseqc.data[1];
1683 cmd.u.diseqc.cmd = diseqc.data[2];
1684 cmd.u.diseqc.numParams = diseqc.len-3;
1685 memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
1686 if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
1688 struct dvb_diseqc_master_cmd cmd;
1689 memcpy(cmd.msg, diseqc.data, diseqc.len);
1690 cmd.msg_len = diseqc.len;
1691 if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
1697 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
1698 #define SEC_DISEQC_SEND_BURST _IO('o', 96)
1700 RESULT eDVBFrontend::sendToneburst(int burst)
1702 #if HAVE_DVB_API_VERSION < 3
1703 secMiniCmd cmd = SEC_MINI_NONE;
1705 fe_sec_mini_cmd_t cmd = SEC_MINI_A;
1707 if ( burst == eDVBSatelliteDiseqcParameters::A )
1709 else if ( burst == eDVBSatelliteDiseqcParameters::B )
1711 #if HAVE_DVB_API_VERSION < 3
1712 if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
1715 if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
1721 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
1727 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
1729 m_sec_sequence = list;
1733 RESULT eDVBFrontend::getData(int num, int &data)
1735 if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1743 RESULT eDVBFrontend::setData(int num, int val)
1745 if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1753 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
1756 if (feparm->getSystem(type) || type != m_type)
1759 if (m_type == eDVBFrontend::feSatellite)
1762 eDVBFrontendParametersSatellite sat_parm;
1763 ASSERT(!feparm->getDVBS(sat_parm));
1764 return m_sec->canTune(sat_parm, this, 1 << m_fe);