diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index bcce7f808625bd1fcd5cd7eebc24680adedb523f..bb860ee33eb108a21dcbf2f344fb16431a076943 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -411,7 +411,17 @@ bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen ptr += userPairSize; leftSize -= userPairSize; - if (!UnPackAppId(&ptr, leftSize) || !UnPackStoreId(&ptr, leftSize) || !UnPackAccountId(&ptr, leftSize)) { + if (!UnPackAppId(&ptr, leftSize)) { + return false; + } + bool flag = false; + auto peerCap = UpgradeManager::GetInstance().GetCapability(session_.sourceDeviceId, flag); + if (!flag) { + ZLOGE("get peer cap failed, peer deviceId:%{public}s", Anonymous::Change(session_.sourceDeviceId).c_str()); + return false; + } + if (peerCap.version >= CapMetaData::ACCOUNT_VERSION && + (!UnPackStoreId(&ptr, leftSize) || !UnPackAccountId(&ptr, leftSize))) { return false; } return true; diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp index 2276159fa202293bbe764d9f337e7141e6b61a6f..7654c983cb7a38d1315096fc3bc08b4fd2bb5cd7 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.cpp @@ -16,8 +16,6 @@ #include "upgrade_manager.h" -#include -#include "account/account_delegate.h" #include "device_manager_adapter.h" #include "log_print.h" #include "metadata/meta_data_manager.h" @@ -34,18 +32,42 @@ UpgradeManager &UpgradeManager::GetInstance() void UpgradeManager::Init(std::shared_ptr executors) { - if (executors_) { - return; + MetaDataManager::GetInstance().Subscribe( + CapMetaRow::KEY_PREFIX, [this](const std::string &key, const std::string &value, int32_t flag) -> auto { + CapMetaData capMeta; + if (value.empty()) { + MetaDataManager::GetInstance().LoadMeta(key, capMeta); + } else { + CapMetaData::Unmarshall(value, capMeta); + } + auto deviceId = CapMetaRow::GetDeviceId(key); + if (deviceId.empty()) { + ZLOGE("deviceId is empty, key:%{public}s, flag:%{public}d", Anonymous::Change(key).c_str(), flag); + return false; + } + if (deviceId == DmAdapter::GetInstance().GetLocalDevice().uuid) { + return true; + } + capMeta.deviceId = capMeta.deviceId.empty() ? deviceId : capMeta.deviceId; + ZLOGI("CapMetaData has change, deviceId:%{public}s, version:%{public}d, flag:%{public}d", + Anonymous::Change(capMeta.deviceId).c_str(), capMeta.version, flag); + if (flag == MetaDataManager::INSERT || flag == MetaDataManager::UPDATE) { + capabilities_.InsertOrAssign(capMeta.deviceId, capMeta); + } else if (flag == MetaDataManager::DELETE) { + capabilities_.Erase(capMeta.deviceId); + } + return true; + }); + if (executors != nullptr) { + executors_ = std::move(executors); + executors_->Execute(GetTask()); } - executors_ = std::move(executors); - executors_->Execute(GetTask()); } ExecutorPool::Task UpgradeManager::GetTask() { return [this] { - auto succ = InitLocalCapability(); - if (succ) { + if (InitLocalCapability() || executors_ == nullptr) { return; } executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask()); @@ -77,6 +99,7 @@ bool UpgradeManager::InitLocalCapability() auto localDeviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; CapMetaData capMetaData; capMetaData.version = CapMetaData::CURRENT_VERSION; + capMetaData.deviceId = localDeviceId; auto dbKey = CapMetaRow::GetKeyFor(localDeviceId); bool status = MetaDataManager::GetInstance().SaveMeta({ dbKey.begin(), dbKey.end() }, capMetaData); if (status) { diff --git a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h index 411181e38e372792d92b7b4dc7c30b57b9ae4730..fd4ec20f3d5e2db193f58573e47c1ef8afe5b960 100644 --- a/services/distributeddataservice/app/src/session_manager/upgrade_manager.h +++ b/services/distributeddataservice/app/src/session_manager/upgrade_manager.h @@ -15,18 +15,13 @@ #ifndef DISTRIBUTEDDATAMGR_UPGRADE_MANAGER_H #define DISTRIBUTEDDATAMGR_UPGRADE_MANAGER_H -#include -#include "auth_delegate.h" #include "concurrent_map.h" -#include "kvstore_meta_manager.h" #include "metadata/capability_meta_data.h" #include "executor_pool.h" #include "types.h" -namespace OHOS::DistributedData { -using DistributedDB::KvStoreNbDelegate; -using OHOS::DistributedKv::KvStoreTuple; +namespace OHOS::DistributedData { class UpgradeManager { public: static UpgradeManager &GetInstance(); @@ -39,10 +34,6 @@ private: ExecutorPool::Task GetTask(); ConcurrentMap capabilities_ {}; std::shared_ptr executors_; - - static constexpr int32_t NO_ACCOUNT = 0; - static constexpr int32_t IDENTICAL_ACCOUNT = 1; - static constexpr const char *defaultAccountId = "ohosAnonymousUid"; }; } // namespace OHOS::DistributedData #endif // DISTRIBUTEDDATAMGR_UPGRADE_MANAGER_H diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index 4feefbd64ad7a21b508e3258e22f111e78696e40..002e0dfa9b67f18727797c3776e11d59be3089b6 100644 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -332,6 +332,42 @@ ohos_unittest("FeatureStubImplTest") { part_name = "datamgr_service" } +ohos_unittest("UpgradeManagerTest") { + module_out_path = module_output_path + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + + include_dirs = [ + "${data_service_path}/app/src/session_manager", + "${data_service_path}/app/test/mock/capability", + "${data_service_path}/framework/include", + ] + + sources = [ + "${data_service_path}/app/test/mock/capability/metadata/meta_data_manager.cpp", + "${data_service_path}/app/test/mock/capability/device_manager_adapter.cpp", + "${data_service_path}/app/src/session_manager/upgrade_manager.cpp", + "${data_service_path}/framework/metadata/capability_meta_data.cpp", + "${data_service_path}/framework/serializable/serializable.cpp", + "${data_service_path}/framework/utils/anonymous.cpp", + "${data_service_path}/framework/utils/constant.cpp", + "${data_service_path}/app/test/unittest/upgrade_manager_test.cpp", + ] + + external_deps = [ + "c_utils:utils", + "googletest:gtest_main", + "hilog:libhilog", + "json:nlohmann_json_static", + "kv_store:distributeddata_inner", + ] + + part_name = "datamgr_service" +} + ############################################################################### group("unittest") { @@ -343,11 +379,6 @@ group("unittest") { ":KvStoreDataServiceClearTest", ":KvStoreDataServiceTest", ":SessionManagerTest", + ":UpgradeManagerTest", ] -} - -############################################################################### -group("moduletest") { - testonly = true -} -############################################################################### +} \ No newline at end of file diff --git a/services/distributeddataservice/app/test/mock/capability/device_manager_adapter.cpp b/services/distributeddataservice/app/test/mock/capability/device_manager_adapter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..76e30d2759644805e1f14b95131c4fad32298a92 --- /dev/null +++ b/services/distributeddataservice/app/test/mock/capability/device_manager_adapter.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_manager_adapter.h" + +namespace OHOS::DistributedData { +static constexpr const char* LOCAL_DEVICE = "ABCDEF0123456789"; +DeviceManagerAdapter &DeviceManagerAdapter::GetInstance() +{ + static DeviceManagerAdapter dmAdapter; + return dmAdapter; +} + +DeviceInfo DeviceManagerAdapter::GetLocalDevice() +{ + return { .uuid = LOCAL_DEVICE }; +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/app/test/mock/capability/device_manager_adapter.h b/services/distributeddataservice/app/test/mock/capability/device_manager_adapter.h new file mode 100644 index 0000000000000000000000000000000000000000..ceb43b532bf2babbb97d826607e1b037cf93f8c5 --- /dev/null +++ b/services/distributeddataservice/app/test/mock/capability/device_manager_adapter.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_META_DEVICE_MANAGER_H +#define MOCK_META_DEVICE_MANAGER_H + +#include + +namespace OHOS::DistributedData { +struct DeviceInfo { + std::string uuid; +}; + +class DeviceManagerAdapter { +public: + static DeviceManagerAdapter &GetInstance(); + DeviceInfo GetLocalDevice(); + +private: + DeviceManagerAdapter() = default; + ~DeviceManagerAdapter() = default; +}; +} // namespace OHOS::DistributedData +#endif // MOCK_META_DEVICE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/app/test/mock/capability/metadata/meta_data_manager.cpp b/services/distributeddataservice/app/test/mock/capability/metadata/meta_data_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..867002fdb90ab82addd9300dcd4f6bd17538e41b --- /dev/null +++ b/services/distributeddataservice/app/test/mock/capability/metadata/meta_data_manager.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "meta_data_manager.h" + +namespace OHOS::DistributedData { +MetaDataManager &MetaDataManager::GetInstance() +{ + static MetaDataManager instance; + return instance; +} + +void MetaDataManager::Init(std::shared_ptr executors) +{ + executors_ = std::move(executors); +} + +bool MetaDataManager::SaveMeta(const std::string &key, const CapMetaData &value) +{ + capabilities_.InsertOrAssign(key, value); + executors_->Execute([this, key, value]() { + if (capObserver_ != nullptr) { + capObserver_(key, Serializable::Marshall(value), INSERT); + } + }); + return true; +} + +bool MetaDataManager::LoadMeta(const std::string &key, CapMetaData &value) +{ + auto index = capabilities_.Find(key); + if (index.first) { + value = index.second; + return true; + } + return false; +} + +bool MetaDataManager::Subscribe(std::string prefix, Observer observer) +{ + (void)prefix; + capObserver_ = observer; + return true; +} + +void MetaDataManager::UnSubscribe() +{ + capObserver_ = nullptr; +} + +bool MetaDataManager::UpdateMeta(const std::string &key, const CapMetaData &value) +{ + capabilities_.InsertOrAssign(key, value); + executors_->Execute([this, key, value]() { + if (capObserver_ != nullptr) { + capObserver_(key, Serializable::Marshall(value), UPDATE); + } + }); + return true; +} + +bool MetaDataManager::DeleteMeta(const std::string &key, const CapMetaData &value) +{ + capabilities_.Erase(key); + executors_->Execute([this, key, value]() { + if (capObserver_ != nullptr) { + capObserver_(key, Serializable::Marshall(value), DELETE); + } + }); + return true; +} +} // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/app/test/mock/capability/metadata/meta_data_manager.h b/services/distributeddataservice/app/test/mock/capability/metadata/meta_data_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..8fbf3814f1d1525ded5214d61f4038de0de9a52c --- /dev/null +++ b/services/distributeddataservice/app/test/mock/capability/metadata/meta_data_manager.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MOCK_META_DATA_MANAGER_H +#define MOCK_META_DATA_MANAGER_H + +#include + +#include "metadata/capability_meta_data.h" +#include "concurrent_map.h" +#include "executor_pool.h" + +namespace OHOS::DistributedData { +class MetaDataManager { +public: + enum Action : int32_t { + INSERT = 0, + UPDATE = 1, + DELETE = 2, + }; + + using Observer = std::function; + + static MetaDataManager &GetInstance(); + void Init(std::shared_ptr executors); + bool SaveMeta(const std::string &key, const CapMetaData &value); + bool LoadMeta(const std::string &key, CapMetaData &value); + bool Subscribe(std::string prefix, Observer observer); + void UnSubscribe(); + bool UpdateMeta(const std::string &key, const CapMetaData &value); + bool DeleteMeta(const std::string &key, const CapMetaData &value); + +private: + MetaDataManager() = default; + ~MetaDataManager() = default; + + Observer capObserver_; + ConcurrentMap capabilities_ {}; + std::shared_ptr executors_; +}; +} // namespace OHOS::DistributedData +#endif // MOCK_META_DATA_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp b/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp index 7b9aeb3d2e66ccf9ae2e12c94008366ee4efd135..e60c4f042e94eb8c2c7a1a8347ac7af3d690ade7 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_data_service_test.cpp @@ -668,24 +668,6 @@ HWTEST_F(KvStoreDataServiceTest, DumpBundleInfo001, TestSize.Level0) EXPECT_NO_FATAL_FAILURE(kvStoreDataServiceTest.DumpBundleInfo(fd, params)); } -/** -* @tc.name: UpgradeManagerTest001 -* @tc.desc: test Init function -* @tc.type: FUNC -* @tc.require: -* @tc.author: SQL -*/ -HWTEST_F(UpgradeManagerTest, UpgradeManagerTest001, TestSize.Level0) -{ - auto executors = std::make_shared(1, 0); - DistributedData::UpgradeManager instance; - instance.Init(executors); - EXPECT_TRUE(instance.executors_); - - instance.Init(executors); - EXPECT_TRUE(instance.executors_); -} - /** * @tc.name: OnExtensionRestore001 * @tc.desc: restore with invalid fd diff --git a/services/distributeddataservice/app/test/unittest/upgrade_manager_test.cpp b/services/distributeddataservice/app/test/unittest/upgrade_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70164b93e9f9ae485bba17fd025dc55c66b99a7e --- /dev/null +++ b/services/distributeddataservice/app/test/unittest/upgrade_manager_test.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "upgrade_manager.h" + +#include + +#include "device_manager_adapter.h" +#include "metadata/meta_data_manager.h" + +using namespace testing; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::DistributedData; + +static constexpr size_t THREAD_MIN = 0; +static constexpr size_t THREAD_MAX = 2; +static constexpr const char* REMOTE_DEVICE = "0123456789ABCDEF"; + +class UpgradeManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void) {}; + void SetUp() {}; + void TearDown() {}; + + static std::shared_ptr executors_; + static std::string localDevice_; +}; + +std::shared_ptr UpgradeManagerTest::executors_; +std::string UpgradeManagerTest::localDevice_; + +void UpgradeManagerTest::SetUpTestCase(void) +{ + localDevice_ = DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid; + executors_ = std::make_shared(THREAD_MAX, THREAD_MIN); + MetaDataManager::GetInstance().Init(executors_); + UpgradeManager::GetInstance().Init(executors_); + sleep(1); +} + +/** +* @tc.name: GetCapabilityTest001 +* @tc.desc: get local device capability +* @tc.type: FUNC +*/ +HWTEST_F(UpgradeManagerTest, GetCapabilityTest001, TestSize.Level1) +{ + bool result = false; + auto capMeta = UpgradeManager::GetInstance().GetCapability(localDevice_, result); + ASSERT_EQ(result, true); + ASSERT_EQ(capMeta.version, CapMetaData::CURRENT_VERSION); + ASSERT_EQ(capMeta.deviceId, localDevice_); +} + +/** +* @tc.name: GetCapabilityTest002 +* @tc.desc: get non-exist remote device capability +* @tc.type: FUNC +*/ +HWTEST_F(UpgradeManagerTest, GetCapabilityTest002, TestSize.Level1) +{ + bool result = true; + auto capMeta = UpgradeManager::GetInstance().GetCapability(REMOTE_DEVICE, result); + ASSERT_EQ(result, false); + ASSERT_EQ(capMeta.version, CapMetaData::INVALID_VERSION); + ASSERT_EQ(capMeta.deviceId, ""); +} + +/** +* @tc.name: GetCapabilityTest003 +* @tc.desc: get remote device capability by callback of meta save-update-delete +* @tc.type: FUNC +*/ +HWTEST_F(UpgradeManagerTest, GetCapabilityTest003, TestSize.Level1) +{ + CapMetaData capMetaData; + capMetaData.version = CapMetaData::CURRENT_VERSION; + capMetaData.deviceId = REMOTE_DEVICE; + auto capKey = CapMetaRow::GetKeyFor(REMOTE_DEVICE); + (void)MetaDataManager::GetInstance().SaveMeta({ capKey.begin(), capKey.end() }, capMetaData); + sleep(1); + + bool result = false; + auto capMeta = UpgradeManager::GetInstance().GetCapability(REMOTE_DEVICE, result); + ASSERT_EQ(result, true); + ASSERT_EQ(capMeta.version, capMetaData.version); + ASSERT_EQ(capMeta.deviceId, REMOTE_DEVICE); + + capMetaData.version = CapMetaData::INVALID_VERSION; + (void)MetaDataManager::GetInstance().UpdateMeta({ capKey.begin(), capKey.end() }, capMetaData); + sleep(1); + + result = false; + capMeta = UpgradeManager::GetInstance().GetCapability(REMOTE_DEVICE, result); + ASSERT_EQ(result, true); + ASSERT_EQ(capMeta.version, capMetaData.version); + ASSERT_EQ(capMeta.deviceId, REMOTE_DEVICE); + + (void)MetaDataManager::GetInstance().DeleteMeta({ capKey.begin(), capKey.end() }, capMetaData); + sleep(1); + + capMeta = UpgradeManager::GetInstance().GetCapability(REMOTE_DEVICE, result); + ASSERT_EQ(result, false); + ASSERT_EQ(capMeta.version, CapMetaData::INVALID_VERSION); + ASSERT_EQ(capMeta.deviceId, ""); +} + +/** +* @tc.name: GetCapabilityTest004 +* @tc.desc: get remote device capability by meta store +* @tc.type: FUNC +*/ +HWTEST_F(UpgradeManagerTest, GetCapabilityTest004, TestSize.Level1) +{ + MetaDataManager::GetInstance().UnSubscribe(); + CapMetaData capMetaData; + capMetaData.version = CapMetaData::CURRENT_VERSION; + capMetaData.deviceId = REMOTE_DEVICE; + auto capKey = CapMetaRow::GetKeyFor(REMOTE_DEVICE); + (void)MetaDataManager::GetInstance().SaveMeta({ capKey.begin(), capKey.end() }, capMetaData); + + bool result = false; + auto capMeta = UpgradeManager::GetInstance().GetCapability(REMOTE_DEVICE, result); + ASSERT_EQ(result, true); + ASSERT_EQ(capMeta.version, capMetaData.version); + ASSERT_EQ(capMeta.deviceId, REMOTE_DEVICE); +} \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/metadata/capability_meta_data.h b/services/distributeddataservice/framework/include/metadata/capability_meta_data.h index 50b6f25eae785417433c4854c34c9c1cc742475d..28102100a140d3adca811f6f60ec9ca96a7537b9 100644 --- a/services/distributeddataservice/framework/include/metadata/capability_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/capability_meta_data.h @@ -25,6 +25,7 @@ public: static constexpr int32_t INVALID_VERSION = -1; static constexpr int32_t ACCOUNT_VERSION = 2; int32_t version = INVALID_VERSION; + std::string deviceId = ""; API_EXPORT bool Marshal(json &node) const override; API_EXPORT bool Unmarshal(const json &node) override; @@ -34,6 +35,7 @@ class CapMetaRow { public: static constexpr const char *KEY_PREFIX = "CapabilityMeta"; API_EXPORT static std::vector GetKeyFor(const std::string &key); + API_EXPORT static const std::string GetDeviceId(const std::string &key); }; } // namespace OHOS::DistributedData #endif // DISTRIBUTEDDATAMGR_CAPABILITY_META_DATA_H diff --git a/services/distributeddataservice/framework/metadata/capability_meta_data.cpp b/services/distributeddataservice/framework/metadata/capability_meta_data.cpp index ab81aaeb816f2668dda522b4aaa898d31c3ea4c9..9e2d29478b3d64a9d6c9ddbf849d7c804b9640c8 100644 --- a/services/distributeddataservice/framework/metadata/capability_meta_data.cpp +++ b/services/distributeddataservice/framework/metadata/capability_meta_data.cpp @@ -20,10 +20,14 @@ namespace OHOS::DistributedData { constexpr int32_t CapMetaData::CURRENT_VERSION; constexpr int32_t CapMetaData::INVALID_VERSION; constexpr const char *CapMetaRow::KEY_PREFIX; +constexpr int32_t INDEX_NUM = 2; +constexpr int32_t INDEX_PREFIX = 0; +constexpr int32_t INDEX_DEVICE = 1; bool CapMetaData::Marshal(json &node) const { bool ret = true; ret = SetValue(node[GET_NAME(version)], version) && ret; + ret = SetValue(node[GET_NAME(deviceId)], deviceId) && ret; return ret; } @@ -31,6 +35,7 @@ bool CapMetaData::Unmarshal(const json &node) { bool ret = true; ret = GetValue(node, GET_NAME(version), version) && ret; + ret = GetValue(node, GET_NAME(deviceId), deviceId) && ret; return ret; } @@ -39,4 +44,13 @@ std::vector CapMetaRow::GetKeyFor(const std::string &key) std::string str = Constant::Concatenate({ KEY_PREFIX, Constant::KEY_SEPARATOR, key }); return { str.begin(), str.end() }; } + +const std::string CapMetaRow::GetDeviceId(const std::string &key) +{ + auto items = Constant::Split(key, Constant::KEY_SEPARATOR); + if (items.size() < INDEX_NUM || items[INDEX_PREFIX] != KEY_PREFIX) { + return ""; + } + return items[INDEX_DEVICE]; +} } // namespace OHOS::DistributedData