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