|
10 | 10 | #include <esp_wifi_types.h> |
11 | 11 | #include <esp_wifi.h> |
12 | 12 | #include <errno.h> |
| 13 | +#include <nvs.h> |
13 | 14 | #include <esp_https_ota.h> |
14 | 15 |
|
15 | 16 | #if CONFIG_BT_ENABLED |
@@ -51,6 +52,83 @@ const char *ESP_RMAKER_OTA_DEFAULT_SERVER_CERT = esp_rmaker_ota_def_cert; |
51 | 52 |
|
52 | 53 | #define ESP_RMAKER_HTTPS_OTA_TIMEOUT_MS 5000 |
53 | 54 |
|
| 55 | +#ifdef CONFIG_ESP_RMAKER_HTTP_OTA_RESUMPTION |
| 56 | +#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 5, 0) |
| 57 | +#define RMAKER_OTA_HTTP_OTA_RESUMPTION |
| 58 | +#else |
| 59 | +#warning "HTTP OTA resumption, needs IDF version >= 5.5.0" |
| 60 | +#endif |
| 61 | +#endif |
| 62 | + |
| 63 | +#ifdef RMAKER_OTA_HTTP_OTA_RESUMPTION |
| 64 | +#define RMAKER_OTA_WRITTEN_LENGTH_NVS_NAME "ota_writen" |
| 65 | +#define RMAKER_OTA_FILE_MD5_NVS_NAME "ota_file_md5" |
| 66 | +static esp_err_t esp_rmaker_https_ota_get_len_and_md5_from_nvs(uint32_t *written_len, char **file_md5) |
| 67 | +{ |
| 68 | + *written_len = 0; |
| 69 | + *file_md5 = NULL; |
| 70 | + nvs_handle handle; |
| 71 | + esp_err_t err = nvs_open_from_partition(ESP_RMAKER_NVS_PART_NAME, RMAKER_OTA_NVS_NAMESPACE, NVS_READWRITE, &handle); |
| 72 | + if (err == ESP_OK) { |
| 73 | + uint32_t len = 0; |
| 74 | + err = nvs_get_u32(handle, RMAKER_OTA_WRITTEN_LENGTH_NVS_NAME, &len); |
| 75 | + if (err == ESP_OK) { |
| 76 | + *written_len = len; |
| 77 | + size_t file_md5_len = 0; |
| 78 | + err = nvs_get_str(handle, RMAKER_OTA_FILE_MD5_NVS_NAME, NULL, &file_md5_len); |
| 79 | + if (err == ESP_OK) { |
| 80 | + *file_md5 = MEM_CALLOC_EXTRAM(1, file_md5_len + 1); |
| 81 | + if (!*file_md5) { |
| 82 | + err = ESP_ERR_NO_MEM; |
| 83 | + } else { |
| 84 | + err = nvs_get_str(handle, RMAKER_OTA_FILE_MD5_NVS_NAME, *file_md5, &file_md5_len); |
| 85 | + if (err == ESP_OK) { |
| 86 | + (*file_md5)[file_md5_len] = '\0'; |
| 87 | + } else { |
| 88 | + free(*file_md5); |
| 89 | + *file_md5 = NULL; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + nvs_close(handle); |
| 95 | + } |
| 96 | + return err; |
| 97 | +} |
| 98 | + |
| 99 | +static esp_err_t esp_rmaker_https_ota_set_len_and_md5_to_nvs(uint32_t written_len, char *file_md5) |
| 100 | +{ |
| 101 | + nvs_handle handle; |
| 102 | + esp_err_t err = nvs_open_from_partition(ESP_RMAKER_NVS_PART_NAME, RMAKER_OTA_NVS_NAMESPACE, NVS_READWRITE, &handle); |
| 103 | + if (err == ESP_OK) { |
| 104 | + err = nvs_set_u32(handle, RMAKER_OTA_WRITTEN_LENGTH_NVS_NAME, written_len); |
| 105 | + if (err == ESP_OK) { |
| 106 | + if (file_md5) { |
| 107 | + err = nvs_set_str(handle, RMAKER_OTA_FILE_MD5_NVS_NAME, file_md5); |
| 108 | + } |
| 109 | + if (err == ESP_OK) { |
| 110 | + nvs_commit(handle); |
| 111 | + } |
| 112 | + } |
| 113 | + nvs_close(handle); |
| 114 | + } |
| 115 | + return err; |
| 116 | +} |
| 117 | + |
| 118 | +static esp_err_t esp_rmaker_https_ota_cleanup_ota_cfg_from_nvs(void) |
| 119 | +{ |
| 120 | + nvs_handle handle; |
| 121 | + esp_err_t err = nvs_open_from_partition(ESP_RMAKER_NVS_PART_NAME, RMAKER_OTA_NVS_NAMESPACE, NVS_READWRITE, &handle); |
| 122 | + if (err == ESP_OK) { |
| 123 | + nvs_erase_key(handle, RMAKER_OTA_WRITTEN_LENGTH_NVS_NAME); |
| 124 | + nvs_erase_key(handle, RMAKER_OTA_FILE_MD5_NVS_NAME); |
| 125 | + nvs_commit(handle); |
| 126 | + nvs_close(handle); |
| 127 | + } |
| 128 | + return err; |
| 129 | +} |
| 130 | +#endif |
| 131 | + |
54 | 132 | static esp_err_t esp_rmaker_ota_use_https(esp_rmaker_ota_handle_t ota_handle, esp_rmaker_ota_data_t *ota_data, char *err_desc, size_t err_desc_size) |
55 | 133 | { |
56 | 134 | int buffer_size_tx = DEF_HTTP_TX_BUFFER_SIZE; |
@@ -85,6 +163,33 @@ static esp_err_t esp_rmaker_ota_use_https(esp_rmaker_ota_handle_t ota_handle, es |
85 | 163 | esp_https_ota_config_t ota_config = { |
86 | 164 | .http_config = &config, |
87 | 165 | }; |
| 166 | +#ifdef RMAKER_OTA_HTTP_OTA_RESUMPTION |
| 167 | + /* Check if file md5 is present and match with the one in the ota_data, if yes, resume the OTA; |
| 168 | + otherwise, start from the beginning and set the written length 0 and file md5 to NVS */ |
| 169 | + if (ota_data->file_md5) { |
| 170 | + char *file_md5 = NULL; |
| 171 | + uint32_t written_len = 0; |
| 172 | + bool resume_ota = false; |
| 173 | + if (esp_rmaker_https_ota_get_len_and_md5_from_nvs(&written_len, &file_md5) == ESP_OK) { |
| 174 | + if (strncmp(file_md5, ota_data->file_md5, strlen(ota_data->file_md5)) != 0) { |
| 175 | + ESP_LOGW(TAG, "File MD5 mismatch, seems a new firmware, not resuming OTA"); |
| 176 | + } else { |
| 177 | + resume_ota = true; |
| 178 | + ota_config.ota_resumption = true; |
| 179 | + ota_config.ota_image_bytes_written = written_len; |
| 180 | + } |
| 181 | + free(file_md5); |
| 182 | + } |
| 183 | + if (!resume_ota) { |
| 184 | + /* Start from the beginning and set the written length 0 and file md5 to NVS */ |
| 185 | + if (esp_rmaker_https_ota_set_len_and_md5_to_nvs(0, ota_data->file_md5) != ESP_OK) { |
| 186 | + ESP_LOGE(TAG, "Failed to set written length 0 and file MD5 to NVS"); |
| 187 | + } |
| 188 | + } |
| 189 | + } else { |
| 190 | + ESP_LOGW(TAG, "File MD5 not present, not resuming OTA because can not verify the already downloaded firmware is the same as the new firmware, please upgrade the backend to support it"); |
| 191 | + } |
| 192 | +#endif |
88 | 193 | /* Using a warning just to highlight the message */ |
89 | 194 | ESP_LOGW(TAG, "Starting OTA. This may take time."); |
90 | 195 | esp_https_ota_handle_t https_ota_handle = NULL; |
@@ -158,6 +263,15 @@ static esp_err_t esp_rmaker_ota_use_https(esp_rmaker_ota_handle_t ota_handle, es |
158 | 263 | ESP_LOGI(TAG, "Image bytes read: %d", esp_https_ota_get_image_len_read(https_ota_handle)); |
159 | 264 | count = 0; |
160 | 265 | } |
| 266 | +#ifdef RMAKER_OTA_HTTP_OTA_RESUMPTION |
| 267 | + /* if file md5 is present, save the written length to NVS */ |
| 268 | + if (ota_data->file_md5) { |
| 269 | + /* file md5 is present, only set the written length to NVS */ |
| 270 | + if (esp_rmaker_https_ota_set_len_and_md5_to_nvs(esp_https_ota_get_image_len_read(https_ota_handle), NULL) != ESP_OK) { |
| 271 | + ESP_LOGE(TAG, "Failed to save OTA written length to NVS"); |
| 272 | + } |
| 273 | + } |
| 274 | +#endif |
161 | 275 | #ifdef CONFIG_ESP_RMAKER_OTA_PROGRESS_SUPPORT |
162 | 276 | int image_size = esp_https_ota_get_image_size(https_ota_handle); |
163 | 277 | int read_size = esp_https_ota_get_image_len_read(https_ota_handle); |
@@ -186,7 +300,11 @@ static esp_err_t esp_rmaker_ota_use_https(esp_rmaker_ota_handle_t ota_handle, es |
186 | 300 | err = ESP_FAIL; |
187 | 301 | goto ota_end; |
188 | 302 | } |
189 | | - |
| 303 | +#ifdef RMAKER_OTA_HTTP_OTA_RESUMPTION |
| 304 | + if (esp_rmaker_https_ota_cleanup_ota_cfg_from_nvs() != ESP_OK) { |
| 305 | + ESP_LOGE(TAG, "Failed to cleanup OTA config from NVS"); |
| 306 | + } |
| 307 | +#endif |
190 | 308 | /* Report completion before finishing */ |
191 | 309 | esp_rmaker_ota_report_status(ota_handle, OTA_STATUS_IN_PROGRESS, "Firmware Image download complete"); |
192 | 310 |
|
|
0 commit comments