Skip to content

Commit 68a3f7d

Browse files
committed
Allow force OTA upgrade to a previous version
1 parent 6e73a11 commit 68a3f7d

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

main/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies:
1010
78/esp_lcd_nv3023: "~1.0.0"
1111
78/esp-wifi-connect: "~2.3.2"
1212
78/esp-opus-encoder: "~2.3.1"
13-
78/esp-ml307: "~1.8.1"
13+
78/esp-ml307: "~1.9.0"
1414
78/xiaozhi-fonts: "~1.3.2"
1515
espressif/led_strip: "^2.4.1"
1616
espressif/esp_codec_dev: "~1.3.2"

main/ota.cc

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include <esp_app_format.h>
1111
#include <esp_efuse.h>
1212
#include <esp_efuse_table.h>
13+
#ifdef SOC_HMAC_SUPPORTED
1314
#include <esp_hmac.h>
15+
#endif
1416

1517
#include <cstring>
1618
#include <vector>
@@ -23,6 +25,7 @@
2325
Ota::Ota() {
2426
SetCheckVersionUrl(CONFIG_OTA_VERSION_URL);
2527

28+
#ifdef ESP_EFUSE_BLOCK_USR_DATA
2629
// Read Serial Number from efuse user_data
2730
uint8_t serial_number[33] = {0};
2831
if (esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA, serial_number, 32 * 8) == ESP_OK) {
@@ -33,6 +36,7 @@ Ota::Ota() {
3336
has_serial_number_ = true;
3437
}
3538
}
39+
#endif
3640
}
3741

3842
Ota::~Ota() {
@@ -89,7 +93,6 @@ bool Ota::CheckVersion() {
8993
}
9094

9195
data = http->GetBody();
92-
http->Close();
9396
delete http;
9497

9598
// Response: { "firmware": { "version": "1.0.0", "url": "http://" } }
@@ -164,36 +167,34 @@ bool Ota::CheckVersion() {
164167
}
165168
}
166169

170+
has_new_version_ = false;
167171
cJSON *firmware = cJSON_GetObjectItem(root, "firmware");
168-
if (firmware == NULL) {
169-
ESP_LOGE(TAG, "Failed to get firmware object");
170-
cJSON_Delete(root);
171-
return false;
172-
}
173-
cJSON *version = cJSON_GetObjectItem(firmware, "version");
174-
if (version == NULL) {
175-
ESP_LOGE(TAG, "Failed to get version object");
176-
cJSON_Delete(root);
177-
return false;
178-
}
179-
cJSON *url = cJSON_GetObjectItem(firmware, "url");
180-
if (url == NULL) {
181-
ESP_LOGE(TAG, "Failed to get url object");
182-
cJSON_Delete(root);
183-
return false;
184-
}
185-
186-
firmware_version_ = version->valuestring;
187-
firmware_url_ = url->valuestring;
188-
cJSON_Delete(root);
172+
if (firmware != NULL) {
173+
cJSON *version = cJSON_GetObjectItem(firmware, "version");
174+
if (version != NULL) {
175+
firmware_version_ = version->valuestring;
176+
}
177+
cJSON *url = cJSON_GetObjectItem(firmware, "url");
178+
if (url != NULL) {
179+
firmware_url_ = url->valuestring;
180+
}
189181

190-
// Check if the version is newer, for example, 0.1.0 is newer than 0.0.1
191-
has_new_version_ = IsNewVersionAvailable(current_version_, firmware_version_);
192-
if (has_new_version_) {
193-
ESP_LOGI(TAG, "New version available: %s", firmware_version_.c_str());
194-
} else {
195-
ESP_LOGI(TAG, "Current is the latest version");
182+
if (version != NULL && url != NULL) {
183+
// Check if the version is newer, for example, 0.1.0 is newer than 0.0.1
184+
has_new_version_ = IsNewVersionAvailable(current_version_, firmware_version_);
185+
if (has_new_version_) {
186+
ESP_LOGI(TAG, "New version available: %s", firmware_version_.c_str());
187+
} else {
188+
ESP_LOGI(TAG, "Current is the latest version");
189+
}
190+
// If the force flag is set to 1, the given version is forced to be installed
191+
cJSON *force = cJSON_GetObjectItem(firmware, "force");
192+
if (force != NULL && force->valueint == 1) {
193+
has_new_version_ = true;
194+
}
195+
}
196196
}
197+
cJSON_Delete(root);
197198
return true;
198199
}
199200

@@ -366,6 +367,8 @@ std::string Ota::GetActivationPayload() {
366367
return "{}";
367368
}
368369

370+
std::string hmac_hex;
371+
#ifdef SOC_HMAC_SUPPORTED
369372
uint8_t hmac_result[32]; // SHA-256 输出为32字节
370373

371374
// 使用Key0计算HMAC
@@ -375,12 +378,12 @@ std::string Ota::GetActivationPayload() {
375378
return "{}";
376379
}
377380

378-
std::string hmac_hex;
379381
for (size_t i = 0; i < sizeof(hmac_result); i++) {
380382
char buffer[3];
381383
sprintf(buffer, "%02x", hmac_result[i]);
382384
hmac_hex += buffer;
383385
}
386+
#endif
384387

385388
cJSON *payload = cJSON_CreateObject();
386389
cJSON_AddStringToObject(payload, "algorithm", "hmac-sha256");

0 commit comments

Comments
 (0)