always initialize diseqc.data to make compiler happy
[enigma2.git] / lib / dvb / sec.cpp
1 #include <config.h>
2 #include <lib/dvb/dvb.h>
3 #include <lib/dvb/sec.h>
4 #include <lib/dvb/rotor_calc.h>
5
6 #if HAVE_DVB_API_VERSION < 3
7 #define INVERSION Inversion
8 #define FREQUENCY Frequency
9 #define FEC_INNER FEC_inner
10 #define SYMBOLRATE SymbolRate
11 #else
12 #define INVERSION inversion
13 #define FREQUENCY frequency
14 #define FEC_INNER fec_inner
15 #define SYMBOLRATE symbol_rate
16 #endif
17 #include <lib/base/eerror.h>
18
19 DEFINE_REF(eDVBSatelliteEquipmentControl);
20
21 eDVBSatelliteEquipmentControl *eDVBSatelliteEquipmentControl::instance;
22
23 eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBRegisteredFrontend> &avail_frontends)
24         :m_lnbidx(-1), m_curSat(m_lnbs[0].m_satellites.end()), m_avail_frontends(avail_frontends), m_rotorMoving(false)
25 {
26         if (!instance)
27                 instance = this;
28
29         clear();
30
31 // ASTRA
32         addLNB();
33         setLNBTunerMask(3);
34         setLNBLOFL(9750000);
35         setLNBThreshold(11750000);
36         setLNBLOFH(10607000);
37         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_0);
38         setToneburst(eDVBSatelliteDiseqcParameters::NO);
39         setRepeats(0);
40         setCommittedCommand(eDVBSatelliteDiseqcParameters::BB);
41         setCommandOrder(0); // committed, toneburst
42         setFastDiSEqC(true);
43         setSeqRepeat(false);
44         addSatellite(192);
45         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
46         setToneMode(eDVBSatelliteSwitchParameters::HILO);
47
48 // Hotbird
49         addLNB();
50         setLNBTunerMask(3);
51         setLNBLOFL(9750000);
52         setLNBThreshold(11750000);
53         setLNBLOFH(10600000);
54         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_0);
55         setToneburst(eDVBSatelliteDiseqcParameters::NO);
56         setRepeats(0);
57         setCommittedCommand(eDVBSatelliteDiseqcParameters::AB);
58         setCommandOrder(0); // committed, toneburst
59         setFastDiSEqC(true);
60         setSeqRepeat(false);
61         addSatellite(130);
62         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
63         setToneMode(eDVBSatelliteSwitchParameters::HILO);
64
65 // Rotor
66         addLNB();
67         setLNBTunerMask(3);
68         setLNBLOFL(9750000);
69         setLNBThreshold(11750000);
70         setLNBLOFH(10600000);
71         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_2);
72         setToneburst(eDVBSatelliteDiseqcParameters::NO);
73         setRepeats(0);
74         setCommittedCommand(eDVBSatelliteDiseqcParameters::AA);
75         setCommandOrder(0); // committed, toneburst
76         setFastDiSEqC(true);
77         setSeqRepeat(false);
78         setLaDirection(eDVBSatelliteRotorParameters::NORTH);
79         setLoDirection(eDVBSatelliteRotorParameters::EAST);
80         setLatitude(51.017);
81         setLongitude(8.683);
82         setUseInputpower(true);
83         setInputpowerDelta(50);
84
85         addSatellite(235);
86         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
87         setToneMode(eDVBSatelliteSwitchParameters::HILO);
88         setRotorPosNum(0);
89
90         addSatellite(284);
91         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
92         setToneMode(eDVBSatelliteSwitchParameters::HILO);
93         setRotorPosNum(0);
94
95         addSatellite(420);
96         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
97         setToneMode(eDVBSatelliteSwitchParameters::HILO);
98         setRotorPosNum(1); // stored pos 1
99 }
100
101 int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite &sat, iDVBFrontend *fe, int frontend_id )
102 {
103         int ret=0;
104
105         for (int idx=0; idx <= m_lnbidx; ++idx )
106         {
107                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
108                 if ( lnb_param.tuner_mask & frontend_id ) // lnb for correct tuner?
109                 {
110                         eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
111
112                         std::map<int, eDVBSatelliteSwitchParameters>::iterator sit =
113                                 lnb_param.m_satellites.find(sat.orbital_position);
114                         if ( sit != lnb_param.m_satellites.end())
115                         {
116                                 int band=0,
117                                         linked_to=0, // linked tuner
118                                         csw = di_param.m_committed_cmd,
119                                         ucsw = di_param.m_uncommitted_cmd,
120                                         toneburst = di_param.m_toneburst_param,
121                                         curRotorPos;
122
123                                 fe->getData(6, curRotorPos);
124                                 fe->getData(7, linked_to);
125
126                                 if ( sat.frequency > lnb_param.m_lof_threshold )
127                                         band |= 1;
128                                 if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
129                                         band |= 2;
130
131                                 bool rotor=false;
132                                 bool diseqc=false;
133
134                                 if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
135                                 {
136                                         diseqc=true;
137                                         if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
138                                                 csw = 0xF0 | (csw << 2);
139
140                                         csw |= band;
141
142                                         if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )  // ROTOR
143                                         {
144                                                 rotor=true;
145                                                 if ( curRotorPos == sat.orbital_position )
146                                                         ret=20;
147                                                 else
148                                                         ret=10;
149                                         }
150                                 }
151                                 else
152                                         csw = band;
153
154                                 if (!ret)
155                                         ret=40;
156
157                                 if (linked_to != -1)  // check for linked tuners..
158                                 {
159                                         eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_to;
160                                         if (linked_fe->m_inuse)
161                                         {
162                                                 int ocsw = -1,
163                                                         oucsw = -1,
164                                                         oToneburst = -1,
165                                                         oRotorPos = -1;
166                                                 linked_fe->m_frontend->getData(0, ocsw);
167                                                 linked_fe->m_frontend->getData(1, oucsw);
168                                                 linked_fe->m_frontend->getData(2, oToneburst);
169                                                 linked_fe->m_frontend->getData(6, oRotorPos);
170 #if 0
171                                                 eDebug("compare csw %02x == lcsw %02x",
172                                                         csw, ocsw);
173                                                 if ( diseqc )
174                                                         eDebug("compare ucsw %02x == lucsw %02x\ncompare toneburst %02x == oToneburst %02x",
175                                                                 ucsw, oucsw, toneburst, oToneburst);
176                                                 if ( rotor )
177                                                         eDebug("compare pos %d == current pos %d",
178                                                                 sat.orbital_position, oRotorPos);
179 #endif
180                                                 if ( (csw != ocsw) ||
181                                                         ( diseqc && (ucsw != oucsw || toneburst != oToneburst) ) ||
182                                                         ( rotor && oRotorPos != sat.orbital_position ) )
183                                                 {
184 //                                                      eDebug("can not tune this transponder with linked tuner in use!!");
185                                                         ret=0;
186                                                 }
187 //                                              else
188 //                                                      eDebug("OK .. can tune this transponder with linked tuner in use :)");
189                                         }
190                                 }
191                         }
192                 }
193         }
194         return ret;
195 }
196
197 #define VOLTAGE(x) (lnb_param.m_increased_voltage ? iDVBFrontend::voltage##x##_5 : iDVBFrontend::voltage##x)
198
199 RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, eDVBFrontendParametersSatellite &sat, int frontend_id)
200 {
201         bool linked=false;
202
203         for (int idx=0; idx <= m_lnbidx; ++idx )
204         {
205                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
206                 if (!(lnb_param.tuner_mask & frontend_id)) // lnb for correct tuner?
207                         continue;
208                 eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
209                 eDVBSatelliteRotorParameters &rotor_param = lnb_param.m_rotor_parameters;
210
211                 std::map<int, eDVBSatelliteSwitchParameters>::iterator sit =
212                         lnb_param.m_satellites.find(sat.orbital_position);
213                 if ( sit != lnb_param.m_satellites.end())
214                 {
215                         eDVBSatelliteSwitchParameters &sw_param = sit->second;
216
217                         int band=0,
218                                 linked_to=-1, // linked tuner
219                                 voltage = iDVBFrontend::voltageOff,
220                                 tone = iDVBFrontend::toneOff,
221                                 csw = di_param.m_committed_cmd,
222                                 ucsw = di_param.m_uncommitted_cmd,
223                                 toneburst = di_param.m_toneburst_param,
224                                 lastcsw = -1,
225                                 lastucsw = -1,
226                                 lastToneburst = -1,
227                                 lastRotorCmd = -1,
228                                 curRotorPos = -1;
229
230                         frontend.getData(0, lastcsw);
231                         frontend.getData(1, lastucsw);
232                         frontend.getData(2, lastToneburst);
233                         frontend.getData(5, lastRotorCmd);
234                         frontend.getData(6, curRotorPos);
235                         frontend.getData(7, linked_to);
236
237                         if (linked_to != -1)
238                         {
239                                 eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_to;
240                                 if (linked_fe->m_inuse)
241                                 {
242                                         eDebug("[SEC] frontend is linked with another and the other one is in use.. so we dont do SEC!!");
243                                         linked=true;
244                                 }
245                         }
246
247                         if ( sat.frequency > lnb_param.m_lof_threshold )
248                                 band |= 1;
249
250                         if (band&1)
251                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_hi;
252                         else
253                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_lo;
254
255                         if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
256                                 band |= 2;
257
258                         switch (sat.inversion)
259                         {
260                                 case eDVBFrontendParametersCable::Inversion::On:
261                                         parm.INVERSION = INVERSION_ON;
262                                         break;
263                                 case eDVBFrontendParametersCable::Inversion::Off:
264                                         parm.INVERSION = INVERSION_OFF;
265                                         break;
266                                 default:
267                                 case eDVBFrontendParametersCable::Inversion::Unknown:
268                                         parm.INVERSION = INVERSION_AUTO;
269                                         break;
270                         }
271
272                         switch (sat.fec)
273                         {
274                                 default:
275                                 case eDVBFrontendParametersSatellite::FEC::fNone:
276                                         eDebug("no fec set.. assume auto");
277                                 case eDVBFrontendParametersSatellite::FEC::fAuto:
278                                         parm.u.qpsk.FEC_INNER = FEC_AUTO;
279                                         break;
280                                 case eDVBFrontendParametersSatellite::FEC::f1_2:
281                                         parm.u.qpsk.FEC_INNER = FEC_1_2;
282                                         break;
283                                 case eDVBFrontendParametersSatellite::FEC::f2_3:
284                                         parm.u.qpsk.FEC_INNER = FEC_2_3;
285                                         break;
286                                 case eDVBFrontendParametersSatellite::FEC::f3_4:
287                                         parm.u.qpsk.FEC_INNER = FEC_3_4;
288                                         break;
289                                 case eDVBFrontendParametersSatellite::FEC::f5_6:
290                                         parm.u.qpsk.FEC_INNER = FEC_5_6;
291                                         break;
292                                 case eDVBFrontendParametersSatellite::FEC::f7_8: 
293                                         parm.u.qpsk.FEC_INNER = FEC_7_8;
294                                         break;
295                         }
296
297                         parm.u.qpsk.SYMBOLRATE = sat.symbol_rate;
298
299                         if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_14V
300                                 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Vertical
301                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
302                                 voltage = VOLTAGE(13);
303                         else if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_18V
304                                 || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal
305                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
306                                 voltage = VOLTAGE(18);
307                         if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::ON)
308                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && (band&1) ) )
309                                 tone = iDVBFrontend::toneOn;
310                         else if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::OFF)
311                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && !(band&1) ) )
312                                 tone = iDVBFrontend::toneOff;
313
314                         eSecCommandList sec_sequence;
315
316                         if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
317                         {
318                                 if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
319                                         csw = 0xF0 | (csw << 2);
320
321                                 csw |= band;
322
323                                 bool send_csw =
324                                         (di_param.m_committed_cmd != eDVBSatelliteDiseqcParameters::SENDNO);
325                                 bool changed_csw = send_csw && csw != lastcsw;
326
327                                 bool send_ucsw =
328                                         (di_param.m_uncommitted_cmd && di_param.m_diseqc_mode > eDVBSatelliteDiseqcParameters::V1_0);
329                                 bool changed_ucsw = send_ucsw && ucsw != lastucsw;
330
331                                 bool send_burst =
332                                         (di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO);
333                                 bool changed_burst = send_burst && toneburst != lastToneburst;
334
335                                 int send_mask = 0; /*
336                                         1 must send csw
337                                         2 must send ucsw
338                                         4 send toneburst first
339                                         8 send toneburst at end */
340                                 if (changed_burst) // toneburst first and toneburst changed
341                                 {
342                                         if (di_param.m_command_order&1)
343                                         {
344                                                 send_mask |= 4;
345                                                 if ( send_csw )
346                                                         send_mask |= 1;
347                                                 if ( send_ucsw )
348                                                         send_mask |= 2;
349                                         }
350                                         else
351                                                 send_mask |= 8;
352                                 }
353                                 if (changed_ucsw)
354                                 {
355                                         send_mask |= 2;
356                                         if ((di_param.m_command_order&4) && send_csw)
357                                                 send_mask |= 1;
358                                         if (di_param.m_command_order==4 && send_burst)
359                                                 send_mask |= 8;
360                                 }
361                                 if (changed_csw) 
362                                 {
363                                         if ( di_param.m_use_fast && (lastcsw & 0xF0) && ((csw / 4) == (lastcsw / 4)) )
364                                                 eDebug("dont send committed cmd (fast diseqc)");
365                                         else
366                                         {
367                                                 send_mask |= 1;
368                                                 if (!(di_param.m_command_order&4) && send_ucsw)
369                                                         send_mask |= 2;
370                                                 if (!(di_param.m_command_order&1) && send_burst)
371                                                         send_mask |= 8;
372                                         }
373                                 }
374
375 #if 0
376                                 eDebugNoNewLine("sendmask: ");
377                                 for (int i=3; i >= 0; --i)
378                                         if ( send_mask & (1<<i) )
379                                                 eDebugNoNewLine("1");
380                                         else
381                                                 eDebugNoNewLine("0");
382                                 eDebug("");
383 #endif
384
385                                 if ( send_mask )
386                                 {
387                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
388                                         eSecCommand::pair compare;
389                                         compare.voltage = voltage;
390                                         compare.steps = +3;
391                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
392                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
393                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
394                                 }
395
396                                 for (int seq_repeat = 0; seq_repeat < (di_param.m_seq_repeat?2:1); ++seq_repeat)
397                                 {
398                                         if ( send_mask & 4 )
399                                         {
400                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
401                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
402                                         }
403
404                                         int loops=0;
405
406                                         if ( send_mask & 1 )
407                                                 ++loops;
408                                         if ( send_mask & 2 )
409                                                 ++loops;
410
411                                         for ( int i=0; i < di_param.m_repeats; ++i )
412                                                 loops *= 2;
413
414                                         for ( int i = 0; i < loops;)  // fill commands...
415                                         {
416                                                 eDVBDiseqcCommand diseqc;
417                                                 diseqc.len = 4;
418                                                 diseqc.data[0] = i ? 0xE1 : 0xE0;
419                                                 diseqc.data[1] = 0x10;
420                                                 if ( (send_mask & 2) && (di_param.m_command_order & 4) )
421                                                 {
422                                                         diseqc.data[2] = 0x39;
423                                                         diseqc.data[3] = ucsw;
424                                                 }
425                                                 else if ( send_mask & 1 )
426                                                 {
427                                                         diseqc.data[2] = 0x38;
428                                                         diseqc.data[3] = csw;
429                                                 }
430                                                 else
431                                                 {
432                                                         diseqc.data[2] = 0x00;
433                                                         diseqc.data[3] = 0x00;
434                                                 }
435                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
436
437                                                 i++;
438                                                 if ( i < loops )
439                                                 {
440                                                         int cmd=0;
441                                                         if (diseqc.data[2] == 0x38 && (send_mask & 2))
442                                                                 cmd=0x39;
443                                                         else if (diseqc.data[2] == 0x39 && (send_mask & 1))
444                                                                 cmd=0x38;
445                                                         if (cmd)
446                                                         {
447                                                                 static int delay = (120 - 54) / 2;  // standard says 100msek between two repeated commands
448                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
449                                                                 diseqc.data[2]=cmd;
450                                                                 diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
451                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
452                                                                 ++i;
453                                                                 if ( i < loops )
454                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
455                                                                 else
456                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
457                                                         }
458                                                         else  // delay 120msek when no command is in repeat gap
459                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 120) );
460                                                 }
461                                                 else
462                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
463                                         }
464
465                                         if ( send_mask & 8 )  // toneburst at end of sequence
466                                         {
467                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
468                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
469                                         }
470                                 }
471
472                                 if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )
473                                 {
474                                         int RotorCmd=0;
475                                         bool useGotoXX = false;
476
477                                         if (sw_param.m_rotorPosNum) // we have stored rotor pos?
478                                                 RotorCmd=sw_param.m_rotorPosNum;
479                                         else  // we must calc gotoxx cmd
480                                         {
481                                                 eDebug("Entry for %d,%d? not in Rotor Table found... i try gotoXX?", sat.orbital_position / 10, sat.orbital_position % 10 );
482                                                 useGotoXX = true;
483
484                                                 double  SatLon = abs(sat.orbital_position)/10.00,
485                                                                 SiteLat = rotor_param.m_gotoxx_parameters.m_latitude,
486                                                                 SiteLon = rotor_param.m_gotoxx_parameters.m_longitude;
487
488                                                 if ( rotor_param.m_gotoxx_parameters.m_la_direction == eDVBSatelliteRotorParameters::SOUTH )
489                                                         SiteLat = -SiteLat;
490
491                                                 if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
492                                                         SiteLon = 360 - SiteLon;
493
494                                                 eDebug("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
495                                                 double satHourAngle =
496                                                         calcSatHourangle( SatLon, SiteLat, SiteLon );
497                                                 eDebug("PolarmountHourAngle=%lf", satHourAngle );
498
499                                                 static int gotoXTable[10] =
500                                                         { 0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E };
501
502                                                 if (SiteLat >= 0) // Northern Hemisphere
503                                                 {
504                                                         int tmp=(int)round( fabs( 180 - satHourAngle ) * 10.0 );
505                                                         RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
506
507                                                         if (satHourAngle < 180) // the east
508                                                                 RotorCmd |= 0xE000;
509                                                         else                                    // west
510                                                                 RotorCmd |= 0xD000;
511                                                 }
512                                                 else // Southern Hemisphere
513                                                 {
514                                                         if (satHourAngle < 180) // the east
515                                                         {
516                                                                 int tmp=(int)round( fabs( satHourAngle ) * 10.0 );
517                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
518                                                                 RotorCmd |= 0xD000;
519                                                         }
520                                                         else                                    // west
521                                                         {          
522                                                                 int tmp=(int)round( fabs( 360 - satHourAngle ) * 10.0 );
523                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
524                                                                 RotorCmd |= 0xE000;
525                                                         }
526                                                 }
527                                                 eDebug("RotorCmd = %04x", RotorCmd);
528                                         }
529                                         if ( RotorCmd != lastRotorCmd )
530                                         {
531                                                 if ( send_mask )
532                                                 {
533                                                         // override first voltage change
534                                                         *(++(++sec_sequence.begin()))=eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13));
535                                                         // wait 1 second after first switch diseqc command
536                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 1000) );
537                                                 }
538                                                 else  // no other diseqc commands before
539                                                 {
540                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
541                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );  // wait 15msec after tone change
542                                                         eSecCommand::pair compare;
543                                                         compare.voltage = voltage;
544                                                         compare.steps = +3;
545                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
546                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) );
547                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
548                                                 }
549
550                                                 eDVBDiseqcCommand diseqc;
551                                                 diseqc.data[0] = 0xE0;
552                                                 diseqc.data[1] = 0x31;          // positioner
553                                                 if ( useGotoXX )
554                                                 {
555                                                         diseqc.len = 5;
556                                                         diseqc.data[2] = 0x6E;  // drive to angular position
557                                                         diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
558                                                         diseqc.data[4] = RotorCmd & 0xFF;
559                                                 }
560                                                 else
561                                                 {
562                                                         diseqc.len = 4;
563                                                         diseqc.data[2] = 0x6B;  // goto stored sat position
564                                                         diseqc.data[3] = RotorCmd;
565                                                         diseqc.data[4] = 0x00;
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, VOLTAGE(18)) );
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, VOLTAGE(13)) ); // 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, +9 ) );  // 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, VOLTAGE(18)) );
598 // rotor running loop
599                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
600                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
601                                                         cmd.direction=0;  // check for stopped rotor
602                                                         cmd.steps=+3;
603                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
604                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) );  // timeout ? this should never happen
605                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // running loop start
606 /////////////////////
607                                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
608                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
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 }