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