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