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