From 6138ec7a61e7e837324bb0a18f1ce3401da12cc6 Mon Sep 17 00:00:00 2001 From: Shyamal Khachane Date: Thu, 22 Jun 2023 13:56:17 +0530 Subject: [PATCH] fix(esp_wifi): NAN Service discovery and datapath bugfixes 1. Increase timeout value for NDP Accepted/Rejected events. 2. Update publisher ID incase publisher restarts(service). 3. Ignore service discovery frames with invalid/inactive service ID. 4. Update NAN documentation --- components/esp_wifi/include/esp_wifi_types.h | 1 + components/esp_wifi/lib | 2 +- .../esp_wifi/wifi_apps/include/esp_nan.h | 2 ++ components/esp_wifi/wifi_apps/src/nan_app.c | 19 ++++++++++++++++++- docs/en/api-guides/wifi.rst | 17 +++++++++++++++++ 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/components/esp_wifi/include/esp_wifi_types.h b/components/esp_wifi/include/esp_wifi_types.h index 9ff0eb1384..72ab60a249 100644 --- a/components/esp_wifi/include/esp_wifi_types.h +++ b/components/esp_wifi/include/esp_wifi_types.h @@ -1064,6 +1064,7 @@ typedef struct { uint8_t subscribe_id; /**< Subscribe Service Identifier */ uint8_t publish_id; /**< Publish Service Identifier */ uint8_t pub_if_mac[6]; /**< NAN Interface MAC of the Publisher */ + bool update_pub_id; /**< Indicates whether publisher's service ID needs to be updated */ } wifi_event_nan_svc_match_t; /** Argument structure for WIFI_EVENT_NAN_REPLIED event */ diff --git a/components/esp_wifi/lib b/components/esp_wifi/lib index 6ddb9f9a86..fd77ffcf8e 160000 --- a/components/esp_wifi/lib +++ b/components/esp_wifi/lib @@ -1 +1 @@ -Subproject commit 6ddb9f9a861a389f65bcc91a195265c523de8afe +Subproject commit fd77ffcf8e56ad5c9ea85d635f40ce63134101b0 diff --git a/components/esp_wifi/wifi_apps/include/esp_nan.h b/components/esp_wifi/wifi_apps/include/esp_nan.h index 9be6bbb665..ae0ae46352 100644 --- a/components/esp_wifi/wifi_apps/include/esp_nan.h +++ b/components/esp_wifi/wifi_apps/include/esp_nan.h @@ -161,6 +161,8 @@ void esp_wifi_nan_get_ipv6_linklocal_from_mac(ip6_addr_t *ip6, uint8_t *mac_addr /** * brief Get own Service information from Service ID OR Name. * + * @attention If service information is to be fetched from service name, set own_svc_id as zero. + * * @param[inout] own_svc_id As input, it indicates Service ID to search for. * As output, it indicates Service ID of the service found using Service Name. * @param[inout] svc_name As input, it indicates Service Name to search for. diff --git a/components/esp_wifi/wifi_apps/src/nan_app.c b/components/esp_wifi/wifi_apps/src/nan_app.c index 0b0c60863f..cc4025cd60 100644 --- a/components/esp_wifi/wifi_apps/src/nan_app.c +++ b/components/esp_wifi/wifi_apps/src/nan_app.c @@ -32,6 +32,8 @@ #define MACADDR_EQUAL(a1, a2) (memcmp(a1, a2, MACADDR_LEN)) #define MACADDR_COPY(dst, src) (memcpy(dst, src, MACADDR_LEN)) #define NAN_DW_INTVL_MS 524 /* NAN DW interval (512 TU's ~= 524 mSec) */ +#define NAN_NDP_RESP_TIMEOUT_DW 4 +#define NAN_NDP_RESP_TIMEOUT NAN_NDP_RESP_TIMEOUT_DW*NAN_DW_INTVL_MS /* Global Variables */ static const char *TAG = "nan_app"; @@ -180,6 +182,16 @@ static struct peer_svc_info *nan_find_peer_svc(uint8_t own_svc_id, uint8_t peer_ return p_peer_svc; } +static bool nan_update_peer_svc(uint8_t own_svc_id, uint8_t peer_svc_id, uint8_t peer_nmi[]) +{ + struct peer_svc_info *peer_info = nan_find_peer_svc(own_svc_id, 0, peer_nmi); + if (peer_info) { + peer_info->svc_id = peer_svc_id; + return true; + } + return false; +} + static bool nan_record_peer_svc(uint8_t own_svc_id, uint8_t peer_svc_id, uint8_t peer_nmi[]) { struct own_svc_info *p_own_svc; @@ -388,6 +400,11 @@ static void nan_fill_params_from_event(void *evt_data, uint8_t event) case WIFI_EVENT_NAN_SVC_MATCH: { wifi_event_nan_svc_match_t *evt = (wifi_event_nan_svc_match_t *)evt_data; + if (evt->update_pub_id) { + if (nan_update_peer_svc(evt->subscribe_id, evt->publish_id, evt->pub_if_mac)) { + break; + } + } if (!nan_find_peer_svc(evt->subscribe_id, evt->publish_id, evt->pub_if_mac)) { nan_record_peer_svc(evt->subscribe_id, evt->publish_id, evt->pub_if_mac); } @@ -867,7 +884,7 @@ uint8_t esp_wifi_nan_datapath_req(wifi_nan_datapath_req_t *req) nan_record_new_ndl(ndp_id, req->pub_id, req->peer_mac, ESP_WIFI_NDP_ROLE_INITIATOR); ESP_LOGD(TAG, "Requested NDP with "MACSTR" [NDP ID - %d]", MAC2STR(req->peer_mac), ndp_id); - EventBits_t bits = os_event_group_wait_bits(nan_event_group, NDP_ACCEPTED | NDP_REJECTED, pdFALSE, pdFALSE, pdMS_TO_TICKS(2*NAN_DW_INTVL_MS)); + EventBits_t bits = os_event_group_wait_bits(nan_event_group, NDP_ACCEPTED | NDP_REJECTED, pdFALSE, pdFALSE, pdMS_TO_TICKS(NAN_NDP_RESP_TIMEOUT)); if (bits & NDP_ACCEPTED) { os_event_group_clear_bits(nan_event_group, NDP_ACCEPTED); return ndp_id; diff --git a/docs/en/api-guides/wifi.rst b/docs/en/api-guides/wifi.rst index 1ea50ec0e5..325ea75bbb 100644 --- a/docs/en/api-guides/wifi.rst +++ b/docs/en/api-guides/wifi.rst @@ -26,6 +26,10 @@ The following features are supported: - Multiple antennas - Channel state information + .. only:: SOC_WIFI_NAN_SUPPORT + + - Wi-Fi Aware (NAN) + .. only:: esp32c6 - 4 virtual Wi-Fi interfaces, which are STA, AP, Sniffer and reserved. @@ -1652,6 +1656,19 @@ For establishing a secure connection, AP and station negotiate and agree on the Detailed information on creating certificates and how to run wpa2_enterprise example on {IDF_TARGET_NAME} can be found in :example:`wifi/wifi_enterprise`. +.. only:: SOC_WIFI_NAN_SUPPORT + + Wi-Fi Aware\ :sup:`TM` (NAN) + ---------------------------- + + Wi-Fi Aware\ :sup:`TM` or NAN (Neighbor Awareness Networking) is a protocol that allows Wi-Fi devices to discover services in their proximity. NAN uses direct device-to-device communication and does not require any Internet or AP connection. + + Multiple NAN devices in the vicinity will form a NAN cluster which allows them to communicate with each other. NAN devices in a cluster synchronise their clocks and listen to each other periodically on Channel 6. Devices can advertise (Publish) or seek for (Subscribe) services within their NAN Cluster using Service Discovery protocols. Matching of services is done by service name and optionally matching filters. Once a Subscriber gets a match with a Publisher, it can either send a message (Follow-up) or establish a datapath (NDP) with the Publisher. After NDP is setup both devices will obtain an IPv6 address and can use it for communication. + + Please note that NAN Datapath security is not supported i.e. the data packets will go out unencrypted. NAN uses a separate interface for Discovery and Datapath, which is other than that used for STA and AP. NAN operates in standalone mode, which means co-existence with STA or AP interface is not supported. + + Refer to ESP-IDF examples :idf_file:`examples/wifi/wifi_aware/nan_publisher/README.md` and :idf_file:`examples/wifi/wifi_aware/nan_subscriber/README.md` to setup a NAN Publisher and Subscriber. + Wireless Network Management ----------------------------