set voltage before send diseqc command (like DiSEqC spec)
[vuplus_dvbapp] / 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                                                 int vlt = iDVBFrontend::voltageOff;
464                                                 eSecCommand::pair compare;
465                                                 compare.steps = +3;
466                                                 compare.tone = iDVBFrontend::toneOff;
467                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
468                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
469                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE]) );
470
471                                                 if ( RotorCmd != -1 && RotorCmd != lastRotorCmd )
472                                                 {
473                                                         if (rotor_param.m_inputpower_parameters.m_use)
474                                                                 vlt = VOLTAGE(18);  // in input power mode set 18V for measure input power
475                                                         else
476                                                                 vlt = VOLTAGE(13);  // in normal mode start turning with 13V
477                                                 }
478                                                 else
479                                                         vlt = voltage;
480
481                                                 // check if voltage is already correct..
482                                                 compare.voltage = vlt;
483                                                 compare.steps = +7;
484                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
485
486                                                 // check if voltage is disabled
487                                                 compare.voltage = iDVBFrontend::voltageOff;
488                                                 compare.steps = +4;
489                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
490
491                                                 // voltage is changed... use DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_SWITCH_CMDS
492                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, vlt) );
493                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_SWITCH_CMDS]) );
494                                                 sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, +3) );
495
496                                                 // voltage was disabled.. use DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS
497                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, vlt) );
498                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS]) );
499
500                                                 for (int seq_repeat = 0; seq_repeat < (di_param.m_seq_repeat?2:1); ++seq_repeat)
501                                                 {
502                                                         if ( send_mask & 4 )
503                                                         {
504                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
505                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_TONEBURST]) );
506                                                         }
507
508                                                         int loops=0;
509
510                                                         if ( send_mask & 1 )
511                                                                 ++loops;
512                                                         if ( send_mask & 2 )
513                                                                 ++loops;
514
515                                                         loops <<= di_param.m_repeats;
516
517                                                         for ( int i = 0; i < loops;)  // fill commands...
518                                                         {
519                                                                 eDVBDiseqcCommand diseqc;
520                                                                 diseqc.len = 4;
521                                                                 diseqc.data[0] = i ? 0xE1 : 0xE0;
522                                                                 diseqc.data[1] = 0x10;
523                                                                 if ( (send_mask & 2) && (di_param.m_command_order & 4) )
524                                                                 {
525                                                                         diseqc.data[2] = 0x39;
526                                                                         diseqc.data[3] = ucsw;
527                                                                 }
528                                                                 else if ( send_mask & 1 )
529                                                                 {
530                                                                         diseqc.data[2] = 0x38;
531                                                                         diseqc.data[3] = csw;
532                                                                 }
533                                                                 else  // no committed command confed.. so send uncommitted..
534                                                                 {
535                                                                         diseqc.data[2] = 0x39;
536                                                                         diseqc.data[3] = ucsw;
537                                                                 }
538                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
539
540                                                                 i++;
541                                                                 if ( i < loops )
542                                                                 {
543                                                                         int cmd=0;
544                                                                         if (diseqc.data[2] == 0x38 && (send_mask & 2))
545                                                                                 cmd=0x39;
546                                                                         else if (diseqc.data[2] == 0x39 && (send_mask & 1))
547                                                                                 cmd=0x38;
548                                                                         int tmp = m_params[DELAY_BETWEEN_DISEQC_REPEATS];
549                                                                         if (cmd)
550                                                                         {
551                                                                                 int delay = di_param.m_repeats ? (tmp - 54) / 2 : tmp;  // standard says 100msek between two repeated commands
552                                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
553                                                                                 diseqc.data[2]=cmd;
554                                                                                 diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
555                                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
556                                                                                 ++i;
557                                                                                 if ( i < loops )
558                                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
559                                                                                 else
560                                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_LAST_DISEQC_CMD]) );
561                                                                         }
562                                                                         else  // delay 120msek when no command is in repeat gap
563                                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, tmp) );
564                                                                 }
565                                                                 else
566                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_LAST_DISEQC_CMD]) );
567                                                         }
568
569                                                         if ( send_mask & 8 )  // toneburst at end of sequence
570                                                         {
571                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
572                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_TONEBURST]) );
573                                                         }
574                                                 }
575                                         }
576
577                                         eDebug("RotorCmd %02x, lastRotorCmd %02x", RotorCmd, lastRotorCmd);
578                                         if ( RotorCmd != -1 && RotorCmd != lastRotorCmd )
579                                         {
580                                                 eSecCommand::pair compare;
581                                                 if (!send_mask)
582                                                 {
583                                                         compare.steps = +3;
584                                                         compare.tone = iDVBFrontend::toneOff;
585                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
586                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
587                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE]) );
588
589                                                         compare.voltage = iDVBFrontend::voltageOff;
590                                                         compare.steps = +4;
591                                                         // the next is a check if voltage is switched off.. then we first set a voltage :)
592                                                         // else we set voltage after all diseqc stuff..
593                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_NOT_VOLTAGE_GOTO, compare) );
594
595                                                         if (rotor_param.m_inputpower_parameters.m_use)
596                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) ); // set 18V for measure input power
597                                                         else
598                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) ); // in normal mode start turning with 13V
599
600                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_MOTOR_CMD]) ); // wait 750ms when voltage was disabled
601                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, +9) );  // no need to send stop rotor cmd and recheck voltage
602                                                 }
603                                                 else
604                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_BETWEEN_SWITCH_AND_MOTOR_CMD]) ); // wait 700ms when diseqc changed
605
606                                                 eDVBDiseqcCommand diseqc;
607                                                 diseqc.len = 3;
608                                                 diseqc.data[0] = 0xE0;
609                                                 diseqc.data[1] = 0x31;  // positioner
610                                                 diseqc.data[2] = 0x60;  // stop
611                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_ROTORPOS_VALID_GOTO, +5) );
612                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
613                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
614                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
615                                                 // wait 150msec after send rotor stop cmd
616                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_MOTOR_STOP_CMD]) );
617
618                                                 diseqc.data[0] = 0xE0;
619                                                 diseqc.data[1] = 0x31;          // positioner
620                                                 if ( useGotoXX )
621                                                 {
622                                                         diseqc.len = 5;
623                                                         diseqc.data[2] = 0x6E;  // drive to angular position
624                                                         diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
625                                                         diseqc.data[4] = RotorCmd & 0xFF;
626                                                 }
627                                                 else
628                                                 {
629                                                         diseqc.len = 4;
630                                                         diseqc.data[2] = 0x6B;  // goto stored sat position
631                                                         diseqc.data[3] = RotorCmd;
632                                                         diseqc.data[4] = 0x00;
633                                                 }
634
635                                                 if ( rotor_param.m_inputpower_parameters.m_use )
636                                                 { // use measure rotor input power to detect rotor state
637                                                         eSecCommand::rotor cmd;
638                                                         eSecCommand::pair compare;
639                                                         compare.voltage = VOLTAGE(18);
640                                                         compare.steps = +3;
641                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
642                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
643 // measure idle power values
644                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MEASURE_IDLE_INPUTPOWER]) );  // wait 150msec after voltage change
645                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 1) );
646                                                         compare.val = 1;
647                                                         compare.steps = -2;
648                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO, compare) );
649                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) );
650                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MEASURE_IDLE_INPUTPOWER]) );  // wait 150msec before measure
651                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 0) );
652                                                         compare.val = 0;
653                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO, compare) );
654 ////////////////////////////
655                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
656                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_DISEQC_RETRYS, m_params[MOTOR_COMMAND_RETRIES]) );  // 2 retries
657                                                         sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
658                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
659                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 40) );  // 2 seconds rotor start timout
660 // rotor start loop
661                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // 50msec delay
662                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
663                                                         cmd.direction=1;  // check for running rotor
664                                                         cmd.deltaA=rotor_param.m_inputpower_parameters.m_delta;
665                                                         cmd.steps=+5;
666                                                         cmd.okcount=0;
667                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );  // check if rotor has started
668                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +2 ) );  // timeout .. we assume now the rotor is already at the correct position
669                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // goto loop start
670                                                         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
671                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -8) );  // goto loop start
672 ////////////////////
673                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
674                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
675                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, m_params[MOTOR_RUNNING_TIMEOUT]*20) );  // 2 minutes running timeout
676 // rotor running loop
677                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
678                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
679                                                         cmd.direction=0;  // check for stopped rotor
680                                                         cmd.steps=+4;
681                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
682                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +4 ) );  // timeout ? this should never happen
683                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // running loop start
684 /////////////////////
685                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
686                                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
687                                                 }
688                                                 else
689                                                 {  // use normal turning mode
690                                                         doSetVoltageToneFrontend=false;
691                                                         doSetFrontend=false;
692                                                         eSecCommand::rotor cmd;
693                                                         eSecCommand::pair compare;
694                                                         compare.voltage = VOLTAGE(13);
695                                                         compare.steps = +3;
696                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
697                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
698                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MOTOR_CMD]) );  // wait 150msec after voltage change
699
700                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
701                                                         sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
702                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
703
704                                                         compare.voltage = voltage;
705                                                         compare.steps = +3;
706                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // correct final voltage?
707                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 2000) );  // wait 2 second before set high voltage
708                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
709
710                                                         compare.tone = tone;
711                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
712                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
713                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE]) );
714                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
715
716                                                         cmd.direction=1;  // check for running rotor
717                                                         cmd.deltaA=0;
718                                                         cmd.steps=+3;
719                                                         cmd.okcount=0;
720                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, m_params[MOTOR_RUNNING_TIMEOUT]*4) );  // 2 minutes running timeout
721                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 250) );  // 250msec delay
722                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TUNER_LOCKED_GOTO, cmd ) );
723                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) ); 
724                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -3) );  // goto loop start
725                                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
726                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
727                                                 }
728                                                 frontend.setData(eDVBFrontend::NEW_ROTOR_CMD, RotorCmd);
729                                                 frontend.setData(eDVBFrontend::NEW_ROTOR_POS, sat.orbital_position);
730                                         }
731                                 }
732                         }
733                         else
734                                 csw = band;
735
736                         frontend.setData(eDVBFrontend::CSW, csw);
737                         frontend.setData(eDVBFrontend::UCSW, ucsw);
738                         frontend.setData(eDVBFrontend::TONEBURST, di_param.m_toneburst_param);
739
740                         if (doSetVoltageToneFrontend)
741                         {
742                                 eSecCommand::pair compare;
743                                 compare.voltage = voltage;
744                                 compare.steps = +3;
745                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
746                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
747                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_FINAL_VOLTAGE_CHANGE]) );
748                                 compare.tone = tone;
749                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
750                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
751                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE]) );
752                         }
753
754                         if (doSetFrontend)
755                         {
756                                 sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
757                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
758                         }
759                         frontend.setSecSequence(sec_sequence);
760
761                         return 0;
762                 }
763         }
764
765         eDebug("found no useable satellite configuration for orbital position (%d)", sat.orbital_position );
766         return -1;
767 }
768
769 RESULT eDVBSatelliteEquipmentControl::clear()
770 {
771         for (int i=0; i <= m_lnbidx; ++i)
772         {
773                 m_lnbs[i].m_satellites.clear();
774                 m_lnbs[i].slot_mask = 0;
775         }
776         m_lnbidx=-1;
777
778         m_not_linked_slot_mask=0;
779
780         //reset some tuner configuration
781         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
782         {
783                 it->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, -1);
784                 it->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, -1);
785                 it->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, -1);
786                 it->m_frontend->setData(eDVBFrontend::ROTOR_POS, -1);
787                 it->m_frontend->setData(eDVBFrontend::ROTOR_CMD, -1);
788         }
789
790         return 0;
791 }
792
793 /* LNB Specific Parameters */
794 RESULT eDVBSatelliteEquipmentControl::addLNB()
795 {
796         if ( (m_lnbidx+1) < (int)(sizeof(m_lnbs) / sizeof(eDVBSatelliteLNBParameters)))
797                 m_curSat=m_lnbs[++m_lnbidx].m_satellites.end();
798         else
799         {
800                 eDebug("no more LNB free... cnt is %d", m_lnbidx);
801                 return -ENOSPC;
802         }
803         return 0;
804 }
805
806 RESULT eDVBSatelliteEquipmentControl::setLNBSlotMask(int slotmask)
807 {
808         if ( currentLNBValid() )
809                 m_lnbs[m_lnbidx].slot_mask = slotmask;
810         else
811                 return -ENOENT;
812         return 0;
813 }
814
815 RESULT eDVBSatelliteEquipmentControl::setLNBLOFL(int lofl)
816 {
817         if ( currentLNBValid() )
818                 m_lnbs[m_lnbidx].m_lof_lo = lofl;
819         else
820                 return -ENOENT;
821         return 0;
822 }
823
824 RESULT eDVBSatelliteEquipmentControl::setLNBLOFH(int lofh)
825 {
826         if ( currentLNBValid() )
827                 m_lnbs[m_lnbidx].m_lof_hi = lofh;
828         else
829                 return -ENOENT;
830         return 0;
831 }
832
833 RESULT eDVBSatelliteEquipmentControl::setLNBThreshold(int threshold)
834 {
835         if ( currentLNBValid() )
836                 m_lnbs[m_lnbidx].m_lof_threshold = threshold;
837         else
838                 return -ENOENT;
839         return 0;
840 }
841
842 RESULT eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(bool onoff)
843 {
844         if ( currentLNBValid() )
845                 m_lnbs[m_lnbidx].m_increased_voltage = onoff;
846         else
847                 return -ENOENT;
848         return 0;
849 }
850
851 /* DiSEqC Specific Parameters */
852 RESULT eDVBSatelliteEquipmentControl::setDiSEqCMode(int diseqcmode)
853 {
854         if ( currentLNBValid() )
855                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_diseqc_mode = (eDVBSatelliteDiseqcParameters::t_diseqc_mode)diseqcmode;
856         else
857                 return -ENOENT;
858         return 0;
859 }
860
861 RESULT eDVBSatelliteEquipmentControl::setToneburst(int toneburst)
862 {
863         if ( currentLNBValid() )
864                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_toneburst_param = (eDVBSatelliteDiseqcParameters::t_toneburst_param)toneburst;
865         else
866                 return -ENOENT;
867         return 0;
868 }
869
870 RESULT eDVBSatelliteEquipmentControl::setRepeats(int repeats)
871 {
872         if ( currentLNBValid() )
873                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_repeats=repeats;
874         else
875                 return -ENOENT;
876         return 0;
877 }
878
879 RESULT eDVBSatelliteEquipmentControl::setCommittedCommand(int command)
880 {
881         if ( currentLNBValid() )
882                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_committed_cmd=command;
883         else
884                 return -ENOENT;
885         return 0;
886 }
887
888 RESULT eDVBSatelliteEquipmentControl::setUncommittedCommand(int command)
889 {
890         if ( currentLNBValid() )
891                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_uncommitted_cmd = command;
892         else
893                 return -ENOENT;
894         return 0;
895 }
896
897 RESULT eDVBSatelliteEquipmentControl::setCommandOrder(int order)
898 {
899         if ( currentLNBValid() )
900                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_command_order=order;
901         else
902                 return -ENOENT;
903         return 0;
904 }
905
906 RESULT eDVBSatelliteEquipmentControl::setFastDiSEqC(bool onoff)
907 {
908         if ( currentLNBValid() )
909                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_use_fast=onoff;
910         else
911                 return -ENOENT;
912         return 0;
913 }
914
915 RESULT eDVBSatelliteEquipmentControl::setSeqRepeat(bool onoff)
916 {
917         if ( currentLNBValid() )
918                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_seq_repeat = onoff;
919         else
920                 return -ENOENT;
921         return 0;
922 }
923
924 /* Rotor Specific Parameters */
925 RESULT eDVBSatelliteEquipmentControl::setLongitude(float longitude)
926 {
927         if ( currentLNBValid() )
928                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_longitude=longitude;
929         else
930                 return -ENOENT;
931         return 0;
932 }
933
934 RESULT eDVBSatelliteEquipmentControl::setLatitude(float latitude)
935 {
936         if ( currentLNBValid() )
937                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_latitude=latitude;
938         else
939                 return -ENOENT;
940         return 0;
941 }
942
943 RESULT eDVBSatelliteEquipmentControl::setLoDirection(int direction)
944 {
945         if ( currentLNBValid() )
946                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_lo_direction=direction;
947         else
948                 return -ENOENT;
949         return 0;
950 }
951
952 RESULT eDVBSatelliteEquipmentControl::setLaDirection(int direction)
953 {
954         if ( currentLNBValid() )
955                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_la_direction=direction;
956         else
957                 return -ENOENT;
958         return 0;
959 }
960
961 RESULT eDVBSatelliteEquipmentControl::setUseInputpower(bool onoff)
962 {
963         if ( currentLNBValid() )
964                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_use=onoff;
965         else
966                 return -ENOENT;
967         return 0;
968 }
969
970 RESULT eDVBSatelliteEquipmentControl::setInputpowerDelta(int delta)
971 {
972         if ( currentLNBValid() )
973                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_delta=delta;
974         else
975                 return -ENOENT;
976         return 0;
977 }
978
979 /* Satellite Specific Parameters */
980 RESULT eDVBSatelliteEquipmentControl::addSatellite(int orbital_position)
981 {
982         if ( currentLNBValid() )
983         {
984                 std::map<int, eDVBSatelliteSwitchParameters>::iterator it =
985                         m_lnbs[m_lnbidx].m_satellites.find(orbital_position);
986                 if ( it == m_lnbs[m_lnbidx].m_satellites.end() )
987                 {
988                         std::pair<std::map<int, eDVBSatelliteSwitchParameters>::iterator, bool > ret =
989                                 m_lnbs[m_lnbidx].m_satellites.insert(
990                                         std::pair<int, eDVBSatelliteSwitchParameters>(orbital_position, eDVBSatelliteSwitchParameters())
991                                 );
992                         if ( ret.second )
993                                 m_curSat = ret.first;
994                         else
995                                 return -ENOMEM;
996                 }
997                 else
998                         return -EEXIST;
999         }
1000         else
1001                 return -ENOENT;
1002         return 0;
1003 }
1004
1005 RESULT eDVBSatelliteEquipmentControl::setVoltageMode(int mode)
1006 {
1007         if ( currentLNBValid() && m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1008                 m_curSat->second.m_voltage_mode = (eDVBSatelliteSwitchParameters::t_voltage_mode)mode;
1009         else
1010                 return -ENOENT;
1011         return 0;
1012
1013 }
1014
1015 RESULT eDVBSatelliteEquipmentControl::setToneMode(int mode)
1016 {
1017         if ( currentLNBValid() )
1018         {
1019                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1020                         m_curSat->second.m_22khz_signal = (eDVBSatelliteSwitchParameters::t_22khz_signal)mode;
1021                 else
1022                         return -EPERM;
1023         }
1024         else
1025                 return -ENOENT;
1026         return 0;
1027 }
1028
1029 RESULT eDVBSatelliteEquipmentControl::setRotorPosNum(int rotor_pos_num)
1030 {
1031         if ( currentLNBValid() )
1032         {
1033                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1034                         m_curSat->second.m_rotorPosNum=rotor_pos_num;
1035                 else
1036                         return -EPERM;
1037         }
1038         else
1039                 return -ENOENT;
1040         return 0;
1041 }
1042
1043 struct sat_compare
1044 {
1045         int orb_pos, lofl, lofh;
1046         sat_compare(int o, int lofl, int lofh)
1047                 :orb_pos(o), lofl(lofl), lofh(lofh)
1048         {}
1049         sat_compare(const sat_compare &x)
1050                 :orb_pos(x.orb_pos), lofl(x.lofl), lofh(x.lofh)
1051         {}
1052         bool operator < (const sat_compare & cmp) const
1053         {
1054                 if (orb_pos == cmp.orb_pos)
1055                 {
1056                         if ( abs(lofl-cmp.lofl) < 200000 )
1057                         {
1058                                 if (abs(lofh-cmp.lofh) < 200000)
1059                                         return false;
1060                                 return lofh<cmp.lofh;
1061                         }
1062                         return lofl<cmp.lofl;
1063                 }
1064                 return orb_pos < cmp.orb_pos;
1065         }
1066 };
1067
1068 PyObject *eDVBSatelliteEquipmentControl::get_exclusive_satellites(int tu1, int tu2)
1069 {
1070         ePyObject ret;
1071
1072         if (tu1 != tu2)
1073         {
1074                 eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1075                 int cnt=0;
1076                 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
1077                 {
1078                         if (cnt == tu1)
1079                                 p1 = *it;
1080                         else if (cnt == tu2)
1081                                 p2 = *it;
1082                 }
1083
1084                 if (p1 && p2)
1085                 {
1086                         // check for linked tuners
1087
1088                         do 
1089                         {
1090                                 int tmp;
1091                                 p1->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, tmp);
1092                                 if (tmp != -1)
1093                                         p1 = (eDVBRegisteredFrontend*)tmp;
1094                                 else
1095                                         break;
1096                         }
1097                         while (true);
1098
1099                         do 
1100                         {
1101                                 int tmp;
1102                                 p2->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, tmp);
1103                                 if (tmp != -1)
1104                                         p2 = (eDVBRegisteredFrontend*)tmp;
1105                                 else
1106                                         break;
1107                         }
1108                         while (true);
1109
1110                         if (p1 != p2)
1111                         {
1112                                 int tmp1=-1;
1113                                 int tmp2=-1;
1114                                 // check for rotor dependency
1115                                 p1->m_frontend->getData(eDVBFrontend::SATPOS_DEPENDS_PTR, tmp1);
1116                                 if (tmp1 != -1)
1117                                         p1 = (eDVBRegisteredFrontend*)tmp1;
1118                                 p2->m_frontend->getData(eDVBFrontend::SATPOS_DEPENDS_PTR, tmp2);
1119                                 if (tmp2 != -1)
1120                                         p2 = (eDVBRegisteredFrontend*)tmp2;
1121                                 if (p1 != p2)
1122                                 {
1123                                         int tu1_mask = 1 << p1->m_frontend->getSlotID(),
1124                                                 tu2_mask = 1 << p2->m_frontend->getSlotID();
1125                                         std::set<sat_compare> tu1sats, tu2sats;
1126                                         std::list<sat_compare> tu1difference, tu2difference;
1127                                         std::insert_iterator<std::list<sat_compare> > insert1(tu1difference, tu1difference.begin()),
1128                                                 insert2(tu2difference, tu2difference.begin());
1129                                         for (int idx=0; idx <= m_lnbidx; ++idx )
1130                                         {
1131                                                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
1132                                                 for (std::map<int, eDVBSatelliteSwitchParameters>::iterator sit(lnb_param.m_satellites.begin());
1133                                                         sit != lnb_param.m_satellites.end(); ++sit)
1134                                                 {
1135                                                         if ( lnb_param.slot_mask & tu1_mask )
1136                                                                 tu1sats.insert(sat_compare(sit->first, lnb_param.m_lof_lo, lnb_param.m_lof_hi));
1137                                                         if ( lnb_param.slot_mask & tu2_mask )
1138                                                                 tu2sats.insert(sat_compare(sit->first, lnb_param.m_lof_lo, lnb_param.m_lof_hi));
1139                                                 }
1140                                         }
1141                                         std::set_difference(tu1sats.begin(), tu1sats.end(),
1142                                                 tu2sats.begin(), tu2sats.end(),
1143                                                 insert1);
1144                                         std::set_difference(tu2sats.begin(), tu2sats.end(),
1145                                                 tu1sats.begin(), tu1sats.end(),
1146                                                 insert2);
1147                                         if (!tu1sats.empty() || !tu2sats.empty())
1148                                         {
1149                                                 int idx=0;
1150                                                 ret = PyList_New(2+tu1difference.size()+tu2difference.size());
1151
1152                                                 PyList_SET_ITEM(ret, idx++, PyInt_FromLong(tu1difference.size()));
1153                                                 for(std::list<sat_compare>::iterator it(tu1difference.begin()); it != tu1difference.end(); ++it)
1154                                                         PyList_SET_ITEM(ret, idx++, PyInt_FromLong(it->orb_pos));
1155
1156                                                 PyList_SET_ITEM(ret, idx++, PyInt_FromLong(tu2difference.size()));
1157                                                 for(std::list<sat_compare>::iterator it(tu2difference.begin()); it != tu2difference.end(); ++it)
1158                                                         PyList_SET_ITEM(ret, idx++, PyInt_FromLong(it->orb_pos));
1159                                         }
1160                                 }
1161                         }
1162                 }
1163         }
1164         if (!ret)
1165         {
1166                 ret = PyList_New(2);
1167                 PyList_SET_ITEM(ret, 0, PyInt_FromLong(0));
1168                 PyList_SET_ITEM(ret, 1, PyInt_FromLong(0));
1169         }
1170         return ret;
1171 }
1172
1173 RESULT eDVBSatelliteEquipmentControl::setTunerLinked(int tu1, int tu2)
1174 {
1175         if (tu1 != tu2)
1176         {
1177                 eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1178
1179                 int cnt=0;
1180                 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
1181                 {
1182                         if (cnt == tu1)
1183                                 p1 = *it;
1184                         else if (cnt == tu2)
1185                                 p2 = *it;
1186                 }
1187                 if (p1 && p2)
1188                 {
1189                         p1->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, (int)p2);
1190                         p2->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, (int)p1);
1191                         return 0;
1192                 }
1193         }
1194         return -1;
1195 }
1196
1197 RESULT eDVBSatelliteEquipmentControl::setTunerDepends(int tu1, int tu2)
1198 {
1199         if (tu1 == tu2)
1200                 return -1;
1201
1202         eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1203
1204         int cnt=0;
1205         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
1206         {
1207                 if (cnt == tu1)
1208                         p1 = *it;
1209                 else if (cnt == tu2)
1210                         p2 = *it;
1211         }
1212         if (p1 && p2)
1213         {
1214                 p1->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (int)p2);
1215                 p2->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (int)p1);
1216                 return 0;
1217         }
1218         return -1;
1219 }
1220
1221 void eDVBSatelliteEquipmentControl::setSlotNotLinked(int slot_no)
1222 {
1223         m_not_linked_slot_mask |= (1 << slot_no);
1224 }
1225
1226 bool eDVBSatelliteEquipmentControl::isRotorMoving()
1227 {
1228         return m_rotorMoving;
1229 }
1230
1231 void eDVBSatelliteEquipmentControl::setRotorMoving(bool b)
1232 {
1233         m_rotorMoving=b;
1234 }