Compare commits
14 Commits
f3749b9985
...
master
Author | SHA1 | Date | |
---|---|---|---|
b4a750bda0 | |||
b9e0769843 | |||
8e614e76c7 | |||
a930f709ff | |||
78dbbfaba5 | |||
9d23788ac8 | |||
e73a05fc42 | |||
a62d3265ca | |||
e88b6e2664 | |||
0f03f2da1c | |||
e5801dd1e1 | |||
c3d6e3c8a2 | |||
9600b76a80 | |||
7fe1f18985 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -60,3 +60,4 @@ test/build/
|
||||
|
||||
*.orig
|
||||
|
||||
Doxygen/html/
|
||||
|
15
README.md
15
README.md
@ -1,19 +1,4 @@
|
||||
# ESP32-Mesh-OTA
|
||||
|
||||
## Work in progress
|
||||
<img src="https://patenschaft.bienenweide.org/img/loading.gif" alt="code_example_output" width="50"/>
|
||||
|
||||
## Todo for first release
|
||||
|
||||
### Refactoring
|
||||
- architecture
|
||||
- full error handling through all functions
|
||||
- export as a component library
|
||||
|
||||
### Features
|
||||
- root node: Download new Firmware from HTTPS server instead using newest ota partition
|
||||
- add example main app
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -56,7 +56,7 @@ https_client_ret_t errHTTPSClientInitialize(void)
|
||||
* @brief receive a image segment from server
|
||||
* @param cpu8Data data buffer
|
||||
* @param cpcu32DataLenght desired byte amount
|
||||
* @param pu32BytesRead actual received byte amount
|
||||
* @param pu32BytesRead actual received byte amount
|
||||
* @return HTTPS_Client error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 20.01.2021
|
||||
|
@ -224,6 +224,15 @@ esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOf
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkSendMeshPacket(const mesh_addr_t* const cpcAddrDest, const MESH_PACKET_t* const cpcPacket)
|
||||
* @brief send packet to mesh node
|
||||
* @param cpcAddrDest address from mesh node
|
||||
* @param cpcPacket packet to send
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 20.01.2021
|
||||
*/
|
||||
esp_err_t errMeshNetworkSendMeshPacket(const mesh_addr_t* const cpcAddrDest, const MESH_PACKET_t* const cpcPacket)
|
||||
{
|
||||
esp_err_t err;
|
||||
@ -240,11 +249,27 @@ esp_err_t errMeshNetworkSendMeshPacket(const mesh_addr_t* const cpcAddrDest, con
|
||||
return err;
|
||||
}
|
||||
|
||||
bool bMeshNetworkIsRootNode()
|
||||
/**
|
||||
* @fn bool bMeshNetworkIsRootNode()
|
||||
* @brief return true if this node is the root
|
||||
* @param void
|
||||
* @return boolean
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
bool bMeshNetworkIsRootNode(void)
|
||||
{
|
||||
return esp_mesh_is_root();
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn bool bMeshNetworkIsNodeNeighbour(const mesh_addr_t* const cpcNode)
|
||||
* @brief return true if node is neighbour if this
|
||||
* @param cpcNode to check
|
||||
* @return boolean
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
bool bMeshNetworkIsNodeNeighbour(const mesh_addr_t* const cpcNode)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
@ -257,7 +282,7 @@ bool bMeshNetworkIsNodeNeighbour(const mesh_addr_t* const cpcNode)
|
||||
|
||||
if(err == ESP_OK)
|
||||
{
|
||||
if(bMeshNetworkCheckMACEquality(cpcNode->addr, addrParent.addr) == true)
|
||||
if(bMeshNetworkCheckMacEquality(cpcNode->addr, addrParent.addr) == true)
|
||||
{
|
||||
bReturn = true; //node was found
|
||||
}
|
||||
@ -271,7 +296,7 @@ bool bMeshNetworkIsNodeNeighbour(const mesh_addr_t* const cpcNode)
|
||||
|
||||
for (uint16_t u16Index = 0; ((u16Index < u16ChildrenSize) && (err == ESP_OK) && (bReturn == false)); u16Index++)
|
||||
{
|
||||
if(bMeshNetworkCheckMACEquality(cpcNode->addr, childrenAddr[u16Index].addr) == true)
|
||||
if(bMeshNetworkCheckMacEquality(cpcNode->addr, childrenAddr[u16Index].addr) == true)
|
||||
{
|
||||
bReturn = true; //node was found
|
||||
}
|
||||
@ -280,33 +305,41 @@ bool bMeshNetworkIsNodeNeighbour(const mesh_addr_t* const cpcNode)
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
//returns true if MAC address is equal
|
||||
bool bMeshNetworkCheckMACEquality(const uint8_t* const cpcu8aMAC, const uint8_t* const cpcu8bMAC)
|
||||
|
||||
/**
|
||||
* @fn bool bMeshNetworkCheckMacEquality(const uint8_t* const cpcu8aMAC, const uint8_t* const cpcu8bMAC)
|
||||
* @brief returns true if MAC address is equal, compares only the first 5 bytes, the last is node specific
|
||||
* @param cpcu8aMAC first MAC
|
||||
* @param cpcu8bMAC second MAC
|
||||
* @return boolean
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
bool bMeshNetworkCheckMacEquality(const uint8_t* const cpcu8aMAC, const uint8_t* const cpcu8bMAC)
|
||||
{
|
||||
bool bRet = true;
|
||||
uint8_t index = 0;
|
||||
|
||||
while ((index < 6) && (bRet == true))
|
||||
while ((index < 5) && (bRet == true))
|
||||
{
|
||||
if(cpcu8aMAC[index] != cpcu8bMAC[index])
|
||||
{
|
||||
bRet = false;
|
||||
}
|
||||
|
||||
if(index == 5)
|
||||
{
|
||||
//last byte of mac
|
||||
if(abs((cpcu8aMAC[index] - cpcu8bMAC[index])) <= 1)
|
||||
{
|
||||
bRet = true; //last byte differs 1 ore less
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return bRet;
|
||||
}
|
||||
|
||||
esp_err_t errMeshNetworkStartReceiveTask()
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkStartReceiveTask()
|
||||
* @brief start the task to receive the mesh packets
|
||||
* @param void
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
esp_err_t errMeshNetworkStartReceiveTask(void)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
BaseType_t xReturned;
|
||||
@ -320,11 +353,28 @@ esp_err_t errMeshNetworkStartReceiveTask()
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn vMeshNetworkGetOwnAddr(mesh_addr_t* const cpMeshOwnAddr)
|
||||
* @brief return own MAC addr
|
||||
* @param cpMeshOwnAddr pointer to own mac
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshNetworkGetOwnAddr(mesh_addr_t* const cpMeshOwnAddr)
|
||||
{
|
||||
memcpy(cpMeshOwnAddr->addr, u8ownMAC, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkGetChildren(mesh_addr_t* const cpChildren, uint16_t* const cpu16ChildrenSize)
|
||||
* @brief get all connected children to node in array
|
||||
* @param cpChildren pointer to array
|
||||
* @param cpu16ChildrenSize pointer to size of array
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
esp_err_t errMeshNetworkGetChildren(mesh_addr_t* const cpChildren, uint16_t* const cpu16ChildrenSize)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
@ -337,10 +387,9 @@ esp_err_t errMeshNetworkGetChildren(mesh_addr_t* const cpChildren, uint16_t* con
|
||||
{
|
||||
for(uint16_t index = 0; index < esp_mesh_get_routing_table_size(); index++)
|
||||
{
|
||||
if(! (bMeshNetworkCheckMACEquality(u8ownMAC, route_table[index].addr)) )
|
||||
if(! (bMeshNetworkCheckMacEquality(u8ownMAC, route_table[index].addr)) )
|
||||
{
|
||||
//child node
|
||||
//ESP_LOGI(LOG_TAG, "adding Node: \"0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\" ", route_table[index].addr[0], route_table[index].addr[1], route_table[index].addr[2], route_table[index].addr[3], route_table[index].addr[4], route_table[index].addr[5]);
|
||||
cpChildren[*cpu16ChildrenSize] = route_table[index];
|
||||
(*cpu16ChildrenSize) = (*cpu16ChildrenSize)+1;
|
||||
}
|
||||
@ -349,6 +398,14 @@ esp_err_t errMeshNetworkGetChildren(mesh_addr_t* const cpChildren, uint16_t* con
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn void vMeshNetworkTaskReceiveMeshData(void *arg)
|
||||
* @brief Task to receive all mesh packets
|
||||
* @param arg
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshNetworkTaskReceiveMeshData(void *arg)
|
||||
{
|
||||
esp_err_t err;
|
||||
@ -398,6 +455,14 @@ void vMeshNetworkTaskReceiveMeshData(void *arg)
|
||||
} //end while
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkGetParentNode(mesh_addr_t* const cpMeshParentAddr)
|
||||
* @brief get parrent node if connected to it
|
||||
* @param cpMeshParentAddr pointer to parent node addrs
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
esp_err_t errMeshNetworkGetParentNode(mesh_addr_t* const cpMeshParentAddr)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
|
@ -1,3 +1,10 @@
|
||||
/**
|
||||
* @file Mesh_OTA.c
|
||||
* @brief Start and implement OTA updates via HTTPS from server and other mesh nodes (bidirectional)
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
|
||||
#include "Mesh_OTA.h"
|
||||
#include "Mesh_OTA_Util.h"
|
||||
#include "Mesh_OTA_Globals.h"
|
||||
@ -5,7 +12,18 @@
|
||||
|
||||
static const char *LOG_TAG = "mesh_ota";
|
||||
|
||||
esp_err_t errMeshOTAInitialize()
|
||||
/**
|
||||
* @fn esp_err_t errMeshOTAInitialize(void)
|
||||
* @brief Starts Mesh OTA functionality
|
||||
* @param void
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*
|
||||
* Initialize queues and tasks
|
||||
* Set callbacks
|
||||
*/
|
||||
esp_err_t errMeshOTAInitialize(void)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
BaseType_t xReturned;
|
||||
@ -15,7 +33,7 @@ esp_err_t errMeshOTAInitialize()
|
||||
queueNodes = xQueueCreate(QUEUE_NODES_SIZE, sizeof(mesh_addr_t));
|
||||
if (queueNodes == 0) // Queue not created
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "Unable to create Queue for Nodes");
|
||||
ESP_LOGE(LOG_TAG, "Unable to create queue for nodes");
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
|
||||
@ -25,7 +43,7 @@ esp_err_t errMeshOTAInitialize()
|
||||
queueMessageOTA = xQueueCreate(QUEUE_MESSAGE_OTA_SIZE, sizeof(MESH_PACKET_t));
|
||||
if (queueMessageOTA == 0) // Queue not created
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "Unable to create Queue for OTA Messages");
|
||||
ESP_LOGE(LOG_TAG, "Unable to create queue for OTA messages");
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
}
|
||||
@ -35,7 +53,7 @@ esp_err_t errMeshOTAInitialize()
|
||||
bsStartStopServerWorker = xSemaphoreCreateBinary();
|
||||
if( bsStartStopServerWorker == NULL )
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "Unable to create Mutex to represent state of Server worker");
|
||||
ESP_LOGE(LOG_TAG, "Unable to create mutex to represent state of server worker");
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
}
|
||||
@ -45,7 +63,7 @@ esp_err_t errMeshOTAInitialize()
|
||||
bsOTAProcess = xSemaphoreCreateBinary();
|
||||
if( bsOTAProcess == NULL )
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "Unable to create Mutex to grant access to OTA Process");
|
||||
ESP_LOGE(LOG_TAG, "Unable to create mutex to grant access to OTA process");
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
}
|
||||
@ -55,11 +73,12 @@ esp_err_t errMeshOTAInitialize()
|
||||
xSemaphoreGive(bsOTAProcess); //unlock binary semaphore
|
||||
if( bsOTAProcess == NULL )
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "Unable to unlock Mutex to grant access to OTA Process");
|
||||
ESP_LOGE(LOG_TAG, "Unable to unlock mutex to grant access to OTA process");
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
//register callbacks in network
|
||||
ERROR_CHECK(errMeshNetworkSetChildConnectedHandle(vMeshOtaUtilAddNodeToPossibleUpdatableQueue));
|
||||
ERROR_CHECK(errMeshNetworkSetOTAMessageHandleHandle(vMeshOtaUtilAddOtaMessageToQueue));
|
||||
ERROR_CHECK(errMeshNetworkSetChangeStateOfServerWorkerHandle(vMeshOtaUtilChangeStateOfServerWorker));
|
||||
@ -95,13 +114,20 @@ esp_err_t errMeshOTAInitialize()
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn void vMeshOtaTaskServerWorker(void *arg)
|
||||
* @brief Task for updating from server via HTTPS
|
||||
* @param arg
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaTaskServerWorker(void *arg)
|
||||
{
|
||||
esp_err_t err;
|
||||
esp_err_t err = ESP_OK;
|
||||
bool bNewOTAImage; //true if a new ota image was downloaded and validated
|
||||
bool bFirstRun = true;
|
||||
|
||||
@ -118,6 +144,7 @@ void vMeshOtaTaskServerWorker(void *arg)
|
||||
|
||||
if(bFirstRun == true)
|
||||
{
|
||||
//init on first run
|
||||
ERROR_CHECK(errHTTPSClientInitialize());
|
||||
bFirstRun = false;
|
||||
}
|
||||
@ -134,18 +161,26 @@ void vMeshOtaTaskServerWorker(void *arg)
|
||||
//set want reboot
|
||||
ESP_LOGI(LOG_TAG, "Updated successfully via HTTPS, set pending reboot");
|
||||
bWantReboot = true;
|
||||
vMeshOtaUtilAddAllNeighboursToQueue(); //add all existing neighbours to queue (aparent will not be added because this node is the root)
|
||||
}
|
||||
vTaskDelay( (SERVER_CHECK_INTERVAL*1000) / portTICK_PERIOD_MS); //sleep till next server checks
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn void vMeshOtaTaskServerWorker(void *arg)
|
||||
* @brief Task for updating from nodes in mesh network
|
||||
* @param arg
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaTaskOTAWorker(void *arg)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
bool bNewOTAImage; //true if a new ota image was downloaded and validated
|
||||
mesh_addr_t meshNodeAddr; //node that should be checked for ota update
|
||||
BaseType_t xReturned;
|
||||
|
||||
while(true)
|
||||
{
|
||||
@ -155,22 +190,21 @@ void vMeshOtaTaskOTAWorker(void *arg)
|
||||
if((uxQueueSpacesAvailable(queueNodes) - QUEUE_NODES_SIZE) == 0)
|
||||
{
|
||||
//nodes queue is empty
|
||||
ESP_LOGI(LOG_TAG, "nodes queue is empty");
|
||||
|
||||
if((bWantReboot == true) && (OTA_ALLOW_REBOOT == 1))
|
||||
xReturned = xSemaphoreTake(bsOTAProcess, portMAX_DELAY); //wait for binary semaphore that allows to start the OTA process
|
||||
if((xReturned == pdTRUE) && (bWantReboot == true) && (OTA_ALLOW_REBOOT == 1))
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "ESP32 Reboot ...");
|
||||
vTaskDelay( (1000) / portTICK_PERIOD_MS);
|
||||
esp_restart();
|
||||
}
|
||||
|
||||
ERROR_CHECK(errMeshOtaSlaveEndpoint(&bNewOTAImage));
|
||||
xSemaphoreGive(bsOTAProcess); //free binary semaphore, this allows other tasks to start the OTA process
|
||||
ERROR_CHECK(errMeshOtaSlaveEndpoint(&bNewOTAImage, &meshNodeAddr));
|
||||
}
|
||||
else
|
||||
{
|
||||
//queue not empty
|
||||
ESP_LOGI(LOG_TAG, "nodes queue not empty: %i", (QUEUE_NODES_SIZE - uxQueueSpacesAvailable(queueNodes)));
|
||||
|
||||
if (xQueueReceive(queueNodes, &meshNodeAddr, ((100) / portTICK_PERIOD_MS)) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "Unable to receive OTA Messages from Queue");
|
||||
@ -188,7 +222,6 @@ void vMeshOtaTaskOTAWorker(void *arg)
|
||||
{
|
||||
vMeshOtaUtilClearNeighboursQueue(&meshNodeAddr); //remove this node from queue
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(bNewOTAImage == true)
|
||||
@ -197,17 +230,29 @@ void vMeshOtaTaskOTAWorker(void *arg)
|
||||
ESP_LOGI(LOG_TAG, "Updated successfully via Mesh, set pending reboot");
|
||||
bWantReboot = true;
|
||||
|
||||
vMeshOtaUtilAddAllNeighboursToQueue(); //add all existing neighbours to queue
|
||||
vMeshOtaUtilAddAllNeighboursToQueue(&meshNodeAddr); //add all existing neighbours to queue
|
||||
}
|
||||
vTaskDelay( (1000) / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage)
|
||||
/**
|
||||
* @fn esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage)
|
||||
* @brief Endpoint for OTA process that is called from remote node
|
||||
* @param cpbNewOTAImage pointer to boolean to signal if a new image was successfully received
|
||||
* @param cpcMeshNodeAddr pointer to mesh node that transmitted the update
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*
|
||||
* Answers the OTA_Version_Request with OTA_Version_Response
|
||||
* calls errMeshOtaPartitionAccessMeshReceive OR errMeshOtaPartitionAccessMeshTransmit based on version number
|
||||
*/
|
||||
esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage, mesh_addr_t* const cpcMeshNodeAddr)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
MESH_PACKET_t sOTAMessage;
|
||||
const esp_partition_t* pBootPartition; //pointer to boot partition (that will booted after reset)
|
||||
const esp_partition_t* cpBootPartition = NULL; //pointer to boot partition (that will booted after reset)
|
||||
esp_app_desc_t bootPartitionDesc; //Metadate from boot partition
|
||||
*cpbNewOTAImage = false; //set default false
|
||||
|
||||
@ -217,7 +262,6 @@ esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage)
|
||||
//queue not empty
|
||||
if (xQueueReceive(queueMessageOTA, &sOTAMessage, ((100) / portTICK_PERIOD_MS)) != pdTRUE)
|
||||
{
|
||||
ESP_LOGE(LOG_TAG, "Unable to receive OTA Messages from Queue");
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
|
||||
@ -225,16 +269,18 @@ esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage)
|
||||
{
|
||||
xSemaphoreTake(bsOTAProcess, portMAX_DELAY); //wait for binary semaphore that allows to start the OTA process
|
||||
|
||||
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
|
||||
memcpy(cpcMeshNodeAddr, &sOTAMessage.meshSenderAddr, sizeof(mesh_addr_t));
|
||||
|
||||
cpBootPartition = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
|
||||
ERROR_CHECK(esp_ota_get_partition_description(cpBootPartition, &bootPartitionDesc)); //get metadata of partition
|
||||
|
||||
//send OTA_Version_Response to sender of OTA_Version_Request packet wirh version in payload
|
||||
ERROR_CHECK(errMeshOtaUtilSendOTAVersionResponse(&sOTAMessage.meshSenderAddr));
|
||||
ERROR_CHECK(errMeshOtaUtilSendOtaVersionResponse(&sOTAMessage.meshSenderAddr));
|
||||
|
||||
if((bMeshOtaUtilNewerVersion((bootPartitionDesc).version, (char*) sOTAMessage.au8Payload)) && (err == ESP_OK)) //compare local and remote version
|
||||
{
|
||||
//remote newer as local
|
||||
ESP_LOGI(LOG_TAG, "remote image on node is newer --> OTA update required");
|
||||
ESP_LOGI(LOG_TAG, "remote image on node is newer --> OTA update required from node \"%x:%x:%x:%x:%x:%x\"", sOTAMessage.meshSenderAddr.addr[0], sOTAMessage.meshSenderAddr.addr[1], sOTAMessage.meshSenderAddr.addr[2], sOTAMessage.meshSenderAddr.addr[3], sOTAMessage.meshSenderAddr.addr[4], sOTAMessage.meshSenderAddr.addr[5]);
|
||||
// --> this version older --> start OTA_Rx --> set cpbNewOTAImage true
|
||||
ERROR_CHECK(errMeshOtaPartitionAccessMeshReceive(cpbNewOTAImage, &sOTAMessage.meshSenderAddr));
|
||||
}
|
||||
@ -242,7 +288,7 @@ esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage)
|
||||
if((bMeshOtaUtilNewerVersion((char*) sOTAMessage.au8Payload, (bootPartitionDesc).version)) && (err == ESP_OK)) //compare remote and local version
|
||||
{
|
||||
//local newer as remote
|
||||
ESP_LOGI(LOG_TAG, "remote image on node is older --> OTA send required");
|
||||
ESP_LOGI(LOG_TAG, "remote image on node is older --> OTA send required to node \"%x:%x:%x:%x:%x:%x\"", sOTAMessage.meshSenderAddr.addr[0], sOTAMessage.meshSenderAddr.addr[1], sOTAMessage.meshSenderAddr.addr[2], sOTAMessage.meshSenderAddr.addr[3], sOTAMessage.meshSenderAddr.addr[4], sOTAMessage.meshSenderAddr.addr[5]);
|
||||
// --> this version newer --> start OTA_Tx
|
||||
ERROR_CHECK(errMeshOtaPartitionAccessMeshTransmit(&sOTAMessage.meshSenderAddr));
|
||||
}
|
||||
@ -252,14 +298,27 @@ esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage)
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshOtaMasterEndpoint(bool* const cpbNewOTAImage, const mesh_addr_t* const cpcMeshNodeAddr)
|
||||
* @brief Endpoint for OTA process that calls remote node
|
||||
* @param cpbNewOTAImage pointer to boolean to signal if a new image was successfully received
|
||||
* @param cpcMeshNodeAddr pointer to remote node addr
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*
|
||||
* Sends the OTA_Version_Request to remote node
|
||||
* calls errMeshOtaPartitionAccessMeshReceive OR errMeshOtaPartitionAccessMeshTransmit based on version number received
|
||||
*/
|
||||
esp_err_t errMeshOtaMasterEndpoint(bool* const cpbNewOTAImage, const mesh_addr_t* const cpcMeshNodeAddr)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
MESH_PACKET_t sOTAMessage;
|
||||
const esp_partition_t* pBootPartition; //pointer to boot partition (that will booted after reset)
|
||||
const esp_partition_t* cpBootPartition = NULL; //pointer to boot partition (that will booted after reset)
|
||||
esp_app_desc_t bootPartitionDesc; //Metadata from boot partition
|
||||
bool bNodeIsConnected = false;
|
||||
bool bNodeIsResponding = false;
|
||||
bool bSameVersion = false;
|
||||
|
||||
*cpbNewOTAImage = false; //set default false
|
||||
|
||||
@ -268,52 +327,49 @@ esp_err_t errMeshOtaMasterEndpoint(bool* const cpbNewOTAImage, const mesh_addr_t
|
||||
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", cpcMeshNodeAddr->addr[5]);
|
||||
ERROR_CHECK(errMeshOtaUtilSendOTAVersionRequest(cpcMeshNodeAddr)); //send OTA_VERSION_REQUEST with local version in payload
|
||||
ERROR_CHECK(errMeshOtaUtilSendOtaVersionRequest(cpcMeshNodeAddr)); //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
|
||||
for (uint32_t u32Index = 0; ((u32Index < QUEUE_MESSAGE_OTA_SIZE) && (bSameVersion == false)); 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, ((3000) / 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) && (bMeshNetworkCheckMacEquality(sOTAMessage.meshSenderAddr.addr, cpcMeshNodeAddr->addr))) //if OTA_Version_Request
|
||||
{
|
||||
bSameVersion = true;
|
||||
bNodeIsResponding = true;
|
||||
cpBootPartition = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
|
||||
ERROR_CHECK(esp_ota_get_partition_description(cpBootPartition, &bootPartitionDesc)); //get metadata of partition
|
||||
|
||||
if((bMeshOtaUtilNewerVersion((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
|
||||
bSameVersion = false;
|
||||
ESP_LOGI(LOG_TAG, "remote image on node is newer --> OTA update required from node \"%x:%x:%x:%x:%x:%x\"", sOTAMessage.meshSenderAddr.addr[0], sOTAMessage.meshSenderAddr.addr[1], sOTAMessage.meshSenderAddr.addr[2], sOTAMessage.meshSenderAddr.addr[3], sOTAMessage.meshSenderAddr.addr[4], sOTAMessage.meshSenderAddr.addr[5]);
|
||||
// --> this version older --> start OTA_Rx --> set cpbNewOTAImage true
|
||||
ERROR_CHECK(errMeshOtaPartitionAccessMeshReceive(cpbNewOTAImage, &sOTAMessage.meshSenderAddr));
|
||||
u32Index = QUEUE_MESSAGE_OTA_SIZE;
|
||||
}
|
||||
|
||||
if((err == ESP_OK) && (sOTAMessage.type == OTA_Version_Response) && (bMeshNetworkCheckMACEquality(sOTAMessage.meshSenderAddr.addr, cpcMeshNodeAddr->addr))) //if OTA_Version_Request
|
||||
if((bMeshOtaUtilNewerVersion((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((bMeshOtaUtilNewerVersion((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 cpbNewOTAImage true
|
||||
ERROR_CHECK(errMeshOtaPartitionAccessMeshReceive(cpbNewOTAImage, &sOTAMessage.meshSenderAddr));
|
||||
}
|
||||
|
||||
if((bMeshOtaUtilNewerVersion((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(errMeshOtaPartitionAccessMeshTransmit(&sOTAMessage.meshSenderAddr));
|
||||
}
|
||||
}
|
||||
else if (err == ESP_OK)
|
||||
{
|
||||
//received from wrong node or type --> back to queue
|
||||
vMeshOtaUtilAddOtaMessageToQueue(&sOTAMessage);
|
||||
//local newer as remote
|
||||
bSameVersion = false;
|
||||
ESP_LOGI(LOG_TAG, "remote image on node is older --> OTA send required to node \"%x:%x:%x:%x:%x:%x\"", sOTAMessage.meshSenderAddr.addr[0], sOTAMessage.meshSenderAddr.addr[1], sOTAMessage.meshSenderAddr.addr[2], sOTAMessage.meshSenderAddr.addr[3], sOTAMessage.meshSenderAddr.addr[4], sOTAMessage.meshSenderAddr.addr[5]);
|
||||
// --> this version newer --> start OTA_Tx
|
||||
ERROR_CHECK(errMeshOtaPartitionAccessMeshTransmit(&sOTAMessage.meshSenderAddr));
|
||||
u32Index = QUEUE_MESSAGE_OTA_SIZE;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}//end loop
|
||||
|
@ -1,3 +1,10 @@
|
||||
/**
|
||||
* @file Mesh_OTA_Globals.c
|
||||
* @brief global variables unsed in Mesh_OTA
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
|
||||
#include "Mesh_OTA_Globals.h"
|
||||
|
||||
xQueueHandle queueNodes; //nodes that should be checked for ota update (contains children and parent)
|
||||
|
@ -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,15 +58,15 @@ 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
|
||||
ESP_LOGI(LOG_TAG, "start OTA download via HTTPS");
|
||||
do
|
||||
{
|
||||
vMeshOtaUtilPrintOTAProgress(&(pOTAPartition->size), &u32OTABytesWritten, Receiver);
|
||||
vMeshOtaUtilPrintOtaProgress(&(pOTAPartition->size), &u32OTABytesWritten, Receiver);
|
||||
ERROR_CHECK(esp_ota_write(otaHandle, (const void*) u8OTABuffer+u32StartOffset, (u32BytesRead-u32StartOffset)));
|
||||
|
||||
if(err == ESP_OK)
|
||||
@ -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)
|
||||
@ -65,12 +89,13 @@ esp_err_t errMeshOtaPartitionAccessHttps(bool* const cpbNewOTAImage)
|
||||
if(err == ESP_OK)
|
||||
{
|
||||
*cpbNewOTAImage = true; //image validated
|
||||
vMeshOtaUtilAddAllNeighboursToQueue(NULL); //add all existing neighbours to queue (aparent will not be added because this node is the root)
|
||||
}
|
||||
}
|
||||
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,28 +109,40 @@ 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);
|
||||
vMeshOtaUtilPrintOTAProgress(&(pBootPartition->size), &u32OTABytesWritten, Transmitter);
|
||||
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)
|
||||
{
|
||||
@ -118,7 +155,7 @@ esp_err_t errMeshOtaPartitionAccessMeshTransmit(const mesh_addr_t* const cpcMesh
|
||||
ESP_LOGI(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,57 +163,39 @@ 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);
|
||||
ESP_LOGI(LOG_TAG, "OTA-TX: no ACK or ABORT message received");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (err == ESP_OK)
|
||||
{
|
||||
//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 +207,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 +233,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
|
||||
|
||||
@ -214,8 +246,6 @@ esp_err_t errMeshOtaPartitionAccessMeshReceive(bool* const cpbNewOTAImage, const
|
||||
// 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)
|
||||
{
|
||||
@ -223,31 +253,29 @@ esp_err_t errMeshOtaPartitionAccessMeshReceive(bool* const cpbNewOTAImage, const
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
|
||||
if((err == ESP_OK) && (bMeshNetworkCheckMACEquality(sMeshPacket.meshSenderAddr.addr, cpcMeshNodeAddr->addr))) //if OTA_Version_Request
|
||||
if((err == ESP_OK) && (bMeshNetworkCheckMacEquality(sMeshPacket.meshSenderAddr.addr, cpcMeshNodeAddr->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);
|
||||
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
|
||||
ESP_LOGE(LOG_TAG, "OTA-RX: receives abort");
|
||||
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 +284,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 +302,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 +322,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)
|
||||
{
|
||||
|
@ -1,17 +1,32 @@
|
||||
/**
|
||||
* @file Mesh_OTA_Util.c
|
||||
* @brief Utility and helper functions to perfrom mesh OTA updates
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
|
||||
#include "Mesh_OTA_Util.h"
|
||||
#include "Mesh_OTA_Globals.h"
|
||||
|
||||
static const char *LOG_TAG = "mesh_ota";
|
||||
|
||||
/**
|
||||
* @fn bool bMeshOtaUtilNewerVersion(const char* cpu8Local, const char* cpu8Remote)
|
||||
* @brief compares to version strings
|
||||
* @param cpu8Local local image version string
|
||||
* @param cpu8Remote remote image version string
|
||||
* @return bool
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*
|
||||
* Returns true if remote is newer
|
||||
*/
|
||||
bool bMeshOtaUtilNewerVersion(const char* cpu8Local, const char* cpu8Remote)
|
||||
{
|
||||
/*
|
||||
* Return true if remote version is newer (higher) than local version
|
||||
*/
|
||||
char u8LocalTmp[12]; //local version
|
||||
char u8RemoteTmp[12]; //remote version
|
||||
char* pu8saveptrLocal; //context for strok_r
|
||||
char* pu8saveptrRemote; //context for strok_r
|
||||
char* pu8saveptrLocal = NULL; //context for strok_r
|
||||
char* pu8saveptrRemote = NULL; //context for strok_r
|
||||
bool bReturn = false; //flag to stop loop
|
||||
uint8_t u8Index = 0; //numbers counter in version string
|
||||
|
||||
@ -34,6 +49,18 @@ bool bMeshOtaUtilNewerVersion(const char* cpu8Local, const char* cpu8Remote)
|
||||
return bReturn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshOtaUtilExtractVersionNumber(const char* cpu8Data, uint32_t* const cpcu32DataLenght, char* const pc8RemoteVersionNumber)
|
||||
* @brief extract version number from image data
|
||||
* @param cpu8Data image data buffer
|
||||
* @param cpcu32DataLenght pointer to lenght of image data
|
||||
* @param pc8RemoteVersionNumber pointer version number
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutters
|
||||
* @date 21.01.2021
|
||||
*
|
||||
* Search version number in raw image data
|
||||
*/
|
||||
esp_err_t errMeshOtaUtilExtractVersionNumber(const char* cpu8Data, uint32_t* const cpcu32DataLenght, char* const pc8RemoteVersionNumber)
|
||||
{
|
||||
uint32_t u32StartOffset;
|
||||
@ -51,13 +78,24 @@ esp_err_t errMeshOtaUtilExtractVersionNumber(const char* cpu8Data, uint32_t* con
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshOtaUtilFindImageStart(const char* const cpu8Data, const uint32_t* const cpcu32DataLenght, uint32_t* const cpu32StartOffset)
|
||||
* @brief find start offset from image raw data
|
||||
* @param cpu8Data image data buffer
|
||||
* @param cpcu32DataLenght pointer to lenght of image data
|
||||
* @param cpu32StartOffset pointer to determined offset
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*
|
||||
* Search offset in raw image data from server (exclude HTTP response)
|
||||
*/
|
||||
esp_err_t errMeshOtaUtilFindImageStart(const char* const cpu8Data, const uint32_t* const cpcu32DataLenght, uint32_t* const cpu32StartOffset)
|
||||
{
|
||||
/*
|
||||
Offset value
|
||||
0 = 0xE9 (first byte in image --> magic byte)
|
||||
48 = first digit of version number
|
||||
*/
|
||||
|
||||
// Offset value
|
||||
// 0 = 0xE9 (first byte in image --> magic byte)
|
||||
// 48 = first digit of version number
|
||||
|
||||
esp_err_t errReturn = ESP_OK;
|
||||
bool bImageStartOffsetFound = false;
|
||||
@ -129,13 +167,21 @@ esp_err_t errMeshOtaUtilFindImageStart(const char* const cpu8Data, const uint32_
|
||||
return errReturn;
|
||||
}
|
||||
|
||||
esp_err_t errMeshOtaUtilSendOTAVersionRequest(const mesh_addr_t* const cpcMeshReceiverAddr)
|
||||
/**
|
||||
* @fn esp_err_t errMeshOtaUtilSendOtaVersionRequest(const mesh_addr_t* const cpcMeshReceiverAddr)
|
||||
* @brief send OTA_Version_Request to node
|
||||
* @param cpcMeshReceiverAddr node addr
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
esp_err_t errMeshOtaUtilSendOtaVersionRequest(const mesh_addr_t* const cpcMeshReceiverAddr)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
MESH_PACKET_t packet;
|
||||
packet.type = OTA_Version_Request;
|
||||
|
||||
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)
|
||||
esp_app_desc_t bootPartitionDesc; //Metadata from boot partition
|
||||
|
||||
pBootPartition = esp_ota_get_boot_partition(); //get boot partition (that will booted after reset), not the running partition
|
||||
@ -145,26 +191,44 @@ esp_err_t errMeshOtaUtilSendOTAVersionRequest(const mesh_addr_t* const cpcMeshRe
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t errMeshOtaUtilSendOTAVersionResponse(const mesh_addr_t* const cpcMeshReceiverAddr)
|
||||
/**
|
||||
* @fn esp_err_t errMeshOtaUtilSendOtaVersionResponse(const mesh_addr_t* const cpcMeshReceiverAddr)
|
||||
* @brief send OTA_Version_Response to node
|
||||
* @param cpcMeshReceiverAddr node addr
|
||||
* @return ESP32 error code
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
esp_err_t errMeshOtaUtilSendOtaVersionResponse(const mesh_addr_t* const cpcMeshReceiverAddr)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
MESH_PACKET_t packet;
|
||||
packet.type = OTA_Version_Response;
|
||||
|
||||
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)
|
||||
esp_app_desc_t bootPartitionDesc; //Metadata from boot partition
|
||||
|
||||
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", cpcMeshReceiverAddr->addr[5]);
|
||||
ESP_LOGD(LOG_TAG, "Send OTA_Version_Response to 0x%x", cpcMeshReceiverAddr->addr[5]);
|
||||
|
||||
err = errMeshNetworkSendMeshPacket(cpcMeshReceiverAddr, &packet);
|
||||
return err;
|
||||
}
|
||||
|
||||
void vMeshOtaUtilPrintOTAProgress(const uint32_t* const cpcu32TotalImageSize, const uint32_t* const cpcu32BytesWritten, const OTA_MESH_ROLE_t ceRole)
|
||||
/**
|
||||
* @fn void vMeshOtaUtilPrintOtaProgress(const uint32_t* const cpcu32TotalImageSize, const uint32_t* const cpcu32BytesWritten, const OTA_MESH_ROLE_t ceRole)
|
||||
* @brief print LOG for OTA process progress
|
||||
* @param cpcu32TotalImageSize size of OTA partition
|
||||
* @param cpcu32BytesWritten actual bytes written
|
||||
* @param ceRole role if this OTA process
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaUtilPrintOtaProgress(const uint32_t* const cpcu32TotalImageSize, const uint32_t* const cpcu32BytesWritten, const OTA_MESH_ROLE_t ceRole)
|
||||
{
|
||||
uint32_t u32Percentage = 0U;
|
||||
static uint32_t u32LastPercentage = 0U;
|
||||
@ -196,10 +260,17 @@ void vMeshOtaUtilPrintOTAProgress(const uint32_t* const cpcu32TotalImageSize, co
|
||||
}
|
||||
}
|
||||
|
||||
void vMeshOtaUtilAddAllNeighboursToQueue(void)
|
||||
/**
|
||||
* @fn void vMeshOtaUtilAddAllNeighboursToQueue(void)
|
||||
* @brief add all neigbhours (children and parent) to queue, except node used in parameter
|
||||
* @param const mesh_addr_t* const cpcMeshNodeAddr
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaUtilAddAllNeighboursToQueue(const mesh_addr_t* const cpcMeshNodeAddr)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
|
||||
mesh_addr_t addrParent; //addr of parent node
|
||||
mesh_addr_t childrenAddr[CONFIG_MESH_ROUTE_TABLE_SIZE]; //array of children attached to this node
|
||||
uint16_t u16ChildrenSize = 0U; //number of children attached to this node
|
||||
@ -208,8 +279,22 @@ void vMeshOtaUtilAddAllNeighboursToQueue(void)
|
||||
|
||||
if(err == ESP_OK)
|
||||
{
|
||||
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(addrParent.addr);
|
||||
ESP_LOGI(LOG_TAG, "added parent");
|
||||
if (cpcMeshNodeAddr != NULL)
|
||||
{
|
||||
if((bMeshNetworkCheckMacEquality(addrParent.addr, cpcMeshNodeAddr->addr)))
|
||||
{
|
||||
//same node --> don't add
|
||||
}
|
||||
else
|
||||
{
|
||||
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(addrParent.addr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(addrParent.addr);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
err = ESP_OK; //reset error code
|
||||
@ -218,11 +303,32 @@ void vMeshOtaUtilAddAllNeighboursToQueue(void)
|
||||
|
||||
for (uint16_t u16Index = 0; ((u16Index < u16ChildrenSize) && (err == ESP_OK)); u16Index++)
|
||||
{
|
||||
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(childrenAddr[u16Index].addr);
|
||||
ESP_LOGI(LOG_TAG, "added child");
|
||||
if (cpcMeshNodeAddr != NULL)
|
||||
{
|
||||
if((bMeshNetworkCheckMacEquality(childrenAddr[u16Index].addr, cpcMeshNodeAddr->addr)))
|
||||
{
|
||||
//same node --> don't add
|
||||
}
|
||||
else
|
||||
{
|
||||
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(childrenAddr[u16Index].addr);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(childrenAddr[u16Index].addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn void vMeshOtaUtilClearOtaMessageQueue(const mesh_addr_t* const cpcMeshNodeAddr)
|
||||
* @brief remode all OTA messages from this node in queue
|
||||
* @param cpcMeshNodeAddr node addr
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaUtilClearOtaMessageQueue(const mesh_addr_t* const cpcMeshNodeAddr)
|
||||
{
|
||||
MESH_PACKET_t sMeshPacket; //packet for sending and receiving
|
||||
@ -230,40 +336,52 @@ void vMeshOtaUtilClearOtaMessageQueue(const mesh_addr_t* const cpcMeshNodeAddr)
|
||||
{
|
||||
if (xQueueReceive(queueMessageOTA, &sMeshPacket, 0) == pdTRUE)
|
||||
{
|
||||
if(!(bMeshNetworkCheckMACEquality(sMeshPacket.meshSenderAddr.addr, cpcMeshNodeAddr->addr)))
|
||||
if(!(bMeshNetworkCheckMacEquality(sMeshPacket.meshSenderAddr.addr, cpcMeshNodeAddr->addr)))
|
||||
{
|
||||
//received OTA message is NOT from cpcMeshNodeAddr --> keep it in queue
|
||||
vMeshOtaUtilAddOtaMessageToQueue(&sMeshPacket);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "Removed type %i from node 0x%x", sMeshPacket.type, cpcMeshNodeAddr->addr[5]);
|
||||
}
|
||||
}
|
||||
}//end OTA message loop
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn void vMeshOtaUtilClearNeighboursQueue(const mesh_addr_t* const cpcMeshNodeAddr)
|
||||
* @brief remode all instances of this node in queue
|
||||
* @param cpcMeshNodeAddr node addr
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaUtilClearNeighboursQueue(const mesh_addr_t* const cpcMeshNodeAddr)
|
||||
{
|
||||
mesh_addr_t sNode; //packet for sending and receiving
|
||||
for (uint32_t u32Index = 0; (u32Index < QUEUE_MESSAGE_OTA_SIZE); u32Index++) //loop through all OTA messages
|
||||
for (uint32_t u32Index = 0; (u32Index < QUEUE_NODES_SIZE); u32Index++) //loop through all node queue
|
||||
{
|
||||
if (xQueueReceive(queueNodes, &sNode, 0) == pdTRUE)
|
||||
{
|
||||
if(!(bMeshNetworkCheckMACEquality(sNode.addr, cpcMeshNodeAddr->addr)))
|
||||
if((bMeshNetworkCheckMacEquality(sNode.addr, cpcMeshNodeAddr->addr)))
|
||||
{
|
||||
//node is NOT cpcMeshNodeAddr --> keep it in queue
|
||||
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(cpcMeshNodeAddr->addr);
|
||||
//same node --> don't add again
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "Removed node 0x%x", cpcMeshNodeAddr->addr[5]);
|
||||
//node is NOT cpcMeshNodeAddr --> keep it in queue
|
||||
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(sNode.addr);
|
||||
}
|
||||
|
||||
}
|
||||
}//end OTA message loop
|
||||
}//end nodes
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @fn void vMeshOtaUtilAddNodeToPossibleUpdatableQueue(const uint8_t* const cpcu8MAC)
|
||||
* @brief add node instance to queue
|
||||
* @param cpcu8MAC MAC addr of node
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaUtilAddNodeToPossibleUpdatableQueue(const uint8_t* const cpcu8MAC)
|
||||
{
|
||||
//send payload to node queues
|
||||
@ -276,10 +394,18 @@ void vMeshOtaUtilAddNodeToPossibleUpdatableQueue(const uint8_t* const cpcu8MAC)
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "added node \"%x:%x:%x:%x:%x:%x\" to possible updatable queue", addrNode.addr[0], addrNode.addr[1], addrNode.addr[2], addrNode.addr[3], addrNode.addr[4], addrNode.addr[5]);
|
||||
ESP_LOGD(LOG_TAG, "added node \"%x:%x:%x:%x:%x:%x\" to possible updatable queue", addrNode.addr[0], addrNode.addr[1], addrNode.addr[2], addrNode.addr[3], addrNode.addr[4], addrNode.addr[5]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @fn void vMeshOtaUtilAddOtaMessageToQueue(const MESH_PACKET_t* const cpcuMeshPacket)
|
||||
* @brief add OTA message to queue
|
||||
* @param cpcuMeshPacket OTA message
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaUtilAddOtaMessageToQueue(const MESH_PACKET_t* const cpcuMeshPacket)
|
||||
{
|
||||
//send ota packet to packet queue
|
||||
@ -292,14 +418,14 @@ void vMeshOtaUtilAddOtaMessageToQueue(const MESH_PACKET_t* const cpcuMeshPacket)
|
||||
switch (cpcuMeshPacket->type)
|
||||
{
|
||||
case OTA_Abort:
|
||||
ESP_LOGI(LOG_TAG, "added ota message to queue: OTA_Abort from 0x%x", cpcuMeshPacket->meshSenderAddr.addr[5]);
|
||||
ESP_LOGD(LOG_TAG, "added ota message to queue: OTA_Abort from 0x%x", cpcuMeshPacket->meshSenderAddr.addr[5]);
|
||||
break;
|
||||
case OTA_Version_Request:
|
||||
ESP_LOGI(LOG_TAG, "added ota message to queue: OTA_Version_Request from 0x%x", cpcuMeshPacket->meshSenderAddr.addr[5]);
|
||||
ESP_LOGD(LOG_TAG, "added ota message to queue: OTA_Version_Request from 0x%x", cpcuMeshPacket->meshSenderAddr.addr[5]);
|
||||
break;
|
||||
|
||||
case OTA_Version_Response:
|
||||
ESP_LOGI(LOG_TAG, "added ota message to queue: OTA_Version Response from 0x%x", cpcuMeshPacket->meshSenderAddr.addr[5]);
|
||||
ESP_LOGD(LOG_TAG, "added ota message to queue: OTA_Version Response from 0x%x", cpcuMeshPacket->meshSenderAddr.addr[5]);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -307,14 +433,20 @@ void vMeshOtaUtilAddOtaMessageToQueue(const MESH_PACKET_t* const cpcuMeshPacket)
|
||||
}
|
||||
}
|
||||
|
||||
void vMeshOtaUtilChangeStateOfServerWorker(const bool cbState) //allow access via function ptn to network_handler
|
||||
/**
|
||||
* @fn void vMeshOtaUtilChangeStateOfServerWorker(const bool cbState)
|
||||
* @brief callback for mesh network if connectivity to server changed
|
||||
* @param cbState boolean state
|
||||
* @return void
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
void vMeshOtaUtilChangeStateOfServerWorker(const bool cbState)
|
||||
{
|
||||
static bool bLastState = false;
|
||||
|
||||
if(cbState != bLastState) //change only if necessary
|
||||
{
|
||||
ESP_LOGI(LOG_TAG, "server worker change handler");
|
||||
|
||||
if(cbState == true)
|
||||
{
|
||||
if (xSemaphoreGive(bsStartStopServerWorker) != pdTRUE)
|
||||
|
@ -1,3 +1,12 @@
|
||||
/**
|
||||
* @file Mesh_Network.h
|
||||
* @brief Mesh network layer used by OTA and APP
|
||||
* @author Hendrik Schutter, init based in ESP32-IDE code
|
||||
* @date 20.01.2021
|
||||
*
|
||||
* Additional Infos: Start network and send and receive data.
|
||||
*/
|
||||
|
||||
#ifndef H_MESH_NETWORK
|
||||
#define H_MESH_NETWORK
|
||||
|
||||
@ -95,7 +104,7 @@ esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOf
|
||||
|
||||
bool bMeshNetworkIsRootNode(void);
|
||||
bool bMeshNetworkIsNodeNeighbour(const mesh_addr_t* const cpcNode);
|
||||
bool bMeshNetworkCheckMACEquality(const uint8_t* const cpcu8aMAC, const uint8_t* const cpcu8bMAC);
|
||||
bool bMeshNetworkCheckMacEquality(const uint8_t* const cpcu8aMAC, const uint8_t* const cpcu8bMAC);
|
||||
|
||||
esp_err_t errMeshNetworkStartReceiveTask(void);
|
||||
esp_err_t errMeshNetworkGetParentNode(mesh_addr_t* const cpMeshParentAddr);
|
||||
|
@ -1,3 +1,10 @@
|
||||
/**
|
||||
* @file Mesh_OTA.h
|
||||
* @brief Start and implement OTA updates via HTTPS from server and other mesh nodes (bidirectional)
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
|
||||
#ifndef H_MESH_OTA
|
||||
#define H_MESH_OTA
|
||||
|
||||
@ -11,14 +18,14 @@
|
||||
#include "HTTPS_Client.h"
|
||||
|
||||
#define ERASE_NVS //erase non volatile storage if full
|
||||
#define QUEUE_NODES_SIZE 10
|
||||
#define QUEUE_MESSAGE_OTA_SIZE 10
|
||||
#define QUEUE_NODES_SIZE 20
|
||||
#define QUEUE_MESSAGE_OTA_SIZE 20
|
||||
#define SERVER_CHECK_INTERVAL 30 //in seconds
|
||||
#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_ALLOW_REBOOT 0
|
||||
#define OTA_ALLOW_REBOOT 1
|
||||
|
||||
#define ERROR_CHECK(x) if (err == ESP_OK) \
|
||||
{ \
|
||||
@ -36,7 +43,7 @@ void vMeshOtaTaskServerWorker(void *arg);
|
||||
void vMeshOtaTaskOTAWorker(void *arg);
|
||||
|
||||
//OTA process endpoints
|
||||
esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage);
|
||||
esp_err_t errMeshOtaSlaveEndpoint(bool* const cpbNewOTAImage, mesh_addr_t* const cpcMeshNodeAddr);
|
||||
esp_err_t errMeshOtaMasterEndpoint(bool* const cpbNewOTAImage, const mesh_addr_t* const cpcMeshNodeAddr);
|
||||
|
||||
#endif /* H_MESH_OTA */
|
||||
|
@ -1,3 +1,11 @@
|
||||
/**
|
||||
* @file Mesh_OTA_Globals.h
|
||||
* @brief global variables unsed in Mesh_OTA
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef H_MESH_OTA_GLOBALS
|
||||
#define H_MESH_OTA_GLOBALS
|
||||
|
||||
|
@ -1,3 +1,13 @@
|
||||
/**
|
||||
* @file Mesh_OTA_Partition_Access.h
|
||||
* @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
|
||||
*/
|
||||
|
||||
#ifndef H_MESH_OTA_PARTITION_ACCESS
|
||||
#define H_MESH_OTA_PARTITION_ACCESS
|
||||
|
||||
|
@ -1,3 +1,10 @@
|
||||
/**
|
||||
* @file Mesh_OTA_Util.h
|
||||
* @brief Utility and helper functions to perfrom mesh OTA updates
|
||||
* @author Hendrik Schutter
|
||||
* @date 21.01.2021
|
||||
*/
|
||||
|
||||
#ifndef H_MESH_OTA_UTIL
|
||||
#define H_MESH_OTA_UTIL
|
||||
|
||||
@ -21,10 +28,10 @@ typedef enum otaMeshRole OTA_MESH_ROLE_t;
|
||||
bool bMeshOtaUtilNewerVersion(const char* cpu8Local, const char* cpu8Remote);
|
||||
esp_err_t errMeshOtaUtilExtractVersionNumber(const char* cpu8Data, uint32_t* const cpu32DataLenght, char* const pc8RemoteVersionNumber);
|
||||
esp_err_t errMeshOtaUtilFindImageStart(const char* const cpu8Data, const uint32_t* const cpu32DataLenght, uint32_t* const cpu32StartOffset);
|
||||
esp_err_t errMeshOtaUtilSendOTAVersionRequest(const mesh_addr_t* const cpcMeshReceiverAddr);
|
||||
esp_err_t errMeshOtaUtilSendOTAVersionResponse(const mesh_addr_t* const cpcMeshReceiverAddr);
|
||||
void vMeshOtaUtilPrintOTAProgress(const uint32_t* const cpcu32TotalImageSize, const uint32_t* const cpcu32BytesWritten, const OTA_MESH_ROLE_t ceRole);
|
||||
void vMeshOtaUtilAddAllNeighboursToQueue(void);
|
||||
esp_err_t errMeshOtaUtilSendOtaVersionRequest(const mesh_addr_t* const cpcMeshReceiverAddr);
|
||||
esp_err_t errMeshOtaUtilSendOtaVersionResponse(const mesh_addr_t* const cpcMeshReceiverAddr);
|
||||
void vMeshOtaUtilPrintOtaProgress(const uint32_t* const cpcu32TotalImageSize, const uint32_t* const cpcu32BytesWritten, const OTA_MESH_ROLE_t ceRole);
|
||||
void vMeshOtaUtilAddAllNeighboursToQueue(const mesh_addr_t* const cpcMeshNodeAddr);
|
||||
void vMeshOtaUtilClearOtaMessageQueue(const mesh_addr_t* const cpcMeshNodeAddr);
|
||||
void vMeshOtaUtilClearNeighboursQueue(const mesh_addr_t* const cpcMeshNodeAddr);
|
||||
|
||||
|
@ -1,8 +1,9 @@
|
||||
#include <limits.h>
|
||||
#include "unity.h"
|
||||
|
||||
#include "Mesh_OTA.h"
|
||||
#include "Mesh_OTA_Util.h"
|
||||
#include "test_image_hex.h"
|
||||
#include "Mesh_Network.h"
|
||||
|
||||
// ### ### ### distinguish newer image version ### ### ###
|
||||
|
||||
@ -206,11 +207,69 @@ TEST_CASE("extract version 999.99.999", "[extract image version number]")
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
TEST_CASE("same MACs", "[check MAC equality]")
|
||||
{
|
||||
unsigned char cMAC_A[] = "012345678901";
|
||||
unsigned char cMAC_B[] = "012345678901";
|
||||
|
||||
TEST_ASSERT_TRUE( bMeshNetworkCheckMacEquality(cMAC_A,cMAC_B) );
|
||||
}
|
||||
|
||||
TEST_CASE("different MACs", "[check MAC equality]")
|
||||
{
|
||||
unsigned char cMAC_A[] = "012345678901";
|
||||
unsigned char cMAC_B[] = "087464874718";
|
||||
|
||||
TEST_ASSERT_FALSE( bMeshNetworkCheckMacEquality(cMAC_A,cMAC_B) );
|
||||
}
|
||||
|
||||
TEST_CASE("same MACs last A_byte+1", "[check MAC equality]")
|
||||
{
|
||||
unsigned char cMAC_A[] = "012345678902";
|
||||
unsigned char cMAC_B[] = "012345678901";
|
||||
|
||||
TEST_ASSERT_TRUE( bMeshNetworkCheckMacEquality(cMAC_A,cMAC_B) );
|
||||
}
|
||||
|
||||
TEST_CASE("same MACs last B_byte+1", "[check MAC equality]")
|
||||
{
|
||||
unsigned char cMAC_A[] = "012345678901";
|
||||
unsigned char cMAC_B[] = "012345678902";
|
||||
|
||||
TEST_ASSERT_TRUE( bMeshNetworkCheckMacEquality(cMAC_A,cMAC_B) );
|
||||
}
|
||||
|
||||
TEST_CASE("same MACs last B_byte+2", "[check MAC equality]")
|
||||
{
|
||||
unsigned char cMAC_A[] = "012345678901";
|
||||
unsigned char cMAC_B[] = "012345678903";
|
||||
|
||||
TEST_ASSERT_TRUE( bMeshNetworkCheckMacEquality(cMAC_A,cMAC_B) );
|
||||
}
|
||||
|
||||
TEST_CASE("same Node0", "[check MAC equality]")
|
||||
{
|
||||
unsigned char cMAC_A[] = "ac67b27162b0";
|
||||
unsigned char cMAC_B[] = "ac67b27162b0";
|
||||
|
||||
TEST_ASSERT_TRUE( bMeshNetworkCheckMacEquality(cMAC_A,cMAC_B) );
|
||||
}
|
||||
|
||||
TEST_CASE("same Node0 first MAC+1", "[check MAC equality]")
|
||||
{
|
||||
unsigned char cMAC_A[] = "ac67b27162b1";
|
||||
unsigned char cMAC_B[] = "ac67b27162b0";
|
||||
|
||||
TEST_ASSERT_TRUE( bMeshNetworkCheckMacEquality(cMAC_A,cMAC_B) );
|
||||
}
|
||||
|
||||
TEST_CASE("same Node0 second MAC+1", "[check MAC equality]")
|
||||
{
|
||||
unsigned char cMAC_A[] = "ac67b27162b0";
|
||||
unsigned char cMAC_B[] = "ac67b27162b1";
|
||||
|
||||
TEST_ASSERT_TRUE( bMeshNetworkCheckMacEquality(cMAC_A,cMAC_B) );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -61,6 +61,15 @@ esp_err_t errBlinkyLEDInitialize(void)
|
||||
err = ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef NEW_VERSION
|
||||
gpio_set_level(GPIO_LED_GREEN, 1); //switch on
|
||||
#endif
|
||||
|
||||
#ifndef NEW_VERSION
|
||||
gpio_set_level(GPIO_LED_GREEN, 0); //switch off
|
||||
#endif
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
@ -79,8 +88,10 @@ void vGPIOInitialize(void)
|
||||
gpio_config_t gpioConf;
|
||||
|
||||
//LED as Output
|
||||
gpio_reset_pin(GPIO_LED);
|
||||
gpio_set_direction(GPIO_LED, GPIO_MODE_OUTPUT);
|
||||
gpio_reset_pin(GPIO_LED_BLUE);
|
||||
gpio_set_direction(GPIO_LED_BLUE, GPIO_MODE_OUTPUT);
|
||||
gpio_reset_pin(GPIO_LED_GREEN);
|
||||
gpio_set_direction(GPIO_LED_GREEN, GPIO_MODE_OUTPUT);
|
||||
|
||||
//BTN as Input
|
||||
gpioConf.intr_type = GPIO_INTR_DISABLE;
|
||||
@ -212,19 +223,19 @@ void vTaskReceiveData(void *arg)
|
||||
{
|
||||
case LED_ON:
|
||||
bLEDisOn = true;
|
||||
gpio_set_level(GPIO_LED, 1); //switch on
|
||||
gpio_set_level(GPIO_LED_BLUE, 1); //switch on
|
||||
ESP_LOGI(LOG_TAG,"switch LED ON");
|
||||
break;
|
||||
|
||||
case LED_OFF:
|
||||
bLEDisOn = false;
|
||||
gpio_set_level(GPIO_LED, 0); //switch off
|
||||
gpio_set_level(GPIO_LED_BLUE, 0); //switch off
|
||||
ESP_LOGI(LOG_TAG,"switch LED OFF");
|
||||
break;
|
||||
|
||||
default:
|
||||
bLEDisOn = false;
|
||||
gpio_set_level(GPIO_LED, 0); //switch off
|
||||
gpio_set_level(GPIO_LED_BLUE, 0); //switch off
|
||||
ESP_LOGI(LOG_TAG,"switch LED OFF");
|
||||
break;
|
||||
}
|
||||
@ -236,7 +247,7 @@ void vTaskReceiveData(void *arg)
|
||||
for (uint16_t u16Index = 0; u16Index < u16ChildrenSize; u16Index++)
|
||||
{
|
||||
//loop through children
|
||||
if(bMeshNetworkCheckMACEquality(bTmpPacket.meshSenderAddr.addr, childrenAddr[u16Index].addr) == false) //exclude the sender node
|
||||
if(bMeshNetworkCheckMacEquality(bTmpPacket.meshSenderAddr.addr, childrenAddr[u16Index].addr) == false) //exclude the sender node
|
||||
{
|
||||
ERROR_CHECK (errMeshNetworkSendMeshPacket(&childrenAddr[u16Index], &meshPacket)); //send to child
|
||||
}
|
||||
|
@ -20,9 +20,11 @@
|
||||
|
||||
#include "Mesh_OTA.h"
|
||||
|
||||
#define NEW_VERSION
|
||||
|
||||
#define GPIO_BOOT_BTN 0 //GPIO0 (Boot BTN)
|
||||
#define GPIO_LED 2 //GPIO2 (internal blue LED in DevKit V1.0)
|
||||
#define GPIO_LED_BLUE 2 //GPIO2 (internal blue LED in DevKit V1.0)
|
||||
#define GPIO_LED_GREEN 13 //GPIO13
|
||||
|
||||
#define GPIO_INPUT_PIN_SEL (1ULL<<GPIO_BOOT_BTN)
|
||||
|
||||
|
@ -13,7 +13,6 @@ static const char *LOG_TAG = "esp_main";
|
||||
void app_main(void)
|
||||
{
|
||||
esp_err_t err = ESP_OK;
|
||||
ESP_LOGI(LOG_TAG, "hardcoded version: 0.0.1");
|
||||
|
||||
ESP_LOGI(LOG_TAG, "start mesh network");
|
||||
err = errMeshNetworkInitialize();
|
||||
|
@ -32,7 +32,7 @@ CONFIG_APP_COMPILE_TIME_DATE=y
|
||||
# CONFIG_APP_EXCLUDE_PROJECT_VER_VAR is not set
|
||||
# CONFIG_APP_EXCLUDE_PROJECT_NAME_VAR is not set
|
||||
CONFIG_APP_PROJECT_VER_FROM_CONFIG=y
|
||||
CONFIG_APP_PROJECT_VER="0.0.1"
|
||||
CONFIG_APP_PROJECT_VER="0.1.3"
|
||||
CONFIG_APP_RETRIEVE_LEN_ELF_SHA=16
|
||||
# end of Application manager
|
||||
|
||||
|
Reference in New Issue
Block a user