2 #include <lib/dvb/sec.h>
3 #include <lib/dvb/rotor_calc.h>
5 #if HAVE_DVB_API_VERSION < 3
6 #define INVERSION Inversion
7 #define FREQUENCY Frequency
8 #define FEC_INNER FEC_inner
9 #define SYMBOLRATE SymbolRate
11 #define INVERSION inversion
12 #define FREQUENCY frequency
13 #define FEC_INNER fec_inner
14 #define SYMBOLRATE symbol_rate
16 #include <lib/base/eerror.h>
18 DEFINE_REF(eDVBSatelliteEquipmentControl);
20 eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl()
22 m_lnblist.push_back(eDVBSatelliteLNBParameters());
23 eDVBSatelliteLNBParameters &lnb_ref = m_lnblist.front();
24 eDVBSatelliteParameters &astra1 = lnb_ref.m_satellites[192];
25 eDVBSatelliteDiseqcParameters &diseqc_ref = astra1.m_diseqc_parameters;
26 eDVBSatelliteSwitchParameters &switch_ref = astra1.m_switch_parameters;
28 lnb_ref.m_lof_hi = 10600000;
29 lnb_ref.m_lof_lo = 9750000;
30 lnb_ref.m_lof_threshold = 11700000;
32 diseqc_ref.m_diseqc_mode = eDVBSatelliteDiseqcParameters::V1_0;
33 diseqc_ref.m_committed_cmd = eDVBSatelliteDiseqcParameters::AA;
34 diseqc_ref.m_repeats = 0;
35 diseqc_ref.m_seq_repeat = false;
36 diseqc_ref.m_swap_cmds = false;
37 diseqc_ref.m_toneburst_param = eDVBSatelliteDiseqcParameters::NO;
38 diseqc_ref.m_uncommitted_cmd = 0;
39 diseqc_ref.m_use_fast = 1;
41 switch_ref.m_22khz_signal = eDVBSatelliteSwitchParameters::HILO;
42 switch_ref.m_voltage_mode = eDVBSatelliteSwitchParameters::HV;
45 RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, eDVBFrontendParametersSatellite &sat)
47 std::list<eDVBSatelliteLNBParameters>::iterator it = m_lnblist.begin();
48 for (;it != m_lnblist.end(); ++it )
50 eDVBSatelliteLNBParameters &lnb_param = *it;
51 std::map<int, eDVBSatelliteParameters>::iterator sit =
52 lnb_param.m_satellites.find(sat.orbital_position);
53 if ( sit != lnb_param.m_satellites.end())
55 eDVBSatelliteDiseqcParameters &di_param = sit->second.m_diseqc_parameters;
56 eDVBSatelliteSwitchParameters &sw_param = sit->second.m_switch_parameters;
57 eDVBSatelliteRotorParameters &rotor_param = sit->second.m_rotor_parameters;
59 voltage = iDVBFrontend::voltageOff,
60 tone = iDVBFrontend::toneOff,
61 csw = di_param.m_committed_cmd,
62 ucsw = di_param.m_uncommitted_cmd,
63 toneburst = di_param.m_toneburst_param,
70 frontend.getData(0, lastcsw);
71 frontend.getData(1, lastucsw);
72 frontend.getData(2, lastToneburst);
73 frontend.getData(5, lastRotorCmd);
74 frontend.getData(6, curRotorPos);
76 if ( sat.frequency > lnb_param.m_lof_threshold )
80 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_hi;
82 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_lo;
84 parm.INVERSION = (!sat.inversion) ? INVERSION_ON : INVERSION_OFF;
89 case eDVBFrontendParametersSatellite::FEC::fNone:
90 eDebug("no fec set.. assume auto");
91 case eDVBFrontendParametersSatellite::FEC::fAuto:
92 parm.u.qpsk.FEC_INNER = FEC_AUTO;
94 case eDVBFrontendParametersSatellite::FEC::f1_2:
95 parm.u.qpsk.FEC_INNER = FEC_1_2;
97 case eDVBFrontendParametersSatellite::FEC::f2_3:
98 parm.u.qpsk.FEC_INNER = FEC_2_3;
100 case eDVBFrontendParametersSatellite::FEC::f3_4:
101 parm.u.qpsk.FEC_INNER = FEC_3_4;
103 case eDVBFrontendParametersSatellite::FEC::f5_6:
104 parm.u.qpsk.FEC_INNER = FEC_5_6;
106 case eDVBFrontendParametersSatellite::FEC::f7_8:
107 parm.u.qpsk.FEC_INNER = FEC_7_8;
111 parm.u.qpsk.SYMBOLRATE = sat.symbol_rate;
113 if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_14V
114 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Vertical
115 && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV ) )
116 voltage = iDVBFrontend::voltage13;
117 else if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_18V
118 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal
119 && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV ) )
120 voltage = iDVBFrontend::voltage18;
122 if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::ON)
123 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && hi ) )
124 tone = iDVBFrontend::toneOn;
125 else if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::OFF)
126 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && !hi ) )
127 tone = iDVBFrontend::toneOff;
129 eSecCommandList sec_sequence;
130 bool setVoltage=true;
132 if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
134 if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
136 csw = 0xF0 | (csw << 2);
139 if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
144 (di_param.m_committed_cmd != eDVBSatelliteDiseqcParameters::SENDNO);
145 bool changed_csw = send_csw && csw != lastcsw;
148 (di_param.m_uncommitted_cmd && di_param.m_diseqc_mode > eDVBSatelliteDiseqcParameters::V1_0);
149 bool changed_ucsw = send_ucsw && ucsw != lastucsw;
152 (di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO);
153 bool changed_burst = send_burst && toneburst != lastToneburst;
155 bool send_diseqc = changed_ucsw;
157 send_diseqc = changed_burst && (send_ucsw || send_csw);
160 send_diseqc = changed_csw;
161 if ( send_diseqc && di_param.m_use_fast && (csw & 0xF0) && (lastcsw & 0xF0) && ((csw / 4) == (lastcsw / 4)) )
165 if ( send_diseqc || changed_burst )
167 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
168 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
169 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) ); // standard says 15 msek here
182 for ( int i=0; i < di_param.m_repeats; ++i )
185 for ( int i = 0; i < loops;) // fill commands...
187 eDVBDiseqcCommand diseqc;
189 diseqc.data[0] = i ? 0xE1 : 0xE0;
190 diseqc.data[1] = 0x10;
192 if ( !send_csw || (di_param.m_swap_cmds && send_ucsw) )
194 diseqc.data[2] = 0x39;
195 diseqc.data[3] = ucsw;
199 diseqc.data[2] = 0x38;
200 diseqc.data[3] = csw;
202 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
208 if (diseqc.data[2] == 0x38 && send_ucsw)
210 else if (diseqc.data[2] == 0x39 && send_csw)
214 static int delay = (120 - 54) / 2; // standard says 100msek between two repeated commands
215 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
217 diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
218 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
221 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
223 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
225 else // delay 120msek when no command is in repeat gap
226 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 120) );
229 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
231 frontend.setData(0, csw);
232 frontend.setData(1, ucsw);
235 if ( (changed_burst || send_diseqc) && di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO )
237 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
238 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
239 frontend.setData(2, di_param.m_toneburst_param);
241 if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )
244 bool useGotoXX = false;
246 std::map<int,int,eDVBSatelliteRotorParameters::Orbital_Position_Compare>::iterator it =
247 rotor_param.m_rotor_position_table.find( sat.orbital_position );
249 if (it != rotor_param.m_rotor_position_table.end()) // position for selected sat found ?
251 else // entry not in table found
253 eDebug("Entry for %d,%d° not in Rotor Table found... i try gotoXX°", sat.orbital_position / 10, sat.orbital_position % 10 );
256 int satDir = sat.orbital_position < 0 ?
257 eDVBSatelliteRotorParameters::WEST :
258 eDVBSatelliteRotorParameters::EAST;
260 double SatLon = abs(sat.orbital_position)/10.00,
261 SiteLat = rotor_param.m_gotoxx_parameters.m_latitude,
262 SiteLon = rotor_param.m_gotoxx_parameters.m_longitude;
264 if ( rotor_param.m_gotoxx_parameters.m_la_direction == eDVBSatelliteRotorParameters::SOUTH )
267 if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
268 SiteLon = 360 - SiteLon;
270 if (satDir == eDVBSatelliteRotorParameters::WEST )
271 SatLon = 360 - SatLon;
273 eDebug("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
274 double satHourAngle =
275 calcSatHourangle( SatLon, SiteLat, SiteLon );
276 eDebug("PolarmountHourAngle=%lf", satHourAngle );
278 static int gotoXTable[10] =
279 { 0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E };
281 if (SiteLat >= 0) // Northern Hemisphere
283 int tmp=(int)round( fabs( 180 - satHourAngle ) * 10.0 );
284 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
286 if (satHourAngle < 180) // the east
291 else // Southern Hemisphere
293 if (satHourAngle < 180) // the east
295 int tmp=(int)round( fabs( satHourAngle ) * 10.0 );
296 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
301 int tmp=(int)round( fabs( 360 - satHourAngle ) * 10.0 );
302 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
306 eDebug("RotorCmd = %04x", RotorCmd);
308 if ( RotorCmd != lastRotorCmd )
310 eDVBDiseqcCommand diseqc;
311 diseqc.data[0] = 0xE0;
312 diseqc.data[1] = 0x31; // positioner
316 diseqc.data[2] = 0x6E; // drive to angular position
317 diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
318 diseqc.data[4] = RotorCmd & 0xFF;
323 diseqc.data[2] = 0x6B; // goto stored sat position
324 diseqc.data[3] = RotorCmd;
326 if ( rotor_param.m_inputpower_parameters.m_use )
327 { // use measure rotor input power to detect rotor state
328 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage18) ); // always turn with high voltage
329 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) ); // wait 50sec after voltage change
330 sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER) );
331 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
332 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 8) ); // 2 seconds rotor start timout
333 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 250) );
334 sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
335 eSecCommand::rotor cmd;
336 cmd.direction=1; // check for running rotor
337 cmd.deltaA=rotor_param.m_inputpower_parameters.m_threshold;
340 sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
341 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +8 ) );
342 sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );
343 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 240) ); // 1 minute running timeout
344 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 250) );
345 sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
346 cmd.direction=0; // check for stopped rotor
347 sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
348 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +2 ) );
349 sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );
350 sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
351 frontend.setData(3, RotorCmd);
352 frontend.setData(4, sat.orbital_position);
355 eFatal("rotor turning without inputpowermeasure not implemented yet");
362 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
363 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 10) );
366 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
367 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );
369 frontend.setSecSequence(sec_sequence);
375 eDebug("not found satellite configuration for orbital position (%d)", sat.orbital_position );