diff --git a/components/usb/esp_tinyuf2/esp_tinyuf2.c b/components/usb/esp_tinyuf2/esp_tinyuf2.c index 8ee4a4a47..bac90ad4d 100644 --- a/components/usb/esp_tinyuf2/esp_tinyuf2.c +++ b/components/usb/esp_tinyuf2/esp_tinyuf2.c @@ -229,7 +229,7 @@ esp_err_t esp_tinyuf2_install(tinyuf2_ota_config_t *ota_config, tinyuf2_nvs_conf if (ota_config->if_restart) { ESP_LOGW(TAG, "Enable restart, SoC will restart after update complete"); } - board_flash_init(ota_config->subtype, ota_config->label, ota_config->complete_cb, ota_config->if_restart); + board_flash_init(ota_config->subtype, ota_config->label, ota_config->event_cb, ota_config->if_restart); } if (nvs_config) { diff --git a/components/usb/esp_tinyuf2/esp_tinyuf2.h b/components/usb/esp_tinyuf2/esp_tinyuf2.h index cbdfd19dc..58a1891f3 100644 --- a/components/usb/esp_tinyuf2/esp_tinyuf2.h +++ b/components/usb/esp_tinyuf2/esp_tinyuf2.h @@ -26,17 +26,23 @@ extern "C" { #define UF2_RESET_REASON_VALUE (CONFIG_UF2_OTA_RESET_REASON_VALUE) +typedef enum { + TINYUF2_UPDATE_COMPLETE = 0, + TINYUF2_UPDATE_PCT = 1, + TINYUF2_UPDATE_MOUNT = 2, +} uf2_update_event_t; + /** * @brief user callback called after uf2 update complete * */ -typedef void (*update_complete_cb_t)(void); +typedef void (*update_event_cb_t)(uf2_update_event_t, uint32_t); /** * @brief user callback called after nvs modified * */ -typedef void (*nvs_modified_cb_t)(void); +typedef void (*nvs_modified_cb_t)(const char*); /** * @brief tinyuf2 configurations @@ -46,7 +52,7 @@ typedef struct { esp_partition_subtype_t subtype; /*!< Partition subtype. if ESP_PARTITION_SUBTYPE_ANY will use the next_update_partition by default. */ const char *label; /*!< Partition label. Set this value if looking for partition with a specific name. if subtype==ESP_PARTITION_SUBTYPE_ANY, label default to NULL.*/ bool if_restart; /*!< if restart system to new app partition after UF2 flashing done */ - update_complete_cb_t complete_cb; /*!< user callback called after uf2 update complete */ + update_event_cb_t event_cb; /*!< user callback called after uf2 update complete */ } tinyuf2_ota_config_t; /** @@ -118,7 +124,7 @@ void esp_restart_from_tinyuf2(void); * @return * - ESP_OK: Operation was successful. */ -esp_err_t esp_tinyuf2_set_all_key_hidden(bool if_hidden) +esp_err_t esp_tinyuf2_set_all_key_hidden(bool); /** * @brief Add a key to the hidden keys list. @@ -134,7 +140,7 @@ esp_err_t esp_tinyuf2_set_all_key_hidden(bool if_hidden) * - ESP_ERR_INVALID_ARG: Provided key is NULL. * - ESP_ERR_NO_MEM: Memory allocation failed or maximum number of hidden keys exceeded. */ -esp_err_t esp_tinyuf2_add_key_hidden(const char *key) +esp_err_t esp_tinyuf2_add_key_hidden(const char *); #endif #ifdef __cplusplus diff --git a/components/usb/esp_tinyuf2/msc/msc.c b/components/usb/esp_tinyuf2/msc/msc.c index ea3fc15ed..43e9f7186 100644 --- a/components/usb/esp_tinyuf2/msc/msc.c +++ b/components/usb/esp_tinyuf2/msc/msc.c @@ -184,6 +184,8 @@ void tud_msc_write10_complete_cb(uint8_t lun) ESP_LOGI(TAG, "STATE_WRITING_FINISHED"); board_dfu_complete(); } + else + board_event_cb(TINYUF2_UPDATE_PCT, _wr_state.numWritten * 100 / _wr_state.numBlocks); } } @@ -207,8 +209,10 @@ bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, boo if (load_eject) { if (start) { + board_event_cb(TINYUF2_UPDATE_MOUNT, 1); // load disk storage } else { + board_event_cb(TINYUF2_UPDATE_MOUNT, 0); // unload disk storage } } diff --git a/components/usb/esp_tinyuf2/private_include/board_flash.h b/components/usb/esp_tinyuf2/private_include/board_flash.h index be141bbe8..0e4369893 100644 --- a/components/usb/esp_tinyuf2/private_include/board_flash.h +++ b/components/usb/esp_tinyuf2/private_include/board_flash.h @@ -74,6 +74,7 @@ #define CFG_UF2_INI_FILE_SIZE CONFIG_UF2_INI_FILE_SIZE extern char *_ini_file; extern char *_ini_file_dummy; +#define NVS_HIDDEN_PLACEHOLDER "****" //--------------------------------------------------------------------+ // Basic API @@ -82,6 +83,9 @@ extern char *_ini_file_dummy; // DFU is complete, should reset or jump to application mode and not return void board_dfu_complete(void); +// Access to user callback from msc.c, etc +void board_event_cb(uf2_update_event_t, uint32_t); + // Fill Serial Number and return its length (limit to 16 bytes) uint8_t board_usb_get_serial(uint8_t serial_id[16]); @@ -90,7 +94,7 @@ uint8_t board_usb_get_serial(uint8_t serial_id[16]); //--------------------------------------------------------------------+ // Initialize flash for DFU -void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_complete_cb_t complete_cb, bool if_restart); +void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_event_cb_t event_cb, bool if_restart); void board_flash_deinit(void); // Initialize flash for NVS diff --git a/components/usb/esp_tinyuf2/uf2/board_flash.c b/components/usb/esp_tinyuf2/uf2/board_flash.c index ff897c978..a36bf68e4 100644 --- a/components/usb/esp_tinyuf2/uf2/board_flash.c +++ b/components/usb/esp_tinyuf2/uf2/board_flash.c @@ -57,7 +57,7 @@ static uint32_t _fl_addr = FLASH_CACHE_INVALID_ADDR; static uint8_t *_fl_buf = NULL; static bool _if_restart = false; -static update_complete_cb_t _complete_cb = NULL; +static update_event_cb_t _event_cb = NULL; static esp_partition_t const* _part_ota = NULL; char *_ini_file = NULL; char *_ini_file_dummy = NULL; @@ -65,7 +65,7 @@ static nvs_modified_cb_t _modified_cb = NULL; static char _part_name[16] = ""; static char _namespace_name[16] = ""; #ifdef CONFIG_UF2_INI_NVS_VALUE_HIDDEN -char *HIDDEN_STR[CONFIG_UF2_INI_NVS_HIDDEN_MAX_NUM]; +char *hidden_str[CONFIG_UF2_INI_NVS_HIDDEN_MAX_NUM]; size_t hidden_str_num = 0; bool if_all_hidden = false; #endif @@ -77,14 +77,17 @@ uint8_t board_usb_get_serial(uint8_t serial_id[16]) return 6; } +void board_event_cb(uf2_update_event_t e, uint32_t p) { + if (!_event_cb) + return; + _event_cb(e, p); +} + void board_dfu_complete(void) { esp_ota_set_boot_partition(_part_ota); - if (_complete_cb) { - PRINTF("dfu_complete: run user callback"); - _complete_cb(); - } + board_event_cb(TINYUF2_UPDATE_COMPLETE, 0); if (_if_restart) { /* code */ @@ -94,11 +97,11 @@ void board_dfu_complete(void) } } -void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_complete_cb_t complete_cb, bool if_restart) +void board_flash_init(esp_partition_subtype_t subtype, const char *label, update_event_cb_t event_cb, bool if_restart) { _fl_addr = FLASH_CACHE_INVALID_ADDR; _if_restart = if_restart; - _complete_cb = complete_cb; + _event_cb = event_cb; if (subtype == ESP_PARTITION_SUBTYPE_ANY) { _part_ota = esp_ota_get_next_update_partition(NULL); @@ -232,8 +235,8 @@ static void ini_gen_from_nvs(const char *part, const char *name) char *str = (char *)malloc(len); if ((result = nvs_get_str(nvs, info.key, str, &len)) == ESP_OK) { #ifdef CONFIG_UF2_INI_NVS_VALUE_HIDDEN - if (!check_value_if_hidden(info.key)) { - ini_insert_pair(info.key, "****"); + if (check_value_if_hidden(info.key)) { + ini_insert_pair(info.key, NVS_HIDDEN_PLACEHOLDER); } else #endif { @@ -259,6 +262,11 @@ static void ini_gen_from_nvs(const char *part, const char *name) static int nvs_write_back(void* user, const char* section, const char* name, const char* value) { + if (check_value_if_hidden(name) && strcmp(value, NVS_HIDDEN_PLACEHOLDER) == 0) + { + PRINTFD("Ignore %s", name); + return 1; + } nvs_handle_t nvs = (nvs_handle_t)user; PRINTFD("... [%s]", section); PRINTFD("... (%s=%s)", name, value); @@ -314,7 +322,7 @@ void board_flash_nvs_update(const char *ini_str) } ini_parse_to_nvs(ini_str); if (_modified_cb) { - _modified_cb(); + _modified_cb(ini_str); } }