From 6526d17da7167230ce0499cf4b74c73c2376ad8d Mon Sep 17 00:00:00 2001 From: hunili Date: Fri, 29 Dec 2023 16:08:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8DDP=E4=BF=AE=E6=94=B9=20issue:?= =?UTF-8?q?=20https://gitee.com/openharmony/ability=5Fdmsfwk/issues/I8SRBC?= =?UTF-8?q?=20Signed-off-by:=20hunili=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bundle.json | 4 +- services/dtbschedmgr/BUILD.gn | 8 +- .../include/dtbschedmgr_device_info_storage.h | 16 +++- .../src/adapter/dnetwork_adapter.cpp | 6 +- .../dtbschedmgr/src/dms_version_manager.cpp | 17 ++-- .../src/dtbschedmgr_device_info_storage.cpp | 90 ++++++++++++++++++- services/dtbschedmgr/test/BUILD.gn | 6 +- .../dtbschedmgr_device_info_storage_test.cpp | 33 ++++++- 8 files changed, 164 insertions(+), 16 deletions(-) diff --git a/bundle.json b/bundle.json index a1e85331..db98bec2 100644 --- a/bundle.json +++ b/bundle.json @@ -65,7 +65,9 @@ "samgr", "ffrt" ], - "third_party": [] + "third_party": [ + "cJSON" + ] }, "build": { "group_type": { diff --git a/services/dtbschedmgr/BUILD.gn b/services/dtbschedmgr/BUILD.gn index b61d7500..6b047ebe 100644 --- a/services/dtbschedmgr/BUILD.gn +++ b/services/dtbschedmgr/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -29,6 +29,7 @@ config("distributed_sched_config") { "${communication_path}/dsoftbus/interfaces/kits/common", "${dms_path}/interfaces/kits/napi/include", "include/distributedWant", + "//third_party/cJSON", ] defines = [] if (dmsfwk_mission_manager) { @@ -111,6 +112,8 @@ ohos_shared_library("distributedschedsvr") { "//foundation/ability/dmsfwk/services/dtbschedmgr/test/resource:coverage_flags", ] + deps = [ "//third_party/cJSON:cjson" ] + external_deps = [ "ability_base:base", "ability_base:want", @@ -123,7 +126,8 @@ ohos_shared_library("distributedschedsvr") { "bundle_framework:appexecfwk_core", "c_utils:utils", "device_auth:deviceauth_sdk", - "device_info_manager:distributed_device_profile_client", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", "device_manager:devicemanagersdk", "device_security_level:dslm_sdk", "distributed_bundle_framework:dbms_fwk", diff --git a/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h b/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h index d24d4c84..34ca80ff 100644 --- a/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h +++ b/services/dtbschedmgr/include/dtbschedmgr_device_info_storage.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H #define OHOS_DISTRIBUTED_DTBSCHEDMGR_DEVICE_INFO_INTERFACE_H +#include #include #include "adapter/dnetwork_adapter.h" @@ -24,6 +25,7 @@ #include "event_handler.h" #include "iremote_object.h" #include "single_instance.h" +#include "sync_completed_callback_stub.h" namespace OHOS { namespace DistributedSchedule { @@ -41,6 +43,7 @@ public: bool Init(); void Stop(); bool GetLocalDeviceId(std::string& networkId); + bool GetLocalUdid(std::string& udid); void DeviceOnlineNotify(const std::shared_ptr devInfo); void DeviceOfflineNotify(const std::string& networkId); void OnDeviceInfoChanged(const std::string& networkId); @@ -81,12 +84,20 @@ public: */ void UpdateDeviceInfoStorage(const std::vector& dmDeviceInfoList); + /** + * Sync dms version info to remote + * + * @param remoteNetworkId + */ + int32_t SyncDmsVersionInfoToRemote(const std::string& remoteNetworkId); + private: bool InitNetworkIdManager(std::shared_ptr dnetworkAdapter); bool ConnectSoftbus(); void ClearAllDevices(); bool WaitForDnetworkReady(); bool GetLocalDeviceFromDnet(std::string& networkId); + bool GetLocalDeviceUdid(std::string& udid); void RegisterUuidNetworkIdMap(const std::string& networkId); void UnregisterUuidNetworkIdMap(const std::string& networkId); std::mutex deviceLock_; @@ -97,6 +108,9 @@ private: std::mutex uuidNetworkIdLock_; std::shared_ptr initHandler_; std::shared_ptr networkIdMgrHandler_; + class SyncCallback : public OHOS::DistributedDeviceProfile::SyncCompletedCallbackStub { + void OnSyncCompleted(const std::map &syncResults); + }; }; } // namespace DistributedSchedule } // namespace OHOS diff --git a/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp b/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp index e1fe1be9..714ba89b 100644 --- a/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp +++ b/services/dtbschedmgr/src/adapter/dnetwork_adapter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -66,7 +66,8 @@ void DnetworkAdapter::DeviceInitCallBack::OnRemoteDied() void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo& deviceInfo) { - HILOGI("OnNodeOnline netwokId = %{public}s", AnonymizeNetworkId(deviceInfo.networkId).c_str()); + std::string networkId = deviceInfo.networkId; + HILOGI("OnNodeOnline netwokId = %{public}s", AnonymizeNetworkId(networkId).c_str()); auto onlineNotifyTask = [deviceInfo]() { std::lock_guard autoLock(listenerSetMutex_); for (auto& listener : listenerSet_) { @@ -77,6 +78,7 @@ void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceOnline(const DmDeviceInfo& HILOGE("OnNodeOnline post task failed"); return; } + DtbschedmgrDeviceInfoStorage::GetInstance().SyncDmsVersionInfoToRemote(networkId); } void DnetworkAdapter::DmsDeviceStateCallback::OnDeviceOffline(const DmDeviceInfo& deviceInfo) diff --git a/services/dtbschedmgr/src/dms_version_manager.cpp b/services/dtbschedmgr/src/dms_version_manager.cpp index 122de7c3..d0742e68 100644 --- a/services/dtbschedmgr/src/dms_version_manager.cpp +++ b/services/dtbschedmgr/src/dms_version_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,14 +16,17 @@ #include "dms_version_manager.h" #include "distributed_device_profile_client.h" +#include "dtbschedmgr_device_info_storage.h" #include "dtbschedmgr_log.h" +#include "nlohmann/json.hpp" #include "string_ex.h" namespace OHOS { namespace DistributedSchedule { namespace { const std::string TAG = "DmsVersionManager"; -const std::string DMS_SERVICE_ID = "appInfo"; +const std::string DMS_SERVICE_ID = "dmsfwk_svr_id"; +const std::string DMS_CHAR_ID = "dmsInfo"; const std::string DMS_SERVICE_TYPE = "appInfo"; const std::string PACKAGE_NAMES = "packageNames"; const std::string VERSIONS = "versions"; @@ -76,13 +79,15 @@ int32_t DmsVersionManager::GetRemoteDmsVersion(const std::string& deviceId, DmsV int32_t DmsVersionManager::GetAppInfoFromDP(const std::string& deviceId, std::string& appInfoJsonData) { - DeviceProfile::ServiceCharacteristicProfile profile; - int32_t result = DeviceProfile::DistributedDeviceProfileClient::GetInstance().GetDeviceProfile(deviceId, - DMS_SERVICE_ID, profile); + DistributedDeviceProfile::CharacteristicProfile profile; + std::string udid = ""; + udid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId); + int32_t result = DistributedDeviceProfile::DistributedDeviceProfileClient::GetInstance().GetCharacteristicProfile( + udid, DMS_SERVICE_ID, DMS_CHAR_ID, profile); if (result != ERR_OK) { return result; } - appInfoJsonData = profile.GetCharacteristicProfileJson(); + appInfoJsonData = profile.GetCharacteristicValue(); return ERR_OK; } diff --git a/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp b/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp index 8f726772..ab74131b 100644 --- a/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp +++ b/services/dtbschedmgr/src/dtbschedmgr_device_info_storage.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -19,11 +19,13 @@ #include #include "distributed_device_node_listener.h" +#include "distributed_device_profile_client.h" #include "distributed_sched_service.h" #include "dtbschedmgr_log.h" #include "ipc_object_proxy.h" #include "ipc_skeleton.h" #include "iservice_registry.h" +#include "cJSON.h" #include "system_ability_definition.h" using namespace std; @@ -31,11 +33,18 @@ namespace OHOS { namespace DistributedSchedule { using namespace std::chrono_literals; using namespace DistributedHardware; +using namespace DistributedDeviceProfile; namespace { constexpr int32_t RETRY_TIMES = 30; constexpr int32_t CONNECT_SOFTBUS_RETRY_TIMES = 60; const std::string TAG = "DtbschedmgrDeviceInfoStorage"; +const std::string DMS_SERVICE_ID = "dmsfwk_svr_id"; +const std::string DMS_CHAR_ID = "dmsInfo"; +const std::string DMS_VERSION = "4.0.0"; +const std::string DMS_NAMES = "dmsfwk"; +constexpr const char *PACKAGE_NAMES = "packageNames"; +constexpr const char *VERSIONS = "versions"; } IMPLEMENT_SINGLE_INSTANCE(DtbschedmgrDeviceInfoStorage); @@ -188,11 +197,62 @@ void DtbschedmgrDeviceInfoStorage::UpdateDeviceInfoStorage( } } +int32_t DtbschedmgrDeviceInfoStorage::SyncDmsVersionInfoToRemote(const std::string& remoteNetworkId) +{ + std::string localUdid = ""; + GetLocalUdid(localUdid); + if (localUdid.empty()) { + HILOGE("getlocaludid failed, localUdid is empty"); + } + DistributedDeviceProfile::ServiceProfile serviceProfile; + serviceProfile.SetDeviceId(localUdid); + serviceProfile.SetServiceName(DMS_SERVICE_ID); + serviceProfile.SetServiceType(DMS_SERVICE_ID); + DistributedDeviceProfile::DistributedDeviceProfileClient::GetInstance().PutServiceProfile(serviceProfile); + + DistributedDeviceProfile::CharacteristicProfile characteristicProfile; + characteristicProfile.SetDeviceId(localUdid); + characteristicProfile.SetServiceName(DMS_SERVICE_ID); + characteristicProfile.SetCharacteristicKey(DMS_CHAR_ID); + cJSON *jObject = cJSON_CreateObject(); + if (jObject == nullptr) { + HILOGE("Failed to create cJSON object."); + return ERR_NULL_OBJECT; + } + cJSON_AddStringToObject(jObject, PACKAGE_NAMES, DMS_NAMES.c_str()); + cJSON_AddStringToObject(jObject, VERSIONS, DMS_VERSION.c_str()); + char *jsonData = cJSON_PrintUnformatted(jObject); + if (jsonData == nullptr) { + HILOGE("Failed to create JSON data."); + cJSON_Delete(jObject); + return ERR_NULL_OBJECT; + } + std::string dmsInfo(jsonData); + cJSON_Delete(jObject); + cJSON_free(jsonData); + characteristicProfile.SetCharacteristicValue(dmsInfo); + DistributedDeviceProfile::DistributedDeviceProfileClient::GetInstance(). + PutCharacteristicProfile(characteristicProfile); + + SyncOptions syncOptions; + syncOptions.AddDevice(remoteNetworkId); + syncOptions.SetSyncMode(SyncMode::PUSH_PULL); + sptr syncCallback = new(std::nothrow) SyncCallback; + int32_t syncRes = DistributedDeviceProfileClient::GetInstance().SyncDeviceProfile(syncOptions, syncCallback); + HILOGI("SyncDeviceProfile result: %{public}d", syncRes); + return syncRes; +} + bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceId(std::string& networkId) { return GetLocalDeviceFromDnet(networkId); } +bool DtbschedmgrDeviceInfoStorage::GetLocalUdid(std::string& udid) +{ + return GetLocalDeviceUdid(udid); +} + bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceFromDnet(std::string& networkId) { auto dnetworkAdapter = DnetworkAdapter::GetInstance(); @@ -211,6 +271,23 @@ bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceFromDnet(std::string& networkId return true; } +bool DtbschedmgrDeviceInfoStorage::GetLocalDeviceUdid(std::string& udid) +{ + auto dnetworkAdapter = DnetworkAdapter::GetInstance(); + if (dnetworkAdapter == nullptr) { + HILOGE("GetLocalDeviceFromDnet dnetworkAdapter null"); + return false; + } + DmDeviceInfo dmDeviceInfo; + if (!dnetworkAdapter->GetLocalBasicInfo(dmDeviceInfo)) { + HILOGE("GetLocalBasicInfo error"); + return false; + } + udid = GetUuidByNetworkId(dmDeviceInfo.networkId); + HILOGI("GetLocalDeviceUdid = %{public}s", DnetworkAdapter::AnonymizeNetworkId(udid).c_str()); + return true; +} + void DtbschedmgrDeviceInfoStorage::ClearAllDevices() { lock_guard autoLock(deviceLock_); @@ -323,6 +400,17 @@ void DtbschedmgrDeviceInfoStorage::OnDeviceInfoChanged(const std::string& device HILOGI("OnDeviceInfoChanged called"); } +void DtbschedmgrDeviceInfoStorage::SyncCallback::OnSyncCompleted(const std::map &syncResults) +{ + for (const auto &item : syncResults) { + std::string networkId = item.first; + int32_t syncResult = item.second; + HILOGI("networkId: %{public}s, SyncStatus: %{public}d", + DnetworkAdapter::AnonymizeNetworkId(networkId).c_str(), syncResult); + } +} + void DnetServiceDeathRecipient::OnRemoteDied(const wptr& remote) { HILOGI("OnRemoteDied dnetwork service died"); diff --git a/services/dtbschedmgr/test/BUILD.gn b/services/dtbschedmgr/test/BUILD.gn index 0f7bbe72..7c8083dc 100644 --- a/services/dtbschedmgr/test/BUILD.gn +++ b/services/dtbschedmgr/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -47,7 +47,8 @@ dsched_external_deps = [ "bundle_framework:appexecfwk_core", "c_utils:utils", "device_auth:deviceauth_sdk", - "device_info_manager:distributed_device_profile_client", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", "device_manager:devicemanagersdk", "device_security_level:dslm_sdk", "distributed_bundle_framework:dbms_fwk", @@ -536,6 +537,7 @@ ohos_unittest("dmsbasetest") { ":test_config", "./resource:coverage_flags", ] + deps = [ "//third_party/cJSON:cjson" ] configs += dsched_configs if (is_standard_system) { external_deps = dsched_external_deps diff --git a/services/dtbschedmgr/test/unittest/dtbschedmgr_device_info_storage_test.cpp b/services/dtbschedmgr/test/unittest/dtbschedmgr_device_info_storage_test.cpp index f0c27480..cfc64196 100644 --- a/services/dtbschedmgr/test/unittest/dtbschedmgr_device_info_storage_test.cpp +++ b/services/dtbschedmgr/test/unittest/dtbschedmgr_device_info_storage_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -399,5 +399,36 @@ HWTEST_F(DtbschedmgrDeviceInfoStorageTest, UpdateDeviceInfoStorageTest_002, Test EXPECT_EQ(!dmDeviceInfoList.empty(), true); DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest UpdateDeviceInfoStorageTest_002 end" << std::endl; } + +/** + * @tc.name: GetLocalDeviceUdidTest_001 + * @tc.desc: test GetLocalDeviceUdid + * @tc.type: FUNC + */ +HWTEST_F(DtbschedmgrDeviceInfoStorageTest, GetLocalDeviceUdidTest_001, TestSize.Level3) +{ + DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetLocalDeviceUdidTest_001 start" << std::endl; + std::string udid = ""; + DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(udid); + EXPECT_EQ(udid.empty(), true); + DistributedSchedUtil::MockPermission(); + DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalUdid(udid); + EXPECT_EQ(!udid.empty(), true); + DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest GetLocalDeviceUdidTest_001 end" << std::endl; +} + +/** + * @tc.name: SyncDmsVersionInfoToRemoteTest_001 + * @tc.desc: test SyncDmsVersionInfoToRemote + * @tc.type: FUNC + */ +HWTEST_F(DtbschedmgrDeviceInfoStorageTest, SyncDmsVersionInfoToRemoteTest_001, TestSize.Level3) +{ + DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest SyncDmsVersionInfoToRemoteTest_001 start" << std::endl; + std::string remoteNetworkId = "test"; + int32_t result = DtbschedmgrDeviceInfoStorage::GetInstance().SyncDmsVersionInfoToRemote(remoteNetworkId); + EXPECT_TRUE(result != DistributedDeviceProfile::DP_SUCCESS); + DTEST_LOG << "DtbschedmgrDeviceInfoStorageTest SyncDmsVersionInfoToRemoteTest_001 end" << std::endl; +} } // namespace DistributedSchedule } // namespace OHOS