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