Skip to content

Commit 42dc133

Browse files
committed
Finished View Upload Component and polished endpoint
- removed useless test-function from web_fs.c - increased partition size again to now 512kbyte
1 parent d63bebc commit 42dc133

File tree

4 files changed

+167
-75
lines changed

4 files changed

+167
-75
lines changed

main/web_fs.c

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,6 @@
1515

1616
static const char* TAG = "web_fs";
1717

18-
// temporary function to test successful SPIFFS file upload
19-
static void read_hello_txt()
20-
{
21-
ESP_LOGI(TAG, "Reading index.html");
22-
23-
FILE* f = fopen("/www/index.html", "r");
24-
if (f == NULL) {
25-
ESP_LOGE(TAG, "Failed to open index.html");
26-
return;
27-
}
28-
29-
char buf[512];
30-
memset(buf, 0, sizeof(buf));
31-
fread(buf, 1, sizeof(buf), f);
32-
fclose(f);
33-
34-
ESP_LOGI(TAG, "File content:\n%s", buf);
35-
}
36-
3718
static esp_err_t check_response(esp_err_t ret)
3819
{
3920
if (ret != ESP_OK) {
@@ -66,7 +47,7 @@ esp_err_t init_fs(void)
6647
.base_path = "/stm_ota",
6748
.partition_label = "stm_ota",
6849
.max_files = 1,
69-
.format_if_mount_failed = false
50+
.format_if_mount_failed = true
7051
};
7152
if(check_response(esp_vfs_spiffs_register(&ota_conf)) != ESP_OK) {
7253
return ESP_FAIL;
@@ -75,12 +56,17 @@ esp_err_t init_fs(void)
7556
size_t total = 0, used = 0;
7657
esp_err_t ret = esp_spiffs_info("website", &total, &used);
7758
if (ret != ESP_OK) {
78-
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
59+
ESP_LOGE(TAG, "Failed to get SPIFFS partition information for website (%s)", esp_err_to_name(ret));
7960
} else {
80-
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
61+
ESP_LOGI(TAG, "Partition size for website: total: %d, used: %d", total, used);
8162
}
8263

83-
read_hello_txt();
64+
ret = esp_spiffs_info("stm_ota", &total, &used);
65+
if (ret != ESP_OK) {
66+
ESP_LOGE(TAG, "Failed to get SPIFFS partition information for ota (%s)", esp_err_to_name(ret));
67+
} else {
68+
ESP_LOGI(TAG, "Partition size ota: total: %d, used: %d", total, used);
69+
}
8470

8571
return ESP_OK;
8672
}

main/web_server.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,44 @@ static esp_err_t ota_start_handler(httpd_req_t *req)
265265
return ESP_OK;
266266
}
267267

