diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..f2c5084 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -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 + } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cad7657 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cmake.configureOnOpen": false +} \ No newline at end of file diff --git a/cmds/cmd_system.c b/cmds/cmd_system.c index 18775b8..802d619 100644 --- a/cmds/cmd_system.c +++ b/cmds/cmd_system.c @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include "esp_log.h" #include "esp_console.h" #include "esp_system.h" @@ -35,18 +37,21 @@ #include #include "esp_vfs.h" #include "esp_spiffs.h" +#include +#include #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 @@ -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 @@ -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 @@ -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; } @@ -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; @@ -870,7 +871,7 @@ static void register_ota(void) }; ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) ); } -#endif + static struct { struct arg_str *pname; @@ -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 } diff --git a/tcp/tcp_log.c b/tcp/tcp_log.c index 1351e72..5200719 100644 --- a/tcp/tcp_log.c +++ b/tcp/tcp_log.c @@ -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); diff --git a/utils/esp32_ota.code-workspace b/utils/esp32_ota.code-workspace new file mode 100644 index 0000000..ed7b71f --- /dev/null +++ b/utils/esp32_ota.code-workspace @@ -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" + } + } +} \ No newline at end of file diff --git a/utils/ota.c b/utils/ota.c index e277d6d..ab87e3d 100644 --- a/utils/ota.c +++ b/utils/ota.c @@ -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" @@ -35,7 +34,7 @@ #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" @@ -43,6 +42,10 @@ TaskHandle_t ota_task_handle; /*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"); @@ -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) { @@ -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(); @@ -81,11 +107,13 @@ 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); @@ -93,6 +121,8 @@ void ota_task(const char *url) { .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, }; @@ -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); diff --git a/utils/utils.c b/utils/utils.c index f1b4913..ac7dad0 100644 --- a/utils/utils.c +++ b/utils/utils.c @@ -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]; @@ -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; @@ -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) { @@ -411,4 +424,4 @@ int write_tpdata(int rw, char *bufdata) } return ret; } - +#endif diff --git a/utils/utils.h b/utils/utils.h index a08ed93..37710c1 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -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);