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