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) { |
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 |
|