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