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