mesh_netif.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* Mesh IP Internal Communication Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "esp_wifi.h"
  8. #include "esp_netif.h"
  9. #include "esp_log.h"
  10. #include <string.h>
  11. #include "esp_mesh.h"
  12. #include "lwip/lwip_napt.h"
  13. #include "esp_wifi_netif.h"
  14. #include "mesh_netif.h"
  15. /*******************************************************
  16. * Macros
  17. *******************************************************/
  18. #define RX_SIZE (1560)
  19. #if CONFIG_MESH_USE_GLOBAL_DNS_IP
  20. #define DNS_IP_ADDR CONFIG_MESH_GLOBAL_DNS_IP
  21. #endif
  22. /*******************************************************
  23. * Type Definitions
  24. *******************************************************/
  25. typedef struct mesh_netif_driver* mesh_netif_driver_t;
  26. typedef struct mesh_netif_driver {
  27. esp_netif_driver_base_t base;
  28. uint8_t sta_mac_addr[MAC_ADDR_LEN];
  29. }* mesh_netif_driver_t;
  30. /*******************************************************
  31. * Constants
  32. *******************************************************/
  33. static const char* TAG = "mesh_netif";
  34. const esp_netif_ip_info_t g_mesh_netif_subnet_ip = { // mesh subnet IP info
  35. .ip = { .addr = ESP_IP4TOADDR( 10, 0, 0, 1) },
  36. .gw = { .addr = ESP_IP4TOADDR( 10, 0, 0, 1) },
  37. .netmask = { .addr = ESP_IP4TOADDR( 255, 255, 0, 0) },
  38. };
  39. /*******************************************************
  40. * Variable Definitions
  41. *******************************************************/
  42. static esp_netif_t *netif_sta = NULL;
  43. static esp_netif_t *netif_ap = NULL;
  44. static bool receive_task_is_running = false;
  45. static mesh_addr_t s_route_table[CONFIG_MESH_ROUTE_TABLE_SIZE] = { 0 };
  46. static mesh_raw_recv_cb_t *s_mesh_raw_recv_cb = NULL;
  47. /*******************************************************
  48. * Function Definitions
  49. *******************************************************/
  50. // setup DHCP server's DNS OFFER
  51. //
  52. static esp_err_t set_dhcps_dns(esp_netif_t *netif, uint32_t addr)
  53. {
  54. esp_netif_dns_info_t dns;
  55. dns.ip.u_addr.ip4.addr = addr;
  56. dns.ip.type = IPADDR_TYPE_V4;
  57. dhcps_offer_t dhcps_dns_value = OFFER_DNS;
  58. ESP_ERROR_CHECK(esp_netif_dhcps_option(netif, ESP_NETIF_OP_SET, ESP_NETIF_DOMAIN_NAME_SERVER, &dhcps_dns_value, sizeof(dhcps_dns_value)));
  59. ESP_ERROR_CHECK(esp_netif_set_dns_info(netif, ESP_NETIF_DNS_MAIN, &dns));
  60. return ESP_OK;
  61. }
  62. // Receive task
  63. //
  64. static void receive_task(void *arg)
  65. {
  66. esp_err_t err;
  67. mesh_addr_t from;
  68. int flag = 0;
  69. mesh_data_t data;
  70. static uint8_t rx_buf[RX_SIZE] = { 0, };
  71. ESP_LOGD(TAG, "Receiving task started");
  72. while (receive_task_is_running) {
  73. data.data = rx_buf;
  74. data.size = RX_SIZE;
  75. err = esp_mesh_recv(&from, &data, portMAX_DELAY, &flag, NULL, 0);
  76. if (err != ESP_OK) {
  77. ESP_LOGE(TAG, "Received with err code %d %s", err, esp_err_to_name(err));
  78. continue;
  79. }
  80. if (data.proto == MESH_PROTO_BIN && s_mesh_raw_recv_cb) {
  81. s_mesh_raw_recv_cb(&from, &data);
  82. }
  83. if (esp_mesh_is_root()) {
  84. if (data.proto == MESH_PROTO_AP) {
  85. ESP_LOGD(TAG, "Root received: from: " MACSTR " to " MACSTR " size: %d",
  86. MAC2STR((uint8_t*)data.data) ,MAC2STR((uint8_t*)(data.data+6)), data.size);
  87. if (netif_ap) {
  88. // actual receive to TCP/IP stack
  89. esp_netif_receive(netif_ap, data.data, data.size, NULL);
  90. }
  91. } else if (data.proto == MESH_PROTO_STA) {
  92. ESP_LOGE(TAG, "Root station Should never receive data from mesh!");
  93. }
  94. } else {
  95. if (data.proto == MESH_PROTO_AP) {
  96. ESP_LOGD(TAG, "Node AP should never receive data from mesh");
  97. } else if (data.proto == MESH_PROTO_STA) {
  98. ESP_LOGD(TAG, "Node received: from: " MACSTR " to " MACSTR " size: %d",
  99. MAC2STR((uint8_t*)data.data) ,MAC2STR((uint8_t*)(data.data+6)), data.size);
  100. if (netif_sta) {
  101. // actual receive to TCP/IP stack
  102. esp_netif_receive(netif_sta, data.data, data.size, NULL);
  103. }
  104. }
  105. }
  106. }
  107. vTaskDelete(NULL);
  108. }
  109. // Free RX buffer (not used as the buffer is static)
  110. //
  111. static void mesh_free(void *h, void* buffer)
  112. {
  113. free(buffer);
  114. }
  115. // Transmit function variants
  116. //
  117. static esp_err_t mesh_netif_transmit_from_root_ap(void *h, void *buffer, size_t len)
  118. {
  119. // Use only to transmit data from root AP to node's AP
  120. static const uint8_t eth_broadcast[MAC_ADDR_LEN] = { 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
  121. int route_table_size = 0;
  122. mesh_netif_driver_t mesh_driver = h;
  123. mesh_addr_t dest_addr;
  124. mesh_data_t data;
  125. ESP_LOGD(TAG, "Sending to node: " MACSTR ", size: %d" ,MAC2STR((uint8_t*)buffer), len);
  126. memcpy(dest_addr.addr, buffer, MAC_ADDR_LEN);
  127. data.data = buffer;
  128. data.size = len;
  129. data.proto = MESH_PROTO_STA; // sending from root AP -> Node's STA
  130. data.tos = MESH_TOS_P2P;
  131. if (MAC_ADDR_EQUAL(dest_addr.addr, eth_broadcast)) {
  132. ESP_LOGD(TAG, "Broadcasting!");
  133. esp_mesh_get_routing_table((mesh_addr_t *) &s_route_table,
  134. CONFIG_MESH_ROUTE_TABLE_SIZE * 6, &route_table_size);
  135. for (int i = 0; i < route_table_size; i++) {
  136. if (MAC_ADDR_EQUAL(s_route_table[i].addr, mesh_driver->sta_mac_addr)) {
  137. ESP_LOGD(TAG, "That was me, skipping!");
  138. continue;
  139. }
  140. ESP_LOGD(TAG, "Broadcast: Sending to [%d] " MACSTR, i, MAC2STR(s_route_table[i].addr));
  141. esp_err_t err = esp_mesh_send(&s_route_table[i], &data, MESH_DATA_P2P, NULL, 0);
  142. if (ESP_OK != err) {
  143. ESP_LOGE(TAG, "Send with err code %d %s", err, esp_err_to_name(err));
  144. }
  145. }
  146. } else {
  147. // Standard P2P
  148. esp_err_t err = esp_mesh_send(&dest_addr, &data, MESH_DATA_P2P, NULL, 0);
  149. if (err != ESP_OK) {
  150. ESP_LOGE(TAG, "Send with err code %d %s", err, esp_err_to_name(err));
  151. return err;
  152. }
  153. }
  154. return ESP_OK;
  155. }
  156. static esp_err_t mesh_netif_transmit_from_root_ap_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
  157. {
  158. return mesh_netif_transmit_from_root_ap(h, buffer, len);
  159. }
  160. static esp_err_t mesh_netif_transmit_from_node_sta(void *h, void *buffer, size_t len)
  161. {
  162. mesh_data_t data;
  163. ESP_LOGD(TAG, "Sending to root, dest addr: " MACSTR ", size: %d" ,MAC2STR((uint8_t*)buffer), len);
  164. data.data = buffer;
  165. data.size = len;
  166. data.proto = MESH_PROTO_AP; // Node's station transmits data to root's AP
  167. data.tos = MESH_TOS_P2P;
  168. esp_err_t err = esp_mesh_send(NULL, &data, MESH_DATA_TODS, NULL, 0);
  169. if (err != ESP_OK) {
  170. ESP_LOGE(TAG, "Send with err code %d %s", err, esp_err_to_name(err));
  171. }
  172. return err;
  173. }
  174. static esp_err_t mesh_netif_transmit_from_node_sta_wrap(void *h, void *buffer, size_t len, void *netstack_buf)
  175. {
  176. return mesh_netif_transmit_from_node_sta(h, buffer, len);
  177. }
  178. // Construct and Destruct functions
  179. //
  180. static esp_err_t mesh_driver_start_root_ap(esp_netif_t * esp_netif, void * args)
  181. {
  182. mesh_netif_driver_t driver = args;
  183. driver->base.netif = esp_netif;
  184. esp_netif_driver_ifconfig_t driver_ifconfig = {
  185. .handle = driver,
  186. .transmit = mesh_netif_transmit_from_root_ap,
  187. .transmit_wrap = mesh_netif_transmit_from_root_ap_wrap,
  188. .driver_free_rx_buffer = mesh_free
  189. };
  190. return esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
  191. }
  192. static esp_err_t mesh_driver_start_node_sta(esp_netif_t * esp_netif, void * args)
  193. {
  194. mesh_netif_driver_t driver = args;
  195. driver->base.netif = esp_netif;
  196. esp_netif_driver_ifconfig_t driver_ifconfig = {
  197. .handle = driver,
  198. .transmit = mesh_netif_transmit_from_node_sta,
  199. .transmit_wrap = mesh_netif_transmit_from_node_sta_wrap,
  200. .driver_free_rx_buffer = mesh_free
  201. };
  202. return esp_netif_set_driver_config(esp_netif, &driver_ifconfig);
  203. }
  204. void mesh_delete_if_driver(mesh_netif_driver_t driver)
  205. {
  206. // Stop the task once both drivers are removed
  207. // receive_task_is_running = true;
  208. free(driver);
  209. }
  210. mesh_netif_driver_t mesh_create_if_driver(bool is_ap, bool is_root)
  211. {
  212. mesh_netif_driver_t driver = calloc(1, sizeof(struct mesh_netif_driver));
  213. if (driver == NULL) {
  214. ESP_LOGE(TAG, "No memory to create a wifi interface handle");
  215. return NULL;
  216. }
  217. if (is_ap && is_root) {
  218. driver->base.post_attach = mesh_driver_start_root_ap;
  219. } else if (!is_ap && !is_root) {
  220. driver->base.post_attach = mesh_driver_start_node_sta;
  221. } else {
  222. return NULL;
  223. }
  224. if (!receive_task_is_running) {
  225. receive_task_is_running = true;
  226. xTaskCreate(receive_task, "netif rx task", 3072, NULL, 5, NULL);
  227. }
  228. // save station mac address to exclude it from routing-table on broadcast
  229. esp_wifi_get_mac(WIFI_IF_STA, driver->sta_mac_addr);
  230. return driver;
  231. }
  232. esp_err_t mesh_netifs_destroy(void)
  233. {
  234. receive_task_is_running = false;
  235. return ESP_OK;
  236. }
  237. static void mesh_netif_init_station(void)
  238. {
  239. // By default create a station that would connect to AP (expecting root to connect to external network)
  240. esp_netif_config_t cfg_sta = ESP_NETIF_DEFAULT_WIFI_STA();
  241. netif_sta = esp_netif_new(&cfg_sta);
  242. assert(netif_sta);
  243. ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif_sta));
  244. ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
  245. }
  246. // Init by default for both potential root and node
  247. //
  248. esp_err_t mesh_netifs_init(mesh_raw_recv_cb_t *cb)
  249. {
  250. mesh_netif_init_station();
  251. s_mesh_raw_recv_cb = cb;
  252. return ESP_OK;
  253. }
  254. /**
  255. * @brief Starts AP esp-netif link over mesh (root's AP on mesh)
  256. */
  257. static esp_err_t start_mesh_link_ap(void)
  258. {
  259. uint8_t mac[MAC_ADDR_LEN];
  260. esp_wifi_get_mac(WIFI_IF_AP, mac);
  261. esp_netif_set_mac(netif_ap, mac);
  262. esp_netif_action_start(netif_ap, NULL, 0, NULL);
  263. return ESP_OK;
  264. }
  265. /**
  266. * @brief Starts station link over wifi (root node to the router)
  267. */
  268. static esp_err_t start_wifi_link_sta(void)
  269. {
  270. uint8_t mac[6];
  271. esp_wifi_get_mac(WIFI_IF_STA, mac);
  272. esp_err_t ret;
  273. void *driver = esp_netif_get_io_driver(netif_sta);
  274. if ((ret = esp_wifi_register_if_rxcb(driver, esp_netif_receive, netif_sta)) != ESP_OK) {
  275. ESP_LOGE(TAG, "esp_wifi_register_if_rxcb for if=%p failed with %d", driver, ret);
  276. return ESP_FAIL;
  277. }
  278. esp_netif_set_mac(netif_sta, mac);
  279. esp_netif_action_start(netif_sta, NULL, 0, NULL);
  280. return ESP_OK;
  281. }
  282. /**
  283. * @brief Starts station link over mesh (node to root over mesh)
  284. */
  285. static esp_err_t start_mesh_link_sta(void)
  286. {
  287. uint8_t mac[MAC_ADDR_LEN];
  288. esp_wifi_get_mac(WIFI_IF_STA, mac);
  289. esp_netif_set_mac(netif_sta, mac);
  290. esp_netif_action_start(netif_sta, NULL, 0, NULL);
  291. esp_netif_action_connected(netif_sta, NULL, 0, NULL);
  292. return ESP_OK;
  293. }
  294. /**
  295. * @brief Creates esp-netif for AP interface over mesh
  296. *
  297. * @return Pointer to esp-netif instance
  298. */
  299. static esp_netif_t* create_mesh_link_ap(void)
  300. {
  301. esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
  302. base_cfg.if_desc = "mesh_link_ap";
  303. base_cfg.ip_info = &g_mesh_netif_subnet_ip;
  304. esp_netif_config_t cfg = {
  305. .base = &base_cfg,
  306. .driver = NULL,
  307. .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP };
  308. esp_netif_t * netif = esp_netif_new(&cfg);
  309. assert(netif);
  310. return netif;
  311. }
  312. /**
  313. * @brief Creates esp-netif for station interface over mesh
  314. *
  315. * @note Interface needs to be started (later) using the above APIs
  316. * based on the actual configuration root/node,
  317. * since root connects normally over wifi
  318. *
  319. * @return Pointer to esp-netif instance
  320. */
  321. static esp_netif_t* create_mesh_link_sta(void)
  322. {
  323. esp_netif_inherent_config_t base_cfg = ESP_NETIF_INHERENT_DEFAULT_WIFI_STA();
  324. base_cfg.if_desc = "mesh_link_sta";
  325. esp_netif_config_t cfg = {
  326. .base = &base_cfg,
  327. .driver = NULL,
  328. .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
  329. esp_netif_t * netif = esp_netif_new(&cfg);
  330. assert(netif);
  331. return netif;
  332. }
  333. esp_err_t mesh_netif_start_root_ap(bool is_root, uint32_t addr)
  334. {
  335. if (is_root) {
  336. if (netif_ap) {
  337. esp_netif_action_disconnected(netif_ap, NULL, 0, NULL);
  338. mesh_delete_if_driver(esp_netif_get_io_driver(netif_ap));
  339. esp_netif_destroy(netif_ap);
  340. netif_ap = NULL;
  341. }
  342. netif_ap = create_mesh_link_ap();
  343. mesh_netif_driver_t driver = mesh_create_if_driver(true, true);
  344. if (driver == NULL) {
  345. ESP_LOGE(TAG, "Failed to create wifi interface handle");
  346. return ESP_FAIL;
  347. }
  348. esp_netif_attach(netif_ap, driver);
  349. set_dhcps_dns(netif_ap, addr);
  350. start_mesh_link_ap();
  351. ip_napt_enable(g_mesh_netif_subnet_ip.ip.addr, 1);
  352. }
  353. return ESP_OK;
  354. }
  355. esp_err_t mesh_netifs_start(bool is_root)
  356. {
  357. if (is_root) {
  358. // ROOT: need both sta should use standard wifi, AP mesh link netif
  359. // Root: Station
  360. if (netif_sta && strcmp(esp_netif_get_desc(netif_sta), "sta") == 0) {
  361. ESP_LOGI(TAG, "Already wifi station, no need to do anything");
  362. } else if (netif_sta && strcmp(esp_netif_get_desc(netif_sta), "mesh_link_sta") == 0) {
  363. esp_netif_action_disconnected(netif_sta, NULL, 0, NULL);
  364. mesh_delete_if_driver(esp_netif_get_io_driver(netif_sta));
  365. esp_netif_destroy(netif_sta);
  366. mesh_netif_init_station();
  367. } else if (netif_sta == NULL) {
  368. mesh_netif_init_station();
  369. }
  370. // Root: AP is initialized only if GLOBAL DNS configured
  371. // (otherwise have to wait until the actual DNS record received from the router)
  372. #if CONFIG_MESH_USE_GLOBAL_DNS_IP
  373. mesh_netif_start_root_ap(true, htonl(DNS_IP_ADDR));
  374. #endif
  375. } else {
  376. // NODE: create only STA in form of mesh link
  377. if (netif_sta && strcmp(esp_netif_get_desc(netif_sta), "mesh_link_sta") == 0) {
  378. ESP_LOGI(TAG, "Already mesh link station, no need to do anything");
  379. return ESP_OK;
  380. }
  381. if (netif_sta) {
  382. esp_netif_action_disconnected(netif_sta, NULL, 0, NULL);
  383. // should remove the actual driver
  384. if (strcmp(esp_netif_get_desc(netif_sta), "sta") == 0) {
  385. ESP_LOGI(TAG, "It was a wifi station removing stuff");
  386. esp_wifi_clear_default_wifi_driver_and_handlers(netif_sta);
  387. }
  388. esp_netif_destroy(netif_sta);
  389. }
  390. netif_sta = create_mesh_link_sta();
  391. // now we create a mesh driver and attach it to the existing netif
  392. mesh_netif_driver_t driver = mesh_create_if_driver(false, false);
  393. if (driver == NULL) {
  394. ESP_LOGE(TAG, "Failed to create wifi interface handle");
  395. return ESP_FAIL;
  396. }
  397. esp_netif_attach(netif_sta, driver);
  398. start_mesh_link_sta();
  399. // If we have a AP on NODE -> stop and remove it!
  400. if (netif_ap) {
  401. esp_netif_action_disconnected(netif_ap, NULL, 0, NULL);
  402. mesh_delete_if_driver(esp_netif_get_io_driver(netif_ap));
  403. esp_netif_destroy(netif_ap);
  404. netif_ap = NULL;
  405. }
  406. }
  407. return ESP_OK;
  408. }
  409. esp_err_t mesh_netifs_stop(void)
  410. {
  411. if (netif_sta && strcmp(esp_netif_get_desc(netif_sta), "sta") == 0 && netif_ap == NULL) {
  412. return ESP_OK;
  413. }
  414. if (netif_sta) {
  415. if (strcmp(esp_netif_get_desc(netif_sta), "sta") == 0) {
  416. esp_netif_action_disconnected(netif_sta, NULL, 0, NULL);
  417. esp_netif_action_stop(netif_sta, NULL, 0, NULL);
  418. esp_wifi_clear_default_wifi_driver_and_handlers(netif_sta);
  419. } else {
  420. esp_netif_action_disconnected(netif_sta, NULL, 0, NULL);
  421. mesh_delete_if_driver(esp_netif_get_io_driver(netif_sta));
  422. }
  423. esp_netif_destroy(netif_sta);
  424. netif_sta = NULL;
  425. }
  426. if (netif_ap) {
  427. esp_netif_action_disconnected(netif_ap, NULL, 0, NULL);
  428. mesh_delete_if_driver(esp_netif_get_io_driver(netif_ap));
  429. esp_netif_destroy(netif_ap);
  430. netif_ap = NULL;
  431. }
  432. // reserve the default (STA gets ready to become root)
  433. mesh_netif_init_station();
  434. start_wifi_link_sta();
  435. return ESP_OK;
  436. }
  437. uint8_t* mesh_netif_get_station_mac(void)
  438. {
  439. mesh_netif_driver_t mesh = esp_netif_get_io_driver(netif_sta);
  440. return mesh->sta_mac_addr;
  441. }