1
6
7
8
9
10
11
12
13
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
42
43
44
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
106
107
108
109
110
111
112
113
114
115
116
117
121
122
123
127
128
129
130
131
132
136
137
141
142
143
144
145
151
152
158
159
160
161
162
163
164
165
166
167
168
169
170
171
175
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
231
235
236
237
238
241
244
245
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
321
325
326
327
328
329
330
334
335
336
337
338
339
340
341
342
343
344
345
346
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
378
379
380
381
382
383
384
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* ... */
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_wnm.h"
#include "esp_rrm.h"
#include "esp_mbo.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include <sys/time.h>16 includes
#define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
#define EXAMPLE_WIFI_PASSWORD CONFIG_EXAMPLE_WIFI_PASSWORD
#define EXAMPLE_WIFI_RSSI_THRESHOLD CONFIG_EXAMPLE_WIFI_RSSI_THRESHOLD
static bool g_neighbor_report_active;
static EventGroupHandle_t wifi_event_group;
static esp_netif_t *sta_netif = NULL;
static const char *TAG = "roaming_example";
static inline uint32_t WPA_GET_LE32(const uint8_t *a)
{
return ((uint32_t) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
}{ ... }
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
}{...} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
wifi_event_sta_disconnected_t *disconn = event_data;
ESP_LOGI(TAG, "station got disconnected reason=%d", disconn->reason);
if (disconn->reason == WIFI_REASON_ROAMING) {
ESP_LOGI(TAG, "station roaming, do nothing");
}{...} else {
esp_wifi_connect();
}{...}
}{...} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
#if EXAMPLE_WIFI_RSSI_THRESHOLD
if (EXAMPLE_WIFI_RSSI_THRESHOLD) {
ESP_LOGI(TAG, "setting rssi threshold as %d", EXAMPLE_WIFI_RSSI_THRESHOLD);
esp_wifi_set_rssi_threshold(EXAMPLE_WIFI_RSSI_THRESHOLD);
}{...}
#endif/* ... */
if (esp_rrm_is_rrm_supported_connection()) {
ESP_LOGI(TAG,"RRM supported");
esp_rrm_send_neighbor_report_request();
g_neighbor_report_active = true;
}{...} else {
ESP_LOGI(TAG,"RRM not supported");
}{...}
if (esp_wnm_is_btm_supported_connection()) {
ESP_LOGI(TAG,"BTM supported");
}{...} else {
ESP_LOGI(TAG,"BTM not supported");
}{...}
}{...}
}{ ... }
#ifndef WLAN_EID_MEASURE_REPORT
#define WLAN_EID_MEASURE_REPORT 39
#endif
#ifndef MEASURE_TYPE_LCI
#define MEASURE_TYPE_LCI 9
#endif
#ifndef MEASURE_TYPE_LOCATION_CIVIC
#define MEASURE_TYPE_LOCATION_CIVIC 11
#endif
#ifndef WLAN_EID_NEIGHBOR_REPORT
#define WLAN_EID_NEIGHBOR_REPORT 52
#endif
#ifndef ETH_ALEN
#define ETH_ALEN 6
#endif
#define MAX_LCI_CIVIC_LEN 256 * 2 + 1
#define MAX_NEIGHBOR_LEN 512
#if 1
static char * get_btm_neighbor_list(uint8_t *report, size_t report_len)
{
size_t len = 0;
const uint8_t *data;
int ret = 0;
char *lci = NULL;
char *civic = NULL;
/* ... */
#define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
if (!report || report_len == 0) {
ESP_LOGI(TAG, "RRM neighbor report is not valid");
return NULL;
}{...}
char *buf = calloc(1, MAX_NEIGHBOR_LEN);
if (!buf) {
ESP_LOGE(TAG, "Memory allocation for neighbor list failed");
goto cleanup;
}{...}
data = report;
while (report_len >= 2 + NR_IE_MIN_LEN) {
const uint8_t *nr;
lci = (char *)malloc(sizeof(char)*MAX_LCI_CIVIC_LEN);
if (!lci) {
ESP_LOGE(TAG, "Memory allocation for lci failed");
goto cleanup;
}{...}
civic = (char *)malloc(sizeof(char)*MAX_LCI_CIVIC_LEN);
if (!civic) {
ESP_LOGE(TAG, "Memory allocation for civic failed");
goto cleanup;
}{...}
uint8_t nr_len = data[1];
const uint8_t *pos = data, *end;
if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
nr_len < NR_IE_MIN_LEN) {
ESP_LOGI(TAG, "CTRL: Invalid Neighbor Report element: id=%u len=%u",
data[0], nr_len);
ret = -1;
goto cleanup;
}{...}
if (2U + nr_len > report_len) {
ESP_LOGI(TAG, "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
data[0], report_len, nr_len);
ret = -1;
goto cleanup;
}{...}
pos += 2;
end = pos + nr_len;
nr = pos;
pos += NR_IE_MIN_LEN;
lci[0] = '\0';
civic[0] = '\0';
while (end - pos > 2) {
uint8_t s_id, s_len;
s_id = *pos++;
s_len = *pos++;
if (s_len > end - pos) {
ret = -1;
goto cleanup;
}{...}
if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
switch (pos[2]) {
case MEASURE_TYPE_LCI:
if (lci[0])
break;
memcpy(lci, pos, s_len);
break;...
case MEASURE_TYPE_LOCATION_CIVIC:
if (civic[0])
break;
memcpy(civic, pos, s_len);
break;...
}{...}
}{...}
pos += s_len;
}{...}
ESP_LOGI(TAG, "RMM neighbor report bssid=" MACSTR
" info=0x%" PRIx32 " op_class=%u chan=%u phy_type=%u%s%s%s%s",
MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
nr[ETH_ALEN + 6],
lci[0] ? " lci=" : "", lci,
civic[0] ? " civic=" : "", civic);
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, " neighbor=");
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, MACSTR, MAC2STR(nr));
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, "0x%04" PRIx32 "", WPA_GET_LE32(nr + ETH_ALEN));
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, "%u", nr[ETH_ALEN + 4]);
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, "%u", nr[ETH_ALEN + 5]);
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, "%u", nr[ETH_ALEN + 6]);
data = end;
report_len -= 2 + nr_len;
if (lci) {
free(lci);
lci = NULL;
}{...}
if (civic) {
free(civic);
civic = NULL;
}{...}
}{...}
cleanup:
if (lci) {
free(lci);
}{...}
if (civic) {
free(civic);
}{...}
if (ret < 0) {
free(buf);
buf = NULL;
}{...}
return buf;
}{ ... }
/* ... */#else
char * get_tmp_neighbor_list(uint8_t *report, size_t report_len)
{
#define MAC1 "00:01:02:03:04:05"
#define MAC2 "00:02:03:04:05:06"
char * buf = calloc(1, MAX_NEIGHBOR_LEN);
size_t len = 0;
char *pos;
if (!buf)
return NULL;
pos = buf;
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, " neighbor=");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, MAC1);
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "0x0000");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "81");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "6");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "7");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, " neighbor=");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, MAC2);
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "0x0000");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "81");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "6");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "7");
len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, " ");
#undef MAC1
#undef MAC2
return buf;
}{...}
/* ... */#endif
static void esp_neighbor_report_recv_handler(void* arg, esp_event_base_t event_base,int32_t event_id, void* event_data)
{
if (!g_neighbor_report_active) {
ESP_LOGV(TAG,"Neighbor report received but not triggered by us");
return;
}{...}
if (!event_data) {
ESP_LOGE(TAG, "No event data received for neighbor report");
return;
}{...}
g_neighbor_report_active = false;
uint8_t cand_list = 0;
wifi_event_neighbor_report_t *neighbor_report_event = (wifi_event_neighbor_report_t*)event_data;
uint8_t *pos = (uint8_t *)neighbor_report_event->report;
char * neighbor_list = NULL;
if (!pos) {
ESP_LOGE(TAG, "Neighbor report is empty");
return;
}{...}
uint8_t report_len = neighbor_report_event->report_len;
ESP_LOGD(TAG, "rrm: neighbor report len=%d", report_len);
ESP_LOG_BUFFER_HEXDUMP(TAG, pos, report_len, ESP_LOG_DEBUG);
neighbor_list = get_btm_neighbor_list(pos + 1, report_len - 1);
if (!neighbor_list) {
wifi_scan_config_t params;
memset(¶ms, 0, sizeof(wifi_scan_config_t));
if (esp_wifi_scan_start(¶ms, true) < 0) {
goto cleanup;
}{...}
esp_wifi_clear_ap_list();
cand_list = 1;
}{...}
esp_wnm_send_bss_transition_mgmt_query(REASON_FRAME_LOSS, neighbor_list, cand_list);
cleanup:
if (neighbor_list)
free(neighbor_list);
}{ ... }
#if EXAMPLE_WIFI_RSSI_THRESHOLD
static void esp_bss_rssi_low_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
wifi_event_bss_rssi_low_t *event = event_data;
ESP_LOGI(TAG, "%s:bss rssi is=%ld", __func__, event->rssi);
if (esp_rrm_send_neighbor_report_request() < 0) {
ESP_LOGI(TAG, "failed to send neighbor report request");
if (esp_wnm_send_bss_transition_mgmt_query(REASON_FRAME_LOSS, NULL, 0) < 0) {
ESP_LOGI(TAG, "failed to send btm query");
}{...}
}{...} else {
g_neighbor_report_active = true;
}{...}
}{...}
/* ... */#endif
#if 0
static void clear_chan_preference()
{
esp_mbo_update_non_pref_chan(NULL);
}{...}
static void update_chan_preference(void)
{
struct non_pref_chan_s *chans = malloc(sizeof(struct non_pref_chan_s) + 2 * sizeof(struct non_pref_chan));
chans->non_pref_chan_num = 2;
struct non_pref_chan *chan = &chans->chan[0];
chan->reason = NON_PREF_CHAN_REASON_UNSPECIFIED;
chan->oper_class = 0x51;
chan->chan = 1;
chan->preference = 0;
chan = &chans->chan[1];
chan->reason = NON_PREF_CHAN_REASON_UNSPECIFIED;
chan->oper_class = 0x51;
chan->chan = 11;
chan->preference = 1;
esp_mbo_update_non_pref_chan(chans);
free(chans);
}{...}
/* ... */#endif
static void initialise_wifi(void)
{
ESP_ERROR_CHECK(esp_netif_init());
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_create_default());
sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_NEIGHBOR_REP,
&esp_neighbor_report_recv_handler, NULL));
#if EXAMPLE_WIFI_RSSI_THRESHOLD
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW,
&esp_bss_rssi_low_handler, NULL));/* ... */
#endif
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASSWORD,
.rm_enabled =1,
.btm_enabled =1,
.mbo_enabled =1,
.pmf_cfg.capable = 1,
.ft_enabled =1,
}{...},
}{...};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}{ ... }
void app_main(void)
{
ESP_ERROR_CHECK( nvs_flash_init() );
initialise_wifi();
}{ ... }