From 30fb38dcdac29c4d8adcdfdf4957cfa7fa46140c Mon Sep 17 00:00:00 2001 From: guoyi Date: Mon, 12 May 2025 18:17:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?dm=E6=A8=A1=E5=9D=97=E6=96=B0=E5=A2=9Endk?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- bundle.json | 10 ++ .../device_manager_ipc_interface_code.h | 1 + common/include/dm_error_type.h | 2 + .../ipc/model/ipc_get_local_device_name_rsp.h | 43 +++++++++ .../native_cpp/include/device_manager.h | 6 ++ .../native_cpp/include/device_manager_impl.h | 1 + .../native_cpp/src/device_manager_impl.cpp | 19 ++++ .../src/ipc/standard/ipc_cmd_parser.cpp | 17 ++++ interfaces/kits/BUILD.gn | 1 + interfaces/kits/ndk/BUILD.gn | 60 ++++++++++++ interfaces/kits/ndk/include/dm_client.h | 45 +++++++++ .../kits/ndk/include/oh_device_manager.h | 66 ++++++++++++++ .../ndk/include/oh_device_manager_err_code.h | 81 +++++++++++++++++ interfaces/kits/ndk/src/dm_client.cpp | 91 +++++++++++++++++++ interfaces/kits/ndk/src/oh_device_manager.cpp | 45 +++++++++ .../service/include/device_manager_service.h | 1 + .../service/src/device_manager_service.cpp | 9 ++ .../src/ipc/standard/ipc_cmd_parser.cpp | 15 +++ .../device_manager_service_fuzzer.cpp | 3 +- test/unittest/UTTest_app_manager.cpp | 21 +++++ .../UTTest_device_manager_impl_two.cpp | 16 +++- .../UTTest_device_manager_service_two.cpp | 7 ++ test/unittest/mock/app_manager_mock.cpp | 5 + test/unittest/mock/app_manager_mock.h | 2 + test/unittest/mock/bundle_mgr_mock.h | 1 + .../unittest/mock/permission_manager_mock.cpp | 1 - utils/include/appInfo/lite/app_manager.h | 3 +- utils/include/appInfo/standard/app_manager.h | 3 +- utils/src/appInfo/lite/app_manager.cpp | 8 +- utils/src/appInfo/standard/app_manager.cpp | 24 ++++- 30 files changed, 600 insertions(+), 7 deletions(-) create mode 100644 common/include/ipc/model/ipc_get_local_device_name_rsp.h create mode 100644 interfaces/kits/ndk/BUILD.gn create mode 100644 interfaces/kits/ndk/include/dm_client.h create mode 100644 interfaces/kits/ndk/include/oh_device_manager.h create mode 100644 interfaces/kits/ndk/include/oh_device_manager_err_code.h create mode 100644 interfaces/kits/ndk/src/dm_client.cpp create mode 100644 interfaces/kits/ndk/src/oh_device_manager.cpp diff --git a/bundle.json b/bundle.json index a00f47d75..aee20468f 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 40d4cfdd7..49709a030 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 2a7c075ae..fd1873e55 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 000000000..ce25dfac5 --- /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 IpcGetLocalDeviceDeviceNameRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGetLocalDeviceDeviceNameRsp); + +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 46f0ff290..5d909a2c5 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 4f8be085e..e864a73aa 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 a2af88d1a..48307cd84 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) { + LOGI("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 e9dafe949..cfa4b6436 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 d8a02b1a0..d3ec01fc9 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 000000000..50b8f6eb9 --- /dev/null +++ b/interfaces/kits/ndk/BUILD.gn @@ -0,0 +1,60 @@ +# 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. + +import("//build/ohos.gni") +import("//foundation/distributedhardware/device_manager/device_manager.gni") + +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", + "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 000000000..1201d76c2 --- /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 000000000..58f8dafef --- /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. Legnth of the localDeviceName. + * @return Returns teh 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 the 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 000000000..cd33a88fe --- /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 000000000..2d5c6273e --- /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 000000000..25c5ff597 --- /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 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 d5f9411db..1c80fe67e 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 a1785e600..f414a0ae4 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 f2340bdee..bea962edc 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 51e187c50..749ed9e22 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 b9901d222..0c1d7c4d3 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 945f117df..2efaca641 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 da77df9cb..bbb9b099d 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 4b063fb4c..aedcc22c1 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 55cdf07b4..1a1f0dbb0 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 005453e1b..97a61db7c 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 84d251e9d..74a7f0d77 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 224ddcebd..ffc506b0a 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 eb5c7a1ca..117fef077 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 598ffc5c5..c44b6f7da 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 25a734ce0..d12914f4b 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 -- Gitee From 0b96fbf46f3508edbe37b190e70e72cbcdf9145f Mon Sep 17 00:00:00 2001 From: guoyi Date: Fri, 16 May 2025 18:59:02 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: guoyi --- .../ipc/model/ipc_get_local_device_name_rsp.h | 4 +- .../native_cpp/src/device_manager_impl.cpp | 2 +- interfaces/kits/ndk/BUILD.gn | 125 ++++++++++++------ .../kits/ndk/include/oh_device_manager.h | 2 +- interfaces/kits/ndk/src/oh_device_manager.cpp | 8 +- 5 files changed, 91 insertions(+), 50 deletions(-) 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 index ce25dfac5..c97c21ac0 100644 --- a/common/include/ipc/model/ipc_get_local_device_name_rsp.h +++ b/common/include/ipc/model/ipc_get_local_device_name_rsp.h @@ -20,8 +20,8 @@ namespace OHOS { namespace DistributedHardware { -class IpcGetLocalDeviceDeviceNameRsp : public IpcRsp { - DECLARE_IPC_MODEL(IpcGetLocalDeviceDeviceNameRsp); +class IpcGetLocalDeviceNameRsp : public IpcRsp { + DECLARE_IPC_MODEL(IpcGetLocalDeviceNameRsp); public: 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 48307cd84..cc1d2e8a9 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp @@ -1467,7 +1467,7 @@ int32_t DeviceManagerImpl::GetLocalDeviceName(std::string &deviceName) } ret = rsp->GetErrCode(); if (ret != DM_OK) { - LOGI("Get local device name failed ret: %{public}d", ret); + LOGE("Get local device name failed ret: %{public}d", ret); return ret; } deviceName = rsp->GetLocalDeviceName(); diff --git a/interfaces/kits/ndk/BUILD.gn b/interfaces/kits/ndk/BUILD.gn index 50b8f6eb9..01786a475 100644 --- a/interfaces/kits/ndk/BUILD.gn +++ b/interfaces/kits/ndk/BUILD.gn @@ -11,50 +11,91 @@ # See the License for the specific language governing permissions and # limitations under the License. -import("//build/ohos.gni") +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") -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 +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", + ] - 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", - "hilog:libhilog", - "ipc:ipc_core", - ] - - relative_install_dir = "ndk" - subsystem_name = "distributedhardware" - part_name = "device_manager" - output_extension = "so" + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + ] + + external_deps = [ + "c_utils:utils", + "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/oh_device_manager.h b/interfaces/kits/ndk/include/oh_device_manager.h index 58f8dafef..1d80ebf98 100644 --- a/interfaces/kits/ndk/include/oh_device_manager.h +++ b/interfaces/kits/ndk/include/oh_device_manager.h @@ -48,7 +48,7 @@ extern "C" { * * @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. Legnth of the localDeviceName. + * @param len This is an output parameter. Length of the localDeviceName. * @return Returns teh 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. diff --git a/interfaces/kits/ndk/src/oh_device_manager.cpp b/interfaces/kits/ndk/src/oh_device_manager.cpp index 25c5ff597..a77411e5e 100644 --- a/interfaces/kits/ndk/src/oh_device_manager.cpp +++ b/interfaces/kits/ndk/src/oh_device_manager.cpp @@ -22,7 +22,7 @@ #include "dm_log.h" #include "oh_device_manager_err_code.h" -int32_t OH_DeviceManager_GetLocalDeviceName(char **localDeviceName, unsigned int &len); +int32_t OH_DeviceManager_GetLocalDeviceName(char **localDeviceName, unsigned int &len) { std::string deviceName = ""; int32_t ret = OHOS::DistributedHardware::DmClient::GetInstance().GetLocalDeviceName(deviceName); @@ -31,14 +31,14 @@ int32_t OH_DeviceManager_GetLocalDeviceName(char **localDeviceName, unsigned int return ret; } len = static_cast(deviceName.size()); - *localDeviceName = new char[len + 1] {0}; - if (localDeviceName == nullptr) { + *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; + delete [] *localDeviceName; return DM_ERR_FAILED; } return ERR_OK; -- Gitee