My application is offline but I want to take advantage of the Wifi on the photon. I’ll be using the SoftAP to display values and I want to be able to do a firmware upgrade of my product via softap because this way it’s easy for the customer and he can do it with a cellphone after downloading the file from my website.
So the first step to do that is to be able to read a file from the soft ap. Right now I’m using a text file but the issue is the same with the binary file.
The html code look like this:
<p>Select file for firmware upgrade</p>
<form id="uploadbanner" enctype="application/x-www-form-urlencoded" method="post" action="upgrade">
<input id="fileupload" name="myfile" type="file" />
<p><input type="submit" value="Begin upgrade" id="submit" />
</form>
The decoding look like this. Right now I’m simply sending everything to the serial port for debugging.
void myPage(const char* url, ResponseCallback* cb, void* cbArg, Reader* body, Writer* result, void* reserved)
{
String urlString = String(url);
Serial.printlnf("handling page %s", url);
if (strcmp(url,"/index")==0) {
Serial.println("sending redirect");
Header h("Location: /index.html\r\n");
cb(cbArg, 0, 301, "text/plain", &h);
return;
}
if (strcmp(url, "/upgrade") == 0)
{
// Here is the output I should get
/*
POST Data: ------WebKitFormBoundaryEEbsS0VLVz50qOeT
Content-Disposition: form-data; name="myfile"; filename="binfile"
Content-Type: application/octet-stream
content file here...
*/
uint8_t contentData[513];
int dataPos;
int datalength = body->read(contentData, sizeof(contentData)-1);
contentData[513] = '\0';
;
String contentString = String((const char*)contentData);
Serial.print((const char*)&contentData[0]);
Serial.println("");
dataPos = contentString.indexOf("filename=");
Serial.printlnf("Index of filename: %d", dataPos);
if(dataPos != -1)
{
// Get filename here if we want
String filename = contentString.substring(dataPos + 10, contentString.indexOf("Content-Type")-1);
Serial.printlnf("Filename: %s", filename.c_str());
}
// dataPos = contentString.indexOf("octet-stream");
// Serial.printlnf("Index of octet-stream: %d", dataPos);
dataPos = contentString.indexOf("\r\n\r\n");
if(dataPos != -1)
{
dataPos += 4;
Serial.println("Data start here:");
datalength -= dataPos;
while (body->bytes_left)
{
Serial.printf("Write data:%s", &contentData[dataPos]);
dataPos = datalength;
if (dataPos == datalength)
{
do
{
if(datalength == -1)
Serial.println("Datalength -1");
//delay(50);
datalength = body->read(contentData, sizeof(contentData)-1);
contentData[513] = '\0';
//if (datalength == 0)
{
Serial.printlnf("Length = %d, Bytes left: %d", datalength, body->bytes_left);
}
} while (datalength <= 0 && body->bytes_left);
dataPos = 0;
}
}
Serial.println("");
Serial.printlnf("Bytes left: %d", body->bytes_left);
delay(20);
}
}
int8_t idx = 0;
for (;; idx++) {
Page& p = myPages[idx];
if (!p.url) {
idx = -1;
break;
}
else if (strcmp(url, p.url) == 0) {
break;
}
}
if (idx == -1) {
cb(cbArg, 0, 404, nullptr, nullptr);
}
else {
cb(cbArg, 0, 200, myPages[idx].mime_type, nullptr);
result->write(myPages[idx].data);
}
}
Right now I’m able to get all the file but there are part missing… the debug output look like this. Check line number 77
Data start here:
Write data:This is a test file to figure out if all the data is sent.
00
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
7@Length = 512, Bytes left: 11019
Write data:
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
@Length = 512, Bytes left: 10507
Write data:441
442
443
444
445
446
And it continues like this to the end of the file.
Why is the read function not returning all the data. Is it because I’m too slow to process the data? What else can I do?
Thanks!