allow more complicated sat configuration
[enigma2.git] / lib / dvb / sec.cpp
index 9e76bb907606b715a75f17a6731764e14a6edf8c..b1f421c06a0e01ad3c62551652d2a8a466f198f7 100644 (file)
@@ -114,9 +114,12 @@ eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBR
        setRotorPosNum(1); // stored pos 1
 }
 
-int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite &sat, iDVBFrontend *fe, int slot_id )
+int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite &sat, iDVBFrontend *fe, int slot_id, int *highest_score_lnb)
 {
-       int ret=0, satcount=0;
+       int score=0, satcount=0;
+
+       if (highest_score_lnb)
+               *highest_score_lnb = -1;
 
        for (int idx=0; idx <= m_lnbidx; ++idx )
        {
@@ -124,6 +127,7 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
                if ( lnb_param.slot_mask & slot_id ) // lnb for correct tuner?
                {
+                       int ret = 0;
                        eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
 
                        satcount += lnb_param.m_satellites.size();
@@ -261,14 +265,20 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                                        if (tuner_freq < 900000 || tuner_freq > 2200000)
                                                ret=0;
                                }
+                               if (ret > score)
+                               {
+                                       score = ret;
+                                       if (highest_score_lnb)
+                                               *highest_score_lnb = idx;
+                               }
                        }
                }
        }
-       if (ret && satcount)
-               ret -= (satcount-1);
-       if (ret && m_not_linked_slot_mask & slot_id)
-               ret += 5; // increase score for tuners with direct sat connection
-       return ret;
+       if (score && satcount)
+               score -= (satcount-1);
+       if (score && m_not_linked_slot_mask & slot_id)
+               score += 5; // increase score for tuners with direct sat connection
+       return score;
 }
 
 bool need_turn_fast(int turn_speed)
@@ -299,11 +309,10 @@ bool need_turn_fast(int turn_speed)
 
 RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPARAMETERS &parm, const eDVBFrontendParametersSatellite &sat, int slot_id, unsigned int tunetimeout)
 {
-       for (int idx=0; idx <= m_lnbidx; ++idx )
+       int lnb_idx = -1;
+       if (canTune(sat, &frontend, slot_id, &lnb_idx))
        {
-               eDVBSatelliteLNBParameters &lnb_param = m_lnbs[idx];
-               if (!(lnb_param.slot_mask & slot_id)) // lnb for correct tuner?
-                       continue;
+               eDVBSatelliteLNBParameters &lnb_param = m_lnbs[lnb_idx];
                eDVBSatelliteDiseqcParameters &di_param = lnb_param.m_diseqc_parameters;
                eDVBSatelliteRotorParameters &rotor_param = lnb_param.m_rotor_parameters;
 
@@ -315,6 +324,7 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                        bool doSetFrontend = true;
                        bool doSetVoltageToneFrontend = true;
                        bool allowDiseqc1_2 = true;
+                       bool sendDiSEqC = false;
                        long band=0,
                                voltage = iDVBFrontend::voltageOff,
                                tone = iDVBFrontend::toneOff,
@@ -556,6 +566,7 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, vlt) );
                                                sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_ENABLE_VOLTAGE_BEFORE_SWITCH_CMDS]) );
 
+                                               sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_SWITCHPARMS) );
                                                for (int seq_repeat = 0; seq_repeat < (di_param.m_seq_repeat?2:1); ++seq_repeat)
                                                {
                                                        if ( send_mask & 4 )
@@ -632,6 +643,7 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                                sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_TONEBURST]) );
                                                        }
                                                }
+                                               sendDiSEqC = true;
                                        }
 
                                        eDebug("RotorCmd %02x, lastRotorCmd %02lx", RotorCmd, lastRotorCmd);
