fix python refcounting
[enigma2.git] / lib / dvb / sec.cpp
index ab5d6397fcba617081c9467ad1d951fedb6c50b9..0c1d79ab29d8195b49fc9875d04bf045f3e86b71 100644 (file)
@@ -28,7 +28,6 @@ eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBR
 
        clear();
 
-#if 1
 // ASTRA
        addLNB();
        setLNBTunerMask(3);
@@ -62,7 +61,6 @@ eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBR
        addSatellite(130);
        setVoltageMode(eDVBSatelliteSwitchParameters::HV);
        setToneMode(eDVBSatelliteSwitchParameters::HILO);
-#else
 
 // Rotor
        addLNB();
@@ -84,12 +82,7 @@ eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBR
        setUseInputpower(true);
        setInputpowerDelta(50);
 
-       addSatellite(130);
-       setVoltageMode(eDVBSatelliteSwitchParameters::HV);
-       setToneMode(eDVBSatelliteSwitchParameters::HILO);
-       setRotorPosNum(0);
-
-       addSatellite(192);
+       addSatellite(235);
        setVoltageMode(eDVBSatelliteSwitchParameters::HV);
        setToneMode(eDVBSatelliteSwitchParameters::HILO);
        setRotorPosNum(0);
@@ -103,7 +96,6 @@ eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBR
        setVoltageMode(eDVBSatelliteSwitchParameters::HV);
        setToneMode(eDVBSatelliteSwitchParameters::HILO);
        setRotorPosNum(1); // stored pos 1
-#endif
 }
 
 int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite &sat, iDVBFrontend *fe, int frontend_id )
@@ -143,10 +135,9 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                                {
                                        diseqc=true;
                                        if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
-                                       {
                                                csw = 0xF0 | (csw << 2);
-                                               csw |= band;
-                                       }
+
+                                       csw |= band;
 
                                        if ( di_param.m_diseqc_mode == eDVBSatelliteDiseqcParameters::V1_2 )  // ROTOR
                                        {
@@ -157,7 +148,9 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                                                        ret=10;
                                        }
                                }
-                               
+                               else
+                                       csw = band;
+
                                if (!ret)
                                        ret=40;
 
@@ -201,6 +194,8 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
        return ret;
 }
 
