/* * SPDX-License-Identifier: Apache-2.0 */ #include #include #include #include #include "cJSON.h" #include "driver/usb_serial_jtag.h" #include "driver/usb_serial_jtag_vfs.h" #include "esp_log.h" #include "esp_system.h" #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "freertos/task.h" #include "usb_cfg.h" #include "web.h" static const char *TAG = "usb_cfg"; static const char API_PREFIX[] = "@api "; static const char REBOOT[] = "@reboot"; // The driver's TX buffer takes a write whole or not at all, so a long reply goes out in chunks. // Log output takes the same lock (log_vprintf), so no log line can land inside a reply. static SemaphoreHandle_t s_tx_lock; static vprintf_like_t s_log_vprintf; static int log_vprintf(const char *fmt, va_list args) { // A log from inside a reply write (none today) would deadlock, so only wait if we don't hold it const bool locked = s_tx_lock && xSemaphoreGetMutexHolder(s_tx_lock) != xTaskGetCurrentTaskHandle() && xSemaphoreTake(s_tx_lock, portMAX_DELAY) == pdTRUE; int r = s_log_vprintf(fmt, args); if (locked) { fflush(stdout); xSemaphoreGive(s_tx_lock); } return r; } static void write_line(const char *s) { static const size_t CHUNK = 256; xSemaphoreTake(s_tx_lock, portMAX_DELAY); fflush(stdout); // finish any log text already in stdio's buffer first for (size_t n = strlen(s); n > 0;) { size_t k = n < CHUNK ? n : CHUNK; if (usb_serial_jtag_write_bytes(s, k, pdMS_TO_TICKS(500)) != (int)k) { break; // no host reading: drop the rest rather than hold up the logs } s += k; n -= k; } xSemaphoreGive(s_tx_lock); } // An "@api" line: run it and send back the envelope, with the request's id added static void handle_api(const char *json, char *reply, size_t cap) { web_api_call(json, reply, cap); cJSON *req = cJSON_Parse(json); const cJSON *id = req ? cJSON_GetObjectItem(req, "id") : NULL; cJSON *env = cJSON_Parse(reply); if (!env) { env = cJSON_CreateObject(); cJSON_AddNumberToObject(env, "status", 500); cJSON_AddStringToObject(env, "body", "{\"error\":\"reply too large\"}"); } if (cJSON_IsNumber(id)) { cJSON_AddNumberToObject(env, "id", id->valuedouble); } char *s = cJSON_PrintUnformatted(env); cJSON_Delete(env); cJSON_Delete(req); if (!s) { return; } size_t n = strlen(s); // A leading newline ends any log line that was half written, so the reply starts its own line const size_t pre = sizeof(API_PREFIX) - 1; char *line = (char *)malloc(1 + pre + n + 2); if (line) { line[0] = '\n'; memcpy(line + 1, API_PREFIX, pre); memcpy(line + 1 + pre, s, n); line[1 + pre + n] = '\n'; line[2 + pre + n] = '\0'; write_line(line); free(line); } cJSON_free(s); } static void handle_line(char *line, char *reply, size_t cap) { size_t n = strlen(line); while (n && (line[n - 1] == '\r' || line[n - 1] == ' ')) { line[--n] = '\0'; } if (strncmp(line, API_PREFIX, sizeof(API_PREFIX) - 1) == 0) { handle_api(line + sizeof(API_PREFIX) - 1, reply, cap); } else if (strcmp(line, REBOOT) == 0) { write_line("\n@ok\n"); usb_serial_jtag_wait_tx_done(pdMS_TO_TICKS(200)); vTaskDelay(pdMS_TO_TICKS(100)); esp_restart(); } // Anything else (a stray keypress in a monitor) is ignored } static void usb_cfg_task(void *) { char *line = (char *)malloc(WEB_API_MSG_MAX + 1); char *reply = (char *)malloc(WEB_API_REPLY_MAX); if (!line || !reply) { ESP_LOGE(TAG, "out of memory"); vTaskDelete(NULL); return; } size_t len = 0; bool overflow = false; // the current line is too long: drop it up to its newline uint8_t buf[64]; for (;;) { int got = usb_serial_jtag_read_bytes(buf, sizeof(buf), portMAX_DELAY); for (int i = 0; i < got; i++) { char c = (char)buf[i]; if (c == '\n') { if (!overflow) { line[len] = '\0'; handle_line(line, reply, WEB_API_REPLY_MAX); } len = 0; overflow = false; } else if (!overflow) { if (len < WEB_API_MSG_MAX) { line[len++] = c; } else { overflow = true; } } } } } esp_err_t usb_cfg_start(void) { usb_serial_jtag_driver_config_t cfg = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT(); cfg.rx_buffer_size = 1024; cfg.tx_buffer_size = 2048; esp_err_t err = usb_serial_jtag_driver_install(&cfg); if (err != ESP_OK) { ESP_LOGE(TAG, "USB serial driver install failed: %s", esp_err_to_name(err)); return err; } // Logs go through the driver too, so they and the replies share one queue usb_serial_jtag_vfs_use_driver(); s_tx_lock = xSemaphoreCreateMutex(); if (!s_tx_lock) { return ESP_ERR_NO_MEM; } s_log_vprintf = esp_log_set_vprintf(log_vprintf); if (xTaskCreate(usb_cfg_task, "usb_cfg", 6144, NULL, 4, NULL) != pdPASS) { return ESP_ERR_NO_MEM; } ESP_LOGI(TAG, "Config API on the USB serial port"); return ESP_OK; }