@ -1,3 +1,13 @@
/**
* @ file Mesh_OTA_Partition_Access . c
* @ brief Write and read partition if requested from Mesh_OTA
* @ author Hendrik Schutter
* @ date 21.01 .2021
*
* Additional Infos : Write image via HTTPS
* Receive or transmit via Mesh
*/
# include "Mesh_OTA.h"
# include "Mesh_OTA_Util.h"
# include "Mesh_OTA_Globals.h"
@ -5,6 +15,18 @@
static const char * LOG_TAG = " mesh_ota_partition_access " ;
/**
* @ fn esp_err_t errMeshOtaPartitionAccessHttps ( bool * const cpbNewOTAImage )
* @ brief Downloads and writes the image from the server to partition
* @ param cpbNewOTAImage pointer to boolean to signal if a new image was successfully received
* @ return ESP32 error code
* @ author Hendrik Schutter
* @ date 21.01 .2021
*
* Checks if the image on server is newer
* Downloads the image in segements
* Handles OTA process
*/
esp_err_t errMeshOtaPartitionAccessHttps ( bool * const cpbNewOTAImage )
{
esp_err_t err = ESP_OK ;
@ -22,7 +44,8 @@ esp_err_t errMeshOtaPartitionAccessHttps(bool* const cpbNewOTAImage)
ERROR_CHECK ( errMeshOtaUtilExtractVersionNumber ( u8OTABuffer , & u32BytesRead , pcRemoteVersionNumber ) ) ; //extract version numbers
if ( err = = ESP_OK ) //check if version number is found
//check if version number is found
if ( err = = ESP_OK )
{
xSemaphoreTake ( bsOTAProcess , portMAX_DELAY ) ; //wait for binary semaphore that allows to start the OTA process
@ -35,8 +58,8 @@ esp_err_t errMeshOtaPartitionAccessHttps(bool* const cpbNewOTAImage)
ESP_LOGI ( LOG_TAG , " Server: image is newer --> OTA update required " ) ;
ERROR_CHECK ( errMeshOtaUtilFindImageStart ( u8OTABuffer , & u32BufferLenght , & u32StartOffset ) ) ; //get image start offset
ERROR_CHECK ( esp_ota_begin ( pOTAPartition , OTA_SIZE_UNKNOWN , & otaHandle ) ) ; //start ota update process
if ( err = = ESP_OK )
{
//image download and ota partition write
@ -54,7 +77,8 @@ esp_err_t errMeshOtaPartitionAccessHttps(bool* const cpbNewOTAImage)
u32OTABytesWritten = u32OTABytesWritten + u32BytesRead ; //update counter
}
}
while ( ( u32BytesRead > 0 ) & & ( err = = ESP_OK ) & & ( u32OTABytesWritten < = pOTAPartition - > size ) ) ; //loop until error or complete image downloaded
//loop until error or complete image downloaded
while ( ( u32BytesRead > 0 ) & & ( err = = ESP_OK ) & & ( u32OTABytesWritten < = pOTAPartition - > size ) ) ;
}
if ( err = = ESP_OK )
@ -70,7 +94,7 @@ esp_err_t errMeshOtaPartitionAccessHttps(bool* const cpbNewOTAImage)
else
{
//error occurred --> abort ota update process
ESP_LOGE ( LOG_TAG , " abort ota process due to error 0x%x -> %s " , err , esp_err_to_name ( err ) ) ;
ESP_LOGE ( LOG_TAG , " abort OTA process due to error 0x%x -> %s " , err , esp_err_to_name ( err ) ) ;
ERROR_CHECK ( esp_ota_abort ( otaHandle ) ) ;
* cpbNewOTAImage = false ; //ota update failed
}
@ -84,27 +108,39 @@ esp_err_t errMeshOtaPartitionAccessHttps(bool* const cpbNewOTAImage)
return err ;
}
/**
* @ fn esp_err_t errMeshOtaPartitionAccessMeshTransmit ( const mesh_addr_t * const cpcMeshNodeAddr )
* @ brief Reads the local image and sends it to node
* @ param cpcMeshNodeAddr pointer to mesh node addr to send the image segments to
* @ return ESP32 error code
* @ author Hendrik Schutter
* @ date 21.01 .2021
*
* Reads the newest OTA image in segments
* Sends the image to mesh node in segments
* Handles OTA process
*/
esp_err_t errMeshOtaPartitionAccessMeshTransmit ( const mesh_addr_t * const cpcMeshNodeAddr )
{
esp_err_t err = ESP_OK ;
const esp_partition_t * pBootPartition ; //pointer to boot partition (that will booted after reset)
const esp_partition_t * pBootPartition = NULL ; //pointer to boot partition (that will booted after reset)
MESH_PACKET_t sMeshPacket ; //packet for sending and receiving
// uint32_t u32Index = 0U; //index for partition read offset
bool bAbort = false ; //abort the OTA process
bool bNodeIsResponding = false ; //remote node is still active
uint32_t u32OTABytesWritten = 0U ;
uint32_t u32SegmentCounter = 0U ;
uint32_t u32OTABytesWritten = 0U ; //counter of bytes unsed for progress log
uint32_t u32SegmentCounter = 0U ; //counter of segments unsed for progress log
pBootPartition = esp_ota_get_boot_partition ( ) ; //get boot partition (that will booted after reset), not the running partition
//loop through partition to read in segmensts until end or error or abort called
while ( ( ( OTA_MESH_SEGMENT_SIZE * u32SegmentCounter ) < pBootPartition - > size ) & & ( err = = ESP_OK ) & & ( bAbort = = false ) )
{
bNodeIsResponding = false ; //reset to default
bNodeIsResponding = false ; //reset to default for this loop
// read partition with offset based in index
ERROR_CHECK ( esp_partition_read ( pBootPartition , ( OTA_MESH_SEGMENT_SIZE * u32SegmentCounter ) , sMeshPacket . au8Payload , OTA_MESH_SEGMENT_SIZE ) ) ;
u32OTABytesWritten = ( ( u32SegmentCounter + 1 ) * OTA_MESH_SEGMENT_SIZE ) ;
u32OTABytesWritten = ( ( u32SegmentCounter + 1 ) * OTA_MESH_SEGMENT_SIZE ) ; //calc bytes that are written in this ota process
vMeshOtaUtilPrintOTAProgress ( & ( pBootPartition - > size ) , & u32OTABytesWritten , Transmitter ) ;
if ( err = = ESP_OK )
@ -115,10 +151,9 @@ esp_err_t errMeshOtaPartitionAccessMeshTransmit(const mesh_addr_t* const cpcMesh
if ( ( OTA_MESH_SEGMENT_SIZE * ( u32SegmentCounter + 1 ) ) > = pBootPartition - > size ) //check if last segment
{
//last partition image segment --> send OTA_Complete
ESP_LOGI ( LOG_TAG , " OTA-TX: last segment--> send Complete " ) ;
ESP_LOGD ( LOG_TAG , " OTA-TX: last segment--> send Complete " ) ;
sMeshPacket . type = OTA_Complete ;
}
//ESP_LOGI(LOG_TAG, "OTA-TX: send packet");
err = errMeshNetworkSendMeshPacket ( cpcMeshNodeAddr , & sMeshPacket ) ;
}
else
@ -126,39 +161,35 @@ esp_err_t errMeshOtaPartitionAccessMeshTransmit(const mesh_addr_t* const cpcMesh
// error while read --> send OTA_ABORT and abort this OTA process
sMeshPacket . type = OTA_Abort ;
bAbort = true ;
ESP_LOGI ( LOG_TAG , " OTA-TX: error while read --> send ABORT " ) ;
ESP_LOGE ( LOG_TAG , " OTA-TX: error while read --> send ABORT " ) ;
errMeshNetworkSendMeshPacket ( cpcMeshNodeAddr , & sMeshPacket ) ;
}
// loop through all OTA messages or until abort is called or error
for ( uint32_t u32Index = 0 ; ( ( u32Index < QUEUE_MESSAGE_OTA_SIZE ) & & ( bAbort = = false ) & & ( err = = ESP_OK ) ) ; u32Index + + ) //loop through all OTA messages
{
// if(uxQueueSpacesAvailable(queueMessageOTA) < QUEUE_MESSAGE_OTA_SIZE)
// {
//queue not empty
//get OTA message from queue
if ( xQueueReceive ( queueMessageOTA , & sMeshPacket , ( ( OTA_MESH_TIMEOUT ) / portTICK_PERIOD_MS ) ) ! = pdTRUE )
{
ESP_LOGE ( LOG_TAG , " Unable to receive OTA Messages from queue " ) ;
err = ESP_FAIL ;
}
if ( ( err = = ESP_OK ) & & ( bMeshNetworkCheckMACEquality ( sMeshPacket . meshSenderAddr . addr , cpcMeshNodeAddr - > addr ) ) ) //if OTA_Version_Request
//check if from correct node
if ( ( err = = ESP_OK ) & & ( bMeshNetworkCheckMACEquality ( sMeshPacket . meshSenderAddr . addr , cpcMeshNodeAddr - > addr ) ) )
{
//packet from node received
//packet from node received --> handle it
switch ( sMeshPacket . type )
{
case OTA_ACK : //increase index for next round
u32Index + + ;
case OTA_ACK : //start next loop for segment
bNodeIsResponding = true ;
u32Index = QUEUE_MESSAGE_OTA_SIZE ; //this will end the loop through all OTA messages
u32Index = QUEUE_MESSAGE_OTA_SIZE ; //this will end the loop through all OTA messages
break ;
case OTA_Abort : //abort this OTA process
bAbort = true ;
bNodeIsResponding = true ;
break ;
default :
//receives wrong OTA message type from node --> back to queue
//vMeshOtaUtilAddOtaMessageToQueue(&sMeshPacket);
break ;
}
}
@ -167,16 +198,6 @@ esp_err_t errMeshOtaPartitionAccessMeshTransmit(const mesh_addr_t* const cpcMesh
//received from wrong node --> back to queue
vMeshOtaUtilAddOtaMessageToQueue ( & sMeshPacket ) ;
}
/*
}
else
{
// OTA Message queue is empty --> wait some time
ESP_LOGI ( LOG_TAG , " OTA-TX: ota message queue empty --> wait " ) ;
vTaskDelay ( ( OTA_MESH_TIMEOUT ) / portTICK_PERIOD_MS ) ;
}
*/
} //end OTA message loop
if ( bNodeIsResponding = = false )
@ -188,10 +209,23 @@ esp_err_t errMeshOtaPartitionAccessMeshTransmit(const mesh_addr_t* const cpcMesh
}
u32SegmentCounter + + ;
} //end of partition segment loop
vMeshOtaUtilClearOtaMessageQueue ( cpcMeshNodeAddr ) ;
vMeshOtaUtilClearOtaMessageQueue ( cpcMeshNodeAddr ) ; //remove all OTA messages from remote node
return err ;
}
/**
* @ fn esp_err_t errMeshOtaPartitionAccessMeshReceive ( bool * const cpbNewOTAImage , const mesh_addr_t * const cpcMeshNodeAddr )
* @ brief Downloads and writes the image from the remote node
* @ param cpbNewOTAImage pointer to boolean to signal if a new image was successfully received
* @ param cpcMeshNodeAddr pointer to mesh node addr to receive the image segments from
* @ return ESP32 error code
* @ author Hendrik Schutter
* @ date 21.01 .2021
*
* Receives the images segments from remote node
* Writtes segments to OTA partition
* Handles OTA process
*/
esp_err_t errMeshOtaPartitionAccessMeshReceive ( bool * const cpbNewOTAImage , const mesh_addr_t * const cpcMeshNodeAddr )
{
esp_err_t err = ESP_OK ;
@ -201,8 +235,8 @@ esp_err_t errMeshOtaPartitionAccessMeshReceive(bool* const cpbNewOTAImage, const
bool bNodeIsResponding = false ; //remote node is still active
uint32_t u32OTABytesWritten = 0U ; //counter unsed for progress log
static esp_ota_handle_t otaHandle ; //OTA process handle
* cpbNewOTAImage = false ;
uint32_t u32SegmentCounter = 0U ;
* cpbNewOTAImage = false ; //set default to false
uint32_t u32SegmentCounter = 0U ; //counter of segments unsed for progress log
ERROR_CHECK ( esp_ota_begin ( pOTAPartition , OTA_SIZE_UNKNOWN , & otaHandle ) ) ; //start ota update process
@ -230,24 +264,21 @@ esp_err_t errMeshOtaPartitionAccessMeshReceive(bool* const cpbNewOTAImage, const
{
case OTA_Complete : //signal end of this OTA process, fall through because same behavior as OTA_Data
bComplete = true ;
ESP_LOGI ( LOG_TAG , " OTA-RX: rec Complete --> last segment " ) ;
//fall through
//fall through
case OTA_Data : //data segement received
bNodeIsResponding = true ;
ERROR_CHECK ( esp_ota_write ( otaHandle , sMeshPacket . au8Payload , OTA_MESH_SEGMENT_SIZE ) ) ;
u32OTABytesWritten = ( ( u32SegmentCounter + 1 ) * OTA_MESH_SEGMENT_SIZE ) ;
vMeshOtaUtilPrintOTAProgress ( & ( pOTAPartition - > size ) , & u32OTABytesWritten , Receiver ) ;
u32OTABytesWritten = ( ( u32SegmentCounter + 1 ) * OTA_MESH_SEGMENT_SIZE ) ; //calc bytes that are written in this ota process
vMeshOtaUtilPrintOTAProgress ( & ( pOTAPartition - > size ) , & u32OTABytesWritten , Receiver ) ;
u32Index = QUEUE_MESSAGE_OTA_SIZE ; //this will end the loop through all OTA messages
break ;
case OTA_Abort : //abort this OTA process
bAbort = true ;
bNodeIsResponding = true ;
ESP_LOGI ( LOG_TAG , " OTA-RX: rec Abort " ) ;
ESP_LOGE ( LOG_TAG , " OTA-RX: receives abort --> abort this OTA process on this node " ) ;
//this will end the loop through all OTA messages
break ;
default :
//receives wrong OTA message type from node --> back to queue
//vMeshOtaUtilAddOtaMessageToQueue(&sMeshPacket);
break ;
}
}
@ -256,16 +287,6 @@ esp_err_t errMeshOtaPartitionAccessMeshReceive(bool* const cpbNewOTAImage, const
//received from wrong node --> back to queue
vMeshOtaUtilAddOtaMessageToQueue ( & sMeshPacket ) ;
}
/* }
else
{
ESP_LOGI ( LOG_TAG , " OTA-RX: ota message queue empty --> wait " ) ;
// OTA Message queue is empty --> wait some time
vTaskDelay ( ( OTA_MESH_TIMEOUT ) / portTICK_PERIOD_MS ) ;
}
*/
} //end of OTA message loop
if ( bNodeIsResponding = = false )
@ -284,7 +305,6 @@ esp_err_t errMeshOtaPartitionAccessMeshReceive(bool* const cpbNewOTAImage, const
if ( bAbort = = false )
{
//no error while ota write --> send OTA_ACK packet
//ESP_LOGI(LOG_TAG, "OTA-RX: no error while ota write --> send OTA_ACK packet");
sMeshPacket . type = OTA_ACK ;
err = errMeshNetworkSendMeshPacket ( cpcMeshNodeAddr , & sMeshPacket ) ;
}
@ -305,7 +325,7 @@ esp_err_t errMeshOtaPartitionAccessMeshReceive(bool* const cpbNewOTAImage, const
{
//all OTA segments received --> validate
ESP_LOGI ( LOG_TAG , " OTA-RX: validate image " ) ;
ERROR_CHECK ( esp_ota_end ( otaHandle ) ) ;
ERROR_CHECK ( esp_ota_end ( otaHandle ) ) ; //validate image
ERROR_CHECK ( esp_ota_set_boot_partition ( pOTAPartition ) ) ;
if ( err = = ESP_OK )
{