disabled unused md5* variables
[enigma2.git] / lib / dvb / sec.cpp
1 #include <config.h>
2 #include <lib/dvb/dvb.h>
3 #include <lib/dvb/sec.h>
4 #include <lib/dvb/rotor_calc.h>
5
6 #if HAVE_DVB_API_VERSION < 3
7 #define INVERSION Inversion
8 #define FREQUENCY Frequency
9 #define FEC_INNER FEC_inner
10 #define SYMBOLRATE SymbolRate
11 #else
12 #define INVERSION inversion
13 #define FREQUENCY frequency
14 #define FEC_INNER fec_inner
15 #define SYMBOLRATE symbol_rate
16 #endif
17 #include <lib/base/eerror.h>
18
19 DEFINE_REF(eDVBSatelliteEquipmentControl);
20
21 eDVBSatelliteEquipmentControl *eDVBSatelliteEquipmentControl::instance;
22
23 eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBRegisteredFrontend> &avail_frontends)
24         :m_lnbidx(-1), m_curSat(m_lnbs[0].m_satellites.end()), m_avail_frontends(avail_frontends), m_rotorMoving(false)
25 {
26         if (!instance)
27                 instance = this;
28
29         clear();
30
31 // ASTRA
32         addLNB();
33         setLNBTunerMask(3);
34         setLNBLOFL(9750000);
35         setLNBThreshold(11750000);
36         setLNBLOFH(10607000);
37         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_0);
38         setToneburst(eDVBSatelliteDiseqcParameters::NO);
39         setRepeats(0);
40         setCommittedCommand(eDVBSatelliteDiseqcParameters::BB);
41         setCommandOrder(0); // committed, toneburst
42         setFastDiSEqC(true);
43         setSeqRepeat(false);
44         addSatellite(192);
45         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
46         setToneMode(eDVBSatelliteSwitchParameters::HILO);
47
48 // Hotbird
49         addLNB();
50         setLNBTunerMask(3);
51         setLNBLOFL(9750000);
52         setLNBThreshold(11750000);
53         setLNBLOFH(10600000);
54         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_0);
55         setToneburst(eDVBSatelliteDiseqcParameters::NO);
56         setRepeats(0);
57         setCommittedCommand(eDVBSatelliteDiseqcParameters::AB);
58         setCommandOrder(0); // committed, toneburst
59         setFastDiSEqC(true);
60         setSeqRepeat(false);
61         addSatellite(130);
62         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
63         setToneMode(eDVBSatelliteSwitchParameters::HILO);
64
65 // Rotor
66         addLNB();
67         setLNBTunerMask(3);
68         setLNBLOFL(9750000);
69         setLNBThreshold(11750000);
70         setLNBLOFH(10600000);
71         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_2);
72         setToneburst(eDVBSatelliteDiseqcParameters::NO);
73         setRepeats(0);
74         setCommittedCommand(eDVBSatelliteDiseqcParameters::AA);
75         setCommandOrder(0); // committed, toneburst
76         setFastDiSEqC(true);
77         setSeqRepeat(false);
78         setLaDirection(eDVBSatelliteRotorParameters::NORTH);
79         setLoDirection(eDVBSatelliteRotorParameters::EAST);
80         setLatitude(51.017);
81         setLongitude(8.683);
82         setUseInputpower(true);
83         setInputpowerDelta(50);
84
85         addSatellite(235);
86         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
87         setToneMode(eDVBSatelliteSwitchParameters::HILO);
88         setRotorPosNum(0);
89
90         addSatellite(284);
91         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
92         setToneMode(eDVBSatelliteSwitchParameters::HILO);
93         setRotorPosNum(0);
94
95         addSatellite(420);
96         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
97         setToneMode(eDVBSatelliteSwitchParameters::HILO);
98         setRotorPosNum(1); // stored pos 1
99 }
100
101 int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite &sat, iDVBFrontend *fe, int frontend_id )
102 {
103         int ret=0;
104
105         for (int idx=0; idx <= m_lnbidx; ++idx )
106         {
107                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
108                 if ( lnb_param.tuner_mask & frontend_id ) // lnb for correct tuner?
109                 {
110                         eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
111
112                         std::map<int, eDVBSatelliteSwitchParameters>::iterator sit =
113                                 lnb_param.m_satellites.find(sat.orbital_position);
114                         if ( sit != lnb_param.m_satellites.end())
115                         {
116                                 int band=0,
117                                         linked_to=0, // linked tuner
118                                         csw = di_param.m_committed_cmd,
119                                         ucsw = di_param.m_uncommitted_cmd,
120                                         toneburst = di_param.m_toneburst_param,
121                                         curRotorPos;
122
123                                 fe->getData(6, curRotorPos);
124                                 fe->getData(7, linked_to);
125
126                                 if ( sat.frequency > lnb_param.m_lof_threshold )
127                                         band |= 1;
128                                 if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
129                                         band |= 2;
130
131                                 bool rotor=false;
132                                 bool diseqc=false;
133
134                                 if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
135                                 {
136                                         diseqc=true;
137                                         if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
138                                                 csw = 0xF0 | (csw << 2);
139
140                                         csw |= band;
141
142                                         if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )  // ROTOR
143                                         {
144                                                 rotor=true;
145                                                 if ( curRotorPos == sat.orbital_position )
146                                                         ret=20;
147                                                 else
148                                                         ret=10;
149                                         }
150                                 }
151                                 else
152                                         csw = band;
153
154                                 if (!ret)
155                                         ret=40;
156
157                                 if (linked_to != -1)  // check for linked tuners..
158                                 {
159                                         eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_to;
160                                         if (linked_fe->m_inuse)
161                                         {
162                                                 int ocsw = -1,
163                                                         oucsw = -1,
164                                                         oToneburst = -1,
165                                                         oRotorPos = -1;
166                                                 linked_fe->m_frontend->getData(0, ocsw);
167                                                 linked_fe->m_frontend->getData(1, oucsw);
168                                                 linked_fe->m_frontend->getData(2, oToneburst);
169                                                 linked_fe->m_frontend->getData(6, oRotorPos);
170 #if 0
171                                                 eDebug("compare csw %02x == lcsw %02x",
172                                                         csw, ocsw);
173                                                 if ( diseqc )
174                                                         eDebug("compare ucsw %02x == lucsw %02x\ncompare toneburst %02x == oToneburst %02x",
175                                                                 ucsw, oucsw, toneburst, oToneburst);
176                                                 if ( rotor )
177                                                         eDebug("compare pos %d == current pos %d",
178                                                                 sat.orbital_position, oRotorPos);
179 #endif
180                                                 if ( (csw != ocsw) ||
181                                                         ( diseqc && (ucsw != oucsw || toneburst != oToneburst) ) ||
182                                                         ( rotor && oRotorPos != sat.orbital_position ) )
183                                                 {
184 //                                                      eDebug("can not tune this transponder with linked tuner in use!!");
185                                                         ret=0;
186                                                 }
187 //                                              else
188 //                                                      eDebug("OK .. can tune this transponder with linked tuner in use :)");
189                                         }
190                                 }
191                         }
192                 }
193         }
194         return ret;
195 }
196
197 #define VOLTAGE(x) (lnb_param.m_increased_voltage ? iDVBFrontend::voltage##x##_5 : iDVBFrontend::voltage##x)
198
199 RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, eDVBFrontendParametersSatellite &sat, int frontend_id)
200 {
201         bool linked=false;
202
203         for (int idx=0; idx <= m_lnbidx; ++idx )
204         {
205                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
206                 if (!(lnb_param.tuner_mask & frontend_id)) // lnb for correct tuner?
207                         continue;
208                 eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
209                 eDVBSatelliteRotorParameters &rotor_param = lnb_param.m_rotor_parameters;
210
211                 std::map<int, eDVBSatelliteSwitchParameters>::iterator sit =
212                         lnb_param.m_satellites.find(sat.orbital_position);
213                 if ( sit != lnb_param.m_satellites.end())
214                 {
215                         eDVBSatelliteSwitchParameters &sw_param = sit->second;
216
217                         int band=0,
218                                 linked_to=-1, // linked tuner
219                                 voltage = iDVBFrontend::voltageOff,
220                                 tone = iDVBFrontend::toneOff,
221                                 csw = di_param.m_committed_cmd,
222                                 ucsw = di_param.m_uncommitted_cmd,
223                                 toneburst = di_param.m_toneburst_param,
224                                 lastcsw = -1,
225                                 lastucsw = -1,
226                                 lastToneburst = -1,
227                                 lastRotorCmd = -1,
228                                 curRotorPos = -1;
229
230                         frontend.getData(0, lastcsw);
231                         frontend.getData(1, lastucsw);
232                         frontend.getData(2, lastToneburst);
233                         frontend.getData(5, lastRotorCmd);
234                         frontend.getData(6, curRotorPos);
235                         frontend.getData(7, linked_to);
236
237                         if (linked_to != -1)
238                         {
239                                 eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_to;
240                                 if (linked_fe->m_inuse)
241                                 {
242                                         eDebug("[SEC] frontend is linked with another and the other one is in use.. so we dont do SEC!!");
243                                         linked=true;
244                                 }
245                         }
246
247                         if ( sat.frequency > lnb_param.m_lof_threshold )
248                                 band |= 1;
249
250                         if (band&1)
251                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_hi;
252                         else
253                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_lo;
254
255                         if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
256                                 band |= 2;
257
258                         switch (sat.inversion)
259                         {
260                                 case eDVBFrontendParametersCable::Inversion::On:
261                                         parm.INVERSION = INVERSION_ON;
262                                         break;
263                                 case eDVBFrontendParametersCable::Inversion::Off:
264                                         parm.INVERSION = INVERSION_OFF;
265                                         break;
266                                 default:
267                                 case eDVBFrontendParametersCable::Inversion::Unknown:
268                                         parm.INVERSION = INVERSION_AUTO;
269                                         break;
270                         }
271
272                         switch (sat.fec)
273                         {
274                                 default:
275                                 case eDVBFrontendParametersSatellite::FEC::fNone:
276                                         eDebug("no fec set.. assume auto");
277                                 case eDVBFrontendParametersSatellite::FEC::fAuto:
278                                         parm.u.qpsk.FEC_INNER = FEC_AUTO;
279                                         break;
280                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
281                                         parm.u.qpsk.FEC_INNER = FEC_1_2;
282                                         break;
283                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
284                                         parm.u.qpsk.FEC_INNER = FEC_2_3;
285                                         break;
286                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
287                                         parm.u.qpsk.FEC_INNER = FEC_3_4;
288                                         break;
289                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
290                                         parm.u.qpsk.FEC_INNER = FEC_5_6;
291                                         break;
292                                 case eDVBFrontendParametersSatellite::FEC::f7_8: 
293                                         parm.u.qpsk.FEC_INNER = FEC_7_8;
294                                         break;
295                         }
296
297                         parm.u.qpsk.SYMBOLRATE = sat.symbol_rate;
298
299                         if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_14V
300                                 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Vertical
301                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
302                                 voltage = VOLTAGE(13);
303                         else if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_18V
304                                 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal
305                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
306                                 voltage = VOLTAGE(18);
307                         if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::ON)
308                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && (band&1) ) )
309                                 tone = iDVBFrontend::toneOn;
310                         else if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::OFF)
311                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && !(band&1) ) )
312                                 tone = iDVBFrontend::toneOff;
313
314                         eSecCommandList sec_sequence;
315
316                         if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
317                         {
318                                 if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
319                                         csw = 0xF0 | (csw << 2);
320
321                                 csw |= band;
322
323                                 bool send_csw =
324                                         (di_param.m_committed_cmd != eDVBSatelliteDiseqcParameters::SENDNO);
325                                 bool changed_csw = send_csw && csw != lastcsw;
326
327                                 bool send_ucsw =
328                                         (di_param.m_uncommitted_cmd && di_param.m_diseqc_mode > eDVBSatelliteDiseqcParameters::V1_0);
329                                 bool changed_ucsw = send_ucsw && ucsw != lastucsw;
330
331                                 bool send_burst =
332                                         (di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO);
333                                 bool changed_burst = send_burst && toneburst != lastToneburst;
334
335                                 int send_mask = 0; /*
336                                         1 must send csw
337                                         2 must send ucsw
338                                         4 send toneburst first
339                                         8 send toneburst at end */
340                                 if (changed_burst) // toneburst first and toneburst changed
341                                 {
342                                         if (di_param.m_command_order&1)
343                                         {
344                                                 send_mask |= 4;
345                                                 if ( send_csw )
346                                                         send_mask |= 1;
347                                                 if ( send_ucsw )
348                                                         send_mask |= 2;
349                                         }
350                                         else
351                                                 send_mask |= 8;
352                                 }
353                                 if (changed_ucsw)
354                                 {
355                                         send_mask |= 2;
356                                         if ((di_param.m_command_order&4) && send_csw)
357                                                 send_mask |= 1;
358                                         if (di_param.m_command_order==4 && send_burst)
359                                                 send_mask |= 8;
360                                 }
361                                 if (changed_csw) 
362                                 {
363                                         if ( di_param.m_use_fast && (lastcsw & 0xF0) && ((csw / 4) == (lastcsw / 4)) )
364                                                 eDebug("dont send committed cmd (fast diseqc)");
365                                         else
366                                         {
367                                                 send_mask |= 1;
368                                                 if (!(di_param.m_command_order&4) && send_ucsw)
369                                                         send_mask |= 2;
370                                                 if (!(di_param.m_command_order&1) && send_burst)
371                                                         send_mask |= 8;
372                                         }
373                                 }
374
375 #if 0
376                                 eDebugNoNewLine("sendmask: ");
377                                 for (int i=3; i >= 0; --i)
378                                         if ( send_mask & (1<<i) )
379                                                 eDebugNoNewLine("1");
380                                         else
381                                                 eDebugNoNewLine("0");
382                                 eDebug("");
383 #endif
384
385                                 if ( send_mask )
386                                 {
387                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
388                                         eSecCommand::pair compare;
389                                         compare.voltage = voltage;
390                                         compare.steps = +3;
391                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
392                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
393                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
394                                 }
395
396                                 for (int seq_repeat = 0; seq_repeat < (di_param.m_seq_repeat?2:1); ++seq_repeat)
397                                 {
398                                         if ( send_mask & 4 )
399                                         {
400                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
401                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
402                                         }
403
404                                         int loops=0;
405
406                                         if ( send_mask & 1 )
407                                                 ++loops;
408                                         if ( send_mask & 2 )
409                                                 ++loops;
410
411                                         for ( int i=0; i < di_param.m_repeats; ++i )
412                                                 loops *= 2;
413
414                                         for ( int i = 0; i < loops;)  // fill commands...
415                                         {
416                                                 eDVBDiseqcCommand diseqc;
417                                                 diseqc.len = 4;
418                                                 diseqc.data[0] = i ? 0xE1 : 0xE0;
419                                                 diseqc.data[1] = 0x10;
420                                                 if ( (send_mask & 2) && (di_param.m_command_order & 4) )
421                                                 {
422                                                         diseqc.data[2] = 0x39;
423                                                         diseqc.data[3] = ucsw;
424                                                 }
425                                                 else if ( send_mask & 1 )
426                                                 {
427                                                         diseqc.data[2] = 0x38;
428                                                         diseqc.data[3] = csw;
429                                                 }
430                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
431
432                                                 i++;
433                                                 if ( i < loops )
434                                                 {
435                                                         int cmd=0;
436                                                         if (diseqc.data[2] == 0x38 && (send_mask & 2))
437                                                                 cmd=0x39;
438                                                         else if (diseqc.data[2] == 0x39 && (send_mask & 1))
439                                                                 cmd=0x38;
440                                                         if (cmd)
441                                                         {
442                                                                 static int delay = (120 - 54) / 2;  // standard says 100msek between two repeated commands
443                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
444                                                                 diseqc.data[2]=cmd;
445                                                                 diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
446                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
447                                                                 ++i;
448                                                                 if ( i < loops )
449                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
450                                                                 else
451                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
452                                                         }
453                                                         else  // delay 120msek when no command is in repeat gap
454                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 120) );
455                                                 }
456                                                 else
457                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
458                                         }
459
460                                         if ( send_mask & 8 )  // toneburst at end of sequence
461                                         {
462                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
463                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
464                                         }
465                                 }
466
467                                 if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )
468                                 {
469                                         int RotorCmd=0;
470                                         bool useGotoXX = false;
471
472                                         if (sw_param.m_rotorPosNum) // we have stored rotor pos?
473                                                 RotorCmd=sw_param.m_rotorPosNum;
474                                         else  // we must calc gotoxx cmd
475                                         {
476                                                 eDebug("Entry for %d,%d? not in Rotor Table found... i try gotoXX?", sat.orbital_position / 10, sat.orbital_position % 10 );
477                                                 useGotoXX = true;
478
479                                                 double  SatLon = abs(sat.orbital_position)/10.00,
480                                                                 SiteLat = rotor_param.m_gotoxx_parameters.m_latitude,
481                                                                 SiteLon = rotor_param.m_gotoxx_parameters.m_longitude;
482
483                                                 if ( rotor_param.m_gotoxx_parameters.m_la_direction == eDVBSatelliteRotorParameters::SOUTH )
484                                                         SiteLat = -SiteLat;
485
486                                                 if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
487                                                         SiteLon = 360 - SiteLon;
488
489                                                 eDebug("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
490                                                 double satHourAngle =
491                                                         calcSatHourangle( SatLon, SiteLat, SiteLon );
492                                                 eDebug("PolarmountHourAngle=%lf", satHourAngle );
493
494                                                 static int gotoXTable[10] =
495                                                         { 0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E };
496
497                                                 if (SiteLat >= 0) // Northern Hemisphere
498                                                 {
499                                                         int tmp=(int)round( fabs( 180 - satHourAngle ) * 10.0 );
500                                                         RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
501
502                                                         if (satHourAngle < 180) // the east
503                                                                 RotorCmd |= 0xE000;
504                                                         else                                    // west
505                                                                 RotorCmd |= 0xD000;
506                                                 }
507                                                 else // Southern Hemisphere
508                                                 {
509                                                         if (satHourAngle < 180) // the east
510                                                         {
511                                                                 int tmp=(int)round( fabs( satHourAngle ) * 10.0 );
512                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
513                                                                 RotorCmd |= 0xD000;
514                                                         }
515                                                         else                                    // west
516                                                         {          
517                                                                 int tmp=(int)round( fabs( 360 - satHourAngle ) * 10.0 );
518                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
519                                                                 RotorCmd |= 0xE000;
520                                                         }
521                                                 }
522                                                 eDebug("RotorCmd = %04x", RotorCmd);
523                                         }
524                                         if ( RotorCmd != lastRotorCmd )
525                                         {
526                                                 if ( send_mask )
527                                                 {
528                                                         // override first voltage change
529                                                         *(++(++sec_sequence.begin()))=eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13));
530                                                         // wait 1 second after first switch diseqc command
531                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 1000) );
532                                                 }
533                                                 else  // no other diseqc commands before
534                                                 {
535                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
536                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );  // wait 15msec after tone change
537                                                         eSecCommand::pair compare;
538                                                         compare.voltage = voltage;
539                                                         compare.steps = +3;
540                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
541                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) );
542                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
543                                                 }
544
545                                                 eDVBDiseqcCommand diseqc;
546                                                 diseqc.data[0] = 0xE0;
547                                                 diseqc.data[1] = 0x31;          // positioner
548                                                 if ( useGotoXX )
549                                                 {
550                                                         diseqc.len = 5;
551                                                         diseqc.data[2] = 0x6E;  // drive to angular position
552                                                         diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
553                                                         diseqc.data[4] = RotorCmd & 0xFF;
554                                                 }
555                                                 else
556                                                 {
557                                                         diseqc.len = 4;
558                                                         diseqc.data[2] = 0x6B;  // goto stored sat position
559                                                         diseqc.data[3] = RotorCmd;
560                                                 }
561
562                                                 if ( rotor_param.m_inputpower_parameters.m_use )
563                                                 { // use measure rotor input power to detect rotor state
564                                                         eSecCommand::rotor cmd;
565 // measure idle power values
566                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_IDLE_INPUTPOWER_AVAIL_GOTO, +8) ); // already measured?
567                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
568                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 0) );
569                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
570                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 100) );  // wait 100msec before measure
571                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 1) );
572                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) ); // back to lower voltage
573                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
574 ////////////////////////////
575                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
576                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
577                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
578                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 40) );  // 2 seconds rotor start timout
579 // rotor start loop
580                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // 50msec delay
581                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
582                                                         cmd.direction=1;  // check for running rotor
583                                                         cmd.deltaA=rotor_param.m_inputpower_parameters.m_delta;
584                                                         cmd.steps=+3;
585                                                         cmd.okcount=0;
586                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );  // check if rotor has started
587                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +9 ) );  // timeout .. we assume now the rotor is already at the correct position
588                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // goto loop start
589 ////////////////////
590                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 2400) );  // 2 minutes running timeout
591                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
592 // rotor running loop
593                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
594                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
595                                                         cmd.direction=0;  // check for stopped rotor
596                                                         cmd.steps=+3;
597                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
598                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) );  // timeout ? this should never happen
599                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // running loop start
600 /////////////////////
601                                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
602                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
603                                                         if ( linked )
604                                                         {
605                                                                 frontend.setData(5, RotorCmd);
606                                                                 frontend.setData(6, sat.orbital_position);
607                                                         }
608                                                         else
609                                                         {
610                                                                 frontend.setData(3, RotorCmd);
611                                                                 frontend.setData(4, sat.orbital_position);
612                                                         }
613                                                 }
614                                                 else
615                                                         eFatal("rotor turning without inputpowermeasure not implemented yet");
616                                         }
617                                 }
618                         }
619                         else
620                                 csw = band;
621
622                         frontend.setData(0, csw);
623                         frontend.setData(1, ucsw);
624                         frontend.setData(2, di_param.m_toneburst_param);
625
626                         if ( linked )
627                                 return 0;
628
629                         eSecCommand::pair compare;
630                         compare.voltage = voltage;
631                         compare.steps = +3;
632                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
633                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
634                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 10) );
635
636                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
637                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );
638
639                         frontend.setSecSequence(sec_sequence);
640
641                         return 0;
642                 }
643         }
644
645         if (linked)
646                 return 0;
647
648         eDebug("found no satellite configuration for orbital position (%d)", sat.orbital_position );
649         return -1;
650 }
651
652 RESULT eDVBSatelliteEquipmentControl::clear()
653 {
654         for (int i=0; i < m_lnbidx; ++i)
655         {
656                 m_lnbs[i].m_satellites.clear();
657                 m_lnbs[i].tuner_mask = 0;
658         }
659         m_lnbidx=-1;
660
661 // clear linked tuner configuration
662         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
663                 it->m_frontend->setData(7, -1);
664
665         return 0;
666 }
667
668 /* LNB Specific Parameters */
669 RESULT eDVBSatelliteEquipmentControl::addLNB()
670 {
671         if ( m_lnbidx < (int)(sizeof(m_lnbs) / sizeof(eDVBSatelliteLNBParameters)))
672                 m_curSat=m_lnbs[++m_lnbidx].m_satellites.end();
673         else
674         {
675                 eDebug("no more LNB free... cnt is %d", m_lnbidx);
676                 return -ENOSPC;
677         }
678         return 0;
679 }
680
681 RESULT eDVBSatelliteEquipmentControl::setLNBTunerMask(int tunermask)
682 {
683         if ( currentLNBValid() )
684                 m_lnbs[m_lnbidx].tuner_mask = tunermask;
685         else
686                 return -ENOENT;
687         return 0;
688 }
689
690 RESULT eDVBSatelliteEquipmentControl::setLNBLOFL(int lofl)
691 {
692         if ( currentLNBValid() )
693                 m_lnbs[m_lnbidx].m_lof_lo = lofl;
694         else
695                 return -ENOENT;
696         return 0;
697 }
698
699 RESULT eDVBSatelliteEquipmentControl::setLNBLOFH(int lofh)
700 {
701         if ( currentLNBValid() )
702                 m_lnbs[m_lnbidx].m_lof_hi = lofh;
703         else
704                 return -ENOENT;
705         return 0;
706 }
707
708 RESULT eDVBSatelliteEquipmentControl::setLNBThreshold(int threshold)
709 {
710         if ( currentLNBValid() )
711                 m_lnbs[m_lnbidx].m_lof_threshold = threshold;
712         else
713                 return -ENOENT;
714         return 0;
715 }
716
717 RESULT eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(bool onoff)
718 {
719         if ( currentLNBValid() )
720                 m_lnbs[m_lnbidx].m_increased_voltage = onoff;
721         else
722                 return -ENOENT;
723         return 0;
724 }
725
726 /* DiSEqC Specific Parameters */
727 RESULT eDVBSatelliteEquipmentControl::setDiSEqCMode(int diseqcmode)
728 {
729         if ( currentLNBValid() )
730                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_diseqc_mode = (eDVBSatelliteDiseqcParameters::t_diseqc_mode)diseqcmode;
731         else
732                 return -ENOENT;
733         return 0;
734 }
735
736 RESULT eDVBSatelliteEquipmentControl::setToneburst(int toneburst)
737 {
738         if ( currentLNBValid() )
739                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_toneburst_param = (eDVBSatelliteDiseqcParameters::t_toneburst_param)toneburst;
740         else
741                 return -ENOENT;
742         return 0;
743 }
744
745 RESULT eDVBSatelliteEquipmentControl::setRepeats(int repeats)
746 {
747         if ( currentLNBValid() )
748                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_repeats=repeats;
749         else
750                 return -ENOENT;
751         return 0;
752 }
753
754 RESULT eDVBSatelliteEquipmentControl::setCommittedCommand(int command)
755 {
756         if ( currentLNBValid() )
757                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_committed_cmd=command;
758         else
759                 return -ENOENT;
760         return 0;
761 }
762
763 RESULT eDVBSatelliteEquipmentControl::setUncommittedCommand(int command)
764 {
765         if ( currentLNBValid() )
766                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_uncommitted_cmd = command;
767         else
768                 return -ENOENT;
769         return 0;
770 }
771
772 RESULT eDVBSatelliteEquipmentControl::setCommandOrder(int order)
773 {
774         if ( currentLNBValid() )
775                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_command_order=order;
776         else
777                 return -ENOENT;
778         return 0;
779 }
780
781 RESULT eDVBSatelliteEquipmentControl::setFastDiSEqC(bool onoff)
782 {
783         if ( currentLNBValid() )
784                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_use_fast=onoff;
785         else
786                 return -ENOENT;
787         return 0;
788 }
789
790 RESULT eDVBSatelliteEquipmentControl::setSeqRepeat(bool onoff)
791 {
792         if ( currentLNBValid() )
793                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_seq_repeat = onoff;
794         else
795                 return -ENOENT;
796         return 0;
797 }
798
799 /* Rotor Specific Parameters */
800 RESULT eDVBSatelliteEquipmentControl::setLongitude(float longitude)
801 {
802         if ( currentLNBValid() )
803                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_longitude=longitude;
804         else
805                 return -ENOENT;
806         return 0;
807 }
808
809 RESULT eDVBSatelliteEquipmentControl::setLatitude(float latitude)
810 {
811         if ( currentLNBValid() )
812                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_latitude=latitude;
813         else
814                 return -ENOENT;
815         return 0;
816 }
817
818 RESULT eDVBSatelliteEquipmentControl::setLoDirection(int direction)
819 {
820         if ( currentLNBValid() )
821                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_lo_direction=direction;
822         else
823                 return -ENOENT;
824         return 0;
825 }
826
827 RESULT eDVBSatelliteEquipmentControl::setLaDirection(int direction)
828 {
829         if ( currentLNBValid() )
830                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_la_direction=direction;
831         else
832                 return -ENOENT;
833         return 0;
834 }
835
836 RESULT eDVBSatelliteEquipmentControl::setUseInputpower(bool onoff)
837 {
838         if ( currentLNBValid() )
839                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_use=onoff;
840         else
841                 return -ENOENT;
842         return 0;
843 }
844
845 RESULT eDVBSatelliteEquipmentControl::setInputpowerDelta(int delta)
846 {
847         if ( currentLNBValid() )
848                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_delta=delta;
849         else
850                 return -ENOENT;
851         return 0;
852 }
853
854 /* Satellite Specific Parameters */
855 RESULT eDVBSatelliteEquipmentControl::addSatellite(int orbital_position)
856 {
857         if ( currentLNBValid() )
858         {
859                 std::map<int, eDVBSatelliteSwitchParameters>::iterator it =
860                         m_lnbs[m_lnbidx].m_satellites.find(orbital_position);
861                 if ( it == m_lnbs[m_lnbidx].m_satellites.end() )
862                 {
863                         std::pair<std::map<int, eDVBSatelliteSwitchParameters>::iterator, bool > ret =
864                                 m_lnbs[m_lnbidx].m_satellites.insert(
865                                         std::pair<int, eDVBSatelliteSwitchParameters>(orbital_position, eDVBSatelliteSwitchParameters())
866                                 );
867                         if ( ret.second )
868                                 m_curSat = ret.first;
869                         else
870                                 return -ENOMEM;
871                 }
872                 else
873                         return -EEXIST;
874         }
875         else
876                 return -ENOENT;
877         return 0;
878 }
879
880 RESULT eDVBSatelliteEquipmentControl::setVoltageMode(int mode)
881 {
882         if ( currentLNBValid() && m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
883                 m_curSat->second.m_voltage_mode = (eDVBSatelliteSwitchParameters::t_voltage_mode)mode;
884         else
885                 return -ENOENT;
886         return 0;
887
888 }
889
890 RESULT eDVBSatelliteEquipmentControl::setToneMode(int mode)
891 {
892         if ( currentLNBValid() )
893         {
894                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
895                         m_curSat->second.m_22khz_signal = (eDVBSatelliteSwitchParameters::t_22khz_signal)mode;
896                 else
897                         return -EPERM;
898         }
899         else
900                 return -ENOENT;
901         return 0;
902 }
903
904 RESULT eDVBSatelliteEquipmentControl::setRotorPosNum(int rotor_pos_num)
905 {
906         if ( currentLNBValid() )
907         {
908                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
909                         m_curSat->second.m_rotorPosNum=rotor_pos_num;
910                 else
911                         return -EPERM;
912         }
913         else
914                 return -ENOENT;
915         return 0;
916 }
917
918 RESULT eDVBSatelliteEquipmentControl::setTunerLinked(int tu1, int tu2)
919 {
920         if (tu1 == tu2)
921                 return -1;
922
923         eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
924
925         int cnt=0;
926         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
927         {
928                 if (cnt == tu1)
929                         p1 = *it;
930                 else if (cnt == tu2)
931                         p2 = *it;
932         }
933         if (p1 && p2)
934         {
935                 p1->m_frontend->setData(7, (int)p2);  // this is evil..
936                 p2->m_frontend->setData(7, (int)p1);
937                 return 0;
938         }
939         return -1;
940 }
941
942 bool eDVBSatelliteEquipmentControl::isRotorMoving()
943 {
944         return m_rotorMoving;
945 }
946
947 void eDVBSatelliteEquipmentControl::setRotorMoving(bool b)
948 {
949         m_rotorMoving=b;
950 }