diff --git a/BUILD.gn b/BUILD.gn index 96f042b4294591331c128dde610be3c6c8e7ea53..ae7b066589c7faefea80b41ee16aa025c501f59e 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -65,6 +65,7 @@ if (defined(ohos_lite)) { "interfaces/cj:cj_distributed_device_manager_ffi_group", "interfaces/inner_kits/native_cpp:devicemanagersdk", "interfaces/kits:devicemanager_native_js", + "interfaces/mini_tools_kits/native_cpp:devicemanagerminisdk", ] } group("device_manager_test") { diff --git a/bundle.json b/bundle.json index 1abf4373daa2f5d671aac014acd3984ca3d44b4f..6f03c8187d134548243b3432ab4e40f478c6cf72 100644 --- a/bundle.json +++ b/bundle.json @@ -89,6 +89,15 @@ ], "header_base": "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include" } + }, { + "type": "so", + "name": "//foundation/distributedhardware/device_manager/interfaces/mini_tools_kits/native_cpp:devicemanagerminisdk", + "header": { + "header_files": [ + "device_manager_mini.h" + ], + "header_base": "//foundation/distributedhardware/device_manager/interfaces/mini_tools_kits/native_cpp/include" + } }], "test": [ "//foundation/distributedhardware/device_manager:device_manager_test" diff --git a/common/include/ipc/model/ipc_get_local_display_device_name_req.h b/common/include/ipc/model/ipc_get_local_display_device_name_req.h index 52729cb9e9da511e327130c28c6a5230467f120b..1b9af5b1baf262eab256b3974a01dc96d559eead 100644 --- a/common/include/ipc/model/ipc_get_local_display_device_name_req.h +++ b/common/include/ipc/model/ipc_get_local_display_device_name_req.h @@ -16,7 +16,6 @@ #ifndef OHOS_DM_IPC_GET_LOCAL_DISPLAY_DEVICE_NAME_REQ_H #define OHOS_DM_IPC_GET_LOCAL_DISPLAY_DEVICE_NAME_REQ_H -#include "dm_device_profile_info.h" #include "ipc_req.h" namespace OHOS { diff --git a/common/include/ipc/model/ipc_get_local_display_device_name_rsp.h b/common/include/ipc/model/ipc_get_local_display_device_name_rsp.h index 57868760dc650884499b5745cf93d4ab6c8b6b33..0c79fa34087880c1cd977b3d8b5f9e289fc85e5f 100644 --- a/common/include/ipc/model/ipc_get_local_display_device_name_rsp.h +++ b/common/include/ipc/model/ipc_get_local_display_device_name_rsp.h @@ -16,7 +16,6 @@ #ifndef OHOS_DM_IPC_GET_LOCAL_DISPLAY_DEVICE_NAME_RSP_H #define OHOS_DM_IPC_GET_LOCAL_DISPLAY_DEVICE_NAME_RSP_H -#include "dm_device_profile_info.h" #include "ipc_req.h" namespace OHOS { diff --git a/interfaces/mini_tools_kits/native_cpp/BUILD.gn b/interfaces/mini_tools_kits/native_cpp/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..abe6fe7f9478aa9ba360848c9dfed585af0069c3 --- /dev/null +++ b/interfaces/mini_tools_kits/native_cpp/BUILD.gn @@ -0,0 +1,88 @@ +# 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") + +config("devicemanagerminisdk_config") { + include_dirs = [ + "include", + "include/ipc", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${common_path}/include/ipc/standard", + "${devicemanager_path}/interfaces/inner_kits/native_cpp/include", + "${devicemanager_path}/interfaces/inner_kits/native_cpp/include/ipc", + "${devicemanager_path}/interfaces/inner_kits/native_cpp/include/ipc/standard", + ] +} + +ohos_shared_library("devicemanagerminisdk") { + branch_protector_ret = "pac_ret" + + sanitize = { + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + integer_overflow = true + ubsan = true + } + + cflags = [ + "-Werror", + "-fPIC", + "-fstack-protector-strong", + ] + + ldflags = [ + "-Wl,-z,relro", + "-Wl,-z,now", + ] + + cflags_cc = cflags + + sources = [ + "${common_path}/src/dm_constants.cpp", + "${common_path}/src/ipc/standard/ipc_cmd_register.cpp", + "${devicemanager_path}/interfaces/inner_kits/native_cpp/src/ipc/ipc_client_proxy.cpp", + "${devicemanager_path}/interfaces/inner_kits/native_cpp/src/ipc/standard/dm_service_load.cpp", + "${devicemanager_path}/interfaces/inner_kits/native_cpp/src/ipc/standard/ipc_client_server_proxy.cpp", + "src/device_manager_impl_mini.cpp", + "src/device_manager_mini.cpp", + "src/ipc/ipc_client_manager_mini.cpp", + "src/ipc/ipc_cmd_parser.cpp", + ] + + public_configs = [ ":devicemanagerminisdk_config" ] + + innerapi_tags = [ "platformsdk" ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanagerminikit\"", + "LOG_DOMAIN=0xD004111", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + subsystem_name = "distributedhardware" + + part_name = "device_manager" +} diff --git a/interfaces/mini_tools_kits/native_cpp/include/device_manager_impl_mini.h b/interfaces/mini_tools_kits/native_cpp/include/device_manager_impl_mini.h new file mode 100644 index 0000000000000000000000000000000000000000..110ed2e913d23cfe4e0757a01b6e74347fbe28b3 --- /dev/null +++ b/interfaces/mini_tools_kits/native_cpp/include/device_manager_impl_mini.h @@ -0,0 +1,65 @@ +/* + * 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_MINI_DM_DEVICE_MANAGER_IMPL_MINI_H +#define OHOS_MINI_DM_DEVICE_MANAGER_IMPL_MINI_H + +#include "device_manager_mini.h" + +#include +#include +#include + +#include "ipc_client_manager_mini.h" +#include "ipc_client_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceManagerImplMini : public DeviceManagerMini { +public: + static DeviceManagerImplMini &GetInstance(); + +public: + /** + * @tc.name: DeviceManagerImpl::InitDeviceManager + * @tc.desc: Initialize DeviceManager + * @tc.type: FUNC + */ + virtual int32_t InitDeviceManager(const std::string &pkgName) override; + /** + * @tc.name: DeviceManagerImpl::UnInitDeviceManager + * @tc.desc: UnInitialize DeviceManager + * @tc.type: FUNC + */ + virtual int32_t UnInitDeviceManager(const std::string &pkgName) override; + int32_t OnDmServiceDied(); + virtual int32_t GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength, + std::string &displayName) override; + +private: + DeviceManagerImplMini() = default; + ~DeviceManagerImplMini() = default; + DeviceManagerImplMini(const DeviceManagerImplMini &) = delete; + DeviceManagerImplMini &operator=(const DeviceManagerImplMini &) = delete; + DeviceManagerImplMini(DeviceManagerImplMini &&) = delete; + DeviceManagerImplMini &operator=(DeviceManagerImplMini &&) = delete; + +private: + std::shared_ptr ipcClientProxy_ = + std::make_shared(std::make_shared()); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_MINI_DM_DEVICE_MANAGER_IMPL_MINI_H diff --git a/interfaces/mini_tools_kits/native_cpp/include/device_manager_mini.h b/interfaces/mini_tools_kits/native_cpp/include/device_manager_mini.h new file mode 100644 index 0000000000000000000000000000000000000000..3265391ce9169c69d420878d69dc8834d4cb1451 --- /dev/null +++ b/interfaces/mini_tools_kits/native_cpp/include/device_manager_mini.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_MINI_DM_DEVICE_MANAGER_MINI_H +#define OHOS_MINI_DM_DEVICE_MANAGER_MINI_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +class DeviceManagerMini { +public: + static DeviceManagerMini &GetInstance(); +public: + /** + * @brief Initialize DeviceManager. + * @param pkgName package name. + * @return Returns 0 if success. + */ + virtual int32_t InitDeviceManager(const std::string &pkgName) = 0; + /** + * @brief UnInitialize DeviceManager. + * @param pkgName package name. + * @return Returns 0 if success. + */ + virtual int32_t UnInitDeviceManager(const std::string &pkgName) = 0; + virtual int32_t GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength, + std::string &displayName) = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_MINI_DM_DEVICE_MANAGER_MINI_H diff --git a/interfaces/mini_tools_kits/native_cpp/include/ipc/ipc_client_manager_mini.h b/interfaces/mini_tools_kits/native_cpp/include/ipc/ipc_client_manager_mini.h new file mode 100644 index 0000000000000000000000000000000000000000..188e493d977a2e11465a44358810d08aaa44f0f9 --- /dev/null +++ b/interfaces/mini_tools_kits/native_cpp/include/ipc/ipc_client_manager_mini.h @@ -0,0 +1,93 @@ +/* + * 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_MINI_DM_IPC_CLIENT_MANAGER_H +#define OHOS_MINI_DM_IPC_CLIENT_MANAGER_H + +#include +#include +#include +#include + +#include "ipc_client.h" +#include "ipc_def.h" +#include "ipc_remote_broker.h" +#include "iremote_object.h" +#include "system_ability_status_change_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class DmDeathRecipient : public IRemoteObject::DeathRecipient { +public: + /** + * @tc.name: DmDeathRecipient::OnRemoteDied + * @tc.desc: ipc client initialization + * @tc.type: FUNC + */ + void OnRemoteDied(const wptr &remote) override; + DmDeathRecipient() = default; + ~DmDeathRecipient() override = default; +}; + +class IpcClientManagerMini : public IpcClient { + friend class DmDeathRecipient; + DECLARE_IPC_INTERFACE(IpcClientManagerMini); +public: + class SystemAbilityListener : public SystemAbilityStatusChangeStub { + public: + void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void InitPkgName(const std::string &pkgName) + { + pkgName_ = pkgName; + } + private: + std::string pkgName_ = ""; + }; + +public: + /** + * @tc.name: IpcClientManager::Init + * @tc.desc: ipc client Manager initialization + * @tc.type: FUNC + */ + virtual int32_t Init(const std::string &pkgName) override; + /** + * @tc.name: IpcClientManager::UnInit + * @tc.desc: ipc client Manager Uninitialization + * @tc.type: FUNC + */ + virtual int32_t UnInit(const std::string &pkgName) override; + /** + * @tc.name: IpcClientManager::SendRequest + * @tc.desc: ipc client Manager Send Request + * @tc.type: FUNC + */ + virtual int32_t SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) override; + + virtual int32_t OnDmServiceDied() override; +private: + int32_t ClientInit(); + void SubscribeDMSAChangeListener(const std::string &pkgName); +private: + std::mutex lock_; + sptr dmInterface_ { nullptr }; + sptr dmRecipient_ { nullptr }; + std::atomic isSubscribeDMSAChangeListener = false; + sptr saListenerCallback = nullptr; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_MINI_DM_IPC_CLIENT_MANAGER_H diff --git a/interfaces/mini_tools_kits/native_cpp/src/device_manager_impl_mini.cpp b/interfaces/mini_tools_kits/native_cpp/src/device_manager_impl_mini.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff35fd1066affd3410769ccc58ebfe0e2a41e2ff --- /dev/null +++ b/interfaces/mini_tools_kits/native_cpp/src/device_manager_impl_mini.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_manager_impl_mini.h" + +#include "device_manager_ipc_interface_code.h" +#include "dm_error_type.h" +#include "dm_log.h" +#include "ipc_get_local_display_device_name_req.h" +#include "ipc_get_local_display_device_name_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { +constexpr int32_t SERVICE_INIT_MAX_NUM = 20; +const int32_t USLEEP_TIME_US_100000 = 100000; // 100ms +} +DeviceManagerImplMini &DeviceManagerImplMini::GetInstance() +{ + static DeviceManagerImplMini instance; + return instance; +} + +int32_t DeviceManagerImplMini::InitDeviceManager(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("InitDeviceManager error: Invalid parameter, pkgName: %{public}s", pkgName.c_str()); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("Start, pkgName: %{public}s", pkgName.c_str()); + int32_t ret = DM_OK; + int32_t retryNum = 0; + while (retryNum < SERVICE_INIT_MAX_NUM) { + ret = ipcClientProxy_->Init(pkgName); + if (ret == DM_OK) { + break; + } + usleep(USLEEP_TIME_US_100000); + retryNum++; + if (retryNum == SERVICE_INIT_MAX_NUM) { + LOGE("InitDeviceManager error, wait for device manager service starting timeout."); + return ERR_DM_NOT_INIT; + } + } + if (ret != DM_OK) { + LOGE("InitDeviceManager error, proxy init failed ret: %{public}d", ret); + return ERR_DM_INIT_FAILED; + } + LOGI("Success"); + return DM_OK; +} + +int32_t DeviceManagerImplMini::UnInitDeviceManager(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("UnInitDeviceManager Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("Start, pkgName: %{public}s", pkgName.c_str()); + int32_t ret = ipcClientProxy_->UnInit(pkgName); + if (ret != DM_OK) { + LOGE("UnInitDeviceManager error, proxy unInit failed ret: %{public}d", ret); + return ERR_DM_FAILED; + } + LOGI("Success"); + return DM_OK; +} + +int32_t DeviceManagerImplMini::OnDmServiceDied() +{ + LOGI("Start"); + int32_t ret = ipcClientProxy_->OnDmServiceDied(); + if (ret != DM_OK) { + LOGE("OnDmServiceDied failed, ret: %{public}d", ret); + return ERR_DM_FAILED; + } + return DM_OK; +} + +int32_t DeviceManagerImplMini::GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength, + std::string &displayName) +{ + LOGI("In pkgName:%{public}s,", pkgName.c_str()); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetMaxNameLength(maxNameLength); + int32_t ret = ipcClientProxy_->SendRequest(GET_LOCAL_DISPLAY_DEVICE_NAME, req, rsp); + if (ret != DM_OK) { + return ERR_DM_IPC_SEND_REQUEST_FAILED; + LOGE("error: Send Request failed ret: %{public}d", ret); + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("error: Failed with ret %{public}d", ret); + return ret; + } + displayName = rsp->GetDisplayName(); + LOGI("Completed"); + return DM_OK; +} + +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/mini_tools_kits/native_cpp/src/device_manager_mini.cpp b/interfaces/mini_tools_kits/native_cpp/src/device_manager_mini.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ee7b7e1e1dc3ba6ae9b8e7997de84155e7e298c1 --- /dev/null +++ b/interfaces/mini_tools_kits/native_cpp/src/device_manager_mini.cpp @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_manager_mini.h" + +#include "device_manager_impl_mini.h" + +namespace OHOS { +namespace DistributedHardware { +DeviceManagerMini &DeviceManagerMini::GetInstance() +{ + return DeviceManagerImplMini::GetInstance(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/mini_tools_kits/native_cpp/src/ipc/ipc_client_manager_mini.cpp b/interfaces/mini_tools_kits/native_cpp/src/ipc/ipc_client_manager_mini.cpp new file mode 100644 index 0000000000000000000000000000000000000000..00dd197ae48266a74106f344fa0f393069035d25 --- /dev/null +++ b/interfaces/mini_tools_kits/native_cpp/src/ipc/ipc_client_manager_mini.cpp @@ -0,0 +1,185 @@ +/* + * 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 "ipc_client_manager_mini.h" + +#include +#include + +#include "device_manager_ipc_interface_code.h" +#include "device_manager_impl_mini.h" +#include "dm_error_type.h" +#include "dm_log.h" +#include "dm_service_load.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace DistributedHardware { + +void DmDeathRecipient::OnRemoteDied(const wptr &remote) +{ + LOGW("DmDeathRecipient : OnRemoteDied"); + (void)remote; +} + +int32_t IpcClientManagerMini::ClientInit() +{ + LOGI("Start"); + if (dmInterface_ != nullptr) { + LOGI("DeviceManagerService Already Init"); + return DM_OK; + } + + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + LOGE("Get SystemAbilityManager Failed"); + return ERR_DM_INIT_FAILED; + } + + auto object = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); + if (object == nullptr) { + LOGE("Get DeviceManager SystemAbility Failed"); + DmServiceLoad::GetInstance().LoadDMService(); + return ERR_DM_INIT_FAILED; + } + + if (dmRecipient_ == nullptr) { + dmRecipient_ = sptr(new DmDeathRecipient()); + } + if (!object->AddDeathRecipient(dmRecipient_)) { + LOGE("InitDeviceManagerService: AddDeathRecipient Failed"); + } + dmInterface_ = iface_cast(object); + LOGI("Completed"); + return DM_OK; +} + +int32_t IpcClientManagerMini::Init(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + std::lock_guard autoLock(lock_); + SubscribeDMSAChangeListener(pkgName); + int32_t ret = ClientInit(); + if (ret != DM_OK) { + LOGE("InitDeviceManager Failed with ret %{public}d", ret); + return ret; + } + return DM_OK; +} + +int32_t IpcClientManagerMini::UnInit(const std::string &pkgName) +{ + if (pkgName.empty()) { + LOGE("Invalid parameter, pkgName is empty."); + return ERR_DM_INPUT_PARA_INVALID; + } + LOGI("UnInit in, pkgName %{public}s", pkgName.c_str()); + std::lock_guard autoLock(lock_); + if (dmInterface_ == nullptr) { + LOGE("DeviceManager not Init"); + return ERR_DM_INIT_FAILED; + } + LOGI("completed, pkgName: %{public}s", pkgName.c_str()); + return DM_OK; +} + +int32_t IpcClientManagerMini::SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) { + LOGE("IpcClientManagerMini::SendRequest cmdCode param invalid!"); + return ERR_DM_UNSUPPORTED_IPC_COMMAND; + } + if (req == nullptr || rsp == nullptr) { + return ERR_DM_INPUT_PARA_INVALID; + } + std::lock_guard autoLock(lock_); + if (dmInterface_ != nullptr) { + LOGD("IpcClientManagerMini::SendRequest cmdCode: %{public}d", cmdCode); + return dmInterface_->SendCmd(cmdCode, req, rsp); + } else { + LOGE("dmInterface_ is not init."); + return ERR_DM_INIT_FAILED; + } +} + +int32_t IpcClientManagerMini::OnDmServiceDied() +{ + LOGI("IpcClientManagerMini::OnDmServiceDied begin"); + { + std::lock_guard autoLock(lock_); + if (dmInterface_ == nullptr) { + LOGE("IpcClientManagerMini::OnDmServiceDied, dmInterface_ null"); + return ERR_DM_POINT_NULL; + } + if (dmRecipient_ != nullptr) { + dmInterface_->AsObject()->RemoveDeathRecipient(dmRecipient_); + dmRecipient_ = nullptr; + } + dmInterface_ = nullptr; + } + LOGI("IpcClientManagerMini::OnDmServiceDied complete"); + return DM_OK; +} + +void IpcClientManagerMini::SubscribeDMSAChangeListener(const std::string &pkgName) +{ + saListenerCallback = new (std::nothrow) SystemAbilityListener(); + if (saListenerCallback == nullptr) { + LOGE("saListenerCallback is nullptr."); + return; + } + saListenerCallback->InitPkgName(pkgName); + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + + if (systemAbilityManager == nullptr) { + LOGE("get system ability manager failed."); + return; + } + + if (!isSubscribeDMSAChangeListener.load()) { + LOGI("try subscribe source sa change listener, sa id: %{public}d", DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); + int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, + saListenerCallback); + if (ret != DM_OK) { + LOGE("subscribe source sa change failed: %{public}d", ret); + return; + } + isSubscribeDMSAChangeListener.store(true); + } +} + +void IpcClientManagerMini::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, + const std::string &deviceId) +{ + if (systemAbilityId == DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID) { + DeviceManagerImplMini::GetInstance().OnDmServiceDied(); + } + LOGI("sa %{public}d is removed.", systemAbilityId); +} + +void IpcClientManagerMini::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, + const std::string &deviceId) +{ + LOGI("sa %{public}d is added.", systemAbilityId); + DeviceManagerImplMini::GetInstance().InitDeviceManager(pkgName_); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/mini_tools_kits/native_cpp/src/ipc/ipc_cmd_parser.cpp b/interfaces/mini_tools_kits/native_cpp/src/ipc/ipc_cmd_parser.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5706768be4db96d54bf5759a3aca1f6963604a48 --- /dev/null +++ b/interfaces/mini_tools_kits/native_cpp/src/ipc/ipc_cmd_parser.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "device_manager_ipc_interface_code.h" +#include "dm_constants.h" +#include "dm_log.h" +#include "ipc_cmd_register.h" +#include "ipc_get_local_display_device_name_req.h" +#include "ipc_get_local_display_device_name_rsp.h" +#include "ipc_def.h" +#include "ipc_req.h" +#include "ipc_rsp.h" +#include "message_parcel.h" +#include "parcel.h" + +namespace OHOS { +namespace DistributedHardware { + +ON_IPC_SET_REQUEST(GET_LOCAL_DISPLAY_DEVICE_NAME, std::shared_ptr pBaseReq, MessageParcel &data) +{ + CHECK_NULL_RETURN(pBaseReq, ERR_DM_FAILED); + std::shared_ptr pReq = + std::static_pointer_cast(pBaseReq); + std::string pkgName = pReq->GetPkgName(); + if (!data.WriteString(pkgName)) { + LOGE("write pkgName failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + if (!data.WriteInt32(pReq->GetMaxNameLength())) { + LOGE("write maxNameLength failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_LOCAL_DISPLAY_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->SetDisplayName(reply.ReadString()); + return DM_OK; +} +} // namespace DistributedHardware +} // namespace OHOS