|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * @file main.c |
| 9 | + * @brief Main application for ESP32 remote console over WebSocket. |
| 10 | + * |
| 11 | + * This application initializes a WebSocket client, redirects standard I/O to a WebSocket |
| 12 | + * via a VFS driver, and runs a console REPL for remote command execution. |
| 13 | + */ |
| 14 | + |
| 15 | +#include <stdio.h> |
| 16 | +#include "esp_log.h" |
| 17 | +#include "esp_vfs.h" |
| 18 | +#include "esp_console.h" |
| 19 | +#include "nvs_flash.h" |
| 20 | +#include "protocol_examples_common.h" |
| 21 | +#include "freertos/FreeRTOS.h" |
| 22 | +#include "freertos/task.h" |
| 23 | +#include "freertos/ringbuf.h" |
| 24 | +#include "linenoise/linenoise.h" |
| 25 | +#include "argtable3/argtable3.h" |
| 26 | +#include "esp_websocket_client.h" |
| 27 | +#include "websocket_client_vfs.h" |
| 28 | +#include "cmd_nvs.h" |
| 29 | +#include "console_simple_init.h" |
| 30 | + |
| 31 | +static const char *TAG = "remote_console"; |
| 32 | +static const char *DEFAULT_WS_URI = "ws://192.168.50.231:8080"; |
| 33 | + |
| 34 | +static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data); |
| 35 | +static esp_websocket_client_handle_t websocket_app_init(void); |
| 36 | +static void websocket_app_exit(esp_websocket_client_handle_t client); |
| 37 | +static void run_console_task(void); |
| 38 | +static void console_task(void* arg); |
| 39 | +static void vfs_exit(FILE* websocket_io); |
| 40 | +static FILE* vfs_init(void); |
| 41 | + |
| 42 | +void app_main(void) |
| 43 | +{ |
| 44 | + ESP_ERROR_CHECK(nvs_flash_init()); |
| 45 | + ESP_ERROR_CHECK(esp_netif_init()); |
| 46 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 47 | + ESP_ERROR_CHECK(example_connect()); |
| 48 | + |
| 49 | + esp_websocket_client_handle_t client = websocket_app_init(); |
| 50 | + if (client == NULL) { |
| 51 | + ESP_LOGE(TAG, "Failed to initialize websocket client"); |
| 52 | + return; |
| 53 | + } |
| 54 | + ESP_LOGI(TAG, "Websocket client initialized"); |
| 55 | + |
| 56 | + FILE* websocket_io = vfs_init(); |
| 57 | + if (websocket_io == NULL) { |
| 58 | + ESP_LOGE(TAG, "Failed to open websocket I/O file"); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + run_console_task(); |
| 63 | + |
| 64 | + while (true) { |
| 65 | + vTaskDelay(pdMS_TO_TICKS(1000)); |
| 66 | + } |
| 67 | + |
| 68 | + vfs_exit(websocket_io); |
| 69 | + websocket_app_exit(client); |
| 70 | +} |
| 71 | + |
| 72 | +static esp_websocket_client_handle_t websocket_app_init(void) |
| 73 | +{ |
| 74 | + websocket_client_vfs_config_t config = { |
| 75 | + .base_path = "/websocket", |
| 76 | + .send_timeout_ms = 10000, |
| 77 | + .recv_timeout_ms = 10000, |
| 78 | + .recv_buffer_size = 256, |
| 79 | + .fallback_stdout = stdout |
| 80 | + }; |
| 81 | + ESP_ERROR_CHECK(websocket_client_vfs_register(&config)); |
| 82 | + |
| 83 | + esp_websocket_client_config_t websocket_cfg = {}; |
| 84 | + //websocket_cfg.uri = "ws://192.168.50.231:8080"; |
| 85 | + websocket_cfg.uri = DEFAULT_WS_URI; |
| 86 | + websocket_cfg.reconnect_timeout_ms = 1000; |
| 87 | + websocket_cfg.network_timeout_ms = 10000; |
| 88 | + |
| 89 | + ESP_LOGI(TAG, "Connecting to %s...", websocket_cfg.uri); |
| 90 | + |
| 91 | + esp_websocket_client_handle_t client = esp_websocket_client_init(&websocket_cfg); |
| 92 | + esp_websocket_register_events(client, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)client); |
| 93 | + esp_websocket_client_start(client); |
| 94 | + websocket_client_vfs_add_client(client, 0); |
| 95 | + |
| 96 | + return client; |
| 97 | +} |
| 98 | + |
| 99 | +static void websocket_app_exit(esp_websocket_client_handle_t client) |
| 100 | +{ |
| 101 | + esp_websocket_client_close(client, portMAX_DELAY); |
| 102 | + ESP_LOGI(TAG, "Websocket Stopped"); |
| 103 | + esp_websocket_client_destroy(client); |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +/** |
| 108 | + * @brief Initialize VFS for WebSocket I/O redirection. |
| 109 | + * @return FILE pointer for WebSocket I/O, or NULL on failure. |
| 110 | + */ |
| 111 | +static FILE* vfs_init(void) |
| 112 | +{ |
| 113 | + FILE *websocket_io = fopen("/websocket/0", "r+"); |
| 114 | + if (!websocket_io) { |
| 115 | + ESP_LOGE(TAG, "Failed to open websocket I/O file"); |
| 116 | + return NULL; |
| 117 | + } |
| 118 | + |
| 119 | + stdin = websocket_io; |
| 120 | + stdout = websocket_io; |
| 121 | + setvbuf(stdin, NULL, _IONBF, 0); |
| 122 | + setvbuf(stdout, NULL, _IONBF, 0); |
| 123 | + |
| 124 | + _GLOBAL_REENT->_stdin = websocket_io; |
| 125 | + _GLOBAL_REENT->_stdout = websocket_io; |
| 126 | + _GLOBAL_REENT->_stderr = websocket_io; |
| 127 | + |
| 128 | + return websocket_io; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * @brief Clean up VFS resources. |
| 133 | + * @param websocket_io FILE pointer for WebSocket I/O. |
| 134 | + */ |
| 135 | +static void vfs_exit(FILE* websocket_io) |
| 136 | +{ |
| 137 | + if (websocket_io) { |
| 138 | + fclose(websocket_io); |
| 139 | + websocket_io = NULL; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/** |
| 144 | + * @brief WebSocket event handler. |
| 145 | + * @param handler_args User-defined arguments. |
| 146 | + * @param base Event base. |
| 147 | + * @param event_id Event ID. |
| 148 | + * @param event_data Event data. |
| 149 | + */ |
| 150 | +static void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) |
| 151 | +{ |
| 152 | + esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data; |
| 153 | + |
| 154 | + switch (event_id) { |
| 155 | + case WEBSOCKET_EVENT_CONNECTED: |
| 156 | + ESP_LOGI(TAG, "Websocket connected"); |
| 157 | + break; |
| 158 | + case WEBSOCKET_EVENT_DISCONNECTED: |
| 159 | + ESP_LOGI(TAG, "Websocket disconnected"); |
| 160 | + break; |
| 161 | + case WEBSOCKET_EVENT_DATA: |
| 162 | + if (data->op_code == 0x08 && data->data_len == 2) { |
| 163 | + ESP_LOGI(TAG, "Received closed message with code=%d", 256 * data->data_ptr[0] + data->data_ptr[1]); |
| 164 | + } |
| 165 | + break; |
| 166 | + case WEBSOCKET_EVENT_ERROR: |
| 167 | + ESP_LOGI(TAG, "Websocket error"); |
| 168 | + break; |
| 169 | + } |
| 170 | + |
| 171 | + websocket_client_vfs_event_handler(data->client, event_id, event_data); |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +static void run_console_task(void) |
| 176 | +{ |
| 177 | + vTaskDelay(pdMS_TO_TICKS(1000)); |
| 178 | + xTaskCreate(console_task, "console_task", 16 * 1024, NULL, 5, NULL); |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +static void console_task(void* arg) |
| 183 | +{ |
| 184 | + // Initialize console REPL |
| 185 | + ESP_ERROR_CHECK(console_cmd_init()); |
| 186 | + ESP_ERROR_CHECK(console_cmd_all_register()); |
| 187 | + |
| 188 | + // start console REPL |
| 189 | + ESP_ERROR_CHECK(console_cmd_start()); |
| 190 | + |
| 191 | + while (true) { |
| 192 | + //fprintf(websocket_io, "From: %s(%d)\n", __func__, __LINE__); |
| 193 | + vTaskDelay(pdMS_TO_TICKS(5000)); |
| 194 | + } |
| 195 | + |
| 196 | + vTaskDelete(NULL); |
| 197 | +} |
0 commit comments