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