1
6
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
46
47
48
49
50
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
142
143
144
145
146
147
/* ... */
/* ... */
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_nan.h"
#include "esp_mac.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"11 includes
#define EXAMPLE_NAN_SERV_NAME CONFIG_ESP_WIFI_NAN_SVC_NAME
#define EXAMPLE_NAN_MATCHING_FILTER CONFIG_ESP_WIFI_NAN_MATCHING_FILTER
#ifdef CONFIG_ESP_WIFI_NAN_SERVICE_MESSAGE
#define EXAMPLE_NAN_SVC_MSG CONFIG_ESP_WIFI_NAN_SERVICE_MESSAGE
#else
#define EXAMPLE_NAN_SVC_MSG "Welcome"
#endif
static EventGroupHandle_t nan_event_group;
static int NAN_RECEIVE = BIT0;
uint8_t g_peer_inst_id;
static void nan_receive_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
wifi_event_nan_receive_t *evt = (wifi_event_nan_receive_t *)event_data;
g_peer_inst_id = evt->peer_inst_id;
xEventGroupSetBits(nan_event_group, NAN_RECEIVE);
}{ ... }
static void nan_ndp_indication_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_data == NULL) {
return;
}{...}
wifi_event_ndp_indication_t *evt = (wifi_event_ndp_indication_t *)event_data;
wifi_nan_datapath_resp_t ndp_resp = {0};
ndp_resp.accept = true;
ndp_resp.ndp_id = evt->ndp_id;
memcpy(ndp_resp.peer_mac, evt->peer_nmi, 6);
esp_wifi_nan_datapath_resp(&ndp_resp);
}{ ... }
void wifi_nan_publish(void)
{
nan_event_group = xEventGroupCreate();
esp_event_handler_instance_t instance_any_id;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
WIFI_EVENT_NAN_RECEIVE,
&nan_receive_event_handler,
NULL,
&instance_any_id));
/* ... */
bool ndp_require_consent = true;
wifi_nan_config_t nan_cfg = WIFI_NAN_CONFIG_DEFAULT();
esp_netif_create_default_wifi_nan();
esp_wifi_nan_start(&nan_cfg);
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
WIFI_EVENT_NDP_INDICATION,
&nan_ndp_indication_event_handler,
NULL,
&instance_any_id));
uint8_t pub_id;
wifi_nan_publish_cfg_t publish_cfg = {
.service_name = EXAMPLE_NAN_SERV_NAME,
#if CONFIG_EXAMPLE_NAN_PUBLISH_UNSOLICITED
.type = NAN_PUBLISH_UNSOLICITED,
#else
.type = NAN_PUBLISH_SOLICITED,
#endif
.matching_filter = EXAMPLE_NAN_MATCHING_FILTER,
.single_replied_event = 1,
}{...};
pub_id = esp_wifi_nan_publish_service(&publish_cfg, ndp_require_consent);
if (pub_id == 0) {
return;
}{...}
while (1) {
EventBits_t bits = xEventGroupWaitBits(nan_event_group, NAN_RECEIVE, pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & NAN_RECEIVE) {
xEventGroupClearBits(nan_event_group, NAN_RECEIVE);
wifi_nan_followup_params_t fup = {0};
fup.inst_id = pub_id,
fup.peer_inst_id = g_peer_inst_id,
strlcpy(fup.svc_info, EXAMPLE_NAN_SVC_MSG, ESP_WIFI_MAX_SVC_INFO_LEN);
esp_wifi_nan_send_message(&fup);
}{...}
vTaskDelay(10);
}{...}
}{ ... }
void initialise_wifi(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM) );
}{ ... }
void app_main(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}{...}
ESP_ERROR_CHECK(ret);
initialise_wifi();
wifi_nan_publish();
}{ ... }