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