Browse Source

renamed functions in modules

pull/2/head
Hendrik Schutter 1 year ago
parent
commit
7f0e82fd29
  1. 1
      components/mesh_ota/HTTPS_Client.c
  2. 227
      components/mesh_ota/Mesh_Network.c
  3. 6
      components/mesh_ota/Mesh_Network_Handler.c
  4. 61
      components/mesh_ota/Mesh_OTA.c
  5. 5
      components/mesh_ota/Mesh_OTA_Globals.c
  6. 193
      components/mesh_ota/Mesh_OTA_Partition_Access.c
  7. 239
      components/mesh_ota/Mesh_OTA_Util.c
  8. 32
      components/mesh_ota/include/Mesh_Network.h
  9. 15
      components/mesh_ota/include/Mesh_OTA.h
  10. 13
      components/mesh_ota/include/Mesh_OTA_Partition_Access.h
  11. 22
      components/mesh_ota/include/Mesh_OTA_Util.h
  12. 40
      components/mesh_ota/test/test_mesh_ota.c
  13. 16
      main/Blinky_LED.c

1
components/mesh_ota/HTTPS_Client.c

@ -2,6 +2,7 @@
static const char *TAG = "https_client";
//HTTP GET data
static const char *REQUEST = "GET " CONFIG_OTA_HTTPS_URL " HTTP/1.1\r\n"
"Host: "CONFIG_OTA_HTTPS_SERVER_COMMON_NAME"\r\n"
"User-Agent: esp-idf/1.0 esp32\r\n"

227
components/mesh_ota/Mesh_Network.c

