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