+#define VOLTAGE(x) (lnb_param.m_increased_voltage ? iDVBFrontend::voltage##x##_5 : iDVBFrontend::voltage##x)
+
 RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, eDVBFrontendParametersSatellite &sat, int frontend_id)
 {
        bool linked=false;
@@ -260,7 +255,19 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                        if (sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal)
                                band |= 2;
 
-                       parm.INVERSION = (!sat.inversion) ? INVERSION_ON : INVERSION_OFF;
+                       switch (sat.inversion)
+                       {
+                               case eDVBFrontendParametersCable::Inversion::On:
+                                       parm.INVERSION = INVERSION_ON;
+                                       break;
+                               case eDVBFrontendParametersCable::Inversion::Off:
+                                       parm.INVERSION = INVERSION_OFF;
+                                       break;
+                               default:
+                               case eDVBFrontendParametersCable::Inversion::Unknown:
+                                       parm.INVERSION = INVERSION_AUTO;
+                                       break;
+                       }
 
                        switch (sat.fec)
                        {
@@ -292,12 +299,11 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                        if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_14V
                                || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Vertical
                                        && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
-                               voltage = iDVBFrontend::voltage13;
+                               voltage = VOLTAGE(13);
                        else if ( sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::_18V
                                || ( sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal
                                        && sw_param.m_voltage_mode == eDVBSatelliteSwitchParameters::HV )  )
-                               voltage = iDVBFrontend::voltage18;
-
+                               voltage = VOLTAGE(18);
                        if ( (sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::ON)
                                || ( sw_param.m_22khz_signal == eDVBSatelliteSwitchParameters::HILO && (band&1) ) )
                                tone = iDVBFrontend::toneOn;
@@ -310,10 +316,9 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                        if (di_param.m_diseqc_mode >= eDVBSatelliteDiseqcParameters::V1_0)
                        {
                                if ( di_param.m_committed_cmd < eDVBSatelliteDiseqcParameters::SENDNO )
-                               {
                                        csw = 0xF0 | (csw << 2);
-                                       csw |= band;
-                               }
+
+                               csw |= band;
 
                                bool send_csw =
                                        (di_param.m_committed_cmd != eDVBSatelliteDiseqcParameters::SENDNO);
@@ -327,20 +332,57 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                        (di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO);
                                bool changed_burst = send_burst && toneburst != lastToneburst;
 
-                               bool send_diseqc = changed_ucsw;
-                               if (!send_diseqc)
-                                       send_diseqc = changed_burst && (send_ucsw || send_csw);
-                               if (!send_diseqc)
+                               int send_mask = 0; /*
+                                       1 must send csw
+                                       2 must send ucsw
+                                       4 send toneburst first
+                                       8 send toneburst at end */
+                               if (changed_burst) // toneburst first and toneburst changed
                                {
-                                       send_diseqc = changed_csw;
-                                       if ( send_diseqc && di_param.m_use_fast && (csw & 0xF0) && (lastcsw & 0xF0) && ((csw / 4) == (lastcsw / 4)) )
+                                       if (di_param.m_command_order&1)
                                        {
-                                               frontend.setData(0, csw);  // needed for linked tuner handling
-                                               send_diseqc = false;
+                                               send_mask |= 4;
+                                               if ( send_csw )
+                                                       send_mask |= 1;
+                                               if ( send_ucsw )
+                                                       send_mask |= 2;
                                        }
+                                       else
+                                               send_mask |= 8;
                                }
+                               if (changed_ucsw)
+                               {
+                                       send_mask |= 2;
+                                       if ((di_param.m_command_order&4) && send_csw)
+                                               send_mask |= 1;
+                                       if (di_param.m_command_order==4 && send_burst)
+                                               send_mask |= 8;
+                               }
+                               if (changed_csw) 
+                               {
+                                       if ( di_param.m_use_fast && (lastcsw & 0xF0) && ((csw / 4) == (lastcsw / 4)) )
+                                               eDebug("dont send committed cmd (fast diseqc)");
+                                       else
+                                       {
+                                               send_mask |= 1;
+                                               if (!(di_param.m_command_order&4) && send_ucsw)
+                                                       send_mask |= 2;
+                                               if (!(di_param.m_command_order&1) && send_burst)
+                                                       send_mask |= 8;
+                                       }
+                               }
+
+#if 0
+                               eDebugNoNewLine("sendmask: ");
+                               for (int i=3; i >= 0; --i)
+                                       if ( send_mask & (1<<i) )
+                                               eDebugNoNewLine("1");
+                                       else
+                                               eDebugNoNewLine("0");
+                               eDebug("");
+#endif
 
-                               if ( send_diseqc || changed_burst )
+                               if ( send_mask )
                                {
                                        sec_sequence.push_back( eSecCommand(eSecCommand::SET_TONE, iDVBFrontend::toneOff) );
                                        eSecCommand::pair compare;
@@ -353,83 +395,72 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
 
                                for (int seq_repeat = 0; seq_repeat < (di_param.m_seq_repeat?2:1); ++seq_repeat)
                                {
-                                       if ( di_param.m_command_order & 1 && // toneburst at begin of sequence
-                                               changed_burst && di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO )
+                                       if ( send_mask & 4 )
                                        {
                                                sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
                                                sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
                                        }
 
-                                       if ( send_diseqc )
-                                       {
-                                               int loops=0;
+                                       int loops=0;
 
-                                               if ( send_csw )
-                                                       ++loops;
-                                               if ( send_ucsw )
-                                                       ++loops;
+                                       if ( send_mask & 1 )
+                                               ++loops;
+                                       if ( send_mask & 2 )
+                                               ++loops;
 
-                                               for ( int i=0; i < di_param.m_repeats; ++i )
-                                                       loops *= 2;
+                                       for ( int i=0; i < di_param.m_repeats; ++i )
+                                               loops *= 2;
 
-                                               for ( int i = 0; i < loops;)  // fill commands...
+                                       for ( int i = 0; i < loops;)  // fill commands...
+                                       {
+                                               eDVBDiseqcCommand diseqc;
+                                               diseqc.len = 4;
+                                               diseqc.data[0] = i ? 0xE1 : 0xE0;
+                                               diseqc.data[1] = 0x10;
+                                               if ( (send_mask & 2) && (di_param.m_command_order & 4) )
                                                {
-                                                       eDVBDiseqcCommand diseqc;
-                                                       diseqc.len = 4;
-                                                       diseqc.data[0] = i ? 0xE1 : 0xE0;
-                                                       diseqc.data[1] = 0x10;
-
-                                                       if ( !send_csw || (send_ucsw && (di_param.m_command_order & 4) ) )
-                                                       {
-                                                               diseqc.data[2] = 0x39;
-                                                               diseqc.data[3] = ucsw;
-                                                       }
-                                                       else
-                                                       {
-                                                               diseqc.data[2] = 0x38;
-                                                               diseqc.data[3] = csw;
-                                                       }
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
+                                                       diseqc.data[2] = 0x39;
+                                                       diseqc.data[3] = ucsw;
+                                               }
+                                               else if ( send_mask & 1 )
+                                               {
+                                                       diseqc.data[2] = 0x38;
+                                                       diseqc.data[3] = csw;
+                                               }
+                                               sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
 
-                                                       i++;
-                                                       if ( i < loops )
+                                               i++;
+                                               if ( i < loops )
+                                               {
+                                                       int cmd=0;
+                                                       if (diseqc.data[2] == 0x38 && (send_mask & 2))
+                                                               cmd=0x39;
+                                                       else if (diseqc.data[2] == 0x39 && (send_mask & 1))
+                                                               cmd=0x38;
+                                                       if (cmd)
                                                        {
-                                                               int cmd=0;
-                                                               if (diseqc.data[2] == 0x38 && send_ucsw)
-                                                                       cmd=0x39;
-                                                               else if (diseqc.data[2] == 0x39 && send_csw)
-                                                                       cmd=0x38;
-                                                               if (cmd)
-                                                               {
-                                                                       static int delay = (120 - 54) / 2;  // standard says 100msek between two repeated commands
-                                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
-                                                                       diseqc.data[2]=cmd;
-                                                                       diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
-                                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
-                                                                       ++i;
-                                                                       if ( i < loops )
-                                                                               sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
-                                                                       else
-                                                                               sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
-                                                               }
-                                                               else  // delay 120msek when no command is in repeat gap
-                                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 120) );
+                                                               static int delay = (120 - 54) / 2;  // standard says 100msek between two repeated commands
+                                                               sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay) );
+                                                               diseqc.data[2]=cmd;
+                                                               diseqc.data[3]=(cmd==0x38) ? csw : ucsw;
+                                                               sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
+                                                               ++i;
+                                                               if ( i < loops )
+                                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, delay ) );
+                                                               else
+                                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
                                                        }
-                                                       else
-                                                               sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
-
-                                                       frontend.setData(0, csw);
-                                                       frontend.setData(1, ucsw);
-                                                       frontend.setData(2, di_param.m_toneburst_param);
+                                                       else  // delay 120msek when no command is in repeat gap
+                                                               sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 120) );
                                                }
