roaming_example.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/event_groups.h"
  6. #include "esp_wifi.h"
  7. #include "esp_wnm.h"
  8. #include "esp_rrm.h"
  9. #include "esp_mbo.h"
  10. #include "esp_event.h"
  11. #include "esp_log.h"
  12. #include "esp_mac.h"
  13. #include "nvs_flash.h"
  14. #include "esp_netif.h"
  15. /* Configuration */
  16. #define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
  17. #define EXAMPLE_WIFI_PASSWORD CONFIG_EXAMPLE_WIFI_PASSWORD
  18. #define EXAMPLE_WIFI_RSSI_THRESHOLD CONFIG_EXAMPLE_WIFI_RSSI_THRESHOLD
  19. /* rrm ctx */
  20. int rrm_ctx = 0;
  21. /* FreeRTOS event group to signal when we are connected & ready to make a request */
  22. static EventGroupHandle_t wifi_event_group;
  23. /* esp netif object representing the WIFI station */
  24. static esp_netif_t *sta_netif = NULL;
  25. static const char *TAG = "roaming_example";
  26. static inline uint32_t WPA_GET_LE32(const uint8_t *a)
  27. {
  28. return ((uint32_t) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
  29. }
  30. static void event_handler(void* arg, esp_event_base_t event_base,
  31. int32_t event_id, void* event_data)
  32. {
  33. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
  34. esp_wifi_connect();
  35. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  36. wifi_event_sta_disconnected_t *disconn = event_data;
  37. ESP_LOGI(TAG, "station got disconnected reason=%d", disconn->reason);
  38. if (disconn->reason == WIFI_REASON_ROAMING) {
  39. ESP_LOGI(TAG, "station roaming, do nothing");
  40. } else {
  41. esp_wifi_connect();
  42. }
  43. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
  44. #if EXAMPLE_WIFI_RSSI_THRESHOLD
  45. if (EXAMPLE_WIFI_RSSI_THRESHOLD) {
  46. ESP_LOGI(TAG, "setting rssi threshold as %d\n", EXAMPLE_WIFI_RSSI_THRESHOLD);
  47. esp_wifi_set_rssi_threshold(EXAMPLE_WIFI_RSSI_THRESHOLD);
  48. }
  49. #endif
  50. if (esp_rrm_is_rrm_supported_connection()) {
  51. ESP_LOGI(TAG,"RRM supported");
  52. } else {
  53. ESP_LOGI(TAG,"RRM not supported");
  54. }
  55. if (esp_wnm_is_btm_supported_connection()) {
  56. ESP_LOGI(TAG,"BTM supported");
  57. } else {
  58. ESP_LOGI(TAG,"BTM not supported");
  59. }
  60. }
  61. }
  62. #ifndef WLAN_EID_MEASURE_REPORT
  63. #define WLAN_EID_MEASURE_REPORT 39
  64. #endif
  65. #ifndef MEASURE_TYPE_LCI
  66. #define MEASURE_TYPE_LCI 9
  67. #endif
  68. #ifndef MEASURE_TYPE_LOCATION_CIVIC
  69. #define MEASURE_TYPE_LOCATION_CIVIC 11
  70. #endif
  71. #ifndef WLAN_EID_NEIGHBOR_REPORT
  72. #define WLAN_EID_NEIGHBOR_REPORT 52
  73. #endif
  74. #ifndef ETH_ALEN
  75. #define ETH_ALEN 6
  76. #endif
  77. #define MAX_NEIGHBOR_LEN 512
  78. #if 1
  79. static char * get_btm_neighbor_list(uint8_t *report, size_t report_len)
  80. {
  81. size_t len = 0;
  82. const uint8_t *data;
  83. int ret = 0;
  84. /*
  85. * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
  86. * BSSID[6]
  87. * BSSID Information[4]
  88. * Operating Class[1]
  89. * Channel Number[1]
  90. * PHY Type[1]
  91. * Optional Subelements[variable]
  92. */
  93. #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
  94. if (!report || report_len == 0) {
  95. ESP_LOGI(TAG, "RRM neighbor report is not valid");
  96. return NULL;
  97. }
  98. char *buf = calloc(1, MAX_NEIGHBOR_LEN);
  99. data = report;
  100. while (report_len >= 2 + NR_IE_MIN_LEN) {
  101. const uint8_t *nr;
  102. char lci[256 * 2 + 1];
  103. char civic[256 * 2 + 1];
  104. uint8_t nr_len = data[1];
  105. const uint8_t *pos = data, *end;
  106. if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
  107. nr_len < NR_IE_MIN_LEN) {
  108. ESP_LOGI(TAG, "CTRL: Invalid Neighbor Report element: id=%u len=%u",
  109. data[0], nr_len);
  110. ret = -1;
  111. goto cleanup;
  112. }
  113. if (2U + nr_len > report_len) {
  114. ESP_LOGI(TAG, "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
  115. data[0], report_len, nr_len);
  116. ret = -1;
  117. goto cleanup;
  118. }
  119. pos += 2;
  120. end = pos + nr_len;
  121. nr = pos;
  122. pos += NR_IE_MIN_LEN;
  123. lci[0] = '\0';
  124. civic[0] = '\0';
  125. while (end - pos > 2) {
  126. uint8_t s_id, s_len;
  127. s_id = *pos++;
  128. s_len = *pos++;
  129. if (s_len > end - pos) {
  130. ret = -1;
  131. goto cleanup;
  132. }
  133. if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
  134. /* Measurement Token[1] */
  135. /* Measurement Report Mode[1] */
  136. /* Measurement Type[1] */
  137. /* Measurement Report[variable] */
  138. switch (pos[2]) {
  139. case MEASURE_TYPE_LCI:
  140. if (lci[0])
  141. break;
  142. memcpy(lci, pos, s_len);
  143. break;
  144. case MEASURE_TYPE_LOCATION_CIVIC:
  145. if (civic[0])
  146. break;
  147. memcpy(civic, pos, s_len);
  148. break;
  149. }
  150. }
  151. pos += s_len;
  152. }
  153. ESP_LOGI(TAG, "RMM neigbor report bssid=" MACSTR
  154. " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
  155. MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
  156. nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
  157. nr[ETH_ALEN + 6],
  158. lci[0] ? " lci=" : "", lci,
  159. civic[0] ? " civic=" : "", civic);
  160. /* neighbor start */
  161. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, " neighbor=");
  162. /* bssid */
  163. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, MACSTR, MAC2STR(nr));
  164. /* , */
  165. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, ",");
  166. /* bssid info */
  167. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, "0x%04x", WPA_GET_LE32(nr + ETH_ALEN));
  168. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, ",");
  169. /* operating class */
  170. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, "%u", nr[ETH_ALEN + 4]);
  171. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, ",");
  172. /* channel number */
  173. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, "%u", nr[ETH_ALEN + 5]);
  174. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, ",");
  175. /* phy type */
  176. len += snprintf(buf + len, MAX_NEIGHBOR_LEN - len, "%u", nr[ETH_ALEN + 6]);
  177. /* optional elements, skip */
  178. data = end;
  179. report_len -= 2 + nr_len;
  180. }
  181. cleanup:
  182. if (ret < 0) {
  183. free(buf);
  184. buf = NULL;
  185. }
  186. return buf;
  187. }
  188. #else
  189. /* Sample API to create neighbor list */
  190. char * get_tmp_neighbor_list(uint8_t *report, size_t report_len)
  191. {
  192. #define MAC1 "00:01:02:03:04:05"
  193. #define MAC2 "00:02:03:04:05:06"
  194. char * buf = calloc(1, MAX_NEIGHBOR_LEN);
  195. size_t len = 0;
  196. char *pos;
  197. if (!buf)
  198. return NULL;
  199. pos = buf;
  200. /* create two temp neighbors */
  201. /* format for neighbor list : neighbor=11:22:33:44:55:66,0x0000,81,3,7,0301ff */
  202. /* neighbor1 start */
  203. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, " neighbor=");
  204. /* bssid */
  205. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, MAC1);
  206. /* , */
  207. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
  208. /* bssid info */
  209. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "0x0000");
  210. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
  211. /* operating class */
  212. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "81");
  213. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
  214. /* channel number */
  215. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "6");
  216. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
  217. /* phy type */
  218. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "7");
  219. /* optional elements, skip */
  220. /* neighbor2 start */
  221. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, " neighbor=");
  222. /* bssid */
  223. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, MAC2);
  224. /* , */
  225. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
  226. /* bssid info */
  227. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "0x0000");
  228. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
  229. /* operating class */
  230. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "81");
  231. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
  232. /* channel number */
  233. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "6");
  234. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, ",");
  235. /* phy type */
  236. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, "7");
  237. /* optional elements, skip */
  238. len += snprintf(pos + len, MAX_NEIGHBOR_LEN - len, " ");
  239. #undef MAC1
  240. #undef MAC2
  241. return buf;
  242. }
  243. #endif
  244. void neighbor_report_recv_cb(void *ctx, const uint8_t *report, size_t report_len)
  245. {
  246. int *val = ctx;
  247. uint8_t *pos = (uint8_t *)report;
  248. int cand_list = 0;
  249. if (!report) {
  250. ESP_LOGE(TAG, "report is null");
  251. return;
  252. }
  253. if (*val != rrm_ctx) {
  254. ESP_LOGE(TAG, "rrm_ctx value didn't match, not initiated by us");
  255. return;
  256. }
  257. /* dump report info */
  258. ESP_LOGI(TAG, "rrm: neighbor report len=%d", report_len);
  259. ESP_LOG_BUFFER_HEXDUMP(TAG, pos, report_len, ESP_LOG_INFO);
  260. /* create neighbor list */
  261. char *neighbor_list = get_btm_neighbor_list(pos + 1, report_len - 1);
  262. /* In case neighbor list is not present issue a scan and get the list from that */
  263. if (!neighbor_list) {
  264. /* issue scan */
  265. wifi_scan_config_t params;
  266. memset(&params, 0, sizeof(wifi_scan_config_t));
  267. if (esp_wifi_scan_start(&params, true) < 0) {
  268. goto cleanup;
  269. }
  270. /* cleanup from net802.11 */
  271. uint16_t number = 1;
  272. wifi_ap_record_t ap_records;
  273. esp_wifi_scan_get_ap_records(&number, &ap_records);
  274. cand_list = 1;
  275. }
  276. /* send AP btm query, this will cause STA to roam as well */
  277. esp_wnm_send_bss_transition_mgmt_query(REASON_FRAME_LOSS, neighbor_list, cand_list);
  278. cleanup:
  279. if (neighbor_list)
  280. free(neighbor_list);
  281. }
  282. #if EXAMPLE_WIFI_RSSI_THRESHOLD
  283. static void esp_bss_rssi_low_handler(void* arg, esp_event_base_t event_base,
  284. int32_t event_id, void* event_data)
  285. {
  286. wifi_event_bss_rssi_low_t *event = event_data;
  287. ESP_LOGI(TAG, "%s:bss rssi is=%d", __func__, event->rssi);
  288. /* Lets check channel conditions */
  289. rrm_ctx++;
  290. if (esp_rrm_send_neighbor_rep_request(neighbor_report_recv_cb, &rrm_ctx) < 0) {
  291. /* failed to send neighbor report request */
  292. ESP_LOGI(TAG, "failed to send neighbor report request");
  293. if (esp_wnm_send_bss_transition_mgmt_query(REASON_FRAME_LOSS, NULL, 0) < 0) {
  294. ESP_LOGI(TAG, "failed to send btm query");
  295. }
  296. }
  297. }
  298. #endif
  299. #if 0
  300. /* Example code to update channel preference in MBO */
  301. static void clear_chan_preference()
  302. {
  303. esp_mbo_update_non_pref_chan(NULL);
  304. }
  305. static void update_chan_preference(void)
  306. {
  307. struct non_pref_chan_s *chans = malloc(sizeof(struct non_pref_chan_s) + 2 * sizeof(struct non_pref_chan));
  308. chans->non_pref_chan_num = 2;
  309. /* first */
  310. struct non_pref_chan *chan = &chans->chan[0];
  311. chan->reason = NON_PREF_CHAN_REASON_UNSPECIFIED;
  312. chan->oper_class = 0x51;
  313. chan->chan = 1;
  314. chan->preference = 0;
  315. /* second */
  316. chan = &chans->chan[1];
  317. chan->reason = NON_PREF_CHAN_REASON_UNSPECIFIED;
  318. chan->oper_class = 0x51;
  319. chan->chan = 11;
  320. chan->preference = 1;
  321. esp_mbo_update_non_pref_chan(chans);
  322. free(chans);
  323. }
  324. #endif
  325. static void initialise_wifi(void)
  326. {
  327. ESP_ERROR_CHECK(esp_netif_init());
  328. wifi_event_group = xEventGroupCreate();
  329. ESP_ERROR_CHECK(esp_event_loop_create_default());
  330. sta_netif = esp_netif_create_default_wifi_sta();
  331. assert(sta_netif);
  332. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  333. ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
  334. ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
  335. ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
  336. #if EXAMPLE_WIFI_RSSI_THRESHOLD
  337. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_BSS_RSSI_LOW,
  338. &esp_bss_rssi_low_handler, NULL));
  339. #endif
  340. ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
  341. wifi_config_t wifi_config = {
  342. .sta = {
  343. .ssid = EXAMPLE_WIFI_SSID,
  344. .password = EXAMPLE_WIFI_PASSWORD,
  345. .rm_enabled =1,
  346. .btm_enabled =1,
  347. .mbo_enabled =1,
  348. .pmf_cfg.capable = 1,
  349. .ft_enabled =1,
  350. },
  351. };
  352. ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
  353. ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
  354. ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
  355. ESP_ERROR_CHECK( esp_wifi_start() );
  356. }
  357. void app_main(void)
  358. {
  359. ESP_ERROR_CHECK( nvs_flash_init() );
  360. initialise_wifi();
  361. }