851e9b1d9c4df565fa8b4b3bfa6040e4e7aefd53
[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 ( diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2
494                                         && !sat.no_rotor_command_on_tune )
495                                 {
496                                         if (sw_param.m_rotorPosNum) // we have stored rotor pos?
497                                                 RotorCmd=sw_param.m_rotorPosNum;
498                                         else  // we must calc gotoxx cmd
499                                         {
500                                                 eDebugNoSimulate("Entry for %d,%d? not in Rotor Table found... i try gotoXX?", sat.orbital_position / 10, sat.orbital_position % 10 );
501                                                 useGotoXX = true;
502
503                                                 double  SatLon = abs(sat.orbital_position)/10.00,
504                                                                 SiteLat = rotor_param.m_gotoxx_parameters.m_latitude,
505                                                                 SiteLon = rotor_param.m_gotoxx_parameters.m_longitude;
506
507                                                 if ( rotor_param.m_gotoxx_parameters.m_la_direction == eDVBSatelliteRotorParameters::SOUTH )
508                                                         SiteLat = -SiteLat;
509
510                                                 if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
511                                                         SiteLon = 360 - SiteLon;
512
513                                                 eDebugNoSimulate("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
514                                                 double satHourAngle =
515                                                         calcSatHourangle( SatLon, SiteLat, SiteLon );
516                                                 eDebugNoSimulate("PolarmountHourAngle=%lf", satHourAngle );
517
518                                                 static int gotoXTable[10] =
519                                                         { 0x00, 0x02, 0x03, 0x05, 0x06, 0x08, 0x0A, 0x0B, 0x0D, 0x0E };
520
521                                                 if (SiteLat >= 0) // Northern Hemisphere
522                                                 {
523                                                         int tmp=(int)round( fabs( 180 - satHourAngle ) * 10.0 );
524                                                         RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
525
526                                                         if (satHourAngle < 180) // the east
527                                                                 RotorCmd |= 0xE000;
528                                                         else                                    // west
529                                                                 RotorCmd |= 0xD000;
530                                                 }
531                                                 else // Southern Hemisphere
532                                                 {
533                                                         if (satHourAngle < 180) // the east
534                                                         {
535                                                                 int tmp=(int)round( fabs( satHourAngle ) * 10.0 );
536                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
537                                                                 RotorCmd |= 0xD000;
538                                                         }
539                                                         else // west
540                                                         {
541                                                                 int tmp=(int)round( fabs( 360 - satHourAngle ) * 10.0 );
542                                                                 RotorCmd = (tmp/10)*0x10 + gotoXTable[ tmp % 10 ];
543                                                                 RotorCmd |= 0xE000;
544                                                         }
545                                                 }
546                                                 eDebugNoSimulate("RotorCmd = %04x", RotorCmd);
547                                         }
548                                 }
549
550                                 if ( send_mask )
551                                 {
552                                         int diseqc_repeats = diseqc_mode > eDVBSatelliteDiseqcParameters::V1_0 ? di_param.m_repeats : 0;
553                                         int vlt = iDVBFrontend::voltageOff;
554                                         eSecCommand::pair compare;
555                                         compare.steps = +3;
556                                         compare.tone = iDVBFrontend::toneOff;
557                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
558                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
559                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE_DISABLE_BEFORE_DISEQC]) );
560
561                                         if (diseqc13V)
562                                                 vlt = iDVBFrontend::voltage13;
563                                         else if ( RotorCmd != -1 && RotorCmd != lastRotorCmd )
564                                         {
565                                                 if (rotor_param.m_inputpower_parameters.m_use && !is_unicable)
566                                                         vlt = VOLTAGE(18);  // in input power mode set 18V for measure input power
567                                                 else
568                                                         vlt = VOLTAGE(13);  // in normal mode start turning with 13V
569                                         }
570                                         else
571                                                 vlt = voltage;
572
573                                         // check if voltage is already correct..
574                                         compare.voltage = vlt;
575                                         compare.steps = +7;
576                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
577
578                                         // check if voltage is disabled
579                                         compare.voltage = iDVBFrontend::voltageOff;
580                                         compare.steps = +4;
581                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
582
583                                         // voltage is changed... use DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_SWITCH_CMDS
584                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, vlt) );
585                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_SWITCH_CMDS]) );
586                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, +3) );
587
588                                         // voltage was disabled.. use DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS
589                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, vlt) );
590                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS]) );
591
592                                         sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_SWITCHPARMS) );
593                                         if (needDiSEqCReset)
594                                         {
595                                                 eDVBDiseqcCommand diseqc;
596                                                 memset(diseqc.data, 0, MAX_DISEQC_LENGTH);
597                                                 diseqc.len = 3;
598                                                 diseqc.data[0] = 0xE0;
599                                                 diseqc.data[1] = 0;
600                                                 diseqc.data[2] = 0;
601                                                 // diseqc reset
602                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
603                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_DISEQC_RESET_CMD]) );
604                                                 diseqc.data[2] = 3;
605                                                 // diseqc peripherial powersupply on
606                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
607                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_DISEQC_PERIPHERIAL_POWERON_CMD]) );
608                                         }
609
610                                         for (int seq_repeat = 0; seq_repeat < (di_param.m_seq_repeat?2:1); ++seq_repeat)
611                                         {
612                                                 if ( send_mask & 4 )
613                                                 {
614                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
615                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_TONEBURST]) );
616                                                 }
617
618                                                 int loops=0;
619
620                                                 if ( send_mask & 1 )
621                                                         ++loops;
622                                                 if ( send_mask & 2 )
623                                                         ++loops;
624
625                                                 loops <<= diseqc_repeats;
626
627                                                 for ( int i = 0; i < loops;)  // fill commands...
628                                                 {
629                                                         eDVBDiseqcCommand diseqc;
630                                                         memset(diseqc.data, 0, MAX_DISEQC_LENGTH);
631                                                         diseqc.len = 4;
632                                                         diseqc.data[0] = i ? 0xE1 : 0xE0;
633                                                         diseqc.data[1] = 0x10;
634                                                         if ( (send_mask & 2) && (di_param.m_command_order & 4) )
635                                                         {
636                                                                 diseqc.data[2] = 0x39;
637                                                                 diseqc.data[3] = ucsw;
638                                                         }
639                                                         else if ( send_mask & 1 )
640                                                         {
641                                                                 diseqc.data[2] = 0x38;
642                                                                 diseqc.data[3] = csw;
643                                                         }
644                                                         else  // no committed command confed.. so send uncommitted..
645                                                         {
646                                                                 diseqc.data[2] = 0x39;
647                                                                 diseqc.data[3] = ucsw;
648                                                         }
649                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
650
651                                                         i++;
652                                                         if ( i < loops )
653                                                         {
654                                                                 int cmd=0;
655                                                                 if (diseqc.data[2] == 0x38 && (send_mask & 2))
656                                                                         cmd=0x39;
657                                                                 else if (diseqc.data[2] == 0x39 && (send_mask & 1))
658                                                                         cmd=0x38;
659                                                                 int tmp = m_params[DELAY_BETWEEN_DISEQC_REPEATS];
660                                                                 if (cmd)
661                                                                 {
662                                                                         int delay = diseqc_repeats ? (tmp - 54) / 2 : tmp;  // standard says 100msek between two repeated commands
663                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
664                                                                         diseqc.data[2]=cmd;
665                                                                         diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
666                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
667                                                                         ++i;
668                                                                         if ( i < loops )
669                                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
670                                                                         else
671                                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_LAST_DISEQC_CMD]) );
672                                                                 }
673                                                                 else  // delay 120msek when no command is in repeat gap
674                                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, tmp) );
675                                                         }
676                                                         else
677                                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_LAST_DISEQC_CMD]) );
678                                                 }
679
680                                                 if ( send_mask & 8 )  // toneburst at end of sequence
681                                                 {
682                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
683                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_TONEBURST]) );
684                                                 }
685
686                                                 if (di_param.m_seq_repeat && seq_repeat == 0)
687                                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_BEFORE_SEQUENCE_REPEAT]) );
688                                         }
689                                 }
690                         }
691                         else
692                         {
693                                 sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_SWITCHPARMS) );
694                                 csw = band;
695                         }
696
697                         sec_fe->setData(eDVBFrontend::NEW_CSW, csw);
698                         sec_fe->setData(eDVBFrontend::NEW_UCSW, ucsw);
699                         sec_fe->setData(eDVBFrontend::NEW_TONEBURST, di_param.m_toneburst_param);
700
701                         if(is_unicable)
702                         {
703                                 // check if voltage is disabled
704                                 eSecCommand::pair compare;
705                                 compare.steps = +3;
706                                 compare.voltage = iDVBFrontend::voltageOff;
707                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_NOT_VOLTAGE_GOTO, compare) );
708                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) );
709                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS] ) );
710
711                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
712                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
713                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_SWITCH_CMDS]) );  // wait 20 ms after voltage change
714         
715                                 eDVBDiseqcCommand diseqc;
716                                 memset(diseqc.data, 0, MAX_DISEQC_LENGTH);
717                                 diseqc.len = 5;
718                                 diseqc.data[0] = 0xE0;
719                                 diseqc.data[1] = 0x10;
720                                 diseqc.data[2] = 0x5A;
721                                 diseqc.data[3] = lnb_param.UnicableTuningWord >> 8;
722                                 diseqc.data[4] = lnb_param.UnicableTuningWord;
723
724                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
725                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_LAST_DISEQC_CMD]) );
726                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) );
727                                 if ( RotorCmd != -1 && RotorCmd != lastRotorCmd && !rotor_param.m_inputpower_parameters.m_use)
728                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MOTOR_CMD]) );  // wait 150msec after voltage change
729                         }
730
731                         eDebugNoSimulate("RotorCmd %02x, lastRotorCmd %02lx", RotorCmd, lastRotorCmd);
732                         if ( RotorCmd != -1 && RotorCmd != lastRotorCmd )
733                         {
734                                 int mrt = m_params[MOTOR_RUNNING_TIMEOUT]; // in seconds!
735                                 eSecCommand::pair compare;
736                                 if (!send_mask && !is_unicable)
737                                 {
738                                         compare.steps = +3;
739                                         compare.tone = iDVBFrontend::toneOff;
740                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
741                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
742                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE_DISABLE_BEFORE_DISEQC]) );
743
744                                         compare.voltage = iDVBFrontend::voltageOff;
745                                         compare.steps = +4;
746                                         // the next is a check if voltage is switched off.. then we first set a voltage :)
747                                         // else we set voltage after all diseqc stuff..
748                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_NOT_VOLTAGE_GOTO, compare) );
749
750                                         if (rotor_param.m_inputpower_parameters.m_use)
751                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) ); // set 18V for measure input power
752                                         else
753                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) ); // in normal mode start turning with 13V
754
755                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_MOTOR_CMD]) ); // wait 750ms when voltage was disabled
756                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, +9) );  // no need to send stop rotor cmd and recheck voltage
757                                 }
758                                 else
759                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_BETWEEN_SWITCH_AND_MOTOR_CMD]) ); // wait 700ms when diseqc changed
760
761                                 eDVBDiseqcCommand diseqc;
762                                 memset(diseqc.data, 0, MAX_DISEQC_LENGTH);
763                                 diseqc.len = 3;
764                                 diseqc.data[0] = 0xE0;
765                                 diseqc.data[1] = 0x31;  // positioner
766                                 diseqc.data[2] = 0x60;  // stop
767                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_ROTORPOS_VALID_GOTO, +5) );
768                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
769                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
770                                 sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
771                                 // wait 150msec after send rotor stop cmd
772                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_MOTOR_STOP_CMD]) );
773
774                                 diseqc.data[0] = 0xE0;
775                                 diseqc.data[1] = 0x31;          // positioner
776                                 if ( useGotoXX )
777                                 {
778                                         diseqc.len = 5;
779                                         diseqc.data[2] = 0x6E;  // drive to angular position
780                                         diseqc.data[3] = ((RotorCmd & 0xFF00) / 0x100);
781                                         diseqc.data[4] = RotorCmd & 0xFF;
782                                 }
783                                 else
784                                 {
785                                         diseqc.len = 4;
786                                         diseqc.data[2] = 0x6B;  // goto stored sat position
787                                         diseqc.data[3] = RotorCmd;
788                                         diseqc.data[4] = 0x00;
789                                 }
790
791                         // use measure rotor input power to detect motor state
792                                 if ( rotor_param.m_inputpower_parameters.m_use)
793                                 {
794                                         bool turn_fast = need_turn_fast(rotor_param.m_inputpower_parameters.m_turning_speed) && !is_unicable;
795                                         eSecCommand::rotor cmd;
796                                         eSecCommand::pair compare;
797                                         if (turn_fast)
798                                                 compare.voltage = VOLTAGE(18);
799                                         else
800                                                 compare.voltage = VOLTAGE(13);
801                                         compare.steps = +3;
802                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
803                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
804                         // measure idle power values
805                                         compare.steps = -2;
806                                         if (turn_fast) {
807                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MEASURE_IDLE_INPUTPOWER]) );  // wait 150msec after voltage change
808                                                 sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 1) );
809                                                 compare.val = 1;
810                                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO, compare) );
811                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) );
812                                         }
813                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MEASURE_IDLE_INPUTPOWER]) );  // wait 150msec before measure
814                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 0) );
815                                         compare.val = 0;
816                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO, compare) );
817                         ////////////////////////////
818                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_DISEQC_RETRYS, m_params[MOTOR_COMMAND_RETRIES]) );  // 2 retries
819                                         sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
820                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
821                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 40) );  // 2 seconds rotor start timout
822                         // rotor start loop
823                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // 50msec delay
824                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
825                                         cmd.direction=1;  // check for running rotor
826                                         cmd.deltaA=rotor_param.m_inputpower_parameters.m_delta;
827                                         cmd.steps=+5;
828                                         cmd.okcount=0;
829                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );  // check if rotor has started
830                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +2 ) );  // timeout .. we assume now the rotor is already at the correct position
831                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // goto loop start
832                                         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 
833                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -8) );  // goto loop start
834                         ////////////////////
835                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_MOVING) );
836                                         if (turn_fast)
837                                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
838                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, mrt*20) );  // mrt is in seconds... our SLEEP time is 50ms.. so * 20
839                         // rotor running loop
840                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
841                                         sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
842                                         cmd.direction=0;  // check for stopped rotor
843                                         cmd.steps=+3;
844                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );
845                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +2 ) );  // timeout ? this should never happen
846                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // running loop start
847                         /////////////////////
848                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
849                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_STOPPED) );
850                                 }
851                         // use normal motor turning mode
852                                 else
853                                 {
854                                         if (curRotorPos != -1)
855                                         {
856                                                 mrt = abs(curRotorPos - sat.orbital_position);
857                                                 if (mrt > 1800)
858                                                         mrt = 3600 - mrt;
859                                                 if (mrt % 10)
860                                                         mrt += 10; // round a little bit
861                                                 mrt *= 2000;  // (we assume a very slow rotor with just 0.5 degree per second here)
862                                                 mrt /= 10000;
863                                                 mrt += 3; // a little bit overhead
864                                         }
865                                         doSetVoltageToneFrontend=false;
866                                         doSetFrontend=false;
867                                         eSecCommand::rotor cmd;
868                                         eSecCommand::pair compare;
869                                         compare.voltage = VOLTAGE(13);
870                                         compare.steps = +3;
871                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
872                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
873                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MOTOR_CMD]) );  // wait 150msec after voltage change
874
875                                         sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
876                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_MOVING) );
877                                         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
878                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 1000) ); // sleep one second before change voltage or tone
879
880                                         compare.voltage = voltage;
881                                         compare.steps = +3;
882                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // correct final voltage?
883                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 2000) );  // wait 2 second before set high voltage
884                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
885
886                                         compare.tone = tone;
887                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
888                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
889                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_FINAL_CONT_TONE_CHANGE]) );
890                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND, 0) );
891
892                                         cmd.direction=1;  // check for running rotor
893                                         cmd.deltaA=0;
894                                         cmd.steps = +3;
895                                         cmd.okcount=0;
896                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, mrt*4) );  // mrt is in seconds... our SLEEP time is 250ms.. so * 4
897                                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 250) );  // 250msec delay
898                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TUNER_LOCKED_GOTO, cmd ) );
899                                         sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +5 ) );
900                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -3) );  // goto loop start
901                                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
902                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_STOPPED) );
903                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, +4) );
904                                         sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT, tunetimeout) );
905                                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND, 1) );
906                                         sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -5) );
907                                         eDebug("set rotor timeout to %d seconds", mrt);
908                                 }
909                                 sec_fe->setData(eDVBFrontend::NEW_ROTOR_CMD, RotorCmd);
910                                 sec_fe->setData(eDVBFrontend::NEW_ROTOR_POS, sat.orbital_position);
911                         }
912
913                         if (doSetVoltageToneFrontend && !is_unicable)
914                         {
915                                 eSecCommand::pair compare;
916                                 compare.voltage = voltage;
917                                 compare.steps = +3;
918                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
919                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, voltage) );
920                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_FINAL_VOLTAGE_CHANGE]) );
921                                 compare.tone = tone;
922                                 sec_sequence.push_back( eSecCommand(eSecCommand::IF_TONE_GOTO, compare) );
923                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, tone) );
924                                 sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_FINAL_CONT_TONE_CHANGE]) );
925                         }
926
927                         sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_SWITCHPARMS) );
928
929                         if (doSetFrontend)
930                         {
931                                 sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT, tunetimeout) );
932                                 sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND, 1) );
933                         }
934
935                         sec_sequence.push_front( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
936                         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 500) );
937                         sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
938
939                         frontend.setSecSequence(sec_sequence);
940
941                         return 0;
942                 }
943         }
944         eDebugNoSimulate("found no useable satellite configuration for %s freq %d%s %s on orbital position (%d)",
945                 sat.system ? "DVB-S2" : "DVB-S",
946                 sat.frequency,
947                 sat.polarisation == eDVBFrontendParametersSatellite::Polarisation_Horizontal ? "H" :
948                         eDVBFrontendParametersSatellite::Polarisation_Vertical ? "V" :
949                         eDVBFrontendParametersSatellite::Polarisation_CircularLeft ? "CL" : "CR",
950                 sat.modulation == eDVBFrontendParametersSatellite::Modulation_Auto ? "AUTO" :
951                         eDVBFrontendParametersSatellite::Modulation_QPSK ? "QPSK" :
952                         eDVBFrontendParametersSatellite::Modulation_8PSK ? "8PSK" : "QAM16",
953                 sat.orbital_position );
954         return -1;
955 }
956
957 void eDVBSatelliteEquipmentControl::prepareTurnOffSatCR(iDVBFrontend &frontend, int satcr)
958 {
959         eSecCommandList sec_sequence;
960
961         // check if voltage is disabled
962         eSecCommand::pair compare;
963         compare.steps = +9;     //only close frontend
964         compare.voltage = iDVBFrontend::voltageOff;
965
966         sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) );
967         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
968         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS]) );
969
970         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage18_5) );
971         sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
972         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_SWITCH_CMDS]) );
973
974         eDVBDiseqcCommand diseqc;
975         memset(diseqc.data, 0, MAX_DISEQC_LENGTH);
976         diseqc.len = 5;
977         diseqc.data[0] = 0xE0;
978         diseqc.data[1] = 0x10;
979         diseqc.data[2] = 0x5A;
980         diseqc.data[3] = satcr << 5;
981         diseqc.data[4] = 0x00;
982
983         sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
984         sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_LAST_DISEQC_CMD]) );
985         sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
986         sec_sequence.push_back( eSecCommand(eSecCommand::DELAYED_CLOSE_FRONTEND) );
987
988         frontend.setSecSequence(sec_sequence);
989 }
990
991 RESULT eDVBSatelliteEquipmentControl::clear()
992 {
993         eSecDebug("eDVBSatelliteEquipmentControl::clear()");
994         for (int i=0; i <= m_lnbidx; ++i)
995         {
996                 m_lnbs[i].m_satellites.clear();
997                 m_lnbs[i].m_slot_mask = 0;
998                 m_lnbs[i].m_prio = -1; // auto
999         }
1000         m_lnbidx=-1;
1001
1002         m_not_linked_slot_mask=0;
1003
1004         //reset some tuner configuration
1005         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
1006         {
1007                 it->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, -1);
1008                 it->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, -1);
1009                 it->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, -1);
1010                 it->m_frontend->setData(eDVBFrontend::ROTOR_POS, -1);
1011                 it->m_frontend->setData(eDVBFrontend::ROTOR_CMD, -1);
1012                 it->m_frontend->setData(eDVBFrontend::SATCR, -1);
1013         }
1014
1015         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_simulate_frontends.begin()); it != m_avail_simulate_frontends.end(); ++it)
1016         {
1017                 it->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, -1);
1018                 it->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, -1);
1019                 it->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, -1);
1020                 it->m_frontend->setData(eDVBFrontend::ROTOR_POS, -1);
1021                 it->m_frontend->setData(eDVBFrontend::ROTOR_CMD, -1);
1022                 it->m_frontend->setData(eDVBFrontend::SATCR, -1);
1023         }
1024
1025         return 0;
1026 }
1027
1028 /* LNB Specific Parameters */
1029 RESULT eDVBSatelliteEquipmentControl::addLNB()
1030 {
1031         if ( (m_lnbidx+1) < (int)(sizeof(m_lnbs) / sizeof(eDVBSatelliteLNBParameters)))
1032                 m_curSat=m_lnbs[++m_lnbidx].m_satellites.end();
1033         else
1034         {
1035                 eDebug("no more LNB free... cnt is %d", m_lnbidx);
1036                 return -ENOSPC;
1037         }
1038         eSecDebug("eDVBSatelliteEquipmentControl::addLNB(%d)", m_lnbidx);
1039         return 0;
1040 }
1041
1042 RESULT eDVBSatelliteEquipmentControl::setLNBSlotMask(int slotmask)
1043 {
1044         eSecDebug("eDVBSatelliteEquipmentControl::setLNBSlotMask(%d)", slotmask);
1045         if ( currentLNBValid() )
1046                 m_lnbs[m_lnbidx].m_slot_mask = slotmask;
1047         else
1048                 return -ENOENT;
1049         return 0;
1050 }
1051
1052 RESULT eDVBSatelliteEquipmentControl::setLNBLOFL(int lofl)
1053 {
1054         eSecDebug("eDVBSatelliteEquipmentControl::setLNBLOFL(%d)", lofl);
1055         if ( currentLNBValid() )
1056                 m_lnbs[m_lnbidx].m_lof_lo = lofl;
1057         else
1058                 return -ENOENT;
1059         return 0;
1060 }
1061
1062 RESULT eDVBSatelliteEquipmentControl::setLNBLOFH(int lofh)
1063 {
1064         eSecDebug("eDVBSatelliteEquipmentControl::setLNBLOFH(%d)", lofh);
1065         if ( currentLNBValid() )
1066                 m_lnbs[m_lnbidx].m_lof_hi = lofh;
1067         else
1068                 return -ENOENT;
1069         return 0;
1070 }
1071
1072 RESULT eDVBSatelliteEquipmentControl::setLNBThreshold(int threshold)
1073 {
1074         eSecDebug("eDVBSatelliteEquipmentControl::setLNBThreshold(%d)", threshold);
1075         if ( currentLNBValid() )
1076                 m_lnbs[m_lnbidx].m_lof_threshold = threshold;
1077         else
1078                 return -ENOENT;
1079         return 0;
1080 }
1081
1082 RESULT eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(bool onoff)
1083 {
1084         eSecDebug("eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(%d)", onoff);
1085         if ( currentLNBValid() )
1086                 m_lnbs[m_lnbidx].m_increased_voltage = onoff;
1087         else
1088                 return -ENOENT;
1089         return 0;
1090 }
1091
1092 RESULT eDVBSatelliteEquipmentControl::setLNBPrio(int prio)
1093 {
1094         eSecDebug("eDVBSatelliteEquipmentControl::setLNBPrio(%d)", prio);
1095         if ( currentLNBValid() )
1096                 m_lnbs[m_lnbidx].m_prio = prio;
1097         else
1098                 return -ENOENT;
1099         return 0;
1100 }
1101
1102 RESULT eDVBSatelliteEquipmentControl::setLNBNum(int LNBNum)
1103 {
1104         eSecDebug("eDVBSatelliteEquipmentControl::setLNBNum(%d)", LNBNum);
1105         if(!((LNBNum >= 1) && (LNBNum <= MAX_LNBNUM)))
1106                 return -EPERM;
1107         if ( currentLNBValid() )
1108                 m_lnbs[m_lnbidx].LNBNum = LNBNum;
1109         else
1110                 return -ENOENT;
1111         return 0;
1112 }
1113
1114 /* DiSEqC Specific Parameters */
1115 RESULT eDVBSatelliteEquipmentControl::setDiSEqCMode(int diseqcmode)
1116 {
1117         eSecDebug("eDVBSatelliteEquipmentControl::setDiSEqcMode(%d)", diseqcmode);
1118         if ( currentLNBValid() )
1119                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_diseqc_mode = (eDVBSatelliteDiseqcParameters::t_diseqc_mode)diseqcmode;
1120         else
1121                 return -ENOENT;
1122         return 0;
1123 }
1124
1125 RESULT eDVBSatelliteEquipmentControl::setToneburst(int toneburst)
1126 {
1127         eSecDebug("eDVBSatelliteEquipmentControl::setToneburst(%d)", toneburst);
1128         if ( currentLNBValid() )
1129                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_toneburst_param = (eDVBSatelliteDiseqcParameters::t_toneburst_param)toneburst;
1130         else
1131                 return -ENOENT;
1132         return 0;
1133 }
1134
1135 RESULT eDVBSatelliteEquipmentControl::setRepeats(int repeats)
1136 {
1137         eSecDebug("eDVBSatelliteEquipmentControl::setRepeats(%d)", repeats);
1138         if ( currentLNBValid() )
1139                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_repeats=repeats;
1140         else
1141                 return -ENOENT;
1142         return 0;
1143 }
1144
1145 RESULT eDVBSatelliteEquipmentControl::setCommittedCommand(int command)
1146 {
1147         eSecDebug("eDVBSatelliteEquipmentControl::setCommittedCommand(%d)", command);
1148         if ( currentLNBValid() )
1149                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_committed_cmd=command;
1150         else
1151                 return -ENOENT;
1152         return 0;
1153 }
1154
1155 RESULT eDVBSatelliteEquipmentControl::setUncommittedCommand(int command)
1156 {
1157         eSecDebug("eDVBSatelliteEquipmentControl::setUncommittedCommand(%d)", command);
1158         if ( currentLNBValid() )
1159                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_uncommitted_cmd = command;
1160         else
1161                 return -ENOENT;
1162         return 0;
1163 }
1164
1165 RESULT eDVBSatelliteEquipmentControl::setCommandOrder(int order)
1166 {
1167         eSecDebug("eDVBSatelliteEquipmentControl::setCommandOrder(%d)", order);
1168         if ( currentLNBValid() )
1169                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_command_order=order;
1170         else
1171                 return -ENOENT;
1172         return 0;
1173 }
1174
1175 RESULT eDVBSatelliteEquipmentControl::setFastDiSEqC(bool onoff)
1176 {
1177         eSecDebug("eDVBSatelliteEquipmentControl::setFastDiSEqc(%d)", onoff);
1178         if ( currentLNBValid() )
1179                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_use_fast=onoff;
1180         else
1181                 return -ENOENT;
1182         return 0;
1183 }
1184
1185 RESULT eDVBSatelliteEquipmentControl::setSeqRepeat(bool onoff)
1186 {
1187         eSecDebug("eDVBSatelliteEquipmentControl::setSeqRepeat(%d)", onoff);
1188         if ( currentLNBValid() )
1189                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_seq_repeat = onoff;
1190         else
1191                 return -ENOENT;
1192         return 0;
1193 }
1194
1195 /* Rotor Specific Parameters */
1196 RESULT eDVBSatelliteEquipmentControl::setLongitude(float longitude)
1197 {
1198         eSecDebug("eDVBSatelliteEquipmentControl::setLongitude(%f)", longitude);
1199         if ( currentLNBValid() )
1200                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_longitude=longitude;
1201         else
1202                 return -ENOENT;
1203         return 0;
1204 }
1205
1206 RESULT eDVBSatelliteEquipmentControl::setLatitude(float latitude)
1207 {
1208         eSecDebug("eDVBSatelliteEquipmentControl::setLatitude(%f)", latitude);
1209         if ( currentLNBValid() )
1210                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_latitude=latitude;
1211         else
1212                 return -ENOENT;
1213         return 0;
1214 }
1215
1216 RESULT eDVBSatelliteEquipmentControl::setLoDirection(int direction)
1217 {
1218         eSecDebug("eDVBSatelliteEquipmentControl::setLoDirection(%d)", direction);
1219         if ( currentLNBValid() )
1220                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_lo_direction=direction;
1221         else
1222                 return -ENOENT;
1223         return 0;
1224 }
1225
1226 RESULT eDVBSatelliteEquipmentControl::setLaDirection(int direction)
1227 {
1228         eSecDebug("eDVBSatelliteEquipmentControl::setLaDirection(%d)", direction);
1229         if ( currentLNBValid() )
1230                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_la_direction=direction;
1231         else
1232                 return -ENOENT;
1233         return 0;
1234 }
1235
1236 RESULT eDVBSatelliteEquipmentControl::setUseInputpower(bool onoff)
1237 {
1238         eSecDebug("eDVBSatelliteEquipmentControl::setUseInputpower(%d)", onoff);
1239         if ( currentLNBValid() )
1240                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_use=onoff;
1241         else
1242                 return -ENOENT;
1243         return 0;
1244 }
1245
1246 RESULT eDVBSatelliteEquipmentControl::setInputpowerDelta(int delta)
1247 {
1248         eSecDebug("eDVBSatelliteEquipmentControl::setInputpowerDelta(%d)", delta);
1249         if ( currentLNBValid() )
1250                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_delta=delta;
1251         else
1252                 return -ENOENT;
1253         return 0;
1254 }
1255
1256 /* Unicable Specific Parameters */
1257 RESULT eDVBSatelliteEquipmentControl::setLNBSatCR(int SatCR_idx)
1258 {
1259         eSecDebug("eDVBSatelliteEquipmentControl::setLNBSatCR(%d)", SatCR_idx);
1260         if(!((SatCR_idx >=-1) && (SatCR_idx < MAX_SATCR)))
1261                 return -EPERM;
1262         if ( currentLNBValid() )
1263                 m_lnbs[m_lnbidx].SatCR_idx = SatCR_idx;
1264         else
1265                 return -ENOENT;
1266         return 0;
1267 }
1268
1269 RESULT eDVBSatelliteEquipmentControl::setLNBSatCRvco(int SatCRvco)
1270 {
1271         eSecDebug("eDVBSatelliteEquipmentControl::setLNBSatCRvco(%d)", SatCRvco);
1272         if(!((SatCRvco >= 950*1000) && (SatCRvco <= 2150*1000)))
1273                 return -EPERM;
1274         if(!((m_lnbs[m_lnbidx].SatCR_idx >= 0) && (m_lnbs[m_lnbidx].SatCR_idx < MAX_SATCR)))
1275                 return -ENOENT;
1276         if ( currentLNBValid() )
1277                 m_lnbs[m_lnbidx].SatCRvco = SatCRvco;
1278         else
1279                 return -ENOENT;
1280         return 0;
1281 }
1282
1283 RESULT eDVBSatelliteEquipmentControl::setLNBSatCRpositions(int SatCR_positions)
1284 {
1285         eSecDebug("eDVBSatelliteEquipmentControl::setLNBSatCRpositions(%d)", SatCR_positions);
1286         if(SatCR_positions < 1 || SatCR_positions > 2)
1287                 return -EPERM;
1288         if ( currentLNBValid() )
1289                 m_lnbs[m_lnbidx].SatCR_positions = SatCR_positions;
1290         else
1291                 return -ENOENT;
1292         return 0;
1293 }
1294
1295 RESULT eDVBSatelliteEquipmentControl::getLNBSatCRpositions()
1296 {
1297         if ( currentLNBValid() )
1298                 return m_lnbs[m_lnbidx].SatCR_positions;
1299         return -ENOENT;
1300 }
1301
1302 RESULT eDVBSatelliteEquipmentControl::getLNBSatCR()
1303 {
1304         if ( currentLNBValid() )
1305                 return m_lnbs[m_lnbidx].SatCR_idx;
1306         return -ENOENT;
1307 }
1308
1309 RESULT eDVBSatelliteEquipmentControl::getLNBSatCRvco()
1310 {
1311         if ( currentLNBValid() )
1312                 return m_lnbs[m_lnbidx].SatCRvco;
1313         return -ENOENT;
1314 }
1315
1316 /* Satellite Specific Parameters */
1317 RESULT eDVBSatelliteEquipmentControl::addSatellite(int orbital_position)
1318 {
1319         eSecDebug("eDVBSatelliteEquipmentControl::addSatellite(%d)", orbital_position);
1320         if ( currentLNBValid() )
1321         {
1322                 std::map<int, eDVBSatelliteSwitchParameters>::iterator it =
1323                         m_lnbs[m_lnbidx].m_satellites.find(orbital_position);
1324                 if ( it == m_lnbs[m_lnbidx].m_satellites.end() )
1325                 {
1326                         std::pair<std::map<int, eDVBSatelliteSwitchParameters>::iterator, bool > ret =
1327                                 m_lnbs[m_lnbidx].m_satellites.insert(
1328                                         std::pair<int, eDVBSatelliteSwitchParameters>(orbital_position, eDVBSatelliteSwitchParameters())
1329                                 );
1330                         if ( ret.second )
1331                                 m_curSat = ret.first;
1332                         else
1333                                 return -ENOMEM;
1334                 }
1335                 else
1336                         return -EEXIST;
1337         }
1338         else
1339                 return -ENOENT;
1340         return 0;
1341 }
1342
1343 RESULT eDVBSatelliteEquipmentControl::setVoltageMode(int mode)
1344 {
1345         eSecDebug("eDVBSatelliteEquipmentControl::setVoltageMode(%d)", mode);
1346         if ( currentLNBValid() && m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1347                 m_curSat->second.m_voltage_mode = (eDVBSatelliteSwitchParameters::t_voltage_mode)mode;
1348         else
1349                 return -ENOENT;
1350         return 0;
1351
1352 }
1353
1354 RESULT eDVBSatelliteEquipmentControl::setToneMode(int mode)
1355 {
1356         eSecDebug("eDVBSatelliteEquipmentControl::setToneMode(%d)", mode);
1357         if ( currentLNBValid() )
1358         {
1359                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1360                         m_curSat->second.m_22khz_signal = (eDVBSatelliteSwitchParameters::t_22khz_signal)mode;
1361                 else
1362                         return -EPERM;
1363         }
1364         else
1365                 return -ENOENT;
1366         return 0;
1367 }
1368
1369 RESULT eDVBSatelliteEquipmentControl::setRotorPosNum(int rotor_pos_num)
1370 {
1371         eSecDebug("eDVBSatelliteEquipmentControl::setRotorPosNum(%d)", rotor_pos_num);
1372         if ( currentLNBValid() )
1373         {
1374                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1375                         m_curSat->second.m_rotorPosNum=rotor_pos_num;
1376                 else
1377                         return -EPERM;
1378         }
1379         else
1380                 return -ENOENT;
1381         return 0;
1382 }
1383
1384 RESULT eDVBSatelliteEquipmentControl::setRotorTurningSpeed(int speed)
1385 {
1386         eSecDebug("eDVBSatelliteEquipmentControl::setRotorTurningSpeed(%d)", speed);
1387         if ( currentLNBValid() )
1388                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_turning_speed = speed;
1389         else
1390                 return -ENOENT;
1391         return 0;
1392 }
1393
1394 struct sat_compare
1395 {
1396         int orb_pos, lofl, lofh;
1397         sat_compare(int o, int lofl, int lofh)
1398                 :orb_pos(o), lofl(lofl), lofh(lofh)
1399         {}
1400         sat_compare(const sat_compare &x)
1401                 :orb_pos(x.orb_pos), lofl(x.lofl), lofh(x.lofh)
1402         {}
1403         bool operator < (const sat_compare & cmp) const
1404         {
1405                 if (orb_pos == cmp.orb_pos)
1406                 {
1407                         if ( abs(lofl-cmp.lofl) < 200000 )
1408                         {
1409                                 if (abs(lofh-cmp.lofh) < 200000)
1410                                         return false;
1411                                 return lofh<cmp.lofh;
1412                         }
1413                         return lofl<cmp.lofl;
1414                 }
1415                 return orb_pos < cmp.orb_pos;
1416         }
1417 };
1418
1419 RESULT eDVBSatelliteEquipmentControl::setTunerLinked(int tu1, int tu2)
1420 {
1421         eSecDebug("eDVBSatelliteEquipmentControl::setTunerLinked(%d, %d)", tu1, tu2);
1422         if (tu1 != tu2)
1423         {
1424                 eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1425                 eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin());
1426                 for (; it != m_avail_frontends.end(); ++it)
1427                 {
1428                         if (it->m_frontend->getSlotID() == tu1)
1429                                 p1 = *it;
1430                         else if (it->m_frontend->getSlotID() == tu2)
1431                                 p2 = *it;
1432                 }
1433                 if (p1 && p2)
1434                 {
1435                         char c;
1436                         p1->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, (long)p2);
1437                         p2->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, (long)p1);
1438                 }
1439
1440                 p1=p2=NULL;
1441                 it=m_avail_simulate_frontends.begin();
1442                 for (; it != m_avail_simulate_frontends.end(); ++it)
1443                 {
1444                         if (it->m_frontend->getSlotID() == tu1)
1445                                 p1 = *it;
1446                         else if (it->m_frontend->getSlotID() == tu2)
1447                                 p2 = *it;
1448                 }
1449                 if (p1 && p2)
1450                 {
1451                         p1->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, (long)p2);
1452                         p2->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, (long)p1);
1453                         return 0;
1454                 }
1455         }
1456         return -1;
1457 }
1458
1459 RESULT eDVBSatelliteEquipmentControl::setTunerDepends(int tu1, int tu2)
1460 {
1461         eSecDebug("eDVBSatelliteEquipmentControl::setTunerDepends(%d, %d)", tu1, tu2);
1462         if (tu1 == tu2)
1463                 return -1;
1464
1465         eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1466
1467         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
1468         {
1469                 if (it->m_frontend->getSlotID() == tu1)
1470                         p1 = *it;
1471                 else if (it->m_frontend->getSlotID() == tu2)
1472                         p2 = *it;
1473         }
1474         if (p1 && p2)
1475         {
1476                 p1->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (long)p2);
1477                 p2->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (long)p1);
1478         }
1479
1480         p1=p2=NULL;
1481         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_simulate_frontends.begin()); it != m_avail_simulate_frontends.end(); ++it)
1482         {
1483                 if (it->m_frontend->getSlotID() == tu1)
1484                         p1 = *it;
1485                 else if (it->m_frontend->getSlotID() == tu2)
1486                         p2 = *it;
1487         }
1488         if (p1 && p2)
1489         {
1490                 p1->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (long)p2);
1491                 p2->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (long)p1);
1492                 return 0;
1493         }
1494
1495         return -1;
1496 }
1497
1498 void eDVBSatelliteEquipmentControl::setSlotNotLinked(int slot_no)
1499 {
1500         eSecDebug("eDVBSatelliteEquipmentControl::setSlotNotLinked(%d)", slot_no);
1501         m_not_linked_slot_mask |= (1 << slot_no);
1502 }
1503
1504 bool eDVBSatelliteEquipmentControl::isRotorMoving()
1505 {
1506         return m_rotorMoving;
1507 }
1508
1509 void eDVBSatelliteEquipmentControl::setRotorMoving(int slot_no, bool b)
1510 {
1511         if (b)
1512                 m_rotorMoving |= (1 << slot_no);
1513         else
1514                 m_rotorMoving &= ~(1 << slot_no);
1515 }