add needed functions
[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                                                 int satDir = sat.orbital_position < 0 ?
479                                                         eDVBSatelliteRotorParameters::WEST :
480                                                         eDVBSatelliteRotorParameters::EAST;
481
482                                                 double  SatLon = abs(sat.orbital_position)/10.00,
483                                                                 SiteLat = rotor_param.m_gotoxx_parameters.m_latitude,
484                                                                 SiteLon = rotor_param.m_gotoxx_parameters.m_longitude;
485
486                                                 if ( rotor_param.m_gotoxx_parameters.m_la_direction == eDVBSatelliteRotorParameters::SOUTH )
487                                                         SiteLat = -SiteLat;
488
489                                                 if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
490                                                         SiteLon = 360 - SiteLon;
491
492                                                 if (satDir == eDVBSatelliteRotorParameters::WEST )
493                                                         SatLon = 360 - SatLon;
494
495                                                 eDebug("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
496                                                 double satHourAngle =
497                                                         calcSatHourangle( SatLon, SiteLat, SiteLon );
498                                                 eDebug("PolarmountHourAngle=%lf", satHourAngle );
499
500                                                 static int gotoXTable[10] =
501                                                         { 0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E };
502
503                                                 if (SiteLat >= 0) // Northern Hemisphere
504                                                 {
505                                                         int tmp=(int)round( fabs( 180 - satHourAngle ) * 10.0 );
506                                                         RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
507
508                                                         if (satHourAngle < 180) // the east
509                                                                 RotorCmd |= 0xE000;
510                                                         else                                    // west
511                                                                 RotorCmd |= 0xD000;
512                                                 }
513                                                 else // Southern Hemisphere
514                                                 {
515                                                         if (satHourAngle < 180) // the east
516                                                         {
517                                                                 int tmp=(int)round( fabs( satHourAngle ) * 10.0 );
518                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
519                                                                 RotorCmd |= 0xD000;
520                                                         }
521                                                         else                                    // west
522                                                         {          
523                                                                 int tmp=(int)round( fabs( 360 - satHourAngle ) * 10.0 );
524                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
525                                                                 RotorCmd |= 0xE000;
526                                                         }
527                                                 }
528                                                 eDebug("RotorCmd = %04x", RotorCmd);
529                                         }
530                                         if ( RotorCmd != lastRotorCmd )
531                                         {
532                                                 if ( send_mask )
533                                                 {
534                                                         // override first voltage change
535                                                         *(++(++sec_sequence.begin()))=eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13);
536                                                         // wait 1 second after first switch diseqc command
537                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 1000) );
538                                                 }
539                                                 else  // no other diseqc commands before
540                                                 {
541                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
542                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );  // wait 15msec after tone change
543                                                         eSecCommand::pair compare;
544                                                         compare.voltage = voltage;
545                                                         compare.steps = +3;
546                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
547                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
548                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
549                                                 }
550
551                                                 eDVBDiseqcCommand diseqc;
552                                                 diseqc.data[0] = 0xE0;
553                                                 diseqc.data[1] = 0x31;          // positioner
554                                                 if ( useGotoXX )
555                                                 {
556                                                         diseqc.len = 5;
557                                                         diseqc.data[2] = 0x6E;  // drive to angular position
558                                                         diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
559                                                         diseqc.data[4] = RotorCmd & 0xFF;
560                                                 }
561                                                 else
562                                                 {
563                                                         diseqc.len = 4;
564                                                         diseqc.data[2] = 0x6B;  // goto stored sat position
565                                                         diseqc.data[3] = RotorCmd;
566                                                 }
567
568                                                 if ( rotor_param.m_inputpower_parameters.m_use )
569                                                 { // use measure rotor input power to detect rotor state
570                                                         eSecCommand::rotor cmd;
571 // measure idle power values
572                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_IDLE_INPUTPOWER_AVAIL_GOTO, +8) ); // already measured?
573                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
574                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 0) );
575                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage18) );
576                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 100) );  // wait 100msec before measure
577                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 1) );
578                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) ); // back to lower voltage
579                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
580 ////////////////////////////
581                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
582                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
583                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
584                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 40) );  // 2 seconds rotor start timout
585 // rotor start loop
586                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // 50msec delay
587                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
588                                                         cmd.direction=1;  // check for running rotor
589                                                         cmd.deltaA=rotor_param.m_inputpower_parameters.m_delta;
590                                                         cmd.steps=+3;
591                                                         cmd.okcount=0;
592                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );  // check if rotor has started
593                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +10 ) );  // timeout .. we assume now the rotor is already at the correct position
594                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // goto loop start
595 ////////////////////
596                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 2400) );  // 2 minutes running timeout
597                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage18) );
598                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
599 // rotor running loop
600                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
601                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
602                                                         cmd.direction=0;  // check for stopped rotor
603                                                         cmd.steps=+3;
604                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
605                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) );  // timeout ? this should never happen
606                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // running loop start
607 /////////////////////
608                                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
609                                                         if ( linked )
610                                                         {
611                                                                 frontend.setData(5, RotorCmd);
612                                                                 frontend.setData(6, sat.orbital_position);
613                                                         }
614                                                         else
615                                                         {
616                                                                 frontend.setData(3, RotorCmd);
617                                                                 frontend.setData(4, sat.orbital_position);
618                                                         }
619                                                 }
620                                                 else
621                                                         eFatal("rotor turning without inputpowermeasure not implemented yet");
622                                         }
623                                 }
624                         }
625                         else
626                                 csw = band;
627
628                         frontend.setData(0, csw);
629                         frontend.setData(1, ucsw);
630                         frontend.setData(2, di_param.m_toneburst_param);
631
632                         if ( linked )
633                                 return 0;
634
635                         eSecCommand::pair compare;
636                         compare.voltage = voltage;
637                         compare.steps = +3;
638                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
639                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
640                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 10) );
641
642                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
643                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );
644
645                         frontend.setSecSequence(sec_sequence);
646
647                         return 0;
648                 }
649         }
650
651         if (linked)
652                 return 0;
653
654         eDebug("found no satellite configuration for orbital position (%d)", sat.orbital_position );
655         return -1;
656 }
657
658 RESULT eDVBSatelliteEquipmentControl::clear()
659 {
660         for (int i=0; i < m_lnbidx; ++i)
661         {
662                 m_lnbs[i].m_satellites.clear();
663                 m_lnbs[i].tuner_mask = 0;
664         }
665         m_lnbidx=-1;
666
667 // clear linked tuner configuration
668         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
669                 it->m_frontend->setData(7, -1);
670
671         return 0;
672 }
673
674 /* LNB Specific Parameters */
675 RESULT eDVBSatelliteEquipmentControl::addLNB()
676 {
677         if ( m_lnbidx < (int)(sizeof(m_lnbs) / sizeof(eDVBSatelliteLNBParameters)))
678                 m_curSat=m_lnbs[++m_lnbidx].m_satellites.end();
679         else
680         {
681                 eDebug("no more LNB free... cnt is %d", m_lnbidx);
682                 return -ENOSPC;
683         }
684         return 0;
685 }
686
687 RESULT eDVBSatelliteEquipmentControl::setLNBTunerMask(int tunermask)
688 {
689         if ( currentLNBValid() )
690                 m_lnbs[m_lnbidx].tuner_mask = tunermask;
691         else
692                 return -ENOENT;
693         return 0;
694 }
695
696 RESULT eDVBSatelliteEquipmentControl::setLNBLOFL(int lofl)
697 {
698         if ( currentLNBValid() )
699                 m_lnbs[m_lnbidx].m_lof_lo = lofl;
700         else
701                 return -ENOENT;
702         return 0;
703 }
704
705 RESULT eDVBSatelliteEquipmentControl::setLNBLOFH(int lofh)
706 {
707         if ( currentLNBValid() )
708                 m_lnbs[m_lnbidx].m_lof_hi = lofh;
709         else
710                 return -ENOENT;
711         return 0;
712 }
713
714 RESULT eDVBSatelliteEquipmentControl::setLNBThreshold(int threshold)
715 {
716         if ( currentLNBValid() )
717                 m_lnbs[m_lnbidx].m_lof_threshold = threshold;
718         else
719                 return -ENOENT;
720         return 0;
721 }
722
723 RESULT eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(bool onoff)
724 {
725         if ( currentLNBValid() )
726                 m_lnbs[m_lnbidx].m_increased_voltage = onoff;
727         else
728                 return -ENOENT;
729         return 0;
730 }
731
732 /* DiSEqC Specific Parameters */
733 RESULT eDVBSatelliteEquipmentControl::setDiSEqCMode(int diseqcmode)
734 {
735         if ( currentLNBValid() )
736                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_diseqc_mode = (eDVBSatelliteDiseqcParameters::t_diseqc_mode)diseqcmode;
737         else
738                 return -ENOENT;
739         return 0;
740 }
741
742 RESULT eDVBSatelliteEquipmentControl::setToneburst(int toneburst)
743 {
744         if ( currentLNBValid() )
745                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_toneburst_param = (eDVBSatelliteDiseqcParameters::t_toneburst_param)toneburst;
746         else
747                 return -ENOENT;
748         return 0;
749 }
750
751 RESULT eDVBSatelliteEquipmentControl::setRepeats(int repeats)
752 {
753         if ( currentLNBValid() )
754                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_repeats=repeats;
755         else
756                 return -ENOENT;
757         return 0;
758 }
759
760 RESULT eDVBSatelliteEquipmentControl::setCommittedCommand(int command)
761 {
762         if ( currentLNBValid() )
763                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_committed_cmd=command;
764         else
765                 return -ENOENT;
766         return 0;
767 }
768
769 RESULT eDVBSatelliteEquipmentControl::setUncommittedCommand(int command)
770 {
771         if ( currentLNBValid() )
772                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_uncommitted_cmd = command;
773         else
774                 return -ENOENT;
775         return 0;
776 }
777
778 RESULT eDVBSatelliteEquipmentControl::setCommandOrder(int order)
779 {
780         if ( currentLNBValid() )
781                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_command_order=order;
782         else
783                 return -ENOENT;
784         return 0;
785 }
786
787 RESULT eDVBSatelliteEquipmentControl::setFastDiSEqC(bool onoff)
788 {
789         if ( currentLNBValid() )
790                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_use_fast=onoff;
791         else
792                 return -ENOENT;
793         return 0;
794 }
795
796 RESULT eDVBSatelliteEquipmentControl::setSeqRepeat(bool onoff)
797 {
798         if ( currentLNBValid() )
799                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_seq_repeat = onoff;
800         else
801                 return -ENOENT;
802         return 0;
803 }
804
805 /* Rotor Specific Parameters */
806 RESULT eDVBSatelliteEquipmentControl::setLongitude(float longitude)
807 {
808         if ( currentLNBValid() )
809                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_longitude=longitude;
810         else
811                 return -ENOENT;
812         return 0;
813 }
814
815 RESULT eDVBSatelliteEquipmentControl::setLatitude(float latitude)
816 {
817         if ( currentLNBValid() )
818                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_latitude=latitude;
819         else
820                 return -ENOENT;
821         return 0;
822 }
823
824 RESULT eDVBSatelliteEquipmentControl::setLoDirection(int direction)
825 {
826         if ( currentLNBValid() )
827                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_lo_direction=direction;
828         else
829                 return -ENOENT;
830         return 0;
831 }
832
833 RESULT eDVBSatelliteEquipmentControl::setLaDirection(int direction)
834 {
835         if ( currentLNBValid() )
836                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_la_direction=direction;
837         else
838                 return -ENOENT;
839         return 0;
840 }
841
842 RESULT eDVBSatelliteEquipmentControl::setUseInputpower(bool onoff)
843 {
844         if ( currentLNBValid() )
845                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_use=onoff;
846         else
847                 return -ENOENT;
848         return 0;
849 }
850
851 RESULT eDVBSatelliteEquipmentControl::setInputpowerDelta(int delta)
852 {
853         if ( currentLNBValid() )
854                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_delta=delta;
855         else
856                 return -ENOENT;
857         return 0;
858 }
859
860 /* Satellite Specific Parameters */
861 RESULT eDVBSatelliteEquipmentControl::addSatellite(int orbital_position)
862 {
863         if ( currentLNBValid() )
864         {
865                 std::map<int, eDVBSatelliteSwitchParameters>::iterator it =
866                         m_lnbs[m_lnbidx].m_satellites.find(orbital_position);
867                 if ( it == m_lnbs[m_lnbidx].m_satellites.end() )
868                 {
869                         std::pair<std::map<int, eDVBSatelliteSwitchParameters>::iterator, bool > ret =
870                                 m_lnbs[m_lnbidx].m_satellites.insert(
871                                         std::pair<int, eDVBSatelliteSwitchParameters>(orbital_position, eDVBSatelliteSwitchParameters())
872                                 );
873                         if ( ret.second )
874                                 m_curSat = ret.first;
875                         else
876                                 return -ENOMEM;
877                 }
878                 else
879                         return -EEXIST;
880         }
881         else
882                 return -ENOENT;
883         return 0;
884 }
885
886 RESULT eDVBSatelliteEquipmentControl::setVoltageMode(int mode)
887 {
888         if ( currentLNBValid() && m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
889                 m_curSat->second.m_voltage_mode = (eDVBSatelliteSwitchParameters::t_voltage_mode)mode;
890         else
891                 return -ENOENT;
892         return 0;
893
894 }
895
896 RESULT eDVBSatelliteEquipmentControl::setToneMode(int mode)
897 {
898         if ( currentLNBValid() )
899         {
900                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
901                         m_curSat->second.m_22khz_signal = (eDVBSatelliteSwitchParameters::t_22khz_signal)mode;
902                 else
903                         return -EPERM;
904         }
905         else
906                 return -ENOENT;
907         return 0;
908 }
909
910 RESULT eDVBSatelliteEquipmentControl::setRotorPosNum(int rotor_pos_num)
911 {
912         if ( currentLNBValid() )
913         {
914                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
915                         m_curSat->second.m_rotorPosNum=rotor_pos_num;
916                 else
917                         return -EPERM;
918         }
919         else
920                 return -ENOENT;
921         return 0;
922 }
923
924 RESULT eDVBSatelliteEquipmentControl::setTunerLinked(int tu1, int tu2)
925 {
926         if (tu1 == tu2)
927                 return -1;
928
929         eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
930
931         int cnt=0;
932         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
933         {
934                 if (cnt == tu1)
935                         p1 = *it;
936                 else if (cnt == tu2)
937                         p2 = *it;
938         }
939         if (p1 && p2)
940         {
941                 p1->m_frontend->setData(7, (int)p2);  // this is evil..
942                 p2->m_frontend->setData(7, (int)p1);
943                 return 0;
944         }
945         return -1;
946 }
947
948 bool eDVBSatelliteEquipmentControl::isRotorMoving()
949 {
950         return m_rotorMoving;
951 }
952
953 void eDVBSatelliteEquipmentControl::setRotorMoving(bool b)
954 {
955         m_rotorMoving=b;
956 }