esp_netif.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 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
  144. * @param event_id
  145. * @param data
  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
  155. * @param event_id
  156. * @param data
  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
  166. * @param event_id
  167. * @param data
  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
  177. * @param event_id
  178. * @param data
  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
  188. * @param event_id
  189. * @param data
  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
  199. * @param event_id
  200. * @param data
  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
  210. * @param event_id
  211. * @param data
  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
  221. * @param event_id
  222. * @param data
  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
  232. * @param event_id
  233. * @param data
  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. * @param[in] esp_netif Handle to esp-netif instance
  881. *
  882. * @return First netif from the list if supplied parameter is NULL, next one otherwise
  883. */
  884. esp_netif_t *esp_netif_next(esp_netif_t *esp_netif);
  885. /**
  886. * @brief Returns number of registered esp_netif objects
  887. *
  888. * @return Number of esp_netifs
  889. */
  890. size_t esp_netif_get_nr_of_ifs(void);
  891. /**
  892. * @brief increase the reference counter of net stack buffer
  893. *
  894. * @param[in] netstack_buf the net stack buffer
  895. *
  896. */
  897. void esp_netif_netstack_buf_ref(void *netstack_buf);
  898. /**
  899. * @brief free the netstack buffer
  900. *
  901. * @param[in] netstack_buf the net stack buffer
  902. *
  903. */
  904. void esp_netif_netstack_buf_free(void *netstack_buf);
  905. /**
  906. * @}
  907. */
  908. /** @addtogroup ESP_NETIF_TCPIP_EXEC
  909. * @{
  910. */
  911. /**
  912. * @brief TCPIP thread safe callback used with esp_netif_tcpip_exec()
  913. */
  914. typedef esp_err_t (*esp_netif_callback_fn)(void *ctx);
  915. /**
  916. * @brief Utility to execute the supplied callback in TCP/IP context
  917. * @param fn Pointer to the callback
  918. * @param ctx Parameter to the callback
  919. * @return The error code (esp_err_t) returned by the callback
  920. */
  921. esp_err_t esp_netif_tcpip_exec(esp_netif_callback_fn fn, void *ctx);
  922. /**
  923. * @}
  924. */
  925. #ifdef __cplusplus
  926. }
  927. #endif
  928. #endif /* _ESP_NETIF_H_ */