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