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