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);
430 m_data[0] = m_data[1] = m_data[2] = -1;
432 #if HAVE_DVB_API_VERSION < 3
445 eDVBFrontend::~eDVBFrontend()
452 void eDVBFrontend::feEvent(int w)
456 #if HAVE_DVB_API_VERSION < 3
459 dvb_frontend_event event;
463 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
465 if (res && (errno == EAGAIN))
470 eWarning("FE_GET_EVENT failed! %m");
477 #if HAVE_DVB_API_VERSION < 3
478 if (event.type == FE_COMPLETION_EV)
480 eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
481 if (event.status & FE_HAS_LOCK)
491 state = stateLostLock;
492 m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
495 if (m_state != state)
498 m_stateChanged(this);
503 void eDVBFrontend::timeout()
506 if (m_state == stateTuning)
508 m_state = stateFailed;
509 m_stateChanged(this);
513 int eDVBFrontend::readFrontendData(int type)
520 if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
521 eDebug("FE_READ_BER failed (%m)");
527 if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
528 eDebug("FE_READ_SNR failed (%m)");
534 if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
535 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
540 #if HAVE_DVB_API_VERSION < 3
541 FrontendStatus status=0;
545 if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
546 eDebug("FE_READ_STATUS failed (%m)");
547 return !!(status&FE_HAS_LOCK);
551 #if HAVE_DVB_API_VERSION < 3
552 FrontendStatus status=0;
556 if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
557 eDebug("FE_READ_STATUS failed (%m)");
558 return !!(status&FE_HAS_SYNC);
564 void PutToDict(PyObject *dict, const char*key, long value)
566 PyObject *item = PyInt_FromLong(value);
569 if (PyDict_SetItemString(dict, key, item))
570 eDebug("put %s to dict failed", key);
574 eDebug("could not create PyObject for %s", key);
577 void PutToDict(PyObject *dict, const char*key, const char *value)
579 PyObject *item = PyString_FromString(value);
582 if (PyDict_SetItemString(dict, key, item))
583 eDebug("put %s to dict failed", key);
587 eDebug("could not create PyObject for %s", key);
590 void fillDictWithSatelliteData(PyObject *dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
596 fe->getData(9, freq_offset);
597 int frequency = parm_frequency + freq_offset;
598 PutToDict(dict, "frequency", frequency);
599 PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
601 switch(parm_u_qpsk_fec_inner)
623 PutToDict(dict, "fec_inner", fec);
626 void fillDictWithCableData(PyObject *dict, const FRONTENDPARAMETERS &parm)
629 PutToDict(dict, "frequency", parm_frequency/1000);
630 PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
631 switch(parm_u_qam_fec_inner)
651 #if HAVE_DVB_API_VERSION >= 3
661 PutToDict(dict, "fec_inner", tmp);
662 switch(parm_u_qam_modulation)
684 PutToDict(dict, "modulation", tmp);
687 void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
690 PutToDict(dict, "frequency", parm_frequency);
691 switch (parm_u_ofdm_bandwidth)
693 case BANDWIDTH_8_MHZ:
694 tmp = "BANDWIDTH_8_MHZ";
696 case BANDWIDTH_7_MHZ:
697 tmp = "BANDWIDTH_7_MHZ";
699 case BANDWIDTH_6_MHZ:
700 tmp = "BANDWIDTH_6_MHZ";
704 tmp = "BANDWIDTH_AUTO";
707 PutToDict(dict, "bandwidth", tmp);
708 switch (parm_u_ofdm_code_rate_LP)
730 PutToDict(dict, "code_rate_lp", tmp);
731 switch (parm_u_ofdm_code_rate_HP)
753 PutToDict(dict, "code_rate_hp", tmp);
754 switch (parm_u_ofdm_constellation)
767 PutToDict(dict, "constellation", tmp);
768 switch (parm_u_ofdm_transmission_mode)
770 case TRANSMISSION_MODE_2K:
771 tmp = "TRANSMISSION_MODE_2K";
773 case TRANSMISSION_MODE_8K:
774 tmp = "TRANSMISSION_MODE_8K";
777 case TRANSMISSION_MODE_AUTO:
778 tmp = "TRANSMISSION_MODE_AUTO";
781 PutToDict(dict, "transmission_mode", tmp);
782 switch (parm_u_ofdm_guard_interval)
784 case GUARD_INTERVAL_1_32:
785 tmp = "GUARD_INTERVAL_1_32";
787 case GUARD_INTERVAL_1_16:
788 tmp = "GUARD_INTERVAL_1_16";
790 case GUARD_INTERVAL_1_8:
791 tmp = "GUARD_INTERVAL_1_8";
793 case GUARD_INTERVAL_1_4:
794 tmp = "GUARD_INTERVAL_1_4";
797 case GUARD_INTERVAL_AUTO:
798 tmp = "GUARD_INTERVAL_AUTO";
801 PutToDict(dict, "guard_interval", tmp);
802 switch (parm_u_ofdm_hierarchy_information)
815 tmp = "HIERARCHY_AUTO";
818 PutToDict(dict, "hierarchy_information", tmp);
821 PyObject *eDVBFrontend::readTransponderData(bool original)
823 PyObject *ret=PyDict_New();
827 bool read=m_fd != -1;
830 PutToDict(ret, "tuner_number", m_fe);
848 PutToDict(ret, "tuner_type", tmp);
852 FRONTENDPARAMETERS front;
875 PutToDict(ret, "tuner_state", tmp);
877 PutToDict(ret, "tuner_locked", readFrontendData(Locked));
878 PutToDict(ret, "tuner_synced", readFrontendData(Synced));
879 PutToDict(ret, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
880 PutToDict(ret, "tuner_signal_power", readFrontendData(signalPower));
881 PutToDict(ret, "tuner_signal_quality", readFrontendData(signalQuality));
883 if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
884 eDebug("FE_GET_FRONTEND (%m)");
887 tmp = "INVERSION_AUTO";
888 switch(parm_inversion)
891 tmp = "INVERSION_ON";
894 tmp = "INVERSION_OFF";
900 PutToDict(ret, "inversion", tmp);
905 fillDictWithSatelliteData(ret, original?parm:front, this);
908 fillDictWithCableData(ret, original?parm:front);
911 fillDictWithTerrestrialData(ret, original?parm:front);
925 #ifndef FP_IOCTL_GET_ID
926 #define FP_IOCTL_GET_ID 0
928 int eDVBFrontend::readInputpower()
930 int power=m_fe; // this is needed for read inputpower from the correct tuner !
932 // open front prozessor
933 int fp=::open("/dev/dbox/fp0", O_RDWR);
936 eDebug("couldn't open fp");
939 static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
940 if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
942 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
950 bool eDVBFrontend::setSecSequencePos(int steps)
952 eDebug("set sequence pos %d", steps);
957 if (m_sec_sequence.current() != m_sec_sequence.end())
958 ++m_sec_sequence.current();
963 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
964 --m_sec_sequence.current();
970 void eDVBFrontend::tuneLoop() // called by m_tuneTimer
973 if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
975 // eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
976 switch (m_sec_sequence.current()->cmd)
978 case eSecCommand::SLEEP:
979 delay = m_sec_sequence.current()++->msec;
980 eDebug("[SEC] sleep %dms", delay);
982 case eSecCommand::GOTO:
983 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
984 ++m_sec_sequence.current();
986 case eSecCommand::SET_VOLTAGE:
988 int voltage = m_sec_sequence.current()++->voltage;
989 eDebug("[SEC] setVoltage %d", voltage);
993 case eSecCommand::IF_VOLTAGE_GOTO:
995 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
996 if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
998 ++m_sec_sequence.current();
1001 case eSecCommand::IF_NOT_VOLTAGE_GOTO:
1003 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1004 if ( compare.voltage != m_curVoltage && setSecSequencePos(compare.steps) )
1006 ++m_sec_sequence.current();
1009 case eSecCommand::SET_TONE:
1010 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
1011 setTone(m_sec_sequence.current()++->tone);
1013 case eSecCommand::SEND_DISEQC:
1014 sendDiseqc(m_sec_sequence.current()->diseqc);
1015 eDebugNoNewLine("[SEC] sendDiseqc: ");
1016 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
1017 eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
1019 ++m_sec_sequence.current();
1021 case eSecCommand::SEND_TONEBURST:
1022 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
1023 sendToneburst(m_sec_sequence.current()++->toneburst);
1025 case eSecCommand::SET_FRONTEND:
1026 eDebug("[SEC] setFrontend");
1028 ++m_sec_sequence.current();
1030 case eSecCommand::START_TUNE_TIMEOUT:
1031 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
1032 ++m_sec_sequence.current();
1034 case eSecCommand::SET_TIMEOUT:
1035 m_timeoutCount = m_sec_sequence.current()++->val;
1036 eDebug("[SEC] set timeout %d", m_timeoutCount);
1038 case eSecCommand::IF_TIMEOUT_GOTO:
1039 if (!m_timeoutCount)
1041 eDebug("[SEC] rotor timout");
1042 m_sec->setRotorMoving(false);
1043 setSecSequencePos(m_sec_sequence.current()->steps);
1046 ++m_sec_sequence.current();
1048 case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1050 int idx = m_sec_sequence.current()++->val;
1051 if ( idx == 0 || idx == 1 )
1053 m_idleInputpower[idx] = readInputpower();
1054 eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1057 eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1060 case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1062 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1063 int idx = compare.voltage;
1064 if ( idx == 0 || idx == 1 )
1066 int idle = readInputpower();
1067 int diff = abs(idle-m_idleInputpower[idx]);
1070 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1071 setSecSequencePos(compare.steps);
1075 ++m_sec_sequence.current();
1078 case eSecCommand::IF_TUNER_LOCKED_GOTO:
1080 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1081 if (readFrontendData(Locked))
1083 eDebug("[SEC] locked step %d ok", cmd.okcount);
1085 if (cmd.okcount > 12)
1087 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1088 setSecSequencePos(cmd.steps);
1094 eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1096 if (!m_timeoutCount && m_retryCount > 0)
1100 ++m_sec_sequence.current();
1103 case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1104 m_runningInputpower = readInputpower();
1105 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1106 ++m_sec_sequence.current();
1108 case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1110 int idleInputpower = m_idleInputpower[ (m_curVoltage&1) ? 0 : 1];
1111 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1112 const char *txt = cmd.direction ? "running" : "stopped";
1113 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1115 m_runningInputpower,
1118 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1119 || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1122 eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1123 if ( cmd.okcount > 6 )
1125 m_sec->setRotorMoving(cmd.direction);
1126 eDebug("[SEC] rotor is %s", txt);
1127 if (setSecSequencePos(cmd.steps))
1133 eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1135 if (!m_timeoutCount && m_retryCount > 0)
1139 ++m_sec_sequence.current();
1142 case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1143 if (m_data[5] != -1 && m_data[6] != -1)
1144 setSecSequencePos(m_sec_sequence.current()->steps);
1146 ++m_sec_sequence.current();
1148 case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1149 m_data[5] = m_data[6] = -1;
1150 eDebug("[SEC] invalidate current rotorparams");
1151 ++m_sec_sequence.current();
1153 case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1154 m_data[5] = m_data[3];
1155 m_data[6] = m_data[4];
1156 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
1157 ++m_sec_sequence.current();
1159 case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1160 m_retryCount = m_sec_sequence.current()++->val;
1161 eDebug("[SEC] set rotor retries %d", m_retryCount);
1163 case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1166 eDebug("[SEC] no more rotor retrys");
1167 setSecSequencePos(m_sec_sequence.current()->steps);
1170 ++m_sec_sequence.current();
1172 case eSecCommand::SET_POWER_LIMITING_MODE:
1175 ::open("/dev/i2c/1", O_RDWR) :
1176 ::open("/dev/i2c/0", O_RDWR);
1178 unsigned char data[2];
1179 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1180 if(::read(fd, data, 1) != 1)
1181 eDebug("[SEC] error read lnbp (%m)");
1182 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1184 data[0] |= 0x80; // enable static current limiting
1185 eDebug("[SEC] set static current limiting");
1189 data[0] &= ~0x80; // enable dynamic current limiting
1190 eDebug("[SEC] set dynamic current limiting");
1192 if(::write(fd, data, 1) != 1)
1193 eDebug("[SEC] error write lnbp (%m)");
1195 ++m_sec_sequence.current();
1199 ++m_sec_sequence.current();
1200 eDebug("[SEC] unhandled sec command");
1202 m_tuneTimer->start(delay,true);
1206 void eDVBFrontend::setFrontend()
1208 eDebug("setting frontend %d", m_fe);
1211 if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1213 perror("FE_SET_FRONTEND failed");
1218 RESULT eDVBFrontend::getFrontendType(int &t)
1226 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1231 eWarning("no SEC module active!");
1234 res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
1237 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1238 switch (feparm.inversion)
1240 case eDVBFrontendParametersSatellite::Inversion::On:
1241 parm_inversion = INVERSION_ON;
1243 case eDVBFrontendParametersSatellite::Inversion::Off:
1244 parm_inversion = INVERSION_OFF;
1247 case eDVBFrontendParametersSatellite::Inversion::Unknown:
1248 parm_inversion = INVERSION_AUTO;
1254 case eDVBFrontendParametersSatellite::FEC::fNone:
1255 eDebug("no fec set.. assume auto");
1256 case eDVBFrontendParametersSatellite::FEC::fAuto:
1257 parm_u_qpsk_fec_inner = FEC_AUTO;
1259 case eDVBFrontendParametersSatellite::FEC::f1_2:
1260 parm_u_qpsk_fec_inner = FEC_1_2;
1262 case eDVBFrontendParametersSatellite::FEC::f2_3:
1263 parm_u_qpsk_fec_inner = FEC_2_3;
1265 case eDVBFrontendParametersSatellite::FEC::f3_4:
1266 parm_u_qpsk_fec_inner = FEC_3_4;
1268 case eDVBFrontendParametersSatellite::FEC::f5_6:
1269 parm_u_qpsk_fec_inner = FEC_5_6;
1271 case eDVBFrontendParametersSatellite::FEC::f7_8:
1272 parm_u_qpsk_fec_inner = FEC_7_8;
1275 // FIXME !!! get frequency range from tuner
1276 if ( parm_frequency < 900000 || parm_frequency > 2200000 )
1278 eDebug("%d mhz out of tuner range.. dont tune", parm_frequency/1000);
1281 eDebug("tuning to %d mhz", parm_frequency/1000);
1286 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1288 parm_frequency = feparm.frequency * 1000;
1289 parm_u_qam_symbol_rate = feparm.symbol_rate;
1290 switch (feparm.modulation)
1292 case eDVBFrontendParametersCable::Modulation::QAM16:
1293 parm_u_qam_modulation = QAM_16;
1295 case eDVBFrontendParametersCable::Modulation::QAM32:
1296 parm_u_qam_modulation = QAM_32;
1298 case eDVBFrontendParametersCable::Modulation::QAM64:
1299 parm_u_qam_modulation = QAM_64;
1301 case eDVBFrontendParametersCable::Modulation::QAM128:
1302 parm_u_qam_modulation = QAM_128;
1304 case eDVBFrontendParametersCable::Modulation::QAM256:
1305 parm_u_qam_modulation = QAM_256;
1308 case eDVBFrontendParametersCable::Modulation::Auto:
1309 parm_u_qam_modulation = QAM_AUTO;
1312 switch (feparm.inversion)
1314 case eDVBFrontendParametersCable::Inversion::On:
1315 parm_inversion = INVERSION_ON;
1317 case eDVBFrontendParametersCable::Inversion::Off:
1318 parm_inversion = INVERSION_OFF;
1321 case eDVBFrontendParametersCable::Inversion::Unknown:
1322 parm_inversion = INVERSION_AUTO;
1325 switch (feparm.fec_inner)
1327 case eDVBFrontendParametersCable::FEC::fNone:
1328 parm_u_qam_fec_inner = FEC_NONE;
1330 case eDVBFrontendParametersCable::FEC::f1_2:
1331 parm_u_qam_fec_inner = FEC_1_2;
1333 case eDVBFrontendParametersCable::FEC::f2_3:
1334 parm_u_qam_fec_inner = FEC_2_3;
1336 case eDVBFrontendParametersCable::FEC::f3_4:
1337 parm_u_qam_fec_inner = FEC_3_4;
1339 case eDVBFrontendParametersCable::FEC::f5_6:
1340 parm_u_qam_fec_inner = FEC_5_6;
1342 case eDVBFrontendParametersCable::FEC::f7_8:
1343 parm_u_qam_fec_inner = FEC_7_8;
1345 #if HAVE_DVB_API_VERSION >= 3
1346 case eDVBFrontendParametersCable::FEC::f8_9:
1347 parm_u_qam_fec_inner = FEC_8_9;
1351 case eDVBFrontendParametersCable::FEC::fAuto:
1352 parm_u_qam_fec_inner = FEC_AUTO;
1358 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1360 parm_frequency = feparm.frequency;
1362 switch (feparm.bandwidth)
1364 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1365 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1367 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1368 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1370 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1371 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1374 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1375 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1378 switch (feparm.code_rate_LP)
1380 case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1381 parm_u_ofdm_code_rate_LP = FEC_1_2;
1383 case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1384 parm_u_ofdm_code_rate_LP = FEC_2_3;
1386 case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1387 parm_u_ofdm_code_rate_LP = FEC_3_4;
1389 case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1390 parm_u_ofdm_code_rate_LP = FEC_5_6;
1392 case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1393 parm_u_ofdm_code_rate_LP = FEC_7_8;
1396 case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1397 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1400 switch (feparm.code_rate_HP)
1402 case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1403 parm_u_ofdm_code_rate_HP = FEC_1_2;
1405 case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1406 parm_u_ofdm_code_rate_HP = FEC_2_3;
1408 case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1409 parm_u_ofdm_code_rate_HP = FEC_3_4;
1411 case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1412 parm_u_ofdm_code_rate_HP = FEC_5_6;
1414 case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1415 parm_u_ofdm_code_rate_HP = FEC_7_8;
1418 case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1419 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1422 switch (feparm.modulation)
1424 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1425 parm_u_ofdm_constellation = QPSK;
1427 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1428 parm_u_ofdm_constellation = QAM_16;
1431 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1432 parm_u_ofdm_constellation = QAM_AUTO;
1435 switch (feparm.transmission_mode)
1437 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1438 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1440 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1441 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1444 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1445 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1448 switch (feparm.guard_interval)
1450 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1451 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1453 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1454 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1456 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1457 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1459 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1460 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1463 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1464 parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1467 switch (feparm.hierarchy)
1469 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1470 parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1472 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1473 parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1475 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1476 parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1479 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1480 parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1486 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1488 eDebug("(%d)tune", m_fe);
1498 m_sec_sequence.clear();
1504 eDVBFrontendParametersSatellite feparm;
1505 if (where.getDVBS(feparm))
1507 eDebug("no dvbs data!");
1510 res=prepare_sat(feparm);
1515 eDVBFrontendParametersCable feparm;
1516 if (where.getDVBC(feparm))
1518 res=prepare_cable(feparm);
1521 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1522 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1528 eDVBFrontendParametersTerrestrial feparm;
1529 if (where.getDVBT(feparm))
1531 eDebug("no -T data");
1534 res=prepare_terrestrial(feparm);
1537 m_sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
1538 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1544 if (!res) // prepare ok
1546 m_tuneTimer->start(0,true);
1547 m_sec_sequence.current() = m_sec_sequence.begin();
1549 if (m_state != stateTuning)
1552 m_state = stateTuning;
1553 m_stateChanged(this);
1560 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1562 connection = new eConnection(this, m_stateChanged.connect(stateChange));
1566 RESULT eDVBFrontend::setVoltage(int voltage)
1568 if (m_type != feSatellite)
1570 #if HAVE_DVB_API_VERSION < 3
1573 bool increased=false;
1574 fe_sec_voltage_t vlt;
1576 m_curVoltage=voltage;
1580 for (int i=0; i < 3; ++i) // reset diseqc
1582 vlt = SEC_VOLTAGE_OFF;
1585 #if HAVE_DVB_API_VERSION < 3
1586 vlt = SEC_VOLTAGE_13_5;
1592 vlt = SEC_VOLTAGE_13;
1595 #if HAVE_DVB_API_VERSION < 3
1596 vlt = SEC_VOLTAGE_18_5;
1602 vlt = SEC_VOLTAGE_18;
1607 #if HAVE_DVB_API_VERSION < 3
1608 return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1610 if (::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1611 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
1612 return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
1616 RESULT eDVBFrontend::getState(int &state)
1622 RESULT eDVBFrontend::setTone(int t)
1624 if (m_type != feSatellite)
1626 #if HAVE_DVB_API_VERSION < 3
1629 fe_sec_tone_mode_t tone;
1638 tone = SEC_TONE_OFF;
1643 #if HAVE_DVB_API_VERSION < 3
1644 return ::ioctl(m_secfd, SEC_SET_TONE, tone);
1646 return ::ioctl(m_fd, FE_SET_TONE, tone);
1650 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
1651 #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
1654 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
1656 #if HAVE_DVB_API_VERSION < 3
1657 struct secCommand cmd;
1658 cmd.type = SEC_CMDTYPE_DISEQC_RAW;
1659 cmd.u.diseqc.cmdtype = diseqc.data[0];
1660 cmd.u.diseqc.addr = diseqc.data[1];
1661 cmd.u.diseqc.cmd = diseqc.data[2];
1662 cmd.u.diseqc.numParams = diseqc.len-3;
1663 memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
1664 if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
1666 struct dvb_diseqc_master_cmd cmd;
1667 memcpy(cmd.msg, diseqc.data, diseqc.len);
1668 cmd.msg_len = diseqc.len;
1669 if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
1675 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
1676 #define SEC_DISEQC_SEND_BURST _IO('o', 96)
1678 RESULT eDVBFrontend::sendToneburst(int burst)
1680 #if HAVE_DVB_API_VERSION < 3
1681 secMiniCmd cmd = SEC_MINI_NONE;
1683 fe_sec_mini_cmd_t cmd = SEC_MINI_A;
1685 if ( burst == eDVBSatelliteDiseqcParameters::A )
1687 else if ( burst == eDVBSatelliteDiseqcParameters::B )
1689 #if HAVE_DVB_API_VERSION < 3
1690 if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
1693 if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
1699 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
1705 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
1707 m_sec_sequence = list;
1711 RESULT eDVBFrontend::getData(int num, int &data)
1713 if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1721 RESULT eDVBFrontend::setData(int num, int val)
1723 if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1731 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
1734 if (feparm->getSystem(type) || type != m_type)
1737 if (m_type == eDVBFrontend::feSatellite)
1740 eDVBFrontendParametersSatellite sat_parm;
1741 ASSERT(!feparm->getDVBS(sat_parm));
1742 return m_sec->canTune(sat_parm, this, 1 << m_fe);