added nims to the about box
[enigma2.git] / lib / dvb / sec.cpp
1 #include <config.h>
2 #include <lib/dvb/sec.h>
3 #include <lib/dvb/rotor_calc.h>
4
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
10 #else
11 #define INVERSION inversion
12 #define FREQUENCY frequency
13 #define FEC_INNER fec_inner
14 #define SYMBOLRATE symbol_rate
15 #endif
16 #include <lib/base/eerror.h>
17
18 DEFINE_REF(eDVBSatelliteEquipmentControl);
19
20 eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl()
21 {
22         m_lnblist.push_back(eDVBSatelliteLNBParameters());
23         eDVBSatelliteLNBParameters &lnb_ref = m_lnblist.front();
24         eDVBSatelliteDiseqcParameters &diseqc_ref = lnb_ref.m_diseqc_parameters;
25         eDVBSatelliteParameters &astra1 = lnb_ref.m_satellites[192];
26         eDVBSatelliteSwitchParameters &switch_ref = astra1.m_switch_parameters;
27
28         lnb_ref.m_lof_hi = 10600000;
29         lnb_ref.m_lof_lo = 9750000;
30         lnb_ref.m_lof_threshold = 11700000;
31
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 = 0;
40
41         switch_ref.m_22khz_signal = eDVBSatelliteSwitchParameters::HILO;
42         switch_ref.m_voltage_mode = eDVBSatelliteSwitchParameters::HV;
43 }
44
45 RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, eDVBFrontendParametersSatellite &sat)
46 {
47         std::list<eDVBSatelliteLNBParameters>::iterator it = m_lnblist.begin();
48         for (;it != m_lnblist.end(); ++it )
49         {
50                 eDVBSatelliteLNBParameters &lnb_param = *it;
51                 eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
52                 std::map<int, eDVBSatelliteParameters>::iterator sit =
53                         lnb_param.m_satellites.find(sat.orbital_position);
54                 if ( sit != lnb_param.m_satellites.end())
55                 {
56
57                         eDVBSatelliteSwitchParameters &sw_param = sit->second.m_switch_parameters;
58                         eDVBSatelliteRotorParameters &rotor_param = sit->second.m_rotor_parameters;
59                         int hi=0,
60                                 voltage = iDVBFrontend::voltageOff,
61                                 tone = iDVBFrontend::toneOff,
62                                 csw = di_param.m_committed_cmd,
63                                 ucsw = di_param.m_uncommitted_cmd,
64                                 toneburst = di_param.m_toneburst_param,
65                                 lastcsw = -1,
66                                 lastucsw = -1,
67                                 lastToneburst = -1,
68                                 lastRotorCmd = -1,
69                                 curRotorPos = -1;
70
71                         frontend.getData(0, lastcsw);
72                         frontend.getData(1, lastucsw);
73                         frontend.getData(2, lastToneburst);
74                         frontend.getData(5, lastRotorCmd);
75                         frontend.getData(6, curRotorPos);
76
77                         if ( sat.frequency > lnb_param.m_lof_threshold )
78                                 hi = 1;
79
80                         if (hi)
81                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_hi;
82                         else
83                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_lo;
84
85                         parm.INVERSION = (!sat.inversion) ? INVERSION_ON : INVERSION_OFF;
86
87                         switch (sat.fec)
88                         {
89                                 default:
90                                 case eDVBFrontendParametersSatellite::FEC::fNone:
91                                         eDebug("no fec set.. assume auto");
92                                 case eDVBFrontendParametersSatellite::FEC::fAuto:
93                                         parm.u.qpsk.FEC_INNER = FEC_AUTO;
94                                         break;
95                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
96                                         parm.u.qpsk.FEC_INNER = FEC_1_2;
97                                         break;
98                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
99                                         parm.u.qpsk.FEC_INNER = FEC_2_3;
100                                         break;
101                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
102                                         parm.u.qpsk.FEC_INNER = FEC_3_4;
103                                         break;
104                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
105                                         parm.u.qpsk.FEC_INNER = FEC_5_6;
106                                         break;
107                                 case eDVBFrontendParametersSatellite::FEC::f7_8: 
108                                         parm.u.qpsk.FEC_INNER = FEC_7_8;
109                                         break;
110                         }
111
112                         parm.u.qpsk.SYMBOLRATE = sat.symbol_rate;
113
114                         if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_14V
115                                 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Vertical
116                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
117                                 voltage = iDVBFrontend::voltage13;
118                         else if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_18V
119                                 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal
120                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
121                                 voltage = iDVBFrontend::voltage18;
122
123                         if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::ON)
124                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && hi ) )
125                                 tone = iDVBFrontend::toneOn;
126                         else if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::OFF)
127                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && !hi ) )
128                                 tone = iDVBFrontend::toneOff;
129
130                         eSecCommandList sec_sequence;
131                         bool setVoltage=true;
132
133                         if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
134                         {
135                                 if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
136                                 {
137                                         csw = 0xF0 | (csw << 2);
138                                         if (hi)
139                                                 csw |= 1;
140                                         if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
141                                                 csw |= 2;
142                                 }
143
144                                 bool send_csw =
145                                         (di_param.m_committed_cmd != eDVBSatelliteDiseqcParameters::SENDNO);
146                                 bool changed_csw = send_csw && csw != lastcsw;
147
148                                 bool send_ucsw =
149                                         (di_param.m_uncommitted_cmd && di_param.m_diseqc_mode > eDVBSatelliteDiseqcParameters::V1_0);
150                                 bool changed_ucsw = send_ucsw && ucsw != lastucsw;
151
152                                 bool send_burst =
153                                         (di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO);
154                                 bool changed_burst = send_burst && toneburst != lastToneburst;
155
156                                 bool send_diseqc = changed_ucsw;
157                                 if (!send_diseqc)
158                                         send_diseqc = changed_burst && (send_ucsw || send_csw);
159                                 if (!send_diseqc)
160                                 {
161                                         send_diseqc = changed_csw;
162                                         if ( send_diseqc && di_param.m_use_fast && (csw & 0xF0) && (lastcsw & 0xF0) && ((csw / 4) == (lastcsw / 4)) )
163                                                 send_diseqc = false;
164                                 }
165
166                                 if ( send_diseqc || changed_burst )
167                                 {
168                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
169                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
170                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );  // standard says 15 msek here
171                                         setVoltage=false;
172                                 }
173
174                                 if ( send_diseqc )
175                                 {
176                                         int loops=0;
177
178                                         if ( send_csw )
179                                                 ++loops;
180                                         if ( send_ucsw )
181                                                 ++loops;
182
183                                         for ( int i=0; i < di_param.m_repeats; ++i )
184                                                 loops *= 2;
185
186                                         for ( int i = 0; i < loops;)  // fill commands...
187                                         {
188                                                 eDVBDiseqcCommand diseqc;
189                                                 diseqc.len = 4;
190                                                 diseqc.data[0] = i ? 0xE1 : 0xE0;
191                                                 diseqc.data[1] = 0x10;
192
193                                                 if ( !send_csw || (di_param.m_swap_cmds && send_ucsw) )
194                                                 {
195                                                         diseqc.data[2] = 0x39;
196                                                         diseqc.data[3] = ucsw;
197                                                 }
198                                                 else
199                                                 {
200                                                         diseqc.data[2] = 0x38;
201                                                         diseqc.data[3] = csw;
202                                                 }
203                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
204
205                                                 i++;
206                                                 if ( i < loops )
207                                                 {
208                                                         int cmd=0;
209                                                         if (diseqc.data[2] == 0x38 && send_ucsw)
210                                                                 cmd=0x39;
211                                                         else if (diseqc.data[2] == 0x39 && send_csw)
212                                                                 cmd=0x38;
213                                                         if (cmd)
214                                                         {
215                                                                 static int delay = (120 - 54) / 2;  // standard says 100msek between two repeated commands
216                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
217                                                                 diseqc.data[2]=cmd;
218                                                                 diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
219                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
220                                                                 ++i;
221                                                                 if ( i < loops )
222                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
223                                                                 else
224                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
225                                                         }
226                                                         else  // delay 120msek when no command is in repeat gap
227                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 120) );
228                                                 }
229                                                 else
230                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
231
232                                                 frontend.setData(0, csw);
233                                                 frontend.setData(1, ucsw);
234                                         }
235                                 }
236                                 if ( (changed_burst || send_diseqc) && di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO )
237                                 {
238                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
239                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
240                                         frontend.setData(2, di_param.m_toneburst_param);
241                                 }
242                                 if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )
243                                 {
244                                         int RotorCmd=0;
245                                         bool useGotoXX = false;
246
247                                         std::map<int,int,eDVBSatelliteRotorParameters::Orbital_Position_Compare>::iterator it =
248                                                 rotor_param.m_rotor_position_table.find( sat.orbital_position );
249
250                                         if (it != rotor_param.m_rotor_position_table.end())  // position for selected sat found ?
251                                                 RotorCmd=it->second;
252                                         else  // entry not in table found
253                                         {
254                                                 eDebug("Entry for %d,%d° not in Rotor Table found... i try gotoXX°", sat.orbital_position / 10, sat.orbital_position % 10 );
255                                                 useGotoXX = true;
256
257                                                 int satDir = sat.orbital_position < 0 ?
258                                                         eDVBSatelliteRotorParameters::WEST :
259                                                         eDVBSatelliteRotorParameters::EAST;
260
261                                                 double  SatLon = abs(sat.orbital_position)/10.00,
262                                                                 SiteLat = rotor_param.m_gotoxx_parameters.m_latitude,
263                                                                 SiteLon = rotor_param.m_gotoxx_parameters.m_longitude;
264
265                                                 if ( rotor_param.m_gotoxx_parameters.m_la_direction == eDVBSatelliteRotorParameters::SOUTH )
266                                                         SiteLat = -SiteLat;
267
268                                                 if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
269                                                         SiteLon = 360 - SiteLon;
270
271                                                 if (satDir == eDVBSatelliteRotorParameters::WEST )
272                                                         SatLon = 360 - SatLon;
273
274                                                 eDebug("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
275                                                 double satHourAngle =
276                                                         calcSatHourangle( SatLon, SiteLat, SiteLon );
277                                                 eDebug("PolarmountHourAngle=%lf", satHourAngle );
278
279                                                 static int gotoXTable[10] =
280                                                         { 0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E };
281
282                                                 if (SiteLat >= 0) // Northern Hemisphere
283                                                 {
284                                                         int tmp=(int)round( fabs( 180 - satHourAngle ) * 10.0 );
285                                                         RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
286
287                                                         if (satHourAngle < 180) // the east
288                                                                 RotorCmd |= 0xE000;
289                                                         else                                    // west
290                                                                 RotorCmd |= 0xD000;
291                                                 }
292                                                 else // Southern Hemisphere
293                                                 {
294                                                         if (satHourAngle < 180) // the east
295                                                         {
296                                                                 int tmp=(int)round( fabs( satHourAngle ) * 10.0 );
297                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
298                                                                 RotorCmd |= 0xD000;
299                                                         }
300                                                         else                                    // west
301                                                         {          
302                                                                 int tmp=(int)round( fabs( 360 - satHourAngle ) * 10.0 );
303                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
304                                                                 RotorCmd |= 0xE000;
305                                                         }
306                                                 }
307                                                 eDebug("RotorCmd = %04x", RotorCmd);
308                                         }
309                                         if ( RotorCmd != lastRotorCmd )
310                                         {
311                                                 eDVBDiseqcCommand diseqc;
312                                                 diseqc.data[0] = 0xE0;
313                                                 diseqc.data[1] = 0x31;          // positioner
314                                                 if ( useGotoXX )
315                                                 {
316                                                         diseqc.len = 5;
317                                                         diseqc.data[2] = 0x6E;  // drive to angular position
318                                                         diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
319                                                         diseqc.data[4] = RotorCmd & 0xFF;
320                                                 }
321                                                 else
322                                                 {
323                                                         diseqc.len = 4;
324                                                         diseqc.data[2] = 0x6B;  // goto stored sat position
325                                                         diseqc.data[3] = RotorCmd;
326                                                 }
327                                                 if ( rotor_param.m_inputpower_parameters.m_use )
328                                                 { // use measure rotor input power to detect rotor state
329                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage18) ); // always turn with high voltage
330                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50sec after voltage change
331                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER) );
332                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
333                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 8) );  // 2 seconds rotor start timout
334                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 250) );
335                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
336                                                         eSecCommand::rotor cmd;
337                                                         cmd.direction=1;  // check for running rotor
338                                                         cmd.deltaA=rotor_param.m_inputpower_parameters.m_threshold;
339                                                         cmd.steps=+3;
340                                                         cmd.okcount=0;
341                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
342                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +8 ) );
343                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );
344                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 240) );  // 1 minute running timeout
345                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 250) );
346                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
347                                                         cmd.direction=0;  // check for stopped rotor
348                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
349                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +2 ) );
350                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );
351                                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
352                                                         frontend.setData(3, RotorCmd);
353                                                         frontend.setData(4, sat.orbital_position);
354                                                 }
355                                                 else
356                                                         eFatal("rotor turning without inputpowermeasure not implemented yet");
357                                         }
358                                 }
359                         }
360
361                         if ( setVoltage )
362                         {
363                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
364                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 10) );
365                         }
366
367                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
368                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );
369
370                         frontend.setSecSequence(sec_sequence);
371
372                         return 0;
373                 }
374         }
375
376         eDebug("not found satellite configuration for orbital position (%d)", sat.orbital_position );
377
378         return -1;
379 }
380