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