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