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