sta2eth_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <string.h>
  7. #include <esp_timer.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/event_groups.h"
  10. #include "esp_log.h"
  11. #include "esp_wifi.h"
  12. #include "esp_mac.h"
  13. #include "esp_netif.h"
  14. #include "esp_event.h"
  15. #include "esp_private/wifi.h"
  16. #include "nvs_flash.h"
  17. #include "driver/gpio.h"
  18. #include "provisioning.h"
  19. #include "wired_iface.h"
  20. static const char *TAG = "example_sta2wired";
  21. static EventGroupHandle_t s_event_flags;
  22. static bool s_wifi_is_connected = false;
  23. static uint8_t s_sta_mac[6];
  24. const int CONNECTED_BIT = BIT0;
  25. const int DISCONNECTED_BIT = BIT1;
  26. const int RECONFIGURE_BIT = BIT2;
  27. const int PROV_SUCCESS_BIT = BIT3;
  28. const int PROV_FAIL_BIT = BIT4;
  29. /**
  30. * WiFi -- Wired packet path
  31. */
  32. static esp_err_t wired_recv_callback(void *buffer, uint16_t len, void *ctx)
  33. {
  34. if (s_wifi_is_connected) {
  35. mac_spoof(FROM_WIRED, buffer, len, s_sta_mac);
  36. if (esp_wifi_internal_tx(ESP_IF_WIFI_STA, buffer, len) != ESP_OK) {
  37. ESP_LOGD(TAG, "Failed to send packet to WiFi!");
  38. }
  39. }
  40. return ESP_OK;
  41. }
  42. static void wifi_buff_free(void *buffer, void *ctx)
  43. {
  44. esp_wifi_internal_free_rx_buffer(buffer);
  45. }
  46. static esp_err_t wifi_recv_callback(void *buffer, uint16_t len, void *eb)
  47. {
  48. mac_spoof(TO_WIRED, buffer, len, s_sta_mac);
  49. if (wired_send(buffer, len, eb) != ESP_OK) {
  50. esp_wifi_internal_free_rx_buffer(eb);
  51. ESP_LOGD(TAG, "Failed to send packet to USB!");
  52. }
  53. return ESP_OK;
  54. }
  55. static void event_handler(void *arg, esp_event_base_t event_base,
  56. int32_t event_id, void *event_data)
  57. {
  58. if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
  59. ESP_LOGI(TAG, "Wi-Fi STA disconnected");
  60. s_wifi_is_connected = false;
  61. esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, NULL);
  62. esp_wifi_connect();
  63. xEventGroupClearBits(s_event_flags, CONNECTED_BIT);
  64. xEventGroupSetBits(s_event_flags, DISCONNECTED_BIT);
  65. } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) {
  66. ESP_LOGI(TAG, "Wi-Fi STA connected");
  67. esp_wifi_internal_reg_rxcb(ESP_IF_WIFI_STA, wifi_recv_callback);
  68. s_wifi_is_connected = true;
  69. xEventGroupClearBits(s_event_flags, DISCONNECTED_BIT);
  70. xEventGroupSetBits(s_event_flags, CONNECTED_BIT);
  71. }
  72. }
  73. static esp_err_t connect_wifi(void)
  74. {
  75. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, NULL));
  76. ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  77. ESP_ERROR_CHECK(esp_wifi_start() );
  78. wifi_config_t wifi_cfg;
  79. if (esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg) != ESP_OK) {
  80. // configuration not available, report error to restart provisioning
  81. return ESP_FAIL;
  82. }
  83. esp_wifi_connect();
  84. EventBits_t status = xEventGroupWaitBits(s_event_flags, CONNECTED_BIT, 0, 1, 10000 / portTICK_PERIOD_MS);
  85. if (status & CONNECTED_BIT) {
  86. ESP_LOGI(TAG, "WiFi station connected successfully");
  87. return ESP_OK;
  88. }
  89. ESP_LOGE(TAG, "WiFi station connected failed");
  90. return ESP_ERR_TIMEOUT;
  91. }
  92. /**
  93. * GPIO button functionality
  94. */
  95. #define GPIO_INPUT CONFIG_EXAMPLE_RECONFIGURE_BUTTON
  96. #define GPIO_LONG_PUSH_US 2000000 /* push for 2 seconds to reconfigure */
  97. static void IRAM_ATTR gpio_isr_handler(void *arg)
  98. {
  99. static int64_t last_pushed = -1;
  100. if (gpio_get_level(GPIO_INPUT) == 0) {
  101. last_pushed = esp_timer_get_time();
  102. } else {
  103. uint64_t now = esp_timer_get_time();
  104. if (last_pushed != -1 && now - last_pushed > GPIO_LONG_PUSH_US) {
  105. BaseType_t high_task_wakeup;
  106. xEventGroupSetBitsFromISR(s_event_flags, RECONFIGURE_BIT, &high_task_wakeup);
  107. if (high_task_wakeup) {
  108. portYIELD_FROM_ISR();
  109. }
  110. }
  111. last_pushed = -1;
  112. }
  113. }
  114. static void gpio_init(void)
  115. {
  116. gpio_config_t io_conf = { .intr_type = GPIO_INTR_ANYEDGE,
  117. .pin_bit_mask = (1ULL << GPIO_INPUT),
  118. .mode = GPIO_MODE_INPUT,
  119. .pull_up_en = 1
  120. };
  121. gpio_config(&io_conf);
  122. gpio_install_isr_service(0);
  123. //hook isr handler for specific gpio pin
  124. gpio_isr_handler_add(GPIO_INPUT, gpio_isr_handler, NULL);
  125. }
  126. /**
  127. * Application
  128. */
  129. void app_main(void)
  130. {
  131. static __NOINIT_ATTR uint32_t s_reconfigure_requested;
  132. static const uint32_t RECONFIGURE_REQUEST = 0x1C55AA;
  133. /* Check reset reason and decide if we should re-provision */
  134. bool do_provision = false;
  135. esp_reset_reason_t reason = esp_reset_reason();
  136. ESP_LOGD(TAG, "After restart! %d", reason);
  137. if (reason != ESP_RST_SW) {
  138. s_reconfigure_requested = 0;
  139. } else if (s_reconfigure_requested == RECONFIGURE_REQUEST) {
  140. do_provision = true;
  141. }
  142. /* Initialize NVS and WiFi */
  143. esp_err_t ret = nvs_flash_init();
  144. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  145. ESP_ERROR_CHECK(nvs_flash_erase());
  146. ret = nvs_flash_init();
  147. }
  148. ESP_ERROR_CHECK(ret);
  149. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  150. ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  151. // init the flags and event loop
  152. s_event_flags = xEventGroupCreate();
  153. ESP_ERROR_CHECK(esp_event_loop_create_default());
  154. /* Init the re-provisioning button (long-press with initiate provisioning restart) */
  155. gpio_init();
  156. esp_read_mac(s_sta_mac, ESP_MAC_WIFI_STA);
  157. /* Start the application in configuration mode (to perform provisioning)
  158. * or in a bridge mode (already provisioned) */
  159. if (do_provision || !is_provisioned()) {
  160. ESP_LOGI(TAG, "Starting provisioning");
  161. ESP_ERROR_CHECK(esp_netif_init());
  162. // needed to complete provisioning with getting a valid IP event
  163. esp_netif_create_default_wifi_sta();
  164. // starts the wired interface with virtual network used to configure/provision the example
  165. wired_netif_init();
  166. start_provisioning(&s_event_flags, PROV_SUCCESS_BIT, PROV_FAIL_BIT);
  167. } else {
  168. ESP_LOGI(TAG, "Starting USB-WiFi bridge");
  169. if (connect_wifi() != ESP_OK) {
  170. // if we cannot connect to WiFi we just try to re-provision
  171. xEventGroupSetBits(s_event_flags, RECONFIGURE_BIT);
  172. } else {
  173. // start the wired interface in the bridge mode
  174. wired_bridge_init(wired_recv_callback, wifi_buff_free);
  175. }
  176. }
  177. EventBits_t bits = xEventGroupWaitBits(s_event_flags, RECONFIGURE_BIT | PROV_SUCCESS_BIT | PROV_FAIL_BIT, pdTRUE, pdFALSE, portMAX_DELAY);
  178. if (bits & RECONFIGURE_BIT || bits & PROV_FAIL_BIT) {
  179. // retry provisioning if it previously failed or if requested by the button press
  180. s_reconfigure_requested = RECONFIGURE_REQUEST;
  181. } else {
  182. // provisioning successfully finished, restart to the bridge mode
  183. s_reconfigure_requested = 0;
  184. }
  185. vTaskDelay(pdMS_TO_TICKS(1000)); // to let httpd handle the closure
  186. esp_restart();
  187. }