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