diff --git a/components/mesh_ota/CMakeLists.txt b/components/mesh_ota/CMakeLists.txt index 5484c68..8d5c8bc 100644 --- a/components/mesh_ota/CMakeLists.txt +++ b/components/mesh_ota/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "https_client.c" "Mesh_network_handler.c" "Mesh_network.c" "Mesh_OTA.c" +idf_component_register(SRCS "HTTPS_client.c" "Mesh_network_handler.c" "Mesh_network.c" "Mesh_OTA.c" INCLUDE_DIRS "include" REQUIRES nvs_flash esp_http_client diff --git a/components/mesh_ota/https_client.c b/components/mesh_ota/HTTPS_client.c similarity index 99% rename from components/mesh_ota/https_client.c rename to components/mesh_ota/HTTPS_client.c index 9c2b075..40fd597 100644 --- a/components/mesh_ota/https_client.c +++ b/components/mesh_ota/HTTPS_client.c @@ -1,4 +1,4 @@ -#include "https_client.h" +#include "HTTPS_client.h" static const char *TAG = "https_client"; diff --git a/components/mesh_ota/Mesh_OTA.c b/components/mesh_ota/Mesh_OTA.c index 2254c5f..4496bed 100644 --- a/components/mesh_ota/Mesh_OTA.c +++ b/components/mesh_ota/Mesh_OTA.c @@ -48,7 +48,7 @@ esp_err_t errMeshOTAInitialize() if(err == ESP_OK) { - xReturned = xTaskCreate(vTaskServerWorker, "vTaskServerWorker", 4096, NULL, 5, NULL); + xReturned = xTaskCreate(vTaskServerWorker, "vTaskServerWorker", 8192, NULL, 5, NULL); if(xReturned != pdPASS) { ESP_LOGE(LOG_TAG, "Unable to create the server worker task"); @@ -128,6 +128,18 @@ void vTaskOTAWorker(void *arg) void vTaskServerWorker(void *arg) { + + esp_err_t err = ESP_OK; + uint32_t u32BufferLenght = 1024U; + char buffer[1024U]; + uint32_t u32BytesRead = 0; + char pcRemoteVersionNumber[12]; + const esp_partition_t * currentPartition; + const esp_partition_t * otaPartition; + static esp_ota_handle_t otaHandle; + uint32_t u32StartOffset; + esp_app_desc_t otaPartitionDesc; + while(true) { xSemaphoreTake(bsStartStopServerWorker, portMAX_DELAY); //wait for binary semaphore that allows to start the worker @@ -139,6 +151,71 @@ void vTaskServerWorker(void *arg) //server get version + currentPartition = esp_ota_get_boot_partition(); + ESP_LOGI(LOG_TAG, "Type: %d", (*currentPartition).subtype); + ESP_LOGI(LOG_TAG, "Start address: %d", (*currentPartition).address); + ESP_LOGI(LOG_TAG, "Size: %d", (*currentPartition).size); + ESP_LOGI(LOG_TAG, "Encrypted: %d", (*currentPartition).encrypted); + + esp_app_desc_t curPartitionDesc; + err = esp_ota_get_partition_description(currentPartition, &curPartitionDesc); + ESP_ERROR_CHECK(err); + ESP_LOGI(LOG_TAG, "currentPartition project_name: %s", (curPartitionDesc).project_name); + ESP_LOGI(LOG_TAG, "currentPartition version: %s", (curPartitionDesc).version); + ESP_LOGI(LOG_TAG, "currentPartition Timestamp: %s %s", (curPartitionDesc).date, (curPartitionDesc).time); + + https_clientInitialize(); + https_clientRetrieveData(buffer, &u32BufferLenght, &u32BytesRead); + ESP_LOGI(LOG_TAG, "Data received: %i", u32BytesRead); + err = errExtractVersionNumber(buffer, &u32BytesRead, pcRemoteVersionNumber); + + if(err == ESP_OK) + { + if(bNewerVersion((curPartitionDesc).version, pcRemoteVersionNumber)) + { + ESP_LOGI(LOG_TAG, "Newer Version available"); + //write ota + otaPartition= esp_ota_get_next_update_partition(currentPartition); + + err = errFindImageStart(buffer, &u32BufferLenght, &u32StartOffset); + + ESP_LOGI(LOG_TAG, "first byte offset: %i", u32StartOffset); + ESP_LOGI(LOG_TAG, "first byte: %x", buffer[u32StartOffset]); + + err = esp_ota_begin(otaPartition, OTA_SIZE_UNKNOWN, &otaHandle); + ESP_ERROR_CHECK(err); + + do + { + ESP_LOGI(LOG_TAG, "OTA-Data written: %i", u32BytesRead); + err = esp_ota_write(otaHandle, (const void*) buffer+u32StartOffset, (u32BytesRead-u32StartOffset)); + u32StartOffset = 0U; + https_clientRetrieveData(buffer, &u32BufferLenght, &u32BytesRead); + } + while (u32BytesRead > 0); + + err = esp_ota_end(otaHandle); + ESP_ERROR_CHECK(err); + + err = esp_ota_get_partition_description(otaPartition, &otaPartitionDesc); + ESP_ERROR_CHECK(err); + ESP_LOGI(LOG_TAG, "otaPartition project_name: %s", (otaPartitionDesc).project_name); + err = esp_ota_set_boot_partition(otaPartition); + ESP_ERROR_CHECK(err); + //esp_restart(); + } + else + { + ESP_LOGI(LOG_TAG, "NO newer Version available"); + } + } + else + { + ESP_LOGI(LOG_TAG, "errExtractVersionNumber failed: %i", err); + } + + https_clientDeinitialize(); + //ota update if newer //lock ota mutex diff --git a/components/mesh_ota/include/https_client.h b/components/mesh_ota/include/HTTPS_client.h similarity index 100% rename from components/mesh_ota/include/https_client.h rename to components/mesh_ota/include/HTTPS_client.h diff --git a/components/mesh_ota/include/Mesh_OTA.h b/components/mesh_ota/include/Mesh_OTA.h index 3c6a99e..343baec 100644 --- a/components/mesh_ota/include/Mesh_OTA.h +++ b/components/mesh_ota/include/Mesh_OTA.h @@ -14,6 +14,7 @@ #include "esp_partition.h" #include "Mesh_network.h" +#include "HTTPS_client.h" #define ERASE_NVS //erase non volatile storage if full #define QUEUE_NODES_SIZE 10 diff --git a/main/Blinky_LED.c b/main/Blinky_LED.c index 2cfc7c7..384d4bb 100644 --- a/main/Blinky_LED.c +++ b/main/Blinky_LED.c @@ -17,31 +17,31 @@ esp_err_t errBlinkyLEDInitialize() //create queue to store led action created from BTN and mesh network events queueBlinkyLEDPackets = xQueueCreate(5, sizeof(BLINKY_PACKET_t)); if (queueBlinkyLEDPackets == 0) // Queue not created - { - ESP_LOGE(LOG_TAG, "Unable to create Queue for Application Packets"); - err = ESP_FAIL; - } + { + ESP_LOGE(LOG_TAG, "Unable to create Queue for Application Packets"); + err = ESP_FAIL; + } //register the receiver handle in mesh network ERROR_CHECK(errMeshNetworkSetAppReceiveHandle(rxHandle)); if(err == ESP_OK) - { - xReturned = xTaskCreate(vTaskReadUserInput, "vTaskReadUserInput", 4096, NULL, 5, NULL); - if(xReturned != pdPASS) { - err = ESP_FAIL; + xReturned = xTaskCreate(vTaskReadUserInput, "vTaskReadUserInput", 4096, NULL, 5, NULL); + if(xReturned != pdPASS) + { + err = ESP_FAIL; + } } - } if(err == ESP_OK) - { - xReturned = xTaskCreate(vTaskReceiveData, "vTaskReceiveData", 4096, NULL, 5, NULL); - if(xReturned != pdPASS) { - err = ESP_FAIL; + xReturned = xTaskCreate(vTaskReceiveData, "vTaskReceiveData", 4096, NULL, 5, NULL); + if(xReturned != pdPASS) + { + err = ESP_FAIL; + } } - } return err; } @@ -69,9 +69,9 @@ void rxHandle(uint8_t* pu8Data, uint8_t* pu8Sender) memcpy(&bTmpPacket, (uint8_t *)pu8Data, sizeof(BLINKY_PACKET_t)); memcpy(&bTmpPacket.meshSenderAddr, (uint8_t *)pu8Sender, 6); //copy MAC from sender into app packet if (xQueueSend(queueBlinkyLEDPackets, &bTmpPacket, portMAX_DELAY) != pdPASS) - { - ESP_LOGE(LOG_TAG, "Unable to push packet from mesh into Queue"); - } + { + ESP_LOGE(LOG_TAG, "Unable to push packet from mesh into Queue"); + } } void vTaskReadUserInput(void *arg) @@ -84,48 +84,49 @@ void vTaskReadUserInput(void *arg) meshPacket.type = APP_Data; //this is a app packet while(true) - { //check for BTN press - if(gpio_get_level(GPIO_BOOT_BTN) == 0) { - err = ESP_OK; - - if(bLEDisOn == false) - { - bTmpPacket.type = LED_ON; - } - else - { - bTmpPacket.type = LED_OFF; - } - - //push led action into queue - if (xQueueSend(queueBlinkyLEDPackets, &bTmpPacket, portMAX_DELAY) != pdPASS) - { - ESP_LOGE(LOG_TAG, "Unable to push packet into queue"); - } - - memcpy(meshPacket.au8Payload, &bTmpPacket, sizeof(BLINKY_PACKET_t)); - - if(bIsRootNode() == false) - { - //this node is not root --> send led action to parent - ERROR_CHECK(errGetParentNode(&addrParent)); - ERROR_CHECK(errSendMeshPacket(&addrParent, &meshPacket)); - } - else - { - //this node is root --> send led action to children - ERROR_CHECK(errGetChildren(childrenAddr, &u16ChildrenSize)); - - for (uint16_t u16Index = 0; u16Index < u16ChildrenSize; u16Index++) + //check for BTN press + if(gpio_get_level(GPIO_BOOT_BTN) == 0) { - ERROR_CHECK (errSendMeshPacket(&childrenAddr[u16Index], &meshPacket)); + err = ESP_OK; + + if(bLEDisOn == false) + { + bTmpPacket.type = LED_ON; + } + else + { + bTmpPacket.type = LED_OFF; + } + + //push led action into queue + if (xQueueSend(queueBlinkyLEDPackets, &bTmpPacket, portMAX_DELAY) != pdPASS) + { + ESP_LOGE(LOG_TAG, "Unable to push packet into queue"); + } + + memcpy(meshPacket.au8Payload, &bTmpPacket, sizeof(BLINKY_PACKET_t)); + + if(bIsRootNode() == false) + { + //this node is not root --> send led action to parent + ERROR_CHECK(errGetParentNode(&addrParent)); + ERROR_CHECK(errSendMeshPacket(&addrParent, &meshPacket)); + } + else + { + //this node is root --> send led action to children + ERROR_CHECK(errGetChildren(childrenAddr, &u16ChildrenSize)); + + for (uint16_t u16Index = 0; u16Index < u16ChildrenSize; u16Index++) + { + ERROR_CHECK (errSendMeshPacket(&childrenAddr[u16Index], &meshPacket)); + } + } + vTaskDelay(200 / portTICK_PERIOD_MS); } - } - vTaskDelay(200 / portTICK_PERIOD_MS); + vTaskDelay(50 / portTICK_PERIOD_MS); } - vTaskDelay(50 / portTICK_PERIOD_MS); - } } void vTaskReceiveData(void *arg) @@ -138,47 +139,48 @@ void vTaskReceiveData(void *arg) meshPacket.type = APP_Data; //this is a app packet while (1) - { - if (xQueueReceive(queueBlinkyLEDPackets, &bTmpPacket, portMAX_DELAY) != pdTRUE) { - ESP_LOGE(LOG_TAG, "Unable to receive packet from Queue"); + if (xQueueReceive(queueBlinkyLEDPackets, &bTmpPacket, portMAX_DELAY) != pdTRUE) + { + ESP_LOGE(LOG_TAG, "Unable to receive packet from Queue"); + } + else + { + err = ESP_OK; + //Successfully RECEIVED the packet + switch (bTmpPacket.type) + { + case LED_ON: + bLEDisOn = true; + gpio_set_level(GPIO_LED, 1); //switch on + ESP_LOGI(LOG_TAG,"switch LED ON"); + break; + + case LED_OFF: + bLEDisOn = false; + gpio_set_level(GPIO_LED, 0); //switch off + ESP_LOGI(LOG_TAG,"switch LED OFF"); + break; + + default: + bLEDisOn = false; + gpio_set_level(GPIO_LED, 0); //switch off + ESP_LOGI(LOG_TAG,"switch LED OFF"); + break; + } + } + + ERROR_CHECK(errGetChildren(childrenAddr, &u16ChildrenSize)); //get all children attached to this node + memcpy(meshPacket.au8Payload, &bTmpPacket, sizeof(BLINKY_PACKET_t)); //copy led action in mesh packet payload + + for (uint16_t u16Index = 0; u16Index < u16ChildrenSize; u16Index++) + { + //loop through children + if(bCheckMACEquality(bTmpPacket.meshSenderAddr.addr, childrenAddr[u16Index].addr) == false) //exclude the sender node + { + ERROR_CHECK (errSendMeshPacket(&childrenAddr[u16Index], &meshPacket)); //send to child + } + } + vTaskDelay(200 / portTICK_PERIOD_MS); } - else - { - err = ESP_OK; - //Successfully RECEIVED the packet - switch (bTmpPacket.type) - { - case LED_ON: - bLEDisOn = true; - gpio_set_level(GPIO_LED, 1); //switch on - ESP_LOGI(LOG_TAG,"switch LED ON"); - break; - - case LED_OFF: - bLEDisOn = false; - gpio_set_level(GPIO_LED, 0); //switch off - ESP_LOGI(LOG_TAG,"switch LED OFF"); - break; - - default: - bLEDisOn = false; - gpio_set_level(GPIO_LED, 0); //switch off - ESP_LOGI(LOG_TAG,"switch LED OFF"); - break; - } - } - - ERROR_CHECK(errGetChildren(childrenAddr, &u16ChildrenSize)); //get all children attached to this node - memcpy(meshPacket.au8Payload, &bTmpPacket, sizeof(BLINKY_PACKET_t)); //copy led action in mesh packet payload - - for (uint16_t u16Index = 0; u16Index < u16ChildrenSize; u16Index++) - { //loop through children - if(bCheckMACEquality(bTmpPacket.meshSenderAddr.addr, childrenAddr[u16Index].addr) == false) //exclude the sender node - { - ERROR_CHECK (errSendMeshPacket(&childrenAddr[u16Index], &meshPacket)); //send to child - } - } - vTaskDelay(200 / portTICK_PERIOD_MS); - } } \ No newline at end of file diff --git a/style_code.sh b/style_code.sh new file mode 100644 index 0000000..2857fb3 --- /dev/null +++ b/style_code.sh @@ -0,0 +1,15 @@ +#! /bin/bash + +cd main +astyle --style=gnu *.c +astyle --style=gnu *.h + +cd .. + +cd components/mesh_ota +astyle --style=gnu *.c +cd include +astyle --style=gnu *.h + + +