protocol_examples_common.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Common functions for protocol examples, to establish Wi-Fi or Ethernet connection.
  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. #pragma once
  8. #include "sdkconfig.h"
  9. #include "esp_err.h"
  10. #if !CONFIG_IDF_TARGET_LINUX
  11. #include "esp_netif.h"
  12. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  13. #include "esp_eth.h"
  14. #endif
  15. #endif // !CONFIG_IDF_TARGET_LINUX
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #if !CONFIG_IDF_TARGET_LINUX
  20. #if CONFIG_EXAMPLE_CONNECT_WIFI
  21. #define EXAMPLE_NETIF_DESC_STA "example_netif_sta"
  22. #endif
  23. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  24. #define EXAMPLE_NETIF_DESC_ETH "example_netif_eth"
  25. #endif
  26. #if CONFIG_EXAMPLE_CONNECT_PPP
  27. #define EXAMPLE_NETIF_DESC_PPP "example_netif_ppp"
  28. #endif
  29. /* Example default interface, prefer the ethernet one if running in example-test (CI) configuration */
  30. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  31. #define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH)
  32. #define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_ETH)
  33. #elif CONFIG_EXAMPLE_CONNECT_WIFI
  34. #define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA)
  35. #define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_STA)
  36. #elif CONFIG_EXAMPLE_CONNECT_PPP
  37. #define EXAMPLE_INTERFACE get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP)
  38. #define get_example_netif() get_example_netif_from_desc(EXAMPLE_NETIF_DESC_PPP)
  39. #endif
  40. /**
  41. * @brief Configure Wi-Fi or Ethernet, connect, wait for IP
  42. *
  43. * This all-in-one helper function is used in protocols examples to
  44. * reduce the amount of boilerplate in the example.
  45. *
  46. * It is not intended to be used in real world applications.
  47. * See examples under examples/wifi/getting_started/ and examples/ethernet/
  48. * for more complete Wi-Fi or Ethernet initialization code.
  49. *
  50. * Read "Establishing Wi-Fi or Ethernet Connection" section in
  51. * examples/protocols/README.md for more information about this function.
  52. *
  53. * @return ESP_OK on successful connection
  54. */
  55. esp_err_t example_connect(void);
  56. /**
  57. * Counterpart to example_connect, de-initializes Wi-Fi or Ethernet
  58. */
  59. esp_err_t example_disconnect(void);
  60. /**
  61. * @brief Configure stdin and stdout to use blocking I/O
  62. *
  63. * This helper function is used in ASIO examples. It wraps installing the
  64. * UART driver and configuring VFS layer to use UART driver for console I/O.
  65. */
  66. esp_err_t example_configure_stdin_stdout(void);
  67. /**
  68. * @brief Returns esp-netif pointer created by example_connect() described by
  69. * the supplied desc field
  70. *
  71. * @param desc Textual interface of created network interface, for example "sta"
  72. * indicate default WiFi station, "eth" default Ethernet interface.
  73. *
  74. */
  75. esp_netif_t *get_example_netif_from_desc(const char *desc);
  76. #if CONFIG_EXAMPLE_PROVIDE_WIFI_CONSOLE_CMD
  77. /**
  78. * @brief Register wifi connect commands
  79. *
  80. * Provide a simple wifi_connect command in esp_console.
  81. * This function can be used after esp_console is initialized.
  82. */
  83. void example_register_wifi_connect_commands(void);
  84. #endif
  85. #if CONFIG_EXAMPLE_CONNECT_ETHERNET
  86. /**
  87. * @brief Get the example Ethernet driver handle
  88. *
  89. * @return esp_eth_handle_t
  90. */
  91. esp_eth_handle_t get_example_eth_handle(void);
  92. #endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
  93. #else
  94. static inline esp_err_t example_connect(void) {return ESP_OK;}
  95. #endif // !CONFIG_IDF_TARGET_LINUX
  96. #ifdef __cplusplus
  97. }
  98. #endif