main/updates.c

Sun, 10 Nov 2019 20:53:01 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 10 Nov 2019 20:53:01 +0100
changeset 27
8bb63daa7b46
parent 26
8a3696620c0a
child 29
106464d4c727
permissions
-rw-r--r--

Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.

0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * @file updates.c
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 * @brief Updates management. It can download and install new firmware
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * downloaded from the internet.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 #include "config.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 #define BUFFSIZE 1024 ///< Download buffer size
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 static char ota_write_data[BUFFSIZE + 1] = { 0 };
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 static const char *TAG = "update";
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
13 int update_running = 0; ///< Not zero if update is running.
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
15 extern u8g2_t u8g2; ///< Structure for the display.
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
16 extern int Main_Loop1;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 static void http_cleanup(esp_http_client_handle_t client)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 esp_http_client_close(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 esp_http_client_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 /**
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 * @brief Run binary update procedure
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30 void bin_update(void)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 char temp[64];
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 esp_err_t err;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 const esp_partition_t *update_partition = NULL;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 esp_ota_handle_t update_handle = 0;
27
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
36 int timeout = 600;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
38 ESP_LOGI(TAG, "Update begin");
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
39 update_running = 1;
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
40 screen_updating("Stop tests", NULL);
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
41
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
42 for (;;) {
27
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
43 vTaskDelay(100 / portTICK_PERIOD_MS);
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
44 if (Main_Loop1 == ML1_DONE)
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
45 break;
27
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
46 if (timeout)
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
47 timeout--;
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
48 else {
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
49 ESP_LOGE(TAG, "Timout request stop");
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
50 goto updateerr;
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
51 }
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
52 }
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
53 screen_updating("Tests stopped", NULL);
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
54
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
55 screen_updating("Stop tests", "Start WiFi");
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
56 requestWiFi_user(true);
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
57 timeout = 600;
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
58 for (;;) {
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
59 vTaskDelay(100 / portTICK_PERIOD_MS);
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
60 if (ready_WiFi())
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
61 break;
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
62 if (timeout)
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
63 timeout--;
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
64 else {
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
65 ESP_LOGE(TAG, "Timout request WiFi");
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
66 goto updateerr;
8bb63daa7b46 Increased some lock timeouts. Application CPU speed from 160 to 240 MHz. Added timeout timers to the update process.
Michiel Broek <mbroek@mbse.eu>
parents: 26
diff changeset
67 }
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
68 }
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
69 ESP_LOGI(TAG, "System is ready for update");
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
70 screen_updating("Prepare update", NULL);
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
71
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 const esp_partition_t *running = esp_ota_get_running_partition();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 * Don't use https because it costs more then 100K memory.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 esp_http_client_config_t update = {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78 .url = "http://update.mbse.eu/ap2/fw/co2meter.bin",
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79 };
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 esp_http_client_handle_t client = esp_http_client_init(&update);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 if (client == NULL) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 ESP_LOGI(TAG, "Failed to init HTTP connection");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 err = esp_http_client_open(client, 0);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 if (err != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89 ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 esp_http_client_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 int content_length = esp_http_client_fetch_headers(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 int status_code = esp_http_client_get_status_code(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 if (status_code != 200) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 ESP_LOGE(TAG, "GET %s error %d", update.url, status_code);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
98 esp_http_client_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
100 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
101 update_partition = esp_ota_get_next_update_partition(NULL);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 if (update_partition == NULL) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 ESP_LOGE(TAG, "No update partition");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104 esp_http_client_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 ESP_LOGI(TAG, "Update to partition subtype %d at offset 0x%x", update_partition->subtype, update_partition->address);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 if (err != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
111 ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 http_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
116 screen_updating("Begin download", NULL);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117 ESP_LOGI(TAG, "Download update %s size %d", update.url, content_length);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118 int binary_file_length = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 /*deal with all receive packet*/
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 bool image_header_was_checked = false;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 if (data_read < 0) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 ESP_LOGE(TAG, "Error: data read error");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125 http_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126 goto updateerr;;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 } else if (data_read > 0) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 if (image_header_was_checked == false) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129 esp_app_desc_t new_app_info;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130 if (data_read > sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t) + sizeof(esp_app_desc_t)) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 // check current version with downloading
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 memcpy(&new_app_info, &ota_write_data[sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t)], sizeof(esp_app_desc_t));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 esp_app_desc_t running_app_info;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136 ESP_LOGI(TAG, "Running firmware version: %s, %s %s", running_app_info.version, running_app_info.date, running_app_info.time);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139 const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 esp_app_desc_t invalid_app_info;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 // check current sha256 with last invalid partition
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146 if (last_invalid_app != NULL) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147 if (memcmp(invalid_app_info.app_elf_sha256, new_app_info.app_elf_sha256, 32) == 0) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148 ESP_LOGW(TAG, "New version is the same as invalid version.");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149 ESP_LOGW(TAG, "Previously, there was an attempt to launch the firmware with %s version, but it failed.", invalid_app_info.version);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
150 ESP_LOGW(TAG, "The firmware has been rolled back to the previous version.");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
151 http_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
152 goto updateok;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
154 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
156 if (memcmp(new_app_info.app_elf_sha256, running_app_info.app_elf_sha256, 32) == 0) {
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
157 screen_updating("No new update", NULL);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
158 ESP_LOGI(TAG, "Current running version is the same as a new.");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159 http_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160 goto updateok;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
161 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
163 image_header_was_checked = true;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
165 err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166 if (err != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
167 ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
168 http_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171 ESP_LOGI(TAG, "Continue upgrade application");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
172 } else {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
173 ESP_LOGE(TAG, "Received package is not fit len");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
174 http_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
177 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
178 err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179 if (err != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 http_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181 goto updateerr;;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
182 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
183 binary_file_length += data_read;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184 ESP_LOGD(TAG, "Written image length %d", binary_file_length);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
185 } else if (data_read == 0) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
186 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
187 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
188 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
189
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
190 ESP_LOGI(TAG, "Download complete, binary data length: %d", binary_file_length);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
191 http_cleanup(client);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
192
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
193 if (binary_file_length != content_length) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
194 ESP_LOGE(TAG, "Incomplete file");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
195 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
196 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
197 if (esp_ota_end(update_handle) != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
198 ESP_LOGE(TAG, "esp_ota_end failed!");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
199 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
200 }
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
201 snprintf(temp, 63, "Ok %d bytes", binary_file_length);
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
202 screen_updating("Begin download", temp);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
203
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
204 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
205 * Here we have new version, install and boot it.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
206 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
207 err = esp_ota_set_boot_partition(update_partition);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
208 if (err != ESP_OK) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
209 ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
210 goto updateerr;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
211 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
212
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 ESP_LOGI(TAG, "Prepare to restart system!");
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
214 screen_updating("Reboot...", "...Reboot");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
215 vTaskDelay(1000 / portTICK_PERIOD_MS);
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
216 update_running = 0;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
217 esp_restart();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
218 return;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
219
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
220 updateerr:
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
221 screen_updating("** ERROR **", "Update failed");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
222
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
223 updateok:
26
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
224 update_running = 0;
8a3696620c0a Increaded stacksize for the user process. Implemented the network update using the proven brewboard code. Reverted the lock release and display sendbuffer lines to the previous code. The networks status screen uses the wifi lock.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
225 requestWiFi_user(false);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226 vTaskDelay(3000 / portTICK_PERIOD_MS);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
227 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
228
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229

mercurial