Rajkumar Verma
Rajkumar Verma

Reputation: 1

I'm trying to perform a FOTA update on an ESP32 using GSM EC200U through AT commands, but the received data for the bin file is showing a length error

Im sharing some snippet from my code.

bool downloadFirmware(const char *url, const esp_partition_t *partition)
{
    printf("Fetching firmware binary...\n");

    // Initialize UART commands
    uint8_t *httpInitCmd = (uint8_t *)"AT+QHTTPURL=65,80\r\n";
    uart_write_bytes(UART_NUM_1, (const char *)httpInitCmd, strlen((const char *)httpInitCmd));
    vTaskDelay(pdMS_TO_TICKS(1000));

    uint8_t *response = (uint8_t *)malloc(BUF_SIZE);
    if (response == NULL)
    {
        printf("Failed to allocate memory for response\n");
        return false;
    }

    // Read initial response
    int len = uart_read_bytes(UART_NUM_1, response, BUF_SIZE, 1000 / portTICK_RATE_MS);
    if (len > 0)
    {
        response[len] = '\0';
        printf("Response received: %s\n", response);
        if (strstr((const char *)response, "CONNECT") == NULL)
        {
            printf("Failed to get CONNECT response\n");
            free(response);
        }
    }

    // Send URL
    uart_write_bytes(UART_NUM_1, url, strlen((const char *)url));
    vTaskDelay(pdMS_TO_TICKS(5000));

    len = uart_read_bytes(UART_NUM_1, response, BUF_SIZE, 1000 / portTICK_RATE_MS);
    if (len > 0)
    {
        response[len] = '\0';
        printf("Response received: %s\n", response);
        if (strstr((const char *)response, "OK") == NULL)
        {
            printf("Failed to get OK response after sending URL\n");
            free(response);
        }
    }

    // Initiate HTTP GET
    uint8_t *httpGetCmd = (uint8_t *)"AT+QHTTPGET=80\r\n";
    if (!checkForOKresponse(httpGetCmd))
    {
        free(response);
    }

    len = uart_read_bytes(UART_NUM_1, response, BUF_SIZE, 1000 / portTICK_RATE_MS);
    if (len > 0)
    {
        response[len] = '\0';
        printf("HTTP GET Response: %s\n", response);
        if (strstr((const char *)response, "+QHTTPGET: 0,200") == NULL)
        {
            printf("Failed to get 200 OK for HTTP GET\n");
            free(response);
        }
    }

    // Read the firmware binary data
    uint8_t *httpReadCmd = (uint8_t *)"AT+QHTTPREAD\r\n";
    uart_write_bytes(UART_NUM_1, (const char *)httpReadCmd, strlen((const char *)httpReadCmd));
    vTaskDelay(pdMS_TO_TICKS(1000));

    int totalBytesRead = 0;
    int startIndex = -1; // To keep track of the index where 0xE9 is found(magic string)
    while (totalBytesRead < BUF_SIZE)
    {
        len = uart_read_bytes(UART_NUM_1, response + totalBytesRead, BUF_SIZE - totalBytesRead, 5000 / portTICK_RATE_MS);
        if (len > 0)
        {
            printf("Data read (%d bytes): ", len);
            for (int i = 0; i < len; i++)
            {
                printf("%02X", response[totalBytesRead + i]);
                if (response[totalBytesRead + i] == 0xE9 && startIndex == -1)
                {
                    startIndex = totalBytesRead + i;
                }
            }
            printf("\n");
            totalBytesRead += len;
        }
        else
        {
            break;
        }
    }

    if (startIndex != -1)
    {
        printf("HTTP READ Response received (%d bytes) starting from 0xE9\n", totalBytesRead - startIndex);

        // Begin OTA process
        if (isPartitionEmpty(partition))
        {
            esp_ota_handle_t updateHandle;
            esp_err_t otaBeginResult = esp_ota_begin(partition, OTA_SIZE_UNKNOWN, &updateHandle);
            if (otaBeginResult != ESP_OK)
            {
                printf("esp_ota_begin failed: %s\n", esp_err_to_name(otaBeginResult));
                free(response);
                return false;
            }

            totalBytesRead -= startIndex;
            uint8_t *dataToWrite = response + startIndex;

            // Write the initial chunk
            esp_err_t otaWriteResult = esp_ota_write(updateHandle, (const void *)dataToWrite, totalBytesRead);
            if (otaWriteResult != ESP_OK)
            {
                printf("esp_ota_write failed: %s\n", esp_err_to_name(otaWriteResult));
                esp_ota_end(updateHandle);
                free(response);
                return false;
            }

            printf("Initial data written: %d bytes\n", totalBytesRead);

            // Read and write the remaining data
            while (true)
            {
                len = uart_read_bytes(UART_NUM_1, response, BUF_SIZE, 5000 / portTICK_RATE_MS);
                if (len > 0)
                {
                    printf("Writing data (%d bytes): ", len);
                    for (int i = 0; i < len; i++)
                    {
                        printf("%02X", response[i]);
                    }
                    printf("\n");

                    otaWriteResult = esp_ota_write(updateHandle, (const void *)response, len);
                    if (otaWriteResult != ESP_OK)
                    {
                        printf("esp_ota_write failed: %s\n", esp_err_to_name(otaWriteResult));
                        esp_ota_end(updateHandle);
                        free(response);
                        return false;
                    }
                }
                else
                {
                    break;
                }
            }

            esp_err_t otaEndResult = esp_ota_end(updateHandle);
            if (otaEndResult != ESP_OK)
            {
                printf("esp_ota_end failed: %s\n", esp_err_to_name(otaEndResult));
                free(response);
                return false;
            }

            printf("OTA Update successful\n");
            free(response);
            return true;
        }
        else
        {
            printf("Partition is not empty, skipping update\n");
            free(response);
            return false;
        }
    }
    else
    {
        printf("No response starting with 0xE9 received\n");
        free(response);
        return false;
    }
}

**The error that i'm facing ->

E (1379450) esp_image: invalid segment length 0xd43c2149 esp_ota_end failed: ESP_ERR_OTA_VALIDATE_FAILED

Using esp32 as a main module for network EC200U-CN module.its on uart so I'm sending at commands from esp32 to ec200u module. I have added picture to better understanding.**

Can anyone help me out.

Upvotes: 0

Views: 107

Answers (0)

Related Questions