diff --git a/core/bus_center/ipc/mini/lnn_bus_center_ipc.c b/core/bus_center/ipc/mini/lnn_bus_center_ipc.c index d4dc7e2a01455473c28d9a6e4c1ce4205e4b2620..828947ac06585ad501d6a44f97235f2ad1bda906 100644 --- a/core/bus_center/ipc/mini/lnn_bus_center_ipc.c +++ b/core/bus_center/ipc/mini/lnn_bus_center_ipc.c @@ -27,6 +27,9 @@ #include "lnn_time_sync_manager.h" #include "softbus_errcode.h" #include "softbus_log.h" +#include "softbus_adapter_crypto.h" +#include "softbus_utils.h" +#include "softbus_adapter_mem.h" static int32_t OnRefreshDeviceFound(const char *packageName, const DeviceInfo *device, const InnerDeviceInfoAddtions *addtions); @@ -46,18 +49,88 @@ static int32_t PublishResultTransfer(int32_t retCode) } } -static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device, +static int32_t LnnIpcGenerateHexStringHash(char *str, uint32_t len, char *hashStr) +{ + int32_t ret; + unsigned char hashResult[UDID_HASH_LEN] = {0}; + if (str == NULL) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc generate str hash invalid param"); + return SOFTBUS_INVALID_PARAM; + } + ret = SoftBusGenerateStrHash((const unsigned char *)str, strlen(str) + 1, hashResult); + if (ret != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc generate str hash fail, ret=%d", ret); + return ret; + } + ret = ConvertBytesToHexString(hashStr, len + 1, hashResult, len / HEXIFY_UNIT_LEN); + if (ret != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc convert bytes to str hash fail, ret=%d", ret); + return ret; + } + return SOFTBUS_OK; +} + +static int32_t LnnIpcRefreshDeviceFoundMediumIsBle(DeviceInfo *device) +{ + int32_t i, infoNum; + NodeBasicInfo *info = NULL; + char udidHash[UDID_HASH_LEN + 1] = {0}; + device->isOnline = false; + + if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc get all online node info fail"); + return SOFTBUS_ERR; + } + if (info == NULL || infoNum == 0) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "lnn none online node"); + return SOFTBUS_OK; + } + for (i = 0; i < infoNum; ++i) { + const NodeInfo *nodeInfo = LnnGetNodeInfoById(info[i].networkId, CATEGORY_NETWORK_ID); + if (nodeInfo == NULL) { + continue; + } + if (LnnIpcGenerateHexStringHash((char *)nodeInfo->deviceInfo.deviceUdid, SHORT_UDID_HASH_LEN, + udidHash) != SOFTBUS_OK) { + continue; + } + if (strncmp(udidHash, device->devId, UDID_HASH_LEN) == 0) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "lnn ipc node udidhash:%s is online", udidHash); + if (memcpy_s(device->devId, DISC_MAX_DEVICE_ID_LEN, nodeInfo->deviceInfo.deviceUdid, + UDID_BUF_LEN) != EOK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc memcpy deviceUdid fail"); + SoftBusFree(info); + return SOFTBUS_ERR; + } + device->isOnline = true; + SoftBusFree(info); + return SOFTBUS_OK; + } + } + SoftBusFree(info); + return SOFTBUS_ERR; +} + +static int32_t OnRefreshDeviceFoundInner(const char *pkgName, DeviceInfo *device, const InnerDeviceInfoAddtions *addtions) { (void)pkgName; - (void)addtions; - if (LnnGetOnlineStateById(device->devId, CATEGORY_UDID)) { - SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "device has online"); + if (addtions->medium == COAP) { + device->isOnline = LnnGetOnlineStateById(device->devId, CATEGORY_UDID); + } + if (addtions->medium == BLE) { + LnnIpcRefreshDeviceFoundMediumIsBle(device); } LnnOnRefreshDeviceFound(device); return SOFTBUS_OK; } +static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device, + const InnerDeviceInfoAddtions *addtions) +{ + return OnRefreshDeviceFoundInner(pkgName, (DeviceInfo *)device, addtions); +} + int32_t LnnIpcServerJoin(const char *pkgName, void *addr, uint32_t addrTypeLen) { (void)pkgName; diff --git a/core/bus_center/ipc/small/src/lnn_bus_center_ipc.c b/core/bus_center/ipc/small/src/lnn_bus_center_ipc.c index 588881057ce15e74401beb782f3ef98c806da2fc..8eebd487879dd3cada5770e084f8a4e5f029886e 100644 --- a/core/bus_center/ipc/small/src/lnn_bus_center_ipc.c +++ b/core/bus_center/ipc/small/src/lnn_bus_center_ipc.c @@ -32,6 +32,7 @@ #include "softbus_errcode.h" #include "softbus_log.h" #include "softbus_utils.h" +#include "softbus_adapter_crypto.h" typedef struct { ListNode node; @@ -173,16 +174,86 @@ static int32_t AddLeaveLNNInfo(const char *pkgName, const char *networkId) return SOFTBUS_OK; } -static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device, +static int32_t LnnIpcGenerateHexStringHash(char *str, uint32_t len, char *hashStr) +{ + int32_t ret; + unsigned char hashResult[UDID_HASH_LEN] = {0}; + if (str == NULL) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc generate str hash invalid param"); + return SOFTBUS_INVALID_PARAM; + } + ret = SoftBusGenerateStrHash((const unsigned char *)str, strlen(str) + 1, hashResult); + if (ret != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc generate str hash fail, ret=%d", ret); + return ret; + } + ret = ConvertBytesToHexString(hashStr, len + 1, hashResult, len / HEXIFY_UNIT_LEN); + if (ret != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc convert bytes to str hash fail, ret=%d", ret); + return ret; + } + return SOFTBUS_OK; +} + +static int32_t LnnIpcRefreshDeviceFoundMediumIsBle(DeviceInfo *device) +{ + int32_t i, infoNum; + NodeBasicInfo *info = NULL; + char udidHash[UDID_HASH_LEN + 1] = {0}; + device->isOnline = false; + + if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc get all online node info fail"); + return SOFTBUS_ERR; + } + if (info == NULL || infoNum == 0) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "lnn none online node"); + return SOFTBUS_OK; + } + for (i = 0; i < infoNum; ++i) { + const NodeInfo *nodeInfo = LnnGetNodeInfoById(info[i].networkId, CATEGORY_NETWORK_ID); + if (nodeInfo == NULL) { + continue; + } + if (LnnIpcGenerateHexStringHash((char *)nodeInfo->deviceInfo.deviceUdid, SHORT_UDID_HASH_LEN, + udidHash) != SOFTBUS_OK) { + continue; + } + if (strncmp(udidHash, device->devId, UDID_HASH_LEN) == 0) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "lnn ipc node udidhash:%s is online", udidHash); + if (memcpy_s(device->devId, DISC_MAX_DEVICE_ID_LEN, nodeInfo->deviceInfo.deviceUdid, + UDID_BUF_LEN) != EOK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc memcpy deviceUdid fail"); + SoftBusFree(info); + return SOFTBUS_ERR; + } + device->isOnline = true; + SoftBusFree(info); + return SOFTBUS_OK; + } + } + SoftBusFree(info); + return SOFTBUS_ERR; +} + +static int32_t OnRefreshDeviceFoundInner(const char *pkgName, DeviceInfo *device, const InnerDeviceInfoAddtions *addtions) { - (void)addtions; - if (LnnGetOnlineStateById(device->devId, CATEGORY_UDID)) { - SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "device has online"); + if (addtions->medium == COAP) { + device->isOnline = LnnGetOnlineStateById(device->devId, CATEGORY_UDID); + } + if (addtions->medium == BLE) { + LnnIpcRefreshDeviceFoundMediumIsBle(device); } return ClientOnRefreshDeviceFound(pkgName, device, sizeof(DeviceInfo)); } +static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device, + const InnerDeviceInfoAddtions *addtions) +{ + return OnRefreshDeviceFoundInner(pkgName, (DeviceInfo *)device, addtions); +} + static int32_t PublishResultTransfer(int32_t retCode) { if (retCode == SOFTBUS_OK) { diff --git a/core/bus_center/ipc/standard/src/lnn_bus_center_ipc.cpp b/core/bus_center/ipc/standard/src/lnn_bus_center_ipc.cpp index 5dbefcf54b7f64d12b72c01179023fc989de78e8..4bd3ebe099ed441c8c9faf95099b7d7c3d3d477d 100644 --- a/core/bus_center/ipc/standard/src/lnn_bus_center_ipc.cpp +++ b/core/bus_center/ipc/standard/src/lnn_bus_center_ipc.cpp @@ -32,6 +32,9 @@ #include "softbus_errcode.h" #include "softbus_log.h" #include "softbus_permission.h" +#include "softbus_adapter_crypto.h" +#include "softbus_adapter_mem.h" +#include "softbus_utils.h" struct JoinLnnRequestInfo { char pkgName[PKG_NAME_SIZE_MAX]; @@ -129,16 +132,86 @@ static int32_t PublishResultTransfer(int32_t retCode) } } -static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device, +static int32_t LnnIpcGenerateHexStringHash(char *str, uint32_t len, char *hashStr) +{ + int32_t ret; + unsigned char hashResult[UDID_HASH_LEN] = {0}; + if (str == nullptr) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc generate str hash invalid param"); + return SOFTBUS_INVALID_PARAM; + } + ret = SoftBusGenerateStrHash((const unsigned char *)str, strlen(str) + 1, hashResult); + if (ret != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc generate str hash fail, ret=%d", ret); + return ret; + } + ret = ConvertBytesToHexString(hashStr, len + 1, hashResult, len / HEXIFY_UNIT_LEN); + if (ret != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc convert bytes to str hash fail, ret=%d", ret); + return ret; + } + return SOFTBUS_OK; +} + +static int32_t LnnIpcRefreshDeviceFoundMediumIsBle(DeviceInfo *device) +{ + int32_t i, infoNum; + NodeBasicInfo *info = nullptr; + char udidHash[UDID_HASH_LEN + 1] = {0}; + device->isOnline = false; + + if (LnnGetAllOnlineNodeInfo(&info, &infoNum) != SOFTBUS_OK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc get all online node info fail"); + return SOFTBUS_ERR; + } + if (info == nullptr || infoNum == 0) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "lnn none online node"); + return SOFTBUS_OK; + } + for (i = 0; i < infoNum; ++i) { + const NodeInfo *nodeInfo = LnnGetNodeInfoById(info[i].networkId, CATEGORY_NETWORK_ID); + if (nodeInfo == nullptr) { + continue; + } + if (LnnIpcGenerateHexStringHash((char *)nodeInfo->deviceInfo.deviceUdid, SHORT_UDID_HASH_LEN, + udidHash) != SOFTBUS_OK) { + continue; + } + if (strncmp(udidHash, device->devId, UDID_HASH_LEN) == 0) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_DBG, "lnn ipc node udidhash:%s is online", udidHash); + if (memcpy_s(device->devId, DISC_MAX_DEVICE_ID_LEN, nodeInfo->deviceInfo.deviceUdid, + UDID_BUF_LEN) != EOK) { + SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "lnn ipc memcpy deviceUdid fail"); + SoftBusFree(info); + return SOFTBUS_ERR; + } + device->isOnline = true; + SoftBusFree(info); + return SOFTBUS_OK; + } + } + SoftBusFree(info); + return SOFTBUS_ERR; +} + +static int32_t OnRefreshDeviceFoundInner(const char *pkgName, DeviceInfo *device, const InnerDeviceInfoAddtions *addtions) { - (void)addtions; - if (LnnGetOnlineStateById(device->devId, CATEGORY_UDID)) { - SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "device has online"); + if (addtions->medium == COAP) { + device->isOnline = LnnGetOnlineStateById(device->devId, CATEGORY_UDID); + } + if (addtions->medium == BLE) { + LnnIpcRefreshDeviceFoundMediumIsBle(device); } return ClientOnRefreshDeviceFound(pkgName, device, sizeof(DeviceInfo)); } +static int32_t OnRefreshDeviceFound(const char *pkgName, const DeviceInfo *device, + const InnerDeviceInfoAddtions *addtions) +{ + return OnRefreshDeviceFoundInner(pkgName, (DeviceInfo *)device, addtions); +} + int32_t LnnIpcServerJoin(const char *pkgName, void *addr, uint32_t addrTypeLen) { ConnectionAddr *connAddr = (ConnectionAddr *)addr; diff --git a/interfaces/kits/common/softbus_common.h b/interfaces/kits/common/softbus_common.h index 5008ff28a267d3661a1e8aa7cee4f4d7fa49512a..d5e0652c7023b956cec7396a864975c2d744e501 100644 --- a/interfaces/kits/common/softbus_common.h +++ b/interfaces/kits/common/softbus_common.h @@ -94,6 +94,14 @@ extern "C" { */ #define UDID_HASH_LEN 32 +/** + * @brief Indicates the length of the short UDID hash value. + * + * @since 1.0 + * @version 1.0 + */ +#define SHORT_UDID_HASH_LEN 8 + /** * @brief Indicates the length of the UUID string, including the terminating null character \0. * @@ -435,6 +443,8 @@ typedef struct { char custData[DISC_MAX_CUST_DATA_LEN]; /** The distance of dicovered device, in centimeters(cm)*/ int32_t range; + /** Device Online Status */ + bool isOnline; } DeviceInfo; /**