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