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 eDVBFrontendParametersSatellite::set(const SatelliteDeliverySystemDescriptor &descriptor)
58 frequency = descriptor.getFrequency() * 10;
59 symbol_rate = descriptor.getSymbolRate() * 100;
60 polarisation = descriptor.getPolarization();
61 fec = descriptor.getFecInner();
64 inversion = Inversion::Unknown;
65 orbital_position = ((descriptor.getOrbitalPosition() >> 12) & 0xF) * 1000;
66 orbital_position += ((descriptor.getOrbitalPosition() >> 8) & 0xF) * 100;
67 orbital_position += ((descriptor.getOrbitalPosition() >> 4) & 0xF) * 10;
68 orbital_position += ((descriptor.getOrbitalPosition()) & 0xF);
69 if (orbital_position && (!descriptor.getWestEastFlag()))
70 orbital_position = 3600 - orbital_position;
71 eDebug("SAT freq %d, %s, pos %d, sr %d, fec %d",
73 polarisation ? "hor" : "vert",
78 void eDVBFrontendParametersCable::set(const CableDeliverySystemDescriptor &descriptor)
80 frequency = descriptor.getFrequency() / 10;
81 symbol_rate = descriptor.getSymbolRate() * 100;
82 fec_inner = descriptor.getFecInner();
83 if ( fec_inner == 0xF )
84 fec_inner = FEC::fNone;
85 modulation = descriptor.getModulation();
86 if ( modulation > 0x5 )
87 modulation = Modulation::Auto;
88 inversion = Inversion::Unknown;
89 eDebug("Cable freq %d, mod %d, sr %d, fec %d",
91 modulation, symbol_rate, fec_inner);
94 void eDVBFrontendParametersTerrestrial::set(const TerrestrialDeliverySystemDescriptor &descriptor)
96 frequency = descriptor.getCentreFrequency() * 10;
97 bandwidth = descriptor.getBandwidth();
98 if ( bandwidth > 2 ) // 5Mhz forced to auto
99 bandwidth = Bandwidth::BwAuto;
100 code_rate_HP = descriptor.getCodeRateHpStream();
101 if (code_rate_HP > 4)
102 code_rate_HP = FEC::fAuto;
103 code_rate_LP = descriptor.getCodeRateLpStream();
104 if (code_rate_LP > 4)
105 code_rate_LP = FEC::fAuto;
106 transmission_mode = descriptor.getTransmissionMode();
107 if (transmission_mode > 2)
108 transmission_mode = TransmissionMode::TMAuto;
109 guard_interval = descriptor.getGuardInterval();
110 if (guard_interval > 3)
111 guard_interval = GuardInterval::GI_Auto;
112 hierarchy = descriptor.getHierarchyInformation()&3;
113 modulation = descriptor.getConstellation();
115 modulation = Modulation::Auto;
116 inversion = Inversion::Unknown;
117 eDebug("Terr freq %d, bw %d, cr_hp %d, cr_lp %d, tm_mode %d, guard %d, hierarchy %d, const %d",
118 frequency, bandwidth, code_rate_HP, code_rate_LP, transmission_mode,
119 guard_interval, hierarchy, modulation);
122 eDVBFrontendParameters::eDVBFrontendParameters(): m_type(-1)
126 DEFINE_REF(eDVBFrontendParameters);
128 RESULT eDVBFrontendParameters::getSystem(int &t) const
136 RESULT eDVBFrontendParameters::getDVBS(eDVBFrontendParametersSatellite &p) const
138 if (m_type != iDVBFrontend::feSatellite)
144 RESULT eDVBFrontendParameters::getDVBC(eDVBFrontendParametersCable &p) const
146 if (m_type != iDVBFrontend::feCable)
152 RESULT eDVBFrontendParameters::getDVBT(eDVBFrontendParametersTerrestrial &p) const
154 if (m_type != iDVBFrontend::feTerrestrial)
160 RESULT eDVBFrontendParameters::setDVBS(const eDVBFrontendParametersSatellite &p)
163 m_type = iDVBFrontend::feSatellite;
167 RESULT eDVBFrontendParameters::setDVBC(const eDVBFrontendParametersCable &p)
170 m_type = iDVBFrontend::feCable;
174 RESULT eDVBFrontendParameters::setDVBT(const eDVBFrontendParametersTerrestrial &p)
177 m_type = iDVBFrontend::feTerrestrial;
181 RESULT eDVBFrontendParameters::calculateDifference(const iDVBFrontendParameters *parm, int &diff) const
186 if (parm->getSystem(type))
190 diff = 1<<30; // big difference
196 case iDVBFrontend::feSatellite:
198 eDVBFrontendParametersSatellite osat;
199 if (parm->getDVBS(osat))
202 if (sat.orbital_position != osat.orbital_position)
204 else if (sat.polarisation != osat.polarisation)
208 diff = abs(sat.frequency - osat.frequency);
209 diff += abs(sat.symbol_rate - osat.symbol_rate);
213 case iDVBFrontend::feCable:
214 eDVBFrontendParametersCable ocable;
215 if (parm->getDVBC(ocable))
218 if (cable.modulation != ocable.modulation && cable.modulation != eDVBFrontendParametersCable::Modulation::Auto && ocable.modulation != eDVBFrontendParametersCable::Modulation::Auto)
220 else if (cable.inversion != ocable.inversion && cable.inversion != eDVBFrontendParametersCable::Inversion::Unknown && ocable.inversion != eDVBFrontendParametersCable::Inversion::Unknown)
224 diff = abs(cable.frequency - ocable.frequency);
225 diff += abs(cable.symbol_rate - ocable.symbol_rate);
229 case iDVBFrontend::feTerrestrial:
230 eDVBFrontendParametersTerrestrial oterrestrial;
231 if (parm->getDVBT(oterrestrial))
234 diff = abs(terrestrial.frequency - oterrestrial.frequency);
243 RESULT eDVBFrontendParameters::getHash(unsigned long &hash) const
247 case iDVBFrontend::feSatellite:
249 hash = (sat.orbital_position << 16);
250 hash |= ((sat.frequency/1000)&0xFFFF)|((sat.polarisation&1) << 15);
253 case iDVBFrontend::feCable:
254 case iDVBFrontend::feTerrestrial:
260 DEFINE_REF(eDVBFrontend);
262 eDVBFrontend::eDVBFrontend(int adap, int fe, int &ok)
263 :m_type(-1), m_fe(fe), m_fd(-1), m_timeout(0), m_tuneTimer(0)
264 #if HAVE_DVB_API_VERSION < 3
268 #if HAVE_DVB_API_VERSION < 3
269 sprintf(m_filename, "/dev/dvb/card%d/frontend%d", adap, fe);
270 sprintf(m_sec_filename, "/dev/dvb/card%d/sec%d", adap, fe);
272 sprintf(m_filename, "/dev/dvb/adapter%d/frontend%d", adap, fe);
274 m_timeout = new eTimer(eApp);
275 CONNECT(m_timeout->timeout, eDVBFrontend::timeout);
277 m_tuneTimer = new eTimer(eApp);
278 CONNECT(m_tuneTimer->timeout, eDVBFrontend::tuneLoop);
280 int entries = sizeof(m_data) / sizeof(int);
281 for (int i=0; i<entries; ++i)
284 m_idleInputpower[0]=m_idleInputpower[1]=0;
286 ok = !openFrontend();
290 int eDVBFrontend::openFrontend()
293 return -1; // already opened
298 #if HAVE_DVB_API_VERSION < 3
299 m_secfd = ::open(m_sec_filename, O_RDWR);
302 eWarning("failed! (%s) %m", m_sec_filename);
305 FrontendInfo fe_info;
307 dvb_frontend_info fe_info;
309 eDebug("opening frontend %d", m_fe);
310 m_fd = ::open(m_filename, O_RDWR|O_NONBLOCK);
313 eWarning("failed! (%s) %m", m_filename);
314 #if HAVE_DVB_API_VERSION < 3
323 if (::ioctl(m_fd, FE_GET_INFO, &fe_info) < 0)
325 eWarning("ioctl FE_GET_INFO failed");
328 #if HAVE_DVB_API_VERSION < 3
335 switch (fe_info.type)
338 m_type = iDVBFrontend::feSatellite;
341 m_type = iDVBFrontend::feCable;
344 m_type = iDVBFrontend::feTerrestrial;
347 eWarning("unknown frontend type.");
350 #if HAVE_DVB_API_VERSION < 3
356 eDebug("detected %s frontend", "satellite\0cable\0 terrestrial"+fe_info.type*10);
359 setTone(iDVBFrontend::toneOff);
360 setVoltage(iDVBFrontend::voltageOff);
362 m_sn = new eSocketNotifier(eApp, m_fd, eSocketNotifier::Read);
363 CONNECT(m_sn->activated, eDVBFrontend::feEvent);
369 int eDVBFrontend::closeFrontend()
371 if (!m_fe && m_data[7] != -1)
373 // try to close the first frontend.. but the second is linked to the first
374 eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*)m_data[7];
375 if (linked_fe->m_inuse)
377 eDebug("dont close frontend %d until the linked frontend %d is still in use",
378 m_fe, linked_fe->m_frontend->getID());
384 eDebug("close frontend %d", m_fe);
385 setTone(iDVBFrontend::toneOff);
386 setVoltage(iDVBFrontend::voltageOff);
389 m_data[0] = m_data[1] = m_data[2] = -1;
391 #if HAVE_DVB_API_VERSION < 3
404 eDVBFrontend::~eDVBFrontend()
411 void eDVBFrontend::feEvent(int w)
415 #if HAVE_DVB_API_VERSION < 3
418 dvb_frontend_event event;
422 res = ::ioctl(m_fd, FE_GET_EVENT, &event);
424 if (res && (errno == EAGAIN))
429 eWarning("FE_GET_EVENT failed! %m");
436 #if HAVE_DVB_API_VERSION < 3
437 if (event.type == FE_COMPLETION_EV)
439 eDebug("(%d)fe event: status %x, inversion %s", m_fe, event.status, (event.parameters.inversion == INVERSION_ON) ? "on" : "off");
440 if (event.status & FE_HAS_LOCK)
450 state = stateLostLock;
451 m_data[0] = m_data[1] = m_data[2] = -1; // reset diseqc
454 if (m_state != state)
457 m_stateChanged(this);
462 void eDVBFrontend::timeout()
465 if (m_state == stateTuning)
467 m_state = stateFailed;
468 m_stateChanged(this);
472 int eDVBFrontend::readFrontendData(int type)
479 if (ioctl(m_fd, FE_READ_BER, &ber) < 0 && errno != ERANGE)
480 eDebug("FE_READ_BER failed (%m)");
486 if (ioctl(m_fd, FE_READ_SNR, &snr) < 0 && errno != ERANGE)
487 eDebug("FE_READ_SNR failed (%m)");
493 if (ioctl(m_fd, FE_READ_SIGNAL_STRENGTH, &strength) < 0 && errno != ERANGE)
494 eDebug("FE_READ_SIGNAL_STRENGTH failed (%m)");
499 #if HAVE_DVB_API_VERSION < 3
500 FrontendStatus status=0;
504 if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
505 eDebug("FE_READ_STATUS failed (%m)");
506 return !!(status&FE_HAS_LOCK);
510 #if HAVE_DVB_API_VERSION < 3
511 FrontendStatus status=0;
515 if ( ioctl(m_fd, FE_READ_STATUS, &status) < 0 && errno != ERANGE )
516 eDebug("FE_READ_STATUS failed (%m)");
517 return !!(status&FE_HAS_SYNC);
523 void PutToDict(PyObject *dict, const char*key, long value)
525 PyObject *item = PyInt_FromLong(value);
528 if (PyDict_SetItemString(dict, key, item))
529 eDebug("put %s to dict failed", key);
533 eDebug("could not create PyObject for %s", key);
536 void PutToDict(PyObject *dict, const char*key, const char *value)
538 PyObject *item = PyString_FromString(value);
541 if (PyDict_SetItemString(dict, key, item))
542 eDebug("put %s to dict failed", key);
546 eDebug("could not create PyObject for %s", key);
549 void fillDictWithSatelliteData(PyObject *dict, const FRONTENDPARAMETERS &parm, eDVBFrontend *fe)
555 fe->getData(9, freq_offset);
556 int frequency = parm_frequency + freq_offset;
557 PutToDict(dict, "frequency", frequency);
558 PutToDict(dict, "symbol_rate", parm_u_qpsk_symbol_rate);
560 switch(parm_u_qpsk_fec_inner)
582 PutToDict(dict, "fec_inner", fec);
585 void fillDictWithCableData(PyObject *dict, const FRONTENDPARAMETERS &parm)
588 PutToDict(dict, "frequency", parm_frequency/1000);
589 PutToDict(dict, "symbol_rate", parm_u_qam_symbol_rate);
590 switch(parm_u_qam_fec_inner)
610 #if HAVE_DVB_API_VERSION >= 3
620 PutToDict(dict, "fec_inner", tmp);
621 switch(parm_u_qam_modulation)
643 PutToDict(dict, "modulation", tmp);
646 void fillDictWithTerrestrialData(PyObject *dict, const FRONTENDPARAMETERS &parm)
649 PutToDict(dict, "frequency", parm_frequency);
650 switch (parm_u_ofdm_bandwidth)
652 case BANDWIDTH_8_MHZ:
653 tmp = "BANDWIDTH_8_MHZ";
655 case BANDWIDTH_7_MHZ:
656 tmp = "BANDWIDTH_7_MHZ";
658 case BANDWIDTH_6_MHZ:
659 tmp = "BANDWIDTH_6_MHZ";
663 tmp = "BANDWIDTH_AUTO";
666 PutToDict(dict, "bandwidth", tmp);
667 switch (parm_u_ofdm_code_rate_LP)
689 PutToDict(dict, "code_rate_lp", tmp);
690 switch (parm_u_ofdm_code_rate_HP)
712 PutToDict(dict, "code_rate_hp", tmp);
713 switch (parm_u_ofdm_constellation)
726 PutToDict(dict, "constellation", tmp);
727 switch (parm_u_ofdm_transmission_mode)
729 case TRANSMISSION_MODE_2K:
730 tmp = "TRANSMISSION_MODE_2K";
732 case TRANSMISSION_MODE_8K:
733 tmp = "TRANSMISSION_MODE_8K";
736 case TRANSMISSION_MODE_AUTO:
737 tmp = "TRANSMISSION_MODE_AUTO";
740 PutToDict(dict, "transmission_mode", tmp);
741 switch (parm_u_ofdm_guard_interval)
743 case GUARD_INTERVAL_1_32:
744 tmp = "GUARD_INTERVAL_1_32";
746 case GUARD_INTERVAL_1_16:
747 tmp = "GUARD_INTERVAL_1_16";
749 case GUARD_INTERVAL_1_8:
750 tmp = "GUARD_INTERVAL_1_8";
752 case GUARD_INTERVAL_1_4:
753 tmp = "GUARD_INTERVAL_1_4";
756 case GUARD_INTERVAL_AUTO:
757 tmp = "GUARD_INTERVAL_AUTO";
760 PutToDict(dict, "guard_interval", tmp);
761 switch (parm_u_ofdm_hierarchy_information)
774 tmp = "HIERARCHY_AUTO";
777 PutToDict(dict, "hierarchy_information", tmp);
780 PyObject *eDVBFrontend::readTransponderData(bool original)
782 PyObject *ret=PyDict_New();
786 bool read=m_fd != -1;
789 PutToDict(ret, "tuner_number", m_fe);
807 PutToDict(ret, "tuner_type", tmp);
811 FRONTENDPARAMETERS front;
834 PutToDict(ret, "tuner_state", tmp);
836 PutToDict(ret, "tuner_locked", readFrontendData(Locked));
837 PutToDict(ret, "tuner_synced", readFrontendData(Synced));
838 PutToDict(ret, "tuner_bit_error_rate", readFrontendData(bitErrorRate));
839 PutToDict(ret, "tuner_signal_power", readFrontendData(signalPower));
840 PutToDict(ret, "tuner_signal_quality", readFrontendData(signalQuality));
842 if (!original && ioctl(m_fd, FE_GET_FRONTEND, &front)<0)
843 eDebug("FE_GET_FRONTEND (%m)");
846 tmp = "INVERSION_AUTO";
847 switch(parm_inversion)
850 tmp = "INVERSION_ON";
853 tmp = "INVERSION_OFF";
859 PutToDict(ret, "inversion", tmp);
864 fillDictWithSatelliteData(ret, original?parm:front, this);
867 fillDictWithCableData(ret, original?parm:front);
870 fillDictWithTerrestrialData(ret, original?parm:front);
884 #ifndef FP_IOCTL_GET_ID
885 #define FP_IOCTL_GET_ID 0
887 int eDVBFrontend::readInputpower()
889 int power=m_fe; // this is needed for read inputpower from the correct tuner !
891 // open front prozessor
892 int fp=::open("/dev/dbox/fp0", O_RDWR);
895 eDebug("couldn't open fp");
898 static bool old_fp = (::ioctl(fp, FP_IOCTL_GET_ID) < 0);
899 if ( ioctl( fp, old_fp ? 9 : 0x100, &power ) < 0 )
901 eDebug("FP_IOCTL_GET_LNB_CURRENT failed (%m)");
909 bool eDVBFrontend::setSecSequencePos(int steps)
911 eDebug("set sequence pos %d", steps);
916 if (m_sec_sequence.current() != m_sec_sequence.end())
917 ++m_sec_sequence.current();
922 if (m_sec_sequence.current() != m_sec_sequence.begin() && m_sec_sequence.current() != m_sec_sequence.end())
923 --m_sec_sequence.current();
929 void eDVBFrontend::tuneLoop() // called by m_tuneTimer
932 if ( m_sec_sequence && m_sec_sequence.current() != m_sec_sequence.end() )
934 // eDebug("tuneLoop %d\n", m_sec_sequence.current()->cmd);
935 switch (m_sec_sequence.current()->cmd)
937 case eSecCommand::SLEEP:
938 delay = m_sec_sequence.current()++->msec;
939 eDebug("[SEC] sleep %dms", delay);
941 case eSecCommand::GOTO:
942 if ( !setSecSequencePos(m_sec_sequence.current()->steps) )
943 ++m_sec_sequence.current();
945 case eSecCommand::SET_VOLTAGE:
947 int voltage = m_sec_sequence.current()++->voltage;
948 eDebug("[SEC] setVoltage %d", voltage);
952 case eSecCommand::IF_VOLTAGE_GOTO:
954 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
955 if ( compare.voltage == m_curVoltage && setSecSequencePos(compare.steps) )
957 ++m_sec_sequence.current();
960 case eSecCommand::IF_NOT_VOLTAGE_GOTO:
962 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
963 if ( compare.voltage != m_curVoltage && setSecSequencePos(compare.steps) )
965 ++m_sec_sequence.current();
968 case eSecCommand::SET_TONE:
969 eDebug("[SEC] setTone %d", m_sec_sequence.current()->tone);
970 setTone(m_sec_sequence.current()++->tone);
972 case eSecCommand::SEND_DISEQC:
973 sendDiseqc(m_sec_sequence.current()->diseqc);
974 eDebugNoNewLine("[SEC] sendDiseqc: ");
975 for (int i=0; i < m_sec_sequence.current()->diseqc.len; ++i)
976 eDebugNoNewLine("%02x", m_sec_sequence.current()->diseqc.data[i]);
978 ++m_sec_sequence.current();
980 case eSecCommand::SEND_TONEBURST:
981 eDebug("[SEC] sendToneburst: %d", m_sec_sequence.current()->toneburst);
982 sendToneburst(m_sec_sequence.current()++->toneburst);
984 case eSecCommand::SET_FRONTEND:
985 eDebug("[SEC] setFrontend");
987 ++m_sec_sequence.current();
989 case eSecCommand::START_TUNE_TIMEOUT:
990 m_timeout->start(5000, 1); // 5 sec timeout. TODO: symbolrate dependent
991 ++m_sec_sequence.current();
993 case eSecCommand::SET_TIMEOUT:
994 m_timeoutCount = m_sec_sequence.current()++->val;
995 eDebug("[SEC] set timeout %d", m_timeoutCount);
997 case eSecCommand::IF_TIMEOUT_GOTO:
1000 eDebug("[SEC] rotor timout");
1001 m_sec->setRotorMoving(false);
1002 setSecSequencePos(m_sec_sequence.current()->steps);
1005 ++m_sec_sequence.current();
1007 case eSecCommand::MEASURE_IDLE_INPUTPOWER:
1009 int idx = m_sec_sequence.current()++->val;
1010 if ( idx == 0 || idx == 1 )
1012 m_idleInputpower[idx] = readInputpower();
1013 eDebug("[SEC] idleInputpower[%d] is %d", idx, m_idleInputpower[idx]);
1016 eDebug("[SEC] idleInputpower measure index(%d) out of bound !!!", idx);
1019 case eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO:
1021 eSecCommand::pair &compare = m_sec_sequence.current()->compare;
1022 int idx = compare.voltage;
1023 if ( idx == 0 || idx == 1 )
1025 int idle = readInputpower();
1026 int diff = abs(idle-m_idleInputpower[idx]);
1029 eDebug("measure idle(%d) was not okay.. (%d - %d = %d) retry", idx, m_idleInputpower[idx], idle, diff);
1030 setSecSequencePos(compare.steps);
1034 ++m_sec_sequence.current();
1037 case eSecCommand::IF_TUNER_LOCKED_GOTO:
1039 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1040 if (readFrontendData(Locked))
1042 eDebug("[SEC] locked step %d ok", cmd.okcount);
1044 if (cmd.okcount > 12)
1046 eDebug("ok > 12 .. goto %d\n",m_sec_sequence.current()->steps);
1047 setSecSequencePos(cmd.steps);
1053 eDebug("[SEC] rotor locked step %d failed", cmd.okcount);
1055 if (!m_timeoutCount && m_retryCount > 0)
1059 ++m_sec_sequence.current();
1062 case eSecCommand::MEASURE_RUNNING_INPUTPOWER:
1063 m_runningInputpower = readInputpower();
1064 eDebug("[SEC] runningInputpower is %d", m_runningInputpower);
1065 ++m_sec_sequence.current();
1067 case eSecCommand::IF_INPUTPOWER_DELTA_GOTO:
1069 int idleInputpower = m_idleInputpower[ (m_curVoltage&1) ? 0 : 1];
1070 eSecCommand::rotor &cmd = m_sec_sequence.current()->measure;
1071 const char *txt = cmd.direction ? "running" : "stopped";
1072 eDebug("[SEC] waiting for rotor %s %d, idle %d, delta %d",
1074 m_runningInputpower,
1077 if ( (cmd.direction && abs(m_runningInputpower - idleInputpower) >= cmd.deltaA)
1078 || (!cmd.direction && abs(m_runningInputpower - idleInputpower) <= cmd.deltaA) )
1081 eDebug("[SEC] rotor %s step %d ok", txt, cmd.okcount);
1082 if ( cmd.okcount > 6 )
1084 m_sec->setRotorMoving(cmd.direction);
1085 eDebug("[SEC] rotor is %s", txt);
1086 if (setSecSequencePos(cmd.steps))
1092 eDebug("[SEC] rotor not %s... reset counter.. increase timeout", txt);
1094 if (!m_timeoutCount && m_retryCount > 0)
1098 ++m_sec_sequence.current();
1101 case eSecCommand::IF_ROTORPOS_VALID_GOTO:
1102 if (m_data[5] != -1 && m_data[6] != -1)
1103 setSecSequencePos(m_sec_sequence.current()->steps);
1105 ++m_sec_sequence.current();
1107 case eSecCommand::INVALIDATE_CURRENT_ROTORPARMS:
1108 m_data[5] = m_data[6] = -1;
1109 eDebug("[SEC] invalidate current rotorparams");
1110 ++m_sec_sequence.current();
1112 case eSecCommand::UPDATE_CURRENT_ROTORPARAMS:
1113 m_data[5] = m_data[3];
1114 m_data[6] = m_data[4];
1115 eDebug("[SEC] update current rotorparams %d %04x %d", m_timeoutCount, m_data[5], m_data[6]);
1116 ++m_sec_sequence.current();
1118 case eSecCommand::SET_ROTOR_DISEQC_RETRYS:
1119 m_retryCount = m_sec_sequence.current()++->val;
1120 eDebug("[SEC] set rotor retries %d", m_retryCount);
1122 case eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO:
1125 eDebug("[SEC] no more rotor retrys");
1126 setSecSequencePos(m_sec_sequence.current()->steps);
1129 ++m_sec_sequence.current();
1131 case eSecCommand::SET_POWER_LIMITING_MODE:
1134 ::open("/dev/i2c/1", O_RDWR) :
1135 ::open("/dev/i2c/0", O_RDWR);
1137 unsigned char data[2];
1138 ::ioctl(fd, I2C_SLAVE_FORCE, 0x10 >> 1);
1139 if(::read(fd, data, 1) != 1)
1140 eDebug("[SEC] error read lnbp (%m)");
1141 if ( m_sec_sequence.current()->mode == eSecCommand::modeStatic )
1143 data[0] |= 0x80; // enable static current limiting
1144 eDebug("[SEC] set static current limiting");
1148 data[0] &= ~0x80; // enable dynamic current limiting
1149 eDebug("[SEC] set dynamic current limiting");
1151 if(::write(fd, data, 1) != 1)
1152 eDebug("[SEC] error write lnbp (%m)");
1154 ++m_sec_sequence.current();
1158 ++m_sec_sequence.current();
1159 eDebug("[SEC] unhandled sec command");
1161 m_tuneTimer->start(delay,true);
1165 void eDVBFrontend::setFrontend()
1167 eDebug("setting frontend %d", m_fe);
1168 if (ioctl(m_fd, FE_SET_FRONTEND, &parm) == -1)
1170 perror("FE_SET_FRONTEND failed");
1175 RESULT eDVBFrontend::getFrontendType(int &t)
1183 RESULT eDVBFrontend::prepare_sat(const eDVBFrontendParametersSatellite &feparm)
1188 eWarning("no SEC module active!");
1191 res = m_sec->prepare(*this, parm, feparm, 1 << m_fe);
1194 parm_u_qpsk_symbol_rate = feparm.symbol_rate;
1195 switch (feparm.inversion)
1197 case eDVBFrontendParametersSatellite::Inversion::On:
1198 parm_inversion = INVERSION_ON;
1200 case eDVBFrontendParametersSatellite::Inversion::Off:
1201 parm_inversion = INVERSION_OFF;
1204 case eDVBFrontendParametersSatellite::Inversion::Unknown:
1205 parm_inversion = INVERSION_AUTO;
1211 case eDVBFrontendParametersSatellite::FEC::fNone:
1212 eDebug("no fec set.. assume auto");
1213 case eDVBFrontendParametersSatellite::FEC::fAuto:
1214 parm_u_qpsk_fec_inner = FEC_AUTO;
1216 case eDVBFrontendParametersSatellite::FEC::f1_2:
1217 parm_u_qpsk_fec_inner = FEC_1_2;
1219 case eDVBFrontendParametersSatellite::FEC::f2_3:
1220 parm_u_qpsk_fec_inner = FEC_2_3;
1222 case eDVBFrontendParametersSatellite::FEC::f3_4:
1223 parm_u_qpsk_fec_inner = FEC_3_4;
1225 case eDVBFrontendParametersSatellite::FEC::f5_6:
1226 parm_u_qpsk_fec_inner = FEC_5_6;
1228 case eDVBFrontendParametersSatellite::FEC::f7_8:
1229 parm_u_qpsk_fec_inner = FEC_7_8;
1232 eDebug("tuning to %d mhz", parm_frequency/1000);
1237 RESULT eDVBFrontend::prepare_cable(const eDVBFrontendParametersCable &feparm)
1239 parm_frequency = feparm.frequency * 1000;
1240 parm_u_qam_symbol_rate = feparm.symbol_rate;
1241 switch (feparm.modulation)
1243 case eDVBFrontendParametersCable::Modulation::QAM16:
1244 parm_u_qam_modulation = QAM_16;
1246 case eDVBFrontendParametersCable::Modulation::QAM32:
1247 parm_u_qam_modulation = QAM_32;
1249 case eDVBFrontendParametersCable::Modulation::QAM64:
1250 parm_u_qam_modulation = QAM_64;
1252 case eDVBFrontendParametersCable::Modulation::QAM128:
1253 parm_u_qam_modulation = QAM_128;
1255 case eDVBFrontendParametersCable::Modulation::QAM256:
1256 parm_u_qam_modulation = QAM_256;
1259 case eDVBFrontendParametersCable::Modulation::Auto:
1260 parm_u_qam_modulation = QAM_AUTO;
1263 switch (feparm.inversion)
1265 case eDVBFrontendParametersCable::Inversion::On:
1266 parm_inversion = INVERSION_ON;
1268 case eDVBFrontendParametersCable::Inversion::Off:
1269 parm_inversion = INVERSION_OFF;
1272 case eDVBFrontendParametersCable::Inversion::Unknown:
1273 parm_inversion = INVERSION_AUTO;
1276 switch (feparm.fec_inner)
1278 case eDVBFrontendParametersCable::FEC::fNone:
1279 parm_u_qam_fec_inner = FEC_NONE;
1281 case eDVBFrontendParametersCable::FEC::f1_2:
1282 parm_u_qam_fec_inner = FEC_1_2;
1284 case eDVBFrontendParametersCable::FEC::f2_3:
1285 parm_u_qam_fec_inner = FEC_2_3;
1287 case eDVBFrontendParametersCable::FEC::f3_4:
1288 parm_u_qam_fec_inner = FEC_3_4;
1290 case eDVBFrontendParametersCable::FEC::f5_6:
1291 parm_u_qam_fec_inner = FEC_5_6;
1293 case eDVBFrontendParametersCable::FEC::f7_8:
1294 parm_u_qam_fec_inner = FEC_7_8;
1296 #if HAVE_DVB_API_VERSION >= 3
1297 case eDVBFrontendParametersCable::FEC::f8_9:
1298 parm_u_qam_fec_inner = FEC_8_9;
1302 case eDVBFrontendParametersCable::FEC::fAuto:
1303 parm_u_qam_fec_inner = FEC_AUTO;
1309 RESULT eDVBFrontend::prepare_terrestrial(const eDVBFrontendParametersTerrestrial &feparm)
1311 parm_frequency = feparm.frequency;
1313 switch (feparm.bandwidth)
1315 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw8MHz:
1316 parm_u_ofdm_bandwidth = BANDWIDTH_8_MHZ;
1318 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw7MHz:
1319 parm_u_ofdm_bandwidth = BANDWIDTH_7_MHZ;
1321 case eDVBFrontendParametersTerrestrial::Bandwidth::Bw6MHz:
1322 parm_u_ofdm_bandwidth = BANDWIDTH_6_MHZ;
1325 case eDVBFrontendParametersTerrestrial::Bandwidth::BwAuto:
1326 parm_u_ofdm_bandwidth = BANDWIDTH_AUTO;
1329 switch (feparm.code_rate_LP)
1331 case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1332 parm_u_ofdm_code_rate_LP = FEC_1_2;
1334 case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1335 parm_u_ofdm_code_rate_LP = FEC_2_3;
1337 case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1338 parm_u_ofdm_code_rate_LP = FEC_3_4;
1340 case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1341 parm_u_ofdm_code_rate_LP = FEC_5_6;
1343 case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1344 parm_u_ofdm_code_rate_LP = FEC_7_8;
1347 case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1348 parm_u_ofdm_code_rate_LP = FEC_AUTO;
1351 switch (feparm.code_rate_HP)
1353 case eDVBFrontendParametersTerrestrial::FEC::f1_2:
1354 parm_u_ofdm_code_rate_HP = FEC_1_2;
1356 case eDVBFrontendParametersTerrestrial::FEC::f2_3:
1357 parm_u_ofdm_code_rate_HP = FEC_2_3;
1359 case eDVBFrontendParametersTerrestrial::FEC::f3_4:
1360 parm_u_ofdm_code_rate_HP = FEC_3_4;
1362 case eDVBFrontendParametersTerrestrial::FEC::f5_6:
1363 parm_u_ofdm_code_rate_HP = FEC_5_6;
1365 case eDVBFrontendParametersTerrestrial::FEC::f7_8:
1366 parm_u_ofdm_code_rate_HP = FEC_7_8;
1369 case eDVBFrontendParametersTerrestrial::FEC::fAuto:
1370 parm_u_ofdm_code_rate_HP = FEC_AUTO;
1373 switch (feparm.modulation)
1375 case eDVBFrontendParametersTerrestrial::Modulation::QPSK:
1376 parm_u_ofdm_constellation = QPSK;
1378 case eDVBFrontendParametersTerrestrial::Modulation::QAM16:
1379 parm_u_ofdm_constellation = QAM_16;
1382 case eDVBFrontendParametersTerrestrial::Modulation::Auto:
1383 parm_u_ofdm_constellation = QAM_AUTO;
1386 switch (feparm.transmission_mode)
1388 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM2k:
1389 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_2K;
1391 case eDVBFrontendParametersTerrestrial::TransmissionMode::TM8k:
1392 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_8K;
1395 case eDVBFrontendParametersTerrestrial::TransmissionMode::TMAuto:
1396 parm_u_ofdm_transmission_mode = TRANSMISSION_MODE_AUTO;
1399 switch (feparm.guard_interval)
1401 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_32:
1402 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_32;
1404 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_16:
1405 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_16;
1407 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_8:
1408 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_8;
1410 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_1_4:
1411 parm_u_ofdm_guard_interval = GUARD_INTERVAL_1_4;
1414 case eDVBFrontendParametersTerrestrial::GuardInterval::GI_Auto:
1415 parm_u_ofdm_guard_interval = GUARD_INTERVAL_AUTO;
1418 switch (feparm.hierarchy)
1420 case eDVBFrontendParametersTerrestrial::Hierarchy::H1:
1421 parm_u_ofdm_hierarchy_information = HIERARCHY_1;
1423 case eDVBFrontendParametersTerrestrial::Hierarchy::H2:
1424 parm_u_ofdm_hierarchy_information = HIERARCHY_2;
1426 case eDVBFrontendParametersTerrestrial::Hierarchy::H4:
1427 parm_u_ofdm_hierarchy_information = HIERARCHY_4;
1430 case eDVBFrontendParametersTerrestrial::Hierarchy::HAuto:
1431 parm_u_ofdm_hierarchy_information = HIERARCHY_AUTO;
1437 RESULT eDVBFrontend::tune(const iDVBFrontendParameters &where)
1439 eDebug("(%d)tune", m_fe);
1448 m_sec_sequence.clear();
1454 eDVBFrontendParametersSatellite feparm;
1455 if (where.getDVBS(feparm))
1457 eDebug("no dvbs data!");
1460 res=prepare_sat(feparm);
1465 eDVBFrontendParametersCable feparm;
1466 if (where.getDVBC(feparm))
1468 res=prepare_cable(feparm);
1470 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1475 eDVBFrontendParametersTerrestrial feparm;
1476 if (where.getDVBT(feparm))
1478 eDebug("no -T data");
1481 res=prepare_terrestrial(feparm);
1483 m_sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
1488 if (!res) // prepare ok
1490 m_tuneTimer->start(0,true);
1492 m_sec_sequence.current() = m_sec_sequence.begin();
1494 if (m_state != stateTuning)
1497 m_state = stateTuning;
1498 m_stateChanged(this);
1505 RESULT eDVBFrontend::connectStateChange(const Slot1<void,iDVBFrontend*> &stateChange, ePtr<eConnection> &connection)
1507 connection = new eConnection(this, m_stateChanged.connect(stateChange));
1511 RESULT eDVBFrontend::setVoltage(int voltage)
1513 if (m_type != feSatellite)
1515 #if HAVE_DVB_API_VERSION < 3
1518 bool increased=false;
1519 fe_sec_voltage_t vlt;
1521 m_curVoltage=voltage;
1525 for (int i=0; i < 3; ++i) // reset diseqc
1527 vlt = SEC_VOLTAGE_OFF;
1530 #if HAVE_DVB_API_VERSION < 3
1531 vlt = SEC_VOLTAGE_13_5;
1537 vlt = SEC_VOLTAGE_13;
1540 #if HAVE_DVB_API_VERSION < 3
1541 vlt = SEC_VOLTAGE_18_5;
1547 vlt = SEC_VOLTAGE_18;
1552 #if HAVE_DVB_API_VERSION < 3
1553 return ::ioctl(m_secfd, SEC_SET_VOLTAGE, vlt);
1555 if (::ioctl(m_fd, FE_ENABLE_HIGH_LNB_VOLTAGE, increased) < 0)
1556 perror("FE_ENABLE_HIGH_LNB_VOLTAGE");
1557 return ::ioctl(m_fd, FE_SET_VOLTAGE, vlt);
1561 RESULT eDVBFrontend::getState(int &state)
1567 RESULT eDVBFrontend::setTone(int t)
1569 if (m_type != feSatellite)
1571 #if HAVE_DVB_API_VERSION < 3
1574 fe_sec_tone_mode_t tone;
1583 tone = SEC_TONE_OFF;
1588 #if HAVE_DVB_API_VERSION < 3
1589 return ::ioctl(m_secfd, SEC_SET_TONE, tone);
1591 return ::ioctl(m_fd, FE_SET_TONE, tone);
1595 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_MASTER_CMD)
1596 #define SEC_DISEQC_SEND_MASTER_CMD _IOW('o', 97, struct secCommand *)
1599 RESULT eDVBFrontend::sendDiseqc(const eDVBDiseqcCommand &diseqc)
1601 #if HAVE_DVB_API_VERSION < 3
1602 struct secCommand cmd;
1603 cmd.type = SEC_CMDTYPE_DISEQC_RAW;
1604 cmd.u.diseqc.cmdtype = diseqc.data[0];
1605 cmd.u.diseqc.addr = diseqc.data[1];
1606 cmd.u.diseqc.cmd = diseqc.data[2];
1607 cmd.u.diseqc.numParams = diseqc.len-3;
1608 memcpy(cmd.u.diseqc.params, diseqc.data+3, diseqc.len-3);
1609 if (::ioctl(m_secfd, SEC_DISEQC_SEND_MASTER_CMD, &cmd))
1611 struct dvb_diseqc_master_cmd cmd;
1612 memcpy(cmd.msg, diseqc.data, diseqc.len);
1613 cmd.msg_len = diseqc.len;
1614 if (::ioctl(m_fd, FE_DISEQC_SEND_MASTER_CMD, &cmd))
1620 #if HAVE_DVB_API_VERSION < 3 && !defined(SEC_DISEQC_SEND_BURST)
1621 #define SEC_DISEQC_SEND_BURST _IO('o', 96)
1623 RESULT eDVBFrontend::sendToneburst(int burst)
1625 #if HAVE_DVB_API_VERSION < 3
1626 secMiniCmd cmd = SEC_MINI_NONE;
1628 fe_sec_mini_cmd_t cmd = SEC_MINI_A;
1630 if ( burst == eDVBSatelliteDiseqcParameters::A )
1632 else if ( burst == eDVBSatelliteDiseqcParameters::B )
1634 #if HAVE_DVB_API_VERSION < 3
1635 if (::ioctl(m_secfd, SEC_DISEQC_SEND_BURST, cmd))
1638 if (::ioctl(m_fd, FE_DISEQC_SEND_BURST, cmd))
1644 RESULT eDVBFrontend::setSEC(iDVBSatelliteEquipmentControl *sec)
1650 RESULT eDVBFrontend::setSecSequence(const eSecCommandList &list)
1652 m_sec_sequence = list;
1656 RESULT eDVBFrontend::getData(int num, int &data)
1658 if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1666 RESULT eDVBFrontend::setData(int num, int val)
1668 if ( num < (int)(sizeof(m_data)/sizeof(int)) )
1676 int eDVBFrontend::isCompatibleWith(ePtr<iDVBFrontendParameters> &feparm)
1679 if (feparm->getSystem(type) || type != m_type)
1682 if (m_type == eDVBFrontend::feSatellite)
1685 eDVBFrontendParametersSatellite sat_parm;
1686 ASSERT(!feparm->getDVBS(sat_parm));
1687 return m_sec->canTune(sat_parm, this, 1 << m_fe);