From ca6aa5bc65831a6861567b8be0a0b6ea381bd8e7 Mon Sep 17 00:00:00 2001 From: Wolfgang <24637325+seeul8er@users.noreply.github.com> Date: Thu, 7 Mar 2024 19:30:41 +0100 Subject: [PATCH] Fixed MSPv2 parsing bug Updated release build script Co-Authored-By: Iasen Kostov <13002165+tbyte007@users.noreply.github.com> --- create_release_zip.ps1 | 8 ++++---- main/db_esp32_control.c | 4 +++- main/msp_ltm_serial.c | 17 ++++++++++------- main/msp_ltm_serial.h | 2 +- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/create_release_zip.ps1 b/create_release_zip.ps1 index 7df2daa..4199293 100644 --- a/create_release_zip.ps1 +++ b/create_release_zip.ps1 @@ -10,7 +10,7 @@ cp .\flashing_instructions.txt $release_foldername rm -Recurse .\build idf.py set-target esp32 build mkdir $release_foldername\esp32 -cp .\build\flash_args $release_foldername\esp32 +cp .\build\flash_args $release_foldername\esp32\flash_args.txt cp .\build\db_esp32.bin $release_foldername\esp32 cp .\build\bootloader\bootloader.bin $release_foldername\esp32 cp .\build\www.bin $release_foldername\esp32 @@ -19,7 +19,7 @@ cp .\build\partition_table\partition-table.bin $release_foldername\esp32 rm -Recurse .\build idf.py set-target esp32s2 build mkdir $release_foldername\esp32s2 -cp .\build\flash_args $release_foldername\esp32s2 +cp .\build\flash_args $release_foldername\esp32s2\flash_args.txt cp .\build\db_esp32.bin $release_foldername\esp32s2 cp .\build\bootloader\bootloader.bin $release_foldername\esp32s2 cp .\build\www.bin $release_foldername\esp32s2 @@ -28,7 +28,7 @@ cp .\build\partition_table\partition-table.bin $release_foldername\esp32s2 rm -Recurse .\build idf.py set-target esp32s3 build mkdir $release_foldername\esp32s3 -cp .\build\flash_args $release_foldername\esp32s3 +cp .\build\flash_args $release_foldername\esp32s3\flash_args.txt cp .\build\db_esp32.bin $release_foldername\esp32s3 cp .\build\bootloader\bootloader.bin $release_foldername\esp32s3 cp .\build\www.bin $release_foldername\esp32s3 @@ -37,7 +37,7 @@ cp .\build\partition_table\partition-table.bin $release_foldername\esp32s3 rm -Recurse .\build idf.py set-target esp32c3 build mkdir $release_foldername\esp32c3 -cp .\build\flash_args $release_foldername\esp32c3 +cp .\build\flash_args $release_foldername\esp32c3\flash_args.txt cp .\build\db_esp32.bin $release_foldername\esp32c3 cp .\build\bootloader\bootloader.bin $release_foldername\esp32c3 cp .\build\www.bin $release_foldername\esp32c3 diff --git a/main/db_esp32_control.c b/main/db_esp32_control.c index f13ab8f..d6516ce 100644 --- a/main/db_esp32_control.c +++ b/main/db_esp32_control.c @@ -137,7 +137,7 @@ void send_to_all_clients(int tcp_clients[], struct udp_conn_list_t *n_udp_conn_l void write_to_uart(const char data_buffer[], const size_t data_length) { int written = uart_write_bytes(UART_NUM, data_buffer, data_length); if (written > 0) - ESP_LOGD(TAG, "Wrote %i bytes", written); + ESP_LOGD(TAG, "Wrote %i bytes to UART", written); else ESP_LOGE(TAG, "Error writing to UART %s", esp_err_to_name(errno)); } @@ -174,6 +174,8 @@ void parse_msp_ltm(int tcp_clients[], struct udp_conn_list_t *udp_connection, ui *serial_read_bytes = 0; } } + } else { // XXX Leads to crashes of the ESP32 without it! + *serial_read_bytes = 0; } } } diff --git a/main/msp_ltm_serial.c b/main/msp_ltm_serial.c index c6e4a83..e6cec4d 100644 --- a/main/msp_ltm_serial.c +++ b/main/msp_ltm_serial.c @@ -68,7 +68,7 @@ bool parse_msp_ltm_byte(msp_ltm_port_t *msp_ltm_port, uint8_t new_byte) { break; default: msp_ltm_port->parse_state = IDLE; - break; + return false; } break; @@ -125,7 +125,7 @@ bool parse_msp_ltm_byte(msp_ltm_port_t *msp_ltm_port, uint8_t new_byte) { break; default: msp_ltm_port->parse_state = IDLE; - break; + return false; } break; @@ -165,15 +165,15 @@ bool parse_msp_ltm_byte(msp_ltm_port_t *msp_ltm_port, uint8_t new_byte) { msp_ltm_port->checksum1 ^= new_byte; if (msp_ltm_port->offset == sizeof(mspHeaderV1_t)) { mspHeaderV1_t *hdr = (mspHeaderV1_t *) &msp_ltm_port->inBuf[0]; - // Check incoming buffer size limit - if (hdr->size > MSP_PORT_INBUF_SIZE) { - msp_ltm_port->parse_state = IDLE; - } else if (hdr->cmd == MSP_V2_FRAME_ID) { + // No need to check incoming buffer size limit for V1 + // because the buffer is 512 which is bigger than the INTMAX for byte + if (hdr->cmd == MSP_V2_FRAME_ID) { if (hdr->size >= sizeof(mspHeaderV2_t) + 1) { msp_ltm_port->mspVersion = MSP_V2_OVER_V1; msp_ltm_port->parse_state = MSP_HEADER_V2_OVER_V1; } else { msp_ltm_port->parse_state = IDLE; + return false; } } else { msp_ltm_port->dataSize = hdr->size; @@ -210,6 +210,7 @@ bool parse_msp_ltm_byte(msp_ltm_port_t *msp_ltm_port, uint8_t new_byte) { msp_ltm_port->dataSize = hdrv2->size; if (hdrv2->size > MSP_PORT_INBUF_SIZE) { msp_ltm_port->parse_state = IDLE; + return false; } else { msp_ltm_port->cmdMSP = hdrv2->cmd; msp_ltm_port->cmdFlags = hdrv2->flags; @@ -236,6 +237,7 @@ bool parse_msp_ltm_byte(msp_ltm_port_t *msp_ltm_port, uint8_t new_byte) { msp_ltm_port->parse_state = MSP_CHECKSUM_V1; } else { msp_ltm_port->parse_state = IDLE; + return false; } break; @@ -271,8 +273,9 @@ bool parse_msp_ltm_byte(msp_ltm_port_t *msp_ltm_port, uint8_t new_byte) { msp_ltm_port->parse_state = MSP_PACKET_RECEIVED; } else { msp_ltm_port->parse_state = IDLE; + return false; } break; } return true; -} \ No newline at end of file +} diff --git a/main/msp_ltm_serial.h b/main/msp_ltm_serial.h index e2dacb3..8c958ff 100644 --- a/main/msp_ltm_serial.h +++ b/main/msp_ltm_serial.h @@ -23,7 +23,7 @@ #define MSP_MAX_HEADER_SIZE 9 #define MSP_V2_FRAME_ID 255 -#define MSP_PORT_INBUF_SIZE 192 +#define MSP_PORT_INBUF_SIZE 512 #define MAX_MSP_PORT_COUNT 3 #define MSP_PORT_DATAFLASH_BUFFER_SIZE 4096 #define MSP_PORT_OUTBUF_SIZE 512