Skip to content

Commit 8aea75a

Browse files
committed
Merge branch 'release/0.4.3' into main
2 parents 62796f6 + 9502ec7 commit 8aea75a

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

mos.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
author: mark dornbach <mark@dornbach.io>
22
description: a minimal firmware for ota flashing tasmota from mongoose os
33
platform: esp8266
4-
version: 0.4.2
4+
version: 0.4.3
55

66
libs_version: ${mos.version}
77
modules_version: ${mos.version}
@@ -36,7 +36,7 @@ conds:
3636
- when: build_vars.TARGETFW == "tasmota"
3737
apply:
3838
config_schema:
39-
- ["mg2x.url", "s", "https://ota.tasmota.com/tasmota/release/tasmota.bin, {"title": "URL of target firmware"}]
39+
- ["mg2x.url", "s", "https://ota.tasmota.com/tasmota/release/tasmota.bin", {"title": "URL of target firmware"}]
4040
- when: build_vars.TARGETFW == "haa"
4141
apply:
4242
config_schema:

src/main.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data, void *ud) {
9696
switch (ev) {
9797
case MG_EV_CONNECT:
9898
// sent when a new outbound connection is created
99-
state->status = *(int *) ev_data;
99+
state->status = 0;
100100
break;
101101
case MG_EV_HTTP_CHUNK:
102102
// esp8266 expects flash to be erased and written in blocks of 4kb, but webservers send
103103
// chunks of data in whatever size they please. we thus buffer the data and write it
104104
// whenever a block is full
105+
if (hm->resp_code != 200) { break; }
106+
105107
state->next_blk = ( state->dest + state->recieved + (uint32) hm->body.len ) / BLOCK_SIZE;
106108
state->left_in_block = ( (state->curr_blk + 1) * BLOCK_SIZE ) - state->dest - state->recieved;
107109

@@ -130,8 +132,23 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data, void *ud) {
130132
c->flags |= MG_F_DELETE_CHUNK;
131133
break;
132134
case MG_EV_HTTP_REPLY:
133-
// set status once file transfer is done
134-
state->status = hm->resp_code;
135+
if (hm->resp_code == 302) {
136+
// follow http redirect ...
137+
for (int i = 0; i < MG_MAX_HTTP_HEADERS; i++) {
138+
if ( mg_strstr(hm->header_names[i], mg_mk_str("location") ) != NULL ) {
139+
LOG(LL_DEBUG, ("302 redirect to %.*s", hm->header_values[i].len, hm->header_values[i].p));
140+
141+
char *url;
142+
asprintf(&url, "%.*s", hm->header_values[i].len, hm->header_values[i].p);
143+
mg_connect_http(mgos_get_mgr(), http_cb, state, url, NULL, NULL);
144+
break;
145+
}
146+
}
147+
} else {
148+
// ...or set status once file transfer is done
149+
state->status = hm->resp_code;
150+
}
151+
135152
c->flags |= MG_F_CLOSE_IMMEDIATELY;
136153
break;
137154
case MG_EV_CLOSE:
@@ -141,21 +158,21 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data, void *ud) {
141158
// write last block
142159
if ( spi_flash_erase_sector(state->curr_blk) != 0 ) {
143160
LOG(LL_ERROR, ("flash delete error! abort at %d recieved bytes.", state->recieved));
144-
state->status = 500;
145161
break;
146162
}
147163
state->left_in_block = ( (state->curr_blk + 1) * BLOCK_SIZE ) - state->dest - state->recieved;
148164
if ( spi_flash_write( state->curr_blk * BLOCK_SIZE, (uint32 *) state->data, BLOCK_SIZE - state->left_in_block) != 0 ) {
149165
LOG(LL_ERROR, ("flash write error! abort at %d recieved bytes.", state->recieved));
150-
state->status = 500;
151166
break;
152167
}
153168
LOG(LL_DEBUG, ("last block dump done"));
154169

155170
block_copy( (*rboot_cfg).roms[TEMP_STORAGE], 0, state->recieved ); //TODO move me
156171
mgos_system_restart_after(200);
172+
} else if (state->status == 0) {
173+
LOG(LL_INFO, ("Following HTTP redirect..."));
157174
} else {
158-
LOG(LL_ERROR, ("HTTP state not 200, abort!"));
175+
LOG(LL_ERROR, ("HTTP response error: %d", state->status));
159176
}
160177
break;
161178
}
@@ -176,21 +193,14 @@ void download_file_to_flash(const char *url, uint32 dest) {
176193
state->curr_blk = dest / BLOCK_SIZE;
177194

178195
LOG(LL_DEBUG, ("fetching %s to 0x%x", url, dest));
179-
if (!mg_connect_http(mgos_get_mgr(), http_cb, state, url, NULL, NULL)) {
180-
free(state);
181-
LOG(LL_ERROR, ("malformed URL"));
182-
return;
183-
}
184-
185-
// TODO wait on download to finish, then continue here. also, return status
186-
// free(state);
196+
mg_connect_http(mgos_get_mgr(), http_cb, state, url, NULL, NULL);
187197
return;
188198
};
189199

190-
// if we are online, lets download and flash tasmota
200+
// if we are online, lets download and flash the target firmware
191201
static void online_cb(int ev, void *evd, void *arg) {
192202
if ( ev == MGOS_NET_EV_IP_ACQUIRED ) {
193-
LOG(LL_INFO, ("device is online, downloading tasmota"));
203+
LOG(LL_INFO, ("device is online, downloading target firmware"));
194204
download_file_to_flash(mgos_sys_config_get_mg2x_url(), (*rboot_cfg).roms[TEMP_STORAGE] );
195205
}
196206
(void) evd;

0 commit comments

Comments
 (0)