protocol_examples_common.h 3.0 KB

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