main/updates.c

changeset 13
8f01b74bf1dd
parent 4
6d1f512cd074
child 42
c6a1a6ca5437
equal deleted inserted replaced
12:063a21ca11f7 13:8f01b74bf1dd
22 } 22 }
23 23
24 24
25 25
26 /** 26 /**
27 * @brief Run update procedure 27 * @brief Run binary update procedure
28 */ 28 */
29 void run_update(void) 29 void bin_update(void)
30 { 30 {
31 char temp[64]; 31 char temp[64];
32 esp_err_t err; 32 esp_err_t err;
33 const esp_partition_t *update_partition = NULL; 33 const esp_partition_t *update_partition = NULL;
34 esp_ota_handle_t update_handle = 0; 34 esp_ota_handle_t update_handle = 0;
36 TFT_setFont(DEJAVU18_FONT, NULL); 36 TFT_setFont(DEJAVU18_FONT, NULL);
37 _fg = TFT_CYAN; 37 _fg = TFT_CYAN;
38 const esp_partition_t *running = esp_ota_get_running_partition(); 38 const esp_partition_t *running = esp_ota_get_running_partition();
39 snprintf(temp, 63, "Running part.type %d sub %d,\r\nat offset 0x%08x\r\n", 39 snprintf(temp, 63, "Running part.type %d sub %d,\r\nat offset 0x%08x\r\n",
40 running->type, running->subtype, running->address); 40 running->type, running->subtype, running->address);
41 TFT_print(temp, 0, 25); 41 TFT_print(temp, 0, LASTY);
42 42
43 /* 43 /*
44 * Don't use https because it costs more then 100K memory. 44 * Don't use https because it costs more then 100K memory.
45 */ 45 */
46 esp_http_client_config_t update = { 46 esp_http_client_config_t update = {
47 .url = "http://seaport.mbse.ym/update/brewboard.bin", 47 .url = "http://update.mbse.eu/ap1/fw/brewboard.bin",
48 }; 48 };
49 49
50 esp_http_client_handle_t client = esp_http_client_init(&update); 50 esp_http_client_handle_t client = esp_http_client_init(&update);
51 if (client == NULL) { 51 if (client == NULL) {
52 ESP_LOGI(TAG, "Failed to init HTTP connection"); 52 ESP_LOGI(TAG, "Failed to init HTTP connection");
58 ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err)); 58 ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
59 esp_http_client_cleanup(client); 59 esp_http_client_cleanup(client);
60 goto updateerr; 60 goto updateerr;
61 } 61 }
62 62
63 esp_http_client_fetch_headers(client); 63 int content_length = esp_http_client_fetch_headers(client);
64 int status_code = esp_http_client_get_status_code(client);
65 if (status_code != 200) {
66 ESP_LOGE(TAG, "GET %s error %d", update.url, status_code);
67 esp_http_client_cleanup(client);
68 goto updateerr;
69 }
64 update_partition = esp_ota_get_next_update_partition(NULL); 70 update_partition = esp_ota_get_next_update_partition(NULL);
65 if (update_partition == NULL) { 71 if (update_partition == NULL) {
66 ESP_LOGE(TAG, "No update partition"); 72 ESP_LOGE(TAG, "No update partition");
67 esp_http_client_cleanup(client); 73 esp_http_client_cleanup(client);
68 goto updateerr; 74 goto updateerr;
75 http_cleanup(client); 81 http_cleanup(client);
76 goto updateerr; 82 goto updateerr;
77 } 83 }
78 84
79 TFT_print("Begin download.\r\n", 0, LASTY); 85 TFT_print("Begin download.\r\n", 0, LASTY);
80 ESP_LOGI(TAG, "Download update %s", update.url); 86 ESP_LOGI(TAG, "Download update %s size %d", update.url, content_length);
81 int binary_file_length = 0; 87 int binary_file_length = 0;
82 /*deal with all receive packet*/ 88 /*deal with all receive packet*/
83 while (1) { 89 while (1) {
84 int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE); 90 int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
85 if (data_read < 0) { 91 if (data_read < 0) {
138 vTaskDelay(3000 / portTICK_PERIOD_MS); 144 vTaskDelay(3000 / portTICK_PERIOD_MS);
139 } 145 }
140 146
141 147
142 148
149 /**
150 * @brief Download a file to /spiffs
151 * @param filename The name and path of the file to download.
152 * @return Return 0 if ok, negative if errors.
153 */
154 int DownloadSpiffs(char *filename)
155 {
156 esp_err_t err;
157 static char theurl[73], thefile[41];
158 FILE *f;
159
160 // static char todel[41];
161 // snprintf(todel, 40, "/spiffs//%s", filename);
162 // unlink(todel);
163 // return 0;
164
165 snprintf(theurl, 72, "http://update.mbse.eu/ap1/image/%s", filename);
166 snprintf(thefile, 40, "/spiffs/%s", filename);
167
168 esp_http_client_config_t update = {
169 .url = theurl,
170 };
171
172 esp_http_client_handle_t client = esp_http_client_init(&update);
173 if (client == NULL) {
174 ESP_LOGE(TAG, "Failed to init HTTP connection");
175 return -1;
176 }
177
178 err = esp_http_client_open(client, 0);
179 if (err != ESP_OK) {
180 ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
181 esp_http_client_cleanup(client);
182 return -1;
183 }
184
185 int content_length = esp_http_client_fetch_headers(client);
186 int status_code = esp_http_client_get_status_code(client);
187 if (status_code != 200) {
188 ESP_LOGE(TAG, "GET %s error %d", update.url, status_code);
189 esp_http_client_cleanup(client);
190 return -1;
191 }
192
193 /*
194 * Remove a possible stale download.
195 */
196 unlink("/spiffs/tmpfile");
197 f = fopen("/spiffs/tmpfile", "w");
198 if (f == NULL) {
199 ESP_LOGE(TAG, "Cannot create /spiffs/tmpfile");
200 esp_http_client_cleanup(client);
201 return -1;
202 }
203
204 int read_length = 0;
205 int write_length = 0;
206 while (1) {
207 int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE);
208 if (data_read < 0) {
209 ESP_LOGE(TAG, "Error: data read error %s", theurl);
210 http_cleanup(client);
211 return -1;
212 } else if (data_read > 0) {
213 size_t bytes = fwrite(ota_write_data, 1, data_read, f);
214 if (bytes != data_read) {
215 ESP_LOGE(TAG, "fwrite %s %d/%d at %d", theurl, bytes, data_read, write_length);
216 }
217 write_length += bytes;
218 read_length += data_read;
219 } else if (data_read == 0) {
220 break;
221 }
222 vTaskDelay(5 / portTICK_PERIOD_MS);
223 }
224 fclose(f);
225
226 if (content_length != write_length) {
227 ESP_LOGE(TAG, "Download %s size %d but got %d bytes", theurl, content_length, write_length);
228 unlink("/spiffs/tmpfile");
229 return -1;
230 }
231
232 ESP_LOGI(TAG, "Download %s size %d Ok", theurl, content_length);
233 unlink(thefile);
234 rename("/spiffs/tmpfile", thefile);
235 esp_http_client_cleanup(client);
236 return 0;
237 }
238
239
240
241 /**
242 * @brief Update /spiffs filesystem
243 */
244 void spiffs_update(void)
245 {
246 int rc;
247 FILE *f;
248 char v1[12], v2[12], fn[41];
249
250 TFT_setFont(DEJAVU18_FONT, NULL);
251 _fg = TFT_CYAN;
252 TFT_print("Update /spiffs ", 0, 25);
253
254 rc = rename("/spiffs/version.txt", "/spiffs/version.old");
255 if ((rc != 0) && (errno == ENOENT)) {
256 /* No old file. */
257 ESP_LOGI(TAG, "No old /spiffs/version.txt");
258 /* Download, install old and new */
259 DownloadSpiffs("version.txt");
260 rename("/spiffs/version.txt", "/spiffs/version.old");
261 DownloadSpiffs("version.txt");
262 goto spiffs_update;
263 }
264
265 if (DownloadSpiffs("version.txt") < 0)
266 goto spiffs_error;
267
268 /* Compare spiffs/version.old and /spiffs/version.txt */
269 v1[0] = '\0';
270 v2[0] = '\0';
271 f = fopen("/spiffs/version.old", "r");
272 if (f) {
273 fgets(v1, 11, f);
274 fclose(f);
275 }
276 f = fopen("/spiffs/version.txt", "r");
277 if (f) {
278 fgets(v2, 11, f);
279 fclose(f);
280 }
281 // ESP_LOG_BUFFER_HEXDUMP(TAG, v1, strlen(v1), ESP_LOG_INFO);
282 // ESP_LOG_BUFFER_HEXDUMP(TAG, v2, strlen(v2), ESP_LOG_INFO);
283 if (strcmp(v1, v2) == 0) {
284 ESP_LOGI(TAG, "/spiffs is up to date");
285 TFT_print("Ok\r\n", LASTX, LASTY);
286 unlink("/spiffs/version.old");
287 return;
288 }
289
290 spiffs_update:
291 /*
292 * Run the update, get the filelist.
293 */
294 ESP_LOGI(TAG, "Full /spiffs update");
295 rc = DownloadSpiffs("files.list");
296 if (rc < 0) {
297 unlink("/spiffs/version.txt");
298 rename("/spiffs/version.old", "/spiffs/version.txt"); // So next time we try again.
299 goto spiffs_error;
300 }
301
302 f = fopen("/spiffs/files.list", "r");
303 while (fgets(fn, 40, f)) {
304 fn[strlen(fn)-1] = '\0';
305 rc = DownloadSpiffs(fn);
306 if (rc < 0) {
307 ESP_LOGE(TAG, "Updates failed");
308 fclose(f);
309 goto spiffs_error;
310 }
311 // vTaskDelay(10 / portTICK_PERIOD_MS);
312 }
313 fclose(f);
314 unlink("/spiffs/version.old");
315 TFT_print("updated\r\n", LASTX, LASTY);
316 return;
317
318 spiffs_error:
319 _fg = TFT_RED;
320 TFT_print("error\r\n", LASTX, LASTY);
321
322 }
323
324
325
143 /* 326 /*
144 * Files init function, only runs once a new screen is entered. 327 * Files init function, only runs once a new screen is entered.
145 */ 328 */
146 void Updates_Init(void) 329 void Updates_Init(void)
147 { 330 {
164 void Updates_Loop(void) 347 void Updates_Loop(void)
165 { 348 {
166 switch (Main_Screen) { 349 switch (Main_Screen) {
167 350
168 case MAIN_TOOLS_UPDATES: 351 case MAIN_TOOLS_UPDATES:
169 run_update(); 352 spiffs_update();
353 bin_update();
170 Main_Screen = MAIN_TOOLS; 354 Main_Screen = MAIN_TOOLS;
171 break; 355 break;
172 356
173 default: break; 357 default: break;
174 } 358 }

mercurial