@ -2,13 +2,29 @@
#include "Mesh_Network.h"
static const char *LOG_TAG = "mesh_network";
static uint8_t tx_buf[CONFIG_MESH_MESSAGE_SIZE] = { 0, };
static uint8_t rx_buf[CONFIG_MESH_MESSAGE_SIZE] = { 0, };
//w: errMeshNetworkInitialize
//r: errMeshNetworkInitialize;vMeshNetworkGetOwnAddr;errMeshNetworkGetChildren
uint8_t u8ownMAC[6];
//w: errMeshNetworkInitialize; vMeshNetworkMeshEventHandler
//r: vMeshNetworkMeshEventHandler
esp_netif_t* netif_sta;
//w: errMeshNetworkInitialize; vMeshNetworkMeshEventHandler
//r: errMeshNetworkInitialize;
bool bIsMeshConnected;
//w: errMeshNetworkInitialize; vMeshNetworkMeshEventHandler
//r: vMeshNetworkMeshEventHandler
int32_t i32MeshLayer;
//w: errMeshNetworkInitialize; vMeshNetworkMeshEventHandler
//r: vMeshNetworkMeshEventHandler
mesh_addr_t meshParentAddr;
//function pointer for callbacks
void (*pAppRxHandle)(uint8_t*, uint8_t* );
void (*pOTAChildConnectHandle)(uint8_t* );
void (*pOTAMessageHandle)(MESH_PACKET_t* );
@ -47,7 +63,7 @@ esp_err_t errMeshNetworkInitialize()
ERROR_CHECK(esp_mesh_init());
//mesh initialization
ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &vMeshEventHandler, NULL));
ERROR_CHECK(esp_event_handler_register(MESH_EVENT, ESP_EVENT_ANY_ID, &vMeshNetworkMeshEventHandler, NULL));
//set mesh topology
ERROR_CHECK(esp_mesh_set_topology(CONFIG_MESH_TOPOLOGY));
@ -97,7 +113,7 @@ esp_err_t errMeshNetworkInitializeWiFi()
esp_err_t err = ESP_OK;
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
ERROR_CHECK(esp_wifi_init(&config));
ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &vIPEventHandler, NULL));
ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &vMeshNetworkIpEventHandler, NULL));
ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH));
ERROR_CHECK(esp_wifi_start());
return err;
@ -115,84 +131,52 @@ esp_err_t errMeshNetworkInitializeRouter(mesh_cfg_t* cfg)
return err;
}
//returns true if MAC address is equal
bool bCheckMACEquality(uint8_t* pu8aMAC, uint8_t* pu8bMAC)
esp_err_t errMeshNetworkSetChildConnectedHandle(void (*pChildConnectHandleTmp)(uint8_t * pu8Data))
{
bool bRet = true;
uint8_t index = 0;
while ((index < 6) && (bRet == true))
{
if(pu8aMAC[index] != pu8bMAC[index])
{
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;
pOTAChildConnectHandle = pChildConnectHandleTmp;
return ESP_OK;
}
esp_err_t errGetChildren(mesh_addr_t* pChildren, uint16_t* pu16ChildrenSize)
esp_err_t errMeshNetworkSetAppReceiveHandle(void (*pAppRxHandleTmp)(uint8_t * pu8Data, uint8_t* pu8Sender))
{
esp_err_t err = ESP_OK;
int route_table_size = 0;
*pu16ChildrenSize = 0;
mesh_addr_t route_table[CONFIG_MESH_ROUTE_TABLE_SIZE];
ERROR_CHECK(esp_mesh_get_routing_table((mesh_addr_t *) &route_table, (CONFIG_MESH_ROUTE_TABLE_SIZE * 6), &route_table_size));
if (err == ESP_OK)
{
for(uint16_t index = 0; index < esp_mesh_get_routing_table_size(); index++)
{
if(! (bCheckMACEquality(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]);
pChildren[*pu16ChildrenSize] = route_table[index];
*pu16ChildrenSize = (*pu16ChildrenSize)+1;
}
}
}
return err;
pAppRxHandle = pAppRxHandleTmp; //set handle from app as receive handle if an app packet is received
return ESP_OK;
}
esp_err_t errGetParentNode(mesh_addr_t* pMeshParentAddr)
esp_err_t errMeshNetworkSetOTAMessageHandleHandle(void (*pOTAMessageHandleTmp)(MESH_PACKET_t* puMeshPacket))
{
esp_err_t err = ESP_OK;
pOTAMessageHandle = pOTAMessageHandleTmp;
return ESP_OK;
}
if((bIsMeshConnected == false) || (esp_mesh_is_root()))
{
//this node is not connected or is the root --> this node has no parent
err = ESP_FAIL;
}
else
{
//node has parent
memcpy(pMeshParentAddr, &meshParentAddr, sizeof(mesh_addr_t));
}
return err;
esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOfServerWorkerHandleTmp)(bool bState))
{
pChangeStateOfServerWorkerHandle = pChangeStateOfServerWorkerHandleTmp;
return ESP_OK;
}
void vGetOwnAddr(mesh_addr_t* pMeshOwnAddr)
esp_err_t errMeshNetworkSendMeshPacket(mesh_addr_t* pAddrDest, MESH_PACKET_t* pPacket)
{
memcpy(pMeshOwnAddr->addr, u8ownMAC, 6);
esp_err_t err;
mesh_data_t data;
uint8_t tx_buf[CONFIG_MESH_MESSAGE_SIZE] = { 0, };
data.data = tx_buf;
data.size = sizeof(tx_buf);
data.proto = MESH_PROTO_BIN;
data.tos = MESH_TOS_P2P;
memcpy(tx_buf, (uint8_t *)pPacket, sizeof(MESH_PACKET_t));
err = esp_mesh_send(pAddrDest, &data, MESH_DATA_P2P, NULL, 0);
return err;
}
bool bIsRootNode()
bool bMeshNetworkIsRootNode()
{
return esp_mesh_is_root();
}
bool bIsNodeNeighbour(mesh_addr_t* pNode)
bool bMeshNetworkIsNodeNeighbour(mesh_addr_t* pNode)
{
esp_err_t err = ESP_OK;
bool bReturn = false;
@ -200,11 +184,11 @@ bool bIsNodeNeighbour(mesh_addr_t* pNode)
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
err = errGetParentNode(&addrParent);
err = errMeshNetworkGetParentNode(&addrParent);
if(err == ESP_OK)
{
if(bCheckMACEquality(pNode->addr, addrParent.addr) == true)
if(bMeshNetworkCheckMACEquality(pNode->addr, addrParent.addr) == true)
{
bReturn = true; //node was found
}
@ -214,11 +198,11 @@ bool bIsNodeNeighbour(mesh_addr_t* pNode)
{
err = ESP_OK; //reset error code
ERROR_CHECK(errGetChildren(childrenAddr, &u16ChildrenSize)); //get all children
ERROR_CHECK(errMeshNetworkGetChildren(childrenAddr, &u16ChildrenSize)); //get all children
for (uint16_t u16Index = 0; ((u16Index < u16ChildrenSize) && (err == ESP_OK) && (bReturn == false)); u16Index++)
{
if(bCheckMACEquality(pNode->addr, childrenAddr[u16Index].addr) == true)
if(bMeshNetworkCheckMACEquality(pNode->addr, childrenAddr[u16Index].addr) == true)
{
bReturn = true; //node was found
}
@ -227,64 +211,82 @@ bool bIsNodeNeighbour(mesh_addr_t* pNode)
return bReturn;
}
esp_err_t errMeshNetworkSetAppReceiveHandle(void (*pAppRxHandleTmp)(uint8_t * pu8Data, uint8_t* pu8Sender))
{
pAppRxHandle = pAppRxHandleTmp; //set handle from app as receive handle if an app packet is received
return ESP_OK;
}
esp_err_t errMeshNetworkSetChildConnectedHandle(void (*pChildConnectHandleTmp)(uint8_t * pu8Data))
//returns true if MAC address is equal
bool bMeshNetworkCheckMACEquality(uint8_t* pu8aMAC, uint8_t* pu8bMAC)
{
pOTAChildConnectHandle = pChildConnectHandleTmp;
return ESP_OK;
}
bool bRet = true;
uint8_t index = 0;
esp_err_t errMeshNetworkSetOTAMessageHandleHandle(void (*pOTAMessageHandleTmp)(MESH_PACKET_t* puMeshPacket))
{
pOTAMessageHandle = pOTAMessageHandleTmp;
return ESP_OK;
}
while ((index < 6) && (bRet == true))
{
if(pu8aMAC[index] != pu8bMAC[index])
{
bRet = false;
}
esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOfServerWorkerHandleTmp)(bool bState))
{
pChangeStateOfServerWorkerHandle = pChangeStateOfServerWorkerHandleTmp;
return ESP_OK;
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;
}
esp_err_t errSendMeshPacket(mesh_addr_t* pAddrDest, MESH_PACKET_t* pPacket)
esp_err_t errMeshNetworkStartReceiveTask()
{
esp_err_t err;
mesh_data_t data;
data.data = tx_buf;
data.size = sizeof(tx_buf);
data.proto = MESH_PROTO_BIN;
data.tos = MESH_TOS_P2P;
memcpy(tx_buf, (uint8_t *)pPacket, sizeof(MESH_PACKET_t));
esp_err_t err = ESP_OK;
BaseType_t xReturned;
err = esp_mesh_send(pAddrDest, &data, MESH_DATA_P2P, NULL, 0);
xReturned = xTaskCreate(vMeshNetworkTaskReceiveMeshData, "ReceiveMeshData", 7000, NULL, 5, NULL);
if(xReturned != pdPASS)
{
err = ESP_FAIL;
}
return err;
}
esp_err_t errStartReceiveTask()
void vMeshNetworkGetOwnAddr(mesh_addr_t* pMeshOwnAddr)
{
esp_err_t err = ESP_OK;
BaseType_t xReturned;
memcpy(pMeshOwnAddr->addr, u8ownMAC, 6);
}
xReturned = xTaskCreate(vTaskReceiveMeshData, "ReceiveMeshData", 7000, NULL, 5, NULL);
if(xReturned != pdPASS)
esp_err_t errMeshNetworkGetChildren(mesh_addr_t* pChildren, uint16_t* pu16ChildrenSize)
{
esp_err_t err = ESP_OK;
int route_table_size = 0;
*pu16ChildrenSize = 0;
mesh_addr_t route_table[CONFIG_MESH_ROUTE_TABLE_SIZE];
ERROR_CHECK(esp_mesh_get_routing_table((mesh_addr_t *) &route_table, (CONFIG_MESH_ROUTE_TABLE_SIZE * 6), &route_table_size));
if (err == ESP_OK)
{
err = ESP_FAIL;
for(uint16_t index = 0; index < esp_mesh_get_routing_table_size(); index++)
{
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]);
pChildren[*pu16ChildrenSize] = route_table[index];
*pu16ChildrenSize = (*pu16ChildrenSize)+1;
}
}
}
return err;
}
void vTaskReceiveMeshData(void *arg)
void vMeshNetworkTaskReceiveMeshData(void *arg)
{
esp_err_t err;
mesh_addr_t from;
mesh_data_t data;
uint8_t rx_buf[CONFIG_MESH_MESSAGE_SIZE] = { 0, };
int flag = 0;
data.data = rx_buf;
data.size = CONFIG_MESH_MESSAGE_SIZE;
@ -329,6 +331,23 @@ void vTaskReceiveMeshData(void *arg)
}
esp_err_t errMeshNetworkGetParentNode(mesh_addr_t* pMeshParentAddr)
{
esp_err_t err = ESP_OK;
if((bIsMeshConnected == false) || (esp_mesh_is_root()))
{
//this node is not connected or is the root --> this node has no parent
err = ESP_FAIL;
}
else
{
//node has parent
memcpy(pMeshParentAddr, &meshParentAddr, sizeof(mesh_addr_t));
}
return err;
}

