749279bccab30b40b597a7fdfa85535f58a23a85
[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                                                 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, +9 ) );  // 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_TIMEOUT, m_params[MOTOR_RUNNING_TIMEOUT]*20) );  // 2 minutes running timeout
675 // rotor running loop
676                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
677                                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
678                                                         cmd.direction=0;  // check for stopped rotor
679                                                         cmd.steps=+4;
680                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
681                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) );  // timeout ? this should never happen
682                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // running loop start
683 /////////////////////
684                                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
685                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
686                                                 }
687                                                 else
688                                                 {  // use normal turning mode
689                                                         doSetVoltageToneFrontend=false;
690                                                         doSetFrontend=false;
691                                                         eSecCommand::rotor cmd;
692                                                         eSecCommand::pair compare;
693                                                         compare.voltage = VOLTAGE(13);
694                                                         compare.steps = +3;
695                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
696                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
697                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MOTOR_CMD]) );  // wait 150msec after voltage change
698
699                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
700                                                         sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
701                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
702
703                                                         compare.voltage = voltage;
704                                                         compare.steps = +3;
705                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // correct final voltage?
706                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 2000) );  // wait 2 second before set high voltage
707                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
708
709                                                         compare.tone = tone;
710                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
711                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
712                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE]) );
713                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
714
715                                                         cmd.direction=1;  // check for running rotor
716                                                         cmd.deltaA=0;
717                                                         cmd.steps=+3;
718                                                         cmd.okcount=0;
719                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, m_params[MOTOR_RUNNING_TIMEOUT]*4) );  // 2 minutes running timeout
720                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 250) );  // 250msec delay
721                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TUNER_LOCKED_GOTO, cmd ) );
722                                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) ); 
723                                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -3) );  // goto loop start
724                                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
725                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
726                                                 }
727                                                 frontend.setData(eDVBFrontend::NEW_ROTOR_CMD, RotorCmd);
728                                                 frontend.setData(eDVBFrontend::NEW_ROTOR_POS, sat.orbital_position);
729                                         }
730                                 }
731                         }
732                         else
733                                 csw = band;
734
735                         frontend.setData(eDVBFrontend::CSW, csw);
736                         frontend.setData(eDVBFrontend::UCSW, ucsw);
737                         frontend.setData(eDVBFrontend::TONEBURST, di_param.m_toneburst_param);
738
739                         if (doSetVoltageToneFrontend)
740                         {
741                                 eSecCommand::pair compare;
742                                 compare.voltage = voltage;
743                                 compare.steps = +3;
744                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
745                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
746                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_FINAL_VOLTAGE_CHANGE]) );
747                                 compare.tone = tone;
748                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
749                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
750                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE]) );
751                         }
752
753                         if (doSetFrontend)
754                         {
755                                 sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT) );
756                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
757                         }
758                         frontend.setSecSequence(sec_sequence);
759
760                         return 0;
761                 }
762         }
763
764         eDebug("found no useable satellite configuration for orbital position (%d)", sat.orbital_position );
765         return -1;
766 }
767
768 RESULT eDVBSatelliteEquipmentControl::clear()
769 {
770         for (int i=0; i <= m_lnbidx; ++i)
771         {
772                 m_lnbs[i].m_satellites.clear();
773                 m_lnbs[i].slot_mask = 0;
774         }
775         m_lnbidx=-1;
776
777         m_not_linked_slot_mask=0;
778
779         //reset some tuner configuration
780         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
781         {
782                 it->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, -1);
783                 it->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, -1);
784                 it->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, -1);
785                 it->m_frontend->setData(eDVBFrontend::ROTOR_POS, -1);
786                 it->m_frontend->setData(eDVBFrontend::ROTOR_CMD, -1);
787         }
788
789         return 0;
790 }
791
792 /* LNB Specific Parameters */
793 RESULT eDVBSatelliteEquipmentControl::addLNB()
794 {
795         if ( (m_lnbidx+1) < (int)(sizeof(m_lnbs) / sizeof(eDVBSatelliteLNBParameters)))
796                 m_curSat=m_lnbs[++m_lnbidx].m_satellites.end();
797         else
798         {
799                 eDebug("no more LNB free... cnt is %d", m_lnbidx);
800                 return -ENOSPC;
801         }
802         return 0;
803 }
804
805 RESULT eDVBSatelliteEquipmentControl::setLNBSlotMask(int slotmask)
806 {
807         if ( currentLNBValid() )
808                 m_lnbs[m_lnbidx].slot_mask = slotmask;
809         else
810                 return -ENOENT;
811         return 0;
812 }
813
814 RESULT eDVBSatelliteEquipmentControl::setLNBLOFL(int lofl)
815 {
816         if ( currentLNBValid() )
817                 m_lnbs[m_lnbidx].m_lof_lo = lofl;
818         else
819                 return -ENOENT;
820         return 0;
821 }
822
823 RESULT eDVBSatelliteEquipmentControl::setLNBLOFH(int lofh)
824 {
825         if ( currentLNBValid() )
826                 m_lnbs[m_lnbidx].m_lof_hi = lofh;
827         else
828                 return -ENOENT;
829         return 0;
830 }
831
832 RESULT eDVBSatelliteEquipmentControl::setLNBThreshold(int threshold)
833 {
834         if ( currentLNBValid() )
835                 m_lnbs[m_lnbidx].m_lof_threshold = threshold;
836         else
837                 return -ENOENT;
838         return 0;
839 }
840
841 RESULT eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(bool onoff)
842 {
843         if ( currentLNBValid() )
844                 m_lnbs[m_lnbidx].m_increased_voltage = onoff;
845         else
846                 return -ENOENT;
847         return 0;
848 }
849
850 /* DiSEqC Specific Parameters */
851 RESULT eDVBSatelliteEquipmentControl::setDiSEqCMode(int diseqcmode)
852 {
853         if ( currentLNBValid() )
854                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_diseqc_mode = (eDVBSatelliteDiseqcParameters::t_diseqc_mode)diseqcmode;
855         else
856                 return -ENOENT;
857         return 0;
858 }
859
860 RESULT eDVBSatelliteEquipmentControl::setToneburst(int toneburst)
861 {
862         if ( currentLNBValid() )
863                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_toneburst_param = (eDVBSatelliteDiseqcParameters::t_toneburst_param)toneburst;
864         else
865                 return -ENOENT;
866         return 0;
867 }
868
869 RESULT eDVBSatelliteEquipmentControl::setRepeats(int repeats)
870 {
871         if ( currentLNBValid() )
872                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_repeats=repeats;
873         else
874                 return -ENOENT;
875         return 0;
876 }
877
878 RESULT eDVBSatelliteEquipmentControl::setCommittedCommand(int command)
879 {
880         if ( currentLNBValid() )
881                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_committed_cmd=command;
882         else
883                 return -ENOENT;
884         return 0;
885 }
886
887 RESULT eDVBSatelliteEquipmentControl::setUncommittedCommand(int command)
888 {
889         if ( currentLNBValid() )
890                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_uncommitted_cmd = command;
891         else
892                 return -ENOENT;
893         return 0;
894 }
895
896 RESULT eDVBSatelliteEquipmentControl::setCommandOrder(int order)
897 {
898         if ( currentLNBValid() )
899                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_command_order=order;
900         else
901                 return -ENOENT;
902         return 0;
903 }
904
905 RESULT eDVBSatelliteEquipmentControl::setFastDiSEqC(bool onoff)
906 {
907         if ( currentLNBValid() )
908                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_use_fast=onoff;
909         else
910                 return -ENOENT;
911         return 0;
912 }
913
914 RESULT eDVBSatelliteEquipmentControl::setSeqRepeat(bool onoff)
915 {
916         if ( currentLNBValid() )
917                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_seq_repeat = onoff;
918         else
919                 return -ENOENT;
920         return 0;
921 }
922
923 /* Rotor Specific Parameters */
924 RESULT eDVBSatelliteEquipmentControl::setLongitude(float longitude)
925 {
926         if ( currentLNBValid() )
927                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_longitude=longitude;
928         else
929                 return -ENOENT;
930         return 0;
931 }
932
933 RESULT eDVBSatelliteEquipmentControl::setLatitude(float latitude)
934 {
935         if ( currentLNBValid() )
936                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_latitude=latitude;
937         else
938                 return -ENOENT;
939         return 0;
940 }
941
942 RESULT eDVBSatelliteEquipmentControl::setLoDirection(int direction)
943 {
944         if ( currentLNBValid() )
945                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_lo_direction=direction;
946         else
947                 return -ENOENT;
948         return 0;
949 }
950
951 RESULT eDVBSatelliteEquipmentControl::setLaDirection(int direction)
952 {
953         if ( currentLNBValid() )
954                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_la_direction=direction;
955         else
956                 return -ENOENT;
957         return 0;
958 }
959
960 RESULT eDVBSatelliteEquipmentControl::setUseInputpower(bool onoff)
961 {
962         if ( currentLNBValid() )
963                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_use=onoff;
964         else
965                 return -ENOENT;
966         return 0;
967 }
968
969 RESULT eDVBSatelliteEquipmentControl::setInputpowerDelta(int delta)
970 {
971         if ( currentLNBValid() )
972                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_delta=delta;
973         else
974                 return -ENOENT;
975         return 0;
976 }
977
978 /* Satellite Specific Parameters */
979 RESULT eDVBSatelliteEquipmentControl::addSatellite(int orbital_position)
980 {
981         if ( currentLNBValid() )
982         {
983                 std::map<int, eDVBSatelliteSwitchParameters>::iterator it =
984                         m_lnbs[m_lnbidx].m_satellites.find(orbital_position);
985                 if ( it == m_lnbs[m_lnbidx].m_satellites.end() )
986                 {
987                         std::pair<std::map<int, eDVBSatelliteSwitchParameters>::iterator, bool > ret =
988                                 m_lnbs[m_lnbidx].m_satellites.insert(
989                                         std::pair<int, eDVBSatelliteSwitchParameters>(orbital_position, eDVBSatelliteSwitchParameters())
990                                 );
991                         if ( ret.second )
992                                 m_curSat = ret.first;
993                         else
994                                 return -ENOMEM;
995                 }
996                 else
997                         return -EEXIST;
998         }
999         else
1000                 return -ENOENT;
1001         return 0;
1002 }
1003
1004 RESULT eDVBSatelliteEquipmentControl::setVoltageMode(int mode)
1005 {
1006         if ( currentLNBValid() && m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1007                 m_curSat->second.m_voltage_mode = (eDVBSatelliteSwitchParameters::t_voltage_mode)mode;
1008         else
1009                 return -ENOENT;
1010         return 0;
1011
1012 }
1013
1014 RESULT eDVBSatelliteEquipmentControl::setToneMode(int mode)
1015 {
1016         if ( currentLNBValid() )
1017         {
1018                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1019                         m_curSat->second.m_22khz_signal = (eDVBSatelliteSwitchParameters::t_22khz_signal)mode;
1020                 else
1021                         return -EPERM;
1022         }
1023         else
1024                 return -ENOENT;
1025         return 0;
1026 }
1027
1028 RESULT eDVBSatelliteEquipmentControl::setRotorPosNum(int rotor_pos_num)
1029 {
1030         if ( currentLNBValid() )
1031         {
1032                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1033                         m_curSat->second.m_rotorPosNum=rotor_pos_num;
1034                 else
1035                         return -EPERM;
1036         }
1037         else
1038                 return -ENOENT;
1039         return 0;
1040 }
1041
1042 struct sat_compare
1043 {
1044         int orb_pos, lofl, lofh;
1045         sat_compare(int o, int lofl, int lofh)
1046                 :orb_pos(o), lofl(lofl), lofh(lofh)
1047         {}
1048         sat_compare(const sat_compare &x)
1049                 :orb_pos(x.orb_pos), lofl(x.lofl), lofh(x.lofh)
1050         {}
1051         bool operator < (const sat_compare & cmp) const
1052         {
1053                 if (orb_pos == cmp.orb_pos)
1054                 {
1055                         if ( abs(lofl-cmp.lofl) < 200000 )
1056                         {
1057                                 if (abs(lofh-cmp.lofh) < 200000)
1058                                         return false;
1059                                 return lofh<cmp.lofh;
1060                         }
1061                         return lofl<cmp.lofl;
1062                 }
1063                 return orb_pos < cmp.orb_pos;
1064         }
1065 };
1066
1067 PyObject *eDVBSatelliteEquipmentControl::get_exclusive_satellites(int tu1, int tu2)
1068 {
1069         ePyObject ret;
1070
1071         if (tu1 != tu2)
1072         {
1073                 eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1074                 int cnt=0;
1075                 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
1076                 {
1077                         if (cnt == tu1)
1078                                 p1 = *it;
1079                         else if (cnt == tu2)
1080                                 p2 = *it;
1081                 }
1082
1083                 if (p1 && p2)
1084                 {
1085                         // check for linked tuners
1086
1087                         do 
1088                         {
1089                                 int tmp;
1090                                 p1->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, tmp);
1091                                 if (tmp != -1)
1092                                         p1 = (eDVBRegisteredFrontend*)tmp;
1093                                 else
1094                                         break;
1095                         }
1096                         while (true);
1097
1098                         do 
1099                         {
1100                                 int tmp;
1101                                 p2->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, tmp);
1102                                 if (tmp != -1)
1103                                         p2 = (eDVBRegisteredFrontend*)tmp;
1104                                 else
1105                                         break;
1106                         }
1107                         while (true);
1108
1109                         if (p1 != p2)
1110                         {
1111                                 int tmp1=-1;
1112                                 int tmp2=-1;
1113                                 // check for rotor dependency
1114                                 p1->m_frontend->getData(eDVBFrontend::SATPOS_DEPENDS_PTR, tmp1);
1115                                 if (tmp1 != -1)
1116                                         p1 = (eDVBRegisteredFrontend*)tmp1;
1117                                 p2->m_frontend->getData(eDVBFrontend::SATPOS_DEPENDS_PTR, tmp2);
1118                                 if (tmp2 != -1)
1119                                         p2 = (eDVBRegisteredFrontend*)tmp2;
1120                                 if (p1 != p2)
1121                                 {
1122                                         int tu1_mask = 1 << p1->m_frontend->getSlotID(),
1123                                                 tu2_mask = 1 << p2->m_frontend->getSlotID();
1124                                         std::set<sat_compare> tu1sats, tu2sats;
1125                                         std::list<sat_compare> tu1difference, tu2difference;
1126                                         std::insert_iterator<std::list<sat_compare> > insert1(tu1difference, tu1difference.begin()),
1127                                                 insert2(tu2difference, tu2difference.begin());
1128                                         for (int idx=0; idx <= m_lnbidx; ++idx )
1129                                         {
1130                                                 eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
1131                                                 for (std::map<int, eDVBSatelliteSwitchParameters>::iterator sit(lnb_param.m_satellites.begin());
1132                                                         sit != lnb_param.m_satellites.end(); ++sit)
1133                                                 {
1134                                                         if ( lnb_param.slot_mask & tu1_mask )
1135                                                                 tu1sats.insert(sat_compare(sit->first, lnb_param.m_lof_lo, lnb_param.m_lof_hi));
1136                                                         if ( lnb_param.slot_mask & tu2_mask )
1137                                                                 tu2sats.insert(sat_compare(sit->first, lnb_param.m_lof_lo, lnb_param.m_lof_hi));
1138                                                 }
1139                                         }
1140                                         std::set_difference(tu1sats.begin(), tu1sats.end(),
1141                                                 tu2sats.begin(), tu2sats.end(),
1142                                                 insert1);
1143                                         std::set_difference(tu2sats.begin(), tu2sats.end(),
1144                                                 tu1sats.begin(), tu1sats.end(),
1145                                                 insert2);
1146                                         if (!tu1sats.empty() || !tu2sats.empty())
1147                                         {
1148                                                 int idx=0;
1149                                                 ret = PyList_New(2+tu1difference.size()+tu2difference.size());
1150
1151                                                 PyList_SET_ITEM(ret, idx++, PyInt_FromLong(tu1difference.size()));
1152                                                 for(std::list<sat_compare>::iterator it(tu1difference.begin()); it != tu1difference.end(); ++it)
1153                                                         PyList_SET_ITEM(ret, idx++, PyInt_FromLong(it->orb_pos));
1154
1155                                                 PyList_SET_ITEM(ret, idx++, PyInt_FromLong(tu2difference.size()));
1156                                                 for(std::list<sat_compare>::iterator it(tu2difference.begin()); it != tu2difference.end(); ++it)
1157                                                         PyList_SET_ITEM(ret, idx++, PyInt_FromLong(it->orb_pos));
1158                                         }
1159                                 }
1160                         }
1161                 }
1162         }
1163         if (!ret)
1164         {
1165                 ret = PyList_New(2);
1166                 PyList_SET_ITEM(ret, 0, PyInt_FromLong(0));
1167                 PyList_SET_ITEM(ret, 1, PyInt_FromLong(0));
1168         }
1169         return ret;
1170 }
1171
1172 RESULT eDVBSatelliteEquipmentControl::setTunerLinked(int tu1, int tu2)
1173 {
1174         if (tu1 != tu2)
1175         {
1176                 eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1177
1178                 int cnt=0;
1179                 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
1180                 {
1181                         if (cnt == tu1)
1182                                 p1 = *it;
1183                         else if (cnt == tu2)
1184                                 p2 = *it;
1185                 }
1186                 if (p1 && p2)
1187                 {
1188                         p1->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, (int)p2);
1189                         p2->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, (int)p1);
1190                         return 0;
1191                 }
1192         }
1193         return -1;
1194 }
1195
1196 RESULT eDVBSatelliteEquipmentControl::setTunerDepends(int tu1, int tu2)
1197 {
1198         if (tu1 == tu2)
1199                 return -1;
1200
1201         eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1202
1203         int cnt=0;
1204         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it, ++cnt)
1205         {
1206                 if (cnt == tu1)
1207                         p1 = *it;
1208                 else if (cnt == tu2)
1209                         p2 = *it;
1210         }
1211         if (p1 && p2)
1212         {
1213                 p1->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (int)p2);
1214                 p2->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (int)p1);
1215                 return 0;
1216         }
1217         return -1;
1218 }
1219
1220 void eDVBSatelliteEquipmentControl::setSlotNotLinked(int slot_no)
1221 {
1222         m_not_linked_slot_mask |= (1 << slot_no);
1223 }
1224
1225 bool eDVBSatelliteEquipmentControl::isRotorMoving()
1226 {
1227         return m_rotorMoving;
1228 }
1229
1230 void eDVBSatelliteEquipmentControl::setRotorMoving(bool b)
1231 {
1232         m_rotorMoving=b;
1233 }