@@ -714,7 +726,6 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                        compare.val = 0;
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::IF_MEASURE_IDLE_WAS_NOT_OK_GOTO, compare) );
 ////////////////////////////
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_DISEQC_RETRYS, m_params[MOTOR_COMMAND_RETRIES]) );  // 2 retries
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
@@ -729,9 +740,10 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                        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, +2 ) );  // 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::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO, turn_fast ? 9 : 8 ) );  // timeout .. we assume now the rotor is already at the correct position  
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::IF_NO_MORE_ROTOR_DISEQC_RETRYS_GOTO, turn_fast ? 10 : 9 ) );  // timeout .. we assume now the rotor is already at the correct position 
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -8) );  // goto loop start
 ////////////////////
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_MOVING) );
                                                        if (turn_fast)
                                                                sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, VOLTAGE(18)) );
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SET_TIMEOUT, m_params[MOTOR_RUNNING_TIMEOUT]*20) );  // 2 minutes running timeout
@@ -745,7 +757,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) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_STOPPED) );
                                                }
                                                else
                                                {  // use normal turning mode
@@ -759,8 +771,8 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SET_VOLTAGE, compare.voltage) );
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_VOLTAGE_CHANGE_BEFORE_MOTOR_CMD]) );  // wait 150msec after voltage change
 
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_ROTORPARMS) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_MOVING) );
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::SEND_DISEQC, diseqc) );
 
                                                        compare.voltage = voltage;
@@ -785,19 +797,29 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::IF_TIMEOUT_GOTO, +3 ) ); 
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -3) );  // goto loop start
                                                        sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_ROTORPARAMS) );
-                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_ROTOR_STOPPED) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, +3) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
+                                                       sec_sequence.push_back( eSecCommand(eSecCommand::GOTO, -4) );
                                                }
                                                sec_fe->setData(eDVBFrontend::NEW_ROTOR_CMD, RotorCmd);
                                                sec_fe->setData(eDVBFrontend::NEW_ROTOR_POS, sat.orbital_position);
+                                               sendDiSEqC = true;
                                        }
                                }
                        }
                        else
+                       {
+                               sec_sequence.push_back( eSecCommand(eSecCommand::INVALIDATE_CURRENT_SWITCHPARMS) );
                                csw = band;
+                       }
+
+                       if (sendDiSEqC)
+                               sec_sequence.push_front( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeStatic) );
 
-                       sec_fe->setData(eDVBFrontend::CSW, csw);
-                       sec_fe->setData(eDVBFrontend::UCSW, ucsw);
-                       sec_fe->setData(eDVBFrontend::TONEBURST, di_param.m_toneburst_param);
+                       sec_fe->setData(eDVBFrontend::NEW_CSW, csw);
+                       sec_fe->setData(eDVBFrontend::NEW_UCSW, ucsw);
+                       sec_fe->setData(eDVBFrontend::NEW_TONEBURST, di_param.m_toneburst_param);
 
                        if (doSetVoltageToneFrontend)
                        {
@@ -813,16 +835,24 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                sec_sequence.push_back( eSecCommand(eSecCommand::SLEEP, m_params[DELAY_AFTER_CONT_TONE]) );
                        }
 
+                       sec_sequence.push_back( eSecCommand(eSecCommand::UPDATE_CURRENT_SWITCHPARMS) );
+
                        if (doSetFrontend)
                        {
                                sec_sequence.push_back( eSecCommand(eSecCommand::START_TUNE_TIMEOUT, tunetimeout) );
                                sec_sequence.push_back( eSecCommand(eSecCommand::SET_FRONTEND) );
                        }
+
+                       if (sendDiSEqC)
+                               sec_sequence.push_back( eSecCommand(eSecCommand::SET_POWER_LIMITING_MODE, eSecCommand::modeDynamic) );
+
                        frontend.setSecSequence(sec_sequence);
 
                        return 0;
                }
        }
+       else
+               eFatal("canTune failed in sec prepare!!! this should never ever happen!");
 
        eDebug("found no useable satellite configuration for orbital position (%d)", sat.orbital_position );
        return -1;