6
components/mesh_ota/Mesh_Network_Handler.c

@ -3,7 +3,7 @@
static const char *LOG_TAG = "mesh_network_handler";
void vIPEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void *vpEventData)
void vMeshNetworkIpEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void *vpEventData)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *) vpEventData;
ESP_LOGI(LOG_TAG, "<IP_EVENT_STA_GOT_IP>IP:" IPSTR, IP2STR(&event->ip_info.ip));
@ -13,7 +13,7 @@ void vIPEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID,
}
}
void vMeshEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void* vpEventData)
void vMeshNetworkMeshEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void* vpEventData)
{
mesh_addr_t id = {0,};
static uint16_t last_layer = 0;
@ -95,7 +95,7 @@ if(esp_netif_dhcpc_start(netif_sta) == ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) /
if(pChangeStateOfServerWorkerHandle){pChangeStateOfServerWorkerHandle(true);}// signal reconnect
}
}
errStartReceiveTask();//start receiving
errMeshNetworkStartReceiveTask();//start receiving
}
break;
case MESH_EVENT_PARENT_DISCONNECTED:

61
components/mesh_ota/Mesh_OTA.c

@ -1,7 +1,7 @@
#include "Mesh_OTA.h"
#include "Mesh_OTA_Util.h"
#include "Mesh_OTA_Globals.h"
#include "Mesh_OTA_Access.h"
#include "Mesh_OTA_Partition_Access.h"
static const char *LOG_TAG = "mesh_ota";
@ -60,9 +60,9 @@ esp_err_t errMeshOTAInitialize()
}
}
ERROR_CHECK(errMeshNetworkSetChildConnectedHandle(vAddNodeToPossibleUpdatableQueue));
ERROR_CHECK(errMeshNetworkSetOTAMessageHandleHandle(vAddOtaMessageToQueue));
ERROR_CHECK(errMeshNetworkSetChangeStateOfServerWorkerHandle(vChangeStateOfServerWorker));
ERROR_CHECK(errMeshNetworkSetChildConnectedHandle(vMeshOtaUtilAddNodeToPossibleUpdatableQueue));
ERROR_CHECK(errMeshNetworkSetOTAMessageHandleHandle(vMeshOtaUtilAddOtaMessageToQueue));
ERROR_CHECK(errMeshNetworkSetChangeStateOfServerWorkerHandle(vMeshOtaUtilChangeStateOfServerWorker));
if(err == ESP_OK)
{
@ -77,7 +77,7 @@ esp_err_t errMeshOTAInitialize()
if(err == ESP_OK)
{
xReturned = xTaskCreate(vTaskServerWorker, "vTaskServerWorker", 8192, NULL, 5, NULL);
xReturned = xTaskCreate(vMeshOtaTaskServerWorker, "vMeshOtaTaskServerWorker", 8192, NULL, 5, NULL);
if(xReturned != pdPASS)
{
ESP_LOGE(LOG_TAG, "Unable to create the server worker task");
@ -88,7 +88,7 @@ esp_err_t errMeshOTAInitialize()
if(err == ESP_OK)
{
xReturned = xTaskCreate(vTaskOTAWorker, "vTaskOTAWorker", 8192, NULL, 5, NULL);
xReturned = xTaskCreate(vMeshOtaTaskOTAWorker, "vMeshOtaTaskOTAWorker", 8192, NULL, 5, NULL);
if(xReturned != pdPASS)
{
ESP_LOGE(LOG_TAG, "Unable to create the OTA worker task");
@ -99,7 +99,7 @@ esp_err_t errMeshOTAInitialize()
return err;
}
void vTaskServerWorker(void *arg)
void vMeshOtaTaskServerWorker(void *arg)
{
esp_err_t err;
bool bNewOTAImage; //true if a new ota image was downloaded and validated
@ -126,7 +126,7 @@ void vTaskServerWorker(void *arg)
ERROR_CHECK(errHTTPSClientValidateServer());
ERROR_CHECK(errHTTPSClientSendRequest());
ERROR_CHECK(errOTAHTTPS(&bNewOTAImage));
ERROR_CHECK(errMeshOtaPartitionAccessHttps(&bNewOTAImage));
errHTTPSClientReset();
if(bNewOTAImage == true)
@ -134,14 +134,14 @@ void vTaskServerWorker(void *arg)
//set want reboot
ESP_LOGI(LOG_TAG, "Updated successfully via HTTPS, set pending reboot");
bWantReboot = true;
vAddAllNeighboursToQueue(); //add all existing neighbours to queue (aparent will not be added because this node is the root)
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
}
}
}
void vTaskOTAWorker(void *arg)
void vMeshOtaTaskOTAWorker(void *arg)
{
esp_err_t err = ESP_OK;
bool bNewOTAImage; //true if a new ota image was downloaded and validated
@ -164,7 +164,7 @@ void vTaskOTAWorker(void *arg)
//esp_restart();
}
ERROR_CHECK(errOTAMeshSlave(&bNewOTAImage));
ERROR_CHECK(errMeshOtaSlaveEndpoint(&bNewOTAImage));
}
else
{
@ -177,12 +177,12 @@ void vTaskOTAWorker(void *arg)
err = ESP_FAIL;
}
ERROR_CHECK(errOTAMeshMaster(&bNewOTAImage, &meshNodeAddr));
ERROR_CHECK(errMeshOtaMasterEndpoint(&bNewOTAImage, &meshNodeAddr));
if (err != ESP_OK)
{
//OTA process faild --> add back to queue
vAddNodeToPossibleUpdatableQueue(meshNodeAddr.addr);
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(meshNodeAddr.addr);
}
}
@ -191,14 +191,13 @@ void vTaskOTAWorker(void *arg)
//set want reboot
ESP_LOGI(LOG_TAG, "Updated successfully via Mesh, set pending reboot");
bWantReboot = true;
vAddAllNeighboursToQueue(); //add all existing neighbours to queue
vMeshOtaUtilAddAllNeighboursToQueue(); //add all existing neighbours to queue
}
vTaskDelay( (1000) / portTICK_PERIOD_MS);
}
}
esp_err_t errOTAMeshSlave(bool* pbNewOTAImage)
esp_err_t errMeshOtaSlaveEndpoint(bool* pbNewOTAImage)
{
esp_err_t err = ESP_OK;
MESH_PACKET_t sOTAMessage;
@ -224,22 +223,22 @@ esp_err_t errOTAMeshSlave(bool* pbNewOTAImage)
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
ERROR_CHECK(errSendOTAVersionResponse(&sOTAMessage.meshSenderAddr));
ERROR_CHECK(errMeshOtaUtilSendOTAVersionResponse(&sOTAMessage.meshSenderAddr));
if((bNewerVersion((bootPartitionDesc).version, (char*) sOTAMessage.au8Payload)) && (err == ESP_OK)) //compare local and remote version
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");
// --> this version older --> start OTA_Rx --> set pbNewOTAImage true
ERROR_CHECK(errOTAMeshReceive(pbNewOTAImage, &sOTAMessage.meshSenderAddr));
ERROR_CHECK(errMeshOtaPartitionAccessMeshReceive(pbNewOTAImage, &sOTAMessage.meshSenderAddr));
}
if((bNewerVersion((char*) sOTAMessage.au8Payload, (bootPartitionDesc).version)) && (err == ESP_OK)) //compare remote and local version
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");
// --> this version newer --> start OTA_Tx
ERROR_CHECK(errOTAMeshTransmit(&sOTAMessage.meshSenderAddr));
ERROR_CHECK(errMeshOtaPartitionAccessMeshTransmit(&sOTAMessage.meshSenderAddr));
}
xSemaphoreGive(bsOTAProcess); //free binary semaphore, this allows other tasks to start the OTA process
}
@ -247,7 +246,7 @@ esp_err_t errOTAMeshSlave(bool* pbNewOTAImage)
return err;
}
esp_err_t errOTAMeshMaster(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
esp_err_t errMeshOtaMasterEndpoint(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
{
esp_err_t err = ESP_OK;
MESH_PACKET_t sOTAMessage;
@ -258,13 +257,13 @@ esp_err_t errOTAMeshMaster(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
*pbNewOTAImage = false; //set default false
if(bIsNodeNeighbour(pMeshNodeAddr) == true) //check if node is still connected
if(bMeshNetworkIsNodeNeighbour(pMeshNodeAddr) == true) //check if node is still connected
{
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
ERROR_CHECK(errMeshOtaUtilSendOTAVersionRequest(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
{
@ -277,32 +276,32 @@ esp_err_t errOTAMeshMaster(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
err = ESP_FAIL;
}
if((err == ESP_OK) && (sOTAMessage.type == OTA_Version_Response) && (bCheckMACEquality(sOTAMessage.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
if((err == ESP_OK) && (sOTAMessage.type == OTA_Version_Response) && (bMeshNetworkCheckMACEquality(sOTAMessage.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
{
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
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 pbNewOTAImage true
ERROR_CHECK(errOTAMeshReceive(pbNewOTAImage, &sOTAMessage.meshSenderAddr));
ERROR_CHECK(errMeshOtaPartitionAccessMeshReceive(pbNewOTAImage, &sOTAMessage.meshSenderAddr));
}
if((bNewerVersion((char*) sOTAMessage.au8Payload, (bootPartitionDesc).version)) && (err == ESP_OK)) //compare remote and local version
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(errOTAMeshTransmit(&sOTAMessage.meshSenderAddr));
ERROR_CHECK(errMeshOtaPartitionAccessMeshTransmit(&sOTAMessage.meshSenderAddr));
}
}
else if (err == ESP_OK)
{
//received from wrong node or type --> back to queue
vAddOtaMessageToQueue(&sOTAMessage);
vMeshOtaUtilAddOtaMessageToQueue(&sOTAMessage);
}
}
else
@ -319,7 +318,7 @@ esp_err_t errOTAMeshMaster(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
{
//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 ");
vAddNodeToPossibleUpdatableQueue(pMeshNodeAddr->addr);
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(pMeshNodeAddr->addr);
}
return err;
}

5
components/mesh_ota/Mesh_OTA_Globals.c

@ -6,5 +6,10 @@ xQueueHandle queueMessageOTA; //mesh ota controll messages like "OTA_Version_Res
SemaphoreHandle_t bsStartStopServerWorker; //binary semaphore
SemaphoreHandle_t bsOTAProcess; //binary semaphore
//w: errMeshOTAInitialize;
//r: errMeshOTAInitialize; errMeshOtaPartitionAccessHttps; errMeshOtaPartitionAccessMeshReceive;
const esp_partition_t* pOTAPartition; //pointer to ota partition
//w: errMeshOTAInitialize; vMeshOtaTaskOTAWorker; vMeshOtaTaskServerWorker
//r: errMeshOTAInitialize; vMeshOtaTaskOTAWorker; vMeshOtaTaskServerWorker
bool bWantReboot; //flag to signal pending reboot

193
components/mesh_ota/Mesh_OTA_Partition_Access.c

@ -1,11 +1,90 @@
#include "Mesh_OTA.h"
#include "Mesh_OTA_Util.h"
#include "Mesh_OTA_Globals.h"
#include "Mesh_OTA_Access.h"
#include "Mesh_OTA_Partition_Access.h"
static const char *LOG_TAG = "mesh_ota_access";
static const char *LOG_TAG = "mesh_ota_partition_access";
esp_err_t errOTAMeshTransmit(mesh_addr_t* pMeshNodeAddr)
esp_err_t errMeshOtaPartitionAccessHttps(bool* pbNewOTAImage)
{
esp_err_t err = ESP_OK;
char u8OTABuffer[OTA_HTTPS_SEGMENT_SIZE]; //store image segment from server before ota write
uint32_t u32BufferLenght = OTA_HTTPS_SEGMENT_SIZE; //size of buffer
uint32_t u32BytesRead = 0; //number of bytes that are read from server, <= u32BufferLenght
char pcRemoteVersionNumber[12]; //string for version number in server image
const esp_partition_t* pBootPartition; //pointer to boot partition (that will booted after reset)
static esp_ota_handle_t otaHandle; //OTA process handle
uint32_t u32StartOffset = 0U; //start offset for image (exclude the http response data)
esp_app_desc_t bootPartitionDesc; //Metadate from boot partition
uint32_t u32OTABytesWritten = 0U; //counter unsed for progress log
ERROR_CHECK(errHTTPSClientRetrieveData(u8OTABuffer, &u32BufferLenght, &u32BytesRead)); //read first bytes if image, including the version
ERROR_CHECK(errMeshOtaUtilExtractVersionNumber(u8OTABuffer, &u32BytesRead, pcRemoteVersionNumber)); //extract version numbers
if(err == ESP_OK) //check if version number is found
{
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
if(bMeshOtaUtilNewerVersion((bootPartitionDesc).version, pcRemoteVersionNumber)) //compare local and remote version
{
// server image is newer --> OTA update required
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);
ERROR_CHECK(esp_ota_write(otaHandle, (const void*) u8OTABuffer+u32StartOffset, (u32BytesRead-u32StartOffset)));
if(err == ESP_OK)
{
//write was succsesfull
u32StartOffset = 0U; //reset the offset for next download
ERROR_CHECK(errHTTPSClientRetrieveData(u8OTABuffer, &u32BufferLenght, &u32BytesRead)); //download next data segment
u32OTABytesWritten = u32OTABytesWritten + u32BytesRead; //update counter
}
}
while ((u32BytesRead > 0) && (err == ESP_OK) && (u32OTABytesWritten <= pOTAPartition->size)); //loop until error or complete image downloaded
}
if(err == ESP_OK)
{
//no error occurred --> finish ota update process
ERROR_CHECK(esp_ota_end(otaHandle)); //finish process
ERROR_CHECK(esp_ota_set_boot_partition(pOTAPartition)); //set new image as boot
if(err == ESP_OK)
{
*pbNewOTAImage = true; //image validated
}
}
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));
ERROR_CHECK(esp_ota_abort(otaHandle));
*pbNewOTAImage = false; //ota update failed
}
}
else
{
ESP_LOGI(LOG_TAG, "server image is NOT newer --> OTA update NOT required");
}
xSemaphoreGive(bsOTAProcess); //free binary semaphore, this allows other tasks to start the OTA process
} //end version number extracted
return err;
}
esp_err_t errMeshOtaPartitionAccessMeshTransmit(mesh_addr_t* pMeshNodeAddr)
{
esp_err_t err = ESP_OK;
const esp_partition_t* pBootPartition; //pointer to boot partition (that will booted after reset)
@ -26,7 +105,7 @@ esp_err_t errOTAMeshTransmit(mesh_addr_t* pMeshNodeAddr)
// 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);
vMeshOtaUtilPrintOTAProgress(&(pBootPartition->size), &u32OTABytesWritten, Transmitter);
if(err == ESP_OK)
{
@ -40,7 +119,7 @@ esp_err_t errOTAMeshTransmit(mesh_addr_t* pMeshNodeAddr)
sMeshPacket.type = OTA_Complete;
}
//ESP_LOGI(LOG_TAG, "OTA-TX: send packet");
err = errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
err = errMeshNetworkSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
else
{
@ -48,7 +127,7 @@ esp_err_t errOTAMeshTransmit(mesh_addr_t* pMeshNodeAddr)
sMeshPacket.type = OTA_Abort;
bAbort = true;
ESP_LOGI(LOG_TAG, "OTA-TX: error while read --> send ABORT");
errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
errMeshNetworkSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
// loop through all OTA messages or until abort is called or error
@ -63,7 +142,7 @@ esp_err_t errOTAMeshTransmit(mesh_addr_t* pMeshNodeAddr)
err = ESP_FAIL;
}
if((err == ESP_OK) && (bCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
if((err == ESP_OK) && (bMeshNetworkCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
{
//packet from node received
switch (sMeshPacket.type)
@ -79,14 +158,14 @@ esp_err_t errOTAMeshTransmit(mesh_addr_t* pMeshNodeAddr)
break;
default:
//receives wrong OTA message type from node --> back to queue
//vAddOtaMessageToQueue(&sMeshPacket);
//vMeshOtaUtilAddOtaMessageToQueue(&sMeshPacket);
break;
}
}
else if (err == ESP_OK)
{
//received from wrong node --> back to queue
vAddOtaMessageToQueue(&sMeshPacket);
vMeshOtaUtilAddOtaMessageToQueue(&sMeshPacket);
}
/*
}
@ -109,11 +188,11 @@ esp_err_t errOTAMeshTransmit(mesh_addr_t* pMeshNodeAddr)
}
u32SegmentCounter++;
}//end of partition segment loop
vClearOtaMessageQueue(pMeshNodeAddr);
vMeshOtaUtilClearOtaMessageQueue(pMeshNodeAddr);
return err;
}
esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
esp_err_t errMeshOtaPartitionAccessMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
{
esp_err_t err = ESP_OK;
MESH_PACKET_t sMeshPacket; //packet for sending and receiving
@ -144,7 +223,7 @@ esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
err = ESP_FAIL;
}
if((err == ESP_OK) && (bCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
if((err == ESP_OK) && (bMeshNetworkCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr))) //if OTA_Version_Request
{
//packet from node received
switch (sMeshPacket.type)
@ -157,7 +236,7 @@ esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
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);
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
@ -168,14 +247,14 @@ esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
break;
default:
//receives wrong OTA message type from node --> back to queue
//vAddOtaMessageToQueue(&sMeshPacket);
//vMeshOtaUtilAddOtaMessageToQueue(&sMeshPacket);
break;
}
}
else if (err == ESP_OK)
{
//received from wrong node --> back to queue
vAddOtaMessageToQueue(&sMeshPacket);
vMeshOtaUtilAddOtaMessageToQueue(&sMeshPacket);
}
/* }
@ -207,7 +286,7 @@ esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
//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);
err = errMeshNetworkSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
}
else
@ -216,7 +295,7 @@ esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
sMeshPacket.type = OTA_Abort;
bAbort = true;
ESP_LOGI(LOG_TAG, "OTA-RX: abort --> send ABORT");
errSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
errMeshNetworkSendMeshPacket(pMeshNodeAddr, &sMeshPacket);
}
}
u32SegmentCounter++;
@ -239,85 +318,7 @@ esp_err_t errOTAMeshReceive(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr)
//not all OTA segments received --> abort this OTA process
ERROR_CHECK(esp_ota_abort(otaHandle));
}
vClearOtaMessageQueue(pMeshNodeAddr);
vMeshOtaUtilClearOtaMessageQueue(pMeshNodeAddr);
return err;
}
esp_err_t errOTAHTTPS(bool* pbNewOTAImage)
{
esp_err_t err = ESP_OK;
char u8OTABuffer[OTA_HTTPS_SEGMENT_SIZE]; //store image segment from server before ota write
uint32_t u32BufferLenght = OTA_HTTPS_SEGMENT_SIZE; //size of buffer
uint32_t u32BytesRead = 0; //number of bytes that are read from server, <= u32BufferLenght
char pcRemoteVersionNumber[12]; //string for version number in server image
const esp_partition_t* pBootPartition; //pointer to boot partition (that will booted after reset)
static esp_ota_handle_t otaHandle; //OTA process handle
uint32_t u32StartOffset = 0U; //start offset for image (exclude the http response data)
esp_app_desc_t bootPartitionDesc; //Metadate from boot partition
uint32_t u32OTABytesWritten = 0U; //counter unsed for progress log
ERROR_CHECK(errHTTPSClientRetrieveData(u8OTABuffer, &u32BufferLenght, &u32BytesRead)); //read first bytes if image, including the version
ERROR_CHECK(errExtractVersionNumber(u8OTABuffer, &u32BytesRead, pcRemoteVersionNumber)); //extract version numbers
if(err == ESP_OK) //check if version number is found
{
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
if(bNewerVersion((bootPartitionDesc).version, pcRemoteVersionNumber)) //compare local and remote version
{
// server image is newer --> OTA update required
ESP_LOGI(LOG_TAG, "Server: image is newer --> OTA update required");
ERROR_CHECK(errFindImageStart(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
{
vPrintOTAProgress(&(pOTAPartition->size), &u32OTABytesWritten, Receiver);
ERROR_CHECK(esp_ota_write(otaHandle, (const void*) u8OTABuffer+u32StartOffset, (u32BytesRead-u32StartOffset)));
if(err == ESP_OK)
{
//write was succsesfull
u32StartOffset = 0U; //reset the offset for next download
ERROR_CHECK(errHTTPSClientRetrieveData(u8OTABuffer, &u32BufferLenght, &u32BytesRead)); //download next data segment
u32OTABytesWritten = u32OTABytesWritten + u32BytesRead; //update counter
}
}
while ((u32BytesRead > 0) && (err == ESP_OK) && (u32OTABytesWritten <= pOTAPartition->size)); //loop until error or complete image downloaded
}
if(err == ESP_OK)
{
//no error occurred --> finish ota update process
ERROR_CHECK(esp_ota_end(otaHandle)); //finish process
ERROR_CHECK(esp_ota_set_boot_partition(pOTAPartition)); //set new image as boot
if(err == ESP_OK)
{
*pbNewOTAImage = true; //image validated
}
}
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));
ERROR_CHECK(esp_ota_abort(otaHandle));
*pbNewOTAImage = false; //ota update failed
}
}
else
{
ESP_LOGI(LOG_TAG, "server image is NOT newer --> OTA update NOT required");
}
xSemaphoreGive(bsOTAProcess); //free binary semaphore, this allows other tasks to start the OTA process
} //end version number extracted
return err;
}

239
components/mesh_ota/Mesh_OTA_Util.c

@ -3,76 +3,7 @@
static const char *LOG_TAG = "mesh_ota";
void vAddNodeToPossibleUpdatableQueue(uint8_t* pu8MAC)
{
//send payload to node queues
mesh_addr_t addrNode;
memcpy(&addrNode.addr, (uint8_t *)pu8MAC, 6); //copy MAC
if (xQueueSend(queueNodes, &addrNode, portMAX_DELAY) != pdPASS)
{
ESP_LOGE(LOG_TAG, "Unable to push node into node queue");
}
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]);
}
}
void vAddOtaMessageToQueue(MESH_PACKET_t* puMeshPacket)
{
//send ota packet to packet queue
if (xQueueSend(queueMessageOTA, puMeshPacket, portMAX_DELAY) != pdPASS)
{
ESP_LOGE(LOG_TAG, "Unable to push ota packet into packet queue");
}
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;
}
}
}
void vChangeStateOfServerWorker(bool bState) //allow access via function ptn to networl_handler
{
static bool bLastState = false;
if(bState != bLastState) //change only if necessary
{
ESP_LOGI(LOG_TAG, "server worker change handler");
if(bState == true)
{
if (xSemaphoreGive(bsStartStopServerWorker) != pdTRUE)
{
ESP_LOGE(LOG_TAG, "Unable to give mutex to activate the server worker");
}
}
else
{
if (xSemaphoreTake(bsStartStopServerWorker,( TickType_t ) 10 ) != pdTRUE)
{
ESP_LOGE(LOG_TAG, "Unable to obtain mutex to deactivate the server worker");
}
}
bLastState = bState;
}
}
bool bNewerVersion(const char* pu8Local, const char* pu8Remote)
bool bMeshOtaUtilNewerVersion(const char* pu8Local, const char* pu8Remote)
{
/*
* Return true if remote version is newer (higher) than local version
@ -103,7 +34,24 @@ bool bNewerVersion(const char* pu8Local, const char* pu8Remote)
return bReturn;
}
esp_err_t errFindImageStart(const char* pu8Data, uint32_t* pu32DataLenght, uint32_t* pu32StartOffset)
esp_err_t errMeshOtaUtilExtractVersionNumber(const char* pu8Data, uint32_t* pu32DataLenght, char* pc8RemoteVersionNumber)
{
uint32_t u32StartOffset;
esp_err_t err = ESP_OK;
strcpy(pc8RemoteVersionNumber, "999.999.999"); //init value
err = errMeshOtaUtilFindImageStart(pu8Data, pu32DataLenght, &u32StartOffset); //get image start offset
if(err == ESP_OK)
{
//image found
strncpy(pc8RemoteVersionNumber, pu8Data+(u32StartOffset+48), 11); //copy version number
pc8RemoteVersionNumber[12] = '\0';
}
return err;
}
esp_err_t errMeshOtaUtilFindImageStart(const char* pu8Data, uint32_t* pu32DataLenght, uint32_t* pu32StartOffset)
{
/*
Offset value
@ -181,24 +129,42 @@ esp_err_t errFindImageStart(const char* pu8Data, uint32_t* pu32DataLenght, uint3
return errReturn;
}
esp_err_t errExtractVersionNumber(const char* pu8Data, uint32_t* pu32DataLenght, char* pc8RemoteVersionNumber)
esp_err_t errMeshOtaUtilSendOTAVersionRequest(mesh_addr_t* pMeshReceiverAddr)
{
uint32_t u32StartOffset;
esp_err_t err = ESP_OK;
MESH_PACKET_t packet;
packet.type = OTA_Version_Request;
strcpy(pc8RemoteVersionNumber, "999.999.999"); //init value
err = errFindImageStart(pu8Data, pu32DataLenght, &u32StartOffset); //get image start offset
const esp_partition_t* pBootPartition; //pointer to boot partition (that will booted after reset)
esp_app_desc_t bootPartitionDesc; //Metadata from boot partition
if(err == ESP_OK)
{
//image found
strncpy(pc8RemoteVersionNumber, pu8Data+(u32StartOffset+48), 11); //copy version number
pc8RemoteVersionNumber[12] = '\0';
}
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_Request packet
err = errMeshNetworkSendMeshPacket(pMeshReceiverAddr, &packet);
return err;
}
void vPrintOTAProgress(const uint32_t* const pu32TotalImageSize, const uint32_t* const pu32BytesWritten, OTA_MESH_ROLE_t eRole)
esp_err_t errMeshOtaUtilSendOTAVersionResponse(mesh_addr_t* pMeshReceiverAddr)
{
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)
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", pMeshReceiverAddr->addr[5]);
err = errMeshNetworkSendMeshPacket(pMeshReceiverAddr, &packet);
return err;
}
void vMeshOtaUtilPrintOTAProgress(const uint32_t* const pu32TotalImageSize, const uint32_t* const pu32BytesWritten, OTA_MESH_ROLE_t eRole)
{
uint32_t u32Percentage = 0U;
static uint32_t u32LastPercentage = 0U;
@ -230,7 +196,7 @@ void vPrintOTAProgress(const uint32_t* const pu32TotalImageSize, const uint32_t*
}
}
void vAddAllNeighboursToQueue(void)
void vMeshOtaUtilAddAllNeighboursToQueue(void)
{
esp_err_t err = ESP_OK;
@ -238,55 +204,36 @@ void vAddAllNeighboursToQueue(void)
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
err = errGetParentNode(&addrParent);
err = errMeshNetworkGetParentNode(&addrParent);
if(err == ESP_OK)
{
vAddNodeToPossibleUpdatableQueue(addrParent.addr);
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(addrParent.addr);
ESP_LOGI(LOG_TAG, "added parent");
}
err = ESP_OK; //reset error code
ERROR_CHECK(errGetChildren(childrenAddr, &u16ChildrenSize)); //get all children
ERROR_CHECK(errMeshNetworkGetChildren(childrenAddr, &u16ChildrenSize)); //get all children
for (uint16_t u16Index = 0; ((u16Index < u16ChildrenSize) && (err == ESP_OK)); u16Index++)
{
vAddNodeToPossibleUpdatableQueue(childrenAddr[u16Index].addr);
vMeshOtaUtilAddNodeToPossibleUpdatableQueue(childrenAddr[u16Index].addr);
ESP_LOGI(LOG_TAG, "added child");
}
}
esp_err_t errSendOTAVersionResponse(mesh_addr_t* pMeshReceiverAddr)
{
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)
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", pMeshReceiverAddr->addr[5]);
err = errSendMeshPacket(pMeshReceiverAddr, &packet);
return err;
}
void vClearOtaMessageQueue(mesh_addr_t* pMeshNodeAddr)
void vMeshOtaUtilClearOtaMessageQueue(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)))
if(!(bMeshNetworkCheckMACEquality(sMeshPacket.meshSenderAddr.addr, pMeshNodeAddr->addr)))
{
//received OTA message is NOT from pMeshNodeAddr --> keep it in queue
vAddOtaMessageToQueue(&sMeshPacket);
vMeshOtaUtilAddOtaMessageToQueue(&sMeshPacket);
}
else
{
@ -296,19 +243,71 @@ void vClearOtaMessageQueue(mesh_addr_t* pMeshNodeAddr)
}//end OTA message loop
}
esp_err_t errSendOTAVersionRequest(mesh_addr_t* pMeshReceiverAddr)
void vMeshOtaUtilAddNodeToPossibleUpdatableQueue(uint8_t* pu8MAC)
{
esp_err_t err = ESP_OK;
MESH_PACKET_t packet;
packet.type = OTA_Version_Request;
//send payload to node queues
mesh_addr_t addrNode;
memcpy(&addrNode.addr, (uint8_t *)pu8MAC, 6); //copy MAC
const esp_partition_t* pBootPartition; //pointer to boot partition (that will booted after reset)
esp_app_desc_t bootPartitionDesc; //Metadata from boot partition
if (xQueueSend(queueNodes, &addrNode, portMAX_DELAY) != pdPASS)
{
ESP_LOGE(LOG_TAG, "Unable to push node into node queue");
}
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]);
}
}
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_Request packet
err = errSendMeshPacket(pMeshReceiverAddr, &packet);
return err;
void vMeshOtaUtilAddOtaMessageToQueue(MESH_PACKET_t* puMeshPacket)
{
//send ota packet to packet queue
if (xQueueSend(queueMessageOTA, puMeshPacket, portMAX_DELAY) != pdPASS)
{
ESP_LOGE(LOG_TAG, "Unable to push ota packet into packet queue");
}
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;
}
}
}
void vMeshOtaUtilChangeStateOfServerWorker(bool bState) //allow access via function ptn to networl_handler
{
static bool bLastState = false;
if(bState != bLastState) //change only if necessary
{
ESP_LOGI(LOG_TAG, "server worker change handler");
if(bState == true)
{
if (xSemaphoreGive(bsStartStopServerWorker) != pdTRUE)
{
ESP_LOGE(LOG_TAG, "Unable to give mutex to activate the server worker");
}
}
else
{
if (xSemaphoreTake(bsStartStopServerWorker,( TickType_t ) 10 ) != pdTRUE)
{
ESP_LOGE(LOG_TAG, "Unable to obtain mutex to deactivate the server worker");
}
}
bLastState = bState;
}
}

32
components/mesh_ota/include/Mesh_Network.h

@ -84,32 +84,28 @@ extern uint8_t u8ownMAC[6];
extern void (*pOTAChildConnectHandle)(uint8_t* );
extern void (*pChangeStateOfServerWorkerHandle)(bool );
esp_err_t errMeshNetworkInitialize();
esp_err_t errMeshNetworkInitializeWiFi();
esp_err_t errMeshNetworkInitialize(void);
esp_err_t errMeshNetworkInitializeWiFi(void);
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 errMeshNetworkSetAppReceiveHandle(void (*pAppRxHandleTmp)(uint8_t * pu8Data, uint8_t* pu8Sender));
esp_err_t errMeshNetworkSetOTAMessageHandleHandle(void (*pOTAMessageHandleTmp)(MESH_PACKET_t* puMeshPacket));
esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOfServerWorkerHandleTmp)(bool bState));
bool bCheckMACEquality(uint8_t* pu8aMAC, uint8_t* pu8bMAC);
void vGetOwnAddr(mesh_addr_t* pMeshOwnAddr);
esp_err_t errGetParentNode(mesh_addr_t* pMeshParentAddr);
esp_err_t errGetChildren(mesh_addr_t* pChildren, uint16_t* pu16ChildrenSize);
bool bIsRootNode();
bool bIsNodeNeighbour(mesh_addr_t* pNode);
esp_err_t errStartReceiveTask();
void vTaskReceiveMeshData(void *arg);
void vMeshEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void* vpEventData);
void vIPEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void *event_data);
esp_err_t errSendMeshPacket(mesh_addr_t* pAddrDest, MESH_PACKET_t* pPacket);
bool bMeshNetworkIsRootNode(void);
bool bMeshNetworkIsNodeNeighbour(mesh_addr_t* pNode);
bool bMeshNetworkCheckMACEquality(uint8_t* pu8aMAC, uint8_t* pu8bMAC);
esp_err_t errMeshNetworkStartReceiveTask(void);
esp_err_t errMeshNetworkGetParentNode(mesh_addr_t* pMeshParentAddr);
esp_err_t errMeshNetworkGetChildren(mesh_addr_t* pChildren, uint16_t* pu16ChildrenSize);
esp_err_t errMeshNetworkSendMeshPacket(mesh_addr_t* pAddrDest, MESH_PACKET_t* pPacket);
void vMeshNetworkTaskReceiveMeshData(void *arg);
void vMeshNetworkGetOwnAddr(mesh_addr_t* pMeshOwnAddr);
void vMeshNetworkIpEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void *vpEventData);
void vMeshNetworkMeshEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void* vpEventData);
#endif /* H_MESH_NETWORK */

15
components/mesh_ota/include/Mesh_OTA.h

@ -28,15 +28,14 @@
} \
} \
esp_err_t errMeshOTAInitialize();
esp_err_t errOTAMeshSlave(bool* pbNewOTAImage);
esp_err_t errOTAMeshMaster(bool* pbNewOTAImage, mesh_addr_t* pMeshNodeAddr);