Skip to content

Commit

Permalink
ota in both directions step 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ves011 committed Apr 14, 2023
1 parent b9228d2 commit fe9b762
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 19 deletions.
37 changes: 37 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"env":
{
"IDF_TOOLS": "D:/dev/.espressif/tools",
"IDF_PATH": "D:/dev/esp-idf-v4.4.3"
},
"configurations": [
{
"name": "esp32",
"browse":
{
"path": [
"${workspaceFolder}",
"${IDF_PATH}",
"${IDF_TOOLS}"
],
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"D:/dev/esp32/esp32_ota/**",
"D:/dev/esp32/esp32_common/**",
"D:/dev/esp-idf-v4.4.3/components/**",
"D:/dev/.espressif/tools/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cmake.configureOnOpen": false
}
27 changes: 15 additions & 12 deletions cmds/cmd_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/dirent.h>
#include "esp_log.h"
#include "esp_console.h"
#include "esp_system.h"
Expand All @@ -35,18 +37,21 @@
#include <sys/fcntl.h>
#include "esp_vfs.h"
#include "esp_spiffs.h"
#include <dirent.h>
#include <sys/stat.h>
#include "ping/ping_sock.h"
#include "mqtt_client.h"
#include "esp_flash_partitions.h"
#include "esp_partition.h"
#include "esp_ota_ops.h"
#include "sdkconfig.h"
#include "common_defines.h"
#include "external_defs.h"
#include "utils.h"
#include "ota.h"
#include "cmd_system.h"
#include "cmd_wifi.h"
#include "sdkconfig.h"



#ifdef CONFIG_FREERTOS_USE_STATS_FORMATTING_FUNCTIONS
Expand All @@ -67,10 +72,8 @@ static void register_console(void);
static void register_boot(void);
static void register_cat(void);
static void register_rm(void);
#if ACTIVE_CONTROLLER == OTA_CONTROLLER
static int ota_start(int argc, char **argv);
static void register_ota(void);
#endif
#if WITH_TASKS_INFO
static void register_tasks(void);
#endif
Expand All @@ -87,9 +90,7 @@ void register_system_common(void)
register_boot();
register_cat();
register_rm();
#if ACTIVE_CONTROLLER == OTA_CONTROLLER
register_ota();
#endif
#if WITH_TASKS_INFO
register_tasks();
#endif
Expand Down Expand Up @@ -547,13 +548,13 @@ static int tasks_info(int argc, char **argv)
ESP_LOGE(TAG, "failed to allocate buffer for vTaskList output");
return 1;
}
fputs("Task Name\tStatus\tPrio\tHWM\tTask#", stdout);
my_fputs("Task Name\tStatus\tPrio\tHWM\tTask#", stdout);
#ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
fputs("\tAffinity", stdout);
#endif
fputs("\n", stdout);
my_fputs("\n", stdout);
vTaskList(task_list_buffer);
fputs(task_list_buffer, stdout);
my_fputs(task_list_buffer, stdout);
free(task_list_buffer);
return 0;
}
Expand Down Expand Up @@ -836,7 +837,7 @@ static void register_console(void)
};
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
}
#if ACTIVE_CONTROLLER == OTA_CONTROLLER

static struct
{
struct arg_str *url;
Expand Down Expand Up @@ -870,7 +871,7 @@ static void register_ota(void)
};
ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
}
#endif

static struct
{
struct arg_str *pname;
Expand Down Expand Up @@ -964,8 +965,10 @@ void do_system_cmd(int argc, char **argv)
do_ping_cmd(argc, argv);
else if(!strcmp(argv[0], "boot"))
boot_from(argc, argv);
#if ACTIVE_CONTROLLER == OTA_CONTROLLER
else if(!strcmp(argv[0], "cat"))
cat_file(argc, argv);
else if(!strcmp(argv[0], "rm"))
rm_file(argc, argv);
else if(!strcmp(argv[0], "ota"))
ota_start(argc, argv);
#endif
}
3 changes: 3 additions & 0 deletions tcp/tcp_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ int tcp_log_init(void)
int tcp_log_message(char *message)
{
char buf[1024];
//if !tcp_log_enable try to init first
if(!tcp_log_enable)
tcp_log_init();
if(tcp_log_enable)
{
strcpy(buf, USER_MQTT);
Expand Down
18 changes: 18 additions & 0 deletions utils/esp32_ota.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"folders": [
{
"path": "../../esp32_ota"
}
],
"settings": {
"files.associations": {
"*.embeddedhtml": "html",
"esp_vfs_fat.h": "c",
"utils.h": "c",
"cmd_system.h": "c",
"ota.h": "c",
"common_defines.h": "c",
"mqtt_client.h": "c"
}
}
}
40 changes: 35 additions & 5 deletions utils/ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "wear_levelling.h"
#include "esp_console.h"
#include "driver/gpio.h"
#include "protocol_examples_common.h"
#include "errno.h"
#include "esp_spi_flash.h"
#include "esp_spiffs.h"
Expand All @@ -35,14 +34,18 @@
#define BUFFSIZE 1024

#define OTA_URL_SIZE 256
#define FW_UPGRADE_URL "http://proxy.gnet:444/fw_upgrade.bin"
//#define FW_UPGRADE_URL "http://proxy.gnet:444/fw_upgrade.bin"
TaskHandle_t ota_task_handle;

#define TAG "OTA_TASK"

/*an ota data write buffer ready to write to the flash*/
static char ota_write_data[BUFFSIZE + 1] = { 0 };

extern const uint8_t client_cert_pem_start[] asm("_binary_client_crt_start");
extern const uint8_t client_cert_pem_end[] asm("_binary_client_crt_end");
extern const uint8_t client_key_pem_start[] asm("_binary_client_key_start");
extern const uint8_t client_key_pem_end[] asm("_binary_client_key_end");
extern const uint8_t server_cert_pem_start[] asm("_binary_ca_crt_start");
extern const uint8_t server_cert_pem_end[] asm("_binary_ca_crt_end");

Expand All @@ -62,6 +65,27 @@ void print_sha256 (const uint8_t *image_hash, const char *label)
}
ESP_LOGI(TAG, "%s: %s", label, hash_print);
}
static esp_partition_t *get_updateable_partition()
{
const esp_partition_t *np = NULL;
const esp_partition_t *running = esp_ota_get_running_partition();
esp_partition_iterator_t pit = esp_partition_find(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_ANY, NULL);
while(pit)
{
np = esp_partition_get(pit);
if(np)
{
if(np != running && np->type == ESP_PARTITION_TYPE_APP)
break;
}
pit = esp_partition_next(pit);
}
if(np)
ESP_LOGI(TAG, "Updateable partition %s @ %0x", np->label, np->address);
else
ESP_LOGI(TAG, "No updateable partition found");
return (esp_partition_t *)np;
}

