good afternoon, I hope someone can help me with the following problem.
Situation: I have a flutter app connected to a websockets server hosted on esp32 with the links2004/websockets library (https://github.com/Links2004/arduinoWebSockets). I try to pass the firmware through the socket as binary, this process is correct, I also add an identifier byte (to know that I am in update) and the next one to know if it is the last one, to conclude I add a byte at the end as a result of calculating the checksum of the frame.
At both ends I check the checksum and the amount of data but I get the following error:
premature end: res:0, pos:864256/1966080
Most relevant code and debugging traces:
esp_err_t Updater::handleUpdateData(const uint8_t *data, size_t length)
{
//debug vars.
static int k;
static int j;
if (!isUpdateInProgress)
{
logger().log("Fatal error, the update isn't started", debug_info_t::WARNING_D);
return ESP_FAIL;
}
if (length < 3)
{
//As I said, I add 3 bytes, therefore at least the frame must contain 3 bytes
logger().log("Frame length is incosistent, frame is broken", debug_info_t::WARNING_D);
return ESP_FAIL;
}
//To check that it is the last frame, I mark it with the 0xff flag
bool isLastFragment = (data[1] == 0xFF);
//the last byte of the frame contains the checksum
uint8_t receivedChecksum = data[length - 1];
uint8_t calculatedChecksum = getChecksum(data + 2, length - 3);
if (calculatedChecksum != receivedChecksum)
{
//At this point, if the checksum does not match it would throw an error
logger().log("Checksum mismatch.", debug_info_t::WARNING_D);
return ESP_FAIL;
}
k+=length-3;
logger().log(std::to_string(k).c_str());
//We write the data to the OTA partition and store its result, to verify that it is exactly
//the length received minus the 3 bytes that are not from the firmware
size_t result=Update.write(const_cast<uint8_t *>(data) + 2, length - 3);
if ( result!= length - 3)
{
logger().log("Failed to write OTA update data.", debug_info_t::WARNING_D);
return ESP_FAIL;
}else{
//debug to check how many bytes are being written to each packet
std::string s="Packet"+std::to_string(j)+" writted"+ std::to_string(result);
j++;
logger().log(s.c_str());
}
if (isLastFragment)
{
std::string lastPacket="Last packet length: "+std::to_string(length-3);
logger().log(lastPacket.c_str());
if (!Update.end())
{
logger().log("Failed to finalize OTA update.", debug_info_t::WARNING_D);
return ESP_FAIL;
}
else if (Update.isFinished())
{
logger().log("OTA update completed successfully.", debug_info_t::DEBUG_D);
ESP.restart();
}
else
{
logger().log("OTA update not finished.", debug_info_t::WARNING_D);
return ESP_FAIL;
}
}
return ESP_OK;
}
Debug traces flutter side:
I/flutter ( 5132):packet 0=> 10048
.....
I/flutter ( 5132):packet 85=> 864128
I/flutter ( 5132):packet 86=> 865712
Debug traces ESP32 side:
[INFO]: Packet0 writted=>10048
[INFO]: total=>10048
....
[INFO]: Packet85 writted=>10048
[INFO]: total=> 864128
[INFO]: Packet86 writted=>1584
[INFO]: total=> 865712
Viewing the properties of my firmware.bin file according to the OS explorer (liux), the firmware occupies 865712 bytes.
So I get the following error message: [ 19662][E][Updater.cpp:284] end(): premature end: res:0, pos:864256/1966080
1456bytes have been lost and I don't understand why.
If anyone can help me I would be very grateful.
After a few intensive tests, I finally got it, I leave the implementation of the class in case it helps someone, thank you very much anyway. Remember that I use 2 bytes at the beginning and one at the end for the checksum.