@@ -96,12 +96,14 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data, void *ud) {
96
96
switch (ev ) {
97
97
case MG_EV_CONNECT :
98
98
// sent when a new outbound connection is created
99
- state -> status = * ( int * ) ev_data ;
99
+ state -> status = 0 ;
100
100
break ;
101
101
case MG_EV_HTTP_CHUNK :
102
102
// esp8266 expects flash to be erased and written in blocks of 4kb, but webservers send
103
103
// chunks of data in whatever size they please. we thus buffer the data and write it
104
104
// whenever a block is full
105
+ if (hm -> resp_code != 200 ) { break ; }
106
+
105
107
state -> next_blk = ( state -> dest + state -> recieved + (uint32 ) hm -> body .len ) / BLOCK_SIZE ;
106
108
state -> left_in_block = ( (state -> curr_blk + 1 ) * BLOCK_SIZE ) - state -> dest - state -> recieved ;
107
109
@@ -130,8 +132,23 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data, void *ud) {
130
132
c -> flags |= MG_F_DELETE_CHUNK ;
131
133
break ;
132
134
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
+
135
152
c -> flags |= MG_F_CLOSE_IMMEDIATELY ;
136
153
break ;
137
154
case MG_EV_CLOSE :
@@ -141,21 +158,21 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data, void *ud) {
141
158
// write last block
142
159
if ( spi_flash_erase_sector (state -> curr_blk ) != 0 ) {
143
160
LOG (LL_ERROR , ("flash delete error! abort at %d recieved bytes." , state -> recieved ));
144
- state -> status = 500 ;
145
161
break ;
146
162
}
147
163
state -> left_in_block = ( (state -> curr_blk + 1 ) * BLOCK_SIZE ) - state -> dest - state -> recieved ;
148
164
if ( spi_flash_write ( state -> curr_blk * BLOCK_SIZE , (uint32 * ) state -> data , BLOCK_SIZE - state -> left_in_block ) != 0 ) {
149
165
LOG (LL_ERROR , ("flash write error! abort at %d recieved bytes." , state -> recieved ));
150
- state -> status = 500 ;
151
166
break ;
152
167
}
153
168
LOG (LL_DEBUG , ("last block dump done" ));
154
169
155
170
block_copy ( (* rboot_cfg ).roms [TEMP_STORAGE ], 0 , state -> recieved ); //TODO move me
156
171
mgos_system_restart_after (200 );
172
+ } else if (state -> status == 0 ) {
173
+ LOG (LL_INFO , ("Following HTTP redirect..." ));
157
174
} else {
158
- LOG (LL_ERROR , ("HTTP state not 200, abort!" ));
175
+ LOG (LL_ERROR , ("HTTP response error: %d" , state -> status ));
159
176
}
160
177
break ;
161
178
}
@@ -176,21 +193,14 @@ void download_file_to_flash(const char *url, uint32 dest) {
176
193
state -> curr_blk = dest / BLOCK_SIZE ;
177
194
178
195
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 );
187
197
return ;
188
198
};
189
199
190
- // if we are online, lets download and flash tasmota
200
+ // if we are online, lets download and flash the target firmware
191
201
static void online_cb (int ev , void * evd , void * arg ) {
192
202
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 " ));
194
204
download_file_to_flash (mgos_sys_config_get_mg2x_url (), (* rboot_cfg ).roms [TEMP_STORAGE ] );
195
205
}
196
206
(void ) evd ;
0 commit comments