Sat, 18 May 2019 11:34:51 +0200
SNTP uses a callback function to mark the system time is valid. SNTP is started after we received a DHCP ip address. Added an config option to set a preffered NTP server name, most likely on your most used LAN. It will always fal back to pool.ntp.org.
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 | /** |
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
27 | * @brief Run binary update procedure |
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
28 | */ |
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
29 | void bin_update(void) |
0 | 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); | |
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
41 | TFT_print(temp, 0, LASTY); |
0 | 42 | |
43 | /* | |
44 | * Don't use https because it costs more then 100K memory. | |
45 | */ | |
46 | esp_http_client_config_t update = { | |
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
47 | .url = "http://update.mbse.eu/ap1/fw/brewboard.bin", |
0 | 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 | ||
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
63 | int content_length = esp_http_client_fetch_headers(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
64 | int status_code = esp_http_client_get_status_code(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
65 | if (status_code != 200) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
66 | ESP_LOGE(TAG, "GET %s error %d", update.url, status_code); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
67 | esp_http_client_cleanup(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
68 | goto updateerr; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
69 | } |
0 | 70 | update_partition = esp_ota_get_next_update_partition(NULL); |
71 | if (update_partition == NULL) { | |
72 | ESP_LOGE(TAG, "No update partition"); | |
73 | esp_http_client_cleanup(client); | |
74 | goto updateerr; | |
75 | } | |
76 | ESP_LOGI(TAG, "Update to partition subtype %d at offset 0x%x", update_partition->subtype, update_partition->address); | |
77 | ||
78 | err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle); | |
79 | if (err != ESP_OK) { | |
80 | ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); | |
81 | http_cleanup(client); | |
82 | goto updateerr; | |
83 | } | |
84 | ||
85 | TFT_print("Begin download.\r\n", 0, LASTY); | |
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
86 | ESP_LOGI(TAG, "Download update %s size %d", update.url, content_length); |
0 | 87 | int binary_file_length = 0; |
88 | /*deal with all receive packet*/ | |
89 | while (1) { | |
90 | int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE); | |
91 | if (data_read < 0) { | |
92 | ESP_LOGE(TAG, "Error: data read error"); | |
93 | http_cleanup(client); | |
94 | goto updateerr;; | |
95 | } else if (data_read > 0) { | |
96 | err = esp_ota_write( update_handle, (const void *)ota_write_data, data_read); | |
97 | if (err != ESP_OK) { | |
98 | http_cleanup(client); | |
99 | goto updateerr;; | |
100 | } | |
101 | binary_file_length += data_read; | |
102 | // ESP_LOGD(TAG, "Written image length %d", binary_file_length); | |
103 | } else if (data_read == 0) { | |
104 | break; | |
105 | } | |
106 | } | |
107 | ||
108 | ESP_LOGI(TAG, "Download complete, binary data length: %d", binary_file_length); | |
109 | http_cleanup(client); | |
110 | ||
111 | if (esp_ota_end(update_handle) != ESP_OK) { | |
112 | ESP_LOGE(TAG, "esp_ota_end failed!"); | |
113 | goto updateerr; | |
114 | } | |
115 | snprintf(temp, 63, "Received image %d bytes\r\n", binary_file_length); | |
116 | TFT_print(temp, 0, LASTY); | |
117 | ||
118 | if (esp_partition_check_identity(esp_ota_get_running_partition(), update_partition) == true) { | |
119 | ESP_LOGI(TAG, "Already the latest version"); | |
120 | TFT_print("Already the latest version.\r\n", LASTX, LASTY); | |
121 | goto updateok; | |
122 | } | |
123 | ||
124 | /* | |
125 | * Here we have a different and hopefully newer version, install and boot it. | |
126 | */ | |
127 | err = esp_ota_set_boot_partition(update_partition); | |
128 | if (err != ESP_OK) { | |
129 | ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); | |
130 | goto updateerr; | |
131 | } | |
132 | ||
133 | ESP_LOGI(TAG, "Prepare to restart system!"); | |
134 | TFT_print("Rebooting ...", 0, LASTY); | |
135 | vTaskDelay(1000 / portTICK_PERIOD_MS); | |
136 | esp_restart(); | |
137 | return ; | |
138 | ||
139 | updateerr: | |
140 | _fg = TFT_RED; | |
141 | TFT_print("Error\r\n", 0, LASTY); | |
142 | ||
143 | updateok: | |
144 | vTaskDelay(3000 / portTICK_PERIOD_MS); | |
145 | } | |
146 | ||
147 | ||
148 | ||
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
149 | /** |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
150 | * @brief Download a file to /spiffs |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
151 | * @param filename The name and path of the file to download. |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
152 | * @return Return 0 if ok, negative if errors. |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
153 | */ |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
154 | int DownloadSpiffs(char *filename) |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
155 | { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
156 | esp_err_t err; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
157 | static char theurl[73], thefile[41]; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
158 | FILE *f; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
159 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
160 | // static char todel[41]; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
161 | // snprintf(todel, 40, "/spiffs//%s", filename); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
162 | // unlink(todel); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
163 | // return 0; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
164 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
165 | snprintf(theurl, 72, "http://update.mbse.eu/ap1/image/%s", filename); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
166 | snprintf(thefile, 40, "/spiffs/%s", filename); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
167 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
168 | esp_http_client_config_t update = { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
169 | .url = theurl, |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
170 | }; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
171 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
172 | esp_http_client_handle_t client = esp_http_client_init(&update); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
173 | if (client == NULL) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
174 | ESP_LOGE(TAG, "Failed to init HTTP connection"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
175 | return -1; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
176 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
177 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
178 | err = esp_http_client_open(client, 0); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
179 | if (err != ESP_OK) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
180 | ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err)); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
181 | esp_http_client_cleanup(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
182 | return -1; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
183 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
184 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
185 | int content_length = esp_http_client_fetch_headers(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
186 | int status_code = esp_http_client_get_status_code(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
187 | if (status_code != 200) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
188 | ESP_LOGE(TAG, "GET %s error %d", update.url, status_code); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
189 | esp_http_client_cleanup(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
190 | return -1; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
191 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
192 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
193 | /* |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
194 | * Remove a possible stale download. |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
195 | */ |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
196 | unlink("/spiffs/tmpfile"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
197 | f = fopen("/spiffs/tmpfile", "w"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
198 | if (f == NULL) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
199 | ESP_LOGE(TAG, "Cannot create /spiffs/tmpfile"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
200 | esp_http_client_cleanup(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
201 | return -1; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
202 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
203 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
204 | int read_length = 0; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
205 | int write_length = 0; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
206 | while (1) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
207 | int data_read = esp_http_client_read(client, ota_write_data, BUFFSIZE); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
208 | if (data_read < 0) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
209 | ESP_LOGE(TAG, "Error: data read error %s", theurl); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
210 | http_cleanup(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
211 | return -1; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
212 | } else if (data_read > 0) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
213 | size_t bytes = fwrite(ota_write_data, 1, data_read, f); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
214 | if (bytes != data_read) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
215 | ESP_LOGE(TAG, "fwrite %s %d/%d at %d", theurl, bytes, data_read, write_length); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
216 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
217 | write_length += bytes; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
218 | read_length += data_read; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
219 | } else if (data_read == 0) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
220 | break; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
221 | } |
42 | 222 | vTaskDelay(10 / portTICK_PERIOD_MS); |
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
223 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
224 | fclose(f); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
225 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
226 | if (content_length != write_length) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
227 | ESP_LOGE(TAG, "Download %s size %d but got %d bytes", theurl, content_length, write_length); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
228 | unlink("/spiffs/tmpfile"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
229 | return -1; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
230 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
231 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
232 | ESP_LOGI(TAG, "Download %s size %d Ok", theurl, content_length); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
233 | unlink(thefile); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
234 | rename("/spiffs/tmpfile", thefile); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
235 | esp_http_client_cleanup(client); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
236 | return 0; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
237 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
238 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
239 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
240 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
241 | /** |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
242 | * @brief Update /spiffs filesystem |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
243 | */ |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
244 | void spiffs_update(void) |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
245 | { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
246 | int rc; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
247 | FILE *f; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
248 | char v1[12], v2[12], fn[41]; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
249 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
250 | TFT_setFont(DEJAVU18_FONT, NULL); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
251 | _fg = TFT_CYAN; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
252 | TFT_print("Update /spiffs ", 0, 25); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
253 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
254 | rc = rename("/spiffs/version.txt", "/spiffs/version.old"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
255 | if ((rc != 0) && (errno == ENOENT)) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
256 | /* No old file. */ |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
257 | ESP_LOGI(TAG, "No old /spiffs/version.txt"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
258 | /* Download, install old and new */ |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
259 | DownloadSpiffs("version.txt"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
260 | rename("/spiffs/version.txt", "/spiffs/version.old"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
261 | DownloadSpiffs("version.txt"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
262 | goto spiffs_update; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
263 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
264 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
265 | if (DownloadSpiffs("version.txt") < 0) |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
266 | goto spiffs_error; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
267 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
268 | /* Compare spiffs/version.old and /spiffs/version.txt */ |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
269 | v1[0] = '\0'; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
270 | v2[0] = '\0'; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
271 | f = fopen("/spiffs/version.old", "r"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
272 | if (f) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
273 | fgets(v1, 11, f); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
274 | fclose(f); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
275 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
276 | f = fopen("/spiffs/version.txt", "r"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
277 | if (f) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
278 | fgets(v2, 11, f); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
279 | fclose(f); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
280 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
281 | if (strcmp(v1, v2) == 0) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
282 | ESP_LOGI(TAG, "/spiffs is up to date"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
283 | TFT_print("Ok\r\n", LASTX, LASTY); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
284 | unlink("/spiffs/version.old"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
285 | return; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
286 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
287 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
288 | spiffs_update: |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
289 | /* |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
290 | * Run the update, get the filelist. |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
291 | */ |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
292 | ESP_LOGI(TAG, "Full /spiffs update"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
293 | rc = DownloadSpiffs("files.list"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
294 | if (rc < 0) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
295 | unlink("/spiffs/version.txt"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
296 | rename("/spiffs/version.old", "/spiffs/version.txt"); // So next time we try again. |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
297 | goto spiffs_error; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
298 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
299 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
300 | f = fopen("/spiffs/files.list", "r"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
301 | while (fgets(fn, 40, f)) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
302 | fn[strlen(fn)-1] = '\0'; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
303 | rc = DownloadSpiffs(fn); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
304 | if (rc < 0) { |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
305 | ESP_LOGE(TAG, "Updates failed"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
306 | fclose(f); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
307 | goto spiffs_error; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
308 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
309 | // vTaskDelay(10 / portTICK_PERIOD_MS); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
310 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
311 | fclose(f); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
312 | unlink("/spiffs/version.old"); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
313 | TFT_print("updated\r\n", LASTX, LASTY); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
314 | return; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
315 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
316 | spiffs_error: |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
317 | _fg = TFT_RED; |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
318 | TFT_print("error\r\n", LASTX, LASTY); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
319 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
320 | } |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
321 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
322 | |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
323 | |
0 | 324 | /* |
325 | * Files init function, only runs once a new screen is entered. | |
326 | */ | |
327 | void Updates_Init(void) | |
328 | { | |
329 | switch (Main_Screen) { | |
330 | case MAIN_TOOLS_UPDATES: | |
331 | _bg = TFT_BLACK; | |
332 | TFT_fillScreen(_bg); | |
333 | TopMessage("Update"); | |
334 | break; | |
335 | ||
336 | default: break; | |
337 | } | |
338 | } | |
339 | ||
340 | ||
341 | ||
342 | /* | |
343 | * Updates management loop, non-blocking. | |
344 | */ | |
345 | void Updates_Loop(void) | |
346 | { | |
347 | switch (Main_Screen) { | |
348 | ||
349 | case MAIN_TOOLS_UPDATES: | |
13
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
350 | spiffs_update(); |
8f01b74bf1dd
Update /spiffs via internet. http://update.mbse.eu is now the update server.
Michiel Broek <mbroek@mbse.eu>
parents:
4
diff
changeset
|
351 | bin_update(); |
0 | 352 | Main_Screen = MAIN_TOOLS; |
353 | break; | |
354 | ||
355 | default: break; | |
356 | } | |
357 | } | |
358 | ||
359 |