void ota_task(const char *url)
{
Expand All @@ -71,7 +95,9 @@ void ota_task(const char *url)
const esp_partition_t *update_partition = NULL;

ESP_LOGI(TAG, "Starting OTA");

update_partition = get_updateable_partition();
if(!update_partition)
return;
const esp_partition_t *configured = esp_ota_get_boot_partition();
const esp_partition_t *running = esp_ota_get_running_partition();

Expand All @@ -81,18 +107,22 @@ void ota_task(const char *url)
configured->address, running->address);
ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)");
}
/*
if(!strcmp(running->label, OTA_PART_NAME))
{
ESP_LOGI(TAG, "Cannot do upgrade while running from %s partition\nReboot from factory partition first", OTA_PART_NAME);
return;
}
*/
ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%08x)",
running->type, running->subtype, running->address);

esp_http_client_config_t config =
{
.url = url,
.cert_pem = (char *)server_cert_pem_start,
.client_cert_pem = (char *)client_cert_pem_start,
.client_key_pem = (char *)client_key_pem_start,
.timeout_ms = 10000,
.keep_alive_enable = true,
};
Expand All @@ -112,8 +142,8 @@ void ota_task(const char *url)
}
esp_http_client_fetch_headers(client);

update_partition = esp_ota_get_next_update_partition(NULL);
assert(update_partition != NULL);
//update_partition = esp_ota_get_next_update_partition(NULL);
//assert(update_partition != NULL);
ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%x",
update_partition->subtype, update_partition->address);

Expand Down
17 changes: 15 additions & 2 deletions utils/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ void my_printf(char *format, ...)
tcp_log_message(buf);
}
}
void my_fputs(char *buf, FILE *f)
{
if(console_state == CONSOLE_ON)
{
puts(buf);
}
else if(console_state == CONSOLE_TCP)
{
//puts(buf);
tcp_log_message(buf);
}
}
int rw_params(int rw, int param_type, void * param_val)
{
char buf[64];
Expand Down Expand Up @@ -376,6 +388,7 @@ int rw_params(int rw, int param_type, void * param_val)
//esp_vfs_spiffs_unregister(conf.partition_label);
return ret;
}
#if ACTIVE_CONTROLLER == WESTA_CONTROLLER
int write_tpdata(int rw, char *bufdata)
{
FILE *f = NULL;
Expand All @@ -385,7 +398,7 @@ int write_tpdata(int rw, char *bufdata)
int ret = ESP_FAIL;
time(&now);
localtime_r(&now, &timeinfo);
strftime(strtime, sizeof(strtime), "%Y-%m-%d/%H:%M:%S", &timeinfo);
strftime(strtime, sizeof(strtime), "%Y-%m-%dT%H:%M:%S", &timeinfo);
sprintf(file_name, "%s/%d.tph", BASE_PATH, timeinfo.tm_year + 1900);
if(rw == PARAM_WRITE)
{
Expand All @@ -411,4 +424,4 @@ int write_tpdata(int rw, char *bufdata)
}
return ret;
}

#endif
1 change: 1 addition & 0 deletions utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

void my_printf(char *format, ...);
int my_log_vprintf(const char *fmt, va_list arguments);
void my_fputs(char *buf, FILE *f);
int rw_params(int rw, int param_type, void * param_val);
int write_tpdata(int rw, char *bufdata);

Expand Down

0 comments on commit fe9b762

Please sign in to comment.