main/updates.c

changeset 26
8a3696620c0a
parent 0
88d965579617
child 27
8bb63daa7b46
equal deleted inserted replaced
25:cc7c423f03fb 26:8a3696620c0a
8 8
9 9
10 #define BUFFSIZE 1024 ///< Download buffer size 10 #define BUFFSIZE 1024 ///< Download buffer size
11 static char ota_write_data[BUFFSIZE + 1] = { 0 }; 11 static char ota_write_data[BUFFSIZE + 1] = { 0 };
12 static const char *TAG = "update"; 12 static const char *TAG = "update";
13 13 int update_running = 0; ///< Not zero if update is running.
14
15 extern u8g2_t u8g2; ///< Structure for the display.
16 extern int Main_Loop1;
14 17
15 18
16 static void http_cleanup(esp_http_client_handle_t client) 19 static void http_cleanup(esp_http_client_handle_t client)
17 { 20 {
18 esp_http_client_close(client); 21 esp_http_client_close(client);
29 char temp[64]; 32 char temp[64];
30 esp_err_t err; 33 esp_err_t err;
31 const esp_partition_t *update_partition = NULL; 34 const esp_partition_t *update_partition = NULL;
32 esp_ota_handle_t update_handle = 0; 35 esp_ota_handle_t update_handle = 0;
33 36
34 // TFT_setFont(DEJAVU18_FONT, NULL); 37 ESP_LOGI(TAG, "Update begin");
35 // _fg = TFT_CYAN; 38 update_running = 1;
39 screen_updating("Stop tests", NULL);
40 requestWiFi_user(true);
41
42 for (;;) {
43 vTaskDelay(50 / portTICK_PERIOD_MS);
44 if (ready_WiFi() && Main_Loop1 == ML1_DONE)
45 break;
46 }
47 ESP_LOGI(TAG, "System is ready for update");
48 screen_updating("Prepare update", NULL);
49
36 const esp_partition_t *running = esp_ota_get_running_partition(); 50 const esp_partition_t *running = esp_ota_get_running_partition();
37 51
38 /* 52 /*
39 * Don't use https because it costs more then 100K memory. 53 * Don't use https because it costs more then 100K memory.
40 */ 54 */
75 ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); 89 ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
76 http_cleanup(client); 90 http_cleanup(client);
77 goto updateerr; 91 goto updateerr;
78 } 92 }
79 93
80 // TFT_print((char *)"Begin firmware download.\r\n", 0, LASTY); 94 screen_updating("Begin download", NULL);
81 ESP_LOGI(TAG, "Download update %s size %d", update.url, content_length); 95 ESP_LOGI(TAG, "Download update %s size %d", update.url, content_length);
82 int binary_file_length = 0; 96 int binary_file_length = 0;
83 /*deal with all receive packet*/ 97 /*deal with all receive packet*/
84 bool image_header_was_checked = false; 98 bool image_header_was_checked = false;
85 while (1) { 99 while (1) {
116 goto updateok; 130 goto updateok;
117 } 131 }
118 } 132 }
119 133
120 if (memcmp(new_app_info.app_elf_sha256, running_app_info.app_elf_sha256, 32) == 0) { 134 if (memcmp(new_app_info.app_elf_sha256, running_app_info.app_elf_sha256, 32) == 0) {
135 screen_updating("No new update", NULL);
121 ESP_LOGI(TAG, "Current running version is the same as a new."); 136 ESP_LOGI(TAG, "Current running version is the same as a new.");
122 http_cleanup(client); 137 http_cleanup(client);
123 goto updateok; 138 goto updateok;
124 } 139 }
125 140
159 } 174 }
160 if (esp_ota_end(update_handle) != ESP_OK) { 175 if (esp_ota_end(update_handle) != ESP_OK) {
161 ESP_LOGE(TAG, "esp_ota_end failed!"); 176 ESP_LOGE(TAG, "esp_ota_end failed!");
162 goto updateerr; 177 goto updateerr;
163 } 178 }
164 snprintf(temp, 63, "Received image %d bytes\r\n", binary_file_length); 179 snprintf(temp, 63, "Ok %d bytes", binary_file_length);
165 // TFT_print(temp, 0, LASTY); 180 screen_updating("Begin download", temp);
166 181
167 /* 182 /*
168 * Here we have new version, install and boot it. 183 * Here we have new version, install and boot it.
169 */ 184 */
170 err = esp_ota_set_boot_partition(update_partition); 185 err = esp_ota_set_boot_partition(update_partition);
172 ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); 187 ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err));
173 goto updateerr; 188 goto updateerr;
174 } 189 }
175 190
176 ESP_LOGI(TAG, "Prepare to restart system!"); 191 ESP_LOGI(TAG, "Prepare to restart system!");
177 // TFT_print((char *)"Rebooting ...", 0, LASTY); 192 screen_updating("Reboot...", "...Reboot");
178 vTaskDelay(1000 / portTICK_PERIOD_MS); 193 vTaskDelay(1000 / portTICK_PERIOD_MS);
194 update_running = 0;
179 esp_restart(); 195 esp_restart();
180 return; 196 return;
181 197
182 updateerr: 198 updateerr:
183 // _fg = TFT_RED; 199 screen_updating("** ERROR **", "Update failed");
184 // TFT_print((char *)"Error\r\n", 0, LASTY);
185 200
186 updateok: 201 updateok:
202 update_running = 0;
203 requestWiFi_user(false);
187 vTaskDelay(3000 / portTICK_PERIOD_MS); 204 vTaskDelay(3000 / portTICK_PERIOD_MS);
188 } 205 }
189 206
190 207
191
192 /**
193 * @brief Download a file to /spiffs
194 * @param filename The name and path of the file to download.
195 * @return Return 0 if ok, negative if errors.
196 */
197 int DownloadSpiffs(char *filename)
198 {
199 esp_err_t err;
200 static char theurl[73], thefile[41];
201 FILE *f;
202
203 // static char todel[41];
204 // snprintf(todel, 40, "/spiffs//%s", filename);
205 // unlink(todel);
206 // return 0;
207
208 snprintf(theurl, 72, "http://update.mbse.eu/ap1/image/%s", filename);
209 snprintf(thefile, 40, "/spiffs/%s", filename);
210
211 esp_http_client_config_t update = {
212 .url = theurl,
213 };
214
215 esp_http_client_handle_t client = esp_http_client_init(&update);
216 if (client == NULL) {
217 ESP_LOGE(TAG, "Failed to init HTTP connection");
218 return -1;
219 }
220
221 err = esp_http_client_open(client, 0);
222 if (err != ESP_OK) {
223 ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
224 esp_http_client_cleanup(client);
225 return -1;
226 }
227
228 int content_length = esp_http_client_fetch_headers(client);
229 int status_code = esp_http_client_get_status_code(client);
230 if (status_code != 200) {
231 ESP_LOGE(TAG, "GET %s error %d", update.url, status_code);
232 esp_http_client_cleanup(client);
233 return -1;
234 }
235
236 /*
237 * Remove a possible stale download.
238 */
239 unlink("/spiffs/tmpfile");
240 f = fopen("/spiffs/tmpfile", "w");
241 if (f == NULL) {
242 ESP_LOGE(TAG, "Cannot create /spiffs/tmpfile");
243 esp_http_client_cleanup(client);
244 return -1;
245 }
246
247 int read_length = 0;
248 int write_length = 0;
249 while (1) {
250 int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
251 if (data_read < 0) {
252 ESP_LOGE(TAG, "Error: data read error %s", theurl);
253 http_cleanup(client);
254 return -1;
255 } else if (data_read > 0) {
256 size_t bytes = fwrite(ota_write_data, 1, data_read, f);
257 if (bytes != data_read) {
258 ESP_LOGE(TAG, "fwrite %s %d/%d at %d", theurl, bytes, data_read, write_length);
259 }
260 write_length += bytes;
261 read_length += data_read;
262 } else if (data_read == 0) {
263 break;
264 }
265 vTaskDelay(10 / portTICK_PERIOD_MS);
266 }
267 fclose(f);
268
269 if (content_length != write_length) {
270 ESP_LOGE(TAG, "Download %s size %d but got %d bytes", theurl, content_length, write_length);
271 unlink("/spiffs/tmpfile");
272 return -1;
273 }
274
275 ESP_LOGI(TAG, "Download %s size %d Ok", theurl, content_length);
276 unlink(thefile);
277 rename("/spiffs/tmpfile", thefile);
278 esp_http_client_cleanup(client);
279 return 0;
280 }
281
282
283
284 /*
285 * Files init function, only runs once a new screen is entered.
286 */
287 void Updates_Init(void)
288 {
289 // _bg = TFT_BLACK;
290 // TFT_fillScreen(_bg);
291 // TopMessage((char *)"Update");
292
293 }
294
295
296
297 /*
298 * Updates management loop, non-blocking.
299 */
300 void Updates_Loop(void)
301 {
302 bin_update();
303 }
304
305

mercurial