show "in timer" icon also in multiepg
[enigma2.git] / lib / dvb / sec.cpp
1 #include <lib/dvb/dvb.h>
2 #include <lib/dvb/sec.h>
3 #include <lib/dvb/rotor_calc.h>
4
5 #include <set>
6
7 #if HAVE_DVB_API_VERSION < 3
8 #define FREQUENCY Frequency
9 #else
10 #define FREQUENCY frequency
11 #endif
12 #include <lib/base/eerror.h>
13
14 DEFINE_REF(eDVBSatelliteEquipmentControl);
15
16 eDVBSatelliteEquipmentControl *eDVBSatelliteEquipmentControl::instance;
17
18 eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBRegisteredFrontend> &avail_frontends)
19         :m_lnbidx(-1), m_curSat(m_lnbs[0].m_satellites.end()), m_avail_frontends(avail_frontends), m_rotorMoving(false)
20 {
21         if (!instance)
22                 instance = this;
23
24         clear();
25
26 // ASTRA
27         addLNB();
28         setLNBTunerMask(3);
29         setLNBLOFL(9750000);
30         setLNBThreshold(11750000);
31         setLNBLOFH(10607000);
32         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_0);
33         setToneburst(eDVBSatelliteDiseqcParameters::NO);
34         setRepeats(0);
35         setCommittedCommand(eDVBSatelliteDiseqcParameters::BB);
36         setCommandOrder(0); // committed, toneburst
37         setFastDiSEqC(true);
38         setSeqRepeat(false);
39         addSatellite(192);
40         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
41         setToneMode(eDVBSatelliteSwitchParameters::HILO);
42
43 // Hotbird
44         addLNB();
45         setLNBTunerMask(3);
46         setLNBLOFL(9750000);
47         setLNBThreshold(11750000);
48         setLNBLOFH(10600000);
49         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_0);
50         setToneburst(eDVBSatelliteDiseqcParameters::NO);
51         setRepeats(0);
52         setCommittedCommand(eDVBSatelliteDiseqcParameters::AB);
53         setCommandOrder(0); // committed, toneburst
54         setFastDiSEqC(true);
55         setSeqRepeat(false);
56         addSatellite(130);
57         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
58         setToneMode(eDVBSatelliteSwitchParameters::HILO);
59
60 // Rotor
61         addLNB();
62         setLNBTunerMask(3);
63         setLNBLOFL(9750000);
64         setLNBThreshold(11750000);
65         setLNBLOFH(10600000);
66         setDiSEqCMode(eDVBSatelliteDiseqcParameters::V1_2);
67         setToneburst(eDVBSatelliteDiseqcParameters::NO);
68         setRepeats(0);
69         setCommittedCommand(eDVBSatelliteDiseqcParameters::AA);
70         setCommandOrder(0); // committed, toneburst
71         setFastDiSEqC(true);
72         setSeqRepeat(false);
73         setLaDirection(eDVBSatelliteRotorParameters::NORTH);
74         setLoDirection(eDVBSatelliteRotorParameters::EAST);
75         setLatitude(51.017);
76         setLongitude(8.683);
77         setUseInputpower(true);
78         setInputpowerDelta(50);
79
80         addSatellite(235);
81         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
82         setToneMode(eDVBSatelliteSwitchParameters::HILO);
83         setRotorPosNum(0);
84
85         addSatellite(284);
86         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
87         setToneMode(eDVBSatelliteSwitchParameters::HILO);
88         setRotorPosNum(0);
89
90         addSatellite(420);
91         setVoltageMode(eDVBSatelliteSwitchParameters::HV);
92         setToneMode(eDVBSatelliteSwitchParameters::HILO);
93         setRotorPosNum(1); // stored pos 1
94 }
95
96 int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite &sat, iDVBFrontend *fe, int frontend_id )
97 {
98         int ret=0, satcount=0;
99
100         for (int idx=0; idx <= m_lnbidx; ++idx )
101         {
102                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
103                 if ( lnb_param.tuner_mask & frontend_id ) // lnb for correct tuner?
104                 {
105                         eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
106
107                         satcount += lnb_param.m_satellites.size();
108
109                         std::map<int, eDVBSatelliteSwitchParameters>::iterator sit =
110                                 lnb_param.m_satellites.find(sat.orbital_position);
111                         if ( sit != lnb_param.m_satellites.end())
112                         {
113                                 int band=0,
114                                         linked_to=-1, // linked tuner
115                                         satpos_depends_to=-1,
116                                         csw = di_param.m_committed_cmd,
117                                         ucsw = di_param.m_uncommitted_cmd,
118                                         toneburst = di_param.m_toneburst_param,
119                                         curRotorPos;
120
121                                 fe->getData(6, curRotorPos);
122                                 fe->getData(7, linked_to);
123                                 fe->getData(8, satpos_depends_to);
124
125                                 if ( sat.frequency > lnb_param.m_lof_threshold )
126                                         band |= 1;
127                                 if (!(sat.polarisation & eDVBFrontendParametersSatellite::Polarisation::Vertical))
128                                         band |= 2;
129
130                                 bool rotor=false;
131                                 bool diseqc=false;
132
133                                 if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
134                                 {
135                                         diseqc=true;
136                                         if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
137                                                 csw = 0xF0 | (csw << 2);
138
139                                         if (di_param.m_committed_cmd <= eDVBSatelliteDiseqcParameters::SENDNO)
140                                                 csw |= band;
141
142                                         if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )  // ROTOR
143                                                 rotor = true;
144
145                                         ret = 1000;
146                                 }
147                                 else
148                                 {
149                                         csw = band;
150                                         ret = 2000;
151                                 }
152
153                                 if (linked_to != -1)  // check for linked tuners..
154                                 {
155                                         eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_to;
156                                         if (linked_fe->m_inuse)
157                                         {
158                                                 int ocsw = -1,
159                                                         oucsw = -1,
160                                                         oToneburst = -1,
161                                                         oRotorPos = -1;
162                                                 linked_fe->m_frontend->getData(0, ocsw);
163                                                 linked_fe->m_frontend->getData(1, oucsw);
164                                                 linked_fe->m_frontend->getData(2, oToneburst);
165                                                 linked_fe->m_frontend->getData(6, oRotorPos);
166 #if 0
167                                                 eDebug("compare csw %02x == lcsw %02x",
168                                                         csw, ocsw);
169                                                 if ( diseqc )
170                                                         eDebug("compare ucsw %02x == lucsw %02x\ncompare toneburst %02x == oToneburst %02x",
171                                                                 ucsw, oucsw, toneburst, oToneburst);
172                                                 if ( rotor )
173                                                         eDebug("compare pos %d == current pos %d",
174                                                                 sat.orbital_position, oRotorPos);
175 #endif
176                                                 if ( (csw != ocsw) ||
177                                                         ( diseqc && (ucsw != oucsw || toneburst != oToneburst) ) ||
178                                                         ( rotor && oRotorPos != sat.orbital_position ) )
179                                                 {
180 //                                                      eDebug("can not tune this transponder with linked tuner in use!!");
181                                                         ret=0;
182                                                 }
183 //                                              else
184 //                                                      eDebug("OK .. can tune this transponder with linked tuner in use :)");
185                                         }
186                                 }
187
188                                 if (satpos_depends_to != -1)  // check for linked tuners..
189                                 {
190                                         eDVBRegisteredFrontend *satpos_depends_to_fe = (eDVBRegisteredFrontend*) satpos_depends_to;
191                                         if ( satpos_depends_to_fe->m_inuse )
192                                         {
193                                                 int oRotorPos = -1;
194                                                 satpos_depends_to_fe->m_frontend->getData(6, oRotorPos);
195                                                 if (!rotor || oRotorPos != sat.orbital_position)
196                                                 {
197 //                                                      eDebug("can not tune this transponder ... rotor on other tuner is positioned to %d", oRotorPos);
198                                                         ret=0;
199                                                 }
200                                         }
201 //                                      else
202 //                                              eDebug("OK .. can tune this transponder satpos is correct :)");
203                                 }
204                                 if (ret)
205                                 {
206                                         int lof = sat.frequency > lnb_param.m_lof_threshold ?
207                                                 lnb_param.m_lof_hi : lnb_param.m_lof_lo;
208                                         int tuner_freq = abs(sat.frequency - lof);
209 //                                      eDebug("tuner freq %d", tuner_freq);
210                                         if (tuner_freq < 900000 || tuner_freq > 2200000)
211                                         {
212                                                 ret=0;
213 //                                              eDebug("Transponder not tuneable with this lnb... %d Khz out of tuner range",
214 //                                                      tuner_freq);
215                                         }
216                                 }
217                         }
218                 }
219         }
220         if (ret && satcount)
221                 ret -= (satcount-1);
222         return ret;
223 }
224
225 #define VOLTAGE(x) (lnb_param.m_increased_voltage ? iDVBFrontend::voltage##x##_5 : iDVBFrontend::voltage##x)
226
227 RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, const eDVBFrontendParametersSatellite &sat, int frontend_id)
228 {
229         bool linked=false;
230         bool depend_satpos_mode=false;
231
232         for (int idx=0; idx <= m_lnbidx; ++idx )
233         {
234                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
235                 if (!(lnb_param.tuner_mask & frontend_id)) // lnb for correct tuner?
236                         continue;
237                 eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
238                 eDVBSatelliteRotorParameters &rotor_param = lnb_param.m_rotor_parameters;
239
240                 std::map<int, eDVBSatelliteSwitchParameters>::iterator sit =
241                         lnb_param.m_satellites.find(sat.orbital_position);
242                 if ( sit != lnb_param.m_satellites.end())
243                 {
244                         eDVBSatelliteSwitchParameters &sw_param = sit->second;
245                         bool doSetVoltageToneFrontend = true;
246                         bool doSetFrontend = true;
247                         int band=0,
248                                 linked_to=-1, // linked tuner
249                                 satpos_depends_to=-1,
250                                 voltage = iDVBFrontend::voltageOff,
251                                 tone = iDVBFrontend::toneOff,
252                                 csw = di_param.m_committed_cmd,
253                                 ucsw = di_param.m_uncommitted_cmd,
254                                 toneburst = di_param.m_toneburst_param,
255                                 lastcsw = -1,
256                                 lastucsw = -1,
257                                 lastToneburst = -1,
258                                 lastRotorCmd = -1,
259                                 curRotorPos = -1;
260
261                         frontend.getData(0, lastcsw);
262                         frontend.getData(1, lastucsw);
263                         frontend.getData(2, lastToneburst);
264                         frontend.getData(5, lastRotorCmd);
265                         frontend.getData(6, curRotorPos);
266                         frontend.getData(7, linked_to);
267                         frontend.getData(8, satpos_depends_to);
268
269                         if (linked_to != -1)
270                         {
271                                 eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_to;
272                                 if (linked_fe->m_inuse)
273                                 {
274                                         eDebug("[SEC] frontend is linked with another and the other one is in use.. so we dont do SEC!!");
275                                         linked=true;
276                                 }
277                         }
278
279                         if (satpos_depends_to != -1)
280                         {
281                                 eDVBRegisteredFrontend *satpos_fe = (eDVBRegisteredFrontend*) satpos_depends_to;
282                                 if (satpos_fe->m_inuse)
283                                 {
284                                         if ( di_param.m_diseqc_mode != eDVBSatelliteDiseqcParameters::V1_2 )
285                                                 continue;
286                                         eDebug("[SEC] frontend is depending on satpos of other one.. so we dont turn rotor!!");
287                                         depend_satpos_mode=true;
288                                 }
289                         }
290
291                         if ( sat.frequency > lnb_param.m_lof_threshold )
292                                 band |= 1;
293
294                         if (band&1)
295                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_hi;
296                         else
297                                 parm.FREQUENCY = sat.frequency - lnb_param.m_lof_lo;
298
299                         parm.FREQUENCY = abs(parm.FREQUENCY);
300
301                         frontend.setData(9, sat.frequency - parm.FREQUENCY);
302
303                         if (!(sat.polarisation & eDVBFrontendParametersSatellite::Polarisation::Vertical))
304                                 band |= 2;
305
306                         if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_14V
307                                 || ( sat.polarisation & eDVBFrontendParametersSatellite::Polarisation::Vertical
308                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
309                                 voltage = VOLTAGE(13);
310                         else if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_18V
311                                 || ( !(sat.polarisation & eDVBFrontendParametersSatellite::Polarisation::Vertical)
312                                         && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
313                                 voltage = VOLTAGE(18);
314                         if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::ON)
315                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && (band&1) ) )
316                                 tone = iDVBFrontend::toneOn;
317                         else if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::OFF)
318                                 || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && !(band&1) ) )
319                                 tone = iDVBFrontend::toneOff;
320
321                         eSecCommandList sec_sequence;
322
323                         if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
324                         {
325                                 if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
326                                         csw = 0xF0 | (csw << 2);
327
328                                 if (di_param.m_committed_cmd <= eDVBSatelliteDiseqcParameters::SENDNO)
329                                         csw |= band;
330
331                                 bool send_csw =
332                                         (di_param.m_committed_cmd != eDVBSatelliteDiseqcParameters::SENDNO);
333                                 bool changed_csw = send_csw && csw != lastcsw;
334
335                                 bool send_ucsw =
336                                         (di_param.m_uncommitted_cmd && di_param.m_diseqc_mode > eDVBSatelliteDiseqcParameters::V1_0);
337                                 bool changed_ucsw = send_ucsw && ucsw != lastucsw;
338
339                                 bool send_burst =
340                                         (di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO);
341                                 bool changed_burst = send_burst && toneburst != lastToneburst;
342
343                                 int send_mask = 0; /*
344                                         1 must send csw
345                                         2 must send ucsw
346                                         4 send toneburst first
347                                         8 send toneburst at end */
348                                 if (changed_burst) // toneburst first and toneburst changed
349                                 {
350                                         if (di_param.m_command_order&1)
351                                         {
352                                                 send_mask |= 4;
353                                                 if ( send_csw )
354                                                         send_mask |= 1;
355                                                 if ( send_ucsw )
356                                                         send_mask |= 2;
357                                         }
358                                         else
359                                                 send_mask |= 8;
360                                 }
361                                 if (changed_ucsw)
362                                 {
363                                         send_mask |= 2;
364                                         if ((di_param.m_command_order&4) && send_csw)
365                                                 send_mask |= 1;
366                                         if (di_param.m_command_order==4 && send_burst)
367                                                 send_mask |= 8;
368                                 }
369                                 if (changed_csw) 
370                                 {
371                                         if ( di_param.m_use_fast
372                                                 && di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO
373                                                 && (lastcsw & 0xF0)
374                                                 && ((csw / 4) == (lastcsw / 4)) )
375                                                 eDebug("dont send committed cmd (fast diseqc)");
376                                         else
377                                         {
378                                                 send_mask |= 1;
379                                                 if (!(di_param.m_command_order&4) && send_ucsw)
380                                                         send_mask |= 2;
381                                                 if (!(di_param.m_command_order&1) && send_burst)
382                                                         send_mask |= 8;
383                                         }
384                                 }
385
386 #if 0
387                                 eDebugNoNewLine("sendmask: ");
388                                 for (int i=3; i >= 0; --i)
389                                         if ( send_mask & (1<<i) )
390                                                 eDebugNoNewLine("1");
391                                         else
392                                                 eDebugNoNewLine("0");
393                                 eDebug("");
394 #endif
395
396                                 int RotorCmd=-1;
397                                 bool useGotoXX = false;
398                                 if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2
399                                         && !sat.no_rotor_command_on_tune )
400                                 {
401                                         if (depend_satpos_mode || linked)
402                                                 // in this both modes we dont really turn the rotor.... but in canTune we need the satpos
403                                                 frontend.setData(6, sat.orbital_position);
404                                         else
405                                         {
406                                                 if (sw_param.m_rotorPosNum) // we have stored rotor pos?
407                                                         RotorCmd=sw_param.m_rotorPosNum;
408                                                 else  // we must calc gotoxx cmd
409                                                 {
410                                                         eDebug("Entry for %d,%d? not in Rotor Table found... i try gotoXX?", sat.orbital_position / 10, sat.orbital_position % 10 );
411                                                         useGotoXX = true;
412
413                                                         double  SatLon = abs(sat.orbital_position)/10.00,
414                                                                         SiteLat = rotor_param.m_gotoxx_parameters.m_latitude,
415                                                                         SiteLon = rotor_param.m_gotoxx_parameters.m_longitude;
416
417                                                         if ( rotor_param.m_gotoxx_parameters.m_la_direction == eDVBSatelliteRotorParameters::SOUTH )
418                                                                 SiteLat = -SiteLat;
419
420                                                         if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
421                                                                 SiteLon = 360 - SiteLon;
422
423                                                         eDebug("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
424                                                         double satHourAngle =
425                                                                 calcSatHourangle( SatLon, SiteLat, SiteLon );
426                                                         eDebug("PolarmountHourAngle=%lf", satHourAngle );
427
428                                                         static int gotoXTable[10] =
429                                                                 { 0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E };
430
431                                                         if (SiteLat >= 0) // Northern Hemisphere
432                                                         {
433                                                                 int tmp=(int)round( fabs( 180 - satHourAngle ) * 10.0 );
434                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
435
436                                                                 if (satHourAngle < 180) // the east
437                                                                         RotorCmd |= 0xE000;
438                                                                 else                                    // west
439                                                                         RotorCmd |= 0xD000;
440                                                         }
441                                                         else // Southern Hemisphere
442                                                         {
443                                                                 if (satHourAngle < 180) // the east
444                                                                 {
445                                                                         int tmp=(int)round( fabs( satHourAngle ) * 10.0 );
446                                                                         RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
447                                                                         RotorCmd |= 0xD000;
448                                                                 }
449                                                                 else                                    // west
450                                                                 {
451                                                                         int tmp=(int)round( fabs( 360 - satHourAngle ) * 10.0 );
452                                                                         RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
453                                                                         RotorCmd |= 0xE000;
454                                                                 }
455                                                         }
456                                                         eDebug("RotorCmd = %04x", RotorCmd);
457                                                 }
458                                         }
459                                 }
460
461                                 if ( send_mask )
462                                 {
463                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
464                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );
465                                         eSecCommand::pair compare;
466                                         compare.voltage = iDVBFrontend::voltageOff;
467                                         compare.steps = +4;
468                                         // the next is a check if voltage is switched off.. then we first set a voltage :)
469                                         // else we set voltage after all diseqc stuff..
470                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_NOT_VOLTAGE_GOTO, compare) );
471
472                                         if ( RotorCmd != -1 && RotorCmd != lastRotorCmd )
473                                         {
474                                                 if (rotor_param.m_inputpower_parameters.m_use)
475                                                         compare.voltage = VOLTAGE(18);  // in input power mode turn rotor always with 18V (fast)
476                                                 else
477                                                         compare.voltage = VOLTAGE(13);  // in normal mode start turning with 13V
478                                         }
479                                         else
480                                                 compare.voltage = voltage;
481
482                                         // voltage already correct ?
483                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
484                                         compare.steps = +3;
485                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
486                                         // voltage was disabled..so we wait a longer time .. for normal switches 200ms should be enough
487                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 200) );
488
489                                         for (int seq_repeat = 0; seq_repeat < (di_param.m_seq_repeat?2:1); ++seq_repeat)
490                                         {
491                                                 if ( send_mask & 4 )
492                                                 {
493                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
494                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
495                                                 }
496
497                                                 int loops=0;
498
499                                                 if ( send_mask & 1 )
500                                                         ++loops;
501                                                 if ( send_mask & 2 )
502                                                         ++loops;
503
504                                                 for ( int i=0; i < di_param.m_repeats; ++i )
505                                                         loops *= 2;
506
507                                                 for ( int i = 0; i < loops;)  // fill commands...
508                                                 {
509                                                         eDVBDiseqcCommand diseqc;
510                                                         diseqc.len = 4;
511                                                         diseqc.data[0] = i ? 0xE1 : 0xE0;
512                                                         diseqc.data[1] = 0x10;
513                                                         if ( (send_mask & 2) && (di_param.m_command_order & 4) )
514                                                         {
515                                                                 diseqc.data[2] = 0x39;
516                                                                 diseqc.data[3] = ucsw;
517                                                         }
518                                                         else if ( send_mask & 1 )
519                                                         {
520                                                                 diseqc.data[2] = 0x38;
521                                                                 diseqc.data[3] = csw;
522                                                         }
523                                                         else  // no committed command confed.. so send uncommitted..
524                                                         {
525                                                                 diseqc.data[2] = 0x39;
526                                                                 diseqc.data[3] = ucsw;
527                                                         }
528                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
529
530                                                         i++;
531                                                         if ( i < loops )
532                                                         {
533                                                                 int cmd=0;
534                                                                 if (diseqc.data[2] == 0x38 && (send_mask & 2))
535                                                                         cmd=0x39;
536                                                                 else if (diseqc.data[2] == 0x39 && (send_mask & 1))
537                                                                         cmd=0x38;
538                                                                 if (cmd)
539                                                                 {
540                                                                         static int delay = (120 - 54) / 2;  // standard says 100msek between two repeated commands
541                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
542                                                                         diseqc.data[2]=cmd;
543                                                                         diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
544                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
545                                                                         ++i;
546                                                                         if ( i < loops )
547                                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
548                                                                         else
549                                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
550                                                                 }
551                                                                 else  // delay 120msek when no command is in repeat gap
552                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 120) );
553                                                         }
554                                                         else
555                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
556                                                 }
557
558                                                 if ( send_mask & 8 )  // toneburst at end of sequence
559                                                 {
560                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
561                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
562                                                 }
563                                         }
564                                 }
565
566                                 if ( RotorCmd != -1 && RotorCmd != lastRotorCmd )
567                                 {
568                                         eSecCommand::pair compare;
569                                         compare.voltage = iDVBFrontend::voltageOff;
570                                         compare.steps = +4;
571                                         // the next is a check if voltage is switched off.. then we first set a voltage :)
572                                         // else we set voltage after all diseqc stuff..
573                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_NOT_VOLTAGE_GOTO, compare) );
574
575                                         if (rotor_param.m_inputpower_parameters.m_use)
576                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) ); // in normal mode start turning with 13V
577                                         else
578                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) ); // turn always with 18V
579
580                                         // voltage was disabled..so we wait a longer time ..
581                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 500) );
582                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, +7) );  // no need to send stop rotor cmd
583
584                                         if (send_mask)
585                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 750) ); // wait 750ms after send switch cmd
586                                         else
587                                                 sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, +1) );
588
589                                         eDVBDiseqcCommand diseqc;
590                                         diseqc.len = 3;
591                                         diseqc.data[0] = 0xE0;
592                                         diseqc.data[1] = 0x31;  // positioner
593                                         diseqc.data[2] = 0x60;  // stop
594                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_ROTORPOS_VALID_GOTO, +5) );
595                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
596                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
597                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
598                                         // wait 300msec after send rotor stop cmd
599                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 300) );
600
601                                         diseqc.data[0] = 0xE0;
602                                         diseqc.data[1] = 0x31;          // positioner
603                                         if ( useGotoXX )
604                                         {
605                                                 diseqc.len = 5;
606                                                 diseqc.data[2] = 0x6E;  // drive to angular position
607                                                 diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
608                                                 diseqc.data[4] = RotorCmd & 0xFF;
609                                         }
610                                         else
611                                         {
612                                                 diseqc.len = 4;
613                                                 diseqc.data[2] = 0x6B;  // goto stored sat position
614                                                 diseqc.data[3] = RotorCmd;
615                                                 diseqc.data[4] = 0x00;
616                                         }
617
618                                         if ( rotor_param.m_inputpower_parameters.m_use )
619                                         { // use measure rotor input power to detect rotor state
620                                                 eSecCommand::rotor cmd;
621                                                 eSecCommand::pair compare;
622                                                 compare.voltage = VOLTAGE(18);
623                                                 compare.steps = +2;
624                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
625                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
626 // measure idle power values
627                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 200) );  // wait 200msec after voltage change
628                                                 sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 1) );
629                                                 compare.voltage = 1;
630                                                 compare.steps = -2;
631                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO, compare) );
632                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) );
633                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 200) );  // wait 200msec before measure
634                                                 sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 0) );
635                                                 compare.voltage = 0;
636                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO, compare) );
637 ////////////////////////////
638                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
639                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_DISEQC_RETRYS, 2) );  // 2 retries
640                                                 sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
641                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
642                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 40) );  // 2 seconds rotor start timout
643 // rotor start loop
644                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // 50msec delay
645                                                 sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
646                                                 cmd.direction=1;  // check for running rotor
647                                                 cmd.deltaA=rotor_param.m_inputpower_parameters.m_delta;
648                                                 cmd.steps=+5;
649                                                 cmd.okcount=0;
650                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );  // check if rotor has started
651                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +2 ) );  // timeout .. we assume now the rotor is already at the correct position
652                                                 sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // goto loop start
653                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO, +9 ) );  // timeout .. we assume now the rotor is already at the correct position
654                                                 sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -8) );  // goto loop start
655 ////////////////////
656                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 2400) );  // 2 minutes running timeout
657                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
658 // rotor running loop
659                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
660                                                 sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
661                                                 cmd.direction=0;  // check for stopped rotor
662                                                 cmd.steps=+3;
663                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
664                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) );  // timeout ? this should never happen
665                                                 sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // running loop start
666 /////////////////////
667                                         }
668                                         else
669                                         {  // use normal turning mode
670                                                 doSetVoltageToneFrontend=false;
671                                                 doSetFrontend=false;
672                                                 eSecCommand::rotor cmd;
673                                                 eSecCommand::pair compare;
674                                                 compare.voltage = VOLTAGE(13);
675                                                 compare.steps = +2;
676                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
677                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
678                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 200) );  // wait 200msec after voltage change
679
680                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
681                                                 sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
682                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
683
684                                                 compare.voltage = voltage;
685                                                 compare.steps = +3;
686                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // correct final voltage?
687                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 2000) );  // wait 2 second before set high voltage
688                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
689
690                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
691                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
692
693                                                 cmd.direction=1;  // check for running rotor
694                                                 cmd.deltaA=0;
695                                                 cmd.steps=+3;
696                                                 cmd.okcount=0;
697
698                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 480) );  // 2 minutes running timeout
699                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 250) );  // 250msec delay
700                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TUNER_LOCKED_GOTO, cmd ) );
701                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) ); 
702                                                 sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -3) );  // goto loop start
703                                         }
704                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
705                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
706                                         frontend.setData(3, RotorCmd);
707                                         frontend.setData(4, sat.orbital_position);
708                                 }
709                         }
710                         else
711                                 csw = band;
712
713                         frontend.setData(0, csw);
714                         frontend.setData(1, ucsw);
715                         frontend.setData(2, di_param.m_toneburst_param);
716
717                         if (!linked && doSetVoltageToneFrontend)
718                         {
719                                 eSecCommand::pair compare;
720                                 compare.voltage = voltage;
721                                 compare.steps = +3;
722                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
723                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
724                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 10) );
725
726                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
727                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 15) );
728                         }
729
730                         if (doSetFrontend)
731                         {
732                                 sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
733                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
734                         }
735                         frontend.setSecSequence(sec_sequence);
736
737                         return 0;
738                 }
739         }
740
741         eDebug("found no useable satellite configuration for orbital position (%d)", sat.orbital_position );
742         return -1;
743 }
744
745 RESULT eDVBSatelliteEquipmentControl::clear()
746 {
747         for (int i=0; i <= m_lnbidx; ++i)
748         {
749                 m_lnbs[i].m_satellites.clear();
750                 m_lnbs[i].tuner_mask = 0;
751         }
752         m_lnbidx=-1;
753
754 // clear linked tuner configuration
755         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
756                 it->m_frontend->setData(7, -1);
757
758         return 0;
759 }
760
761 // helper function for setTunerLinked and setTunerDepends
762 RESULT eDVBSatelliteEquipmentControl::setDependencyPointers( int tu1, int tu2, int dest_data_byte )
763 {
764         if (tu1 == tu2)
765                 return -1;
766
767         eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
768
769         int cnt=0;
770         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
771         {
772                 if (cnt == tu1)
773                         p1 = *it;
774                 else if (cnt == tu2)
775                         p2 = *it;
776         }
777         if (p1 && p2)
778         {
779                 p1->m_frontend->setData(dest_data_byte, (int)p2);  // this is evil..
780                 p2->m_frontend->setData(dest_data_byte, (int)p1);
781                 return 0;
782         }
783         return -1;
784 }
785
786 /* LNB Specific Parameters */
787 RESULT eDVBSatelliteEquipmentControl::addLNB()
788 {
789         if ( (m_lnbidx+1) < (int)(sizeof(m_lnbs) / sizeof(eDVBSatelliteLNBParameters)))
790                 m_curSat=m_lnbs[++m_lnbidx].m_satellites.end();
791         else
792         {
793                 eDebug("no more LNB free... cnt is %d", m_lnbidx);
794                 return -ENOSPC;
795         }
796         return 0;
797 }
798
799 RESULT eDVBSatelliteEquipmentControl::setLNBTunerMask(int tunermask)
800 {
801         if ( currentLNBValid() )
802                 m_lnbs[m_lnbidx].tuner_mask = tunermask;
803         else
804                 return -ENOENT;
805         return 0;
806 }
807
808 RESULT eDVBSatelliteEquipmentControl::setLNBLOFL(int lofl)
809 {
810         if ( currentLNBValid() )
811                 m_lnbs[m_lnbidx].m_lof_lo = lofl;
812         else
813                 return -ENOENT;
814         return 0;
815 }
816
817 RESULT eDVBSatelliteEquipmentControl::setLNBLOFH(int lofh)
818 {
819         if ( currentLNBValid() )
820                 m_lnbs[m_lnbidx].m_lof_hi = lofh;
821         else
822                 return -ENOENT;
823         return 0;
824 }
825
826 RESULT eDVBSatelliteEquipmentControl::setLNBThreshold(int threshold)
827 {
828         if ( currentLNBValid() )
829                 m_lnbs[m_lnbidx].m_lof_threshold = threshold;
830         else
831                 return -ENOENT;
832         return 0;
833 }
834
835 RESULT eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(bool onoff)
836 {
837         if ( currentLNBValid() )
838                 m_lnbs[m_lnbidx].m_increased_voltage = onoff;
839         else
840                 return -ENOENT;
841         return 0;
842 }
843
844 /* DiSEqC Specific Parameters */
845 RESULT eDVBSatelliteEquipmentControl::setDiSEqCMode(int diseqcmode)
846 {
847         if ( currentLNBValid() )
848                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_diseqc_mode = (eDVBSatelliteDiseqcParameters::t_diseqc_mode)diseqcmode;
849         else
850                 return -ENOENT;
851         return 0;
852 }
853
854 RESULT eDVBSatelliteEquipmentControl::setToneburst(int toneburst)
855 {
856         if ( currentLNBValid() )
857                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_toneburst_param = (eDVBSatelliteDiseqcParameters::t_toneburst_param)toneburst;
858         else
859                 return -ENOENT;
860         return 0;
861 }
862
863 RESULT eDVBSatelliteEquipmentControl::setRepeats(int repeats)
864 {
865         if ( currentLNBValid() )
866                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_repeats=repeats;
867         else
868                 return -ENOENT;
869         return 0;
870 }
871
872 RESULT eDVBSatelliteEquipmentControl::setCommittedCommand(int command)
873 {
874         if ( currentLNBValid() )
875                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_committed_cmd=command;
876         else
877                 return -ENOENT;
878         return 0;
879 }
880
881 RESULT eDVBSatelliteEquipmentControl::setUncommittedCommand(int command)
882 {
883         if ( currentLNBValid() )
884                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_uncommitted_cmd = command;
885         else
886                 return -ENOENT;
887         return 0;
888 }
889
890 RESULT eDVBSatelliteEquipmentControl::setCommandOrder(int order)
891 {
892         if ( currentLNBValid() )
893                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_command_order=order;
894         else
895                 return -ENOENT;
896         return 0;
897 }
898
899 RESULT eDVBSatelliteEquipmentControl::setFastDiSEqC(bool onoff)
900 {
901         if ( currentLNBValid() )
902                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_use_fast=onoff;
903         else
904                 return -ENOENT;
905         return 0;
906 }
907
908 RESULT eDVBSatelliteEquipmentControl::setSeqRepeat(bool onoff)
909 {
910         if ( currentLNBValid() )
911                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_seq_repeat = onoff;
912         else
913                 return -ENOENT;
914         return 0;
915 }
916
917 /* Rotor Specific Parameters */
918 RESULT eDVBSatelliteEquipmentControl::setLongitude(float longitude)
919 {
920         if ( currentLNBValid() )
921                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_longitude=longitude;
922         else
923                 return -ENOENT;
924         return 0;
925 }
926
927 RESULT eDVBSatelliteEquipmentControl::setLatitude(float latitude)
928 {
929         if ( currentLNBValid() )
930                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_latitude=latitude;
931         else
932                 return -ENOENT;
933         return 0;
934 }
935
936 RESULT eDVBSatelliteEquipmentControl::setLoDirection(int direction)
937 {
938         if ( currentLNBValid() )
939                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_lo_direction=direction;
940         else
941                 return -ENOENT;
942         return 0;
943 }
944
945 RESULT eDVBSatelliteEquipmentControl::setLaDirection(int direction)
946 {
947         if ( currentLNBValid() )
948                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_la_direction=direction;
949         else
950                 return -ENOENT;
951         return 0;
952 }
953
954 RESULT eDVBSatelliteEquipmentControl::setUseInputpower(bool onoff)
955 {
956         if ( currentLNBValid() )
957                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_use=onoff;
958         else
959                 return -ENOENT;
960         return 0;
961 }
962
963 RESULT eDVBSatelliteEquipmentControl::setInputpowerDelta(int delta)
964 {
965         if ( currentLNBValid() )
966                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_delta=delta;
967         else
968                 return -ENOENT;
969         return 0;
970 }
971
972 /* Satellite Specific Parameters */
973 RESULT eDVBSatelliteEquipmentControl::addSatellite(int orbital_position)
974 {
975         if ( currentLNBValid() )
976         {
977                 std::map<int, eDVBSatelliteSwitchParameters>::iterator it =
978                         m_lnbs[m_lnbidx].m_satellites.find(orbital_position);
979                 if ( it == m_lnbs[m_lnbidx].m_satellites.end() )
980                 {
981                         std::pair<std::map<int, eDVBSatelliteSwitchParameters>::iterator, bool > ret =
982                                 m_lnbs[m_lnbidx].m_satellites.insert(
983                                         std::pair<int, eDVBSatelliteSwitchParameters>(orbital_position, eDVBSatelliteSwitchParameters())
984                                 );
985                         if ( ret.second )
986                                 m_curSat = ret.first;
987                         else
988                                 return -ENOMEM;
989                 }
990                 else
991                         return -EEXIST;
992         }
993         else
994                 return -ENOENT;
995         return 0;
996 }
997
998 RESULT eDVBSatelliteEquipmentControl::setVoltageMode(int mode)
999 {
1000         if ( currentLNBValid() && m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1001                 m_curSat->second.m_voltage_mode = (eDVBSatelliteSwitchParameters::t_voltage_mode)mode;
1002         else
1003                 return -ENOENT;
1004         return 0;
1005
1006 }
1007
1008 RESULT eDVBSatelliteEquipmentControl::setToneMode(int mode)
1009 {
1010         if ( currentLNBValid() )
1011         {
1012                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1013                         m_curSat->second.m_22khz_signal = (eDVBSatelliteSwitchParameters::t_22khz_signal)mode;
1014                 else
1015                         return -EPERM;
1016         }
1017         else
1018                 return -ENOENT;
1019         return 0;
1020 }
1021
1022 RESULT eDVBSatelliteEquipmentControl::setRotorPosNum(int rotor_pos_num)
1023 {
1024         if ( currentLNBValid() )
1025         {
1026                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1027                         m_curSat->second.m_rotorPosNum=rotor_pos_num;
1028                 else
1029                         return -EPERM;
1030         }
1031         else
1032                 return -ENOENT;
1033         return 0;
1034 }
1035
1036 struct sat_compare
1037 {
1038         int orb_pos, lofl, lofh;
1039         sat_compare(int o, int lofl, int lofh)
1040                 :orb_pos(o), lofl(lofl), lofh(lofh)
1041         {}
1042         sat_compare(const sat_compare &x)
1043                 :orb_pos(x.orb_pos), lofl(x.lofl), lofh(x.lofh)
1044         {}
1045         bool operator < (const sat_compare & cmp) const
1046         {
1047                 if (orb_pos == cmp.orb_pos)
1048                 {
1049                         if ( abs(lofl-cmp.lofl) < 200000 )
1050                         {
1051                                 if (abs(lofh-cmp.lofh) < 200000)
1052                                         return false;
1053                                 return lofh<cmp.lofh;
1054                         }
1055                         return lofl<cmp.lofl;
1056                 }
1057                 return orb_pos < cmp.orb_pos;
1058         }
1059 };
1060
1061 PyObject *eDVBSatelliteEquipmentControl::get_exclusive_satellites(int tu1, int tu2)
1062 {
1063         PyObject *ret=0;
1064
1065         int tu1_mask = 1 << tu1,
1066                 tu2_mask = 1 << tu2;
1067
1068         if (tu1 != tu2)
1069         {
1070                 eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1071                 int cnt=0;
1072                 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
1073                 {
1074                         if (cnt == tu1)
1075                                 p1 = *it;
1076                         else if (cnt == tu2)
1077                                 p2 = *it;
1078                 }
1079                 if (p1 && p2)
1080                 {
1081                         // check for linked tuners
1082                         int tmp1, tmp2;
1083                         p1->m_frontend->getData(7, tmp1);
1084                         p2->m_frontend->getData(7, tmp2);
1085                         if ((void*)tmp1 != p2 && (void*)tmp2 != p1)
1086                         {
1087                                 // check for rotor dependency
1088                                 p1->m_frontend->getData(8, tmp1);
1089                                 p2->m_frontend->getData(8, tmp2);
1090                                 if ((void*)tmp1 != p2 && (void*)tmp2 != p1)
1091                                 {
1092                                         std::set<sat_compare> tu1sats, tu2sats;
1093                                         std::list<sat_compare> tu1difference, tu2difference;
1094                                         std::insert_iterator<std::list<sat_compare> > insert1(tu1difference, tu1difference.begin()),
1095                                                 insert2(tu2difference, tu2difference.begin());
1096                                         for (int idx=0; idx <= m_lnbidx; ++idx )
1097                                         {
1098                                                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
1099                                                 for (std::map<int, eDVBSatelliteSwitchParameters>::iterator sit(lnb_param.m_satellites.begin());
1100                                                         sit != lnb_param.m_satellites.end(); ++sit)
1101                                                 {
1102                                                         if ( lnb_param.tuner_mask & tu1_mask )
1103                                                                 tu1sats.insert(sat_compare(sit->first, lnb_param.m_lof_lo, lnb_param.m_lof_hi));
1104                                                         if ( lnb_param.tuner_mask & tu2_mask )
1105                                                                 tu2sats.insert(sat_compare(sit->first, lnb_param.m_lof_lo, lnb_param.m_lof_hi));
1106                                                 }
1107                                         }
1108                                         std::set_difference(tu1sats.begin(), tu1sats.end(),
1109                                                 tu2sats.begin(), tu2sats.end(),
1110                                                 insert1);
1111                                         std::set_difference(tu2sats.begin(), tu2sats.end(),
1112                                                 tu1sats.begin(), tu1sats.end(),
1113                                                 insert2);
1114                                         if (!tu1sats.empty() || !tu2sats.empty())
1115                                         {
1116                                                 int idx=0;
1117                                                 ret = PyList_New(2+tu1difference.size()+tu2difference.size());
1118
1119                                                 PyList_SET_ITEM(ret, idx++, PyInt_FromLong(tu1difference.size()));
1120                                                 for(std::list<sat_compare>::iterator it(tu1difference.begin()); it != tu1difference.end(); ++it)
1121                                                         PyList_SET_ITEM(ret, idx++, PyInt_FromLong(it->orb_pos));
1122
1123                                                 PyList_SET_ITEM(ret, idx++, PyInt_FromLong(tu2difference.size()));
1124                                                 for(std::list<sat_compare>::iterator it(tu2difference.begin()); it != tu2difference.end(); ++it)
1125                                                         PyList_SET_ITEM(ret, idx++, PyInt_FromLong(it->orb_pos));
1126                                         }
1127                                 }
1128                         }
1129                 }
1130         }
1131         if (!ret)
1132         {
1133                 ret = PyList_New(2);
1134                 PyList_SET_ITEM(ret, 0, PyInt_FromLong(0));
1135                 PyList_SET_ITEM(ret, 1, PyInt_FromLong(0));
1136         }
1137         return ret;
1138 }
1139
1140 RESULT eDVBSatelliteEquipmentControl::setTunerLinked(int tu1, int tu2)
1141 {
1142         return setDependencyPointers(tu1, tu2, 7);
1143 }
1144
1145 RESULT eDVBSatelliteEquipmentControl::setTunerDepends(int tu1, int tu2)
1146 {
1147         return setDependencyPointers(tu1, tu2, 8);
1148 }
1149
1150 bool eDVBSatelliteEquipmentControl::isRotorMoving()
1151 {
1152         return m_rotorMoving;
1153 }
1154
1155 void eDVBSatelliteEquipmentControl::setRotorMoving(bool b)
1156 {
1157         m_rotorMoving=b;
1158 }