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