roaming_example.c 11 KB

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