diff --git a/core/discovery/coap/common/include/disc_coap_parser.h b/core/discovery/coap/common/include/disc_coap_parser.h index 475d08a5cb837d24b6fd9ed1d2fd21fa45a69d0c..d18b1a1e37005ab6e6469eae76150bb9c32ca211 100644 --- a/core/discovery/coap/common/include/disc_coap_parser.h +++ b/core/discovery/coap/common/include/disc_coap_parser.h @@ -31,6 +31,7 @@ int32_t DiscCoapParseDeviceUdid(const char *raw, DeviceInfo *device); void DiscCoapParseWifiIpAddr(const cJSON *data, DeviceInfo *device); int32_t DiscCoapParseServiceData(const cJSON *data, DeviceInfo *device); void DiscCoapParseHwAccountHash(const cJSON *data, DeviceInfo *device); +int32_t DiscCoapFillServiceData(uint32_t capability, const char *capabilityData, uint32_t dataLen, char *outData); #ifdef __cplusplus } diff --git a/core/discovery/coap/common/src/disc_coap_parser.c b/core/discovery/coap/common/src/disc_coap_parser.c index 00f37bc951eb9246291c75c59ed75f5d4d1bdb7f..6bfc8a730155bb5625afa473c25c4736ad455719 100644 --- a/core/discovery/coap/common/src/disc_coap_parser.c +++ b/core/discovery/coap/common/src/disc_coap_parser.c @@ -26,6 +26,7 @@ #define JSON_WLAN_IP "wifiIpAddr" #define JSON_SERVICE_DATA "serviceData" #define JSON_HW_ACCOUNT "hwAccountHashVal" +#define JSON_KEY_CAST_PLUS "castPlus" #define MAX_SERVICE_DATA_LEN 64 #define HEX_HASH_LEN 16 @@ -108,6 +109,11 @@ int32_t DiscCoapParseServiceData(const cJSON *data, DeviceInfo *device) DISC_LOGW(DISC_COAP, "parse service data failed."); return SOFTBUS_ERR; } + char serviceDataBak[MAX_SERVICE_DATA_LEN] = {0}; + if (memcpy_s(serviceDataBak, MAX_SERVICE_DATA_LEN, serviceData, MAX_SERVICE_DATA_LEN) != EOK) { + DISC_LOGE(DISC_COAP, "copy service data bak failed."); + return SOFTBUS_ERR; + } char port[MAX_PORT_STR_LEN] = {0}; ParseItemDataFromServiceData(serviceData, SERVICE_DATA_PORT, port, sizeof(port)); int authPort = atoi(port); @@ -116,6 +122,29 @@ int32_t DiscCoapParseServiceData(const cJSON *data, DeviceInfo *device) return SOFTBUS_ERR; } device->addr[0].info.ip.port = (uint16_t)authPort; + + char castData[MAX_SERVICE_DATA_LEN] = {0}; + ParseItemDataFromServiceData(serviceDataBak, JSON_KEY_CAST_PLUS, castData, sizeof(castData)); + if (strlen(castData) == 0) { + // no cast data, just return ok + return SOFTBUS_OK; + } + cJSON *castJson = cJSON_CreateObject(); + DISC_CHECK_AND_RETURN_RET_LOGE(castJson != NULL, SOFTBUS_CREATE_JSON_ERR, DISC_COAP, "create cast json failed"); + if (!AddStringToJsonObject(castJson, JSON_KEY_CAST_PLUS, castData)) { + DISC_LOGE(DISC_COAP, "add cast data failed"); + cJSON_Delete(castJson); + return SOFTBUS_CREATE_JSON_ERR; + } + char *castStr = cJSON_PrintUnformatted(castJson); + cJSON_Delete(castJson); + + if (strcpy_s(device->custData, strlen(castStr) + 1, castStr) != EOK) { + DISC_LOGE(DISC_COAP, "copy cast data failed"); + cJSON_free(castStr); + return SOFTBUS_STRCPY_ERR; + } + cJSON_free(castStr); return SOFTBUS_OK; } @@ -132,4 +161,38 @@ void DiscCoapParseHwAccountHash(const cJSON *data, DeviceInfo *device) int32_t ret = SoftBusGenerateStrHash((const unsigned char *)tmpAccount, strlen(tmpAccount), (unsigned char *)device->accountHash); DISC_CHECK_AND_RETURN_LOGE(ret == SOFTBUS_OK, DISC_COAP, "generate account hash failed, ret=%d", ret); +} + +int32_t DiscCoapFillServiceData(uint32_t capability, const char *capabilityData, uint32_t dataLen, char *outData) +{ + DISC_CHECK_AND_RETURN_RET_LOGE(outData != NULL, SOFTBUS_INVALID_PARAM, DISC_COAP, "out data is NULL"); + if (capability != (1 << CASTPLUS_CAPABILITY_BITMAP)) { + // only castPlus need add extra service data + return SOFTBUS_OK; + } + (void)memset_s(outData, sizeof(outData), 0, sizeof(outData)); + if (capabilityData == NULL || dataLen == 0) { + DISC_LOGI(DISC_COAP, "no capability data, no need to fill service data"); + return SOFTBUS_OK; + } + DISC_CHECK_AND_RETURN_RET_LOGE(strlen(capabilityData) == dataLen, SOFTBUS_INVALID_PARAM, DISC_COAP, + "capability data len(%u) != expected len(%u), data=%s", strlen(capabilityData), dataLen, capabilityData); + + cJSON *json = cJSON_ParseWithLength(capabilityData, dataLen); + DISC_CHECK_AND_RETURN_RET_LOGE(json != NULL, SOFTBUS_CREATE_JSON_ERR, DISC_COAP, + "trans capability data to json failed"); + + char jsonStr[MAX_SERVICE_DATA_LEN] = {0}; + if (!GetJsonObjectStringItem(json, JSON_KEY_CAST_PLUS, jsonStr, MAX_SERVICE_DATA_LEN)) { + DISC_LOGE(DISC_COAP, "parse cast capability data failed"); + cJSON_Delete(json); + return SOFTBUS_PARSE_JSON_ERR; + } + if (sprintf_s(outData, MAX_SERVICE_DATA_LEN, "%s:%s", JSON_KEY_CAST_PLUS, jsonStr) < 0) { + DISC_LOGE(DISC_COAP, "write cast capability data failed"); + cJSON_Delete(json); + return SOFTBUS_ERR; + } + cJSON_Delete(json); + return SOFTBUS_OK; } \ No newline at end of file diff --git a/core/discovery/coap/nstackx_coap/include/disc_nstackx_adapter.h b/core/discovery/coap/nstackx_coap/include/disc_nstackx_adapter.h index 4b3977893cad7a32b05763bb3d0a3566a132e30c..cfb0e74cba9a45a82a979958ce0b99c85b45e9e7 100644 --- a/core/discovery/coap/nstackx_coap/include/disc_nstackx_adapter.h +++ b/core/discovery/coap/nstackx_coap/include/disc_nstackx_adapter.h @@ -52,7 +52,7 @@ void DiscNstackxDeinit(void); int32_t DiscCoapRegisterCb(const DiscInnerCallback *discCoapCb); int32_t DiscCoapRegisterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]); int32_t DiscCoapSetFilterCapability(uint32_t capabilityBitmapNum, uint32_t capabilityBitmap[]); -int32_t DiscCoapRegisterServiceData(const unsigned char *serviceData, uint32_t dataLen); +int32_t DiscCoapRegisterServiceData(const unsigned char *capabilityData, uint32_t dataLen, uint32_t capability); int32_t DiscCoapRegisterCapabilityData(const unsigned char *capabilityData, uint32_t dataLen, uint32_t capability); int32_t DiscCoapStartDiscovery(DiscCoapOption *option); int32_t DiscCoapStopDiscovery(void); diff --git a/core/discovery/coap/nstackx_coap/src/disc_coap.c b/core/discovery/coap/nstackx_coap/src/disc_coap.c index 0bcb3563aa3c61eb20e06be198db77634ceca8d5..1242b654ae037f05121e47a95e68fbd29ddc95c6 100644 --- a/core/discovery/coap/nstackx_coap/src/disc_coap.c +++ b/core/discovery/coap/nstackx_coap/src/disc_coap.c @@ -145,7 +145,8 @@ static int32_t Publish(const PublishOption *option, bool isActive) DISC_LOGW(DISC_COAP, "register all capability to dfinder failed."); goto PUB_FAIL; } - if (DiscCoapRegisterServiceData(option->capabilityData, option->dataLen) != SOFTBUS_OK) { + if (DiscCoapRegisterServiceData(option->capabilityData, option->dataLen, + option->capabilityBitmap[0]) != SOFTBUS_OK) { DISC_LOGW(DISC_COAP, "register service data to dfinder failed."); goto PUB_FAIL; } @@ -222,7 +223,8 @@ static int32_t UnPublish(const PublishOption *option, bool isActive) return SOFTBUS_DISCOVER_COAP_REGISTER_CAP_FAIL; } } - if (DiscCoapRegisterServiceData(option->capabilityData, option->dataLen) != SOFTBUS_OK) { + if (DiscCoapRegisterServiceData(option->capabilityData, option->dataLen, + option->capabilityBitmap[0]) != SOFTBUS_OK) { discScanEventExtra.errcode = SOFTBUS_DISCOVER_END_SCAN_FAIL; DISC_EVENT(EVENT_SCENE_SCAN, EVENT_STAGE_SCAN_START, discScanEventExtra); (void)SoftBusMutexUnlock(&(g_publishMgr->lock)); diff --git a/core/discovery/coap/nstackx_coap/src/disc_nstackx_adapter.c b/core/discovery/coap/nstackx_coap/src/disc_nstackx_adapter.c index 5510d03dee52a4637c6283c8e723f37f3445802f..52b728a3a73a86c0996a939e522a2a35b7a4131a 100644 --- a/core/discovery/coap/nstackx_coap/src/disc_nstackx_adapter.c +++ b/core/discovery/coap/nstackx_coap/src/disc_nstackx_adapter.c @@ -236,26 +236,33 @@ int32_t DiscCoapSetFilterCapability(uint32_t capabilityBitmapNum, uint32_t capab return SOFTBUS_OK; } -int32_t DiscCoapRegisterServiceData(const unsigned char *serviceData, uint32_t dataLen) +int32_t DiscCoapRegisterServiceData(const unsigned char *capabilityData, uint32_t dataLen, uint32_t capability) { - (void)serviceData; - (void)dataLen; - DISC_CHECK_AND_RETURN_RET_LOGW(g_capabilityData != NULL, SOFTBUS_DISCOVER_COAP_INIT_FAIL, + DISC_CHECK_AND_RETURN_RET_LOGE(g_capabilityData != NULL, SOFTBUS_DISCOVER_COAP_INIT_FAIL, DISC_COAP, "g_capabilityData=NULL"); int32_t authPort = 0; - if (LnnGetLocalNumInfo(NUM_KEY_AUTH_PORT, &authPort) != SOFTBUS_OK) { - DISC_LOGE(DISC_COAP, "get auth port from lnn failed."); - } - (void)memset_s(g_capabilityData, NSTACKX_MAX_SERVICE_DATA_LEN, 0, NSTACKX_MAX_SERVICE_DATA_LEN); - if (sprintf_s(g_capabilityData, NSTACKX_MAX_SERVICE_DATA_LEN, "port:%d,", authPort) == -1) { + int32_t ret = LnnGetLocalNumInfo(NUM_KEY_AUTH_PORT, &authPort); + DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_ERR, DISC_COAP, "get auth port failed: %d", ret); + + char serviceData[NSTACKX_MAX_SERVICE_DATA_LEN] = {0}; + if (sprintf_s(serviceData, NSTACKX_MAX_SERVICE_DATA_LEN, "port:%d,", authPort) == -1) { DISC_LOGE(DISC_COAP, "write auth port to service data failed."); return SOFTBUS_ERR; } - if (NSTACKX_RegisterServiceData(g_capabilityData) != SOFTBUS_OK) { - DISC_LOGE(DISC_COAP, "register service data to nstackx failed."); + // capabilityData can be NULL, it will be check in this func + ret = DiscCoapFillServiceData(capability, (const char *)capabilityData, dataLen, g_capabilityData); + DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_ERR, DISC_COAP, "fill service data failed: %d", ret); + + if (strlen(g_capabilityData) != 0 && + sprintf_s(serviceData, NSTACKX_MAX_SERVICE_DATA_LEN, "%s%s", serviceData, g_capabilityData) < 0) { + DISC_LOGE(DISC_COAP, "write capability data to service data failed."); return SOFTBUS_ERR; } + + ret = NSTACKX_RegisterServiceData(serviceData); + DISC_CHECK_AND_RETURN_RET_LOGE(ret == SOFTBUS_OK, SOFTBUS_ERR, DISC_COAP, + "register service data to nstackx failed: %d", ret); return SOFTBUS_OK; } diff --git a/tests/core/discovery/coap/nstackx_adapter_mock/nstackx_adapter_test.cpp b/tests/core/discovery/coap/nstackx_adapter_mock/nstackx_adapter_test.cpp index 2241298a791e52faabeaf95d77c287c4e2a59d64..58cba247fc716c315a67557c17e0d9d4f0b95b9a 100644 --- a/tests/core/discovery/coap/nstackx_adapter_mock/nstackx_adapter_test.cpp +++ b/tests/core/discovery/coap/nstackx_adapter_mock/nstackx_adapter_test.cpp @@ -49,7 +49,7 @@ HWTEST_F(NstackxAdapterTest, DiscCoapRegisterServiceData002, TestSize.Level1) EXPECT_CALL(adapterMock, NSTACKX_RegisterServiceData(NotNull())).WillRepeatedly(Return(!SOFTBUS_OK)); uint32_t dataLen = 0; - EXPECT_EQ(DiscCoapRegisterServiceData(nullptr, dataLen), SOFTBUS_ERR); + EXPECT_EQ(DiscCoapRegisterServiceData(nullptr, dataLen, 0), SOFTBUS_ERR); DISC_LOGI(DISC_TEST, "DiscCoapRegisterServiceData002 end ----"); } } diff --git a/tests/core/discovery/coap/unittest/disc_nstackx_adapter_test.cpp b/tests/core/discovery/coap/unittest/disc_nstackx_adapter_test.cpp index 74d12a5f9c66be3952bd0f5dd17d99fe1d14f1ff..a1a047020836adf7485a5a82fb9b11da1935cd61 100644 --- a/tests/core/discovery/coap/unittest/disc_nstackx_adapter_test.cpp +++ b/tests/core/discovery/coap/unittest/disc_nstackx_adapter_test.cpp @@ -179,12 +179,12 @@ HWTEST_F(DiscNstackxAdapterTest, TestDiscCoapAdapterSetFilter001, TestSize.Level HWTEST_F(DiscNstackxAdapterTest, TestDiscCoapAdapterRegData001, TestSize.Level1) { DiscNstackxDeinit(); - int32_t ret = DiscCoapRegisterServiceData(nullptr, 0); + int32_t ret = DiscCoapRegisterServiceData(nullptr, 0, 0); EXPECT_EQ(ret, SOFTBUS_DISCOVER_COAP_INIT_FAIL); ret = DiscNstackxInit(); EXPECT_EQ(ret, SOFTBUS_OK); - ret = DiscCoapRegisterServiceData(nullptr, 0); + ret = DiscCoapRegisterServiceData(nullptr, 0, 0); EXPECT_EQ(ret, SOFTBUS_OK); }