#!/usr/bin/env python3 """Compile actual console driver and CLI framing/handlers with UART/CDC stubs.""" from pathlib import Path import re import subprocess import tempfile ROOT = Path(__file__).resolve().parents[1] def extract(source, name): match = re.search(r'^(?:static )?[\w *]+\b' + name + r'\([^;]*?\)\s*\{', source, re.M) assert match, name end, depth = match.end(), 1 while depth: depth += (source[end] == '{') - (source[end] == '}') end += 1 return source[match.start():end] STUBS = r''' #include #include #include #include #include #include #include #include #define CONFIG_VFS_SUPPORT_IO 1 #define ESP_OK 0 #define ESP_FAIL 1 #define ESP_ERR_TIMEOUT 2 #define UART_NUM_0 0 #define portMUX_INITIALIZER_UNLOCKED 0 #define portENTER_CRITICAL(p) ((void)(p)) #define portEXIT_CRITICAL(p) ((void)(p)) #define pdTRUE 1 #define pdMS_TO_TICKS(n) (n) #define ESP_LOGI(tag, ...) do { (void)(tag); if(0) printf(__VA_ARGS__); } while(0) #define ESP_LOGW ESP_LOGI #define ESP_LOGE ESP_LOGI #define FACTORY_CLI_MAX_LINE_LEN 512 #define FACTORY_CLI_MAX_ARGC 8 #define FACTORY_CLI_DEVICE_ID_MAX_LEN 64 #define FACTORY_CLI_SERIAL_MAX_LEN 64 #define FACTORY_CLI_INPUT_BURST_BYTES 64 #define FACTORY_CLI_PARTIAL_TIMEOUT_MS 2000 #define FACTORY_CLI_JSON_MAX_LEN 640 #define DOMAIN_EXPRESSION_CHOUQIAN 1 #define DOMAIN_EXPRESSION_TRIGGER_FACTORY_LCD_TEST 2 typedef int esp_err_t; typedef uint32_t TickType_t; typedef int StaticSemaphore_t; typedef int *SemaphoreHandle_t; typedef int portMUX_TYPE; typedef struct {uint32_t rx_buffer_size,tx_buffer_size;} usb_serial_jtag_driver_config_t; typedef struct {int id,trigger;bool chouqian_result_index_valid;size_t chouqian_result_index;} domain_expression_request_t; static TickType_t ticks; static bool uart_ready,usb_ready,lock_fail,usb_block; static int uart_error,usb_error,uart_installs,usb_installs; static const char *uart_rx="", *usb_rx=""; static char uart_tx[16384],usb_tx[16384]; static size_t uart_tx_len,usb_tx_len; static int saves,notifies; static char saved_id[64],saved_sn[64]; static TickType_t xTaskGetTickCount(void) {return ticks;} #if CONFIG_TQ_FACTORY_CLI_USB_CDC_ENABLE static void vTaskDelay(TickType_t delay) {ticks+=delay;} #endif static SemaphoreHandle_t xSemaphoreCreateMutexStatic(StaticSemaphore_t *p) {return p;} static int xSemaphoreTake(SemaphoreHandle_t p, TickType_t wait) {assert(p && wait<=100);return !lock_fail;} static int xSemaphoreGive(SemaphoreHandle_t p) {assert(p);return 1;} static const char *esp_err_to_name(int err) {(void)err;return "fake";} static bool uart_is_driver_installed(int port) {assert(port==0);return uart_ready;} static int uart_driver_install(int port,int rx,int tx,int q,void *handle,int flags) { assert(port==0 && rx==2048 && tx==0 && q==0 && !handle && !flags); ++uart_installs;uart_ready=!uart_error;return uart_error; } static int uart_read_bytes(int port,void *buf,size_t len,TickType_t wait) { assert(port==0 && len>0);(void)wait; if(!*uart_rx)return 0; *(char *)buf=*uart_rx++;return 1; } static int uart_write_bytes(int port,const void *buf,size_t len) { assert(port==0 && uart_tx_len+lenrx_buffer_size==2048 && p->tx_buffer_size==1024); ++usb_installs;usb_ready=!usb_error;return usb_error; } static int usb_serial_jtag_read_bytes(void *buf,uint32_t len,TickType_t wait) { assert(len>0);(void)wait; if(!*usb_rx)return 0;*(char *)buf=*usb_rx++;return 1; } static int usb_serial_jtag_write_bytes(const void *buf,size_t len,TickType_t wait) { assert(wait==0 && len<=64);if(usb_block)return 0; assert(usb_tx_len+len