26 changed files with 5511 additions and 801 deletions
@ -0,0 +1,355 @@
|
||||
/**
|
||||
* @file HTTPS_Client.c |
||||
* @brief Used to download the OTA image from the server |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* Additional Infos: Connects via HTTPS and HTTPS Basic Auth to the Server. |
||||
* Downloads the image in segments |
||||
*/ |
||||
|
||||
#include "HTTPS_Client.h" |
||||
|
||||
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" |
||||
"Authorization: Basic " CONFIG_OTA_HTTPS_AUTH "\r\n" |
||||
"\r\n"; |
||||
|
||||
|
||||
static HTTPS_Client_t sHTTPS_ClientConfig; |
||||
|
||||
https_client_ret_t https_clientInitEmbedTLS(void); |
||||
https_client_ret_t errHTTPSClientConnectToServer(void); |
||||
https_client_ret_t errHTTPSClientValidateServer(void); |
||||
https_client_ret_t errHTTPSClientSendRequest(void); |
||||
|
||||
/**
|
||||
* @fn https_client_ret_t errHTTPSClientInitialize(void) |
||||
* @brief Initialize the client |
||||
* @param void |
||||
* @return HTTPS_Client error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* Initialize embedTLS |
||||
*/ |
||||
https_client_ret_t errHTTPSClientInitialize(void) |
||||
{ |
||||
https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; |
||||
|
||||
i32RetHTTPClient = https_clientInitEmbedTLS(); |
||||
|
||||
if (i32RetHTTPClient == HTTPS_CLIENT_ERROR_INIT_EMBEDTLS) |
||||
{ |
||||
ESP_LOGE(TAG, "Unable to initialize EmbedTLS"); |
||||
i32RetHTTPClient = HTTPS_CLIENT_ERROR; |
||||
} |
||||
return i32RetHTTPClient; |
||||
} |
||||
|
||||
/**
|
||||
* @fn https_client_ret_t errHTTPSClientRetrieveData(char* const cpu8Data, const uint32_t* const cpcu32DataLenght, uint32_t* pu32BytesRead) |
||||
* @brief receive a image segment from server |
||||
* @param cpu8Data data buffer |
||||
* @param cpcu32DataLenght desired byte amount |
||||
* @param pu32BytesRead actual received byte amount |
||||
* @return HTTPS_Client error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* Read segement and handle all events like EOF or timeout |
||||
*/ |
||||
https_client_ret_t errHTTPSClientRetrieveData(char* const cpu8Data, const uint32_t* const cpcu32DataLenght, uint32_t* pu32BytesRead) |
||||
{ |
||||
https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; |
||||
int32_t i32RetRetrieveData = ESP_OK; |
||||
bool bRetriveData = true; |
||||
|
||||
bzero(cpu8Data, *cpcu32DataLenght); |
||||
(*pu32BytesRead) = 0U; |
||||
|
||||
while (bRetriveData) |
||||
{ |
||||
mbedtls_ssl_conf_read_timeout(&sHTTPS_ClientConfig.conf, HTTPS_READ_TIMEOUT); //set timeout
|
||||
//Reading HTTP response
|
||||
i32RetRetrieveData = mbedtls_ssl_read(&sHTTPS_ClientConfig.ssl, (unsigned char *)(cpu8Data+(*pu32BytesRead)), ((*cpcu32DataLenght)-(*pu32BytesRead))); |
||||
|
||||
if(i32RetRetrieveData > 0) |
||||
{ |
||||
//Data received
|
||||
*pu32BytesRead = *pu32BytesRead + i32RetRetrieveData; |
||||
|
||||
if(*cpcu32DataLenght > 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_TIMEOUT ) |
||||
{ |
||||
//timeout --> stop reading
|
||||
bRetriveData = false; |
||||
} |
||||
|
||||
if(i32RetRetrieveData == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) |
||||
{ |
||||
//connection is going to be closed
|
||||
i32RetHTTPClient = HTTPS_CLIENT_ERROR; |
||||
bRetriveData = false; |
||||
} |
||||
} |
||||
return i32RetHTTPClient; |
||||
} |
||||
|
||||
/**
|
||||
* @fn https_client_ret_t errHTTPSClientReset(void) |
||||
* @brief reset client for next receive of image |
||||
* @param void |
||||
* @return HTTPS_Client error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* reset session |
||||
*/ |
||||
https_client_ret_t errHTTPSClientReset(void) |
||||
{ |
||||
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; |
||||
} |
||||
|
||||
/**
|
||||
* @fn https_client_ret_t https_clientInitEmbedTLS(void) |
||||
* @brief init embedTLS |
||||
* @param void |
||||
* @return HTTPS_Client error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* attach certs for tls |
||||
*/ |
||||
https_client_ret_t https_clientInitEmbedTLS(void) |
||||
{ |
||||
https_client_ret_t i32RetHTTPClient = HTTPS_CLIENT_OK; |
||||
int32_t i32RetEmbedTLS = ESP_OK; |
||||
static bool bAlreadySetup = false; |
||||
|
||||
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); |
||||
|
||||
if (bAlreadySetup == false) //check if mbedtls_ssl_setup was called before
|
||||
{ |
||||
i32RetEmbedTLS = mbedtls_ssl_setup(&sHTTPS_ClientConfig.ssl, &sHTTPS_ClientConfig.conf); //call this only once
|
||||
if(i32RetEmbedTLS != ESP_OK) |
||||
{ |
||||
ESP_LOGE(TAG, "mbedtls_ssl_setup returned 0x%x\n", i32RetEmbedTLS); |
||||
} |
||||
else |
||||
{ |
||||
bAlreadySetup = true; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if(i32RetEmbedTLS == ESP_OK) |
||||
{ |
||||
mbedtls_net_init(&sHTTPS_ClientConfig.server_fd); |
||||
} |
||||
|
||||
if (i32RetEmbedTLS != ESP_OK) |
||||
{ |
||||
i32RetHTTPClient = HTTPS_CLIENT_ERROR_INIT_EMBEDTLS; |
||||
} |
||||
|
||||
return i32RetHTTPClient; |
||||
} |
||||
|
||||
/**
|
||||
* @fn https_client_ret_t errHTTPSClientConnectToServer(void) |
||||
* @brief connect to server |
||||
* @param void |
||||
* @return HTTPS_Client error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* open TLS session |
||||
*/ |
||||
https_client_ret_t errHTTPSClientConnectToServer(void) |
||||
{ |
||||
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, mbedtls_net_recv_timeout); |
||||
|
||||
//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; |
||||
} |
||||
|
||||
/**
|
||||
* @fn https_client_ret_t errHTTPSClientValidateServer(void) |
||||
* @brief validate server |
||||
* @param void |
||||
* @return HTTPS_Client error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* check CDN and cert |
||||
*/ |
||||
https_client_ret_t errHTTPSClientValidateServer(void) |
||||
{ |
||||
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; |
||||
} |
||||
|
||||
/**
|
||||
* @fn https_client_ret_t errHTTPSClientSendRequest(void) |
||||
* @brief send request to server |
||||
* @param void |
||||
* @return HTTPS_Client error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* send HTTP GET request |
||||
*/ |
||||
https_client_ret_t errHTTPSClientSendRequest(void) |
||||
{ |
||||
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; |
||||
} |
@ -0,0 +1,495 @@
|
||||
/**
|
||||
* @file Mesh_Network.c |
||||
* @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. |
||||
*/ |
||||
|
||||
#include "Mesh_Network.h" |
||||
|
||||
static const char *LOG_TAG = "mesh_network"; |
||||
|
||||
//w: errMeshNetworkInitialize
|
||||
//r: errMeshNetworkInitialize;vMeshNetworkGetOwnAddr;errMeshNetworkGetChildren
|
||||
uint8_t u8ownMAC[6]; |
||||
|
||||
//w: errMeshNetworkInitialize; vMeshNetworkMeshEventHandler
|
||||
//r: vMeshNetworkMeshEventHandler
|
||||
esp_netif_t* pNetifSta; |
||||
|
||||
//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)(const uint8_t* const, const uint8_t* const); |
||||
void (*pOTAChildConnectHandle)(const uint8_t* const); |
||||
void (*pOTAMessageHandle)(const MESH_PACKET_t* const); |
||||
void (*pChangeStateOfServerWorkerHandle)(const bool ); |
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkInitialize() |
||||
* @brief Starts the mesh network |
||||
* @param void |
||||
* @return ESP32 error code |
||||
* @author Hendrik Schutter, init based in ESP32-IDE code |
||||
* @date 20.01.2021 |
||||
* |
||||
* Initialize the network |
||||
*/ |
||||
esp_err_t errMeshNetworkInitialize(void) |
||||
{ |
||||
//init module variables
|
||||
esp_err_t err; |
||||
bIsMeshConnected = false; |
||||
i32MeshLayer = -1; |
||||
pNetifSta = NULL; |
||||
|
||||
err = nvs_flash_init(); //init non-volatile storage
|
||||
|
||||
#ifdef ERASE_NVS |
||||
if(err == ESP_ERR_NVS_NO_FREE_PAGES) //check if storage is full
|
||||
{ |
||||
ERROR_CHECK(nvs_flash_erase()); |
||||
} |
||||
#endif |
||||
|
||||
// tcpip initialization
|
||||
ERROR_CHECK(esp_netif_init()); |
||||
|
||||
//event initialization
|
||||
ERROR_CHECK(esp_event_loop_create_default()); |
||||
|
||||
//create network interfaces for mesh (only station instance saved for further manipulation, soft AP instance ignored
|
||||
ERROR_CHECK(esp_netif_create_default_wifi_mesh_netifs(&pNetifSta, NULL)); |
||||
|
||||
//wifi initialization
|
||||
ERROR_CHECK(errMeshNetworkInitializeWiFi()); |
||||
|
||||
//mesh initialization
|
||||
ERROR_CHECK(esp_mesh_init()); |
||||
|
||||
//mesh initialization
|
||||
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)); |
||||
|
||||
//set mesh max layer according to the topology
|
||||
ERROR_CHECK(esp_mesh_set_max_layer(CONFIG_MESH_MAX_LAYER)); |
||||
ERROR_CHECK(esp_mesh_set_vote_percentage(1)); |
||||
ERROR_CHECK(esp_mesh_set_xon_qsize(128)); |
||||
|
||||
//Disable mesh PS function
|
||||
ERROR_CHECK(esp_mesh_disable_ps()); |
||||
ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10)); |
||||
|
||||
mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT(); |
||||
|
||||
/* mesh ID */ |
||||
memcpy((uint8_t *) &cfg.mesh_id, CONFIG_MESH_ID, 6); |
||||
|
||||
ERROR_CHECK(errMeshNetworkInitializeRouter(&cfg)); |
||||
|
||||
/* mesh softAP */ |
||||
ERROR_CHECK(esp_mesh_set_ap_authmode(CONFIG_MESH_AP_AUTHMODE)); |
||||
cfg.mesh_ap.max_connection = CONFIG_MESH_AP_CONNECTIONS; |
||||
memcpy((uint8_t *) &cfg.mesh_ap.password, CONFIG_MESH_AP_PASSWD, |
||||
strlen(CONFIG_MESH_AP_PASSWD)); |
||||
ERROR_CHECK(esp_mesh_set_config(&cfg)); |
||||
|
||||
/* mesh start */ |
||||
ERROR_CHECK(esp_mesh_start()); |
||||
|
||||
ERROR_CHECK(esp_base_mac_addr_get(u8ownMAC)) |
||||
|
||||
//debug info
|
||||
ESP_LOGD(LOG_TAG, "mesh starts successfully, heap:%d, %s<%d>%s, ps:%d\n", esp_get_minimum_free_heap_size(), |
||||
esp_mesh_is_root_fixed() ? "root fixed" : "root not fixed", |
||||
esp_mesh_get_topology(), esp_mesh_get_topology() ? "(chain)":"(tree)", esp_mesh_is_ps_enabled()); |
||||
|
||||
|
||||
ESP_LOGI(LOG_TAG, "Node MAC: \"%x:%x:%x:%x:%x:%x\" ", u8ownMAC[0], u8ownMAC[1], u8ownMAC[2], u8ownMAC[3], u8ownMAC[4], u8ownMAC[5]); |
||||
|
||||
return ESP_OK; |
||||
} |
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkInitializeWiFi() |
||||
* @brief Starts the WiFI |
||||
* @param void |
||||
* @return ESP32 error code |
||||
* @author Hendrik Schutter, init based in ESP32-IDE code |
||||
* @date 20.01.2021 |
||||
* |
||||
* start the wifi |
||||
*/ |
||||
esp_err_t errMeshNetworkInitializeWiFi() |
||||
{ |
||||
//wifi initialization
|
||||
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, &vMeshNetworkIpEventHandler, NULL)); |
||||
ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_FLASH)); |
||||
ERROR_CHECK(esp_wifi_start()); |
||||
return err; |
||||
} |
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkInitializeRouter(mesh_cfg_t* cfg) |
||||
* @brief Starts the router |
||||
* @param cfg router config |
||||
* @return ESP32 error code |
||||
* @author Hendrik Schutter, init based in ESP32-IDE code |
||||
* @date 20.01.2021 |
||||
* |
||||
* Initialize the network |
||||
*/ |
||||
esp_err_t errMeshNetworkInitializeRouter(mesh_cfg_t* cfg) |
||||
{ |
||||
//router initialization
|
||||
esp_err_t err = ESP_OK; |
||||
(*cfg).channel = CONFIG_MESH_CHANNEL; |
||||
(*cfg).router.ssid_len = strlen(CONFIG_MESH_ROUTER_SSID); |
||||
memcpy((uint8_t *) &(*cfg).router.ssid, CONFIG_MESH_ROUTER_SSID, (*cfg).router.ssid_len); |
||||
memcpy((uint8_t *) &(*cfg).router.password, CONFIG_MESH_ROUTER_PASSWD, |
||||
strlen(CONFIG_MESH_ROUTER_PASSWD)); |
||||
return err; |
||||
} |
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkSetChildConnectedHandle(void (*pChildConnectHandleTmp)(const uint8_t* const cpcu8Data)) |
||||
* @brief set callback for event when child connects |
||||
* @param (*pChildConnectHandleTmp)(const uint8_t* const cpcu8Data) function pointer |
||||
* @return ESP32 error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
*/ |
||||
esp_err_t errMeshNetworkSetChildConnectedHandle(void (*pChildConnectHandleTmp)(const uint8_t* const cpcu8Data)) |
||||
{ |
||||
pOTAChildConnectHandle = pChildConnectHandleTmp; |
||||
return ESP_OK; |
||||
} |
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkSetAppReceiveHandle(void (*pAppRxHandleTmp)(const uint8_t* const cpcu8Data, const uint8_t* const pu8Sender)) |
||||
* @brief set callback for event when application data is received |
||||
* @param (*pAppRxHandleTmp)(const uint8_t* const cpcu8Data, const uint8_t* const pu8Sender) function pointer |
||||
* @return ESP32 error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
*/ |
||||
esp_err_t errMeshNetworkSetAppReceiveHandle(void (*pAppRxHandleTmp)(const uint8_t* const cpcu8Data, const uint8_t* const pu8Sender)) |
||||
{ |
||||
pAppRxHandle = pAppRxHandleTmp; //set handle from app as receive handle if an app packet is received
|
||||
return ESP_OK; |
||||
} |
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkSetOTAMessageHandleHandle(void (*pOTAMessageHandleTmp)(const MESH_PACKET_t* const cpcuMeshPacket)) |
||||
* @brief set callback for event when OTA message is received |
||||
* @param (*pOTAMessageHandleTmp)(const MESH_PACKET_t* const cpcuMeshPacket) function pointer |
||||
* @return ESP32 error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
*/ |
||||
esp_err_t errMeshNetworkSetOTAMessageHandleHandle(void (*pOTAMessageHandleTmp)(const MESH_PACKET_t* const cpcuMeshPacket)) |
||||
{ |
||||
pOTAMessageHandle = pOTAMessageHandleTmp; |
||||
return ESP_OK; |
||||
} |
||||
|
||||
/**
|
||||
* @fn esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOfServerWorkerHandleTmp)(const bool cbState)) |
||||
* @brief set callback for event when connectify to server is changed |
||||
* @param (*pChangeStateOfServerWorkerHandleTmp)(const bool cbState) function pointer |
||||
* @return ESP32 error code |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
*/ |
||||
esp_err_t errMeshNetworkSetChangeStateOfServerWorkerHandle(void (*pChangeStateOfServerWorkerHandleTmp)(const bool cbState)) |
||||
{ |
||||
pChangeStateOfServerWorkerHandle = pChangeStateOfServerWorkerHandleTmp; |
||||
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; |
||||
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 *)cpcPacket, sizeof(MESH_PACKET_t)); |
||||
|
||||
err = esp_mesh_send(cpcAddrDest, &data, MESH_DATA_P2P, NULL, 0); |
||||
|
||||
return err; |
||||
} |
||||
|
||||
/**
|
||||
* @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; |
||||
bool bReturn = false; |
||||
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
|
||||
|
||||
err = errMeshNetworkGetParentNode(&addrParent); |
||||
|
||||
if(err == ESP_OK) |
||||
{ |
||||
if(bMeshNetworkCheckMacEquality(cpcNode->addr, addrParent.addr) == true) |
||||
{ |
||||
bReturn = true; //node was found
|
||||
} |
||||
} |
||||
|
||||
if(bReturn == false) |
||||
{ |
||||
err = ESP_OK; //reset error code
|
||||
|
||||
ERROR_CHECK(errMeshNetworkGetChildren(childrenAddr, &u16ChildrenSize)); //get all children
|
||||
|
||||
for (uint16_t u16Index = 0; ((u16Index < u16ChildrenSize) && (err == ESP_OK) && (bReturn == false)); u16Index++) |
||||
{ |
||||
if(bMeshNetworkCheckMacEquality(cpcNode->addr, childrenAddr[u16Index].addr) == true) |
||||
{ |
||||
bReturn = true; //node was found
|
||||
} |
||||
} |
||||
} |
||||
return bReturn; |
||||
} |
||||
|
||||
|
||||
/**
|
||||
* @fn bool bMeshNetworkCheckMacEquality(const uint8_t* const cpcu8aMAC, const uint8_t* const cpcu8bMAC) |
||||
* @brief returns true if MAC address is equal |
||||
* @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)) |
||||
{ |
||||
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; |
||||
} |
||||
|
||||
/**
|
||||
* @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; |
||||
|
||||
xReturned = xTaskCreate(vMeshNetworkTaskReceiveMeshData, "ReceiveMeshData", 7000, NULL, 5, NULL); |
||||
|
||||
if(xReturned != pdPASS) |
||||
{ |
||||
err = ESP_FAIL; |
||||
} |
||||
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; |
||||
int route_table_size = 0; |
||||
*cpu16ChildrenSize = 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(! (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; |
||||
} |
||||
} |
||||
} |
||||
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; |
||||
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; |
||||
|
||||
while (true) |
||||
{ |
||||
data.size = CONFIG_MESH_MESSAGE_SIZE; |
||||
err = esp_mesh_recv(&from, &data, portMAX_DELAY, &flag, NULL, 0); |
||||
if (err != ESP_OK || !data.size) |
||||
{ |
||||
ESP_LOGE(LOG_TAG, "err:0x%x, size:%d", err, data.size); |
||||
continue; |
||||
} |
||||
MESH_PACKET_t packet; |
||||
memcpy(&packet, (uint8_t *)rx_buf, sizeof(MESH_PACKET_t)); //parse MESH_PACKET_t
|
||||
memcpy(&packet.meshSenderAddr, &from, sizeof(mesh_addr_t)); //copy sender into packet
|
||||
|
||||
switch (packet.type) |
||||
{ |
||||
case APP_Data: |
||||
ESP_LOGD(LOG_TAG, "recv: APP_Data"); |
||||
//call the rx handle from app
|
||||
pAppRxHandle(packet.au8Payload, from.addr); //hand over payload and sender of this mesh packet
|
||||
break; |
||||
case OTA_Version_Request: |
||||
case OTA_Version_Response: |
||||
case OTA_Data: |
||||
case OTA_ACK: |
||||
case OTA_Complete: |
||||
case OTA_Abort: |
||||
//call the rx handle from OTA
|
||||
if(pOTAMessageHandle) |
||||
{ |
||||
pOTAMessageHandle(&packet); |
||||
} |
||||
break; |
||||
default: |
||||
ESP_LOGE(LOG_TAG, "recv: something"); |
||||
break; |
||||
}//end switch
|
||||
} //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; |
||||
|
||||
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(cpMeshParentAddr, &meshParentAddr, sizeof(mesh_addr_t)); |
||||
} |
||||
return err; |
||||
} |
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,261 @@
|
||||
|
||||
/**
|
||||
* @file Mesh_Network_Handler.c |
||||
* @brief Handler for events from mesh network (no messages) |
||||
* @author Hendrik Schutter |
||||
* @date 20.01.2021 |
||||
* |
||||
* Additional Infos: IP event received or parrent or child connected or disconnected |
||||
*/ |
||||
|
||||
#include "Mesh_Network.h" |
||||
|
||||
static const char *LOG_TAG = "mesh_network_handler"; |
||||
|
||||
/**
|
||||
* @fn void vMeshNetworkIpEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void *vpEventData) |
||||
* @brief received an IP event |
||||
* @param arg |
||||
* @param event_base |
||||
* @param i32EventID |
||||
* @param vpEventData |
||||
* @return void |
||||
* @author ESP-IDF |
||||
* @date 20.01.2021 |
||||
*/ |
||||
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)); |
||||
if(pChangeStateOfServerWorkerHandle) |
||||
{ |
||||
pChangeStateOfServerWorkerHandle(true); //signal that this node (root node) has access to internet
|
||||
} |
||||
} |
||||
|
||||
/**
|
||||
* @fn void vMeshNetworkMeshEventHandler(void *arg, esp_event_base_t event_base, int32_t i32EventID, void* vpEventData) |
||||
* @brief received an mesh event |
||||
* @param arg |
||||
* @param event_base |
||||
* @param i32EventID |
||||
* @param vpEventData |
||||
* @return void |
||||
* @author ESP-IDF |
||||
* @date 20.01.2021 |
||||
*/ |
||||
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; |
||||
|
||||
switch (i32EventID) |
||||
{ |
||||
case MESH_EVENT_STARTED: |
||||
{ |
||||
esp_mesh_get_id(&id); |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_MESH_STARTED>ID:"MACSTR"", MAC2STR(id.addr)); |
||||
bIsMeshConnected = false; |
||||
i32MeshLayer = esp_mesh_get_layer(); |
||||
} |
||||
break; |
||||
case MESH_EVENT_STOPPED:
|
||||
{ |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_STOPPED>"); |
||||
bIsMeshConnected = false; |
||||
i32MeshLayer = esp_mesh_get_layer(); |
||||
} |
||||
break; |
||||
case MESH_EVENT_CHILD_CONNECTED:
|
||||
{ |
||||
mesh_event_child_connected_t *child_connected = (mesh_event_child_connected_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_CHILD_CONNECTED>aid:%d, "MACSTR"", child_connected->aid, MAC2STR(child_connected->mac)); |
||||
|
||||
if(pOTAChildConnectHandle){pOTAChildConnectHandle(child_connected->mac);}//add this child to queue using handle
|
||||
|
||||
} |
||||
break; |
||||
case MESH_EVENT_CHILD_DISCONNECTED:
|
||||
{ |
||||
mesh_event_child_disconnected_t *child_disconnected = (mesh_event_child_disconnected_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_CHILD_DISCONNECTED>aid:%d, "MACSTR"", |
||||
child_disconnected->aid, |
||||
MAC2STR(child_disconnected->mac)); |
||||
} |
||||
break; |
||||
case MESH_EVENT_ROUTING_TABLE_ADD:
|
||||
{ |
||||
mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)vpEventData; |
||||
ESP_LOGW(LOG_TAG, "<MESH_EVENT_ROUTING_TABLE_ADD>add %d, new:%d, layer:%d", |
||||
routing_table->rt_size_change, |
||||
routing_table->rt_size_new, i32MeshLayer); |
||||
} |
||||
break; |
||||
case MESH_EVENT_ROUTING_TABLE_REMOVE:
|
||||
{
|
||||
mesh_event_routing_table_change_t *routing_table = (mesh_event_routing_table_change_t *)vpEventData; |
||||
ESP_LOGW(LOG_TAG, "<MESH_EVENT_ROUTING_TABLE_REMOVE>remove %d, new:%d, layer:%d", |
||||
routing_table->rt_size_change, |
||||
routing_table->rt_size_new, i32MeshLayer); |
||||
} |
||||
break; |
||||
case MESH_EVENT_NO_PARENT_FOUND:
|
||||
{ |
||||
mesh_event_no_parent_found_t *no_parent = (mesh_event_no_parent_found_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_NO_PARENT_FOUND>scan times:%d", |
||||
no_parent->scan_times); |
||||
/* TODO handler for the failure, maybe nominate themselves */ |
||||
} |
||||
break; |
||||
case MESH_EVENT_PARENT_CONNECTED:
|
||||
{ |
||||
mesh_event_connected_t *connected = (mesh_event_connected_t *)vpEventData; |
||||
esp_mesh_get_id(&id); |
||||
i32MeshLayer = connected->self_layer; |
||||
memcpy(&meshParentAddr.addr, connected->connected.bssid, 6);
|
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_PARENT_CONNECTED>layer:%d-->%d, parent:"MACSTR"%s, ID:"MACSTR", duty:%d", |
||||
last_layer, i32MeshLayer, MAC2STR(meshParentAddr.addr), |
||||
esp_mesh_is_root() ? "<ROOT>" : (i32MeshLayer == 2) ? "<layer2>" : "", //print own node title
|
||||
MAC2STR(id.addr), connected->duty); |
||||
last_layer = i32MeshLayer; |
||||
bIsMeshConnected = true; |
||||
if (esp_mesh_is_root())
|
||||
{ |
||||
if(esp_netif_dhcpc_start(pNetifSta) == ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) //get a IP from router
|
||||
{ |
||||
if(pChangeStateOfServerWorkerHandle){pChangeStateOfServerWorkerHandle(true);}// signal reconnect
|
||||
}
|
||||
} |
||||
errMeshNetworkStartReceiveTask();//start receiving
|
||||
} |
||||
break; |
||||
case MESH_EVENT_PARENT_DISCONNECTED:
|
||||
{ |
||||
mesh_event_disconnected_t *disconnected = (mesh_event_disconnected_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_PARENT_DISCONNECTED>reason:%d", disconnected->reason); |
||||
bIsMeshConnected = false; |
||||
if(pChangeStateOfServerWorkerHandle){pChangeStateOfServerWorkerHandle(false);} |
||||
i32MeshLayer = esp_mesh_get_layer(); |
||||
} |
||||
break; |
||||
case MESH_EVENT_LAYER_CHANGE:
|
||||
{ |
||||
mesh_event_layer_change_t *layer_change = (mesh_event_layer_change_t *)vpEventData; |
||||
i32MeshLayer = layer_change->new_layer; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_LAYER_CHANGE>layer:%d-->%d%s", |
||||
last_layer, i32MeshLayer, |
||||
esp_mesh_is_root() ? "<ROOT>" : (i32MeshLayer == 2) ? "<layer2>" : ""); |
||||
last_layer = i32MeshLayer; |
||||
} |
||||
break; |
||||
case MESH_EVENT_ROOT_ADDRESS:
|
||||
{ |
||||
mesh_event_root_address_t *root_addr = (mesh_event_root_address_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_ROOT_ADDRESS>root address:"MACSTR"", |
||||
MAC2STR(root_addr->addr)); |
||||
} |
||||
break; |
||||
case MESH_EVENT_VOTE_STARTED:
|
||||
{ |
||||
mesh_event_vote_started_t *vote_started = (mesh_event_vote_started_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_VOTE_STARTED>attempts:%d, reason:%d, rc_addr:"MACSTR"", |
||||
vote_started->attempts, |
||||
vote_started->reason, |
||||
MAC2STR(vote_started->rc_addr.addr)); |
||||
} |
||||
break; |
||||
case MESH_EVENT_VOTE_STOPPED:
|
||||
{ |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_VOTE_STOPPED>"); |
||||
} |
||||
break; |
||||
case MESH_EVENT_ROOT_SWITCH_REQ:
|
||||
{ |
||||
mesh_event_root_switch_req_t *switch_req = (mesh_event_root_switch_req_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_ROOT_SWITCH_REQ>reason:%d, rc_addr:"MACSTR"", switch_req->reason, |
||||
MAC2STR( switch_req->rc_addr.addr)); |
||||
} |
||||
break; |
||||
case MESH_EVENT_ROOT_SWITCH_ACK:
|
||||
{ |
||||
//new root
|
||||
i32MeshLayer = esp_mesh_get_layer(); |
||||
esp_mesh_get_parent_bssid(&meshParentAddr); |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_ROOT_SWITCH_ACK>layer:%d, parent:"MACSTR"", i32MeshLayer, MAC2STR(meshParentAddr.addr)); |
||||
} |
||||
break; |
||||
case MESH_EVENT_TODS_STATE:
|
||||
{ |
||||
mesh_event_toDS_state_t *toDs_state = (mesh_event_toDS_state_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_TODS_REACHABLE>state:%d", *toDs_state); |
||||
} |
||||
break; |
||||
case MESH_EVENT_ROOT_FIXED:
|
||||
{ |
||||
mesh_event_root_fixed_t *root_fixed = (mesh_event_root_fixed_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_ROOT_FIXED>%s", |
||||
root_fixed->is_fixed ? "fixed" : "not fixed"); |
||||
} |
||||
break; |
||||
case MESH_EVENT_ROOT_ASKED_YIELD:
|
||||
{ |
||||
mesh_event_root_conflict_t *root_conflict = (mesh_event_root_conflict_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_ROOT_ASKED_YIELD>"MACSTR", rssi:%d, capacity:%d", |
||||
MAC2STR(root_conflict->addr), root_conflict->rssi, root_conflict->capacity); |
||||
} |
||||
break; |
||||
case MESH_EVENT_CHANNEL_SWITCH:
|
||||
{ |
||||
mesh_event_channel_switch_t *channel_switch = (mesh_event_channel_switch_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_CHANNEL_SWITCH>new channel:%d", channel_switch->channel); |
||||
} |
||||
break; |
||||
case MESH_EVENT_SCAN_DONE:
|
||||
{ |
||||
mesh_event_scan_done_t *scan_done = (mesh_event_scan_done_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_SCAN_DONE>number:%d", scan_done->number); |
||||
} |
||||
break; |
||||
case MESH_EVENT_NETWORK_STATE:
|
||||
{ |
||||
mesh_event_network_state_t *network_state = (mesh_event_network_state_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_NETWORK_STATE>is_rootless:%d", network_state->is_rootless); |
||||
} |
||||
break; |
||||
case MESH_EVENT_STOP_RECONNECTION:
|
||||
{ |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_STOP_RECONNECTION>");
|
||||
} |
||||
break; |
||||
case MESH_EVENT_FIND_NETWORK:
|
||||
{ |
||||
mesh_event_find_network_t *find_network = (mesh_event_find_network_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_FIND_NETWORK>new channel:%d, router BSSID:"MACSTR"", |
||||
find_network->channel, MAC2STR(find_network->router_bssid)); |
||||
} |
||||
break; |
||||
case MESH_EVENT_ROUTER_SWITCH:
|
||||
{ |
||||
mesh_event_router_switch_t *router_switch = (mesh_event_router_switch_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_ROUTER_SWITCH>new router:%s, channel:%d, "MACSTR"", |
||||
router_switch->ssid, router_switch->channel, MAC2STR(router_switch->bssid)); |
||||
} |
||||
break; |
||||
case MESH_EVENT_PS_PARENT_DUTY:
|
||||
{ |
||||
mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_PS_PARENT_DUTY>duty:%d", ps_duty->duty); |
||||
} |
||||
break; |
||||
case MESH_EVENT_PS_CHILD_DUTY:
|
||||
{ |
||||
mesh_event_ps_duty_t *ps_duty = (mesh_event_ps_duty_t *)vpEventData; |
||||
ESP_LOGI(LOG_TAG, "<MESH_EVENT_PS_CHILD_DUTY>cidx:%d, "MACSTR", duty:%d", ps_duty->child_connected.aid-1, |
||||
MAC2STR(ps_duty->child_connected.mac), ps_duty->duty); |
||||
} |
||||
break; |
||||
default: |
||||
ESP_LOGI(LOG_TAG, "unknown id:%d", i32EventID); |
||||
break; |
||||
} |
||||
} |
@ -1,237 +1,382 @@
|
||||
#include "Mesh_OTA.h" |
||||
/**
|
||||
* @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 |
||||
*/ |
||||
|
||||
/*
|
||||
* 999.999.999 |
||||
* Return true if remote version is newer (higher) than local version |
||||
#include "Mesh_OTA.h" |
||||
#include "Mesh_OTA_Util.h" |
||||
#include "Mesh_OTA_Globals.h" |
||||
#include "Mesh_OTA_Partition_Access.h" |
||||
|
||||
static const char *LOG_TAG = "mesh_ota"; |
||||
|
||||
/**
|
||||
* @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 |
||||
*/ |
||||
bool bNewerVersion(const char* pu8Local, const char* pu8Remote)
|
||||
esp_err_t errMeshOTAInitialize(void) |
||||
{ |
||||
char u8LocalTmp[12]; //local version
|
||||
char u8RemoteTmp[12]; //remote version
|
||||
char* pu8saveptrLocal; //context for strok_r
|
||||
char* pu8saveptrRemote; //context for strok_r
|
||||
bool bReturn = false; //flag to stop loop
|
||||
uint8_t u8Index = 0; //numbers counter in version string
|
||||
|
||||
strcpy(u8LocalTmp, pu8Local); //copy in tmp
|
||||
strcpy(u8RemoteTmp, pu8Remote); //copy in tmp
|
||||
|
||||
char* pu8TokenLocal = strtok_r(u8LocalTmp, ".", &pu8saveptrLocal); //split tokens
|
||||
char* pu8TokenRemote = strtok_r(u8RemoteTmp, ".", &pu8saveptrRemote); //split tokens
|
||||
|
||||
while( (u8Index <= 2) && (bReturn == false)) //loop through tokens
|
||||
{ |
||||
u8Index++; |
||||
if(atoi(pu8TokenLocal) < atoi(pu8TokenRemote)) |
||||
esp_err_t err = ESP_OK; |
||||
BaseType_t xReturned; |
||||
bWantReboot = false; |
||||
|
||||
//create queue to store nodes for ota worker task
|
||||
queueNodes = xQueueCreate(QUEUE_NODES_SIZE, sizeof(mesh_addr_t)); |
||||
if (queueNodes == 0) // Queue not created
|
||||
{ |
||||
bReturn = true; //version number difference --> stop loop
|
||||
ESP_LOGE(LOG_TAG, "Unable to create queue for nodes"); |
||||
err = ESP_FAIL; |
||||
} |
||||
pu8TokenLocal = strtok_r(NULL, ".", &pu8saveptrLocal); //split tokens
|
||||
pu8TokenRemote = strtok_r(NULL, ".", &pu8saveptrRemote); //split tokens
|
||||
} |
||||
return bReturn; |
||||
} |
||||
|
||||
esp_err_t errFindImageStart(const char* pu8Data, uint32_t* pu32DataLenght, uint32_t* pu32StartOffset) |
||||
{ |
||||
/*
|
||||
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; |
||||
uint32_t u32DataIndex = 0; |
||||
uint32_t u32FirstDotOffset = 0; |
||||
uint32_t u32SecondDotOffset = 0; |
||||
uint8_t u8FirstDotIndex = 0; |
||||
uint8_t u8SecondDotIndex = 0; |
||||
|
||||
*pu32StartOffset = 0U; //reset offset to zero
|
||||
|
||||
while((u32DataIndex < *pu32DataLenght) && (bImageStartOffsetFound == false)) |
||||
{ |
||||
//search for magic byte
|
||||
if(pu8Data[u32DataIndex] == 0xe9) |
||||
if(err == ESP_OK) |
||||
{ |
||||
//magic byte found
|
||||
while ((u8FirstDotIndex < 3) && (u32FirstDotOffset == 0)) |
||||
{ |
||||
//search first dot in version number
|
||||
if((u32DataIndex+49+u8FirstDotIndex) < *pu32DataLenght) |
||||
//create queue to store ota messages
|
||||
queueMessageOTA = xQueueCreate(QUEUE_MESSAGE_OTA_SIZE, sizeof(MESH_PACKET_t)); |
||||
if (queueMessageOTA == 0) // Queue not created
|
||||
{ |
||||
if((pu8Data[(u32DataIndex+49+u8FirstDotIndex)] == 0x2e)) |
||||
{ |
||||
//first dot found
|
||||
u32FirstDotOffset = (u32DataIndex+49+u8FirstDotIndex); |
||||
} |
||||
ESP_LOGE(LOG_TAG, "Unable to create queue for OTA messages"); |
||||
err = ESP_FAIL; |
||||
} |
||||
u8FirstDotIndex++; |
||||
} |
||||
} |
||||
|
||||
while ((u8SecondDotIndex < 3) && (u32SecondDotOffset == 0) && (u32FirstDotOffset != 0)) |
||||
{ |
||||
//search first dot in version number
|
||||
if((u32FirstDotOffset+(u8SecondDotIndex+2)) < *pu32DataLenght) |
||||
if(err == ESP_OK) |
||||
{ |
||||
bsStartStopServerWorker = xSemaphoreCreateBinary(); |
||||
if( bsStartStopServerWorker == NULL ) |
||||
{ |
||||
if((pu8Data[(u32FirstDotOffset+(u8SecondDotIndex+2))] == 0x2e)) |
||||
{ |
||||
//second dot found
|
||||
u32SecondDotOffset = (u32FirstDotOffset+(u8SecondDotIndex+2)); |
||||
} |
||||
ESP_LOGE(LOG_TAG, "Unable to create mutex to represent state of server worker"); |
||||
err = ESP_FAIL; |
||||
} |
||||
u8SecondDotIndex++; |
||||
} |
||||
|
||||
if((u32FirstDotOffset != 0) && (u32SecondDotOffset != 0)) |
||||
{ |
||||
//image start found based on magic byte and version number systax
|
||||
*pu32StartOffset = u32DataIndex; //store image start offset
|
||||
bImageStartOffsetFound = true; |
||||
} |
||||
else |
||||
{ |
||||
// this is propably not the magic byte --> reset
|
||||
u32FirstDotOffset = 0; |
||||
u32SecondDotOffset = 0; |
||||
u8FirstDotIndex = 0; |
||||
u8SecondDotIndex = 0; |
||||
} |
||||
} |
||||
u32DataIndex++; |
||||
} |
||||
|
||||
if(bImageStartOffsetFound == false) |
||||
{ |
||||
errReturn = ESP_ERR_NOT_FOUND; |
||||
} |
||||
|
||||
return errReturn; |
||||
} |
||||
|
||||
esp_err_t errExtractVersionNumber(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 = errFindImageStart(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 esp_mesh_ota_send(mesh_addr_t* dest) |
||||
{ |
||||
esp_err_t err = ESP_OK; |
||||
|
||||
static uint32_t u32index; |
||||
|
||||
const esp_partition_t * currentPartition = esp_ota_get_boot_partition(); |
||||
|
||||
if((*currentPartition).subtype == 0) |
||||
{ |
||||
{ |
||||
bsOTAProcess = xSemaphoreCreateBinary(); |
||||
if( bsOTAProcess == NULL ) |
||||
{ |
||||
ESP_LOGE(LOG_TAG, "Unable to create mutex to grant access to OTA process"); |
||||
err = ESP_FAIL; |
||||
} |
||||
} |
||||
|
||||
int data_read = 0; |
||||
if(err == ESP_OK) |
||||
{ |
||||
xSemaphoreGive(bsOTAProcess); //unlock binary semaphore
|
||||
if( bsOTAProcess == NULL ) |
||||
{ |
||||
ESP_LOGE(LOG_TAG, "Unable to unlock mutex to grant access to OTA process"); |
||||
err = ESP_FAIL; |
||||
} |
||||
} |
||||
|
||||
struct ota_mesh_packet packet; |
||||
packet.type=OTA_Data; |
||||
//register callbacks in network
|
||||
ERROR_CHECK(errMeshNetworkSetChildConnectedHandle(vMeshOtaUtilAddNodeToPossibleUpdatableQueue)); |
||||
ERROR_CHECK(errMeshNetworkSetOTAMessageHandleHandle(vMeshOtaUtilAddOtaMessageToQueue)); |
||||
ERROR_CHECK(errMeshNetworkSetChangeStateOfServerWorkerHandle(vMeshOtaUtilChangeStateOfServerWorker)); |
||||
|
||||
if(u32index == 1024) |
||||
if(err == ESP_OK) |
||||
{ |
||||
//all data read
|
||||
data_read = 0; |
||||
u32index = 0; |
||||
pOTAPartition = esp_ota_get_next_update_partition(NULL); //get ota partition
|
||||
|
||||
if(pOTAPartition == NULL) |
||||
{ |
||||
err = ESP_FAIL; |
||||
ESP_LOGE(LOG_TAG, "unable to get next ota partition"); |
||||
} |
||||
} |
||||
else |
||||
|
||||
if(err == ESP_OK) |
||||
{ |
||||
ESP_LOGI(MESH_TAG, "OTA-Data read: %i", u32index); |
||||
err = esp_partition_read(currentPartition, (1024*u32index), packet.au8Payload, 1024 ); |
||||
ESP_ERROR_CHECK(err); |
||||
data_read = 1024; |
||||
u32index++; |
||||
xReturned = xTaskCreate(vMeshOtaTaskServerWorker, "vMeshOtaTaskServerWorker", 8192, NULL, 5, NULL); |
||||
if(xReturned != pdPASS) |
||||
{ |
||||
ESP_LOGE(LOG_TAG, "Unable to create the server worker task"); |
||||
err = ESP_FAIL; |
||||
} |
||||
} |
||||
|
||||
if (data_read > 0) |
||||
|
||||
if(err == ESP_OK) |
||||
{ |
||||
//send ota fragemnt to node
|
||||
esp_mesh_send_packet(dest, &packet); |
||||
xReturned = xTaskCreate(vMeshOtaTaskOTAWorker, "vMeshOtaTaskOTAWorker", 8192, NULL, 5, NULL); |
||||
if(xReturned != pdPASS) |
||||
{ |
||||
ESP_LOGE(LOG_TAG, "Unable to create the OTA worker task"); |
||||
err = ESP_FAIL; |
||||
} |
||||
} |
||||
|
||||
ESP_ERROR_CHECK(err); |
||||
} |
||||
else |
||||
{ |
||||
ESP_LOGI(MESH_TAG, "Subtype: %d", (*currentPartition).subtype); |
||||
} |
||||
return err; |
||||
} |
||||
|
||||
esp_err_t esp_mesh_ota_receive(mesh_addr_t* dest, struct ota_mesh_packet* packet) |
||||
/**
|
||||
* @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) |
||||
{ |
||||