Wed, 24 Oct 2018 11:55:39 +0200
Removed old logfiles
0 | 1 | /** |
2 | * @file updates.c | |
4 | 3 | * @brief Updates management. It can download and install new firmware |
4 | * downloaded from the internet. | |
0 | 5 | */ |
6 | ||
7 | #include "config.h" | |
8 | ||
9 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
10 | #define BUFFSIZE 1024 ///< Download buffer size |
0 | 11 | static char ota_write_data[BUFFSIZE + 1] = { 0 }; |
12 | static const char *TAG = "update"; | |
13 | ||
14 | extern sButton Buttons[MAXBUTTONS]; | |
15 | extern int Main_Screen; | |
16 | ||
17 | ||
18 | static void http_cleanup(esp_http_client_handle_t client) | |
19 | { | |
20 | esp_http_client_close(client); | |
21 | esp_http_client_cleanup(client); | |
22 | } | |
23 | ||
24 | ||
25 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
26 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
27 | * @brief Run update procedure |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
28 | */ |
0 | 29 | void run_update(void) |
30 | { | |
31 | char temp[64]; | |
32 | esp_err_t err; | |
33 | const esp_partition_t *update_partition = NULL; | |
34 | esp_ota_handle_t update_handle = 0; | |
35 | ||
36 | TFT_setFont(DEJAVU18_FONT, NULL); | |
37 | _fg = TFT_CYAN; | |
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", | |
40 | running->type, running->subtype, running->address); | |
41 | TFT_print(temp, 0, 25); | |
42 | ||
43 | /* | |
44 | * Don't use https because it costs more then 100K memory. | |
45 | */ | |
46 | esp_http_client_config_t update = { | |
47 | .url = "http://seaport.mbse.ym/update/brewboard.bin", | |
48 | }; | |
49 | ||
50 | esp_http_client_handle_t client = esp_http_client_init(&update); | |
51 | if (client == NULL) { | |
52 | ESP_LOGI(TAG, "Failed to init HTTP connection"); | |
53 | goto updateerr; | |
54 | } | |
55 | ||
56 | err = esp_http_client_open(client, 0); | |
57 | if (err != ESP_OK) { | |
58 | ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err)); | |
59 | esp_http_client_cleanup(client); | |
60 | goto updateerr; | |
61 | } | |
62 | ||
63 | esp_http_client_fetch_headers(client); | |
64 | update_partition = esp_ota_get_next_update_partition(NULL); | |
65 | if (update_partition == NULL) { | |
66 | ESP_LOGE(TAG, "No update partition"); | |
67 | esp_http_client_cleanup(client); | |
68 | goto updateerr; | |
69 | } | |
70 | ESP_LOGI(TAG, "Update to partition subtype %d at offset 0x%x", update_partition->subtype, update_partition->address); | |
71 | ||
72 | err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle); | |
73 | if (err != ESP_OK) { | |
74 | ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); | |
75 | http_cleanup(client); | |
76 | goto updateerr; | |
77 | } | |
78 | ||
79 | TFT_print("Begin download.\r\n", 0, LASTY); | |
80 | ESP_LOGI(TAG, "Download update %s", update.url); | |
81 | int binary_file_length = 0; | |
82 | /*deal with all receive packet*/ | |
83 | while (1) { | |
84 | int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE); | |
85 | if (data_read < 0) { | |
86 | ESP_LOGE(TAG, "Error: data read error"); | |
87 | http_cleanup(client); | |
88 | goto updateerr;; | |
89 | } else if (data_read > 0) { | |
90 | err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read); | |
91 | if (err != ESP_OK) { | |
92 | http_cleanup(client); | |
93 | goto updateerr;; | |
94 | } | |
95 | binary_file_length += data_read; | |
96 | // ESP_LOGD(TAG, "Written image length %d", binary_file_length); | |
97 | } else if (data_read == 0) { | |
98 | break; | |
99 | } | |
100 | } | |
101 | ||
102 | ESP_LOGI(TAG, "Download complete, binary data length: %d", binary_file_length); | |
103 | http_cleanup(client); | |
104 | ||
105 | if (esp_ota_end(update_handle) != ESP_OK) { | |
106 | ESP_LOGE(TAG, "esp_ota_end failed!"); | |
107 | goto updateerr; | |
108 | } | |
109 | snprintf(temp, 63, "Received image %d bytes\r\n", binary_file_length); | |
110 | TFT_print(temp, 0, LASTY); | |
111 | ||
112 | if (esp_partition_check_identity(esp_ota_get_running_partition(), update_partition) == true) { | |
113 | ESP_LOGI(TAG, "Already the latest version"); | |
114 | TFT_print("Already the latest version.\r\n", LASTX, LASTY); | |
115 | goto updateok; | |
116 | } | |
117 | ||
118 | /* | |
119 | * Here we have a different and hopefully newer version, install and boot it. | |
120 | */ | |
121 | err = esp_ota_set_boot_partition(update_partition); | |
122 | if (err != ESP_OK) { | |
123 | ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); | |
124 | goto updateerr; | |
125 | } | |
126 | ||
127 | ESP_LOGI(TAG, "Prepare to restart system!"); | |
128 | TFT_print("Rebooting ...", 0, LASTY); | |
129 | vTaskDelay(1000 / portTICK_PERIOD_MS); | |
130 | esp_restart(); | |
131 | return ; | |
132 | ||
133 | updateerr: | |
134 | _fg = TFT_RED; | |
135 | TFT_print("Error\r\n", 0, LASTY); | |
136 | ||
137 | updateok: | |
138 | vTaskDelay(3000 / portTICK_PERIOD_MS); | |
139 | } | |
140 | ||
141 | ||
142 | ||
143 | /* | |
144 | * Files init function, only runs once a new screen is entered. | |
145 | */ | |
146 | void Updates_Init(void) | |
147 | { | |
148 | switch (Main_Screen) { | |
149 | case MAIN_TOOLS_UPDATES: | |
150 | _bg = TFT_BLACK; | |
151 | TFT_fillScreen(_bg); | |
152 | TopMessage("Update"); | |
153 | break; | |
154 | ||
155 | default: break; | |
156 | } | |
157 | } | |
158 | ||
159 | ||
160 | ||
161 | /* | |
162 | * Updates management loop, non-blocking. | |
163 | */ | |
164 | void Updates_Loop(void) | |
165 | { | |
166 | switch (Main_Screen) { | |
167 | ||
168 | case MAIN_TOOLS_UPDATES: | |
169 | run_update(); | |
170 | Main_Screen = MAIN_TOOLS; | |
171 | break; | |
172 | ||
173 | default: break; | |
174 | } | |
175 | } | |
176 | ||
177 |