|
| 1 | +#if 0 |
| 2 | +#include <freertos/FreeRTOS.h> |
| 3 | +#include <tcpip_adapter.h> |
| 4 | +#include <esp_http_server.h> |
| 5 | +#include <esp_system.h> |
| 6 | +#include <esp_event.h> |
| 7 | +// #include <esp_netif.h> |
| 8 | +#include <esp_wifi.h> |
| 9 | +#include <esp_log.h> |
| 10 | +#include <string.h> |
| 11 | +#include <unistd.h> |
| 12 | +#include <assert.h> |
| 13 | +#include <netdb.h> |
| 14 | +#include <rg_system.h> |
| 15 | + |
| 16 | +tcpip_adapter_ip_info_t ip_info; |
| 17 | +static char local_ipv4[16]; |
| 18 | +static bool wifi_ready = false; |
| 19 | + |
| 20 | +#define WIFI_SSID "RETRO-GO" |
| 21 | +#define WIFI_CHANNEL 12 |
| 22 | + |
| 23 | + |
| 24 | +static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) |
| 25 | +{ |
| 26 | + if (event_id == IP_EVENT_AP_STAIPASSIGNED) // A client connected to us |
| 27 | + { |
| 28 | + tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ip_info); |
| 29 | + sprintf(local_ipv4, IPSTR, IP2STR(&ip_info.ip)); |
| 30 | + wifi_ready = true; |
| 31 | + } |
| 32 | + else if (event_id == IP_EVENT_STA_GOT_IP) // We connected to an AP |
| 33 | + { |
| 34 | + tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info); |
| 35 | + sprintf(local_ipv4, IPSTR, IP2STR(&ip_info.ip)); |
| 36 | + wifi_ready = true; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +static esp_err_t http_handler(httpd_req_t *req) |
| 42 | +{ |
| 43 | + if (req->method == HTTP_POST) |
| 44 | + { |
| 45 | + char content[100]; |
| 46 | + size_t bytes = 0; |
| 47 | + |
| 48 | + while (bytes < req->content_len) |
| 49 | + { |
| 50 | + int ret = httpd_req_recv(req, content, sizeof(content)); |
| 51 | + if (ret < 0) |
| 52 | + { |
| 53 | + return ESP_FAIL; |
| 54 | + } |
| 55 | + bytes += ret; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + httpd_resp_send(req, "Hello World!", HTTPD_RESP_USE_STRLEN); |
| 60 | + return ESP_OK; |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +static void ftp_server_task(void *arg) |
| 65 | +{ |
| 66 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 67 | + wifi_mode_t mode = arg ? WIFI_MODE_STA : WIFI_MODE_AP; |
| 68 | + httpd_handle_t http_server = NULL; |
| 69 | + httpd_config_t http_config = HTTPD_DEFAULT_CONFIG(); |
| 70 | + |
| 71 | + wifi_ready = false; |
| 72 | + |
| 73 | + tcpip_adapter_init(); |
| 74 | + esp_event_loop_create_default(); |
| 75 | + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); |
| 76 | + ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); |
| 77 | + ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
| 78 | + ESP_ERROR_CHECK(esp_wifi_set_config(mode, &(wifi_config_t){ |
| 79 | + .sta.ssid = WIFI_SSID, |
| 80 | + .sta.channel = WIFI_CHANNEL, |
| 81 | + })); |
| 82 | + ESP_ERROR_CHECK(esp_wifi_set_config(mode, &(wifi_config_t){ |
| 83 | + .ap.ssid = WIFI_SSID, |
| 84 | + .ap.authmode = WIFI_AUTH_OPEN, |
| 85 | + .ap.channel = WIFI_CHANNEL, |
| 86 | + .ap.max_connection = 2, |
| 87 | + })); |
| 88 | + ESP_ERROR_CHECK(esp_wifi_set_mode(mode)); |
| 89 | + ESP_ERROR_CHECK(esp_wifi_start()); |
| 90 | + if (mode == WIFI_MODE_STA) |
| 91 | + ESP_ERROR_CHECK(esp_wifi_connect()); |
| 92 | + |
| 93 | + // Wait for Wifi to connect/start |
| 94 | + for (int i = 0; i < 1000 && !wifi_ready; i++) |
| 95 | + { |
| 96 | + vTaskDelay(pdMS_TO_TICKS(10)); |
| 97 | + } |
| 98 | + |
| 99 | + if (!wifi_ready) |
| 100 | + { |
| 101 | + RG_LOGE("Wifi init failure.\n"); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + RG_LOGI("Wifi connected. Local IP = %s\n", local_ipv4); |
| 106 | + |
| 107 | + if (httpd_start(&http_server, &http_config) == ESP_OK) |
| 108 | + { |
| 109 | + httpd_register_uri_handler(http_server, &(httpd_uri_t){"/", HTTP_GET, http_handler, 0}); |
| 110 | + httpd_register_uri_handler(http_server, &(httpd_uri_t){"/", HTTP_POST, http_handler, 0}); |
| 111 | + RG_LOGI("Web Server started!\n"); |
| 112 | + } |
| 113 | + |
| 114 | + while (1) |
| 115 | + { |
| 116 | + vTaskDelay(10); |
| 117 | + } |
| 118 | + |
| 119 | + httpd_stop(http_server); |
| 120 | +} |
| 121 | + |
| 122 | +void ftp_server_start(void) |
| 123 | +{ |
| 124 | + ftp_server_task(1); |
| 125 | +} |
| 126 | + |
| 127 | +void ftp_server_stop(void) |
| 128 | +{ |
| 129 | + |
| 130 | +} |
| 131 | +#else |
| 132 | +void ftp_server_start(void) {} |
| 133 | +void ftp_server_stop(void) {} |
| 134 | +#endif |
0 commit comments