the epgcache can now handle more then one channel(transponder),
[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         eDVBSatelliteParameters &astra1 = lnb_ref.m_satellites[192];
25         eDVBSatelliteDiseqcParameters &diseqc_ref = astra1.m_diseqc_parameters;
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 = 1;
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                 std::map<int, eDVBSatelliteParameters>::iterator sit =
52                         lnb_param.m_satellites.find(sat.orbital_position);
53                 if ( sit != lnb_param.m_satellites.end())
54                 {
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;
58                         int hi=0,
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,
64                                 lastcsw = -1,
65                                 lastucsw = -1,
66                                 lastToneburst = -1,
67                                 lastRotorCmd = -1,
68                                 curRotorPos = -1;
69
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);
75
76                         if ( sat.frequency > lnb_param.m_lof_threshold )
77                                 hi = 1;
78
79                         if (hi)
80                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_hi;
81                         else
82                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_lo;
83
84                         parm.INVERSION = (!sat.inversion) ? INVERSION_ON : INVERSION_OFF;
85
86                         switch (sat.fec)
87                         {
88                                 default:
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;
93                                         break;
94                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
95                                         parm.u.qpsk.FEC_INNER = FEC_1_2;
96                                         break;
97                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
98                                         parm.u.qpsk.FEC_INNER = FEC_2_3;
99                                         break;
100                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
101                                         parm.u.qpsk.FEC_INNER = FEC_3_4;
102                                         break;
103                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
104                                         parm.u.qpsk.FEC_INNER = FEC_5_6;
105                                         break;
106                                 case eDVBFrontendParametersSatellite::FEC::f7_8: 
107                                         parm.u.qpsk.FEC_INNER = FEC_7_8;
108                                         break;
109                         }
110
111                         parm.u.qpsk.SYMBOLRATE = sat.symbol_rate;
112
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;
121
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;
128
129                         eSecCommandList sec_sequence;
130                         bool setVoltage=true;
131
132                         if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
133                         {
134                                 if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
135                                 {
136                                         csw = 0xF0 | (csw << 2);
137                                         if (hi)
138                                                 csw |= 1;
139                                         if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
140                                                 csw |= 2;
141                                 }
142
143                                 bool send_csw =
144                                         (di_param.m_committed_cmd != eDVBSatelliteDiseqcParameters::SENDNO);
145                                 bool changed_csw = send_csw && csw != lastcsw;
146
147                                 bool send_ucsw =
148                                         (di_param.m_uncommitted_cmd && di_param.m_diseqc_mode > eDVBSatelliteDiseqcParameters::V1_0);
149                                 bool changed_ucsw = send_ucsw && ucsw != lastucsw;
150
151                                 bool send_burst =
152                                         (di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO);
153                                 bool changed_burst = send_burst && toneburst != lastToneburst;
154
155                                 bool send_diseqc = changed_ucsw;
156                                 if (!send_diseqc)
157                                         send_diseqc = changed_burst && (send_ucsw || send_csw);
158                                 if (!send_diseqc)
159                                 {
160                                         send_diseqc = changed_csw;
161                                         if ( send_diseqc && di_param.m_use_fast && (csw & 0xF0) && (lastcsw & 0xF0) && ((csw / 4) == (lastcsw / 4)) )
162                                                 send_diseqc = false;
163                                 }
164
165                                 if ( send_diseqc || changed_burst )
166                                 {
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
170                                         setVoltage=false;
171                                 }
172
173                                 if ( send_diseqc )
174                                 {
175                                         int loops=0;
176
177                                         if ( send_csw )
178                                                 ++loops;
179                                         if ( send_ucsw )
180                                                 ++loops;
181
182                                         for ( int i=0; i < di_param.m_repeats; ++i )
183                                                 loops *= 2;
184
185                                         for ( int i = 0; i < loops;)  // fill commands...
186                                         {
187                                                 eDVBDiseqcCommand diseqc;
188                                                 diseqc.len = 4;
189                                                 diseqc.data[0] = i ? 0xE1 : 0xE0;
190                                                 diseqc.data[1] = 0x10;
191
192                                                 if ( !send_csw || (di_param.m_swap_cmds && send_ucsw) )
193                                                 {
194                                                         diseqc.data[2] = 0x39;
195                                                         diseqc.data[3] = ucsw;
196                                                 }
197                                                 else
198                                                 {
199                                                         diseqc.data[2] = 0x38;
200                                                         diseqc.data[3] = csw;
201                                                 }
202                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
203
204                                                 i++;
205                                                 if ( i < loops )
206                                                 {
207                                                         int cmd=0;
208                                                         if (diseqc.data[2] == 0x38 && send_ucsw)
209                                                                 cmd=0x39;
210                                                         else if (diseqc.data[2] == 0x39 && send_csw)
211                                                                 cmd=0x38;
212                                                         if (cmd)
213                                                         {
214                                                                 static int delay = (120 - 54) / 2;  // standard says 100msek between two repeated commands
215                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
216                                                                 diseqc.data[2]=cmd;
217                                                                 diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
218                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
219                                                                 ++i;
220                                                                 if ( i < loops )
221                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
222                                                                 else
223                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
224                                                         }
225                                                         else  // delay 120msek when no command is in repeat gap
226                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 120) );
227                                                 }
228                                                 else
229                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 30) );
230
231                                                 frontend.setData(0, csw);
232                                                 frontend.setData(1, ucsw);
233                                         }
234                                 }
235                                 if ( (changed_burst || send_diseqc) && di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO )
236                                 {
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);
240                                 }
241                                 if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )
242                                 {
243                                         int RotorCmd=0;
244                                         bool useGotoXX = false;
245
246                                         std::map<int,int,eDVBSatelliteRotorParameters::Orbital_Position_Compare>::iterator it =
247                                                 rotor_param.m_rotor_position_table.find( sat.orbital_position );
248
249                                         if (it != rotor_param.m_rotor_position_table.end())  // position for selected sat found ?
250                                                 RotorCmd=it->second;
251                                         else  // entry not in table found
252                                         {
253                                                 eDebug("Entry for %d,%d° not in Rotor Table found... i try gotoXX°", sat.orbital_position / 10, sat.orbital_position % 10 );
254                                                 useGotoXX = true;
255
256                                                 int satDir = sat.orbital_position < 0 ?
257                                                         eDVBSatelliteRotorParameters::WEST :
258                                                         eDVBSatelliteRotorParameters::EAST;
259
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;
263
264                                                 if ( rotor_param.m_gotoxx_parameters.m_la_direction == eDVBSatelliteRotorParameters::SOUTH )
265                                                         SiteLat = -SiteLat;
266
267                                                 if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
268                                                         SiteLon = 360 - SiteLon;
269
270                                                 if (satDir == eDVBSatelliteRotorParameters::WEST )
271                                                         SatLon = 360 - SatLon;
272
273                                                 eDebug("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
274                                                 double satHourAngle =
275                                                         calcSatHourangle( SatLon, SiteLat, SiteLon );
276                                                 eDebug("PolarmountHourAngle=%lf", satHourAngle );
277
278                                                 static int gotoXTable[10] =
279                                                         { 0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E };
280
281                                                 if (SiteLat >= 0) // Northern Hemisphere
282                                                 {
283                                                         int tmp=(int)round( fabs( 180 - satHourAngle ) * 10.0 );
284                                                         RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
285
286                                                         if (satHourAngle < 180) // the east
287                                                                 RotorCmd |= 0xE000;
288                                                         else                                    // west
289                                                                 RotorCmd |= 0xD000;
290                                                 }
291                                                 else // Southern Hemisphere
292                                                 {
293                                                         if (satHourAngle < 180) // the east
294                                                         {
295                                                                 int tmp=(int)round( fabs( satHourAngle ) * 10.0 );
296                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
297                                                                 RotorCmd |= 0xD000;
298                                                         }
299                                                         else                                    // west
300                                                         {          
301                                                                 int tmp=(int)round( fabs( 360 - satHourAngle ) * 10.0 );
302                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
303                                                                 RotorCmd |= 0xE000;
304                                                         }
305                                                 }
306                                                 eDebug("RotorCmd = %04x", RotorCmd);
307                                         }
308                                         if ( RotorCmd != lastRotorCmd )
309                                         {
310                                                 eDVBDiseqcCommand diseqc;
311                                                 diseqc.data[0] = 0xE0;
312                                                 diseqc.data[1] = 0x31;          // positioner
313                                                 if ( useGotoXX )
314                                                 {
315                                                         diseqc.len = 5;
316                                                         diseqc.data[2] = 0x6E;  // drive to angular position
317                                                         diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
318                                                         diseqc.data[4] = RotorCmd & 0xFF;
319                                                 }
320                                                 else
321                                                 {
322                                                         diseqc.len = 4;
323                                                         diseqc.data[2] = 0x6B;  // goto stored sat position
324                                                         diseqc.data[3] = RotorCmd;
325                                                 }
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;
338                                                         cmd.steps=+3;
339                                                         cmd.okcount=0;
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);
353                                                 }
354                                                 else
355                                                         eFatal("rotor turning without inputpowermeasure not implemented yet");
356                                         }
357                                 }
358                         }
359
360                         if ( setVoltage )
361                         {
362                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
363                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 10) );
364                         }
365
366                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
367                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );
368
369                         frontend.setSecSequence(sec_sequence);
370
371                         return 0;
372                 }
373         }
374
375         eDebug("not found satellite configuration for orbital position (%d)", sat.orbital_position );
376
377         return -1;
378 }
379