replace eFatal (which can occour during servicescan at some conditions) with
[enigma2.git] / lib / dvb / sec.cpp
index 8a186014f792f1209e5d128bcb22b962b49c58b2..d56024e142e90bb94680697e65594360c3544455 100644 (file)
@@ -114,43 +114,12 @@ eDVBSatelliteEquipmentControl::eDVBSatelliteEquipmentControl(eSmartPtrList<eDVBR
        setRotorPosNum(1); // stored pos 1
 }
 
-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)
+int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite &sat, iDVBFrontend *fe, int slot_id, int *highest_score_lnb)
 {
-       eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_ptr;
-       if (linked_fe->m_inuse)
-       {
-               long ocsw = -1,
-                       oucsw = -1,
-                       oToneburst = -1;
-               linked_fe->m_frontend->getData(eDVBFrontend::CSW, ocsw);
-               linked_fe->m_frontend->getData(eDVBFrontend::UCSW, oucsw);
-               linked_fe->m_frontend->getData(eDVBFrontend::TONEBURST, oToneburst);
-#if 0
-               eDebug("compare csw %02x == lcsw %02x",
-                       csw, ocsw);
-               if ( diseqc )
-                       eDebug("compare ucsw %02x == lucsw %02x\ncompare toneburst %02x == oToneburst %02x",
-                               ucsw, oucsw, toneburst, oToneburst);
-               if ( rotor )
-                       eDebug("compare pos %d == current pos %d",
-                               sat.orbital_position, oRotorPos);
-#endif
-               if ( (csw != ocsw) ||
-                       ( diseqc && (ucsw != oucsw || toneburst != oToneburst) ) ||
-                       ( rotor && RotorPos != sat.orbital_position ) )
-               {
-//                     eDebug("can not tune this transponder with linked tuner in use!!");
-                       ret=0;
-               }
-//             else
-//                     eDebug("OK .. can tune this transponder with linked tuner in use :)");
-       }
-       linked_fe->m_frontend->getData(direction, (long&)linked_ptr);
-}
+       int score=0, satcount=0;
 
