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