From 6bcb46dc2ab8ff072d1a5be2edd0c44f0e23ee2f Mon Sep 17 00:00:00 2001 From: benlau Date: Sun, 15 Dec 2024 22:06:03 +0800 Subject: [PATCH] =?UTF-8?q?APN=E8=87=AA=E6=84=88=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: benlau --- .../apn_manager/connection_retry_policy.h | 11 ++- .../apn_manager/connection_retry_policy.cpp | 72 ++++++++++++++++--- services/src/cellular_data_handler.cpp | 1 + .../include/telephony_ext_wrapper.h | 12 +++- .../src/telephony_ext_wrapper.cpp | 32 ++++++--- test/apn_manager_test.cpp | 43 +++++++++++ 6 files changed, 147 insertions(+), 24 deletions(-) diff --git a/services/include/apn_manager/connection_retry_policy.h b/services/include/apn_manager/connection_retry_policy.h index 23eb370a..e5b56a89 100644 --- a/services/include/apn_manager/connection_retry_policy.h +++ b/services/include/apn_manager/connection_retry_policy.h @@ -25,6 +25,11 @@ namespace OHOS { namespace Telephony { +namespace { + constexpr int32_t DEFAULT_DELAY_FOR_SETUP_FAIL = 3000; + constexpr int32_t DEFAULT_DELAY_FOR_MODEM_DEND = 1000; +} + class ConnectionRetryPolicy { public: ConnectionRetryPolicy(); @@ -40,13 +45,17 @@ public: static void OnPropChanged(const char *key, const char *value, void *context); static DisConnectionReason ConvertPdpErrorToDisconnReason(int32_t reason); bool IsAllBadApn() const; + static void RestartRadioIfRequired(int32_t failCause, int32_t slotId); private: int64_t GetRandomDelay(); + static bool ConvertStrToInt(const std::string& str, int32_t& value); private: - inline static bool isPropOn_ = false; + static inline bool isPropOn_ = false; std::vector> matchedApns_; + static inline int32_t defaultSetupFailDelay_ = DEFAULT_DELAY_FOR_SETUP_FAIL; + static inline int32_t defaultModemDendDelay_ = DEFAULT_DELAY_FOR_MODEM_DEND; mutable int32_t tryCount_ = 0; int32_t maxCount_ = 5; mutable int32_t currentApnIndex_ = 0; diff --git a/services/src/apn_manager/connection_retry_policy.cpp b/services/src/apn_manager/connection_retry_policy.cpp index 7c6e3435..ee7bab92 100644 --- a/services/src/apn_manager/connection_retry_policy.cpp +++ b/services/src/apn_manager/connection_retry_policy.cpp @@ -15,6 +15,7 @@ #include "connection_retry_policy.h" +#include #include #include "parameter.h" @@ -25,12 +26,14 @@ namespace OHOS { namespace Telephony { static const char* PROP_RETRY_STRATEGY_ALLOW = "persist.telephony.retrystrategy.allow"; +static const char* PROP_SETUP_FAIL_DELAY = "persist.telephony.setupfail.delay"; +static const char* PROP_MODEM_DEND_DELAY = "persist.telephony.modemdend.delay"; +static const char* DEFAULT_DELAY = "3000"; static const char* DEFAULT_RETRY_STRATEGY_ALLOW = "false"; +static constexpr int16_t BASE_10 = 10; static constexpr int32_t SYSPARA_SIZE = 8; static constexpr int64_t DEFAULT_DELAY_FOR_INTERNAL_DEFAULT_APN_L = 60 * 1000; static constexpr int64_t DEFAULT_DELAY_FOR_INTERNAL_DEFAULT_APN_S = 5 * 1000; -static constexpr int64_t DEFAULT_DELAY_DATA_SETUP_FAIL = 3000; -static constexpr int64_t DEFAULT_DELAY_MODEM_DEACTIVATE_FAIL = 1000; static constexpr int64_t DEFAULT_DELAY_FOR_OTHER_APN = 2 * 1000; static constexpr int32_t MIN_RANDOM_DELAY = 0; static constexpr int32_t MAX_RANDOM_DELAY = 2000; @@ -40,7 +43,19 @@ ConnectionRetryPolicy::ConnectionRetryPolicy() char retryStrategyAllow[SYSPARA_SIZE] = { 0 }; GetParameter(PROP_RETRY_STRATEGY_ALLOW, DEFAULT_RETRY_STRATEGY_ALLOW, retryStrategyAllow, SYSPARA_SIZE); isPropOn_ = (strcmp(retryStrategyAllow, "true") == 0); + char setupFailDelay[SYSPARA_SIZE] = { 0 }; + GetParameter(PROP_SETUP_FAIL_DELAY, DEFAULT_DELAY, setupFailDelay, SYSPARA_SIZE); + if (!ConvertStrToInt(setupFailDelay, defaultSetupFailDelay_)) { + TELEPHONY_LOGE("setupFailDelay is invalid: %{public}s", setupFailDelay); + } + char modemDendDelay[SYSPARA_SIZE] = { 0 }; + GetParameter(PROP_MODEM_DEND_DELAY, DEFAULT_DELAY, modemDendDelay, SYSPARA_SIZE); + if (!ConvertStrToInt(modemDendDelay, defaultModemDendDelay_)) { + TELEPHONY_LOGE("modemDendDelay is invalid: %{public}s", modemDendDelay); + } WatchParameter(PROP_RETRY_STRATEGY_ALLOW, OnPropChanged, this); + WatchParameter(PROP_SETUP_FAIL_DELAY, OnPropChanged, this); + WatchParameter(PROP_MODEM_DEND_DELAY, OnPropChanged, this); } sptr ConnectionRetryPolicy::GetNextRetryApnItem() const @@ -89,13 +104,17 @@ int64_t ConnectionRetryPolicy::GetNextRetryDelay(std::string apnType, int32_t ca DEFAULT_DELAY_FOR_INTERNAL_DEFAULT_APN_S; } else if (apnType == DATA_CONTEXT_ROLE_DEFAULT) { if (scene == RetryScene::RETRY_SCENE_MODEM_DEACTIVATE) { - retryDelay += DEFAULT_DELAY_MODEM_DEACTIVATE_FAIL; + retryDelay += defaultModemDendDelay_; } else { - retryDelay += DEFAULT_DELAY_DATA_SETUP_FAIL; + retryDelay += defaultSetupFailDelay_; } #ifdef OHOS_BUILD_ENABLE_TELEPHONY_EXT - if (isPropOn_ && TELEPHONY_EXT_WRAPPER.dataEndRetryStrategy_) { - TELEPHONY_EXT_WRAPPER.dataEndRetryStrategy_(retryDelay, cause, suggestTime, maxCount_); + int64_t updatedDelay = 0; + if (isPropOn_ && TELEPHONY_EXT_WRAPPER.handleDendFailcause_) { + updatedDelay = TELEPHONY_EXT_WRAPPER.handleDendFailcause_(cause, suggestTime); + } + if (updatedDelay > 0) { + retryDelay = updatedDelay; } #endif } else { @@ -118,17 +137,33 @@ std::vector> ConnectionRetryPolicy::GetMatchedApns() const void ConnectionRetryPolicy::OnPropChanged(const char *key, const char *value, void *context) { - if ((key == nullptr) || (value == nullptr) || (strcmp(key, PROP_RETRY_STRATEGY_ALLOW) != 0)) { + if ((key == nullptr) || (value == nullptr)) { return; } - char retryStrategyAllow[SYSPARA_SIZE] = { 0 }; - GetParameter(PROP_RETRY_STRATEGY_ALLOW, DEFAULT_RETRY_STRATEGY_ALLOW, retryStrategyAllow, SYSPARA_SIZE); - isPropOn_ = (strcmp(retryStrategyAllow, "true") == 0); - TELEPHONY_LOGI("prop changes to %{public}s", retryStrategyAllow); + if (strcmp(key, PROP_RETRY_STRATEGY_ALLOW) == 0) { + isPropOn_ = (strcmp(value, "true") == 0); + } else if ((strcmp(key, PROP_SETUP_FAIL_DELAY) == 0)) { + if (!ConvertStrToInt(value, defaultSetupFailDelay_)) { + TELEPHONY_LOGE("invalid value: %{public}s", value); + } + } else if ((strcmp(key, PROP_MODEM_DEND_DELAY) == 0)) { + if (!ConvertStrToInt(value, defaultModemDendDelay_)) { + TELEPHONY_LOGE("invalid value: %{public}s", value); + } + } else { + TELEPHONY_LOGI("invalid key: %{public}s", key); + } + TELEPHONY_LOGI("prop change: allow=%{public}d, delay=%{public}d,%{public}d", isPropOn_, defaultSetupFailDelay_, + defaultModemDendDelay_); } DisConnectionReason ConnectionRetryPolicy::ConvertPdpErrorToDisconnReason(int32_t reason) { +#ifdef OHOS_BUILD_ENABLE_TELEPHONY_EXT + if (isPropOn_ && TELEPHONY_EXT_WRAPPER.convertPdpError_) { + reason = TELEPHONY_EXT_WRAPPER.convertPdpError_(reason); + } +#endif switch (reason) { case PdpErrorReason::PDP_ERR_TO_NORMAL: return DisConnectionReason::REASON_NORMAL; @@ -173,5 +208,20 @@ int64_t ConnectionRetryPolicy::GetRandomDelay() std::uniform_int_distribution<> dis(MIN_RANDOM_DELAY, MAX_RANDOM_DELAY); return dis(gen); } + +bool ConnectionRetryPolicy::ConvertStrToInt(const std::string& str, int32_t& value) +{ + auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value, BASE_10); + return ec == std::errc{} && ptr == str.data() + str.size(); +} + +void ConnectionRetryPolicy::RestartRadioIfRequired(int32_t failCause, int32_t slotId) +{ +#ifdef OHOS_BUILD_ENABLE_TELEPHONY_EXT + if (isPropOn_ && TELEPHONY_EXT_WRAPPER.restartRadioIfRequired_) { + TELEPHONY_EXT_WRAPPER.restartRadioIfRequired_(failCause, slotId); + } +#endif +} } // namespace Telephony } // namespace OHOS \ No newline at end of file diff --git a/services/src/cellular_data_handler.cpp b/services/src/cellular_data_handler.cpp index 50c2a012..629e98cc 100644 --- a/services/src/cellular_data_handler.cpp +++ b/services/src/cellular_data_handler.cpp @@ -996,6 +996,7 @@ void CellularDataHandler::RetryOrClearConnection(const sptr &apnHolde if (apnHolder == nullptr || netInfo == nullptr || apnManager_ == nullptr) { return; } + ConnectionRetryPolicy::RestartRadioIfRequired(netInfo->reason, slotId_); if (reason == DisConnectionReason::REASON_CLEAR_CONNECTION) { TELEPHONY_LOGI("clear connection"); ClearConnection(apnHolder, reason); diff --git a/services/telephony_ext_wrapper/include/telephony_ext_wrapper.h b/services/telephony_ext_wrapper/include/telephony_ext_wrapper.h index c2ef730e..9dead4b0 100644 --- a/services/telephony_ext_wrapper/include/telephony_ext_wrapper.h +++ b/services/telephony_ext_wrapper/include/telephony_ext_wrapper.h @@ -45,7 +45,9 @@ public: typedef bool (*IS_ALL_CELLULAR_DATA_ALLOWED)( const NetRequest &, const HasSystemUse hasSystemUse); typedef bool (*IS_DUAL_CELLULAR_CARD_ALLOWED)(); - typedef void (*DATA_END_RETRY_STRATEGY)(int64_t&, int32_t&, int64_t&, int32_t&); + typedef int64_t (*HANDLE_DEND_FAILCAUSE)(int32_t, int64_t); + typedef int32_t (*CONVERT_PDP_ERROR)(int32_t); + typedef void (*RESTART_RADIO_IF_RQUIRED)(int32_t, int32_t); typedef bool (*GET_USER_DATA_ROAMING_EXPEND)(int32_t, bool); DATA_EDN_SELF_CURE dataEndSelfCure_ = nullptr; IS_APN_ALLOWED_ACTIVE isApnAllowedActive_ = nullptr; @@ -56,8 +58,10 @@ public: SEND_DATA_SWITCH_CHANGE_INFO sendDataSwitchChangeInfo_ = nullptr; IS_ALL_CELLULAR_DATA_ALLOWED isAllCellularDataAllowed_ = nullptr; IS_DUAL_CELLULAR_CARD_ALLOWED isDualCellularCardAllowed_ = nullptr; - DATA_END_RETRY_STRATEGY dataEndRetryStrategy_ = nullptr; GET_USER_DATA_ROAMING_EXPEND getUserDataRoamingExpend_; + HANDLE_DEND_FAILCAUSE handleDendFailcause_ = nullptr; + CONVERT_PDP_ERROR convertPdpError_ = nullptr; + RESTART_RADIO_IF_RQUIRED restartRadioIfRequired_ = nullptr; private: void* telephonyExtWrapperHandle_ = nullptr; void* telephonyVSimWrapperHandle_ = nullptr; @@ -69,7 +73,9 @@ private: void InitSendDataSwitchChangeInfo(); void InitIsAllCellularDataAllowed(); void InitIsDualCellularCardAllowed(); - void InitDataEndRetryStrategy(); + void InitHandleDendFailcause(); + void InitConvertPdpError(); + void InitRestartRadioIfRequired(); }; #define TELEPHONY_EXT_WRAPPER ::OHOS::DelayedRefSingleton::GetInstance() diff --git a/services/telephony_ext_wrapper/src/telephony_ext_wrapper.cpp b/services/telephony_ext_wrapper/src/telephony_ext_wrapper.cpp index 5ddde8ee..463a5d10 100644 --- a/services/telephony_ext_wrapper/src/telephony_ext_wrapper.cpp +++ b/services/telephony_ext_wrapper/src/telephony_ext_wrapper.cpp @@ -58,7 +58,9 @@ void TelephonyExtWrapper::InitTelephonyExtWrapperForCellularData() InitSendDataSwitchChangeInfo(); InitIsAllCellularDataAllowed(); InitIsDualCellularCardAllowed(); - InitDataEndRetryStrategy(); + InitHandleDendFailcause(); + InitConvertPdpError(); + InitRestartRadioIfRequired(); } void TelephonyExtWrapper::InitDataEndSelfCure() @@ -139,16 +141,28 @@ void TelephonyExtWrapper::InitIsDualCellularCardAllowed() TELEPHONY_LOGD("telephony ext wrapper init IsDualCellularCardAllowed success"); } -void TelephonyExtWrapper::InitDataEndRetryStrategy() +void TelephonyExtWrapper::InitHandleDendFailcause() { - dataEndRetryStrategy_ = - (DATA_END_RETRY_STRATEGY)dlsym(telephonyExtWrapperHandle_, "DataEndRetryStrategy"); - if (dataEndRetryStrategy_ == nullptr) { - TELEPHONY_LOGE("telephony ext wrapper symbol DataEndRetryStrategy failed,\ - error: %{public}s", dlerror()); - return; + handleDendFailcause_ = (HANDLE_DEND_FAILCAUSE)dlsym(telephonyExtWrapperHandle_, "HandleDendFailcause"); + if (handleDendFailcause_ == nullptr) { + TELEPHONY_LOGE("telephony ext wrapper symbol HandleDendFailcause failed, error: %{public}s", dlerror()); + } +} + +void TelephonyExtWrapper::InitConvertPdpError() +{ + convertPdpError_ = (CONVERT_PDP_ERROR)dlsym(telephonyExtWrapperHandle_, "ConvertPdpError"); + if (convertPdpError_ == nullptr) { + TELEPHONY_LOGE("telephony ext wrapper symbol ConvertPdpError failed, error: %{public}s", dlerror()); + } +} + +void TelephonyExtWrapper::InitRestartRadioIfRequired() +{ + restartRadioIfRequired_ = (RESTART_RADIO_IF_RQUIRED)dlsym(telephonyExtWrapperHandle_, "RestartRadioIfRequired"); + if (restartRadioIfRequired_ == nullptr) { + TELEPHONY_LOGE("telephony ext wrapper symbol RestartRadioIfRequired failed, error: %{public}s", dlerror()); } - TELEPHONY_LOGD("telephony ext wrapper init DataEndRetryStrategy success"); } } // namespace Telephony diff --git a/test/apn_manager_test.cpp b/test/apn_manager_test.cpp index 54efe390..f5cabf24 100644 --- a/test/apn_manager_test.cpp +++ b/test/apn_manager_test.cpp @@ -828,6 +828,14 @@ HWTEST_F(ApnManagerTest, OnPropChanged_001, TestSize.Level0) EXPECT_EQ(connectionRetryPolicy->isPropOn_, true); connectionRetryPolicy->OnPropChanged("fakeKey", "true", nullptr); EXPECT_EQ(connectionRetryPolicy->isPropOn_, true); + connectionRetryPolicy->OnPropChanged("persist.telephony.setupfail.delay", "3000", nullptr); + EXPECT_EQ(connectionRetryPolicy->defaultSetupFailDelay_, 3000); + connectionRetryPolicy->OnPropChanged("persist.telephony.setupfail.delay", "ABC", nullptr); + EXPECT_EQ(connectionRetryPolicy->defaultSetupFailDelay_, 3000); + connectionRetryPolicy->OnPropChanged("persist.telephony.modemdend.delay", "1000", nullptr); + EXPECT_EQ(connectionRetryPolicy->defaultModemDendDelay_, 1000); + connectionRetryPolicy->OnPropChanged("persist.telephony.modemdend.delay", "ABNC", nullptr); + EXPECT_EQ(connectionRetryPolicy->defaultModemDendDelay_, 1000); } /** @@ -998,6 +1006,41 @@ HWTEST_F(ApnManagerTest, ConvertPdpErrorToDisconnReason_002, TestSize.Level0) EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT); } +/** +@tc.number ConvertPdpErrorToDisconnReason_003 +@tc.name test function branch +@tc.desc Function test +*/ +HWTEST_F(ApnManagerTest, ConvertPdpErrorToDisconnReason_003, TestSize.Level0) +{ + std::shared_ptr connectionRetryPolicy = std::make_shared(); + connectionRetryPolicy->isPropOn_ = false; + auto res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason( + PdpErrorReason::PDP_ERR_TO_PERMANENT_REJECT); + EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT); + connectionRetryPolicy->isPropOn_ = true; + res = connectionRetryPolicy->ConvertPdpErrorToDisconnReason( + PdpErrorReason::PDP_ERR_TO_PERMANENT_REJECT); + EXPECT_EQ(res, DisConnectionReason::REASON_PERMANENT_REJECT); +} + +/** + +@tc.number RestartRadioIfRequired_001 +@tc.name test function branch +@tc.desc Function test +*/ +HWTEST_F(ApnManagerTest, RestartRadioIfRequired_003, TestSize.Level0) +{ + std::shared_ptr connectionRetryPolicy = std::make_shared(); + connectionRetryPolicy->isPropOn_ = true; + int32_t failCause = 65536; + connectionRetryPolicy->RestartRadioIfRequired(failCause, 0); + connectionRetryPolicy->isPropOn_ = false; + connectionRetryPolicy->RestartRadioIfRequired(failCause, 0); + EXPECT_FALSE(connectionRetryPolicy->isPropOn_); +} + /** * @tc.number InitialRetryCountValue_001 * @tc.name test function branch -- Gitee