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