-int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite &sat, iDVBFrontend *fe, int slot_id )
-{
-       int ret=0, satcount=0;
+       if (highest_score_lnb)
+               *highest_score_lnb = -1;
 
        for (int idx=0; idx <= m_lnbidx; ++idx )
        {
@@ -158,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();
@@ -175,9 +145,7 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                                        toneburst = di_param.m_toneburst_param,
                                        curRotorPos;
 
-                               fe->getData(eDVBFrontend::ROTOR_POS, curRotorPos);
                                fe->getData(eDVBFrontend::LINKED_PREV_PTR, linked_prev_ptr);
-                               fe->getData(eDVBFrontend::LINKED_NEXT_PTR, linked_next_ptr);
                                fe->getData(eDVBFrontend::SATPOS_DEPENDS_PTR, satpos_depends_ptr);
 
                                if ( sat.frequency > lnb_param.m_lof_threshold )
@@ -200,8 +168,6 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                                                rotor = true;
 
                                        ret=10000;
-                                       if (rotor && curRotorPos != -1)
-                                               ret -= abs(curRotorPos-sat.orbital_position);
                                }
                                else
                                {
@@ -209,11 +175,76 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                                        ret = 15000;
                                }
 
-                               while (ret && linked_prev_ptr != -1)  // check for linked tuners..
-                                       checkLinkedParams(eDVBFrontend::LINKED_PREV_PTR, linked_prev_ptr, ret, sat, csw, ucsw, toneburst, diseqc, rotor, curRotorPos);
+                               if (m_not_linked_slot_mask & slot_id)  // frontend with direct connection?
+                               {
+                                       long ocsw = -1,
+                                               oucsw = -1,
+                                               oToneburst = -1;
+                                       fe->getData(eDVBFrontend::ROTOR_POS, curRotorPos);
+                                       fe->getData(eDVBFrontend::LINKED_NEXT_PTR, linked_next_ptr);
+                                       fe->getData(eDVBFrontend::CSW, ocsw);
+                                       fe->getData(eDVBFrontend::UCSW, oucsw);
+                                       fe->getData(eDVBFrontend::TONEBURST, oToneburst);
+                                       while (ret && linked_prev_ptr != -1)  // check for linked tuners..
+                                       {
+                                               eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_prev_ptr;
+                                               if (linked_fe->m_inuse)
+                                               {
+                                                       if ( (csw != ocsw) ||
+                                                               ( diseqc && (ucsw != oucsw || toneburst != oToneburst) ) ||
+                                                               ( rotor && curRotorPos != sat.orbital_position ) )
+                                                       {
+                                                               ret=0;
+                                                       }
+                                               }
+                                               linked_fe->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, (long&)linked_prev_ptr);
+                                       }
+                                       while (ret && linked_next_ptr != -1)  // check for linked tuners..
+                                       {
+                                               eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_next_ptr;
+                                               if (linked_fe->m_inuse)
+                                               {
+                                                       if ( (csw != ocsw) ||
+                                                               ( diseqc && (ucsw != oucsw || toneburst != oToneburst) ) ||
+                                                               ( rotor && curRotorPos != sat.orbital_position ) )
+                                                       {
+                                                               ret=0;
+                                                       }
+                                               }
+                                               linked_fe->m_frontend->getData(eDVBFrontend::LINKED_NEXT_PTR, (long&)linked_next_ptr);
+                                       }
+                               }
+                               else // linked frontend..
+                               {
+                                       long ocsw = -1,
+                                               oucsw = -1,
+                                               oToneburst = -1;
+                                       while (linked_prev_ptr != -1)
+                                       {
+                                               eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_prev_ptr;
+                                               linked_fe->m_frontend->getData(eDVBFrontend::LINKED_PREV_PTR, (long&)linked_prev_ptr);
+                                               if (linked_prev_ptr == -1)
+                                               {
+                                                       iDVBFrontend *sec_fe = linked_fe->m_frontend;
+                                                       sec_fe->getData(eDVBFrontend::ROTOR_POS, curRotorPos);
+                                                       if (linked_fe->m_inuse)
+                                                       {
+                                                               sec_fe->getData(eDVBFrontend::CSW, ocsw);
+                                                               sec_fe->getData(eDVBFrontend::UCSW, oucsw);
+                                                               sec_fe->getData(eDVBFrontend::TONEBURST, oToneburst);
+                                                               if ( (csw != ocsw) ||
+                                                                       ( diseqc && (ucsw != oucsw || toneburst != oToneburst) ) ||
+                                                                       ( rotor && curRotorPos != sat.orbital_position ) )
+                                                               {
+                                                                       ret=0;
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
 
-                               while (ret && linked_next_ptr != -1)  // check for linked tuners..
-                                       checkLinkedParams(eDVBFrontend::LINKED_NEXT_PTR, linked_next_ptr, ret, sat, csw, ucsw, toneburst, diseqc, rotor, curRotorPos);
+                               if (ret && rotor && curRotorPos != -1)
+                                       ret -= abs(curRotorPos-sat.orbital_position);
 
                                if (ret)
                                        if (satpos_depends_ptr != -1)
@@ -222,13 +253,8 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                                                if ( satpos_depends_to_fe->m_inuse )
                                                {
                                                        if (!rotor || curRotorPos != sat.orbital_position)
-                                                       {
-//                                                             eDebug("can not tune this transponder ... rotor on other tuner is positioned to %d", oRotorPos);
                                                                ret=0;
-                                                       }
                                                }
-//                                             else
-//                                                     eDebug("OK .. can tune this transponder satpos is correct :)");
                                        }
 
                                if (ret)
@@ -236,22 +262,23 @@ int eDVBSatelliteEquipmentControl::canTune(const eDVBFrontendParametersSatellite
                                        int lof = sat.frequency > lnb_param.m_lof_threshold ?
                                                lnb_param.m_lof_hi : lnb_param.m_lof_lo;
                                        int tuner_freq = abs(sat.frequency - lof);
-//                                     eDebug("tuner freq %d", tuner_freq);
                                        if (tuner_freq < 900000 || tuner_freq > 2200000)
-                                       {
                                                ret=0;
-//                                             eDebug("Transponder not tuneable with this lnb... %d Khz out of tuner range",
-//                                                     tuner_freq);
-                                       }
+                               }
+                               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)
@@ -282,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;
 
@@ -298,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,
@@ -310,16 +337,29 @@ RESULT eDVBSatelliteEquipmentControl::prepare(iDVBFrontend &frontend, FRONTENDPA
                                lastRotorCmd = -1,
                                curRotorPos = -1,
                                satposDependPtr = -1;
+                       iDVBFrontend *sec_fe=&frontend;
 
-                       frontend.getData(eDVBFrontend::CSW, lastcsw);
-                       frontend.getData(eDVBFrontend::UCSW, lastucsw);
-                       frontend.getData(eDVBFrontend::TONEBURST, lastToneburst);
-                       frontend.getData(eDVBFrontend::ROTOR_CMD, lastRotorCmd);
-                       frontend.getData(eDVBFrontend::ROTOR_POS, curRotorPos);
                        frontend.getData(eDVBFrontend::SATPOS_DEPENDS_PTR, satposDependPtr);
 
-                       if (satposDependPtr != -1 && !(m_not_linked_slot_mask & slot_id))
-                               allowDiseqc1_2 = false;
+                       if (!(m_not_linked_slot_mask & slot_id))  // frontend with direct connection?
+                       {
+                               long linked_prev_ptr;
+                               frontend.getData(eDVBFrontend::LINKED_PREV_PTR, linked_prev_ptr);
+                               while (linked_prev_ptr != -1)
+                               {
+                                       eDVBRegisteredFrontend *linked_fe = (eDVBRegisteredFrontend*) linked_prev_ptr;
+                                       sec_fe = linked_fe->m_frontend;
+                                       sec_fe->getData(eDVBFrontend::LINKED_PREV_PTR, (long&)linked_prev_ptr);
+                               }
+                               if (satposDependPtr != -1)
+                                       allowDiseqc1_2 = false;
+                       }
+
+                       sec_fe->getData(eDVBFrontend::CSW, lastcsw);
+                       sec_fe->getData(eDVBFrontend::UCSW, lastucsw);
+                       sec_fe->getData(eDVBFrontend::TONEBURST, lastToneburst);
+                       sec_fe->getData(eDVBFrontend::ROTOR_CMD, lastRotorCmd);
+                       sec_fe->getData(eDVBFrontend::ROTOR_POS, curRotorPos);
 
                        if ( sat.frequency > lnb_param.m_lof_threshold )
                                band |= 1;
@@ -526,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 )
@@ -602,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);
@@ -684,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) );
@@ -699,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
@@ -715,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
@@ -729,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;
@@ -755,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) );
                                                }
