diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..319133a3259fb578253ff8a26d3987fe1a3bcb4c --- /dev/null +++ b/README_zh.md @@ -0,0 +1,69 @@ +# **分布式包管理服务(DBMS)** + +## 简介 + +分布式包管理服务负责管理跨设备的组件调度和任务管理,实现跨设备RPC的能力,可以按需获取跨设备指定语言的资源。 + +## 目录 + +``` +foundation/bundlemanager/distributed_bundle_framework +├── interfaces +│ ├── inner_api # 内部接口存放目录 +│ └── kits/js # JS应用接口 +│ ├── distributebundlemgr +│ └── distributedBundle +├── services/dbms # dbms服务框架代码 +└── services/dbms/test # 测试目录 +``` + +## 说明 +### 获取远程设备AbilityInfo信息 +getRemoteAbilityInfo获取由elementName指定的远程设备上的应用的AbilityInfo信息(callback形式) + +* **参数:** + + | 参数名 | 类型 | 必填 | 说明 | + | ----------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | + | elementName | [ElementName](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-bundleManager-elementName.md) | 是 | ElementName信息。 | + | callback | AsyncCallback<[RemoteAbilityInfo](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-bundleManager-remoteAbilityInfo.md)> | 是 | 回调函数,调用成功返回err为null,data为RemoteAbilityInfo对象;调用失败err为错误对象, data为undefined。 | + +* **错误码:** + + 以下错误码的详细介绍请参见[ohos.bundle错误码](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/errorcodes/errorcode-bundle.md)。 + + | 错误码ID | 错误信息 | + | -------- | ---------------------------------------- | + | 17700001 | The specified bundle name is not found. | + | 17700003 | The specified ability name is not found. | + | 17700007 | The specified device ID is not found. | + | 17700027 | The distributed service is not running. | + +* **示例:** + +```ts + try { + distributedBundle.getRemoteAbilityInfo( + { + deviceId: '1', + bundleName: 'com.example.application', + abilityName: 'EntryAbility' + }, (err, data) => { + if (err) { + console.log(`Operation failed: error code is ${err.code} and error message is ${err.message}`); + } else { + console.info('Operation succeed:' + JSON.stringify(data)); + } + }); + } catch (err) { + console.log(`Operation failed: error code is ${err.code} and error message is ${err.message}`); + } + ``` + +* **指南:** + + 更多开发指导可参考[**示例文档**](https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-distributedBundleManager.md) + +## 相关仓 + +[bundlemanager_bundle_framework](https://gitee.com/openharmony/bundlemanager_bundle_framework) diff --git a/services/dbms/BUILD.gn b/services/dbms/BUILD.gn index 784f32c875381a1d3dc8cbe197cac8cadf1f302a..0fd9d833dc7a3e318afe3aef28db3d23a2d4d157 100644 --- a/services/dbms/BUILD.gn +++ b/services/dbms/BUILD.gn @@ -27,6 +27,7 @@ config("distributed_bms_config") { ohos_shared_library("libdbms") { sources = [ "src/account_manager_helper.cpp", + "src/dbms_device_manager.cpp", "src/distributed_bms.cpp", "src/distributed_bms_host.cpp", "src/distributed_data_storage.cpp", @@ -53,6 +54,7 @@ ohos_shared_library("libdbms") { "bundle_framework:appexecfwk_core", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "device_manager:devicemanagersdk", "hicollie_native:libhicollie", "hiviewdfx_hilog_native:libhilog", "i18n:intl_util", diff --git a/services/dbms/include/dbms_device_manager.h b/services/dbms/include/dbms_device_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..b0fb7153a6d8508e5aaf7bfcf63390a56d336da8 --- /dev/null +++ b/services/dbms/include/dbms_device_manager.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021-2023 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 FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BMS_DEVICE_MANAGER_H +#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BMS_DEVICE_MANAGER_H + +#include +#include + +#include "device_manager_callback.h" +#include "dm_device_info.h" +#include "system_ability_load_callback_stub.h" + +namespace OHOS { +namespace AppExecFwk { +class DbmsDeviceManager { +public: + DbmsDeviceManager(); + int32_t GetUdidByNetworkId(const std::string &netWorkId, std::string &udid); + +private: + bool InitDeviceManager(); + std::shared_ptr initCallback_; + mutable std::mutex isInitMutex_; + bool isInit_ = false; + +class DeviceInitCallBack : public DistributedHardware::DmInitCallback { + void OnRemoteDied() override; +}; +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BMS_DEVICE_MANAGER_H \ No newline at end of file diff --git a/services/dbms/include/distributed_bms.h b/services/dbms/include/distributed_bms.h index b7d2159e29f95665a68fb4b770becd6ba0140f89..ba1469dae09e555c0037c1a1e518d584399b10ea 100644 --- a/services/dbms/include/distributed_bms.h +++ b/services/dbms/include/distributed_bms.h @@ -20,6 +20,7 @@ #include "bundle_info.h" #include "bundle_mgr_interface.h" +#include "dbms_device_manager.h" #include "distributed_bms_host.h" #include "distributed_monitor.h" #include "if_system_ability_manager.h" @@ -36,6 +37,7 @@ class DistributedBms : public SystemAbility, public DistributedBmsHost { public: OHOS::sptr GetBundleMgr(); + OHOS::sptr GetDeviceManager(); /** * @brief get remote ability info * @param elementName Indicates the elementName. @@ -109,7 +111,7 @@ public: */ int32_t GetAbilityInfos(const std::vector &elementNames, const std::string &localeInfo, std::vector &remoteAbilityInfos) override; - + bool GetDistributedBundleInfo(const std::string &networkId, const std::string &bundleName, DistributedBundleInfo &distributedBundleInfo) override; @@ -123,6 +125,8 @@ public: int32_t GetDistributedBundleName(const std::string &networkId, uint32_t accessTokenId, std::string &bundleName) override; + int32_t GetUdidByNetworkId(const std::string &networkId, std::string &udid); + /** * @brief Start the bundle manager service. * @return @@ -133,12 +137,15 @@ public: * @return */ virtual void OnStop() override; + private: OHOS::sptr bundleMgr_; + std::shared_ptr dbmsDeviceManager_; std::shared_ptr distributedSub_; std::mutex bundleMgrMutex_; void Init(); + void InitDeviceManager(); bool GetMediaBase64(std::unique_ptr &data, int64_t fileLength, std::string &imageType, std::string &value); std::unique_ptr LoadResourceFile(std::string &path, int &len); diff --git a/services/dbms/src/dbms_device_manager.cpp b/services/dbms/src/dbms_device_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ec3e85b42bad58cdef5f9040650bd40ed5f3462 --- /dev/null +++ b/services/dbms/src/dbms_device_manager.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2021-2023 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 "dbms_device_manager.h" + +#include "app_log_wrapper.h" +#include "bundle_constants.h" +#include "device_manager.h" +#include "service_control.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { + const std::string DISTRIBUTED_BUNDLE_NAME = "distributed_bundle_framework"; + const std::string SERVICES_NAME = "d-bms"; +} + +DbmsDeviceManager::DbmsDeviceManager() +{ + APP_LOGI("DbmsDeviceManager instance is created"); +} + +bool DbmsDeviceManager::InitDeviceManager() +{ + std::lock_guard lock(isInitMutex_); + if (isInit_) { + APP_LOGI("device manager already init"); + return true; + } + + initCallback_ = std::make_shared(); + int32_t ret = + DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(DISTRIBUTED_BUNDLE_NAME, initCallback_); + if (ret != 0) { + APP_LOGE("init device manager failed, ret:%{public}d", ret); + return false; + } + isInit_ = true; + APP_LOGI("register device manager success"); + return true; +} + +void DbmsDeviceManager::DeviceInitCallBack::OnRemoteDied() +{ + APP_LOGI("DeviceInitCallBack OnRemoteDied"); +} + +int32_t DbmsDeviceManager::GetUdidByNetworkId(const std::string &netWorkId, std::string &udid) +{ + APP_LOGI("GetUdidByNetworkId"); + if (!InitDeviceManager()) { + return -1; + } + return DistributedHardware::DeviceManager::GetInstance().GetUdidByNetworkId( + DISTRIBUTED_BUNDLE_NAME, netWorkId, udid); +} +} +} \ No newline at end of file diff --git a/services/dbms/src/distributed_bms.cpp b/services/dbms/src/distributed_bms.cpp index 8ea53a2ca135b97be8d4b7f78cfbffddcdba7f10..8e22013449f9a173b07fe154f6128922fa92644b 100644 --- a/services/dbms/src/distributed_bms.cpp +++ b/services/dbms/src/distributed_bms.cpp @@ -139,6 +139,7 @@ void DistributedBms::OnStop() void DistributedBms::Init() { APP_LOGI("DistributedBms: Init"); + InitDeviceManager(); DistributedDataStorage::GetInstance(); if (distributedSub_ == nullptr) { EventFwk::MatchingSkills matchingSkills; @@ -158,6 +159,14 @@ void DistributedBms::Init() DistributedDataStorage::GetInstance()->UpdateDistributedData(userId); } +void DistributedBms::InitDeviceManager() +{ + if (dbmsDeviceManager_ == nullptr) { + APP_LOGI("Create device manager"); + dbmsDeviceManager_ = std::make_shared(); + } +} + OHOS::sptr DistributedBms::GetBundleMgr() { if (bundleMgr_ == nullptr) { @@ -179,6 +188,15 @@ OHOS::sptr DistributedBms::GetBundleMgr() return bundleMgr_; } +int32_t DistributedBms::GetUdidByNetworkId(const std::string &networkId, std::string &udid) +{ + if (dbmsDeviceManager_ == nullptr) { + APP_LOGI("deviceManager_ is nullptr"); + InitDeviceManager(); + } + return dbmsDeviceManager_->GetUdidByNetworkId(networkId, udid); +} + static OHOS::sptr GetDistributedBundleMgr(const std::string &deviceId) { auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); diff --git a/services/dbms/src/distributed_data_storage.cpp b/services/dbms/src/distributed_data_storage.cpp index c82a1ab9fcd86d25d4bd7792605b180b1b2af3e1..6d1dc6f8731a4d7d2ba547f4640225237059a2a4 100644 --- a/services/dbms/src/distributed_data_storage.cpp +++ b/services/dbms/src/distributed_data_storage.cpp @@ -306,12 +306,12 @@ bool DistributedDataStorage::GetLocalUdid(std::string &udid) int32_t DistributedDataStorage::GetUdidByNetworkId(const std::string &networkId, std::string &udid) { - auto bundleMgr = DelayedSingleton::GetInstance()->GetBundleMgr(); - if (bundleMgr == nullptr) { - APP_LOGE("Get bundleMgr shared_ptr nullptr"); - return -1; + auto dbms = DelayedSingleton::GetInstance(); + if (dbms == nullptr) { + APP_LOGE("dbms is null"); + return Constants::INVALID_UDID; } - return bundleMgr->GetUdidByNetworkId(networkId, udid); + return dbms->GetUdidByNetworkId(networkId, udid); } DistributedBundleInfo DistributedDataStorage::ConvertToDistributedBundleInfo(const BundleInfo &bundleInfo) diff --git a/services/dbms/test/unittest/dbms_services_kit_test/BUILD.gn b/services/dbms/test/unittest/dbms_services_kit_test/BUILD.gn index ddf72d7ef9a37712a5833c8fdc732b8fca13eacf..ae7d58177917276f79d44a5d511729ab996bfcc0 100644 --- a/services/dbms/test/unittest/dbms_services_kit_test/BUILD.gn +++ b/services/dbms/test/unittest/dbms_services_kit_test/BUILD.gn @@ -26,6 +26,7 @@ ohos_unittest("DbmsServicesKitTest") { sources = [ "${dbms_inner_api_path}/src/distributed_bms_proxy.cpp", "${dbms_services_path}/src/account_manager_helper.cpp", + "${dbms_services_path}/src/dbms_device_manager.cpp", "${dbms_services_path}/src/distributed_bms.cpp", "${dbms_services_path}/src/distributed_bms_host.cpp", "${dbms_services_path}/src/distributed_data_storage.cpp", @@ -47,6 +48,7 @@ ohos_unittest("DbmsServicesKitTest") { "bundle_framework:appexecfwk_core", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "device_manager:devicemanagersdk", "hicollie_native:libhicollie", "hiviewdfx_hilog_native:libhilog", "i18n:intl_util", diff --git a/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp b/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp index e147dd43f23eaccf51723315c505e7090aee2fda..0ba2a5f3adfdea629b8966cd0176ab281bc8c97e 100644 --- a/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp +++ b/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp @@ -23,6 +23,7 @@ #include #include "appexecfwk_errors.h" +#include "dbms_device_manager.h" #include "distributed_ability_info.h" #include "distributed_bms.h" #include "distributed_bms_interface.h" @@ -1444,7 +1445,6 @@ HWTEST_F(DbmsServicesKitTest, DbmsServicesKitTest_0074, Function | SmallTest | L } } - /** * @tc.number: DbmsServicesKitTest * @tc.name: test GetDistributedBundleName @@ -1462,4 +1462,43 @@ HWTEST_F(DbmsServicesKitTest, DbmsServicesKitTest_0075, Function | SmallTest | L } } +/** + * @tc.number: InitDeviceManager_0100 + * @tc.name: test InitDeviceManager + * @tc.desc: isInit_ is true, return true. + */ +HWTEST_F(DbmsServicesKitTest, InitDeviceManager_0100, Function | SmallTest | Level0) +{ + DbmsDeviceManager deviceManager; + deviceManager.isInit_ = true; + bool res = deviceManager.InitDeviceManager(); + EXPECT_TRUE(res); +} + +/** + * @tc.number: InitDeviceManager_0200 + * @tc.name: test InitDeviceManager + * @tc.desc: isInit_ is false, return true. + */ +HWTEST_F(DbmsServicesKitTest, InitDeviceManager_0200, Function | SmallTest | Level0) +{ + DbmsDeviceManager deviceManager; + deviceManager.isInit_ = false; + bool res = deviceManager.InitDeviceManager(); + EXPECT_TRUE(res); +} + +/** + * @tc.number: GetUdidByNetworkId_0100 + * @tc.name: test GetUdidByNetworkId + * @tc.desc: GetUdidByNetworkId is false + */ +HWTEST_F(DbmsServicesKitTest, GetUdidByNetworkId_0100, Function | SmallTest | Level0) +{ + DbmsDeviceManager deviceManager; + std::string netWorkId = EMPTY_STRING; + std::string uid = EMPTY_STRING; + auto ret = deviceManager.GetUdidByNetworkId(netWorkId, uid); + EXPECT_FALSE(ret == -1); +} } // OHOS \ No newline at end of file diff --git a/services/dbms/test/unittest/distributed_bms_host_test/BUILD.gn b/services/dbms/test/unittest/distributed_bms_host_test/BUILD.gn index 76bb0cbcce9227c5542822e6a2aa4fd0874ed307..fd327ec0cf64f9b678999761c9576a7af3c51830 100644 --- a/services/dbms/test/unittest/distributed_bms_host_test/BUILD.gn +++ b/services/dbms/test/unittest/distributed_bms_host_test/BUILD.gn @@ -20,7 +20,10 @@ ohos_unittest("DistributedBmsHostTest") { module_out_path = module_output_path include_dirs = [ "${dbms_services_path}/include" ] - sources = [ "${dbms_services_path}/src/distributed_bms_host.cpp" ] + sources = [ + "${dbms_services_path}/src/dbms_device_manager.cpp", + "${dbms_services_path}/src/distributed_bms_host.cpp", + ] sources += [ "distributed_bms_host_test.cpp", @@ -42,6 +45,7 @@ ohos_unittest("DistributedBmsHostTest") { "bundle_framework:appexecfwk_core", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "device_manager:devicemanagersdk", "hicollie_native:libhicollie", "hiviewdfx_hilog_native:libhilog", "i18n:intl_util",