b1f421c06a0e01ad3c62551652d2a8a466f198f7
[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         else
855                 eFatal("canTune failed in sec prepare!!! this should never ever happen!");
856
857         eDebug("found no useable satellite configuration for orbital position (%d)", sat.orbital_position );
858         return -1;
859 }
860
861 RESULT eDVBSatelliteEquipmentControl::clear()
862 {
863         eSecDebug("eDVBSatelliteEquipmentControl::clear()");
864         for (int i=0; i <= m_lnbidx; ++i)
865         {
866                 m_lnbs[i].m_satellites.clear();
867                 m_lnbs[i].slot_mask = 0;
868         }
869         m_lnbidx=-1;
870
871         m_not_linked_slot_mask=0;
872
873         //reset some tuner configuration
874         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
875         {
876                 long tmp;
877                 if (!strcmp(it->m_frontend->getDescription(), "BCM4501 (internal)") && !it->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, tmp) && tmp != -1)
878                 {
879                         FILE *f=fopen("/proc/stb/tsmux/lnb_b_input", "w");
880                         if (!f || fwrite("B", 1, 1, f) != 1)
881                                 eDebug("set /proc/stb/tsmux/lnb_b_input to B failed!! (%m)");
882                         else
883                         {
884                                 eDebug("set /proc/stb/tsmux/lnb_b_input to B OK");
885                                 fclose(f);
886                         }
887                 }
888                 it->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, -1);
889                 it->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, -1);
890                 it->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, -1);
891                 it->m_frontend->setData(eDVBFrontend::ROTOR_POS, -1);
892                 it->m_frontend->setData(eDVBFrontend::ROTOR_CMD, -1);
893         }
894
895         return 0;
896 }
897
898 /* LNB Specific Parameters */
899 RESULT eDVBSatelliteEquipmentControl::addLNB()
900 {
901         if ( (m_lnbidx+1) < (int)(sizeof(m_lnbs) / sizeof(eDVBSatelliteLNBParameters)))
902                 m_curSat=m_lnbs[++m_lnbidx].m_satellites.end();
903         else
904         {
905                 eDebug("no more LNB free... cnt is %d", m_lnbidx);
906                 return -ENOSPC;
907         }
908         eSecDebug("eDVBSatelliteEquipmentControl::addLNB(%d)", m_lnbidx);
909         return 0;
910 }
911
912 RESULT eDVBSatelliteEquipmentControl::setLNBSlotMask(int slotmask)
913 {
914         eSecDebug("eDVBSatelliteEquipmentControl::setLNBSlotMask(%d)", slotmask);
915         if ( currentLNBValid() )
916                 m_lnbs[m_lnbidx].slot_mask = slotmask;
917         else
918                 return -ENOENT;
919         return 0;
920 }
921
922 RESULT eDVBSatelliteEquipmentControl::setLNBLOFL(int lofl)
923 {
924         eSecDebug("eDVBSatelliteEquipmentControl::setLNBLOFL(%d)", lofl);
925         if ( currentLNBValid() )
926                 m_lnbs[m_lnbidx].m_lof_lo = lofl;
927         else
928                 return -ENOENT;
929         return 0;
930 }
931
932 RESULT eDVBSatelliteEquipmentControl::setLNBLOFH(int lofh)
933 {
934         eSecDebug("eDVBSatelliteEquipmentControl::setLNBLOFH(%d)", lofh);
935         if ( currentLNBValid() )
936                 m_lnbs[m_lnbidx].m_lof_hi = lofh;
937         else
938                 return -ENOENT;
939         return 0;
940 }
941
942 RESULT eDVBSatelliteEquipmentControl::setLNBThreshold(int threshold)
943 {
944         eSecDebug("eDVBSatelliteEquipmentControl::setLNBThreshold(%d)", threshold);
945         if ( currentLNBValid() )
946                 m_lnbs[m_lnbidx].m_lof_threshold = threshold;
947         else
948                 return -ENOENT;
949         return 0;
950 }
951
952 RESULT eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(bool onoff)
953 {
954         eSecDebug("eDVBSatelliteEquipmentControl::setLNBIncreasedVoltage(%d)", onoff);
955         if ( currentLNBValid() )
956                 m_lnbs[m_lnbidx].m_increased_voltage = onoff;
957         else
958                 return -ENOENT;
959         return 0;
960 }
961
962 /* DiSEqC Specific Parameters */
963 RESULT eDVBSatelliteEquipmentControl::setDiSEqCMode(int diseqcmode)
964 {
965         eSecDebug("eDVBSatelliteEquipmentControl::setDiSEqcMode(%d)", diseqcmode);
966         if ( currentLNBValid() )
967                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_diseqc_mode = (eDVBSatelliteDiseqcParameters::t_diseqc_mode)diseqcmode;
968         else
969                 return -ENOENT;
970         return 0;
971 }
972
973 RESULT eDVBSatelliteEquipmentControl::setToneburst(int toneburst)
974 {
975         eSecDebug("eDVBSatelliteEquipmentControl::setToneburst(%d)", toneburst);
976         if ( currentLNBValid() )
977                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_toneburst_param = (eDVBSatelliteDiseqcParameters::t_toneburst_param)toneburst;
978         else
979                 return -ENOENT;
980         return 0;
981 }
982
983 RESULT eDVBSatelliteEquipmentControl::setRepeats(int repeats)
984 {
985         eSecDebug("eDVBSatelliteEquipmentControl::setRepeats(%d)", repeats);
986         if ( currentLNBValid() )
987                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_repeats=repeats;
988         else
989                 return -ENOENT;
990         return 0;
991 }
992
993 RESULT eDVBSatelliteEquipmentControl::setCommittedCommand(int command)
994 {
995         eSecDebug("eDVBSatelliteEquipmentControl::setCommittedCommand(%d)", command);
996         if ( currentLNBValid() )
997                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_committed_cmd=command;
998         else
999                 return -ENOENT;
1000         return 0;
1001 }
1002
1003 RESULT eDVBSatelliteEquipmentControl::setUncommittedCommand(int command)
1004 {
1005         eSecDebug("eDVBSatelliteEquipmentControl::setUncommittedCommand(%d)", command);
1006         if ( currentLNBValid() )
1007                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_uncommitted_cmd = command;
1008         else
1009                 return -ENOENT;
1010         return 0;
1011 }
1012
1013 RESULT eDVBSatelliteEquipmentControl::setCommandOrder(int order)
1014 {
1015         eSecDebug("eDVBSatelliteEquipmentControl::setCommandOrder(%d)", order);
1016         if ( currentLNBValid() )
1017                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_command_order=order;
1018         else
1019                 return -ENOENT;
1020         return 0;
1021 }
1022
1023 RESULT eDVBSatelliteEquipmentControl::setFastDiSEqC(bool onoff)
1024 {
1025         eSecDebug("eDVBSatelliteEquipmentControl::setFastDiSEqc(%d)", onoff);
1026         if ( currentLNBValid() )
1027                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_use_fast=onoff;
1028         else
1029                 return -ENOENT;
1030         return 0;
1031 }
1032
1033 RESULT eDVBSatelliteEquipmentControl::setSeqRepeat(bool onoff)
1034 {
1035         eSecDebug("eDVBSatelliteEquipmentControl::setSeqRepeat(%d)", onoff);
1036         if ( currentLNBValid() )
1037                 m_lnbs[m_lnbidx].m_diseqc_parameters.m_seq_repeat = onoff;
1038         else
1039                 return -ENOENT;
1040         return 0;
1041 }
1042
1043 /* Rotor Specific Parameters */
1044 RESULT eDVBSatelliteEquipmentControl::setLongitude(float longitude)
1045 {
1046         eSecDebug("eDVBSatelliteEquipmentControl::setLongitude(%f)", longitude);
1047         if ( currentLNBValid() )
1048                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_longitude=longitude;
1049         else
1050                 return -ENOENT;
1051         return 0;
1052 }
1053
1054 RESULT eDVBSatelliteEquipmentControl::setLatitude(float latitude)
1055 {
1056         eSecDebug("eDVBSatelliteEquipmentControl::setLatitude(%f)", latitude);
1057         if ( currentLNBValid() )
1058                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_latitude=latitude;
1059         else
1060                 return -ENOENT;
1061         return 0;
1062 }
1063
1064 RESULT eDVBSatelliteEquipmentControl::setLoDirection(int direction)
1065 {
1066         eSecDebug("eDVBSatelliteEquipmentControl::setLoDirection(%d)", direction);
1067         if ( currentLNBValid() )
1068                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_lo_direction=direction;
1069         else
1070                 return -ENOENT;
1071         return 0;
1072 }
1073
1074 RESULT eDVBSatelliteEquipmentControl::setLaDirection(int direction)
1075 {
1076         eSecDebug("eDVBSatelliteEquipmentControl::setLaDirection(%d)", direction);
1077         if ( currentLNBValid() )
1078                 m_lnbs[m_lnbidx].m_rotor_parameters.m_gotoxx_parameters.m_la_direction=direction;
1079         else
1080                 return -ENOENT;
1081         return 0;
1082 }
1083
1084 RESULT eDVBSatelliteEquipmentControl::setUseInputpower(bool onoff)
1085 {
1086         eSecDebug("eDVBSatelliteEquipmentControl::setUseInputpower(%d)", onoff);
1087         if ( currentLNBValid() )
1088                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_use=onoff;
1089         else
1090                 return -ENOENT;
1091         return 0;
1092 }
1093
1094 RESULT eDVBSatelliteEquipmentControl::setInputpowerDelta(int delta)
1095 {
1096         eSecDebug("eDVBSatelliteEquipmentControl::setInputpowerDelta(%d)", delta);
1097         if ( currentLNBValid() )
1098                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_delta=delta;
1099         else
1100                 return -ENOENT;
1101         return 0;
1102 }
1103
1104 /* Satellite Specific Parameters */
1105 RESULT eDVBSatelliteEquipmentControl::addSatellite(int orbital_position)
1106 {
1107         eSecDebug("eDVBSatelliteEquipmentControl::addSatellite(%d)", orbital_position);
1108         if ( currentLNBValid() )
1109         {
1110                 std::map<int, eDVBSatelliteSwitchParameters>::iterator it =
1111                         m_lnbs[m_lnbidx].m_satellites.find(orbital_position);
1112                 if ( it == m_lnbs[m_lnbidx].m_satellites.end() )
1113                 {
1114                         std::pair<std::map<int, eDVBSatelliteSwitchParameters>::iterator, bool > ret =
1115                                 m_lnbs[m_lnbidx].m_satellites.insert(
1116                                         std::pair<int, eDVBSatelliteSwitchParameters>(orbital_position, eDVBSatelliteSwitchParameters())
1117                                 );
1118                         if ( ret.second )
1119                                 m_curSat = ret.first;
1120                         else
1121                                 return -ENOMEM;
1122                 }
1123                 else
1124                         return -EEXIST;
1125         }
1126         else
1127                 return -ENOENT;
1128         return 0;
1129 }
1130
1131 RESULT eDVBSatelliteEquipmentControl::setVoltageMode(int mode)
1132 {
1133         eSecDebug("eDVBSatelliteEquipmentControl::setVoltageMode(%d)", mode);
1134         if ( currentLNBValid() && m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1135                 m_curSat->second.m_voltage_mode = (eDVBSatelliteSwitchParameters::t_voltage_mode)mode;
1136         else
1137                 return -ENOENT;
1138         return 0;
1139
1140 }
1141
1142 RESULT eDVBSatelliteEquipmentControl::setToneMode(int mode)
1143 {
1144         eSecDebug("eDVBSatelliteEquipmentControl::setToneMode(%d)", mode);
1145         if ( currentLNBValid() )
1146         {
1147                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1148                         m_curSat->second.m_22khz_signal = (eDVBSatelliteSwitchParameters::t_22khz_signal)mode;
1149                 else
1150                         return -EPERM;
1151         }
1152         else
1153                 return -ENOENT;
1154         return 0;
1155 }
1156
1157 RESULT eDVBSatelliteEquipmentControl::setRotorPosNum(int rotor_pos_num)
1158 {
1159         eSecDebug("eDVBSatelliteEquipmentControl::setRotorPosNum(%d)", rotor_pos_num);
1160         if ( currentLNBValid() )
1161         {
1162                 if ( m_curSat != m_lnbs[m_lnbidx].m_satellites.end() )
1163                         m_curSat->second.m_rotorPosNum=rotor_pos_num;
1164                 else
1165                         return -EPERM;
1166         }
1167         else
1168                 return -ENOENT;
1169         return 0;
1170 }
1171
1172 RESULT eDVBSatelliteEquipmentControl::setRotorTurningSpeed(int speed)
1173 {
1174         eSecDebug("eDVBSatelliteEquipmentControl::setRotorTurningSpeed(%d)", speed);
1175         if ( currentLNBValid() )
1176                 m_lnbs[m_lnbidx].m_rotor_parameters.m_inputpower_parameters.m_turning_speed = speed;
1177         else
1178                 return -ENOENT;
1179         return 0;
1180 }
1181
1182 struct sat_compare
1183 {
1184         int orb_pos, lofl, lofh;
1185         sat_compare(int o, int lofl, int lofh)
1186                 :orb_pos(o), lofl(lofl), lofh(lofh)
1187         {}
1188         sat_compare(const sat_compare &x)
1189                 :orb_pos(x.orb_pos), lofl(x.lofl), lofh(x.lofh)
1190         {}
1191         bool operator < (const sat_compare & cmp) const
1192         {
1193                 if (orb_pos == cmp.orb_pos)
1194                 {
1195                         if ( abs(lofl-cmp.lofl) < 200000 )
1196                         {
1197                                 if (abs(lofh-cmp.lofh) < 200000)
1198                                         return false;
1199                                 return lofh<cmp.lofh;
1200                         }
1201                         return lofl<cmp.lofl;
1202                 }
1203                 return orb_pos < cmp.orb_pos;
1204         }
1205 };
1206
1207 RESULT eDVBSatelliteEquipmentControl::setTunerLinked(int tu1, int tu2)
1208 {
1209         eSecDebug("eDVBSatelliteEquipmentControl::setTunerLinked(%d, %d)", tu1, tu2);
1210         if (tu1 != tu2)
1211         {
1212                 eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1213
1214                 for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
1215                 {
1216                         if (it->m_frontend->getSlotID() == tu1)
1217                                 p1 = *it;
1218                         else if (it->m_frontend->getSlotID() == tu2)
1219                                 p2 = *it;
1220                 }
1221                 if (p1 && p2)
1222                 {
1223                         p1->m_frontend->setData(eDVBFrontend::LINKED_PREV_PTR, (long)p2);
1224                         p2->m_frontend->setData(eDVBFrontend::LINKED_NEXT_PTR, (long)p1);
1225                         if (!strcmp(p1->m_frontend->getDescription(), p2->m_frontend->getDescription()) && !strcmp(p1->m_frontend->getDescription(), "BCM4501 (internal)"))
1226                         {
1227                                 FILE *f=fopen("/proc/stb/tsmux/lnb_b_input", "w");
1228                                 if (!f || fwrite("A", 1, 1, f) != 1)
1229                                         eDebug("set /proc/stb/tsmux/lnb_b_input to A failed!! (%m)");
1230                                 else
1231                                 {
1232                                         eDebug("set /proc/stb/tsmux/lnb_b_input to A OK");
1233                                         fclose(f);
1234                                 }
1235                         }
1236                         return 0;
1237                 }
1238         }
1239         return -1;
1240 }
1241
1242 RESULT eDVBSatelliteEquipmentControl::setTunerDepends(int tu1, int tu2)
1243 {
1244         eSecDebug("eDVBSatelliteEquipmentControl::setTunerDepends(%d, %d)", tu1, tu2);
1245         if (tu1 == tu2)
1246                 return -1;
1247
1248         eDVBRegisteredFrontend *p1=NULL, *p2=NULL;
1249
1250         for (eSmartPtrList<eDVBRegisteredFrontend>::iterator it(m_avail_frontends.begin()); it != m_avail_frontends.end(); ++it)
1251         {
1252                 if (it->m_frontend->getSlotID() == tu1)
1253                         p1 = *it;
1254                 else if (it->m_frontend->getSlotID() == tu2)
1255                         p2 = *it;
1256         }
1257         if (p1 && p2)
1258         {
1259                 p1->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (long)p2);
1260                 p2->m_frontend->setData(eDVBFrontend::SATPOS_DEPENDS_PTR, (long)p1);
1261                 return 0;
1262         }
1263         return -1;
1264 }
1265
1266 void eDVBSatelliteEquipmentControl::setSlotNotLinked(int slot_no)
1267 {
1268         eSecDebug("eDVBSatelliteEquipmentControl::setSlotNotLinked(%d)", slot_no);
1269         m_not_linked_slot_mask |= (1 << slot_no);
1270 }
1271
1272 bool eDVBSatelliteEquipmentControl::isRotorMoving()
1273 {
1274         return m_rotorMoving;
1275 }
1276
1277 void eDVBSatelliteEquipmentControl::setRotorMoving(bool b)
1278 {
1279         m_rotorMoving=b;
1280 }