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