+                                               else
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
                                        }
 
-                                       if ( !(di_param.m_command_order & 1) && // toneburst at end of sequence
-                                               (changed_burst || send_diseqc) && di_param.m_toneburst_param != eDVBSatelliteDiseqcParameters::NO )
+                                       if ( send_mask & 8 )  // toneburst at end of sequence
                                        {
                                                sec_sequence.push_back( eSecCommand(eSecCommand::SEND_TONEBURST, di_param.m_toneburst_param) );
                                                sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );
-                                               frontend.setData(2, di_param.m_toneburst_param);
                                        }
                                }
 
@@ -445,10 +476,6 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                eDebug("Entry for %d,%d? not in Rotor Table found... i try gotoXX?", sat.orbital_position / 10, sat.orbital_position % 10 );
                                                useGotoXX = true;
 
-                                               int satDir = sat.orbital_position < 0 ?
-                                                       eDVBSatelliteRotorParameters::WEST :
-                                                       eDVBSatelliteRotorParameters::EAST;
-
                                                double  SatLon = abs(sat.orbital_position)/10.00,
                                                                SiteLat = rotor_param.m_gotoxx_parameters.m_latitude,
                                                                SiteLon = rotor_param.m_gotoxx_parameters.m_longitude;
@@ -459,9 +486,6 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                if ( rotor_param.m_gotoxx_parameters.m_lo_direction == eDVBSatelliteRotorParameters::WEST )
                                                        SiteLon = 360 - SiteLon;
 
