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