-                                               frontend.setData(eDVBFrontend::NEW_ROTOR_CMD, RotorCmd);
-                                               frontend.setData(eDVBFrontend::NEW_ROTOR_POS, sat.orbital_position);
+                                               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) );
 
-                       frontend.setData(eDVBFrontend::CSW, csw);
-                       frontend.setData(eDVBFrontend::UCSW, ucsw);
-                       frontend.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)
                        {
@@ -783,18 +835,32 @@ 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;
                }
        }
-
-       eDebug("found no useable satellite configuration for orbital position (%d)", sat.orbital_position );
+       eDebug("found no useable satellite configuration for %s freq %d%s %s on orbital position (%d)",
+               sat.system ? "DVB-S2" : "DVB-S",
+               sat.frequency,
+               sat.polarisation == eDVBFrontendParametersSatellite::Polarisation::Horizontal ? "H" :
+                       eDVBFrontendParametersSatellite::Polarisation::Vertical ? "V" :
+                       eDVBFrontendParametersSatellite::Polarisation::CircularLeft ? "CL" : "CR",
+               sat.modulation == eDVBFrontendParametersSatellite::Modulation::Auto ? "AUTO" :
+                       eDVBFrontendParametersSatellite::Modulation::QPSK ? "QPSK" :
+                       eDVBFrontendParametersSatellite::Modulation::M8PSK ? "8PSK" : "QAM16",
+               sat.orbital_position );
        return -1;
 }