-                                               if (satDir == eDVBSatelliteRotorParameters::WEST )
-                                                       SatLon = 360 - SatLon;
-
                                                eDebug("siteLatitude = %lf, siteLongitude = %lf, %lf degrees", SiteLat, SiteLon, SatLon );
                                                double satHourAngle =
                                                        calcSatHourangle( SatLon, SiteLat, SiteLon );
@@ -499,10 +523,10 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                        }
                                        if ( RotorCmd != lastRotorCmd )
                                        {
-                                               if ( changed_burst || send_diseqc )
+                                               if ( send_mask )
                                                {
                                                        // override first voltage change
-                                                       *(++(++sec_sequence.begin()))=eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13);
+                                                       *(++(++sec_sequence.begin()))=eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13));
                                                        // wait 1 second after first switch diseqc command
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 1000) );
                                                }
@@ -514,7 +538,7 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                        compare.voltage = voltage;
                                                        compare.steps = +3;
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::IF_VOLTAGE_GOTO, compare) ); // voltage already correct ?
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) );
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
                                                }
 
@@ -542,10 +566,10 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::IF_IDLE_INPUTPOWER_AVAIL_GOTO, +8) ); // already measured?
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec after voltage change
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 0) );
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage18) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 100) );  // wait 100msec before measure
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_IDLE_INPUTPOWER, 1) );
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage13) ); // back to lower voltage
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(13)) ); // back to lower voltage
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
 ////////////////////////////
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
@@ -560,12 +584,11 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                        cmd.steps=+3;
                                                        cmd.okcount=0;
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::IF_INPUTPOWER_DELTA_GOTO, cmd ) );  // check if rotor has started
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +10 ) );  // timeout .. we assume now the rotor is already at the correct position
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +9 ) );  // timeout .. we assume now the rotor is already at the correct position
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // goto loop start
 ////////////////////
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, 2400) );  // 2 minutes running timeout
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, iDVBFrontend::voltage18) );
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
 // rotor running loop
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, 50) );  // wait 50msec
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::MEASURE_RUNNING_INPUTPOWER) );
@@ -576,6 +599,7 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );  // running loop start
 /////////////////////
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
                                                        if ( linked )
                                                        {
                                                                frontend.setData(5, RotorCmd);
@@ -593,7 +617,11 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                }
                        }
                        else
-                               frontend.setData(0, band); // store band as csw .. needed for linked tuner handling
+                               csw = band;
+
+                       frontend.setData(0, csw);
+                       frontend.setData(1, ucsw);
+                       frontend.setData(2, di_param.m_toneburst_param);
 
                        if ( linked )
                                return 0;