esp_netif.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ESP_NETIF_H_
  7. #define _ESP_NETIF_H_
  8. #include <stdint.h>
  9. #include "sdkconfig.h"
  10. #include "esp_netif_ip_addr.h"
  11. #include "esp_netif_types.h"
  12. #include "esp_netif_defaults.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /**
  17. * @defgroup ESP_NETIF_INIT_API ESP-NETIF Initialization API
  18. * @brief Initialization and deinitialization of underlying TCP/IP stack and esp-netif instances
  19. *
  20. */
  21. /** @addtogroup ESP_NETIF_INIT_API
  22. * @{
  23. */
  24. /**
  25. * @brief Initialize the underlying TCP/IP stack
  26. *
  27. * @return
  28. * - ESP_OK on success
  29. * - ESP_FAIL if initializing failed
  30. * @note This function should be called exactly once from application code, when the application starts up.
  31. */
  32. esp_err_t esp_netif_init(void);
  33. /**
  34. * @brief Deinitialize the esp-netif component (and the underlying TCP/IP stack)
  35. *
  36. * Note: Deinitialization is not supported yet
  37. *
  38. * @return
  39. * - ESP_ERR_INVALID_STATE if esp_netif not initialized
  40. * - ESP_ERR_NOT_SUPPORTED otherwise
  41. */
  42. esp_err_t esp_netif_deinit(void);
  43. /**
  44. * @brief Creates an instance of new esp-netif object based on provided config
  45. *
  46. * @param[in] esp_netif_config pointer esp-netif configuration
  47. *
  48. * @return
  49. * - pointer to esp-netif object on success
  50. * - NULL otherwise
  51. */
  52. esp_netif_t *esp_netif_new(const esp_netif_config_t *esp_netif_config);
  53. /**
  54. * @brief Destroys the esp_netif object
  55. *
  56. * @param[in] esp_netif pointer to the object to be deleted
  57. */
  58. void esp_netif_destroy(esp_netif_t *esp_netif);
  59. /**
  60. * @brief Configures driver related options of esp_netif object
  61. *
  62. * @param[inout] esp_netif pointer to the object to be configured
  63. * @param[in] driver_config pointer esp-netif io driver related configuration
  64. * @return
  65. * - ESP_OK on success
  66. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS if invalid parameters provided
  67. *
  68. */
  69. esp_err_t esp_netif_set_driver_config(esp_netif_t *esp_netif,
  70. const esp_netif_driver_ifconfig_t *driver_config);
  71. /**
  72. * @brief Attaches esp_netif instance to the io driver handle
  73. *
  74. * Calling this function enables connecting specific esp_netif object
  75. * with already initialized io driver to update esp_netif object with driver
  76. * specific configuration (i.e. calls post_attach callback, which typically
  77. * sets io driver callbacks to esp_netif instance and starts the driver)
  78. *
  79. * @param[inout] esp_netif pointer to esp_netif object to be attached
  80. * @param[in] driver_handle pointer to the driver handle
  81. * @return
  82. * - ESP_OK on success
  83. * - ESP_ERR_ESP_NETIF_DRIVER_ATTACH_FAILED if driver's pot_attach callback failed
  84. */
  85. esp_err_t esp_netif_attach(esp_netif_t *esp_netif, esp_netif_iodriver_handle driver_handle);
  86. /**
  87. * @}
  88. */
  89. /**
  90. * @defgroup ESP_NETIF_DATA_IO_API ESP-NETIF Input Output API
  91. * @brief Input and Output functions to pass data packets from communication media (IO driver)
  92. * to TCP/IP stack.
  93. *
  94. * These functions are usually not directly called from user code, but installed, or registered
  95. * as callbacks in either IO driver on one hand or TCP/IP stack on the other. More specifically
  96. * esp_netif_receive is typically called from io driver on reception callback to input the packets
  97. * to TCP/IP stack. Similarly esp_netif_transmit is called from the TCP/IP stack whenever
  98. * a packet ought to output to the communication media.
  99. *
  100. * @note These IO functions are registerd (installed) automatically for default interfaces
  101. * (interfaces with the keys such as WIFI_STA_DEF, WIFI_AP_DEF, ETH_DEF). Custom interface
  102. * has to register these IO functions when creating interface using @ref esp_netif_new
  103. *
  104. */
  105. /** @addtogroup ESP_NETIF_DATA_IO_API
  106. * @{
  107. */
  108. /**
  109. * @brief Passes the raw packets from communication media to the appropriate TCP/IP stack
  110. *
  111. * This function is called from the configured (peripheral) driver layer.
  112. * The data are then forwarded as frames to the TCP/IP stack.
  113. *
  114. * @param[in] esp_netif Handle to esp-netif instance
  115. * @param[in] buffer Received data
  116. * @param[in] len Length of the data frame
  117. * @param[in] eb Pointer to internal buffer (used in Wi-Fi driver)
  118. *
  119. * @return
  120. * - ESP_OK
  121. */
  122. esp_err_t esp_netif_receive(esp_netif_t *esp_netif, void *buffer, size_t len, void *eb);
  123. /**
  124. * @}
  125. */
  126. /**
  127. * @defgroup ESP_NETIF_LIFECYCLE ESP-NETIF Lifecycle control
  128. * @brief These APIS define basic building blocks to control network interface lifecycle, i.e.
  129. * start, stop, set_up or set_down. These functions can be directly used as event handlers
  130. * registered to follow the events from communication media.
  131. */
  132. /** @addtogroup ESP_NETIF_LIFECYCLE
  133. * @{
  134. */
  135. /**
  136. * @brief Default building block for network interface action upon IO driver start event
  137. * Creates network interface, if AUTOUP enabled turns the interface on,
  138. * if DHCPS enabled starts dhcp server
  139. *
  140. * @note This API can be directly used as event handler
  141. *
  142. * @param[in] esp_netif Handle to esp-netif instance
  143. * @param base The base type of the event
  144. * @param event_id The specific ID of the event
  145. * @param data Optional data associated with the event
  146. */
  147. void esp_netif_action_start(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  148. /**
  149. * @brief Default building block for network interface action upon IO driver stop event
  150. *
  151. * @note This API can be directly used as event handler
  152. *
  153. * @param[in] esp_netif Handle to esp-netif instance
  154. * @param base The base type of the event
  155. * @param event_id The specific ID of the event
  156. * @param data Optional data associated with the event
  157. */
  158. void esp_netif_action_stop(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  159. /**
  160. * @brief Default building block for network interface action upon IO driver connected event
  161. *
  162. * @note This API can be directly used as event handler
  163. *
  164. * @param[in] esp_netif Handle to esp-netif instance
  165. * @param base The base type of the event
  166. * @param event_id The specific ID of the event
  167. * @param data Optional data associated with the event
  168. */
  169. void esp_netif_action_connected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  170. /**
  171. * @brief Default building block for network interface action upon IO driver disconnected event
  172. *
  173. * @note This API can be directly used as event handler
  174. *
  175. * @param[in] esp_netif Handle to esp-netif instance
  176. * @param base The base type of the event
  177. * @param event_id The specific ID of the event
  178. * @param data Optional data associated with the event
  179. */
  180. void esp_netif_action_disconnected(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  181. /**
  182. * @brief Default building block for network interface action upon network got IP event
  183. *
  184. * @note This API can be directly used as event handler
  185. *
  186. * @param[in] esp_netif Handle to esp-netif instance
  187. * @param base The base type of the event
  188. * @param event_id The specific ID of the event
  189. * @param data Optional data associated with the event
  190. */
  191. void esp_netif_action_got_ip(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  192. /**
  193. * @brief Default building block for network interface action upon IPv6 multicast group join
  194. *
  195. * @note This API can be directly used as event handler
  196. *
  197. * @param[in] esp_netif Handle to esp-netif instance
  198. * @param base The base type of the event
  199. * @param event_id The specific ID of the event
  200. * @param data Optional data associated with the event
  201. */
  202. void esp_netif_action_join_ip6_multicast_group(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  203. /**
  204. * @brief Default building block for network interface action upon IPv6 multicast group leave
  205. *
  206. * @note This API can be directly used as event handler
  207. *
  208. * @param[in] esp_netif Handle to esp-netif instance
  209. * @param base The base type of the event
  210. * @param event_id The specific ID of the event
  211. * @param data Optional data associated with the event
  212. */
  213. void esp_netif_action_leave_ip6_multicast_group(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  214. /**
  215. * @brief Default building block for network interface action upon IPv6 address added by the underlying stack
  216. *
  217. * @note This API can be directly used as event handler
  218. *
  219. * @param[in] esp_netif Handle to esp-netif instance
  220. * @param base The base type of the event
  221. * @param event_id The specific ID of the event
  222. * @param data Optional data associated with the event
  223. */
  224. void esp_netif_action_add_ip6_address(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  225. /**
  226. * @brief Default building block for network interface action upon IPv6 address removed by the underlying stack
  227. *
  228. * @note This API can be directly used as event handler
  229. *
  230. * @param[in] esp_netif Handle to esp-netif instance
  231. * @param base The base type of the event
  232. * @param event_id The specific ID of the event
  233. * @param data Optional data associated with the event
  234. */
  235. void esp_netif_action_remove_ip6_address(void *esp_netif, esp_event_base_t base, int32_t event_id, void *data);
  236. /**
  237. * @brief Manual configuration of the default netif
  238. *
  239. * This API overrides the automatic configuration of the default interface based on the route_prio
  240. * If the selected netif is set default using this API, no other interface could be set-default disregarding
  241. * its route_prio number (unless the selected netif gets destroyed)
  242. *
  243. * @param[in] esp_netif Handle to esp-netif instance
  244. * @return ESP_OK on success
  245. */
  246. esp_err_t esp_netif_set_default_netif(esp_netif_t *esp_netif);
  247. /**
  248. * @brief Getter function of the default netif
  249. *
  250. * This API returns the selected default netif.
  251. *
  252. * @return Handle to esp-netif instance of the default netif.
  253. */
  254. esp_netif_t* esp_netif_get_default_netif(void);
  255. #if CONFIG_ESP_NETIF_BRIDGE_EN
  256. /**
  257. * @brief Add a port to the bridge
  258. *
  259. * @param esp_netif_br Handle to bridge esp-netif instance
  260. * @param esp_netif_port Handle to port esp-netif instance
  261. * @return ESP_OK on success
  262. */
  263. esp_err_t esp_netif_bridge_add_port(esp_netif_t *esp_netif_br, esp_netif_t *esp_netif_port);
  264. /**
  265. * @brief Add a static entry to bridge forwarding database
  266. *
  267. * @param esp_netif_br Handle to bridge esp-netif instance
  268. * @param addr MAC address entry to be added
  269. * @param ports_mask Port(s) mask where to be the address forwarded
  270. * @return ESP_OK on success
  271. */
  272. esp_err_t esp_netif_bridge_fdb_add(esp_netif_t *esp_netif_br, uint8_t *addr, uint64_t ports_mask);
  273. /**
  274. * @brief Remove a static entry from bridge forwarding database
  275. *
  276. * @param esp_netif_br Handle to bridge esp-netif instance
  277. * @param addr MAC address entry to be removed
  278. * @return ESP_OK on success
  279. */
  280. esp_err_t esp_netif_bridge_fdb_remove(esp_netif_t *esp_netif_br, uint8_t *addr);
  281. #endif // CONFIG_ESP_NETIF_BRIDGE_EN
  282. /**
  283. * @brief Cause the TCP/IP stack to join a IPv6 multicast group
  284. *
  285. * @param[in] esp_netif Handle to esp-netif instance
  286. * @param[in] addr The multicast group to join
  287. *
  288. * @return
  289. * - ESP_OK
  290. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  291. * - ESP_ERR_ESP_NETIF_MLD6_FAILED
  292. * - ESP_ERR_NO_MEM
  293. */
  294. esp_err_t esp_netif_join_ip6_multicast_group(esp_netif_t *esp_netif, const esp_ip6_addr_t *addr);
  295. /**
  296. * @brief Cause the TCP/IP stack to leave a IPv6 multicast group
  297. *
  298. * @param[in] esp_netif Handle to esp-netif instance
  299. * @param[in] addr The multicast group to leave
  300. *
  301. * @return
  302. * - ESP_OK
  303. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  304. * - ESP_ERR_ESP_NETIF_MLD6_FAILED
  305. * - ESP_ERR_NO_MEM
  306. */
  307. esp_err_t esp_netif_leave_ip6_multicast_group(esp_netif_t *esp_netif, const esp_ip6_addr_t *addr);
  308. /**
  309. * @}
  310. */
  311. /**
  312. * @defgroup ESP_NETIF_GET_SET ESP-NETIF Runtime configuration
  313. * @brief Getters and setters for various TCP/IP related parameters
  314. */
  315. /** @addtogroup ESP_NETIF_GET_SET
  316. * @{
  317. */
  318. /**
  319. * @brief Set the mac address for the interface instance
  320. * @param[in] esp_netif Handle to esp-netif instance
  321. * @param[in] mac Desired mac address for the related network interface
  322. * @return
  323. * - ESP_OK - success
  324. * - ESP_ERR_ESP_NETIF_IF_NOT_READY - interface status error
  325. * - ESP_ERR_NOT_SUPPORTED - mac not supported on this interface
  326. */
  327. esp_err_t esp_netif_set_mac(esp_netif_t *esp_netif, uint8_t mac[]);
  328. /**
  329. * @brief Get the mac address for the interface instance
  330. * @param[in] esp_netif Handle to esp-netif instance
  331. * @param[out] mac Resultant mac address for the related network interface
  332. * @return
  333. * - ESP_OK - success
  334. * - ESP_ERR_ESP_NETIF_IF_NOT_READY - interface status error
  335. * - ESP_ERR_NOT_SUPPORTED - mac not supported on this interface
  336. */
  337. esp_err_t esp_netif_get_mac(esp_netif_t *esp_netif, uint8_t mac[]);
  338. /**
  339. * @brief Set the hostname of an interface
  340. *
  341. * The configured hostname overrides the default configuration value CONFIG_LWIP_LOCAL_HOSTNAME.
  342. * Please note that when the hostname is altered after interface started/connected the changes
  343. * would only be reflected once the interface restarts/reconnects
  344. *
  345. * @param[in] esp_netif Handle to esp-netif instance
  346. * @param[in] hostname New hostname for the interface. Maximum length 32 bytes.
  347. *
  348. * @return
  349. * - ESP_OK - success
  350. * - ESP_ERR_ESP_NETIF_IF_NOT_READY - interface status error
  351. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS - parameter error
  352. */
  353. esp_err_t esp_netif_set_hostname(esp_netif_t *esp_netif, const char *hostname);
  354. /**
  355. * @brief Get interface hostname.
  356. *
  357. * @param[in] esp_netif Handle to esp-netif instance
  358. * @param[out] hostname Returns a pointer to the hostname. May be NULL if no hostname is set. If set non-NULL, pointer remains valid (and string may change if the hostname changes).
  359. *
  360. * @return
  361. * - ESP_OK - success
  362. * - ESP_ERR_ESP_NETIF_IF_NOT_READY - interface status error
  363. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS - parameter error
  364. */
  365. esp_err_t esp_netif_get_hostname(esp_netif_t *esp_netif, const char **hostname);
  366. /**
  367. * @brief Test if supplied interface is up or down
  368. *
  369. * @param[in] esp_netif Handle to esp-netif instance
  370. *
  371. * @return
  372. * - true - Interface is up
  373. * - false - Interface is down
  374. */
  375. bool esp_netif_is_netif_up(esp_netif_t *esp_netif);
  376. /**
  377. * @brief Get interface's IP address information
  378. *
  379. * If the interface is up, IP information is read directly from the TCP/IP stack.
  380. * If the interface is down, IP information is read from a copy kept in the ESP-NETIF instance
  381. *
  382. * @param[in] esp_netif Handle to esp-netif instance
  383. * @param[out] ip_info If successful, IP information will be returned in this argument.
  384. *
  385. * @return
  386. * - ESP_OK
  387. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  388. */
  389. esp_err_t esp_netif_get_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info);
  390. /**
  391. * @brief Get interface's old IP information
  392. *
  393. * Returns an "old" IP address previously stored for the interface when the valid IP changed.
  394. *
  395. * If the IP lost timer has expired (meaning the interface was down for longer than the configured interval)
  396. * then the old IP information will be zero.
  397. *
  398. * @param[in] esp_netif Handle to esp-netif instance
  399. * @param[out] ip_info If successful, IP information will be returned in this argument.
  400. *
  401. * @return
  402. * - ESP_OK
  403. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  404. */
  405. esp_err_t esp_netif_get_old_ip_info(esp_netif_t *esp_netif, esp_netif_ip_info_t *ip_info);
  406. /**
  407. * @brief Set interface's IP address information
  408. *
  409. * This function is mainly used to set a static IP on an interface.
  410. *
  411. * If the interface is up, the new IP information is set directly in the TCP/IP stack.
  412. *
  413. * The copy of IP information kept in the ESP-NETIF instance is also updated (this
  414. * copy is returned if the IP is queried while the interface is still down.)
  415. *
  416. * @note DHCP client/server must be stopped (if enabled for this interface) before setting new IP information.
  417. *
  418. * @note Calling this interface for may generate a SYSTEM_EVENT_STA_GOT_IP or SYSTEM_EVENT_ETH_GOT_IP event.
  419. *
  420. * @param[in] esp_netif Handle to esp-netif instance
  421. * @param[in] ip_info IP information to set on the specified interface
  422. *
  423. * @return
  424. * - ESP_OK
  425. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  426. * - ESP_ERR_ESP_NETIF_DHCP_NOT_STOPPED If DHCP server or client is still running
  427. */
  428. esp_err_t esp_netif_set_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info);
  429. /**
  430. * @brief Set interface old IP information
  431. *
  432. * This function is called from the DHCP client (if enabled), before a new IP is set.
  433. * It is also called from the default handlers for the SYSTEM_EVENT_STA_CONNECTED and SYSTEM_EVENT_ETH_CONNECTED events.
  434. *
  435. * Calling this function stores the previously configured IP, which can be used to determine if the IP changes in the future.
  436. *
  437. * If the interface is disconnected or down for too long, the "IP lost timer" will expire (after the configured interval) and set the old IP information to zero.
  438. *
  439. * @param[in] esp_netif Handle to esp-netif instance
  440. * @param[in] ip_info Store the old IP information for the specified interface
  441. *
  442. * @return
  443. * - ESP_OK
  444. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  445. */
  446. esp_err_t esp_netif_set_old_ip_info(esp_netif_t *esp_netif, const esp_netif_ip_info_t *ip_info);
  447. /**
  448. * @brief Get net interface index from network stack implementation
  449. *
  450. * @note This index could be used in `setsockopt()` to bind socket with multicast interface
  451. *
  452. * @param[in] esp_netif Handle to esp-netif instance
  453. *
  454. * @return
  455. * implementation specific index of interface represented with supplied esp_netif
  456. */
  457. int esp_netif_get_netif_impl_index(esp_netif_t *esp_netif);
  458. /**
  459. * @brief Get net interface name from network stack implementation
  460. *
  461. * @note This name could be used in `setsockopt()` to bind socket with appropriate interface
  462. *
  463. * @param[in] esp_netif Handle to esp-netif instance
  464. * @param[out] name Interface name as specified in underlying TCP/IP stack. Note that the
  465. * actual name will be copied to the specified buffer, which must be allocated to hold
  466. * maximum interface name size (6 characters for lwIP)
  467. *
  468. * @return
  469. * - ESP_OK
  470. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  471. */
  472. esp_err_t esp_netif_get_netif_impl_name(esp_netif_t *esp_netif, char* name);
  473. /**
  474. * @brief Enable NAPT on an interface
  475. *
  476. * @note Enable operation can be performed only on one interface at a time.
  477. * NAPT cannot be enabled on multiple interfaces according to this implementation.
  478. *
  479. * @param[in] esp_netif Handle to esp-netif instance
  480. *
  481. * @return
  482. * - ESP_OK
  483. * - ESP_FAIL
  484. * - ESP_ERR_NOT_SUPPORTED
  485. */
  486. esp_err_t esp_netif_napt_enable(esp_netif_t *esp_netif);
  487. /**
  488. * @brief Disable NAPT on an interface.
  489. *
  490. * @param[in] esp_netif Handle to esp-netif instance
  491. *
  492. * @return
  493. * - ESP_OK
  494. * - ESP_FAIL
  495. * - ESP_ERR_NOT_SUPPORTED
  496. */
  497. esp_err_t esp_netif_napt_disable(esp_netif_t *esp_netif);
  498. /**
  499. * @}
  500. */
  501. /**
  502. * @defgroup ESP_NETIF_NET_DHCP ESP-NETIF DHCP Settings
  503. * @brief Network stack related interface to DHCP client and server
  504. */
  505. /** @addtogroup ESP_NETIF_NET_DHCP
  506. * @{
  507. */
  508. /**
  509. * @brief Set or Get DHCP server option
  510. *
  511. * @param[in] esp_netif Handle to esp-netif instance
  512. * @param[in] opt_op ESP_NETIF_OP_SET to set an option, ESP_NETIF_OP_GET to get an option.
  513. * @param[in] opt_id Option index to get or set, must be one of the supported enum values.
  514. * @param[inout] opt_val Pointer to the option parameter.
  515. * @param[in] opt_len Length of the option parameter.
  516. *
  517. * @return
  518. * - ESP_OK
  519. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  520. * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED
  521. * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED
  522. */
  523. esp_err_t
  524. esp_netif_dhcps_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id,
  525. void *opt_val, uint32_t opt_len);
  526. /**
  527. * @brief Set or Get DHCP client option
  528. *
  529. * @param[in] esp_netif Handle to esp-netif instance
  530. * @param[in] opt_op ESP_NETIF_OP_SET to set an option, ESP_NETIF_OP_GET to get an option.
  531. * @param[in] opt_id Option index to get or set, must be one of the supported enum values.
  532. * @param[inout] opt_val Pointer to the option parameter.
  533. * @param[in] opt_len Length of the option parameter.
  534. *
  535. * @return
  536. * - ESP_OK
  537. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  538. * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED
  539. * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED
  540. */
  541. esp_err_t
  542. esp_netif_dhcpc_option(esp_netif_t *esp_netif, esp_netif_dhcp_option_mode_t opt_op, esp_netif_dhcp_option_id_t opt_id,
  543. void *opt_val, uint32_t opt_len);
  544. /**
  545. * @brief Start DHCP client (only if enabled in interface object)
  546. *
  547. * @note The default event handlers for the SYSTEM_EVENT_STA_CONNECTED and SYSTEM_EVENT_ETH_CONNECTED events call this function.
  548. *
  549. * @param[in] esp_netif Handle to esp-netif instance
  550. *
  551. * @return
  552. * - ESP_OK
  553. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  554. * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED
  555. * - ESP_ERR_ESP_NETIF_DHCPC_START_FAILED
  556. */
  557. esp_err_t esp_netif_dhcpc_start(esp_netif_t *esp_netif);
  558. /**
  559. * @brief Stop DHCP client (only if enabled in interface object)
  560. *
  561. * @note Calling action_netif_stop() will also stop the DHCP Client if it is running.
  562. *
  563. * @param[in] esp_netif Handle to esp-netif instance
  564. *
  565. * @return
  566. * - ESP_OK
  567. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  568. * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED
  569. * - ESP_ERR_ESP_NETIF_IF_NOT_READY
  570. */
  571. esp_err_t esp_netif_dhcpc_stop(esp_netif_t *esp_netif);
  572. /**
  573. * @brief Get DHCP client status
  574. *
  575. * @param[in] esp_netif Handle to esp-netif instance
  576. * @param[out] status If successful, the status of DHCP client will be returned in this argument.
  577. *
  578. * @return
  579. * - ESP_OK
  580. */
  581. esp_err_t esp_netif_dhcpc_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status);
  582. /**
  583. * @brief Get DHCP Server status
  584. *
  585. * @param[in] esp_netif Handle to esp-netif instance
  586. * @param[out] status If successful, the status of the DHCP server will be returned in this argument.
  587. *
  588. * @return
  589. * - ESP_OK
  590. */
  591. esp_err_t esp_netif_dhcps_get_status(esp_netif_t *esp_netif, esp_netif_dhcp_status_t *status);
  592. /**
  593. * @brief Start DHCP server (only if enabled in interface object)
  594. *
  595. * @param[in] esp_netif Handle to esp-netif instance
  596. *
  597. * @return
  598. * - ESP_OK
  599. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  600. * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED
  601. */
  602. esp_err_t esp_netif_dhcps_start(esp_netif_t *esp_netif);
  603. /**
  604. * @brief Stop DHCP server (only if enabled in interface object)
  605. *
  606. * @param[in] esp_netif Handle to esp-netif instance
  607. *
  608. * @return
  609. * - ESP_OK
  610. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  611. * - ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED
  612. * - ESP_ERR_ESP_NETIF_IF_NOT_READY
  613. */
  614. esp_err_t esp_netif_dhcps_stop(esp_netif_t *esp_netif);
  615. /**
  616. * @brief Populate IP addresses of clients connected to DHCP server listed by their MAC addresses
  617. *
  618. * @param[in] esp_netif Handle to esp-netif instance
  619. * @param[in] num Number of clients with specified MAC addresses in the array of pairs
  620. * @param[in,out] mac_ip_pair Array of pairs of MAC and IP addresses (MAC are inputs, IP outputs)
  621. * @return
  622. * - ESP_OK on success
  623. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS on invalid params
  624. * - ESP_ERR_NOT_SUPPORTED if DHCP server not enabled
  625. */
  626. esp_err_t esp_netif_dhcps_get_clients_by_mac(esp_netif_t *esp_netif, int num, esp_netif_pair_mac_ip_t *mac_ip_pair);
  627. /**
  628. * @}
  629. */
  630. /**
  631. * @defgroup ESP_NETIF_NET_DNS ESP-NETIF DNS Settings
  632. * @brief Network stack related interface to NDS
  633. */
  634. /** @addtogroup ESP_NETIF_NET_DNS
  635. * @{
  636. */
  637. /**
  638. * @brief Set DNS Server information
  639. *
  640. * This function behaves differently if DHCP server or client is enabled
  641. *
  642. * If DHCP client is enabled, main and backup DNS servers will be updated automatically
  643. * from the DHCP lease if the relevant DHCP options are set. Fallback DNS Server is never updated from the DHCP lease
  644. * and is designed to be set via this API.
  645. * If DHCP client is disabled, all DNS server types can be set via this API only.
  646. *
  647. * If DHCP server is enabled, the Main DNS Server setting is used by the DHCP server to provide a DNS Server option
  648. * to DHCP clients (Wi-Fi stations).
  649. * - The default Main DNS server is typically the IP of the DHCP server itself.
  650. * - This function can override it by setting server type ESP_NETIF_DNS_MAIN.
  651. * - Other DNS Server types are not supported for the DHCP server.
  652. * - To propagate the DNS info to client, please stop the DHCP server before using this API.
  653. *
  654. * @param[in] esp_netif Handle to esp-netif instance
  655. * @param[in] type Type of DNS Server to set: ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP, ESP_NETIF_DNS_FALLBACK
  656. * @param[in] dns DNS Server address to set
  657. *
  658. * @return
  659. * - ESP_OK on success
  660. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS invalid params
  661. */
  662. esp_err_t esp_netif_set_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns);
  663. /**
  664. * @brief Get DNS Server information
  665. *
  666. * Return the currently configured DNS Server address for the specified interface and Server type.
  667. *
  668. * This may be result of a previous call to esp_netif_set_dns_info(). If the interface's DHCP client is enabled,
  669. * the Main or Backup DNS Server may be set by the current DHCP lease.
  670. *
  671. * @param[in] esp_netif Handle to esp-netif instance
  672. * @param[in] type Type of DNS Server to get: ESP_NETIF_DNS_MAIN, ESP_NETIF_DNS_BACKUP, ESP_NETIF_DNS_FALLBACK
  673. * @param[out] dns DNS Server result is written here on success
  674. *
  675. * @return
  676. * - ESP_OK on success
  677. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS invalid params
  678. */
  679. esp_err_t esp_netif_get_dns_info(esp_netif_t *esp_netif, esp_netif_dns_type_t type, esp_netif_dns_info_t *dns);
  680. /**
  681. * @}
  682. */
  683. /**
  684. * @defgroup ESP_NETIF_NET_IP ESP-NETIF IP address related interface
  685. * @brief Network stack related interface to IP
  686. */
  687. /** @addtogroup ESP_NETIF_NET_IP
  688. * @{
  689. */
  690. #if CONFIG_LWIP_IPV6
  691. /**
  692. * @brief Create interface link-local IPv6 address
  693. *
  694. * Cause the TCP/IP stack to create a link-local IPv6 address for the specified interface.
  695. *
  696. * This function also registers a callback for the specified interface, so that if the link-local address becomes
  697. * verified as the preferred address then a SYSTEM_EVENT_GOT_IP6 event will be sent.
  698. *
  699. * @param[in] esp_netif Handle to esp-netif instance
  700. *
  701. * @return
  702. * - ESP_OK
  703. * - ESP_ERR_ESP_NETIF_INVALID_PARAMS
  704. */
  705. esp_err_t esp_netif_create_ip6_linklocal(esp_netif_t *esp_netif);
  706. /**
  707. * @brief Get interface link-local IPv6 address
  708. *
  709. * If the specified interface is up and a preferred link-local IPv6 address
  710. * has been created for the interface, return a copy of it.
  711. *
  712. * @param[in] esp_netif Handle to esp-netif instance
  713. * @param[out] if_ip6 IPv6 information will be returned in this argument if successful.
  714. *
  715. * @return
  716. * - ESP_OK
  717. * - ESP_FAIL If interface is down, does not have a link-local IPv6 address,
  718. * or the link-local IPv6 address is not a preferred address.
  719. */
  720. esp_err_t esp_netif_get_ip6_linklocal(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6);
  721. /**
  722. * @brief Get interface global IPv6 address
  723. *
  724. * If the specified interface is up and a preferred global IPv6 address
  725. * has been created for the interface, return a copy of it.
  726. *
  727. * @param[in] esp_netif Handle to esp-netif instance
  728. * @param[out] if_ip6 IPv6 information will be returned in this argument if successful.
  729. *
  730. * @return
  731. * - ESP_OK
  732. * - ESP_FAIL If interface is down, does not have a global IPv6 address,
  733. * or the global IPv6 address is not a preferred address.
  734. */
  735. esp_err_t esp_netif_get_ip6_global(esp_netif_t *esp_netif, esp_ip6_addr_t *if_ip6);
  736. /**
  737. * @brief Get all IPv6 addresses of the specified interface
  738. *
  739. * @param[in] esp_netif Handle to esp-netif instance
  740. * @param[out] if_ip6 Array of IPv6 addresses will be copied to the argument
  741. *
  742. * @return
  743. * number of returned IPv6 addresses
  744. */
  745. int esp_netif_get_all_ip6(esp_netif_t *esp_netif, esp_ip6_addr_t if_ip6[]);
  746. #endif
  747. /**
  748. * @brief Sets IPv4 address to the specified octets
  749. *
  750. * @param[out] addr IP address to be set
  751. * @param a the first octet (127 for IP 127.0.0.1)
  752. * @param b
  753. * @param c
  754. * @param d
  755. */
  756. void esp_netif_set_ip4_addr(esp_ip4_addr_t *addr, uint8_t a, uint8_t b, uint8_t c, uint8_t d);
  757. /**
  758. * @brief Converts numeric IP address into decimal dotted ASCII representation.
  759. *
  760. * @param addr ip address in network order to convert
  761. * @param buf target buffer where the string is stored
  762. * @param buflen length of buf
  763. * @return either pointer to buf which now holds the ASCII
  764. * representation of addr or NULL if buf was too small
  765. */
  766. char *esp_ip4addr_ntoa(const esp_ip4_addr_t *addr, char *buf, int buflen);
  767. /**
  768. * @brief Ascii internet address interpretation routine
  769. * The value returned is in network order.
  770. *
  771. * @param addr IP address in ascii representation (e.g. "127.0.0.1")
  772. * @return ip address in network order
  773. */
  774. uint32_t esp_ip4addr_aton(const char *addr);
  775. /**
  776. * @brief Converts Ascii internet IPv4 address into esp_ip4_addr_t
  777. *
  778. * @param[in] src IPv4 address in ascii representation (e.g. "127.0.0.1")
  779. * @param[out] dst Address of the target esp_ip4_addr_t structure to receive converted address
  780. * @return
  781. * - ESP_OK on success
  782. * - ESP_FAIL if conversion failed
  783. * - ESP_ERR_INVALID_ARG if invalid parameter is passed into
  784. */
  785. esp_err_t esp_netif_str_to_ip4(const char *src, esp_ip4_addr_t *dst);
  786. /**
  787. * @brief Converts Ascii internet IPv6 address into esp_ip4_addr_t
  788. * Zeros in the IP address can be stripped or completely ommited: "2001:db8:85a3:0:0:0:2:1" or "2001:db8::2:1")
  789. *
  790. * @param[in] src IPv6 address in ascii representation (e.g. ""2001:0db8:85a3:0000:0000:0000:0002:0001")
  791. * @param[out] dst Address of the target esp_ip6_addr_t structure to receive converted address
  792. * @return
  793. * - ESP_OK on success
  794. * - ESP_FAIL if conversion failed
  795. * - ESP_ERR_INVALID_ARG if invalid parameter is passed into
  796. */
  797. esp_err_t esp_netif_str_to_ip6(const char *src, esp_ip6_addr_t *dst);
  798. /**
  799. * @}
  800. */
  801. /**
  802. * @defgroup ESP_NETIF_CONVERT ESP-NETIF Conversion utilities
  803. * @brief ESP-NETIF conversion utilities to related keys, flags, implementation handle
  804. */
  805. /** @addtogroup ESP_NETIF_CONVERT
  806. * @{
  807. */
  808. /**
  809. * @brief Gets media driver handle for this esp-netif instance
  810. *
  811. * @param[in] esp_netif Handle to esp-netif instance
  812. *
  813. * @return opaque pointer of related IO driver
  814. */
  815. esp_netif_iodriver_handle esp_netif_get_io_driver(esp_netif_t *esp_netif);
  816. /**
  817. * @brief Searches over a list of created objects to find an instance with supplied if key
  818. *
  819. * @param if_key Textual description of network interface
  820. *
  821. * @return Handle to esp-netif instance
  822. */
  823. esp_netif_t *esp_netif_get_handle_from_ifkey(const char *if_key);
  824. /**
  825. * @brief Returns configured flags for this interface
  826. *
  827. * @param[in] esp_netif Handle to esp-netif instance
  828. *
  829. * @return Configuration flags
  830. */
  831. esp_netif_flags_t esp_netif_get_flags(esp_netif_t *esp_netif);
  832. /**
  833. * @brief Returns configured interface key for this esp-netif instance
  834. *
  835. * @param[in] esp_netif Handle to esp-netif instance
  836. *
  837. * @return Textual description of related interface
  838. */
  839. const char *esp_netif_get_ifkey(esp_netif_t *esp_netif);
  840. /**
  841. * @brief Returns configured interface type for this esp-netif instance
  842. *
  843. * @param[in] esp_netif Handle to esp-netif instance
  844. *
  845. * @return Enumerated type of this interface, such as station, AP, ethernet
  846. */
  847. const char *esp_netif_get_desc(esp_netif_t *esp_netif);
  848. /**
  849. * @brief Returns configured routing priority number
  850. *
  851. * @param[in] esp_netif Handle to esp-netif instance
  852. *
  853. * @return Integer representing the instance's route-prio, or -1 if invalid paramters
  854. */
  855. int esp_netif_get_route_prio(esp_netif_t *esp_netif);
  856. /**
  857. * @brief Returns configured event for this esp-netif instance and supplied event type
  858. *
  859. * @param[in] esp_netif Handle to esp-netif instance
  860. *
  861. * @param event_type (either get or lost IP)
  862. *
  863. * @return specific event id which is configured to be raised if the interface lost or acquired IP address
  864. * -1 if supplied event_type is not known
  865. */
  866. int32_t esp_netif_get_event_id(esp_netif_t *esp_netif, esp_netif_ip_event_type_t event_type);
  867. /**
  868. * @}
  869. */
  870. /**
  871. * @defgroup ESP_NETIF_LIST ESP-NETIF List of interfaces
  872. * @brief APIs to enumerate all registered interfaces
  873. */
  874. /** @addtogroup ESP_NETIF_LIST
  875. * @{
  876. */
  877. /**
  878. * @brief Iterates over list of interfaces. Returns first netif if NULL given as parameter
  879. *
  880. * @note This API doesn't lock the list, nor the TCPIP context, as this it's usually required
  881. * to get atomic access between iteration steps rather that within a single iteration.
  882. * Therefore it is recommended to iterate over the interfaces inside esp_netif_tcpip_exec()
  883. *
  884. * @note This API is deprecated. Please use esp_netif_next_unsafe() directly if all the system
  885. * interfaces are under your control and you can safely iterate over them.
  886. * Otherwise, iterate over interfaces using esp_netif_tcpip_exec(), or use esp_netif_find_if()
  887. * to search in the list of netifs with defined predicate.
  888. *
  889. * @param[in] esp_netif Handle to esp-netif instance
  890. *
  891. * @return First netif from the list if supplied parameter is NULL, next one otherwise
  892. */
  893. esp_netif_t *esp_netif_next(esp_netif_t *esp_netif)
  894. __attribute__((deprecated("use esp_netif_next_unsafe() either directly or via esp_netif_tcpip_exec")));
  895. /**
  896. * @brief Iterates over list of interfaces without list locking. Returns first netif if NULL given as parameter
  897. *
  898. * Used for bulk search loops within TCPIP context, e.g. using esp_netif_tcpip_exec(), or if we're sure
  899. * that the iteration is safe from our application perspective (e.g. no interface is removed between iterations)
  900. *
  901. * @param[in] esp_netif Handle to esp-netif instance
  902. *
  903. * @return First netif from the list if supplied parameter is NULL, next one otherwise
  904. */
  905. esp_netif_t* esp_netif_next_unsafe(esp_netif_t* esp_netif);
  906. /**
  907. * @brief Predicate callback for esp_netif_find_if() used to find interface
  908. * which meets defined criteria
  909. */
  910. typedef bool (*esp_netif_find_predicate_t)(esp_netif_t *netif, void *ctx);
  911. /**
  912. * @brief Return a netif pointer for the first interface that meets criteria defined
  913. * by the callback
  914. *
  915. * @param fn Predicate function returning true for the desired interface
  916. * @param ctx Context pointer passed to the predicate, typically a descriptor to compare with
  917. * @return valid netif pointer if found, NULL if not
  918. */
  919. esp_netif_t *esp_netif_find_if(esp_netif_find_predicate_t fn, void *ctx);
  920. /**
  921. * @brief Returns number of registered esp_netif objects
  922. *
  923. * @return Number of esp_netifs
  924. */
  925. size_t esp_netif_get_nr_of_ifs(void);
  926. /**
  927. * @brief increase the reference counter of net stack buffer
  928. *
  929. * @param[in] netstack_buf the net stack buffer
  930. *
  931. */
  932. void esp_netif_netstack_buf_ref(void *netstack_buf);
  933. /**
  934. * @brief free the netstack buffer
  935. *
  936. * @param[in] netstack_buf the net stack buffer
  937. *
  938. */
  939. void esp_netif_netstack_buf_free(void *netstack_buf);
  940. /**
  941. * @}
  942. */
  943. /** @addtogroup ESP_NETIF_TCPIP_EXEC
  944. * @{
  945. */
  946. /**
  947. * @brief TCPIP thread safe callback used with esp_netif_tcpip_exec()
  948. */
  949. typedef esp_err_t (*esp_netif_callback_fn)(void *ctx);
  950. /**
  951. * @brief Utility to execute the supplied callback in TCP/IP context
  952. * @param fn Pointer to the callback
  953. * @param ctx Parameter to the callback
  954. * @return The error code (esp_err_t) returned by the callback
  955. */
  956. esp_err_t esp_netif_tcpip_exec(esp_netif_callback_fn fn, void *ctx);
  957. /**
  958. * @}
  959. */
  960. #ifdef __cplusplus
  961. }
  962. #endif
  963. #endif /* _ESP_NETIF_H_ */