roaming_example.c 10 KB

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