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