From 8462b38f01ccec7fa86f171b3d87713906d33c29 Mon Sep 17 00:00:00 2001 From: BrainL Date: Mon, 3 Feb 2025 15:47:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8F=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=AD=90=E7=B3=BB=E7=BB=9FIDL=E5=8C=96=E5=85=B3=E8=81=94?= =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: BrainL --- interfaces/inner_kits/native_cpp/BUILD.gn | 2 + .../native_cpp/include/dm_device_info.h | 85 +++++++++++++++++++ .../native_cpp/src/dm_device_info.cpp | 81 ++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 interfaces/inner_kits/native_cpp/src/dm_device_info.cpp diff --git a/interfaces/inner_kits/native_cpp/BUILD.gn b/interfaces/inner_kits/native_cpp/BUILD.gn index 1012479bf..583e1b329 100644 --- a/interfaces/inner_kits/native_cpp/BUILD.gn +++ b/interfaces/inner_kits/native_cpp/BUILD.gn @@ -96,6 +96,7 @@ if (defined(ohos_lite)) { "${common_path}/src/dm_anonymous.cpp", "${devicemanager_path}/radar/src/lite/dm_radar_helper.cpp", "src/device_manager.cpp", + "src/dm_device_info.cpp", "src/device_manager_impl.cpp", "src/ipc/ipc_client_proxy.cpp", "src/ipc/lite/ipc_client_manager.cpp", @@ -169,6 +170,7 @@ if (defined(ohos_lite)) { "${common_path}/src/ipc/standard/ipc_model_codec.cpp", "${devicemanager_path}/radar/src/dm_radar_helper.cpp", "src/device_manager.cpp", + "src/dm_device_info.cpp", "src/device_manager_impl.cpp", "src/ipc/ipc_client_proxy.cpp", "src/ipc/standard/dm_service_load.cpp", 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 00d087fcb..93a39b4a3 100644 --- a/interfaces/inner_kits/native_cpp/include/dm_device_info.h +++ b/interfaces/inner_kits/native_cpp/include/dm_device_info.h @@ -21,6 +21,7 @@ #include #include "dm_app_image_info.h" +#include "parcel.h" #define DM_MAX_DEVICE_ID_LEN (97) #define DM_MAX_DEVICE_NAME_LEN (129) @@ -413,6 +414,90 @@ typedef struct DmNotifyKey { notifyUserId == other.notifyUserId && udid < other.udid); } } DmNotifyKey; + +struct DmDeviceInfoExt : public Parcelable +{ + + /** + * Device Id of the device. + */ + char deviceId[DM_MAX_DEVICE_ID_LEN] = {0}; + /** + * Device name of the device. + */ + char deviceName[DM_MAX_DEVICE_NAME_LEN] = {0}; + /** + * Device type of the device. + */ + uint16_t deviceTypeId; + /** + * NetworkId of the device. + */ + char networkId[DM_MAX_DEVICE_ID_LEN] = {0}; + /** + * The distance of discovered device, in centimeter(cm). + */ + int32_t range; + /** + * NetworkType of the device. + */ + int32_t networkType; + /** + * Device authentication form. + */ + int32_t authForm; + /** + * Extra data of the device. + * include json keys: "CONN_ADDR_TYPE", "BR_MAC_", "BLE_MAC", "WIFI_IP", "WIFI_PORT", "CUSTOM_DATA" + */ + std::string extraData; + + DmDeviceInfoExt() {} + + DmDeviceInfoExt(const DmDeviceInfo &devInfo) + { + strncpy(deviceId, devInfo.deviceId, DM_MAX_DEVICE_ID_LEN - 1); + deviceId[DM_MAX_DEVICE_ID_LEN - 1] = '\0'; + + strncpy(deviceName, devInfo.deviceName, DM_MAX_DEVICE_NAME_LEN - 1); + deviceName[DM_MAX_DEVICE_NAME_LEN - 1] = '\0'; + + strncpy(networkId, devInfo.networkId, DM_MAX_DEVICE_ID_LEN - 1); + networkId[DM_MAX_DEVICE_ID_LEN - 1] = '\0'; + + deviceTypeId = devInfo.deviceTypeId; + range = devInfo.range; + networkType = devInfo.networkType; + authForm = static_cast(devInfo.authForm); + extraData = devInfo.extraData; + } + + DmDeviceInfo ConvertToDmDeviceInfo() + { + DmDeviceInfo dmDeviceInfo; + + strncpy(dmDeviceInfo.deviceId, deviceId, DM_MAX_DEVICE_ID_LEN - 1); + dmDeviceInfo.deviceId[DM_MAX_DEVICE_ID_LEN - 1] = '\0'; + + strncpy(dmDeviceInfo.deviceName, deviceName, DM_MAX_DEVICE_NAME_LEN - 1); + dmDeviceInfo.deviceName[DM_MAX_DEVICE_NAME_LEN - 1] = '\0'; + + strncpy(dmDeviceInfo.networkId, networkId, DM_MAX_DEVICE_ID_LEN - 1); + dmDeviceInfo.networkId[DM_MAX_DEVICE_ID_LEN - 1] = '\0'; + + dmDeviceInfo.deviceTypeId = deviceTypeId; + dmDeviceInfo.range = range; + dmDeviceInfo.networkType = networkType; + dmDeviceInfo.authForm = static_cast(authForm); + dmDeviceInfo.extraData = extraData; + + return dmDeviceInfo; + } + + virtual bool Marshalling(Parcel &parcel) const override; + + static DmDeviceInfoExt *Unmarshalling(Parcel &parcel); +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DEVICE_INFO_H \ No newline at end of file diff --git a/interfaces/inner_kits/native_cpp/src/dm_device_info.cpp b/interfaces/inner_kits/native_cpp/src/dm_device_info.cpp new file mode 100644 index 000000000..45f416c64 --- /dev/null +++ b/interfaces/inner_kits/native_cpp/src/dm_device_info.cpp @@ -0,0 +1,81 @@ +/* + * 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 + * + * 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 +#include +#include + +#include "dm_device_info.h" + +#include "dm_log.h" +#include "dm_constants.h" + +#define DM_MAX_DEVICE_ID_LEN (97) +#define DM_MAX_DEVICE_NAME_LEN (129) + +namespace OHOS { +namespace DistributedHardware { +bool DmDeviceInfoExt::Marshalling(Parcel &parcel) const +{ + bool bRet = true; + std::string deviceIdStr(deviceId); + bRet = (bRet && parcel.WriteString(deviceIdStr)); + std::string deviceNameStr(deviceName); + bRet = (bRet && parcel.WriteString(deviceNameStr)); + bRet = (bRet && parcel.WriteUint16(deviceTypeId)); + std::string networkIdStr(networkId); + bRet = (bRet && parcel.WriteString(networkIdStr)); + bRet = (bRet && parcel.WriteInt32(range)); + bRet = (bRet && parcel.WriteInt32(networkType)); + bRet = (bRet && parcel.WriteInt32(authForm)); + bRet = (bRet && parcel.WriteString(extraData)); + return bRet; +} + +DmDeviceInfoExt *DmDeviceInfoExt::Unmarshalling(Parcel &parcel) +{ + DmDeviceInfoExt *deviceInfoExt = new (std::nothrow) DmDeviceInfoExt(); + if (deviceInfoExt == nullptr) { + LOGE("Create DmDeviceInfoExt failed"); + return nullptr; + } + std::string deviceIdStr = parcel.ReadString(); + if (strcpy_s(deviceInfoExt->deviceId, deviceIdStr.size() + 1, deviceIdStr.c_str()) != DM_OK) { + LOGE("strcpy_s deviceId failed!"); + delete deviceInfoExt; + return nullptr; + } + std::string deviceNameStr = parcel.ReadString(); + if (strcpy_s(deviceInfoExt->deviceName, deviceNameStr.size() + 1, deviceNameStr.c_str()) != DM_OK) { + LOGE("strcpy_s deviceName failed!"); + delete deviceInfoExt; + return nullptr; + } + deviceInfoExt->deviceTypeId = parcel.ReadUint16(); + std::string networkIdStr = parcel.ReadString(); + if (strcpy_s(deviceInfoExt->networkId, networkIdStr.size() + 1, networkIdStr.c_str()) != DM_OK) { + LOGE("strcpy_s networkId failed!"); + delete deviceInfoExt; + return nullptr; + } + deviceInfoExt->range = parcel.ReadInt32(); + deviceInfoExt->networkType = parcel.ReadInt32(); + deviceInfoExt->authForm = static_cast(parcel.ReadInt32()); + deviceInfoExt->extraData = parcel.ReadString(); + return deviceInfoExt; +} + +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file -- Gitee