Ready to implement OTA logic
This commit is contained in:
		@@ -39,3 +39,8 @@
 | 
				
			|||||||
 * Interval of time (in seconds) between two synchronisations
 | 
					 * Interval of time (in seconds) between two synchronisations
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
#define SYNC_TIME_INTERVAL 5
 | 
					#define SYNC_TIME_INTERVAL 5
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * OTA download timeout (in milliseconds)
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					#define OTA_REC_TIMEOUT 15000
 | 
				
			||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
#include "esp_log.h"
 | 
					#include "esp_log.h"
 | 
				
			||||||
#include "esp_partition.h"
 | 
					#include "esp_partition.h"
 | 
				
			||||||
#include "esp_ota_ops.h"
 | 
					#include "esp_ota_ops.h"
 | 
				
			||||||
 | 
					#include "esp_http_client.h"
 | 
				
			||||||
 | 
					#include "constants.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "ota.h"
 | 
					#include "ota.h"
 | 
				
			||||||
#include "storage.h"
 | 
					#include "storage.h"
 | 
				
			||||||
@@ -25,15 +27,50 @@ bool ota_perform_update(const char *version)
 | 
				
			|||||||
             running->type, running->subtype, running->address);
 | 
					             running->type, running->subtype, running->address);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Determine firmware download URL
 | 
					    // Determine firmware download URL
 | 
				
			||||||
    char *secure_url = calloc(256, 1);
 | 
					    char *update_url = calloc(256, 1);
 | 
				
			||||||
    assert(secure_url != NULL);
 | 
					    assert(update_url != NULL);
 | 
				
			||||||
    assert(storage_get_secure_origin(secure_url) > 0);
 | 
					    assert(storage_get_secure_origin(update_url) > 0);
 | 
				
			||||||
    strcat(secure_url, "/devices_api/ota/Wt32-Eth01/");
 | 
					    strcat(update_url, "/devices_api/ota/Wt32-Eth01/");
 | 
				
			||||||
    strcat(secure_url, version);
 | 
					    strcat(update_url, version);
 | 
				
			||||||
    ESP_LOGI(TAG, "Firmware URL: %s", secure_url);
 | 
					    ESP_LOGI(TAG, "Firmware URL: %s", update_url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    free(secure_url);
 | 
					    char *root_ca = calloc(1, ROOT_CA_MAX_BYTES);
 | 
				
			||||||
 | 
					    assert(root_ca);
 | 
				
			||||||
 | 
					    assert(storage_get_root_ca(root_ca) > 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    esp_http_client_config_t config = {
 | 
				
			||||||
 | 
					        .url = update_url,
 | 
				
			||||||
 | 
					        .cert_pem = root_ca,
 | 
				
			||||||
 | 
					        .timeout_ms = OTA_REC_TIMEOUT,
 | 
				
			||||||
 | 
					        .keep_alive_enable = true,
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    esp_http_client_handle_t client = esp_http_client_init(&config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (client == NULL)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ESP_LOGE(TAG, "Failed to initialise HTTP connection");
 | 
				
			||||||
 | 
					        free(update_url);
 | 
				
			||||||
 | 
					        free(root_ca);
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    int err = esp_http_client_open(client, 0);
 | 
				
			||||||
 | 
					    free(update_url);
 | 
				
			||||||
 | 
					    free(root_ca);
 | 
				
			||||||
 | 
					    if (err != ESP_OK)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
 | 
				
			||||||
 | 
					        esp_http_client_cleanup(client);
 | 
				
			||||||
 | 
					        return false;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    esp_http_client_fetch_headers(client);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const esp_partition_t *update_partition = esp_ota_get_next_update_partition(NULL);
 | 
				
			||||||
 | 
					    assert(update_partition != NULL);
 | 
				
			||||||
 | 
					    ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%" PRIx32,
 | 
				
			||||||
 | 
					             update_partition->subtype, update_partition->address);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO (from native example)
 | 
					 | 
				
			||||||
    return false;
 | 
					    return false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user