1 Commits

Author SHA1 Message Date
7301305135 blocking OTA Tx and Rx 2021-01-20 10:23:41 +01:00
4 changed files with 403 additions and 317 deletions

View File

@ -11,6 +11,12 @@ SemaphoreHandle_t bsOTAProcess; //binary semaphore
const esp_partition_t* pOTAPartition; //pointer to ota partition
bool bWantReboot; //flag to signal pending reboot
bool bOtaAbortTx = false;
esp_partition_t* pBootPartitionTx; //pointer to boot partition (that will booted after reset)
uint32_t u32OTABytesWrittenTx = 0U;
uint32_t u32SegmentCounterTx = 0U;
bool bNodeIsRespondingTx = false; //remote node is still active
esp_err_t errMeshOTAInitialize()
{
esp_err_t err = ESP_OK;
@ -67,8 +73,12 @@ esp_err_t errMeshOTAInitialize()
}
ERROR_CHECK(errMeshNetworkSetChildConnectedHandle(vAddNodeToPossibleUpdatableQueue));
ERROR_CHECK(errMeshNetworkSetOTAMessageHandleHandle(vAddOtaMessageToQueue));
ERROR_CHECK(errMeshNetworkSetOTAMessageHandle(vAddOTAControllMessageToQueue));
ERROR_CHECK(errMeshNetworkSetChangeStateOfServerWorkerHandle(vChangeStateOfServerWorker));
ERROR_CHECK(errMeshNetworkSetOtaDataHandle(vRecOtaDataHandler));
ERROR_CHECK(errMeshNetworkSetOtaAckHandle(vRecOtaAckHandler));
//ERROR_CHECK(errMeshNetworkSetOtaCompleteHandle(vRecOtaCompleteHandler));
// ERROR_CHECK(errMeshNetworkSetOtaAbortHandle(vRecOtaAbortHandler));
if(err == ESP_OK)
{
@ -121,7 +131,7 @@ void vAddNodeToPossibleUpdatableQueue(uint8_t* pu8MAC)
}
}
void vAddOtaMessageToQueue(MESH_PACKET_t* puMeshPacket)
void vAddOTAControllMessageToQueue(MESH_PACKET_t* puMeshPacket)
{
//send ota packet to packet queue
if (xQueueSend(queueMessageOTA, puMeshPacket, portMAX_DELAY) != pdPASS)
@ -130,21 +140,7 @@ void vAddOtaMessageToQueue(MESH_PACKET_t* puMeshPacket)
}
else
{
switch (puMeshPacket->type)
{
case OTA_Abort:
ESP_LOGI(LOG_TAG, "added ota message to queue: OTA_Abort from 0x%x", puMeshPacket->meshSenderAddr.addr[5]);
break;
case OTA_Version_Request:
ESP_LOGI(LOG_TAG, "added ota message to queue: OTA_Version_Request from 0x%x", puMeshPacket->meshSenderAddr.addr[5]);
break;
case OTA_Version_Response:
ESP_LOGI(LOG_TAG, "added ota message to queue: OTA_Version Response from 0x%x", puMeshPacket->meshSenderAddr.addr[5]);
break;
default:
break;
}
ESP_LOGI(LOG_TAG, "added ota message to queue: %i (type)", puMeshPacket->type);
}
}
@ -234,7 +230,7 @@ void vTaskOTAWorker(void *arg)
if(bWantReboot == true)
{
//ESP_LOGI(LOG_TAG, "ESP32 Reboot ...");
ESP_LOGI(LOG_TAG, "ESP32 Reboot ...");
//vTaskDelay( (1000) / portTICK_PERIOD_MS);
//esp_restart();
}
@ -268,7 +264,7 @@ void vTaskOTAWorker(void *arg)
bWantReboot = true;
vAddAllNeighboursToQueue(); //add all existing neighbours to queue
}
vTaskDelay( (1000) / portTICK_PERIOD_MS);
vTaskDelay( (1000) / portTICK_PERIOD_MS); //TODO this is debug
}
}
@ -376,7 +372,7 @@ esp_err_t errOTAMeshSlave(bool* pbNewOTAImage)
pBootPartition = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
ERROR_CHECK(esp_ota_get_partition_description(pBootPartition, &bootPartitionDesc)); //get metadata of partition
//send OTA_Version_Response to sender of OTA_Version_Request packet wirh version in payload
ESP_LOGI(LOG_TAG, "//send OTA_Version_Response to sender of OTA_Version_Request packet with version in payload");
ERROR_CHECK(errSendOTAVersionResponse(&sOTAMessage.meshSenderAddr));
if((bNewerVersion((bootPartitionDesc).version, (char*) sOTAMessage.au8Payload)) && (err == ESP_OK)) //compare local and remote version
@ -416,62 +412,61 @@ esp_err_t errOTAMeshMaster(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
bNodeIsConnected = true; //node is one of the neighbours
xSemaphoreTake(bsOTAProcess, portMAX_DELAY); //wait for binary semaphore that allows to start the OTA process
ESP_LOGI(LOG_TAG, "Mesh-Master: send Version_Request to 0x%x", pMeshNodeAddr->addr[5]);
ERROR_CHECK(errSendOTAVersionRequest(pMeshNodeAddr)); //send OTA_VERSION_REQUEST with local version in payload
for (uint32_t u32Index = 0; u32Index < QUEUE_MESSAGE_OTA_SIZE; u32Index++) //loop through all OTA messages
{
if(uxQueueSpacesAvailable(queueMessageOTA) < QUEUE_MESSAGE_OTA_SIZE)
//if(uxQueueSpacesAvailable(queueMessageOTA) < QUEUE_MESSAGE_OTA_SIZE)
// {
//queue not empty
if (xQueueReceive(queueMessageOTA, &sOTAMessage, ((OTA_MESH_TIMEOUT) / portTICK_PERIOD_MS)) != pdTRUE)
{
//queue not empty
if (xQueueReceive(queueMessageOTA, &sOTAMessage, ((3000) / portTICK_PERIOD_MS)) != pdTRUE)
ESP_LOGE(LOG_TAG, "Unable to receive OTA Messages from queue");
err = ESP_FAIL;
}
if((err == ESP_OK) && (sOTAMessage.type == OTA_Version_Response) && (bCheckMACEquality(sOTAMessage.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
{
bNodeIsResponding = true;
u32Index = QUEUE_MESSAGE_OTA_SIZE; //this will exit the loop through all OTA messages
pBootPartition = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
ERROR_CHECK(esp_ota_get_partition_description(pBootPartition, &bootPartitionDesc)); //get metadata of partition
if((bNewerVersion((bootPartitionDesc).version, (char*) sOTAMessage.au8Payload)) && (err == ESP_OK)) //compare local and remote version
{
ESP_LOGE(LOG_TAG, "Unable to receive OTA Messages from queue");
err = ESP_FAIL;
//remote newer as local
ESP_LOGI(LOG_TAG, "Mesh: remote image on node is newer --> OTA update required");
// --> this version older --> start OTA_Rx --> set pbNewOTAImage true
ERROR_CHECK(errOTAMeshReceive(pbNewOTAImage, &sOTAMessage.meshSenderAddr));
}
if((err == ESP_OK) && (sOTAMessage.type == OTA_Version_Response) && (bCheckMACEquality(sOTAMessage.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
if((bNewerVersion((char*) sOTAMessage.au8Payload, (bootPartitionDesc).version)) && (err == ESP_OK)) //compare remote and local version
{
bNodeIsResponding = true;
pBootPartition = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
ERROR_CHECK(esp_ota_get_partition_description(pBootPartition, &bootPartitionDesc)); //get metadata of partition
if((bNewerVersion((bootPartitionDesc).version, (char*) sOTAMessage.au8Payload)) && (err == ESP_OK)) //compare local and remote version
{
//remote newer as local
ESP_LOGI(LOG_TAG, "Mesh: remote image on node is newer --> OTA update required");
// --> this version older --> start OTA_Rx --> set pbNewOTAImage true
ERROR_CHECK(errOTAMeshReceive(pbNewOTAImage, &sOTAMessage.meshSenderAddr));
}
if((bNewerVersion((char*) sOTAMessage.au8Payload, (bootPartitionDesc).version)) && (err == ESP_OK)) //compare remote and local version
{
//local newer as remote
ESP_LOGI(LOG_TAG, "Mesh: remote image on node is older --> OTA send required");
// --> this version newer --> start OTA_Tx
ERROR_CHECK(errOTAMeshTransmit(&sOTAMessage.meshSenderAddr));
}
}
else if (err == ESP_OK)
{
//received from wrong node or type --> back to queue
vAddOtaMessageToQueue(&sOTAMessage);
//local newer as remote
ESP_LOGI(LOG_TAG, "Mesh: remote image on node is older --> OTA send required");
// --> this version newer --> start OTA_Tx
ERROR_CHECK(errOTAMeshTransmit(&sOTAMessage.meshSenderAddr));
}
}
else if (err == ESP_OK)
{
ESP_LOGI(LOG_TAG, "//received from wrong node or type --> back to queue");
vAddOTAControllMessageToQueue(&sOTAMessage);
}
/* }
else
{
// OTA Message queue is empty --> wait some time
ESP_LOGI(LOG_TAG, "OTA-Master: OTA Message queue is empty --> wait some time");
vTaskDelay( (1000/QUEUE_MESSAGE_OTA_SIZE) / portTICK_PERIOD_MS);
}
{
ESP_LOGI(LOG_TAG, "// OTA Message queue is empty --> wait some time");
vTaskDelay(OTA_MESH_TIMEOUT*u32Index / portTICK_PERIOD_MS);
}
*/
}//end loop
xSemaphoreGive(bsOTAProcess); //free binary semaphore, this allows other tasks to start the OTA process
}
if((bNodeIsResponding == false) && (bNodeIsConnected == true))
{
//add node back to queue if connected and NOT responding
ESP_LOGI(LOG_TAG, "OTA-Master: connected and NOT responding --> add node back to queue ");
ESP_LOGI(LOG_TAG, "//add node back to queue if connected and NOT responding");
vAddNodeToPossibleUpdatableQueue(pMeshNodeAddr->addr);
}
return err;
@ -621,12 +616,12 @@ void vPrintOTAProgress(const uint32_t* const pu32TotalImageSize, const uint32_t*
{
if(eRole == Transmitter)
{
ESP_LOGI(LOG_TAG, "Transmitting OTA update: %i %%", u32Percentage);
ESP_LOGE(LOG_TAG, "Transmitting OTA update: %i %%", u32Percentage);
}
if(eRole == Receiver)
{
ESP_LOGI(LOG_TAG, "Receiving OTA update: %i %%", u32Percentage);
ESP_LOGE(LOG_TAG, "Receiving OTA update: %i %%", u32Percentage);
}
@ -674,33 +669,10 @@ esp_err_t errSendOTAVersionResponse(mesh_addr_t* pMeshReceiverAddr)
pBootPartition = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
ERROR_CHECK(esp_ota_get_partition_description(pBootPartition, &bootPartitionDesc)); //get metadate of partition
memcpy(&packet.au8Payload, &bootPartitionDesc.version, 12); //copy local version to OTA_Version_Response packet
ESP_LOGI(LOG_TAG, "Send OTA_Version_Response to 0x%x", pMeshReceiverAddr->addr[5]);
err = errSendMeshPacket(pMeshReceiverAddr, &packet);
return err;
}
void vClearOtaMessageQueue(mesh_addr_t* pMeshNodeAddr)
{
MESH_PACKET_t sMeshPacket; //packet for sending and receiving
for (uint32_t u32Index = 0; (u32Index < QUEUE_MESSAGE_OTA_SIZE); u32Index++) //loop through all OTA messages
{
if (xQueueReceive(queueMessageOTA, &sMeshPacket, 0) == pdTRUE)
{
if(!(bCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr)))
{
//received OTA message is NOT from pMeshNodeAddr --> keep it in queue
vAddOtaMessageToQueue(&sMeshPacket);
}
else
{
ESP_LOGI(LOG_TAG, "Removed type %i from node 0x%x", sMeshPacket.type, pMeshNodeAddr->addr[5]);
}
}
}//end OTA message loop
}
esp_err_t errSendOTAVersionRequest(mesh_addr_t* pMeshReceiverAddr)
{
esp_err_t err = ESP_OK;
@ -720,237 +692,301 @@ esp_err_t errSendOTAVersionRequest(mesh_addr_t* pMeshReceiverAddr)
esp_err_t errOTAMeshTransmit(mesh_addr_t* pMeshNodeAddr)
{
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; //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 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;
pBootPartition = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
pBootPartitionTx = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
u32OTABytesWrittenTx = 0U;
u32SegmentCounterTx = 0U;
bNodeIsRespondingTx = false; //remote node is still active
//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
// while( ((OTA_MESH_SEGMENT_SIZE * u32SegmentCounter) < pBootPartition->size) && (err == ESP_OK) && (bAbort == false))
{
//bNodeIsResponding = false; //reset to default
/*
ESP_LOGI(LOG_TAG, "// read partition with offset based in index");
ERROR_CHECK(esp_partition_read(pBootPartition, (OTA_MESH_SEGMENT_SIZE * u32Index), sMeshPacket.au8Payload, OTA_MESH_SEGMENT_SIZE));
u32OTABytesWritten = ((u32SegmentCounter+1) * OTA_MESH_SEGMENT_SIZE);
vPrintOTAProgress(&(pBootPartition->size), &u32OTABytesWritten, Transmitter);
// 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);
vPrintOTAProgress(&(pBootPartition->size), &u32OTABytesWritten, Transmitter);
if(err == ESP_OK)
{
//no error while read --> send OTA_DATA packet
sMeshPacket.type = OTA_Data;
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");
sMeshPacket.type = OTA_Complete;
}
//ESP_LOGI(LOG_TAG, "OTA-TX: send packet");
err = errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
else
{
// 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");
errSendMeshPacket(pMeshNodeAddr, &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
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) && (bCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
{
//packet from node received
switch (sMeshPacket.type)
{
case OTA_ACK: //increase index for next round
u32Index++;
bNodeIsResponding = true;
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
//vAddOtaMessageToQueue(&sMeshPacket);
break;
}
}
else if (err == ESP_OK)
{
//received from wrong node --> back to queue
vAddOtaMessageToQueue(&sMeshPacket);
}
/*
}
else
if(err == ESP_OK)
{
// 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);
//ESP_LOGI(LOG_TAG, "//no error while read --> send OTA_DATA packet");
sMeshPacket.type = OTA_Data;
if((OTA_MESH_SEGMENT_SIZE * (u32SegmentCounter+1)) >= pBootPartition->size) //check if last segment
{
ESP_LOGI(LOG_TAG, "//last partition image segment --> send OTA_Complete");
sMeshPacket.type = OTA_Complete;
ESP_LOGE(LOG_TAG, "u32SegmentCounter: %i", u32SegmentCounter);
}
err = errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
else
{
ESP_LOGI(LOG_TAG, "// error while read --> send OTA_ABORT and abort this OTA process");
sMeshPacket.type = OTA_Abort;
bAbort = true;
errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
*/
// loop through all OTA messages or until abort is called or error
for (uint32_t u32Index = 0; ((u32Index < QUEUE_MESSAGE_OTA_SIZE) && (bOtaAbortTx == false)); u32Index++) //loop through all OTA messages
{
// if(uxQueueSpacesAvailable(queueMessageOTA) < QUEUE_MESSAGE_OTA_SIZE)
// {
//queue not empty
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;
}
*/
}//end OTA message loop
if((err == ESP_OK) && (bCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
{
ESP_LOGI(LOG_TAG, "//packet from node received");
switch (sMeshPacket.type)
{
case OTA_Abort: //abort this OTA process
bOtaAbortTx = true;
bNodeIsRespondingTx = true;
break;
default:
ESP_LOGI(LOG_TAG, "//receives wrong OTA message type from node --> back to queue");
vAddOTAControllMessageToQueue(&sMeshPacket);
break;
}
}
else if (err == ESP_OK)
{
ESP_LOGI(LOG_TAG, "//received from wrong node --> back to queue");
vAddOTAControllMessageToQueue(&sMeshPacket);
if(bNodeIsResponding == false)
{
//no abort was called but node didnt responded
ESP_LOGE(LOG_TAG, "OTA-TX: no abort was called but node didnt responded --> error");
bAbort = true;
err = ESP_FAIL; //this OTA process failed with error
}
u32SegmentCounter++;
}//end of partition segment loop
vClearOtaMessageQueue(pMeshNodeAddr);
return err;
}
if(bNodeIsRespondingTx == false)
{
ESP_LOGI(LOG_TAG, "//no abort was called but node didnt responded");
bOtaAbortTx = true;
err = ESP_FAIL; //this OTA process failed with error
}
else
{
bNodeIsRespondingTx = false;
}
esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
{
esp_err_t err = ESP_OK;
MESH_PACKET_t sMeshPacket; //packet for sending and receiving
bool bComplete = false; //complete the OTA process
bool bAbort = false; //abort the OTA process
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
*pbNewOTAImage = false;
uint32_t u32SegmentCounter = 0U;
ERROR_CHECK(esp_ota_begin(pOTAPartition, OTA_SIZE_UNKNOWN, &otaHandle)); //start ota update process
}
// }
// else
//{
// ESP_LOGI(LOG_TAG, "// OTA Message queue is empty --> wait some time");
//vTaskDelay(OTA_MESH_TIMEOUT*u32Index/ portTICK_PERIOD_MS);
// }
//partition segement loop through partition to read in segmensts until end or error or abort called
while((bComplete == false) && (err == ESP_OK) && (bAbort == false) && (u32OTABytesWritten <= pOTAPartition->size))
{
bNodeIsResponding = false; //reset to default
}//end OTA message loop
// loop through all OTA messages or until abort is called
for (uint32_t u32Index = 0; ((u32Index < QUEUE_MESSAGE_OTA_SIZE) && (bAbort == false)); u32Index++) //loop through all OTA messages
{
//if(uxQueueSpacesAvailable(queueMessageOTA) < QUEUE_MESSAGE_OTA_SIZE)
// {
//queue not empty
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;
}
}
// u32SegmentCounter++;
//}//end of partition segment loop
return err;
}
if((err == ESP_OK) && (bCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
{
//packet from node received
switch (sMeshPacket.type)
{
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
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);
vPrintOTAProgress(&(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");
//this will end the loop through all OTA messages
break;
default:
//receives wrong OTA message type from node --> back to queue
//vAddOtaMessageToQueue(&sMeshPacket);
break;
}
}
else if (err == ESP_OK)
{
//received from wrong node --> back to queue
vAddOtaMessageToQueue(&sMeshPacket);
}
esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
{
esp_err_t err = ESP_OK;
MESH_PACKET_t sMeshPacket; //packet for sending and receiving
bool bComplete = false; //complete the OTA process
bool bAbort = false; //abort the OTA process
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
*pbNewOTAImage = false;
uint32_t u32SegmentCounter = 0U;
/* }
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);
}
*/
ERROR_CHECK(esp_ota_begin(pOTAPartition, OTA_SIZE_UNKNOWN, &otaHandle)); //start ota update process
}//end of OTA message loop
//partition segement loop through partition to read in segmensts until end or error or abort called
while((bComplete == false) && (err == ESP_OK) && (bAbort == false) && (u32OTABytesWritten <= pOTAPartition->size))
{
bNodeIsResponding = false; //reset to default
if(bNodeIsResponding == false)
{
//no abort was called but node didnt responded --> error
ESP_LOGI(LOG_TAG, "OTA-RX: no abort was called but node didnt responded --> error");
bAbort = true; //this will stop the partition segement loop
err = ESP_FAIL; //this OTA process failed with error
}
else
{
//node has responded with OTA_DATA or OTA_Complete or OTA_ABORT
if(err == ESP_OK)
{
// loop through all OTA messages or until abort is called
for (uint32_t u32Index = 0; ((u32Index < QUEUE_MESSAGE_OTA_SIZE) && (bAbort == false)); u32Index++) //loop through all OTA messages
{
//if(uxQueueSpacesAvailable(queueMessageOTA) < QUEUE_MESSAGE_OTA_SIZE)
// {
//queue not empty
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(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 = errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
}
else
{
// error while read --> send OTA_ABORT and abort this OTA process
sMeshPacket.type = OTA_Abort;
bAbort = true;
ESP_LOGI(LOG_TAG, "OTA-RX: abort --> send ABORT");
errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
}
u32SegmentCounter++;
}//end of partition segement loop
if((err == ESP_OK) && (bCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
{
ESP_LOGI(LOG_TAG, "//packet from node received");
switch (sMeshPacket.type)
{
case OTA_Complete:
ESP_LOGI(LOG_TAG, "//signal end of this OTA process, fall through because same behavior as OTA_Data");
bComplete = true;
ESP_LOGE(LOG_TAG, "after rec:Complete u32SegmentCounter: %i", u32SegmentCounter);
//fall through
case OTA_Data:
ESP_LOGI(LOG_TAG, "//data segement received");
bNodeIsResponding = true;
u32Index = QUEUE_MESSAGE_OTA_SIZE; //this will exit the loop through all OTA messages
ERROR_CHECK(esp_ota_write(otaHandle, sMeshPacket.au8Payload, OTA_MESH_SEGMENT_SIZE));
if(bComplete == true)
{
//all OTA segments received --> validate
ESP_LOGI(LOG_TAG, "OTA-RX: validate image ");
ERROR_CHECK(esp_ota_end(otaHandle));
ERROR_CHECK(esp_ota_set_boot_partition(pOTAPartition));
if(err == ESP_OK)
{
//successfully updated OTA partition
*pbNewOTAImage = true;
}
}
else
{
//not all OTA segments received --> abort this OTA process
ERROR_CHECK(esp_ota_abort(otaHandle));
}
vClearOtaMessageQueue(pMeshNodeAddr);
return err;
}
if(err != ESP_OK)
{
ESP_LOGE(LOG_TAG, "write error - Segement: %i", u32SegmentCounter);
}
u32OTABytesWritten = ((u32SegmentCounter+1) * OTA_MESH_SEGMENT_SIZE);
vPrintOTAProgress(&(pOTAPartition->size), &u32OTABytesWritten, Receiver);
break;
case OTA_Abort: //abort this OTA process
bAbort = true;
bNodeIsResponding = true;
break;
default:
ESP_LOGI(LOG_TAG, "//receives wrong OTA message type from node --> back to queue");
vAddOTAControllMessageToQueue(&sMeshPacket);
break;
}
}
else if (err == ESP_OK)
{
ESP_LOGI(LOG_TAG, "//received from wrong node --> back to queue");
vAddOTAControllMessageToQueue(&sMeshPacket);
}
// }
// else
// {
// ESP_LOGI(LOG_TAG, "// OTA Message queue is empty --> wait some time");
// vTaskDelay((OTA_MESH_TIMEOUT*u32Index) / portTICK_PERIOD_MS);
// }
}//end of OTA message loop
if(bNodeIsResponding == false)
{
ESP_LOGI(LOG_TAG, "//no abort was called but node didnt responded --> error");
bAbort = true; //this will stop the partition segement loop
err = ESP_FAIL; //this OTA process failed with error
}
else
{
ESP_LOGI(LOG_TAG, "//node has responded with OTA_DATA or OTA_Complete or OTA_ABORT");
if(err == ESP_OK)
{
if((bAbort == false) && (bComplete == false))
{
ESP_LOGI(LOG_TAG, "//no error while ota write --> send OTA_ACK packet");
sMeshPacket.type = OTA_ACK;
err = errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
}
else if (bComplete == false)
{
ESP_LOGI(LOG_TAG, "// error while read --> send OTA_ABORT and abort this OTA process");
sMeshPacket.type = OTA_Abort;
bAbort = true;
errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
}
u32SegmentCounter++;
}//end of partition segement loop
if(bComplete == true)
{
ESP_LOGI(LOG_TAG, "//all OTA segments received --> validate");
ERROR_CHECK(esp_ota_end(otaHandle));
ERROR_CHECK(esp_ota_set_boot_partition(pOTAPartition));
if(err == ESP_OK)
{
ESP_LOGI(LOG_TAG, "//successfully updated OTA partition");
*pbNewOTAImage = true;
}
}
else
{
ESP_LOGI(LOG_TAG, "//not all OTA segments received --> abort this OTA process");
ERROR_CHECK(esp_ota_abort(otaHandle));
}
return err;
}
void vRecOtaDataHandler(MESH_PACKET_t* puMeshPacket)
{
if(bOtaAbortTx == false)
{
}
}
void vRecOtaAckHandler(MESH_PACKET_t* puMeshPacket)
{
esp_err_t err = ESP_OK;
MESH_PACKET_t sMeshPacket; //packet for sending and receiving
if(bOtaAbortTx == false)
{
//sender node == ota node ?
bNodeIsRespondingTx = true;
ESP_LOGI(LOG_TAG, "// read partition with offset based in index");
ERROR_CHECK(esp_partition_read(pBootPartitionTx, (OTA_MESH_SEGMENT_SIZE * u32SegmentCounterTx), sMeshPacket.au8Payload, OTA_MESH_SEGMENT_SIZE));
u32OTABytesWrittenTx = ((u32SegmentCounterTx+1) * OTA_MESH_SEGMENT_SIZE);
vPrintOTAProgress(&(pBootPartitionTx->size), &u32OTABytesWrittenTx, Transmitter);
sMeshPacket.type = OTA_Data;
if(err != ESP_OK)
{
bOtaAbortTx = true;
sMeshPacket.type = OTA_Abort;
}
if((OTA_MESH_SEGMENT_SIZE * (u32SegmentCounterTx+1)) >= pBootPartitionTx->size) //check if last segment
{
ESP_LOGI(LOG_TAG, "//last partition image segment --> send OTA_Complete");
sMeshPacket.type = OTA_Complete;
ESP_LOGE(LOG_TAG, "u32SegmentCounterTx: %i", u32SegmentCounterTx);
}
err = errSendMeshPacket(&puMeshPacket->meshSenderAddr, &sMeshPacket);
u32SegmentCounterTx++;
}
}
/*
void vRecOtaCompleteHandler(MESH_PACKET_t* puMeshPacket)
{
return;
}
void vRecOtaAbortHandler(MESH_PACKET_t* puMeshPacket)
{
return;
}
*/

View File

@ -14,6 +14,12 @@ void (*pOTAChildConnectHandle)(uint8_t* );
void (*pOTAMessageHandle)(MESH_PACKET_t* );
void (*pChangeStateOfServerWorkerHandle)(bool );
void (*pOtaDataHandle)(MESH_PACKET_t* );
void (*pOtaAckHandle)(MESH_PACKET_t* );
void (*pOtaCompleteHandle)(MESH_PACKET_t* );
void (*pOtaAbortHandle)(MESH_PACKET_t* );
esp_err_t errMeshNetworkInitialize()
{
//init module variables
@ -127,15 +133,6 @@ bool bCheckMACEquality(uint8_t* pu8aMAC, uint8_t* pu8bMAC)
{
bRet = false;
}
if(index == 5)
{
//last byte of mac
if(abs((pu8aMAC[index] - pu8bMAC[index])) <= 1)
{
bRet = true; //last byte differs 1 ore less
}
}
index++;
}
return bRet;
@ -239,12 +236,36 @@ esp_err_t errMeshNetworkSetChildConnectedHandle(void (*pChildConnectHandleTmp)(u
return ESP_OK;
}
esp_err_t errMeshNetworkSetOTAMessageHandleHandle(void (*pOTAMessageHandleTmp)(MESH_PACKET_t* puMeshPacket))
esp_err_t errMeshNetworkSetOTAMessageHandle(void (*pOTAMessageHandleTmp)(MESH_PACKET_t* puMeshPacket))
{
pOTAMessageHandle = pOTAMessageHandleTmp;
return ESP_OK;
}
esp_err_t errMeshNetworkSetOtaDataHandle(void (*pOtaDataHandleTmp)(MESH_PACKET_t* puMeshPacket))
{
pOtaDataHandle = pOtaDataHandleTmp;
return ESP_OK;
}
esp_err_t errMeshNetworkSetOtaAckHandle(void (*pOtaAckHandleTmp)(MESH_PACKET_t* puMeshPacket))
{
pOtaAckHandle = pOtaAckHandleTmp;
return ESP_OK;
}
esp_err_t errMeshNetworkSetOtaCompleteHandle(void (*pOtaCompleteHandleTmp)(MESH_PACKET_t* puMeshPacket))
{
pOtaCompleteHandle = pOtaCompleteHandleTmp;
return ESP_OK;
}
esp_err_t errMeshNetworkSetOtaAbortHandle(void (*pOtaAbortHandleTmp)(MESH_PACKET_t* puMeshPacket))
{
pOtaAbortHandle = pOtaAbortHandleTmp;
return ESP_OK;
}
esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOfServerWorkerHandleTmp)(bool bState))
{
pChangeStateOfServerWorkerHandle = pChangeStateOfServerWorkerHandleTmp;
@ -311,8 +332,6 @@ void vTaskReceiveMeshData(void *arg)
break;
case OTA_Version_Request:
case OTA_Version_Response:
case OTA_Data:
case OTA_ACK:
case OTA_Complete:
case OTA_Abort:
//call the rx handle from OTA
@ -321,6 +340,31 @@ void vTaskReceiveMeshData(void *arg)
pOTAMessageHandle(&packet);
}
break;
case OTA_Data:
if(pOtaDataHandle)
{
pOtaDataHandle(&packet);
}
break;
case OTA_ACK:
if(pOtaAckHandle)
{
pOtaAckHandle(&packet);
}
break;
/* case OTA_Complete:
if(pOtaCompleteHandle)
{
pOtaCompleteHandle(&packet);
}
break;
case OTA_Abort:
if(pOtaAbortHandle)
{
pOtaAbortHandle(&packet);
}
break;
*/
default:
ESP_LOGE(LOG_TAG, "recv: something");
break;
@ -330,5 +374,3 @@ void vTaskReceiveMeshData(void *arg)

View File

@ -23,7 +23,7 @@
#define OTA_HTTPS_SEGMENT_SIZE 2048U
#define OTA_PROGRESS_LOG_INTERVAL 7U
#define OTA_MESH_SEGMENT_SIZE MESH_NETWORK_PAYLOAD_SIZE
#define OTA_MESH_TIMEOUT 20000U //in ms
#define OTA_MESH_TIMEOUT 100U //in ms
#define ERROR_CHECK(x) if (err == ESP_OK) \
{ \
@ -59,13 +59,17 @@ void vPrintOTAProgress(const uint32_t* const pu32TotalImageSize, const uint32_t*
void vAddAllNeighboursToQueue(void);
esp_err_t errSendOTAVersionResponse(mesh_addr_t* meshReceiverAddr);
esp_err_t errSendOTAVersionRequest(mesh_addr_t* meshReceiverAddr);
void vClearOtaMessageQueue(mesh_addr_t* pMeshNodeAddr);
//Handler
void vAddNodeToPossibleUpdatableQueue(uint8_t* pu8MAC);
void vAddOtaMessageToQueue(MESH_PACKET_t* puMeshPacket);
void vAddOTAControllMessageToQueue(MESH_PACKET_t* puMeshPacket);
void vChangeStateOfServerWorker(bool state);
void vRecOtaDataHandler(MESH_PACKET_t* puMeshPacket);
void vRecOtaAckHandler(MESH_PACKET_t* puMeshPacket);
//void vRecOtaCompleteHandler(MESH_PACKET_t* puMeshPacket);
//void vRecOtaAbortHandler(MESH_PACKET_t* puMeshPacket);
//Tasks
void vTaskServerWorker(void *arg);
void vTaskOTAWorker(void *arg);

View File

@ -8,7 +8,6 @@
#include "esp_log.h"
#include "esp_mesh.h"
#include "esp_mesh_internal.h"
#include <stdlib.h>
#ifndef CONFIG_MESH_MESSAGE_SIZE
#define CONFIG_MESH_MESSAGE_SIZE 1500
@ -81,7 +80,12 @@ esp_err_t errMeshNetworkInitializeRouter(mesh_cfg_t* cfg);
esp_err_t errMeshNetworkSetAppReceiveHandle(void (*pAppRxHandleTmp)(uint8_t * pu8Data, uint8_t* pu8Sender));
esp_err_t errMeshNetworkSetChildConnectedHandle(void (*pChildConnectHandleTmp)(uint8_t * pu8Data));
esp_err_t errMeshNetworkSetOTAMessageHandleHandle(void (*pOTAMessageHandleTmp)(MESH_PACKET_t* puMeshPacket));
esp_err_t errMeshNetworkSetOTAMessageHandle(void (*pOTAMessageHandleTmp)(MESH_PACKET_t* puMeshPacket));
esp_err_t errMeshNetworkSetOtaDataHandle(void (*pOtaDataHandleTmp)(MESH_PACKET_t* puMeshPacket));
esp_err_t errMeshNetworkSetOtaAckHandle(void (*pOtaAckHandleTmp)(MESH_PACKET_t* puMeshPacket));
esp_err_t errMeshNetworkSetOtaCompleteHandle(void (*pOtaCompleteHandleTmp)(MESH_PACKET_t* puMeshPacket));
esp_err_t errMeshNetworkSetOtaAbortHandle(void (*pOtaAbortHandleTmp)(MESH_PACKET_t* puMeshPacket));
esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOfServerWorkerHandleTmp)(bool bState));