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