diff --git a/.gitignore b/.gitignore index 37d7f21..19e3a2a 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,6 @@ build/ test/build/ *.old + +*.orig + diff --git a/components/mesh_ota/CMakeLists.txt b/components/mesh_ota/CMakeLists.txt index c524a09..5484c68 100644 --- a/components/mesh_ota/CMakeLists.txt +++ b/components/mesh_ota/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "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 new file mode 100644 index 0000000..cb4d204 --- /dev/null +++ b/components/mesh_ota/https_client.c @@ -0,0 +1,292 @@ +#include "https_client.h" + +static const char *TAG = "https_client"; + +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" + "Authorization: Basic " CONFIG_OTA_HTTPS_AUTH "\r\n" + "\r\n"; + + +static HTTPS_Client_t sHTTPS_ClientConfig; + +https_client_ret_t https_clientInitEmbedTLS(); +https_client_ret_t https_clientConnectToServer(); +https_client_ret_t https_clientValidateServer(); +https_client_ret_t https_clientSendRequest(); + +https_client_ret_t https_clientInitialize() +{ + https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; + + i32RetHTTPClient = https_clientInitEmbedTLS(); + + if(i32RetHTTPClient == HTTPS_CLIENT_OK) + { + i32RetHTTPClient = https_clientConnectToServer(); + } + + if(i32RetHTTPClient == HTTPS_CLIENT_OK) + { + i32RetHTTPClient = https_clientValidateServer(); + } + + if(i32RetHTTPClient == HTTPS_CLIENT_OK) + { + i32RetHTTPClient = https_clientSendRequest(); + } + + switch (i32RetHTTPClient) + { + case HTTPS_CLIENT_ERROR_INIT_EMBEDTLS: + ESP_LOGE(TAG, "Unable to initialize EmbedTLS"); + i32RetHTTPClient = HTTPS_CLIENT_ERROR; + break; + case HTTPS_CLIENT_ERROR_INIT_CONNECT_TWO_SERVER: + ESP_LOGE(TAG, "Unable to connect to server"); + i32RetHTTPClient = HTTPS_CLIENT_ERROR; + break; + case HTTPS_CLIENT_ERROR_INIT_VALIDATE_SERVER: + ESP_LOGE(TAG, "Unable to validate the server"); + i32RetHTTPClient = HTTPS_CLIENT_ERROR; + break; + case HTTPS_CLIENT_ERROR_INIT_SEND_REQUEST: + ESP_LOGE(TAG, "Unable to send request to server"); + i32RetHTTPClient = HTTPS_CLIENT_ERROR; + break; + case HTTPS_CLIENT_OK: + ESP_LOGI(TAG, "HTTPS Client successfully initialized"); + i32RetHTTPClient = HTTPS_CLIENT_OK; + break; + default: + i32RetHTTPClient = HTTPS_CLIENT_ERROR; + ESP_LOGE(TAG, "Unknown error while init https client"); + break; + } + return i32RetHTTPClient; +} + +https_client_ret_t https_clientRetrieveData(char* pu8Data, uint32_t* pu32DataLenght, uint32_t* pu32BytesRead) +{ + https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; + int32_t i32RetRetrieveData = ESP_OK; + bool bRetriveData = true; + + bzero(pu8Data, *pu32DataLenght); + *pu32BytesRead = 0U; + + while (bRetriveData) + { + //Reading HTTP response + i32RetRetrieveData = mbedtls_ssl_read(&sHTTPS_ClientConfig.ssl, (unsigned char *)(pu8Data+(*pu32BytesRead)), ((*pu32DataLenght)-(*pu32BytesRead))); + + if(i32RetRetrieveData > 0) + { + //Data received + *pu32BytesRead = *pu32BytesRead + i32RetRetrieveData; + + if(*pu32DataLenght > 0) + { + //buffer not full yet --> read some more + bRetriveData = true; + } + else + { + //buffer full --> stop reading + bRetriveData = false; + } + } + + if(i32RetRetrieveData == 0) + { + //all data read --> stop reading + bRetriveData = false; + pu32BytesRead = 0; + } + + if(i32RetRetrieveData == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) + { + //connection is going to be closed + i32RetHTTPClient = HTTPS_CLIENT_ERROR; + bRetriveData = false; + } + } + return i32RetHTTPClient; +} + +https_client_ret_t https_clientDeinitialize() +{ + https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; + + i32RetHTTPClient = mbedtls_ssl_close_notify(&sHTTPS_ClientConfig.ssl); //close session + + if(i32RetHTTPClient != ESP_OK) + { + ESP_LOGE(TAG, "mbedtls_ssl_close_notify returned 0x%x", i32RetHTTPClient); + } + + mbedtls_ssl_session_reset(&sHTTPS_ClientConfig.ssl); //reset embedssl + mbedtls_net_free(&sHTTPS_ClientConfig.server_fd); //free ram + + return i32RetHTTPClient; +} + +https_client_ret_t https_clientInitEmbedTLS() { + + https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; + int32_t i32RetEmbedTLS = ESP_OK; + + mbedtls_ssl_init(&sHTTPS_ClientConfig.ssl); + mbedtls_x509_crt_init(&sHTTPS_ClientConfig.cacert); + mbedtls_ctr_drbg_init(&sHTTPS_ClientConfig.ctr_drbg); + mbedtls_ssl_config_init(&sHTTPS_ClientConfig.conf); + mbedtls_entropy_init(&sHTTPS_ClientConfig.entropy); + + i32RetEmbedTLS = mbedtls_ctr_drbg_seed(&sHTTPS_ClientConfig.ctr_drbg, mbedtls_entropy_func, &sHTTPS_ClientConfig.entropy, NULL, 0); + + if(i32RetEmbedTLS!= ESP_OK) + { + ESP_LOGE(TAG, "mbedtls_ctr_drbg_seed returned %d", i32RetEmbedTLS); + } + + if(i32RetEmbedTLS == ESP_OK) + { + //Attaching the certificate bundle + i32RetEmbedTLS = esp_crt_bundle_attach(&sHTTPS_ClientConfig.conf); + if(i32RetEmbedTLS != ESP_OK) + { + ESP_LOGE(TAG, "esp_crt_bundle_attach returned 0x%x\n\n", i32RetEmbedTLS); + } + } + + if(i32RetEmbedTLS == ESP_OK) + { + //Setting hostname for TLS session. + i32RetEmbedTLS = mbedtls_ssl_set_hostname(&sHTTPS_ClientConfig.ssl, CONFIG_OTA_HTTPS_SERVER_COMMON_NAME); + // Hostname set here should match CN in server certificate + if(i32RetEmbedTLS != ESP_OK) + { + ESP_LOGE(TAG, "mbedtls_ssl_set_hostname returned 0x%x", i32RetEmbedTLS); + } + } + if(i32RetEmbedTLS == ESP_OK) + { + //Setting up the SSL/TLS structure + i32RetEmbedTLS = mbedtls_ssl_config_defaults(&sHTTPS_ClientConfig.conf, + MBEDTLS_SSL_IS_CLIENT, + MBEDTLS_SSL_TRANSPORT_STREAM, + MBEDTLS_SSL_PRESET_DEFAULT); + + if(i32RetEmbedTLS != ESP_OK) + { + ESP_LOGE(TAG, "mbedtls_ssl_config_defaults returned %d", i32RetEmbedTLS); + } + } + + if(i32RetEmbedTLS == ESP_OK) + { + mbedtls_ssl_conf_authmode(&sHTTPS_ClientConfig.conf, MBEDTLS_SSL_VERIFY_REQUIRED); + mbedtls_ssl_conf_ca_chain(&sHTTPS_ClientConfig.conf, &sHTTPS_ClientConfig.cacert, NULL); + mbedtls_ssl_conf_rng(&sHTTPS_ClientConfig.conf, mbedtls_ctr_drbg_random, &sHTTPS_ClientConfig.ctr_drbg); + + i32RetEmbedTLS = mbedtls_ssl_setup(&sHTTPS_ClientConfig.ssl, &sHTTPS_ClientConfig.conf); + if(i32RetEmbedTLS != ESP_OK) + { + ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%x\n\n", i32RetEmbedTLS); + } + } + + if(i32RetEmbedTLS == ESP_OK) + { + mbedtls_net_init(&sHTTPS_ClientConfig.server_fd); + } + + if (i32RetEmbedTLS != ESP_OK) + { + i32RetHTTPClient = HTTPS_CLIENT_ERROR_INIT_EMBEDTLS; + } + + return i32RetHTTPClient; +} + +https_client_ret_t https_clientConnectToServer() +{ + https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; + int32_t i32RetServerConnect = ESP_OK; + + //Connecting to server + i32RetServerConnect = mbedtls_net_connect(&sHTTPS_ClientConfig.server_fd, CONFIG_OTA_HTTPS_SERVER_COMMON_NAME, CONFIG_OTA_HTTPS_SERVER_PORT, MBEDTLS_NET_PROTO_TCP); + if (i32RetServerConnect != ESP_OK) + { + ESP_LOGE(TAG, "mbedtls_net_connect returned %x", i32RetServerConnect); + } + + if(i32RetServerConnect == ESP_OK) + { + mbedtls_ssl_set_bio(&sHTTPS_ClientConfig.ssl, &sHTTPS_ClientConfig.server_fd, mbedtls_net_send, mbedtls_net_recv, NULL); + + //Performing the SSL/TLS handshake + while ((i32RetServerConnect = mbedtls_ssl_handshake(&sHTTPS_ClientConfig.ssl)) != 0) + { + if ((i32RetServerConnect != MBEDTLS_ERR_SSL_WANT_READ) && (i32RetServerConnect != MBEDTLS_ERR_SSL_WANT_WRITE)) + { + ESP_LOGE(TAG, "mbedtls_ssl_handshake returned 0x%x", i32RetServerConnect); + } + } + } + + if(i32RetServerConnect != ESP_OK) + { + i32RetHTTPClient = HTTPS_CLIENT_ERROR_INIT_CONNECT_TWO_SERVER; + } + return i32RetHTTPClient; +} + +https_client_ret_t https_clientValidateServer() +{ + https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; + int32_t i32RetValidateServer = ESP_OK; + + //Verifying peer X.509 certificate + if ((i32RetValidateServer = mbedtls_ssl_get_verify_result(&sHTTPS_ClientConfig.ssl)) != 0) + { + ESP_LOGE(TAG, "Failed to verify peer certificate!"); + } + + if(i32RetValidateServer != ESP_OK) + { + i32RetHTTPClient = HTTPS_CLIENT_ERROR_INIT_VALIDATE_SERVER; + } + return i32RetHTTPClient; +} + +https_client_ret_t https_clientSendRequest() +{ + https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; + int32_t i32RetSendRequest = ESP_OK; + uint32_t u32WrittenBytes = 0; + bool bWrite = true; //flag to stop loop + + //Writing HTTP request + while((u32WrittenBytes < strlen(REQUEST)) && bWrite) + { + i32RetSendRequest = mbedtls_ssl_write(&sHTTPS_ClientConfig.ssl, + (const unsigned char *)REQUEST + u32WrittenBytes, + strlen(REQUEST) - u32WrittenBytes); + if (i32RetSendRequest >= 0) + { + //bytes written + u32WrittenBytes += i32RetSendRequest; + } else if (i32RetSendRequest != MBEDTLS_ERR_SSL_WANT_WRITE && i32RetSendRequest != MBEDTLS_ERR_SSL_WANT_READ) { + ESP_LOGE(TAG, "mbedtls_ssl_write returned 0x%x", i32RetSendRequest); + bWrite = false; + } + } + + if(bWrite == false) + { + i32RetHTTPClient = HTTPS_CLIENT_ERROR_INIT_SEND_REQUEST; + } + return i32RetHTTPClient; +} diff --git a/components/mesh_ota/include/Mesh_network.h b/components/mesh_ota/include/Mesh_network.h index bb303c0..7c60526 100644 --- a/components/mesh_ota/include/Mesh_network.h +++ b/components/mesh_ota/include/Mesh_network.h @@ -47,9 +47,10 @@ #endif -struct mesh_packet +struct meshPacket { - enum ota_mesh_packet_type { + enum otaMeshPacketType + { APP_Data, //data for application OTA_Version_Request, //send own version in payload OTA_Version_Respone, //send own version in payload @@ -60,7 +61,7 @@ struct mesh_packet uint8_t au8Payload[1024]; }; -typedef struct mesh_packet MESH_PACKET_t; +typedef struct meshPacket MESH_PACKET_t; extern bool bIsMeshConnected; extern int32_t i32MeshLayer; diff --git a/components/mesh_ota/include/https_client.h b/components/mesh_ota/include/https_client.h new file mode 100644 index 0000000..60d192b --- /dev/null +++ b/components/mesh_ota/include/https_client.h @@ -0,0 +1,75 @@ +#ifndef H_HTTPS_CLIENT +#define H_HTTPS_CLIENT + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_log.h" +#include "esp_system.h" +#include "nvs_flash.h" +#include "esp_netif.h" + +#include "lwip/err.h" +#include "lwip/sockets.h" +#include "lwip/sys.h" +#include "lwip/netdb.h" +#include "lwip/dns.h" + +#include "mbedtls/platform.h" +#include "mbedtls/net_sockets.h" +#include "mbedtls/esp_debug.h" +#include "mbedtls/ssl.h" +#include "mbedtls/entropy.h" +#include "mbedtls/ctr_drbg.h" +#include "mbedtls/error.h" +#include "mbedtls/certs.h" +#include "esp_crt_bundle.h" + +#ifndef CONFIG_OTA_HTTPS_URL +#define CONFIG_OTA_HTTPS_URL "https://exmaple.com/theImage.bin" +#endif + +#ifndef CONFIG_OTA_HTTPS_SERVER_PORT +#define CONFIG_OTA_HTTPS_SERVER_PORT "443" +#endif + +#ifndef CONFIG_OTA_HTTPS_AUTH +#define CONFIG_OTA_HTTPS_AUTH "base64(user:password)" +#endif + +#ifndef CONFIG_OTA_HTTPS_SERVER_COMMON_NAME +#define CONFIG_OTA_HTTPS_SERVER_COMMON_NAME "exmaple.com" +#endif + +#define HTTPS_CLIENT_OK 0 +#define HTTPS_CLIENT_ERROR -1 +#define HTTPS_CLIENT_ERROR_INIT_EMBEDTLS -2 +#define HTTPS_CLIENT_ERROR_INIT_CONNECT_TWO_SERVER -3 +#define HTTPS_CLIENT_ERROR_INIT_VALIDATE_SERVER -4 +#define HTTPS_CLIENT_ERROR_INIT_SEND_REQUEST -5 + +struct HTTPS_Client +{ + mbedtls_entropy_context entropy; + mbedtls_ctr_drbg_context ctr_drbg; + mbedtls_ssl_context ssl; + mbedtls_x509_crt cacert; + mbedtls_ssl_config conf; + mbedtls_net_context server_fd; +}; + +typedef int32_t https_client_ret_t; +typedef struct HTTPS_Client HTTPS_Client_t; + +https_client_ret_t https_clientInitialize(); +https_client_ret_t https_clientRetrieveData(char* pu8Data, uint32_t* pu32DataLenght, uint32_t* pu32BytesRead); +https_client_ret_t https_clientDeinitialize(); + +#endif /* H_HTTPS_CLIENT */ + + + + diff --git a/main/Blinky_LED.c b/main/Blinky_LED.c new file mode 100644 index 0000000..95e262c --- /dev/null +++ b/main/Blinky_LED.c @@ -0,0 +1,124 @@ + + +#include "Blinky_LED.h" + +static const char *LOG_TAG = "blinky_led"; +static bool bLEDisOn = true; + +xQueueHandle queueBlinkyLEDPackets; + +esp_err_t errBlinkyLEDInitialize() +{ + esp_err_t err = ESP_OK; + BaseType_t xReturned; + + vGPIOInitialize(); + + 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; + } + + if(err == ESP_OK) + { + xReturned = xTaskCreate(vTaskReadUserInput, "vTaskReadUserInput", 2048, NULL, 5, NULL); + if(xReturned != pdPASS) + { + err = ESP_FAIL; + } + } + + if(err == ESP_OK) + { + xReturned = xTaskCreate(vTaskReceiveData, "vTaskReceiveData", 2048, NULL, 5, NULL); + if(xReturned != pdPASS) + { + err = ESP_FAIL; + } + } + return err; +} + +void vGPIOInitialize() +{ + gpio_config_t gpioConf; + + //LED as Output + gpio_reset_pin(GPIO_LED); + gpio_set_direction(GPIO_LED, GPIO_MODE_OUTPUT); + + //BTN as Input + gpioConf.intr_type = GPIO_INTR_DISABLE; + gpioConf.mode = GPIO_MODE_INPUT; + gpioConf.pin_bit_mask = GPIO_INPUT_PIN_SEL; + gpioConf.pull_down_en = 0; + gpioConf.pull_up_en = 1; + gpio_config(&gpioConf); +} + +void vTaskReadUserInput(void *arg) +{ + BLINKY_PACKET_t bTmpStateLED = LED_OFF; + while(true) + { + if(gpio_get_level(GPIO_BOOT_BTN) == 0) + { + if(bLEDisOn == false) + { + ESP_LOGI(LOG_TAG,"switch ON"); + bTmpStateLED = LED_ON; + } + else + { + ESP_LOGI(LOG_TAG,"switch OFF"); + bTmpStateLED = LED_OFF; + } + + if (xQueueSend(queueBlinkyLEDPackets, &bTmpStateLED, portMAX_DELAY) != pdPASS) + { + ESP_LOGE(LOG_TAG, "Unable to push packet into Queue"); + } + vTaskDelay(200 / portTICK_PERIOD_MS); + } + vTaskDelay(50 / portTICK_PERIOD_MS); + } +} + +void vTaskReceiveData(void *arg) +{ + BLINKY_PACKET_t bTmpStateLED = LED_OFF; + + while (1) + { + if (xQueueReceive(queueBlinkyLEDPackets, &bTmpStateLED, portMAX_DELAY) != pdTRUE) + { + ESP_LOGE(LOG_TAG, "Unable to receive packet from Queue"); + } + else + { + //Successfully RECEIVED the packet + switch (bTmpStateLED) + { + case LED_ON: + bLEDisOn = true; + gpio_set_level(GPIO_LED, 1); //switch on + ESP_LOGI(LOG_TAG,"rec ON"); + break; + + case LED_OFF: + bLEDisOn = false; + gpio_set_level(GPIO_LED, 0); //switch off + ESP_LOGI(LOG_TAG,"rec OFF"); + break; + + default: + bLEDisOn = false; + gpio_set_level(GPIO_LED, 0); //switch off + break; + } + } + vTaskDelay(200 / portTICK_PERIOD_MS); + } +} \ No newline at end of file diff --git a/main/Blinky_LED.h b/main/Blinky_LED.h new file mode 100644 index 0000000..ab6b5e2 --- /dev/null +++ b/main/Blinky_LED.h @@ -0,0 +1,35 @@ +#ifndef H_BLINKY_LED +#define H_BLINKY_LED + +#include +#include "esp_wifi.h" +#include "esp_system.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "driver/gpio.h" + +#include "Mesh_OTA.h" + +#define GPIO_BOOT_BTN 0 +#define GPIO_LED 2 + +#define GPIO_INPUT_PIN_SEL (1ULL<