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