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