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