268+
esp_err_t ota_upload_handler(httpd_req_t *req)
269+
{
270+
if (req->content_len > 0x20000) {
271+
httpd_resp_set_status(req, "413");
272+
httpd_resp_sendstr(req, "Maximum size: 128 kByte");
273+
return ESP_OK;
274+
}
275+
276+
unsigned int bytes_received = 0;
277+
unsigned int bytes_written = 0;
278+
int buffer_size = 8*1024;
279+
char * buf = malloc(buffer_size);
280+
FILE *fd = fopen("/stm_ota/firmware.bin", "w");
281+
if (fd == NULL) {
282+
ESP_LOGE(TAG, "Failed to create file for image");
283+
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to create file");
284+
goto out;
285+
}
286+
while (req->content_len - bytes_received > 0) {
287+
int chunk_size = httpd_req_recv(req, buf, buffer_size);
288+
bytes_received += chunk_size;
289+
ESP_LOGD(TAG, "Got chunk of %d bytes", chunk_size);
290+
bytes_written = fwrite(buf, 1, chunk_size, fd);
291+
ESP_LOGD(TAG, "Wrote chunk of %d bytes", bytes_written);
292+
if (ferror(fd)) {
293+
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to write file");
294+
goto out;
295+
}
296+
}
297+
ESP_LOGD(TAG, "Wrote %d to flash", bytes_received);
298+
httpd_resp_set_status(req, "200");
299+
httpd_resp_send(req, NULL, 0);
300+
out:
301+
free(buf);
302+
fclose(fd);
303+
return ESP_OK;
304+
}
305+
268306
esp_err_t start_web_server(const char *base_path)
269307
{
270308
if (base_path == NULL) {
@@ -345,6 +383,15 @@ esp_err_t start_web_server(const char *base_path)
345383
};
346384
httpd_register_uri_handler(server, &ota_start_uri);
347385

386+
httpd_uri_t ota_upload_uri = {
387+
.uri = "/api/v1/ota/upload",
388+
.method = HTTP_POST,
389+
.handler = ota_upload_handler,
390+
.user_ctx = server_ctx
391+
};
392+
httpd_register_uri_handler(server, &ota_upload_uri);
393+
394+
348395
/* URI handler for getting web server files */
349396
httpd_uri_t common_get_uri = {
350397
.uri = "/*",

partitions.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
nvs, data, nvs, 0x9000, 0x6000,
44
phy_init, data, phy, 0xf000, 0x1000,
55
factory, app, factory, 0x10000, 2M,
6-
website, data, spiffs, , 336K,
6+
website, data, spiffs, , 512K,
77
stm_ota, data, spiffs, , 0x20000

webapp/src/views/Ota.vue

Lines changed: 110 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,69 @@
11
<template>
2-
<v-container>
3-
<v-row justify="center">
4-
<v-col cols=5>
5-
<div class="ma-auto">
6-
Select an image from your PC to upload. Once uploaded, you can start the update process
7-
</div>
8-
</v-col>
9-
</v-row>
10-
<v-row justify="center">
11-
<v-col cols=2 >
12-
<v-file-input
13-
v-model="file"
14-
label="File input"
15-
outlined
16-
dense
17-
:show-size="1000"
18-
></v-file-input>
19-
</v-col>
20-
<v-col cols=1>
21-
<v-btn
22-
:loading="uploading"
23-
:disabled="uploading"
24-
@click="upload()">
25-
Upload</v-btn>
26-
</v-col>
27-
<v-col cols=1>
28-
<v-btn
29-
:loading="flashing"
30-
:disabled="disabled"
31-
@click="flash()">
32-
Start Flashing</v-btn></v-col>
33-
<v-col cols=1></v-col>
34-
</v-row>
35-
<v-row justify="center">
36-
<v-col cols=5>
37-
<div class="ma-auto">
38-
{{ status }}
39-
</div>
40-
</v-col>
41-
</v-row>
42-
</v-container>
2+
<v-container>
3+
<v-layout text-xs-center wrap>
4+
<v-flex xs12 sm6 offset-sm3>
5+
<v-card>
6+
<v-card-title class="justify-center">Over-the-Air-Upgrade</v-card-title>
7+
<v-card-text>
8+
<v-row justify="center" dense>
9+
<v-col cols=10>
10+
<div class="ma-auto">
11+
Select an image from your PC to upload. Once uploaded, you can start the update process
12+
</div>
13+
</v-col>
14+
</v-row>
15+
<v-row justify="center">
16+
<v-col cols=10 >
17+
<v-file-input
18+
v-model="file"
19+
label="File input"
20+
outlined
21+
dense
22+
:show-size="1000"
23+
></v-file-input>
24+
</v-col>
25+
</v-row>
26+
<v-row justify="center" dense>
27+
<v-spacer></v-spacer>
28+
<v-col cols=2>
29+
<v-btn
30+
:loading="uploading"
31+
:disabled="upload_disabled"
32+
@click="upload()">
33+
Upload</v-btn>
34+
</v-col>
35+
<v-spacer></v-spacer>
36+
<v-col cols=2>
37+
<v-btn
38+
:loading="flashing"
39+
:disabled="flash_disabled"
40+
@click="flash()">
41+
Flash</v-btn></v-col>
42+
<v-spacer></v-spacer>
43+
</v-row>
44+
<v-row justify="center">
45+
<v-alert
46+
v-model="alert"
47+
dense
48+
outlined
49+
type="error"
50+
transition="scale-transition"
51+
dismissible
52+
>{{status}}</v-alert>
53+
<v-alert
54+
v-model="success"
55+
dense
56+
outlined
57+
type="success"
58+
transition="scale-transition"
59+
dismissible
60+
>{{status}}</v-alert>
61+
</v-row>
62+
</v-card-text>
63+
</v-card>
64+
</v-flex>
65+
</v-layout>
66+
</v-container>
4367
</template>
4468

4569
<script>
@@ -49,23 +73,32 @@ export default {
4973
return {
5074
uploading: false,
5175
flashing: false,
52-
start_flash_btn: true,
76+
disable_flash_btn: true,
77+
disable_upload_btn: false,
5378
file: null,
54-
status: null
79+
status: null,
80+
alert: false,
81+
success: false
5582
}
5683
},
5784
computed: {
58-
disabled() {
59-
return this.start_flash_btn;
85+
flash_disabled() {
86+
return this.disable_flash_btn;
87+
},
88+
upload_disabled() {
89+
return this.disable_upload_btn;
6090
}
6191
},
6292
methods: {
6393
upload: function() {
6494
this.uploading = !this.uploading;
95+
this.disable_upload_btn = true
6596
var reader = new FileReader();
6697
if(!this.file) {
6798
this.status = "No file selected"
99+
this.alert = true
68100
this.uploading = !this.uploading;
101+
this.disable_upload_btn = false
69102
return;
70103
}
71104
reader.readAsArrayBuffer(this.file);
@@ -74,18 +107,44 @@ export default {
74107
.post('api/v1/ota/upload', reader.result)
75108
.then(res => {
76109
this.status = "Image upload successfull";
77-
this.start_flash_btn = !this.start_flash_btn;
110+
this.disable_flash_btn = false;
111+
this.disable_upload_btn = false
112+
this.uploading = !this.uploading;
113+
this.alert=false
114+
this.success=true
78115
})
79116
.catch(error => {
80117
this.status = "Could not upload image - Statuscode: " + error.response.status
81118
this.uploading = !this.uploading;
119+
this.disable_upload_btn = false
120+
this.disable_flash_btn = true
121+
this.alert=true
122+
this.success=false
123+
});
124+
}},
125+
flash: function() {
126+
this.flashing = !this.flashing;
127+
this.disable_flash_btn = true
128+
let id = this.$store.state.active_device_id
129+
this.$ajax
130+
.get('api/v1/ota/' + 'kk8a4zv')
131+
.then(res => {
132+
this.status = "Image flashed successfully";
133+
this.disable_flash_btn = false;
134+
this.disable_upload_btn = false
135+
this.flashing = !this.uploading;
136+
this.alert=false
137+
this.success=true
138+
})
139+
.catch(error => {
140+
this.status = "Could not be flashed: " + error.response.status + "-" + error.response.data
141+
this.flashing = !this.flashing;
142+
this.disable_flash_btn = false
143+
this.disable_upload_btn = false
144+
this.success=false
145+
this.alert=true
82146
});
83147
}
84-
},
85-
flash: function() {
86-
this.flashing = !this.flashing
87-
this.start_flash_btn = !this.start_flash_btn
88148
}
89-
}
90149
}
91150
</script>

0 commit comments

Comments
 (0)