diff --git a/bundle.json b/bundle.json index a00f47d7568b5e4e641c63016a5d501efbb9e37d..aee20468fc6b1cf0d8f7bd5b4e718fdfa40ba301 100644 --- a/bundle.json +++ b/bundle.json @@ -111,6 +111,16 @@ ], "header_base": "//foundation/distributedhardware/device_manager/json/include" } + }, { + "type": "so", + "name": "//foundation/distributedhardware/device_manager/interfaces/kits/ndk:devicemanager_ndk", + "header": { + "header_files": [ + "oh_device_manager_err_code.h", + "oh_device_manager.h" + ], + "header_base": "//foundation/distributedhardware/device_manager/interfaces/kits/ndk/include" + } }], "test": [ "//foundation/distributedhardware/device_manager:device_manager_test" diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index 40d4cfdd74c00e3beb6715e329379f9aa279cde5..49709a0300bcfc4a2ba599ad245d8a6253cf1483 100644 --- a/common/include/device_manager_ipc_interface_code.h +++ b/common/include/device_manager_ipc_interface_code.h @@ -116,6 +116,7 @@ enum DMIpcCmdInterfaceCode { RESTORE_LOCAL_DEVICE_NAME, GET_DEVICE_NETWORK_ID_LIST, UNREGISTER_PIN_HOLDER_CALLBACK, + GET_LOCAL_DEVICE_NAME, // Add ipc msg here IPC_MSG_BUTT }; diff --git a/common/include/dm_error_type.h b/common/include/dm_error_type.h index 2a7c075ae5c22dbbb997aae7bf27917c627a9075..fd1873e55c76ed498e9f07d59921ddc12f275917 100644 --- a/common/include/dm_error_type.h +++ b/common/include/dm_error_type.h @@ -134,6 +134,8 @@ enum { ERR_DM_GET_TOKENID_FAILED = 969298346, ERR_DM_SHOW_CONFIRM_FAILED = 969298347, ERR_DM_PARSE_MESSAGE_FAILED = 969298348, + ERR_DM_GET_BMS_FAILED = 969298349, + ERR_DM_GET_BUNDLE_NAME_FAILED = 969298349, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/model/ipc_get_local_device_name_rsp.h b/common/include/ipc/model/ipc_get_local_device_name_rsp.h new file mode 100644 index 0000000000000000000000000000000000000000..c97c21ac00bbc903db656ad9d5ccabb3e589a670 --- /dev/null +++ b/common/include/ipc/model/ipc_get_local_device_name_rsp.h @@ -0,0 +1,43 @@ +/* + * 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 OHOS_DM_IPC_GET_LOCAL_DEVICE_NAME_RSP_H +#define OHOS_DM_IPC_GET_LOCAL_DEVICE_NAME_RSP_H + +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetLocalDeviceNameRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGetLocalDeviceNameRsp); + +public: + + const std::string GetLocalDeviceName() const + { + return localDeviceName_; + } + + void SetLocalDeviceName(const std::string &localDeviceName) + { + localDeviceName_ = localDeviceName; + } + +private: + std::string localDeviceName_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GET_LOCAL_DEVICE_NAME_RSP_H diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index 46f0ff290e41bc1417f91ba5e5bea196f5d88292..5d909a2c58a119881baf2a8d12a9b9b693766020 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -342,6 +342,12 @@ public: */ virtual int32_t GetLocalDeviceName(const std::string &pkgName, std::string &deviceName) = 0; + /** + * @brief Get local device name. + * @return Returns device name. + */ + virtual int32_t GetLocalDeviceName(std::string &deviceName) = 0; + /** * @brief Get local device type. * @param pkgName package name. diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h index 4f8be085e27ab27d669ed60387d11000ab38b0df..e864a73aa5037d2866969c20c80d7776dd72a4cd 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -327,6 +327,7 @@ public: virtual int32_t GetLocalDeviceId(const std::string &pkgName, std::string &networkId) override; virtual int32_t GetLocalDeviceType(const std::string &pkgName, int32_t &deviceType) override; virtual int32_t GetLocalDeviceName(const std::string &pkgName, std::string &deviceName) override; + virtual int32_t GetLocalDeviceName(std::string &deviceName) override; virtual int32_t GetDeviceName(const std::string &pkgName, const std::string &networkId, std::string &deviceName) override; virtual int32_t GetDeviceType(const std::string &pkgName, diff --git a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp index a2af88d1a6fc72eef39c98eefeab8b0646b49854..cc1d2e8a9a69e2d6a42216765f6d8f7a70703bba 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -46,6 +46,7 @@ #include "ipc_get_info_by_network_req.h" #include "ipc_get_info_by_network_rsp.h" #include "ipc_get_local_device_info_rsp.h" +#include "ipc_get_local_device_name_rsp.h" #include "ipc_get_local_display_device_name_req.h" #include "ipc_get_local_display_device_name_rsp.h" #include "ipc_get_localserviceinfo_rsp.h" @@ -1455,6 +1456,24 @@ int32_t DeviceManagerImpl::GetLocalDeviceName(const std::string &pkgName, std::s return DM_OK; } +int32_t DeviceManagerImpl::GetLocalDeviceName(std::string &deviceName) +{ + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DEVICE_NAME, req, rsp); + if (ret != DM_OK) { + LOGE("Send Request failed ret: %{public}d", ret); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("Get local device name failed ret: %{public}d", ret); + return ret; + } + deviceName = rsp->GetLocalDeviceName(); + return DM_OK; +} + int32_t DeviceManagerImpl::GetLocalDeviceType(const std::string &pkgName, int32_t &deviceType) { LOGI("Start, pkgName : %{public}s", pkgName.c_str()); diff --git a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp index e9dafe94936942675c38fc51b49307f92363457e..cfa4b64361093155c6cf86eb715ef88dda4f4777 100644 --- a/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp +++ b/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_cmd_parser.cpp @@ -44,6 +44,7 @@ #include "ipc_get_info_by_network_rsp.h" #include "ipc_get_info_by_network_req.h" #include "ipc_get_local_device_info_rsp.h" +#include "ipc_get_local_device_name_rsp.h" #include "ipc_get_local_display_device_name_req.h" #include "ipc_get_local_display_device_name_rsp.h" #include "ipc_get_localserviceinfo_rsp.h" @@ -2142,5 +2143,21 @@ ON_IPC_READ_RESPONSE(UNREGISTER_PIN_HOLDER_CALLBACK, MessageParcel &reply, std:: pRsp->SetErrCode(reply.ReadInt32()); return DM_OK; } + +ON_IPC_SET_REQUEST(GET_LOCAL_DEVICE_NAME, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = std::static_pointer_cast(pBaseReq); + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_LOCAL_DEVICE_NAME, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + CHECK_NULL_RETURN(pBaseRsp, ERR_DM_FAILED); + std::shared_ptr pRsp = std::static_pointer_cast(pBaseRsp); + pRsp->SetErrCode(reply.ReadInt32()); + pRsp->SetLocalDeviceName(reply.ReadString()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/kits/BUILD.gn b/interfaces/kits/BUILD.gn index d8a02b1a0ba34b050d0572963e9daeac8ec7755e..d3ec01fc9ee7613495620df330d0fab0b4e94140 100644 --- a/interfaces/kits/BUILD.gn +++ b/interfaces/kits/BUILD.gn @@ -19,6 +19,7 @@ group("devicemanager_native_js") { deps += [ "./js:devicemanager", "./js4.0:distributeddevicemanager", + "./ndk:devicemanager_ndk", ] } } diff --git a/interfaces/kits/ndk/BUILD.gn b/interfaces/kits/ndk/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..428baf69a66dce4dd5fbbb9984c5725ea5c38156 --- /dev/null +++ b/interfaces/kits/ndk/BUILD.gn @@ -0,0 +1,102 @@ +# 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. + +if (defined(ohos_lite)) { + import("//build/lite/config/component/lite_component.gni") +} else { + import("//build/ohos.gni") +} +import("//foundation/distributedhardware/device_manager/device_manager.gni") + +if (defined(ohos_lite)) { + if (ohos_kernel_type == "liteos_m") { + static_library("devicemanager_ndk") { + include_dirs = [] + + sources = [] + + deps = [] + + cflags = [ + "-Wall", + "-O2", + "-std=c99", + "-Wdate-time", + "-Wextra", + "-Wfloat-equal", + ] + cflags_cc = cflags + } + } else { + shared_library("devicemanager_ndk") { + include_dirs = [] + + sources = [] + + defines = [ + "LITE_DEVICE", + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanager_ndk\"", + "LOG_DOMAIN=0xD004110", + ] + + deps = [] + } + } +} else { + ohos_shared_library("devicemanager_ndk") { + branch_protector_ret = "pac_ret" + sanitize = { + integer_overflow = true + ubsan = true + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + } + + include_dirs = [ + "include", + "${common_path}/include", + "${common_path}/include/ipc", + "${innerkits_path}/native_cpp/include", + ] + sources = [ + "src/dm_client.cpp", + "src/oh_device_manager.cpp", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanager_ndk\"", + "LOG_DOMAIN=0xD004110", + ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "ipc:ipc_core", + ] + + relative_install_dir = "ndk" + subsystem_name = "distributedhardware" + part_name = "device_manager" + output_extension = "so" + } +} diff --git a/interfaces/kits/ndk/include/dm_client.h b/interfaces/kits/ndk/include/dm_client.h new file mode 100644 index 0000000000000000000000000000000000000000..1201d76c2f18cd50e9e2a7ad3ae50944b632a2c1 --- /dev/null +++ b/interfaces/kits/ndk/include/dm_client.h @@ -0,0 +1,45 @@ +/* + * 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 OHOS_NDK_DM_CLIENT_H +#define OHOS_NDK_DM_CLIENT_H + +#include +#include +#include "device_manager_callback.h" + +namespace OHOS { +namespace DistributedHardware { +class DmClient { +public: + static DmClient &GetInstance(); +public: + int32_t Init(); + int32_t UnInit(); + int32_t ReInit(); + int32_t GetLocalDeviceName(std::string &deviceName); +private: + std::mutex initMtx_; + std::string pkgName_ = ""; + std::shared_ptr dmInitCallback_; + +class InitCallback : public DmInitCallback { +public: + void OnRemoteDied() override; +}; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_NDK_DM_CLIENT_H diff --git a/interfaces/kits/ndk/include/oh_device_manager.h b/interfaces/kits/ndk/include/oh_device_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..e4218188e250cef70bcdb723de803a746735aa90 --- /dev/null +++ b/interfaces/kits/ndk/include/oh_device_manager.h @@ -0,0 +1,66 @@ +/* + * 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. + */ + +/** + * @addtogroup DeviceManager + * @{ + * + * @brief Provides APIs to obtain information about trusted devices and local devices. + * + * @since 20 + */ + +/** + * @file oh_device_manager.h + * + * @brief Provides APIs of the DeviceManager module. + * + * @kit DistributedServiceKit + * @library libdevicemanager_ndk.so + * @syscap SystemCapability.DistributedHardware.DeviceManager + * + * @since 20 + */ + +#ifndef OH_DEVICE_MANAGER_H +#define OH_DEVICE_MANAGER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Get local device name. + * + * @param localDeviceName This is an output parameter. It indicates the address pointer of the localDeviceName. + * You need to manually release space resources after using. + * @param len This is an output parameter. Length of the localDeviceName. + * @return Returns the status code of the execution. For detail, see {@link DeviceManager_ErrorCode}. + * Returns {@link ERR_OK}, The operation is successful. + * Returns {@link DM_ERR_FAILED}, Failed to execute the function. + * Returns {@link DM_ERR_OBTAIN_SERVICE}, Failed to obtain devicemanager service. + * Returns {@link DM_ERR_OBTAIN_BUNDLE_NAME}, Failed to obtain the bundleName. + * @since 20 + */ +int32_t OH_DeviceManager_GetLocalDeviceName(char **localDeviceName, unsigned int &len); + +#ifdef __cplusplus +}; +#endif + +/** @} */ +#endif \ No newline at end of file diff --git a/interfaces/kits/ndk/include/oh_device_manager_err_code.h b/interfaces/kits/ndk/include/oh_device_manager_err_code.h new file mode 100644 index 0000000000000000000000000000000000000000..cd33a88fe48a2c2ef4941a88bf754679d633f343 --- /dev/null +++ b/interfaces/kits/ndk/include/oh_device_manager_err_code.h @@ -0,0 +1,81 @@ +/* + * 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. + */ + +/** + * @addtogroup DeviceManager + * @{ + * + * @brief Provides APIs to obtain information about trusted devices and local devices. + * + * @since 20 + */ + +/** + * @file oh_device_manager_err_code.h + * + * @brief Declaration error code information. + * + * @kit DistributedServiceKit + * @library libdevicemanager_ndk.so + * @syscap SystemCapability.DistributedHardware.DeviceManager + * + * @since 20 + */ + + +#ifndef OH_DEVICE_MANAGER_ERR_CODE_H +#define OH_DEVICE_MANAGER_ERR_CODE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enumerates the error codes. + * + * @since 20 + */ +typedef enum DeviceManager_ErrorCode { + /** + * @error The operation is successful. + */ + ERR_OK = 0, + /** + * @error Permission verification failed. + */ + ERR_PERMISSION_ERROR = 201, + /** + * @error Invalid parameter is detected. + */ + ERR_INVALID_PARAMETER = 401, + /** + * @error Failed to execute the function. + */ + DM_ERR_FAILED = 11600101, + /** + * @error Failed to obtain the service. + */ + DM_ERR_OBTAIN_SERVICE = 11600102, + /** + * @error Failed to obtain the bundleName. + */ + DM_ERR_OBTAIN_BUNDLE_NAME = 11600109, +} DeviceManager_ErrorCode; +#ifdef __cplusplus +}; +#endif + +/** @} */ +#endif diff --git a/interfaces/kits/ndk/src/dm_client.cpp b/interfaces/kits/ndk/src/dm_client.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d5c6273e40a7402a385d48b4f922b370f3136a0 --- /dev/null +++ b/interfaces/kits/ndk/src/dm_client.cpp @@ -0,0 +1,91 @@ +/* + * 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 "dm_client.h" + +#include "app_manager.h" +#include "device_manager.h" +#include "dm_error_type.h" +#include "dm_log.h" +#include "oh_device_manager_err_code.h" + +namespace OHOS { +namespace DistributedHardware { +DmClient &DmClient::GetInstance() +{ + static DmClient instance; + return instance; +} + +int32_t DmClient::Init() +{ + std::lock_guard lck(initMtx_); + if (pkgName_.empty()) { + std::string bundleName = ""; + int32_t ret = AppManager::GetInstance().GetBundleNameForSelf(bundleName); + if (ret != DM_OK || bundleName.empty()) { + LOGE("Get bundle name failed, ret=%{public}d", ret); + return DM_ERR_OBTAIN_BUNDLE_NAME; + } + pkgName_ = bundleName; + } + if (dmInitCallback_ == nullptr) { + dmInitCallback_ = std::make_shared(); + } + int32_t ret = DeviceManager::GetInstance().InitDeviceManager(pkgName_, dmInitCallback_); + if (ret != DM_OK) { + LOGE("Init DeviceManager failed, ret=%{public}d", ret); + return DM_ERR_OBTAIN_SERVICE; + } + return ERR_OK; +} + +int32_t DmClient::UnInit() +{ + std::lock_guard lck(initMtx_); + if (dmInitCallback_ != nullptr) { + DeviceManager::GetInstance().UnInitDeviceManager(pkgName_); + } + pkgName_ = ""; + return ERR_OK; +} + +int32_t DmClient::ReInit() +{ + UnInit(); + return Init(); +} + +int32_t DmClient::GetLocalDeviceName(std::string &deviceName) +{ + int32_t ret = Init(); + if (ret != ERR_OK) { + LOGE("Init dm client failed, ret=%{public}d", ret); + return ret; + } + ret = DeviceManager::GetInstance().GetLocalDeviceName(deviceName); + if (ret != DM_OK) { + LOGE("Get local device name failed, ret=%{public}d", ret); + return DM_ERR_OBTAIN_BUNDLE_NAME; + } + return ERR_OK; +} + +void DmClient::InitCallback::OnRemoteDied() +{ + DmClient::GetInstance().ReInit(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/kits/ndk/src/oh_device_manager.cpp b/interfaces/kits/ndk/src/oh_device_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a77411e5e471736568e5e5007581e4a891b1c58d --- /dev/null +++ b/interfaces/kits/ndk/src/oh_device_manager.cpp @@ -0,0 +1,45 @@ +/* + * 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 "oh_device_manager.h" + +#include + +#include "app_manager.h" +#include "dm_client.h" +#include "dm_log.h" +#include "oh_device_manager_err_code.h" + +int32_t OH_DeviceManager_GetLocalDeviceName(char **localDeviceName, unsigned int &len) +{ + std::string deviceName = ""; + int32_t ret = OHOS::DistributedHardware::DmClient::GetInstance().GetLocalDeviceName(deviceName); + if (ret != ERR_OK) { + LOGE("Get local device name failed, ret=%{public}d", ret); + return ret; + } + len = static_cast(deviceName.size()); + *localDeviceName = new (std::nothrow) char[len + 1] {0}; + if (*localDeviceName == nullptr) { + LOGE("create localDeviceName fail"); + return DM_ERR_FAILED; + } + if (strcpy_s(*localDeviceName, len + 1, deviceName.c_str()) != EOK) { + LOGE("copy string fail"); + delete [] *localDeviceName; + return DM_ERR_FAILED; + } + return ERR_OK; +} \ No newline at end of file diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index d5f9411db25e37cb86c18a3b917bb3438de50395..1c80fe67e510f7df8bd32c2de38cebaaabe603de 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -247,6 +247,7 @@ public: int32_t UnRegisterPinHolderCallback(const std::string &pkgName); void ProcessCommonUserStatusEvent(const std::vector &foregroundUserIds, const std::vector &backgroundUserIds, const std::string &remoteUdid); + int32_t GetLocalDeviceName(std::string &deviceName); private: bool IsDMServiceImplReady(); bool IsDMImplSoLoaded(); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index a1785e60012d6d97e71548c7ef72354f3a77b64d..f414a0ae446da801c92580fdc1b3e05951ed04e6 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -3907,5 +3907,14 @@ int32_t DeviceManagerService::UnRegisterPinHolderCallback(const std::string &pkg CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL); return pinHolder_->UnRegisterPinHolderCallback(pkgName); } + +int32_t DeviceManagerService::GetLocalDeviceName(std::string &deviceName) +{ +#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) + return DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(0, deviceName); +#endif + (void) deviceName; + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/ipc/standard/ipc_cmd_parser.cpp b/services/service/src/ipc/standard/ipc_cmd_parser.cpp index f2340bdeecd7caa0041f9323cdad4e0814cad212..bea962edcfdafd15a03999ac73cacaec792f4a55 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -1943,5 +1943,20 @@ ON_IPC_CMD(UNREGISTER_PIN_HOLDER_CALLBACK, MessageParcel &data, MessageParcel &r } return DM_OK; } + +ON_IPC_CMD(GET_LOCAL_DEVICE_NAME, MessageParcel &data, MessageParcel &reply) +{ + std::string deviceName = ""; + int32_t result = DeviceManagerService::GetInstance().GetLocalDeviceName(deviceName); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!reply.WriteString(deviceName)) { + LOGE("write displayName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/servicesfuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.cpp b/test/servicesfuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.cpp index 51e187c50c56a1ecd380d87920ea11b7e01c79bf..749ed9e2224894ab04786ebf16131479d5c4e3c8 100644 --- a/test/servicesfuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.cpp +++ b/test/servicesfuzztest/devicemanagerservice_fuzzer/device_manager_service_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -66,6 +66,7 @@ void DeviceManagerServiceFuzzTest(const uint8_t* data, size_t size) DeviceManagerService::GetInstance().ImportCredential(inputStr, inputStr, inputStr); DeviceManagerService::GetInstance().DeleteCredential(inputStr, inputStr, inputStr); DeviceManagerService::GetInstance().DpAclAdd(inputStr); + DeviceManagerService::GetInstance().GetLocalDeviceName(retStr); } } } diff --git a/test/unittest/UTTest_app_manager.cpp b/test/unittest/UTTest_app_manager.cpp index b9901d222a98c412a8bddcff38e39c31aa6d8383..0c1d7c4d34b53e20f38f11bd4f8e7c3a4f82df00 100644 --- a/test/unittest/UTTest_app_manager.cpp +++ b/test/unittest/UTTest_app_manager.cpp @@ -333,5 +333,26 @@ HWTEST_F(AppManagerTest, GetCallerProcessName_002, testing::ext::TestSize.Level2 auto ret = AppManager::GetInstance().GetCallerProcessName(output); EXPECT_EQ(ret, ERR_DM_FAILED); } + +HWTEST_F(AppManagerTest, GetBundleNameForSelf_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(client_ != nullptr); + auto bundleMgr = sptr(new (std::nothrow) BundleMgrMock()); + auto systemAbilityManager = sptr(new (std::nothrow) SystemAbilityManagerMock()); + EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)) + .Times(INVOKE_COUNT) + .WillOnce(Return(bundleMgr)); + EXPECT_CALL(*client_, GetSystemAbilityManager()) + .Times(INVOKE_COUNT) + .WillOnce(Return(systemAbilityManager)); + EXPECT_CALL(*bundleMgr, GetBundleInfoForSelf(_, _)) + .Times(INVOKE_COUNT) + .WillOnce(Return(ERR_OK)); + + std::string bundleName = ""; + auto result = AppManager::GetInstance().GetBundleNameForSelf(bundleName); + EXPECT_EQ(result, DM_OK); + EXPECT_EQ(bundleName, ""); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_impl_two.cpp b/test/unittest/UTTest_device_manager_impl_two.cpp index 945f117dfb2096fbd67beb360e991e5249105dd0..2efaca6410e4c054a55e3e507503b8ca7d937fd0 100644 --- a/test/unittest/UTTest_device_manager_impl_two.cpp +++ b/test/unittest/UTTest_device_manager_impl_two.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -2076,6 +2076,20 @@ HWTEST_F(DeviceManagerImplTest, GetLocalDisplayDeviceName_201, testing::ext::Tes ret = DeviceManager::GetInstance().GetLocalDisplayDeviceName(pkgName, maxNameLength, displayName); ASSERT_EQ(ret, DM_OK); } + +HWTEST_F(DeviceManagerImplTest, GetLocalDeviceName_201, testing::ext::TestSize.Level0) +{ + std::string deviceName = "deviceName"; + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(ERR_DM_IPC_SEND_REQUEST_FAILED)); + int32_t ret = DeviceManager::GetInstance().GetLocalDeviceName(deviceName); + ASSERT_EQ(ret, ERR_DM_IPC_SEND_REQUEST_FAILED); + + EXPECT_CALL(*ipcClientProxyMock_, SendRequest(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_OK)); + ret = DeviceManager::GetInstance().GetLocalDeviceName(deviceName); + ASSERT_EQ(ret, DM_OK); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service_two.cpp b/test/unittest/UTTest_device_manager_service_two.cpp index da77df9cbdd8b7b47b1131c5c84660b3e6f6725b..bbb9b099d96c563f5572550726b7e35ad801cd23 100644 --- a/test/unittest/UTTest_device_manager_service_two.cpp +++ b/test/unittest/UTTest_device_manager_service_two.cpp @@ -1894,6 +1894,13 @@ HWTEST_F(DeviceManagerServiceTest, HandleShareUnbindBroadCast_003, testing::ext: DeviceManagerService::GetInstance().HandleShareUnbindBroadCast(userId, credId); } + +HWTEST_F(DeviceManagerServiceTest, GetLocalDeviceName_201, testing::ext::TestSize.Level1) +{ + std::string deviceName = ""; + int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceName(deviceName); + EXPECT_EQ(ret, DM_OK); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/mock/app_manager_mock.cpp b/test/unittest/mock/app_manager_mock.cpp index 4b063fb4cf345cc7ff5edc59c6d4e9fcf61d65c2..aedcc22c10067e1a6fd7ee06efe963f709b560f5 100644 --- a/test/unittest/mock/app_manager_mock.cpp +++ b/test/unittest/mock/app_manager_mock.cpp @@ -44,5 +44,10 @@ int32_t AppManager::GetAppIdByPkgName(const std::string &pkgName, std::string &a { return DmAppManager::dmAppManager->GetAppIdByPkgName(pkgName, appId); } + +int32_t AppManager::GetBundleNameForSelf(std::string &bundleName) +{ + return DmAppManager::dmAppManager->GetBundleNameForSelf(bundleName); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/app_manager_mock.h b/test/unittest/mock/app_manager_mock.h index 55cdf07b43bc298e902916332cb5616a602fb9c9..1a1f0dbb0d007dd0948558c0367bf655f63067e5 100644 --- a/test/unittest/mock/app_manager_mock.h +++ b/test/unittest/mock/app_manager_mock.h @@ -32,6 +32,7 @@ public: virtual int32_t GetHapTokenIdByName(int32_t userId, std::string &bundleName, int32_t instIndex, int64_t &tokenId) = 0; virtual int32_t GetAppIdByPkgName(const std::string &pkgName, std::string &appId) = 0; + virtual int32_t GetBundleNameForSelf(std::string &bundleName) = 0; public: static inline std::shared_ptr dmAppManager = nullptr; }; @@ -43,6 +44,7 @@ public: MOCK_METHOD(int32_t, GetNativeTokenIdByName, (std::string &, int64_t &)); MOCK_METHOD(int32_t, GetHapTokenIdByName, (int32_t, std::string &, int32_t, int64_t &)); MOCK_METHOD(int32_t, GetAppIdByPkgName, (const std::string &, std::string &)); + MOCK_METHOD(int32_t, GetBundleNameForSelf, (std::string &)); }; } } diff --git a/test/unittest/mock/bundle_mgr_mock.h b/test/unittest/mock/bundle_mgr_mock.h index 005453e1b338a65f447c85161b7d9dbe9f7d6549..97a61db7ced69e5c3ae303a0bb9c72fcbf67604d 100644 --- a/test/unittest/mock/bundle_mgr_mock.h +++ b/test/unittest/mock/bundle_mgr_mock.h @@ -32,6 +32,7 @@ public: MOCK_METHOD(ErrCode, GetNameForUid, (const int, std::string &)); MOCK_METHOD(ErrCode, GetBundleInfoV9, (const std::string &, int32_t, BundleInfo &, int32_t)); + MOCK_METHOD(ErrCode, GetBundleInfoForSelf, (int32_t, BundleInfo &)); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/test/unittest/mock/permission_manager_mock.cpp b/test/unittest/mock/permission_manager_mock.cpp index 84d251e9dd725f9531967f208dec641cb5920082..74a7f0d77f207ae76a75532ed902356e27d4c96a 100644 --- a/test/unittest/mock/permission_manager_mock.cpp +++ b/test/unittest/mock/permission_manager_mock.cpp @@ -59,6 +59,5 @@ bool PermissionManager::CheckProcessNameValidPutDeviceProfileInfoList(const std: { return DmPermissionManager::dmPermissionManager->CheckProcessNameValidPutDeviceProfileInfoList(processName); } - } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/utils/include/appInfo/lite/app_manager.h b/utils/include/appInfo/lite/app_manager.h index 224ddcebdd01c6c013e22d3d8455851a4db8a95f..ffc506b0a4332055ba6bcb94f31376d2dd12e6db 100644 --- a/utils/include/appInfo/lite/app_manager.h +++ b/utils/include/appInfo/lite/app_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -34,6 +34,7 @@ public: int32_t GetCallerName(bool isSystemSA, std::string &callerName); int32_t GetNativeTokenIdByName(std::string &processName, int64_t &tokenId); int32_t GetHapTokenIdByName(int32_t userId, std::string &bundleName, int32_t instIndex, int64_t &tokenId); + int32_t GetBundleNameForSelf(std::string &bundleName); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/include/appInfo/standard/app_manager.h b/utils/include/appInfo/standard/app_manager.h index eb5c7a1cad06b54f0395fd75a6b1e337904138fa..117fef077c0f44fcfd935151c114af5792e929eb 100644 --- a/utils/include/appInfo/standard/app_manager.h +++ b/utils/include/appInfo/standard/app_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -43,6 +43,7 @@ public: DM_EXPORT int32_t GetHapTokenIdByName(int32_t userId, std::string &bundleName, int32_t instIndex, int64_t &tokenId); DM_EXPORT int32_t GetCallerProcessName(std::string &processName); + DM_EXPORT int32_t GetBundleNameForSelf(std::string &bundleName); private: bool GetBundleManagerProxy(sptr &bundleManager); std::mutex appIdMapLock_; diff --git a/utils/src/appInfo/lite/app_manager.cpp b/utils/src/appInfo/lite/app_manager.cpp index 598ffc5c5a3a0bf33350f705c84be349aa5dc06c..c44b6f7da93e48643a1b55d225e38a0d34620b53 100644 --- a/utils/src/appInfo/lite/app_manager.cpp +++ b/utils/src/appInfo/lite/app_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -73,5 +73,11 @@ int32_t AppManager::GetHapTokenIdByName(int32_t userId, std::string &bundleName, (void)tokenId; return DM_OK; } + +int32_t AppManager::GetBundleNameForSelf(std::string &bundleName) +{ + (void)bundleName; + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/appInfo/standard/app_manager.cpp b/utils/src/appInfo/standard/app_manager.cpp index 25a734ce0bc712b6ad76a3758f39a1054cd9a928..d12914f4bc473d729922b4246a1c9c13c85b7c45 100644 --- a/utils/src/appInfo/standard/app_manager.cpp +++ b/utils/src/appInfo/standard/app_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -251,5 +251,27 @@ DM_EXPORT int32_t AppManager::GetCallerProcessName(std::string &processName) LOGI("Get process name: %{public}s success.", processName.c_str()); return DM_OK; } + +int32_t AppManager::GetBundleNameForSelf(std::string &bundleName) +{ + sptr bundleManager = nullptr; + if (!GetBundleManagerProxy(bundleManager)) { + LOGE("get bundleManager failed."); + return ERR_DM_GET_BMS_FAILED; + } + if (bundleManager == nullptr) { + LOGE("bundleManager is nullptr."); + return ERR_DM_GET_BMS_FAILED; + } + AppExecFwk::BundleInfo bundleInfo; + int32_t flags = static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION); + int32_t ret = static_cast(bundleManager->GetBundleInfoForSelf(flags, bundleInfo)); + if (ret != ERR_OK) { + LOGE("failed, ret=%{public}d.", ret); + return ERR_DM_GET_BUNDLE_NAME_FAILED; + } + bundleName = bundleInfo.name; + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS