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