diff --git a/common/include/constants/distributed_device_profile_constants.h b/common/include/constants/distributed_device_profile_constants.h index c33cb2ffa9b5d0cebf5ba30bba9a950a6d26e63c..e0c6ceacbb747a8978459b8d7e4f153927be6523 100644 --- a/common/include/constants/distributed_device_profile_constants.h +++ b/common/include/constants/distributed_device_profile_constants.h @@ -246,6 +246,11 @@ constexpr uint32_t MAX_TRUSTED_DEVICE_SIZE = 1000; constexpr int32_t MAX_SESSIONKEY_SIZE = 8*1024; constexpr int32_t COUNTER_INIT = 0; constexpr int32_t COUNTER_MAX = 999999; +constexpr int32_t US_100000 = 100000; +constexpr int32_t US_200000 = 200000; +constexpr int32_t US_300000 = 300000; +constexpr int32_t US_400000 = 400000; +constexpr int32_t US_500000 = 500000; extern const std::string IS_MULTI_USER; extern const std::string SEPARATOR; extern const std::string SLASHES; diff --git a/common/include/constants/distributed_device_profile_errors.h b/common/include/constants/distributed_device_profile_errors.h index 0d0918fe679d6c4622a2650fe0fea1d29bfb33a2..789db20050c62e92f3cbc82524fc717b166fee75 100644 --- a/common/include/constants/distributed_device_profile_errors.h +++ b/common/include/constants/distributed_device_profile_errors.h @@ -213,6 +213,8 @@ constexpr int32_t DP_BUSINESS_EVENT_MANAGER_UNINIT_FAIL = 98566331; constexpr int32_t DP_PUT_BUSINESS_EVENT_FAIL = 98566332; constexpr int32_t DP_GET_BUSINESS_EVENT_FAIL = 98566333; constexpr int32_t DP_NOTIFY_BUSINESS_EVENT_FAIL = 98566334; +constexpr int32_t DP_SERVICE_ON_IDLE = 98566335; +constexpr int32_t DP_SERVICE_STOPPED = 98566336; } // namespace DistributedDeviceProfile } // namespace OHOS #endif // OHOS_DP_DISTRIBUTED_DEVICE_PROFILE_ERRORS_H diff --git a/interfaces/innerkits/core/include/distributed_device_profile_client.h b/interfaces/innerkits/core/include/distributed_device_profile_client.h index f0140a52f1e6c48b23514d06f1e3f8f930ce4b3a..7f27d01f5f010033c51596609f807dead79cb7c2 100644 --- a/interfaces/innerkits/core/include/distributed_device_profile_client.h +++ b/interfaces/innerkits/core/include/distributed_device_profile_client.h @@ -137,6 +137,8 @@ private: void ReRegisterBusinessCallback(); void ReleaseRegisterBusinessCallback(); void StartThreadReRegisterBusinessCallback(); + template + int32_t RetryClientRequest(int32_t firesRet, Method method, Args&& ... args); class DeviceProfileDeathRecipient : public IRemoteObject::DeathRecipient { public: @@ -166,6 +168,8 @@ private: sptr businessCallback_ = nullptr; std::string strSaId_ = ""; std::string businessKey_ = ""; + + const std::set retryErrCodes_ = {DP_SERVICE_STOPPED, DP_LOAD_SERVICE_ERR}; }; } // namespace DeviceProfile } // namespace OHOS diff --git a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp index 781bde7671e1bdf024665542dc5fb679f9c8dbfd..bb6bae509931b7dc8bdf29fcb484e652b9a97622 100644 --- a/interfaces/innerkits/core/src/distributed_device_profile_client.cpp +++ b/interfaces/innerkits/core/src/distributed_device_profile_client.cpp @@ -47,6 +47,7 @@ using namespace std::chrono_literals; namespace { const std::string TAG = "DistributedDeviceProfileClient"; constexpr int32_t LOAD_SA_TIMEOUT_MS = 10000; + constexpr int32_t MAX_RETRY_TIMES = 7; } IMPLEMENT_SINGLE_INSTANCE(DistributedDeviceProfileClient); @@ -134,7 +135,11 @@ int32_t DistributedDeviceProfileClient::PutAccessControlProfile(const AccessCont HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->PutAccessControlProfile(accessControlProfile); + int32_t ret = dpService->PutAccessControlProfile(accessControlProfile); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::PutAccessControlProfile, accessControlProfile); + } + return ret; } int32_t DistributedDeviceProfileClient::UpdateAccessControlProfile(const AccessControlProfile& accessControlProfile) @@ -144,7 +149,11 @@ int32_t DistributedDeviceProfileClient::UpdateAccessControlProfile(const AccessC HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->UpdateAccessControlProfile(accessControlProfile); + int32_t ret = dpService->UpdateAccessControlProfile(accessControlProfile); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::UpdateAccessControlProfile, accessControlProfile); + } + return ret; } int32_t DistributedDeviceProfileClient::GetTrustDeviceProfile(const std::string& deviceId, @@ -155,7 +164,11 @@ int32_t DistributedDeviceProfileClient::GetTrustDeviceProfile(const std::string& HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->GetTrustDeviceProfile(deviceId, trustDeviceProfile); + int32_t ret = dpService->GetTrustDeviceProfile(deviceId, trustDeviceProfile); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetTrustDeviceProfile, deviceId, trustDeviceProfile); + } + return ret; } int32_t DistributedDeviceProfileClient::GetAllTrustDeviceProfile(std::vector& trustDeviceProfiles) @@ -165,7 +178,11 @@ int32_t DistributedDeviceProfileClient::GetAllTrustDeviceProfile(std::vectorGetAllTrustDeviceProfile(trustDeviceProfiles); + int32_t ret = dpService->GetAllTrustDeviceProfile(trustDeviceProfiles); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetAllTrustDeviceProfile, trustDeviceProfiles); + } + return ret; } int32_t DistributedDeviceProfileClient::GetAccessControlProfile(std::map params, @@ -177,10 +194,15 @@ int32_t DistributedDeviceProfileClient::GetAccessControlProfile(std::map MAX_PARAM_SIZE) { - HILOGE("Params size is invalid!size: %{public}zu!", params.size()); + HILOGE("Params size is invalid! size: %{public}zu!", params.size()); return DP_INVALID_PARAMS; } - return dpService->GetAccessControlProfile(params, accessControlProfiles); + int32_t ret = dpService->GetAccessControlProfile(params, accessControlProfiles); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetAccessControlProfile, params, + accessControlProfiles); + } + return ret; } int32_t DistributedDeviceProfileClient::GetAllAccessControlProfile( @@ -191,7 +213,11 @@ int32_t DistributedDeviceProfileClient::GetAllAccessControlProfile( HILOGE("Get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->GetAllAccessControlProfile(accessControlProfiles); + int32_t ret = dpService->GetAllAccessControlProfile(accessControlProfiles); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetAllAccessControlProfile, accessControlProfiles); + } + return ret; } int32_t DistributedDeviceProfileClient::GetAllAclIncludeLnnAcl(std::vector& accessControlProfiles) @@ -201,7 +227,11 @@ int32_t DistributedDeviceProfileClient::GetAllAclIncludeLnnAcl(std::vectorGetAllAclIncludeLnnAcl(accessControlProfiles); + int32_t ret = dpService->GetAllAclIncludeLnnAcl(accessControlProfiles); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetAllAclIncludeLnnAcl, accessControlProfiles); + } + return ret; } int32_t DistributedDeviceProfileClient::DeleteAccessControlProfile(int32_t accessControlId) @@ -211,7 +241,11 @@ int32_t DistributedDeviceProfileClient::DeleteAccessControlProfile(int32_t acces HILOGE("Get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->DeleteAccessControlProfile(accessControlId); + int32_t ret = dpService->DeleteAccessControlProfile(accessControlId); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::DeleteAccessControlProfile, accessControlId); + } + return ret; } int32_t DistributedDeviceProfileClient::PutDeviceProfileBatch(std::vector& deviceProfiles) @@ -813,7 +847,11 @@ int32_t DistributedDeviceProfileClient::PutAllTrustedDevices(const std::vectorPutAllTrustedDevices(deviceInfos); + int32_t ret = dpService->PutAllTrustedDevices(deviceInfos); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::PutAllTrustedDevices, deviceInfos); + } + return ret; } int32_t DistributedDeviceProfileClient::PutSessionKey( @@ -824,7 +862,11 @@ int32_t DistributedDeviceProfileClient::PutSessionKey( HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->PutSessionKey(userId, sessionKey, sessionKeyId); + int32_t ret = dpService->PutSessionKey(userId, sessionKey, sessionKeyId); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::PutSessionKey, userId, sessionKey, sessionKeyId); + } + return ret; } int32_t DistributedDeviceProfileClient::GetSessionKey( @@ -856,7 +898,11 @@ int32_t DistributedDeviceProfileClient::DeleteSessionKey(uint32_t userId, int32_ HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->DeleteSessionKey(userId, sessionKeyId); + int32_t ret = dpService->DeleteSessionKey(userId, sessionKeyId); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::DeleteSessionKey, userId, sessionKeyId); + } + return ret; } int32_t DistributedDeviceProfileClient::PutLocalServiceInfo(const LocalServiceInfo& localServiceInfo) @@ -866,7 +912,11 @@ int32_t DistributedDeviceProfileClient::PutLocalServiceInfo(const LocalServiceIn HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->PutLocalServiceInfo(localServiceInfo); + int32_t ret = dpService->PutLocalServiceInfo(localServiceInfo); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::PutLocalServiceInfo, localServiceInfo); + } + return ret; } int32_t DistributedDeviceProfileClient::UpdateLocalServiceInfo(const LocalServiceInfo& localServiceInfo) @@ -876,7 +926,11 @@ int32_t DistributedDeviceProfileClient::UpdateLocalServiceInfo(const LocalServic HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->UpdateLocalServiceInfo(localServiceInfo); + int32_t ret = dpService->UpdateLocalServiceInfo(localServiceInfo); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::UpdateLocalServiceInfo, localServiceInfo); + } + return ret; } int32_t DistributedDeviceProfileClient::GetLocalServiceInfoByBundleAndPinType(const std::string& bundleName, @@ -887,7 +941,12 @@ int32_t DistributedDeviceProfileClient::GetLocalServiceInfoByBundleAndPinType(co HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->GetLocalServiceInfoByBundleAndPinType(bundleName, pinExchangeType, localServiceInfo); + int32_t ret = dpService->GetLocalServiceInfoByBundleAndPinType(bundleName, pinExchangeType, localServiceInfo); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::GetLocalServiceInfoByBundleAndPinType, bundleName, + pinExchangeType, localServiceInfo); + } + return ret; } int32_t DistributedDeviceProfileClient::DeleteLocalServiceInfo(const std::string& bundleName, @@ -898,7 +957,11 @@ int32_t DistributedDeviceProfileClient::DeleteLocalServiceInfo(const std::string HILOGE("get dp service failed"); return DP_GET_SERVICE_FAILED; } - return dpService->DeleteLocalServiceInfo(bundleName, pinExchangeType); + int32_t ret = dpService->DeleteLocalServiceInfo(bundleName, pinExchangeType); + if (retryErrCodes_.count(ret) != 0) { + ret = RetryClientRequest(ret, &IDistributedDeviceProfile::DeleteLocalServiceInfo, bundleName, pinExchangeType); + } + return ret; } void DistributedDeviceProfileClient::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, @@ -992,6 +1055,28 @@ void DistributedDeviceProfileClient::ReleaseDeathRecipient() dpProxy_ = nullptr; } +template +int32_t DistributedDeviceProfileClient::RetryClientRequest(int32_t firesRet, Method method, Args&& ... args) +{ + int32_t ret = firesRet; + int32_t delays[MAX_RETRY_TIMES] = {US_100000, US_200000, US_300000, US_400000, US_500000, US_500000, US_500000}; + for (int32_t i = 0; i < MAX_RETRY_TIMES; ++i) { + HILOGI("retry times:%{public}d, retry reason:%{public}d", i + 1, ret); + usleep(delays[i]); + auto dpService = GetDeviceProfileService(); + if (dpService == nullptr) { + HILOGE("get dp service failed"); + return DP_GET_SERVICE_FAILED; + } + ret = (dpService->*method)(std::forward(args)...); + if (retryErrCodes_.count(ret) == 0) { + break; + } + } + HILOGI("retry end, reason:%{public}d", ret); + return ret; +} + void DistributedDeviceProfileClient::ReleaseResource() { ReleaseSubscribeDeviceProfileSA(); diff --git a/services/core/include/distributed_device_profile_service_new.h b/services/core/include/distributed_device_profile_service_new.h index 7a3f654408be2299490736182ddcf3bd63e94aff..79a6310b065c4a575e5bc2f3d94d08c0dbbf2fdc 100644 --- a/services/core/include/distributed_device_profile_service_new.h +++ b/services/core/include/distributed_device_profile_service_new.h @@ -50,6 +50,8 @@ public: int32_t PostInit(); int32_t PostInitNext(); int32_t UnInit(); + bool ExitIdleState(); + bool IsStopped(); int32_t PutAccessControlProfile(const AccessControlProfile& aclProfile) override; int32_t UpdateAccessControlProfile(const AccessControlProfile& aclProfile) override; int32_t GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& trustDeviceProfile) override; @@ -122,6 +124,7 @@ public: protected: void OnStart(const SystemAbilityOnDemandReason& startReason) override; void OnStop() override; + void OnActive(const SystemAbilityOnDemandReason& activeReason) override; int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason) override; void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; @@ -146,6 +149,7 @@ private: std::shared_ptr unloadHandler_; std::mutex unloadMutex_; std::atomic isInited_{false}; + std::atomic isStopped_{false}; std::mutex dynamicProfileMapMtx_; std::map dynamicProfileMap_; std::mutex dpInitedCallbackMapMtx_; diff --git a/services/core/src/distributed_device_profile_service_new.cpp b/services/core/src/distributed_device_profile_service_new.cpp index 5a1fd52f09f0b4c053b6ba3f25c0b99bd502bf3b..aa0a8c7c8ec8ad854b0d8637be1310b020f6a76a 100644 --- a/services/core/src/distributed_device_profile_service_new.cpp +++ b/services/core/src/distributed_device_profile_service_new.cpp @@ -204,6 +204,20 @@ int32_t DistributedDeviceProfileServiceNew::UnInit() return DP_SUCCESS; } +bool DistributedDeviceProfileServiceNew::ExitIdleState() +{ + if (!CancelIdle()) { + HILOGE("Cancel idle failed!"); + return false; + } + return true; +} + +bool DistributedDeviceProfileServiceNew::IsStopped() +{ + return isStopped_.load(); +} + int32_t DistributedDeviceProfileServiceNew::UnInitNext() { if (ProfileCache::GetInstance().UnInit() != DP_SUCCESS) { @@ -917,10 +931,17 @@ void DistributedDeviceProfileServiceNew::OnStart(const SystemAbilityOnDemandReas void DistributedDeviceProfileServiceNew::OnStop() { + HILOGI("call"); + isStopped_ = true; int32_t ret = UnInit(); HILOGI("UnInit ret=%{public}d", ret); } +void DistributedDeviceProfileServiceNew::OnActive(const SystemAbilityOnDemandReason& activeReason) +{ + HILOGI("active reason %{public}d", activeReason.GetId()); +} + int32_t DistributedDeviceProfileServiceNew::OnIdle(const SystemAbilityOnDemandReason& idleReason) { HILOGI("idle reason %{public}d", idleReason.GetId()); diff --git a/services/core/src/distributed_device_profile_stub_new.cpp b/services/core/src/distributed_device_profile_stub_new.cpp index 807636e87f087a6d4c44bccac75e3e3522e37dca..19e7fb23ff69d54af18a357638707bf0f44230c6 100644 --- a/services/core/src/distributed_device_profile_stub_new.cpp +++ b/services/core/src/distributed_device_profile_stub_new.cpp @@ -20,9 +20,10 @@ #include "ipc_skeleton.h" #include "ipc_utils.h" +#include "distributed_device_profile_enums.h" #include "distributed_device_profile_errors.h" #include "distributed_device_profile_log.h" -#include "distributed_device_profile_enums.h" +#include "distributed_device_profile_service_new.h" #include "profile_utils.h" namespace OHOS { @@ -225,6 +226,15 @@ int32_t DistributedDeviceProfileStubNew::OnRemoteRequest(uint32_t code, MessageP MessageParcel& reply, MessageOption& option) { HILOGI("code = %{public}u, CallingPid = %{public}u", code, IPCSkeleton::GetCallingPid()); + if (DistributedDeviceProfileServiceNew::GetInstance().IsStopped()) { + HILOGE("dp service has stopped"); + return DP_SERVICE_STOPPED; + } + bool exitIdleStateResult = DistributedDeviceProfileServiceNew::GetInstance().ExitIdleState(); + if (!exitIdleStateResult) { + HILOGE("CancelIdle failed, ExitIdleState = %{public}d", exitIdleStateResult); + return DP_SERVICE_ON_IDLE; + } DelayUnloadTask(); if (!IsInterfaceTokenValid(data)) { HILOGE("check interface token failed"); diff --git a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp index 28c5cf93a88566a5530bc6ef4b38a0108825e31a..7bd48b7328c6b1f45e5f6fb843792d7a6d45a3d6 100644 --- a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp @@ -26,6 +26,7 @@ #include "distributed_device_profile_log.h" #include "distributed_device_profile_errors.h" #include "distributed_device_profile_enums.h" +#include "distributed_device_profile_service_new.h" #include "distributed_device_profile_stub_new.h" namespace OHOS { @@ -527,6 +528,23 @@ HWTEST_F(DistributedDeviceProfileStubNewTest, OnRemoteRequest_002, TestSize.Leve EXPECT_EQ(DP_INTERFACE_CHECK_FAILED, ret); } +/** + * @tc.name: OnRemoteRequest003 + * @tc.desc: OnRemoteRequest + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, OnRemoteRequest_003, TestSize.Level1) +{ + uint32_t code = static_cast(DPInterfaceCode::PUT_ACL_PROFILE); + MessageParcel data; + MessageParcel reply; + MessageOption option; + DistributedDeviceProfileServiceNew::GetInstance().isStopped_ = true; + int32_t ret = ProfileStub_->OnRemoteRequest(code, data, reply, option); + EXPECT_EQ(DP_SERVICE_STOPPED, ret); +} + /** * @tc.name: PutAccessControlProfileInner001 * @tc.desc: PutAccessControlProfileInner diff --git a/services/core/test/unittest/dp_profile_service_test.cpp b/services/core/test/unittest/dp_profile_service_test.cpp index fccddf0db63c90293464212035fda29adb32f3f0..8e19b113fc7d073528b457aa7c94732d917a54ed 100644 --- a/services/core/test/unittest/dp_profile_service_test.cpp +++ b/services/core/test/unittest/dp_profile_service_test.cpp @@ -473,6 +473,8 @@ HWTEST_F(DpProfileServiceTest, SubscribeDeviceProfileInited_001, TestSize.Level1 HWTEST_F(DpProfileServiceTest, UnSubscribeDeviceProfileInited_001, TestSize.Level1) { int32_t saId = 1000; + DistributedDeviceProfileServiceNew::GetInstance().ExitIdleState(); + DistributedDeviceProfileServiceNew::GetInstance().IsStopped(); int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().UnSubscribeDeviceProfileInited(saId); EXPECT_EQ(DP_PERMISSION_DENIED, ret); }