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