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