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