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