From 99f0fc72902e41859f81bf32bd2e6a5b68fbba43 Mon Sep 17 00:00:00 2001 From: guoyi Date: Wed, 5 Feb 2025 20:00:13 +0800 Subject: [PATCH] dm add getDeviceProfileInfoList interface Signed-off-by: guoyi --- .../device_manager_ipc_interface_code.h | 4 +- common/include/dm_constants.h | 12 +- .../ipc_get_device_profile_info_list_req.h | 53 +++++ ..._notify_get_device_profile_info_list_req.h | 62 ++++++ common/include/ipc/model/ipc_req.h | 13 +- common/include/ipc/standard/ipc_model_codec.h | 41 ++++ common/src/ipc/standard/ipc_model_codec.cpp | 199 ++++++++++++++++++ interfaces/inner_kits/native_cpp/BUILD.gn | 3 +- .../native_cpp/include/device_manager.h | 13 +- .../include/device_manager_callback.h | 10 +- .../native_cpp/include/device_manager_impl.h | 6 +- .../native_cpp/include/dm_device_info.h | 16 ++ .../include/dm_device_profile_info.h | 77 +++++++ .../include/notify/device_manager_notify.h | 10 +- .../native_cpp/src/device_manager_impl.cpp | 33 ++- .../src/ipc/standard/ipc_cmd_parser.cpp | 51 +++++ .../src/notify/device_manager_notify.cpp | 48 ++++- .../kits/js4.0/include/dm_native_util.h | 17 ++ .../js4.0/include/native_devicemanager_js.h | 33 ++- interfaces/kits/js4.0/src/dm_native_util.cpp | 150 +++++++++++++ .../js4.0/src/native_devicemanager_js.cpp | 169 ++++++++++++++- services/service/BUILD.gn | 3 +- .../service/include/device_manager_service.h | 2 + .../include/device_manager_service_listener.h | 5 +- .../service/include/i_dm_service_impl_ext.h | 9 + .../idevice_manager_service_listener.h | 5 +- .../service/src/device_manager_service.cpp | 17 +- .../src/device_manager_service_listener.cpp | 15 ++ .../src/ipc/standard/ipc_cmd_parser.cpp | 68 ++++++ test/unittest/UTTest_dm_pin_holder.h | 8 + 30 files changed, 1135 insertions(+), 17 deletions(-) create mode 100644 common/include/ipc/model/ipc_get_device_profile_info_list_req.h create mode 100644 common/include/ipc/model/ipc_notify_get_device_profile_info_list_req.h create mode 100644 common/include/ipc/standard/ipc_model_codec.h create mode 100644 common/src/ipc/standard/ipc_model_codec.cpp create mode 100644 interfaces/inner_kits/native_cpp/include/dm_device_profile_info.h diff --git a/common/include/device_manager_ipc_interface_code.h b/common/include/device_manager_ipc_interface_code.h index ad592b650..a1bb5342e 100644 --- a/common/include/device_manager_ipc_interface_code.h +++ b/common/include/device_manager_ipc_interface_code.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 @@ -97,6 +97,8 @@ enum DMIpcCmdInterfaceCode { GET_DEVICE_SCREEN_STATUS, SERVICE_CREDENTIAL_AUTH_STATUS_NOTIFY, SYNC_CALLBACK, + GET_DEVICE_PROFILE_INFO_LIST, + GET_DEVICE_PROFILE_INFO_LIST_RESULT, IPC_MSG_BUTT }; } // namespace DistributedHardware diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 9de198ac5..90b761d67 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -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 @@ -118,6 +118,12 @@ enum { ERR_DM_CHANNLE_OPEN_TIMEOUT = 96929818, ERR_DM_ADD_GOUTP_TIMEOUT = 96929819, ERR_DM_INPUT_TIMEOUT = 96929820, + ERR_DM_MAX_SIZE_FAIL = 96929825, + ERR_DM_HILINKSVC_RSP_PARSE_FAILD = 96929826, + ERR_DM_HILINKSVC_REPLY_FAILED = 96929827, + ERR_DM_HILINKSVC_ICON_URL_EMPTY = 96929828, + ERR_DM_HILINKSVC_DISCONNECT = 96929829, + ERR_DM_WISE_NEED_LOGIN = 96929830 }; constexpr const char* TAG_GROUP_ID = "groupId"; @@ -255,6 +261,10 @@ const std::map MAP_ERROR_CODE = { { ERR_DM_AUTH_CODE_INCORRECT, 96929796 }, { ERR_DM_BIND_USER_CANCEL_PIN_CODE_DISPLAY, 96929799 }, { ERR_DM_SYNC_DELETE_DEVICE_REPEATED, 96929801 } }; + +// wise device +constexpr int32_t MAX_DEVICE_PROFILE_SIZE = 500; +constexpr int32_t MAX_CONTAINER_SIZE = 500; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_CONSTANTS_H \ No newline at end of file diff --git a/common/include/ipc/model/ipc_get_device_profile_info_list_req.h b/common/include/ipc/model/ipc_get_device_profile_info_list_req.h new file mode 100644 index 000000000..0ea05cf5d --- /dev/null +++ b/common/include/ipc/model/ipc_get_device_profile_info_list_req.h @@ -0,0 +1,53 @@ +/* + * 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_DEVICE_PROFILE_INFO_LIST_REQ_H +#define OHOS_DM_IPC_GET_DEVICE_PROFILE_INFO_LIST_REQ_H + +#include "dm_device_profile_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcGetDeviceProfileInfoListReq : public IpcReq { + DECLARE_IPC_MODEL(IpcGetDeviceProfileInfoListReq); + +public: + /** + * @tc.name: IpcBindTargetReq::GetFirstParam + * @tc.desc: Ipc get first string parameter + * @tc.type: FUNC + */ + DmDeviceProfileInfoFilterOptions GetFilterOptions() const + { + return filterOptions_; + } + + /** + * @tc.name: IpcBindTargetReq::SetFirstParam + * @tc.desc: Ipc set first string parameter + * @tc.type: FUNC + */ + void SetFilterOptions(const DmDeviceProfileInfoFilterOptions &filterOptions) + { + filterOptions_ = filterOptions; + } + +private: + DmDeviceProfileInfoFilterOptions filterOptions_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_GET_DEVICE_PROFILE_INFO_LIST_REQ_H diff --git a/common/include/ipc/model/ipc_notify_get_device_profile_info_list_req.h b/common/include/ipc/model/ipc_notify_get_device_profile_info_list_req.h new file mode 100644 index 000000000..0231e3a62 --- /dev/null +++ b/common/include/ipc/model/ipc_notify_get_device_profile_info_list_req.h @@ -0,0 +1,62 @@ +/* + * 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_NOTIFY_GET_DEVICE_PROFILE_INFO_LIST_REQ_H +#define OHOS_DM_IPC_NOTIFY_GET_DEVICE_PROFILE_INFO_LIST_REQ_H + +#include "dm_device_profile_info.h" +#include "ipc_req.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcNotifyGetDeviceProfileInfoListReq : public IpcReq { + DECLARE_IPC_MODEL(IpcNotifyGetDeviceProfileInfoListReq); + +public: + std::vector GetDeviceProfileInfoList() const + { + return deviceProfileInfoList_; + } + + void SetDeviceProfileInfoList(const std::vector &deviceProfileInfoList) + { + deviceProfileInfoList_ = deviceProfileInfoList; + } + + /** + * @tc.desc: Ipc get result + * @tc.type: FUNC + */ + int32_t GetResult() const + { + return result_; + } + + /** + * @tc.desc: Ipc set result + * @tc.type: FUNC + */ + void SetResult(int32_t result) + { + result_ = result; + } + +private: + std::vector deviceProfileInfoList_ = {}; + int32_t result_ = 0; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_NOTIFY_GET_DEVICE_PROFILE_INFO_LIST_REQ_H diff --git a/common/include/ipc/model/ipc_req.h b/common/include/ipc/model/ipc_req.h index fd0d0c785..f16984c71 100644 --- a/common/include/ipc/model/ipc_req.h +++ b/common/include/ipc/model/ipc_req.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 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 @@ -19,6 +19,7 @@ #include #include "ipc_def.h" +#include "dm_device_info.h" namespace OHOS { namespace DistributedHardware { @@ -36,8 +37,18 @@ public: pkgName_ = pkgName; } + const ProcessInfo GetProcessInfo() const + { + return processInfo_; + } + + void SetProcessInfo(const ProcessInfo &processInfo) + { + processInfo_ = processInfo; + } private: std::string pkgName_; + ProcessInfo processInfo_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/include/ipc/standard/ipc_model_codec.h b/common/include/ipc/standard/ipc_model_codec.h new file mode 100644 index 000000000..910f7c8da --- /dev/null +++ b/common/include/ipc/standard/ipc_model_codec.h @@ -0,0 +1,41 @@ +/* + * 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_MODEL_CODEC_H +#define OHOS_DM_IPC_MODEL_CODEC_H + +#include "dm_device_profile_info.h" +#include "message_parcel.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcModelCodec { +public: + static int32_t DecodeDmDeviceProfileInfoFilterOptions(MessageParcel &parcel, + DmDeviceProfileInfoFilterOptions &filterOptions); + static bool EncodeDmDeviceProfileInfoFilterOptions(const DmDeviceProfileInfoFilterOptions &filterOptions, + MessageParcel &parcel); + static void DecodeDmProductInfo(MessageParcel &parcel, DmProductInfo &prodInfo); + static void DecodeDmServiceProfileInfo(MessageParcel &parcel, DmServiceProfileInfo &svrInfo); + static void DecodeDmServiceProfileInfos(MessageParcel &parcel, std::vector &svrInfos); + static void DecodeDmDeviceProfileInfo(MessageParcel &parcel, DmDeviceProfileInfo &devInfo); + static bool EncodeDmProductInfo(const DmProductInfo &prodInfo, MessageParcel &parcel); + static bool EncodeDmServiceProfileInfo(const DmServiceProfileInfo &svrInfo, MessageParcel &parcel); + static bool EncodeDmServiceProfileInfos(const std::vector &svrInfos, MessageParcel &parcel); + static bool EncodeDmDeviceProfileInfo(const DmDeviceProfileInfo &devInfo, MessageParcel &parcel); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_MODEL_CODEC_H diff --git a/common/src/ipc/standard/ipc_model_codec.cpp b/common/src/ipc/standard/ipc_model_codec.cpp new file mode 100644 index 000000000..48ea686d4 --- /dev/null +++ b/common/src/ipc/standard/ipc_model_codec.cpp @@ -0,0 +1,199 @@ +/* + * 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_model_codec.h" +#include "dm_constants.h" +#include "dm_log.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t IpcModelCodec::DecodeDmDeviceProfileInfoFilterOptions(MessageParcel &parcel, + DmDeviceProfileInfoFilterOptions &filterOptions) +{ + filterOptions.isCloud = parcel.ReadBool(); + size_t size = parcel.ReadUint32(); + if (size > MAX_DEVICE_PROFILE_SIZE) { + LOGE("size more than %{public}d,", MAX_DEVICE_PROFILE_SIZE); + return ERR_DM_IPC_WRITE_FAILED; + } + if (size > 0) { + for (uint32_t i = 0; i < size; i++) { + std::string item = parcel.ReadString(); + filterOptions.deviceIdList.emplace_back(item); + } + } + return DM_OK; +} + +bool IpcModelCodec::EncodeDmDeviceProfileInfoFilterOptions(const DmDeviceProfileInfoFilterOptions &filterOptions, + MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteBool(filterOptions.isCloud)); + uint32_t size = filterOptions.deviceIdList.size(); + bRet = (bRet && parcel.WriteUint32(size)); + if (bRet && size > 0) { + for (const auto& item : filterOptions.deviceIdList) { + bRet = (bRet && parcel.WriteString(item)); + } + } + return bRet; +} + +void IpcModelCodec::DecodeDmProductInfo(MessageParcel &parcel, DmProductInfo &prodInfo) +{ + prodInfo.prodId = parcel.ReadString(); + prodInfo.model = parcel.ReadString(); + prodInfo.prodName = parcel.ReadString(); + prodInfo.prodShortName = parcel.ReadString(); + prodInfo.imageVersion = parcel.ReadString(); +} + +void IpcModelCodec::DecodeDmServiceProfileInfo(MessageParcel &parcel, DmServiceProfileInfo &svrInfo) +{ + svrInfo.deviceId = parcel.ReadString(); + svrInfo.serviceId = parcel.ReadString(); + svrInfo.serviceType = parcel.ReadString(); + int32_t num = parcel.ReadInt32(); + if (num > 0 && num <= MAX_DEVICE_PROFILE_SIZE) { + for (int32_t i = 0; i < num; ++i) { + std::string key = parcel.ReadString(); + std::string value = parcel.ReadString(); + svrInfo.data[key] = value; + } + } +} + +void IpcModelCodec::DecodeDmServiceProfileInfos(MessageParcel &parcel, std::vector &svrInfos) +{ + int32_t svrNum = parcel.ReadInt32(); + if (svrNum > 0 && svrNum <= MAX_DEVICE_PROFILE_SIZE) { + for (int32_t i = 0; i < svrNum; ++i) { + DmServiceProfileInfo svrInfo; + DecodeDmServiceProfileInfo(parcel, svrInfo); + svrInfos.emplace_back(svrInfo); + } + } +} + +void IpcModelCodec::DecodeDmDeviceProfileInfo(MessageParcel &parcel, DmDeviceProfileInfo &devInfo) +{ + devInfo.deviceId = parcel.ReadString(); + devInfo.deviceSn = parcel.ReadString(); + devInfo.mac = parcel.ReadString(); + devInfo.model = parcel.ReadString(); + devInfo.innerModel = parcel.ReadString(); + devInfo.devType = parcel.ReadString(); + devInfo.manu = parcel.ReadString(); + devInfo.deviceName = parcel.ReadString(); + devInfo.prodId = parcel.ReadString(); + devInfo.subProdId = parcel.ReadString(); + devInfo.hiv = parcel.ReadString(); + devInfo.bleMac = parcel.ReadString(); + devInfo.sleMac = parcel.ReadString(); + devInfo.fwv = parcel.ReadString(); + devInfo.hwv = parcel.ReadString(); + devInfo.swv = parcel.ReadString(); + devInfo.protType = parcel.ReadInt32(); + devInfo.setupType = parcel.ReadInt32(); + devInfo.wiseDeviceId = parcel.ReadString(); + devInfo.wiseUserId = parcel.ReadString(); + devInfo.registerTime = parcel.ReadString(); + devInfo.modifyTime = parcel.ReadString(); + devInfo.shareTime = parcel.ReadString(); + devInfo.isLocalDevice = parcel.ReadBool(); + DecodeDmServiceProfileInfos(parcel, devInfo.services); +} + +bool IpcModelCodec::EncodeDmProductInfo(const DmProductInfo &prodInfo, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteString(prodInfo.prodId)); + bRet = (bRet && parcel.WriteString(prodInfo.model)); + bRet = (bRet && parcel.WriteString(prodInfo.prodName)); + bRet = (bRet && parcel.WriteString(prodInfo.prodShortName)); + bRet = (bRet && parcel.WriteString(prodInfo.imageVersion)); + return bRet; +} + +bool IpcModelCodec::EncodeDmServiceProfileInfo(const DmServiceProfileInfo &svrInfo, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteString(svrInfo.deviceId)); + bRet = (bRet && parcel.WriteString(svrInfo.serviceId)); + bRet = (bRet && parcel.WriteString(svrInfo.serviceType)); + if (bRet) { + if (!parcel.WriteInt32((int32_t)svrInfo.data.size())) { + return false; + } + for (const auto &[key, value] : svrInfo.data) { + if (!parcel.WriteString(key)) { + return false; + } + if (!parcel.WriteString(value)) { + return false; + } + } + } + return bRet; +} + +bool IpcModelCodec::EncodeDmServiceProfileInfos(const std::vector &svrInfos, + MessageParcel &parcel) +{ + if (!parcel.WriteInt32((int32_t)svrInfos.size())) { + return false; + } + for (const auto &svrInfo : svrInfos) { + if (!EncodeDmServiceProfileInfo(svrInfo, parcel)) { + LOGE("write dm service profile info failed"); + return false; + } + } + return true; +} + +bool IpcModelCodec::EncodeDmDeviceProfileInfo(const DmDeviceProfileInfo &devInfo, MessageParcel &parcel) +{ + bool bRet = true; + bRet = (bRet && parcel.WriteString(devInfo.deviceId)); + bRet = (bRet && parcel.WriteString(devInfo.deviceSn)); + bRet = (bRet && parcel.WriteString(devInfo.mac)); + bRet = (bRet && parcel.WriteString(devInfo.model)); + bRet = (bRet && parcel.WriteString(devInfo.innerModel)); + bRet = (bRet && parcel.WriteString(devInfo.devType)); + bRet = (bRet && parcel.WriteString(devInfo.manu)); + bRet = (bRet && parcel.WriteString(devInfo.deviceName)); + bRet = (bRet && parcel.WriteString(devInfo.prodId)); + bRet = (bRet && parcel.WriteString(devInfo.subProdId)); + bRet = (bRet && parcel.WriteString(devInfo.hiv)); + bRet = (bRet && parcel.WriteString(devInfo.bleMac)); + bRet = (bRet && parcel.WriteString(devInfo.sleMac)); + bRet = (bRet && parcel.WriteString(devInfo.fwv)); + bRet = (bRet && parcel.WriteString(devInfo.hwv)); + bRet = (bRet && parcel.WriteString(devInfo.swv)); + bRet = (bRet && parcel.WriteInt32(devInfo.protType)); + bRet = (bRet && parcel.WriteInt32(devInfo.setupType)); + bRet = (bRet && parcel.WriteString(devInfo.wiseDeviceId)); + bRet = (bRet && parcel.WriteString(devInfo.wiseUserId)); + bRet = (bRet && parcel.WriteString(devInfo.registerTime)); + bRet = (bRet && parcel.WriteString(devInfo.modifyTime)); + bRet = (bRet && parcel.WriteString(devInfo.shareTime)); + bRet = (bRet && parcel.WriteBool(devInfo.isLocalDevice)); + bRet = (bRet && EncodeDmServiceProfileInfos(devInfo.services, parcel)); + return bRet; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/BUILD.gn b/interfaces/inner_kits/native_cpp/BUILD.gn index d3ebc8029..0012fe338 100644 --- a/interfaces/inner_kits/native_cpp/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/BUILD.gn @@ -1,4 +1,4 @@ -# 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 @@ -153,6 +153,7 @@ if (defined(ohos_lite)) { "${common_path}/src/dfx/standard/dm_hitrace.cpp", "${common_path}/src/dm_anonymous.cpp", "${common_path}/src/ipc/standard/ipc_cmd_register.cpp", + "${common_path}/src/ipc/standard/ipc_model_codec.cpp", "src/device_manager.cpp", "src/device_manager_impl.cpp", "src/ipc/ipc_client_proxy.cpp", diff --git a/interfaces/inner_kits/native_cpp/include/device_manager.h b/interfaces/inner_kits/native_cpp/include/device_manager.h index dc7f7b360..87710f46c 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager.h @@ -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 @@ -23,6 +23,7 @@ #include "device_manager_callback.h" #include "dm_device_info.h" +#include "dm_device_profile_info.h" #include "dm_publish_info.h" #include "dm_subscribe_info.h" @@ -625,6 +626,16 @@ public: virtual int32_t RegisterCredentialAuthStatusCallback(const std::string &pkgName, std::shared_ptr callback) = 0; virtual int32_t UnRegisterCredentialAuthStatusCallback(const std::string &pkgName) = 0; + + /** + * @brief Query the device list of same account. + * @param filterOptions query parameters. + * @param callback qeury result callback. + * @return Returns 0 if success. + */ + virtual int32_t GetDeviceProfileInfoList(const std::string &pkgName, + const DmDeviceProfileInfoFilterOptions &filterOptions, + std::shared_ptr callback) = 0; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h index 747e32edb..db1c9e671 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_callback.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_callback.h @@ -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 @@ -20,6 +20,7 @@ #include #include "dm_device_info.h" +#include "dm_device_profile_info.h" namespace OHOS { namespace DistributedHardware { @@ -113,6 +114,13 @@ public: virtual void OnCall(const std::string ¶mJson) = 0; }; +class GetDeviceProfileInfoListCallback { +public: + virtual ~GetDeviceProfileInfoListCallback() + {} + virtual void OnResult(const std::vector &deviceProfileInfos, int32_t code) = 0; +}; + class CredentialCallback { public: virtual ~CredentialCallback() 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 d7d373df1..f3c06f6e4 100644 --- a/interfaces/inner_kits/native_cpp/include/device_manager_impl.h +++ b/interfaces/inner_kits/native_cpp/include/device_manager_impl.h @@ -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 @@ -401,6 +401,10 @@ public: virtual int32_t UnRegisterCredentialAuthStatusCallback(const std::string &pkgName) override; void SyncCallbacksToService(std::map> &callbackMap); + virtual int32_t GetDeviceProfileInfoList(const std::string &pkgName, + const DmDeviceProfileInfoFilterOptions &filterOptions, + std::shared_ptr callback) override; + private: DeviceManagerImpl() = default; ~DeviceManagerImpl() = default; diff --git a/interfaces/inner_kits/native_cpp/include/dm_device_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_info.h index 2eb65d951..145e8bd89 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -374,6 +374,22 @@ typedef struct DmAccessCallee { int32_t userId; std::string extra; } DmAccessCallee; + +typedef struct ProcessInfo { + int32_t userId; + std::string pkgName; + + bool operator==(const ProcessInfo &other) const + { + return (userId == other.userId) && (pkgName == other.pkgName); + } + + bool operator<(const ProcessInfo &other) const + { + return (userId < other.userId) || + (userId == other.userId && pkgName < other.pkgName); + } +} ProcessInfo; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DEVICE_INFO_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/dm_device_profile_info.h b/interfaces/inner_kits/native_cpp/include/dm_device_profile_info.h new file mode 100644 index 000000000..ab2b60084 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/include/dm_device_profile_info.h @@ -0,0 +1,77 @@ +/* + * 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_DEVICE_PROFILE_INFO_H +#define OHOS_DM_DEVICE_PROFILE_INFO_H + +#include +#include +#include +#include + +namespace OHOS { +namespace DistributedHardware { +typedef struct DmProductInfo { + std::string prodId; + std::string model; + std::string prodName; + std::string prodShortName; + std::string imageVersion; +} DmProductInfo; + +typedef struct DmServiceProfileInfo { + std::string deviceId; + std::string serviceId; + std::string serviceType; + std::map data = {}; +} DmServiceProfileInfo; + +typedef struct DmDeviceProfileInfoFilterOptions { + bool isCloud; + std::vector deviceIdList = {}; +} DmDeviceProfileInfoFilterOptions; + +typedef struct DmDeviceProfileInfo { + std::string deviceId; + std::string deviceSn; + std::string mac; + std::string model; + std::string innerModel; + std::string deviceType; + std::string manufacturer; + std::string deviceName; + std::string productId; + std::string subProductId; + std::string sdkVersion; + std::string bleMac; + std::string brMac; + std::string sleMac; + std::string firmwareVersion; + std::string hardwareVersion; + std::string softwareVersion; + int32_t protocolType; + int32_t setupType; + std::string wiseDeviceId; + std::string wiseUserId; + std::string registerTime; + std::string modifyTime; + std::string shareTime; + bool isLocalDevice; + std::vector services = {}; +} DmDeviceProfileInfo; + +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_DEVICE_PROFILE_INFO_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h index 0f183ecc3..dba223901 100644 --- a/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h +++ b/interfaces/inner_kits/native_cpp/include/notify/device_manager_notify.h @@ -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 @@ -27,6 +27,7 @@ #include "device_manager_callback.h" #include "dm_device_info.h" +#include "dm_device_profile_info.h" #include "dm_subscribe_info.h" #include "dm_single_instance.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) @@ -74,6 +75,12 @@ public: std::shared_ptr callback); void UnRegisterCredentialAuthStatusCallback(const std::string &pkgName); + int32_t RegisterGetDeviceProfileInfoListCallback(const std::string &pkgName, + std::shared_ptr callback); + int32_t UnRegisterGetDeviceProfileInfoListCallback(const std::string &pkgName); + void OnGetDeviceProfileInfoListResult(const std::string &pkgName, + const std::vector &deviceProfileInfos, int32_t code); + public: static void DeviceInfoOnline(const DmDeviceInfo &deviceInfo, std::shared_ptr tempCbk); static void DeviceInfoOffline(const DmDeviceInfo &deviceInfo, std::shared_ptr tempCbk); @@ -137,6 +144,7 @@ private: std::map> deviceScreenStatusCallback_; std::map> credentialAuthStatusCallback_; std::mutex bindLock_; + std::map> getDeviceProfileInfoCallback_; }; } // namespace DistributedHardware } // namespace OHOS 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 0b701dbd7..c98e07ab8 100644 --- a/interfaces/inner_kits/native_cpp/src/device_manager_impl.cpp +++ b/interfaces/inner_kits/native_cpp/src/device_manager_impl.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 @@ -36,6 +36,7 @@ #include "ipc_export_auth_code_rsp.h" #include "ipc_generate_encrypted_uuid_req.h" #include "ipc_get_device_info_rsp.h" +#include "ipc_get_device_profile_info_list_req.h" #include "ipc_get_device_screen_status_req.h" #include "ipc_get_device_screen_status_rsp.h" #include "ipc_get_encrypted_uuid_req.h" @@ -62,6 +63,7 @@ #include "ipc_unpublish_req.h" #include "securec.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) +#include "ipc_model_codec.h" #include "iservice_registry.h" #include "system_ability_definition.h" #endif @@ -2358,5 +2360,34 @@ void DeviceManagerImpl::SyncCallbacksToService(std::map callback) +{ + LOGI("In pkgName:%{public}s, isCloud:%{public}d", pkgName.c_str(), filterOptions.isCloud); + int32_t ret = DeviceManagerNotify::GetInstance().RegisterGetDeviceProfileInfoListCallback(pkgName, callback); + if (ret != DM_OK) { + LOGE("register callback error, ret: %{public}d", ret); + return ret; + } + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + req->SetFilterOptions(filterOptions); + ret = ipcClientProxy_->SendRequest(GET_DEVICE_PROFILE_INFO_LIST, req, rsp); + if (ret != DM_OK) { + LOGE("error: Send Request failed ret: %{public}d", ret); + DeviceManagerNotify::GetInstance().UnRegisterGetDeviceProfileInfoListCallback(pkgName); + return ERR_DM_IPC_SEND_REQUEST_FAILED; + } + ret = rsp->GetErrCode(); + if (ret != DM_OK) { + LOGE("error: Failed with ret %{public}d", ret); + DeviceManagerNotify::GetInstance().UnRegisterGetDeviceProfileInfoListCallback(pkgName); + return ret; + } + LOGI("Completed"); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file 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 c1b939b36..3a1990cbb 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 @@ -18,6 +18,7 @@ #include "dm_anonymous.h" #include "dm_constants.h" #include "dm_device_info.h" +#include "dm_device_profile_info.h" #include "dm_log.h" #include "ipc_acl_profile_req.h" #include "ipc_authenticate_device_req.h" @@ -41,6 +42,7 @@ #include "ipc_get_trustdevice_req.h" #include "ipc_get_trustdevice_rsp.h" #include "ipc_import_auth_code_req.h" +#include "ipc_model_codec.h" #include "ipc_notify_event_req.h" #include "ipc_register_listener_req.h" #include "ipc_req.h" @@ -1751,5 +1753,54 @@ ON_IPC_READ_RESPONSE(SYNC_CALLBACK, MessageParcel &reply, std::shared_ptrSetErrCode(reply.ReadInt32()); return DM_OK; } + +ON_IPC_SET_REQUEST(GET_DEVICE_PROFILE_INFO_LIST, std::shared_ptr pBaseReq, MessageParcel &data) +{ + if (pBaseReq == nullptr) { + LOGE("pBaseReq is null"); + return 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 (!IpcModelCodec::EncodeDmDeviceProfileInfoFilterOptions(pReq->GetFilterOptions(), data)) { + LOGE("write filterOptions failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_DEVICE_PROFILE_INFO_LIST, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} + +ON_IPC_CMD(GET_DEVICE_PROFILE_INFO_LIST_RESULT, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + int32_t code = data.ReadInt32(); + int32_t deviceNum = data.ReadInt32(); + std::vector deviceProfileInfos; + if (deviceNum > 0 && deviceNum <= MAX_DEVICE_PROFILE_SIZE) { + for (int32_t i = 0; i < deviceNum; ++i) { + DmDeviceProfileInfo deviceInfo; + IpcModelCodec::DecodeDmDeviceProfileInfo(data, deviceInfo); + deviceProfileInfos.emplace_back(deviceInfo); + } + } + + DeviceManagerNotify::GetInstance().OnGetDeviceProfileInfoListResult(pkgName, deviceProfileInfos, code); + reply.WriteInt32(DM_OK); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp index 00ab9b31d..38deceb24 100644 --- a/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.cpp +++ b/interfaces/inner_kits/native_cpp/src/notify/device_manager_notify.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 @@ -1175,5 +1175,51 @@ void DeviceManagerNotify::GetCallBack(std::map callback) +{ + LOGI("In, pkgName: %{public}s.", pkgName.c_str()); + std::lock_guard autoLock(bindLock_); + if (getDeviceProfileInfoCallback_.size() > MAX_CONTAINER_SIZE) { + LOGI("callback map size is more than max size"); + return ERR_DM_MAX_SIZE_FAIL; + } + getDeviceProfileInfoCallback_[pkgName] = callback; + return DM_OK; +} + +int32_t DeviceManagerNotify::UnRegisterGetDeviceProfileInfoListCallback(const std::string &pkgName) +{ + LOGI("In, pkgName: %{public}s.", pkgName.c_str()); + std::lock_guard autoLock(bindLock_); + getDeviceProfileInfoCallback_.erase(pkgName); + return DM_OK; +} + +void DeviceManagerNotify::OnGetDeviceProfileInfoListResult(const std::string &pkgName, + const std::vector &deviceProfileInfos, int32_t code) +{ + if (pkgName.empty()) { + LOGE("Invalid para, pkgName: %{public}s.", pkgName.c_str()); + return; + } + LOGI("In, pkgName:%{public}s, code:%{public}d", pkgName.c_str(), code); + std::shared_ptr tempCbk; + { + std::lock_guard autoLock(bindLock_); + if (getDeviceProfileInfoCallback_.count(pkgName) == 0) { + LOGE("error, callback not register for pkgName %{public}s.", pkgName.c_str()); + return; + } + tempCbk = getDeviceProfileInfoCallback_[pkgName]; + getDeviceProfileInfoCallback_.erase(pkgName); + } + if (tempCbk == nullptr) { + LOGE("error, registered GetDeviceProfileInfoList callback is nullptr."); + return; + } + tempCbk->OnResult(deviceProfileInfos, code); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/kits/js4.0/include/dm_native_util.h b/interfaces/kits/js4.0/include/dm_native_util.h index c4d97572d..ef778b01b 100644 --- a/interfaces/kits/js4.0/include/dm_native_util.h +++ b/interfaces/kits/js4.0/include/dm_native_util.h @@ -22,6 +22,7 @@ #include #include "dm_device_info.h" +#include "dm_device_profile_info.h" #include "dm_publish_info.h" #include "napi/native_api.h" #include "napi/native_node_api.h" @@ -46,6 +47,10 @@ enum DMBussinessErrorCode { DM_ERR_DISCOVERY_INVALID = 11600104, // Publish invalid. DM_ERR_PUBLISH_INVALID = 11600105, + // Get data from cloud failed. + DM_ERR_FROM_CLOUD_FAILED = 11600106, + // Need Login. + DM_ERR_NEED_LOGIN = 11600107, }; void DeviceBasicInfoToJsArray(const napi_env &env, const std::vector &vecDevInfo, @@ -67,6 +72,18 @@ bool CheckArgsType(napi_env env, bool assertion, const std::string ¶mName, c bool IsFunctionType(napi_env env, napi_value value); void DmDeviceBasicToJsObject(napi_env env, const DmDeviceBasicInfo &vecDevInfo, napi_value &result); bool JsToStringAndCheck(napi_env env, napi_value value, const std::string &valueName, std::string &strValue); + +void JsObjectToStrVector(const napi_env &env, const napi_value &object, const std::string &fieldStr, + std::vector &fieldRef); +void JsToDmDeviceProfileInfoFilterOptions(const napi_env &env, const napi_value &object, + DmDeviceProfileInfoFilterOptions &info); +void DmServiceProfileInfoToJsArray(const napi_env &env, const std::vector &svrInfos, + napi_value &arrayResult); +void DmProductInfoToJs(const napi_env &env, const DmProductInfo &prodInfos, napi_value &jsObj); +void DmDeviceProfileInfoToJs(const napi_env &env, const DmDeviceProfileInfo &devInfo, napi_value &jsObj); +void DmDeviceProfileInfoToJsArray(const napi_env &env, const std::vector &devInfos, + napi_value &arrayResult); + } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_NATIVE_UTIL_H diff --git a/interfaces/kits/js4.0/include/native_devicemanager_js.h b/interfaces/kits/js4.0/include/native_devicemanager_js.h index 2d1faa73f..c20ed99e5 100644 --- a/interfaces/kits/js4.0/include/native_devicemanager_js.h +++ b/interfaces/kits/js4.0/include/native_devicemanager_js.h @@ -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 @@ -23,6 +23,7 @@ #include "device_manager_callback.h" #include "dm_app_image_info.h" #include "dm_device_info.h" +#include "dm_device_profile_info.h" #include "dm_native_event.h" #include "dm_subscribe_info.h" #include "dm_publish_info.h" @@ -61,6 +62,16 @@ struct DeviceBasicInfoListAsyncCallbackInfo { int32_t ret = 0; }; +struct DeviceProfileInfosAsyncCallbackInfo { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + std::string bundleName; + napi_deferred deferred = nullptr; + int32_t code = -1; + OHOS::DistributedHardware::DmDeviceProfileInfoFilterOptions filterOptions; + std::vector deviceProfileInfos; +}; + struct AuthAsyncCallbackInfo { napi_env env = nullptr; @@ -212,6 +223,23 @@ private: std::string bundleName_; }; +class DmNapiGetDeviceProfileInfoListCallback : public OHOS::DistributedHardware::GetDeviceProfileInfoListCallback { +public: + explicit DmNapiGetDeviceProfileInfoListCallback(napi_env env, const std::string &bundleName, + const napi_deferred &deferred) : env_(env), bundleName_(bundleName), deferred_(deferred) + { + } + ~DmNapiGetDeviceProfileInfoListCallback() override {}; + + void OnResult(const std::vector &deviceProfileInfos, + int32_t code) override; + +private: + napi_env env_; + std::string bundleName_; + napi_deferred deferred_ = nullptr; +}; + class DeviceManagerNapi : public DmNativeEvent { public: explicit DeviceManagerNapi(napi_env env, napi_value thisVar); @@ -239,6 +267,7 @@ public: static napi_value UnBindTarget(napi_env env, napi_callback_info info); static napi_value JsOn(napi_env env, napi_callback_info info); static napi_value JsOff(napi_env env, napi_callback_info info); + static napi_value JsGetDeviceProfileInfoList(napi_env env, napi_callback_info info); static DeviceManagerNapi *GetDeviceManagerNapi(std::string &bundleName); static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType); static void CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType, std::string &extra); @@ -250,6 +279,7 @@ public: void OnPublishResult(int32_t publishId, int32_t publishResult); void OnAuthResult(const std::string &deviceId, const std::string &token, int32_t status, int32_t reason); void OnDmUiCall(const std::string ¶mJson); + void OnGetDeviceProfileInfoListCallbackResult(DeviceProfileInfosAsyncCallbackInfo *jsCallback); private: static void ReleasePublishCallback(std::string &bundleName); @@ -271,6 +301,7 @@ private: static int32_t BindTargetWarpper(const std::string &pkgName, const std::string &deviceId, const std::string &bindParam, std::shared_ptr callback); static void RegisterDevStatusCallback(napi_env env, std::string &bundleName); + static napi_value GetDeviceProfileInfoListPromise(napi_env env, DeviceProfileInfosAsyncCallbackInfo *jsCallback); private: napi_env env_; diff --git a/interfaces/kits/js4.0/src/dm_native_util.cpp b/interfaces/kits/js4.0/src/dm_native_util.cpp index d4f56c138..81e229d36 100644 --- a/interfaces/kits/js4.0/src/dm_native_util.cpp +++ b/interfaces/kits/js4.0/src/dm_native_util.cpp @@ -37,6 +37,8 @@ const std::string ERR_MESSAGE_OBTAIN_SERVICE = "Failed to obtain the service."; const std::string ERR_MESSAGE_AUTHENTICALTION_INVALID = "Authentication unavailable."; const std::string ERR_MESSAGE_DISCOVERY_INVALID = "Discovery unavailable."; const std::string ERR_MESSAGE_PUBLISH_INVALID = "Publish unavailable."; +const std::string ERR_MESSAGE_FROM_CLOUD_FAILED = "Get data from cloud failed."; +const std::string ERR_MESSAGE_NEED_LOGIN = "A login account is required."; const int32_t DM_NAPI_DISCOVER_EXTRA_INIT_ONE = -1; const int32_t DM_NAPI_DISCOVER_EXTRA_INIT_TWO = -2; @@ -81,6 +83,8 @@ void JsObjectToBool(const napi_env &env, const napi_value &object, const std::st napi_get_value_bool(env, field, &fieldRef); } else { LOGE("devicemanager napi js to bool no property: %{public}s", fieldStr.c_str()); + std::string errMsg = ERR_MESSAGE_INVALID_PARAMS + " no property " + fieldStr; + napi_throw_error(env, std::to_string(ERR_INVALID_PARAMS).c_str(), errMsg.c_str()); } } @@ -203,6 +207,7 @@ napi_value CreateBusinessError(napi_env env, int32_t errCode, bool isAsync) break; case ERR_DM_INPUT_PARA_INVALID: case ERR_DM_UNSUPPORTED_AUTH_TYPE: + case ERR_DM_MAX_SIZE_FAIL: error = CreateErrorForCall(env, ERR_INVALID_PARAMS, ERR_MESSAGE_INVALID_PARAMS, isAsync); break; case ERR_DM_INIT_FAILED: @@ -211,6 +216,15 @@ napi_value CreateBusinessError(napi_env env, int32_t errCode, bool isAsync) case ERR_NOT_SYSTEM_APP: error = CreateErrorForCall(env, ERR_NOT_SYSTEM_APP, ERR_MESSAGE_NOT_SYSTEM_APP, isAsync); break; + case ERR_DM_HILINKSVC_RSP_PARSE_FAILD: + case ERR_DM_HILINKSVC_REPLY_FAILED: + case ERR_DM_HILINKSVC_ICON_URL_EMPTY: + case ERR_DM_HILINKSVC_DISCONNECT: + error = CreateErrorForCall(env, DM_ERR_FROM_CLOUD_FAILED, ERR_MESSAGE_FROM_CLOUD_FAILED, isAsync); + break; + case ERR_DM_WISE_NEED_LOGIN: + error = CreateErrorForCall(env, DM_ERR_NEED_LOGIN, ERR_MESSAGE_NEED_LOGIN, isAsync); + break; default: error = CreateErrorForCall(env, DM_ERR_FAILED, ERR_MESSAGE_FAILED, isAsync); break; @@ -452,5 +466,141 @@ bool JsToStringAndCheck(napi_env env, napi_value value, const std::string &value strValue = temp; return true; } + +void JsObjectToStrVector(const napi_env &env, const napi_value &object, const std::string &fieldStr, + std::vector &fieldRef) +{ + bool hasProperty = false; + NAPI_CALL_RETURN_VOID(env, napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty)); + if (!hasProperty) { + LOGE("no property: %{public}s", fieldStr.c_str()); + return; + } + + napi_value field = nullptr; + napi_get_named_property(env, object, fieldStr.c_str(), &field); + bool isArr = false; + napi_is_array(env, field, &isArr); + if (!isArr) { + LOGE("property: %{public}s is not array", fieldStr.c_str()); + return; + } + uint32_t length = 0; + napi_get_array_length(env, field, &length); + for (size_t i = 0; i < length; i++) { + napi_value element; + napi_get_element(env, field, i, &element); + size_t strLen = 0; + napi_get_value_string_utf8(env, element, nullptr, 0, &strLen); + if (strLen == 0) { + continue; + } + char buf[DEVICE_UUID_LENGTH] = {0}; + napi_get_value_string_utf8(env, element, buf, strLen + 1, &strLen); + fieldRef.emplace_back(buf); + } +} + +void JsToDmDeviceProfileInfoFilterOptions(const napi_env &env, const napi_value &object, + DmDeviceProfileInfoFilterOptions &info) +{ + napi_valuetype filterOptionsType; + napi_typeof(env, object, &filterOptionsType); + if (filterOptionsType != napi_object) { + LOGE("filterOptions is not object"); + std::string errMsg = ERR_MESSAGE_INVALID_PARAMS + " The type of filterOptions must be object"; + napi_throw_error(env, std::to_string(ERR_INVALID_PARAMS).c_str(), errMsg.c_str()); + return; + } + bool isCloud = false; + JsObjectToBool(env, object, "isCloud", isCloud); + info.isCloud = isCloud; + std::vector deviceIdList; + JsObjectToStrVector(env, object, "deviceIdList", deviceIdList); + info.deviceIdList = deviceIdList; +} + +void DmServiceProfileInfoToJsArray(const napi_env &env, const std::vector &svrInfos, + napi_value &arrayResult) +{ + for (unsigned int i = 0; i < svrInfos.size(); i++) { + napi_value item = nullptr; + napi_create_object(env, &item); + SetValueUtf8String(env, "deviceId", svrInfos[i].deviceId, item); + SetValueUtf8String(env, "serviceId", svrInfos[i].serviceId, item); + SetValueUtf8String(env, "serviceType", svrInfos[i].serviceType, item); + napi_value data = nullptr; + napi_create_object(env, &data); + for (const auto& [key, value] : svrInfos[i].data) { + SetValueUtf8String(env, key, value, data); + } + napi_set_named_property(env, item, "data", data); + napi_status status = napi_set_element(env, arrayResult, i, item); + if (status != napi_ok) { + LOGE("DmServiceProfileInfoToJsArray To JsArray set element error: %{public}d", status); + } + } +} + +void DmProductInfoToJs(const napi_env &env, const DmProductInfo &prodInfo, napi_value &jsObj) +{ + SetValueUtf8String(env, "prodId", prodInfo.prodId, jsObj); + SetValueUtf8String(env, "model", prodInfo.model, jsObj); + SetValueUtf8String(env, "prodName", prodInfo.prodName, jsObj); + SetValueUtf8String(env, "prodShortName", prodInfo.prodShortName, jsObj); +} + +void DmDeviceProfileInfoToJs(const napi_env &env, const DmDeviceProfileInfo &devInfo, napi_value &jsObj) +{ + SetValueUtf8String(env, "deviceId", devInfo.deviceId, jsObj); + SetValueUtf8String(env, "deviceSn", devInfo.deviceSn, jsObj); + SetValueUtf8String(env, "mac", devInfo.mac, jsObj); + SetValueUtf8String(env, "model", devInfo.model, jsObj); + SetValueUtf8String(env, "deviceType", devInfo.deviceType, jsObj); + SetValueUtf8String(env, "manufacturer", devInfo.manufacturer, jsObj); + SetValueUtf8String(env, "deviceName", devInfo.deviceName, jsObj); + SetValueUtf8String(env, "productId", devInfo.productId, jsObj); + SetValueUtf8String(env, "subProductId", devInfo.subProductId, jsObj); + SetValueUtf8String(env, "sdkVersion", devInfo.sdkVersion, jsObj); + SetValueUtf8String(env, "bleMac", devInfo.bleMac, jsObj); + SetValueUtf8String(env, "brMac", devInfo.brMac, jsObj); + SetValueUtf8String(env, "sleMac", devInfo.sleMac, jsObj); + SetValueUtf8String(env, "firmwareVersion", devInfo.firmwareVersion, jsObj); + SetValueUtf8String(env, "hardwareVersion", devInfo.hardwareVersion, jsObj); + SetValueUtf8String(env, "softwareVersion", devInfo.softwareVersion, jsObj); + SetValueInt32(env, "protocolType", devInfo.protocolType, jsObj); + SetValueInt32(env, "setupType", devInfo.setupType, jsObj); + SetValueUtf8String(env, "wiseDeviceId", devInfo.wiseDeviceId, jsObj); + SetValueUtf8String(env, "wiseUserId", devInfo.wiseUserId, jsObj); + SetValueUtf8String(env, "registerTime", devInfo.registerTime, jsObj); + SetValueUtf8String(env, "modifyTime", devInfo.modifyTime, jsObj); + SetValueUtf8String(env, "shareTime", devInfo.shareTime, jsObj); + SetValueInt32(env, "isLocalDevice", devInfo.isLocalDevice ? 1 : 0, jsObj); +} + +void DmDeviceProfileInfoToJsArray(const napi_env &env, const std::vector &devInfos, + napi_value &arrayResult) +{ + for (unsigned int i = 0; i < devInfos.size(); i++) { + napi_value item = nullptr; + napi_create_object(env, &item); + DmDeviceProfileInfoToJs(env, devInfos[i], item); + if (!devInfos[i].services.empty()) { + napi_value services = nullptr; + napi_create_array(env, &services); + bool isArray = false; + napi_is_array(env, services, &isArray); + if (!isArray) { + LOGE("napi_create_array failed"); + } + DmServiceProfileInfoToJsArray(env, devInfos[i].services, services); + napi_set_named_property(env, item, "services", services); + } + napi_status status = napi_set_element(env, arrayResult, i, item); + if (status != napi_ok) { + LOGE("DmDeviceProfileInfo To JsArray set element error: %{public}d", status); + } + } +} } // namespace DistributedHardware } // namespace OHOS diff --git a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp b/interfaces/kits/js4.0/src/native_devicemanager_js.cpp index cdc023721..3bb9dce6a 100644 --- a/interfaces/kits/js4.0/src/native_devicemanager_js.cpp +++ b/interfaces/kits/js4.0/src/native_devicemanager_js.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 @@ -23,6 +23,7 @@ #include "device_manager.h" #include "dm_constants.h" #include "dm_device_info.h" +#include "dm_device_profile_info.h" #include "dm_log.h" #include "dm_native_util.h" #include "ipc_skeleton.h" @@ -539,6 +540,56 @@ void DmNapiAuthenticateCallback::OnAuthResult(const std::string &deviceId, const } } +void DmNapiGetDeviceProfileInfoListCallback::OnResult(const std::vector &deviceProfileInfos, + int32_t code) +{ + LOGI("In code:%{public}d, size:%{public}zu", code, deviceProfileInfos.size()); + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(env_, &loop); + if (loop == nullptr) { + LOGE("get loop fail"); + return; + } + uv_work_t *work = new (std::nothrow) uv_work_t; + if (work == nullptr) { + LOGE("OnResult, No memory"); + return; + } + auto *jsCallback = new DeviceProfileInfosAsyncCallbackInfo(); + if (jsCallback == nullptr) { + LOGE("create jsCallback fail"); + DeleteUvWork(work); + return; + } + jsCallback->env = env_; + jsCallback->bundleName = bundleName_; + jsCallback->deferred = deferred_; + jsCallback->deviceProfileInfos = deviceProfileInfos; + jsCallback->code = code; + work->data = reinterpret_cast(jsCallback); + int ret = uv_queue_work_with_qos(loop, work, [] (uv_work_t *work) { + LOGD("OnResult uv_queue_work_with_qos"); + }, [] (uv_work_t *work, int status) { + DeviceProfileInfosAsyncCallbackInfo *callback = + reinterpret_cast(work->data); + DeviceManagerNapi *deviceManagerNapi = DeviceManagerNapi::GetDeviceManagerNapi(callback->bundleName); + if (deviceManagerNapi == nullptr) { + LOGE("deviceManagerNapi not find for bundleName %{public}s", callback->bundleName.c_str()); + } else { + deviceManagerNapi->OnGetDeviceProfileInfoListCallbackResult(callback); + } + delete callback; + callback = nullptr; + DeleteUvWork(work); + }, uv_qos_user_initiated); + if (ret != 0) { + LOGE("Failed to execute OnBindResult work queue"); + delete jsCallback; + jsCallback = nullptr; + DeleteUvWork(work); + } +} + DeviceManagerNapi::DeviceManagerNapi(napi_env env, napi_value thisVar) : DmNativeEvent(env, thisVar) { env_ = env; @@ -671,6 +722,34 @@ void DeviceManagerNapi::OnAuthResult(const std::string &deviceId, const std::str napi_close_handle_scope(env_, scope); } +void DeviceManagerNapi::OnGetDeviceProfileInfoListCallbackResult(DeviceProfileInfosAsyncCallbackInfo *jsCallback) +{ + LOGI("In"); + napi_handle_scope scope; + napi_open_handle_scope(env_, &scope); + if (jsCallback->code != DM_OK) { + napi_value error = CreateBusinessError(env_, jsCallback->code, false); + napi_reject_deferred(env_, jsCallback->deferred, error); + LOGE("jsCallback->code(%{public}d) != DM_OK", jsCallback->code); + napi_close_handle_scope(env_, scope); + return; + } + napi_value devInfosJsObj; + napi_create_array(env_, &devInfosJsObj); + bool isArray = false; + napi_is_array(env_, devInfosJsObj, &isArray); + if (!isArray) { + LOGE("napi_create_array failed"); + napi_value error = CreateBusinessError(env_, ERR_DM_POINT_NULL, false); + napi_reject_deferred(env_, jsCallback->deferred, error); + napi_close_handle_scope(env_, scope); + return; + } + DmDeviceProfileInfoToJsArray(env_, jsCallback->deviceProfileInfos, devInfosJsObj); + napi_resolve_deferred(env_, jsCallback->deferred, devInfosJsObj); + napi_close_handle_scope(env_, scope); +} + void DeviceManagerNapi::CreateDmCallback(napi_env env, std::string &bundleName, std::string &eventType) { LOGI("CreateDmCallback for bundleName %{public}s eventType %{public}s", bundleName.c_str(), eventType.c_str()); @@ -1818,6 +1897,91 @@ napi_value DeviceManagerNapi::JsOff(napi_env env, napi_callback_info info) } } +napi_value DeviceManagerNapi::GetDeviceProfileInfoListPromise(napi_env env, + DeviceProfileInfosAsyncCallbackInfo *asyncCallback) +{ + LOGI("In"); + napi_value promise = 0; + napi_deferred deferred; + napi_create_promise(env, &deferred, &promise); + asyncCallback->deferred = deferred; + napi_value workName; + napi_create_string_latin1(env, "GetDeviceProfileInfosPromise", NAPI_AUTO_LENGTH, &workName); + napi_create_async_work(env, nullptr, workName, + [](napi_env env, void *data) { + DeviceProfileInfosAsyncCallbackInfo *jsCallback = + reinterpret_cast(data); + std::shared_ptr callback = + std::make_shared(jsCallback->env, jsCallback->bundleName, + jsCallback->deferred); + int32_t ret = DeviceManager::GetInstance().GetDeviceProfileInfoList(jsCallback->bundleName, + jsCallback->filterOptions, callback); + jsCallback->code = ret; + if (ret != DM_OK) { + LOGE("GetDeviceProfileInfos failed, bundleName:%{public}s, ret=%{public}d", + jsCallback->bundleName.c_str(), ret); + } + }, + [](napi_env env, napi_status status, void *data) { + (void)status; + DeviceProfileInfosAsyncCallbackInfo *jsCallback = + reinterpret_cast(data); + if (jsCallback->code != DM_OK) { + napi_value error = CreateBusinessError(env, jsCallback->code, false); + napi_reject_deferred(env, jsCallback->deferred, error); + } + napi_delete_async_work(env, jsCallback->asyncWork); + delete jsCallback; + jsCallback = nullptr; + }, + (void *)asyncCallback, &asyncCallback->asyncWork); + napi_queue_async_work_with_qos(env, asyncCallback->asyncWork, napi_qos_user_initiated); + return promise; +} + +napi_value DeviceManagerNapi::JsGetDeviceProfileInfoList(napi_env env, napi_callback_info info) +{ + LOGI("In"); + if (!IsSystemApp()) { + LOGE("Caller is not systemApp"); + CreateBusinessError(env, static_cast(DMBussinessErrorCode::ERR_NOT_SYSTEM_APP)); + return nullptr; + } + int32_t ret = DeviceManager::GetInstance().CheckAPIAccessPermission(); + if (ret != DM_OK) { + CreateBusinessError(env, ret); + return nullptr; + } + + size_t argc = 0; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, nullptr, &thisVar, nullptr)); + if (!CheckArgsCount(env, argc >= DM_NAPI_ARGS_ONE, "Wrong number of arguments, required 1")) { + return nullptr; + } + DeviceManagerNapi *deviceManagerWrapper = nullptr; + if (IsDeviceManagerNapiNull(env, thisVar, &deviceManagerWrapper)) { + LOGE("deviceManagerWrapper is NULL"); + CreateBusinessError(env, ERR_DM_POINT_NULL); + return nullptr; + } + napi_value argv[DM_NAPI_ARGS_ONE] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + DmDeviceProfileInfoFilterOptions filterOptions; + JsToDmDeviceProfileInfoFilterOptions(env, argv[0], filterOptions); + auto *jsCallback = new DeviceProfileInfosAsyncCallbackInfo(); + if (jsCallback == nullptr) { + LOGE("jsCallback is nullptr"); + CreateBusinessError(env, ERR_DM_POINT_NULL); + return nullptr; + } + + jsCallback->env = env; + jsCallback->bundleName = deviceManagerWrapper->bundleName_; + jsCallback->filterOptions = filterOptions; + return GetDeviceProfileInfoListPromise(env, jsCallback); +} + void DeviceManagerNapi::ClearBundleCallbacks(std::string &bundleName) { LOGI("ClearBundleCallbacks start for bundleName %{public}s", bundleName.c_str()); @@ -1972,7 +2136,8 @@ napi_value DeviceManagerNapi::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("bindTarget", BindTarget), DECLARE_NAPI_FUNCTION("replyUiAction", SetUserOperationSync), DECLARE_NAPI_FUNCTION("on", JsOn), - DECLARE_NAPI_FUNCTION("off", JsOff)}; + DECLARE_NAPI_FUNCTION("off", JsOff), + DECLARE_NAPI_FUNCTION("getDeviceProfileInfoList", JsGetDeviceProfileInfoList)}; napi_property_descriptor static_prop[] = { DECLARE_NAPI_STATIC_FUNCTION("createDeviceManager", CreateDeviceManager), diff --git a/services/service/BUILD.gn b/services/service/BUILD.gn index 17ec30690..b032dff83 100644 --- a/services/service/BUILD.gn +++ b/services/service/BUILD.gn @@ -1,4 +1,4 @@ -# 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 @@ -160,6 +160,7 @@ if (defined(ohos_lite)) { "${common_path}/src/dfx/standard/dm_hidumper.cpp", "${common_path}/src/dm_anonymous.cpp", "${common_path}/src/ipc/standard/ipc_cmd_register.cpp", + "${common_path}/src/ipc/standard/ipc_model_codec.cpp", "src/advertise/advertise_manager.cpp", "src/device_manager_service.cpp", "src/device_manager_service_listener.cpp", diff --git a/services/service/include/device_manager_service.h b/services/service/include/device_manager_service.h index 0f4bb9279..b3e9ba1e5 100644 --- a/services/service/include/device_manager_service.h +++ b/services/service/include/device_manager_service.h @@ -26,6 +26,7 @@ #include "advertise_manager.h" #include "discovery_manager.h" +#include "dm_device_profile_info.h" #include "pin_holder.h" #include "device_manager_service_listener.h" #include "idevice_manager_service_impl.h" @@ -205,6 +206,7 @@ public: int32_t GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId, int32_t &screenStatus); void HandleCredentialAuthStatus(const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode); + int32_t GetDeviceProfileInfoList(const std::string &pkgName, DmDeviceProfileInfoFilterOptions &filterOptions); private: bool IsDMServiceImplReady(); bool IsDMServiceAdapterLoad(); diff --git a/services/service/include/device_manager_service_listener.h b/services/service/include/device_manager_service_listener.h index 6bbcb8f4c..6145a5dc8 100644 --- a/services/service/include/device_manager_service_listener.h +++ b/services/service/include/device_manager_service_listener.h @@ -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 @@ -23,6 +23,7 @@ #include #include "dm_device_info.h" +#include "dm_device_profile_info.h" #include "idevice_manager_service_listener.h" #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) #include "kv_adapter_manager.h" @@ -76,6 +77,8 @@ public: void OnDeviceScreenStateChange(const std::string &pkgName, DmDeviceInfo &devInfo) override; void OnCredentialAuthStatus(const std::string &pkgName, const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode) override; + void OnGetDeviceProfileInfoListResult(const ProcessInfo &processInfo, + const std::vector &deviceProfileInfos, int32_t code) override; private: void ConvertDeviceInfoToDeviceBasicInfo(const std::string &pkgName, const DmDeviceInfo &info, DmDeviceBasicInfo &deviceBasicInfo); diff --git a/services/service/include/i_dm_service_impl_ext.h b/services/service/include/i_dm_service_impl_ext.h index fe5da7e43..ffd630286 100644 --- a/services/service/include/i_dm_service_impl_ext.h +++ b/services/service/include/i_dm_service_impl_ext.h @@ -22,6 +22,7 @@ #include "idevice_manager_service_listener.h" #include "dm_device_info.h" +#include "dm_device_profile_info.h" namespace OHOS { namespace DistributedHardware { @@ -99,6 +100,14 @@ public: * @tc.type: FUNC */ virtual int32_t AccountUserSwitched(int32_t userId, const std::string &accountId) = 0; + + /** + * @tc.name: IDMServiceImplExt::GetDeviceProfileInfoList + * @tc.desc: GetDeviceProfileInfoList + * @tc.type: FUNC + */ + virtual int32_t GetDeviceProfileInfoList(const std::string &pkgName, + const DmDeviceProfileInfoFilterOptions &filterOptions) = 0; }; using CreateDMServiceImplExtFuncPtr = IDMServiceImplExt *(*)(void); diff --git a/services/service/include/idevice_manager_service_listener.h b/services/service/include/idevice_manager_service_listener.h index 9f1a5d032..3f3f85f9b 100644 --- a/services/service/include/idevice_manager_service_listener.h +++ b/services/service/include/idevice_manager_service_listener.h @@ -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 @@ -159,6 +159,9 @@ public: */ virtual void OnCredentialAuthStatus(const std::string &pkgName, const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode) = 0; + + virtual void OnGetDeviceProfileInfoListResult(const ProcessInfo &processInfo, + const std::vector &deviceProfileInfos, int32_t code); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index 8c287727c..1252997d9 100755 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.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 @@ -1844,5 +1844,20 @@ void DeviceManagerService::HandleCredentialAuthStatus(const std::string &proofIn dmServiceImpl_->HandleCredentialAuthStatus(proofInfo, deviceTypeId, errcode); } } + +int32_t DeviceManagerService::GetDeviceProfileInfoList(const std::string &pkgName, + DmDeviceProfileInfoFilterOptions &filterOptions) +{ + if (!PermissionManager::GetInstance().CheckPermission()) { + LOGE("The caller does not have permission to call"); + return ERR_DM_NO_PERMISSION; + } + LOGI("Start for pkgName = %{public}s", pkgName.c_str()); + if (!IsDMServiceAdapterResidentLoad()) { + LOGE("GetDeviceProfileInfoList failed, adapter instance not init or init failed."); + return ERR_DM_UNSUPPORTED_METHOD; + } + return dmServiceImplExtResident_->GetDeviceProfileInfoList(pkgName, filterOptions); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/service/src/device_manager_service_listener.cpp b/services/service/src/device_manager_service_listener.cpp index 3c2b68d97..ae93fde8f 100644 --- a/services/service/src/device_manager_service_listener.cpp +++ b/services/service/src/device_manager_service_listener.cpp @@ -33,6 +33,7 @@ #include "ipc_notify_device_discovery_req.h" #include "ipc_notify_device_state_req.h" #include "ipc_notify_discover_result_req.h" +#include "ipc_notify_get_device_profile_info_list_req.h" #include "ipc_notify_pin_holder_event_req.h" #include "ipc_notify_publish_result_req.h" #include "ipc_server_stub.h" @@ -584,5 +585,19 @@ std::vector DeviceManagerServiceListener::GetNotifyPkgName( } return pkgNamesTemp; } + +void DeviceManagerServiceListener::OnGetDeviceProfileInfoListResult(const ProcessInfo &processInfo, + const std::vector &deviceProfileInfos, int32_t code) +{ + LOGI("pkgName %{public}s.", processInfo.pkgName.c_str()); + std::shared_ptr pReq = + std::make_shared(); + std::shared_ptr pRsp = std::make_shared(); + pReq->SetPkgName(processInfo.pkgName); + pReq->SetDeviceProfileInfoList(deviceProfileInfos); + pReq->SetResult(code); + pReq->SetProcessInfo(processInfo); + ipcServerListener_.SendRequest(GET_DEVICE_PROFILE_INFO_LIST_RESULT, pReq, pRsp); +} } // 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 796773020..93477cc9a 100644 --- a/services/service/src/ipc/standard/ipc_cmd_parser.cpp +++ b/services/service/src/ipc/standard/ipc_cmd_parser.cpp @@ -23,6 +23,7 @@ #include "dm_anonymous.h" #include "dm_constants.h" #include "dm_device_info.h" +#include "dm_device_profile_info.h" #include "dm_log.h" #include "dm_subscribe_info.h" #include "dm_publish_info.h" @@ -32,6 +33,7 @@ #include "ipc_create_pin_holder_req.h" #include "ipc_credential_auth_status_req.h" #include "ipc_destroy_pin_holder_req.h" +#include "ipc_model_codec.h" #include "ipc_notify_auth_result_req.h" #include "ipc_notify_bind_result_req.h" #include "ipc_notify_credential_req.h" @@ -39,6 +41,7 @@ #include "ipc_notify_device_discovery_req.h" #include "ipc_notify_device_state_req.h" #include "ipc_notify_discover_result_req.h" +#include "ipc_notify_get_device_profile_info_list_req.h" #include "ipc_notify_publish_result_req.h" #include "ipc_notify_pin_holder_event_req.h" #include "ipc_server_client_proxy.h" @@ -1586,5 +1589,70 @@ ON_IPC_CMD(SYNC_CALLBACK, MessageParcel &data, MessageParcel &reply) } return DM_OK; } + +ON_IPC_CMD(GET_DEVICE_PROFILE_INFO_LIST, MessageParcel &data, MessageParcel &reply) +{ + std::string pkgName = data.ReadString(); + DmDeviceProfileInfoFilterOptions filterOptions; + filterOptions.isCloud = data.ReadBool(); + size_t size = data.ReadUint32(); + if (size > MAX_DEVICE_PROFILE_SIZE) { + LOGE("size more than %{public}d,", MAX_DEVICE_PROFILE_SIZE); + return ERR_DM_IPC_WRITE_FAILED; + } + if (size > 0) { + for (uint32_t i = 0; i < size; i++) { + std::string item = data.ReadString(); + filterOptions.deviceIdList.emplace_back(item); + } + } + int32_t result = DeviceManagerService::GetInstance().GetDeviceProfileInfoList(pkgName, filterOptions); + if (!reply.WriteInt32(result)) { + LOGE("write result failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + return DM_OK; +} + +ON_IPC_SET_REQUEST(GET_DEVICE_PROFILE_INFO_LIST_RESULT, std::shared_ptr pBaseReq, MessageParcel &data) +{ + if (pBaseReq == nullptr) { + return 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; + } + int32_t result = pReq->GetResult(); + if (!data.WriteInt32(result)) { + LOGE("write result code failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + std::vector deviceProfileInfos = pReq->GetDeviceProfileInfoList(); + if (!data.WriteInt32((int32_t)deviceProfileInfos.size())) { + LOGE("write device list size failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + for (const auto &devInfo : deviceProfileInfos) { + if (!IpcModelCodec::EncodeDmDeviceProfileInfo(devInfo, data)) { + LOGE("write dm device profile info failed"); + return ERR_DM_IPC_WRITE_FAILED; + } + } + return DM_OK; +} + +ON_IPC_READ_RESPONSE(GET_DEVICE_PROFILE_INFO_LIST_RESULT, MessageParcel &reply, std::shared_ptr pBaseRsp) +{ + if (pBaseRsp == nullptr) { + LOGE("pBaseRsp is null"); + return ERR_DM_FAILED; + } + pBaseRsp->SetErrCode(reply.ReadInt32()); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/UTTest_dm_pin_holder.h b/test/unittest/UTTest_dm_pin_holder.h index 71eae95a2..085d32ae8 100644 --- a/test/unittest/UTTest_dm_pin_holder.h +++ b/test/unittest/UTTest_dm_pin_holder.h @@ -194,6 +194,14 @@ public: (void)deviceTypeId; (void)errcode; } + + virtual void OnGetDeviceProfileInfoListResult(const ProcessInfo &processInfo, + const std::vector &deviceProfileInfos, int32_t code) override + { + (void)processInfo; + (void)deviceProfileInfos; + (void)code; + } }; } // namespace DistributedHardware } // namespace OHOS -- Gitee