1
2
3
4
5
6
7
8
9
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
44
45
46
47
48
49
50
51
52
53
54
55
56
60
61
62
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
87
88
89
95
102
103
109
110
111
112
113
114
115
116
117
118
124
125
126
127
128
129
131
135
139
140
145
146
147
153
165
166
173
174
175
176
177
178
179
183
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
262
263
264
265
266
267
268
269
270
271
272
273
274
280
286
287
288
289
290
291
292
293
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
324
325
326
327
328
329
330
331
332
333
334
338
339
340
341
342
346
360
361
362
363
371
372
381
382
383
384
385
386
387
388
389
390
391
392
393
394
396
397
398
399
400
401
402
403
407
408
409
410
411
412
413
414
415
416
417
418
420
421
422
423
427
428
429
430
431
432
433
434
435
436
437
438
439
440
444
445
446
447
448
449
450
451
452
453
454
457
458
459
460
461
462
463
464
465
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
502
503
504
509
510
511
512
513
520
521
522
523
524
525
526
527
531
532
533
534
535
536
538
539
540
542
543
544
545
546
547
548
549
550
555
556
557
558
559
560
561
562
563
564
565
566
567
568
574
589
590
596
597
598
599
600
601
602
603
604
605
613
614
634
635
636
637
643
644
645
646
647
648
649
650
651
663
664
665
666
667
668
669
670
671
672
673
674
675
676
687
688
689
690
691
/* ... */
#include "httpserver.h"
#include "lwip/tcp.h"
#include "fsdata.c"
#include "main.h"
#include "flash_if.h"
#include "lcd_log.h"
#include <string.h>
#include <stdio.h>
8 includes
#ifdef USE_IAP_HTTP
static __IO uint32_t DataFlag=0;
static __IO uint32_t size =0;
static __IO uint32_t FlashWriteAddress;
static uint32_t TotalReceived=0;
static char LeftBytesTab[4];
static uint8_t LeftBytes=0;
static __IO uint8_t resetpage=0;
static uint32_t ContentLengthOffset =0,BrowserFlag=0;
static __IO uint32_t TotalData=0, checklogin=0;
struct http_state
{
char *file;
u32_t left;
...};
typedef enum
{
LoginPage = 0,
FileUploadPage,
UploadDonePage,
ResetDonePage
...}htmlpageState;
htmlpageState htmlpage;
static const char http_crnl_2[4] =
{0xd, 0xa,0x2d,0x2d};
static const char octet_stream[14] =
{0x6f, 0x63, 0x74, 0x65, 0x74, 0x2d, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d,0x0d, };
static const char Content_Length[17] =
{0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x4c, 0x65, 0x6e, 0x67,0x74, 0x68, 0x3a, 0x20, };
static uint32_t Parse_Content_Length(char *data, uint32_t len);
static void IAP_HTTP_writedata(char* data, uint32_t len);
/* ... */
static int fs_open(char *name, struct fs_file *file);
/* ... */
static void conn_err(void *arg, err_t err)
{
struct http_state *hs;
hs = arg;
mem_free(hs);
}{ ... }
/* ... */
static void close_conn(struct tcp_pcb *pcb, struct http_state *hs)
{
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
mem_free(hs);
tcp_close(pcb);
}{ ... }
/* ... */
static void send_data(struct tcp_pcb *pcb, struct http_state *hs)
{
err_t err;
u16_t len;
/* ... */
if (tcp_sndbuf(pcb) < hs->left)
{
len = tcp_sndbuf(pcb);
}if (tcp_sndbuf(pcb) < hs->left) { ... }
else
{
len = hs->left;
}else { ... }
err = tcp_write(pcb, hs->file, len, 0);
if (err == ERR_OK)
{
hs->file += len;
hs->left -= len;
}if (err == ERR_OK) { ... }
}{ ... }
/* ... */
static err_t http_poll(void *arg, struct tcp_pcb *pcb)
{
if (arg == NULL)
{
tcp_close(pcb);
}if (arg == NULL) { ... }
else
{
send_data(pcb, (struct http_state *)arg);
}else { ... }
return ERR_OK;
}{ ... }
/* ... */
static err_t http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
{
struct http_state *hs;
hs = arg;
if (hs->left > 0)
{
send_data(pcb, hs);
}if (hs->left > 0) { ... }
else
{
close_conn(pcb, hs);
if(resetpage ==1)
{
NVIC_SystemReset();
}if (resetpage ==1) { ... }
}else { ... }
return ERR_OK;
}{ ... }
/* ... */
static err_t http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
int32_t i,len=0;
uint32_t DataOffset, FilenameOffset;
char *data, *ptr, filename[40], login[LOGIN_SIZE+1];
struct fs_file file = {0, 0};
struct http_state *hs;
struct pbuf *ptmp = p;
#ifdef USE_LCD
char message[46];
#endif
hs = arg;
if (err == ERR_OK && p != NULL)
{
tcp_recved(pcb, p->tot_len);
if (hs->file == NULL)
{
data = p->payload;
len = p->tot_len;
if (strncmp(data, "GET / HTTP", 10) == 0)
{
htmlpage = LoginPage;
fs_open("/index.html", &file);
hs->file = file.data;
hs->left = file.len;
pbuf_free(p);
send_data(pcb, hs);
/* ... */
tcp_sent(pcb, http_sent);
}if (strncmp(data, "GET / HTTP", 10) == 0) { ... }
else if ((strncmp(data, "GET /resetmcu.cgi", 17) ==0)&&(htmlpage == UploadDonePage))
{
htmlpage = ResetDonePage;
fs_open("/reset.html", &file);
hs->file = file.data;
hs->left = file.len;
pbuf_free(p);
send_data(pcb, hs);
resetpage = 1;
/* ... */
tcp_sent(pcb, http_sent);
}else if ((strncmp(data, "GET /resetmcu.cgi", 17) ==0)&&(htmlpage == UploadDonePage)) { ... }
else if ((strncmp(data, "POST /checklogin.cgi",20)==0)&&(htmlpage== LoginPage))
{
for (i=0;i<len;i++)
{
if (strncmp ((char*)(data+i), "username=", 9)==0)
{
sprintf((char *)login,"username=%s&password=%s",USERID,PASSWORD);
if (strncmp((char*)(data+i), (char *)login ,LOGIN_SIZE)==0)
{
htmlpage = FileUploadPage;
fs_open("/upload.html", &file);
}if (strncmp((char*)(data+i), (char *)login ,LOGIN_SIZE)==0) { ... }
else
{
htmlpage = LoginPage;
fs_open("/index.html", &file);
}else { ... }
hs->file = file.data;
hs->left = file.len;
pbuf_free(p);
send_data(pcb, hs);
/* ... */
tcp_sent(pcb, http_sent);
break;
}if (strncmp ((char*)(data+i), "username=", 9)==0) { ... }
}for (i=0;i
}else if ((strncmp(data, "POST /checklogin.cgi",20)==0)&&(htmlpage== LoginPage)) { ... }
else if (((strncmp(data, "POST /upload.cgi",16)==0)||(DataFlag >=1))&&(htmlpage == FileUploadPage))
{
DataOffset =0;
if (DataFlag ==0)
{
BrowserFlag=0;
TotalReceived =0;
size = Parse_Content_Length(data, (uint32_t)(p->tot_len));
for (i=0;i<len;i++)
{
if (strncmp ((char*)(data+i), octet_stream, 13)==0)
{
DataOffset = i+16;
break;
}if (strncmp ((char*)(data+i), octet_stream, 13)==0) { ... }
}for (i=0;i
if (DataOffset==0)
{
DataFlag++;
BrowserFlag = 1;
pbuf_free(p);
return ERR_OK;
}if (DataOffset==0) { ... }
else
{
TotalReceived = len - (ContentLengthOffset + 4);
}else { ... }
}if (DataFlag ==0) { ... }
if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0)))
{
if ((DataFlag ==0)&&(BrowserFlag==0))
{
DataFlag++;
}if ((DataFlag ==0)&&(BrowserFlag==0)) { ... }
else if ((DataFlag ==1)&&(BrowserFlag==1))
{
for (i=0;i<len;i++)
{
if (strncmp ((char*)(data+i), octet_stream, 13)==0)
{
DataOffset = i+16;
break;
}if (strncmp ((char*)(data+i), octet_stream, 13)==0) { ... }
}for (i=0;i
TotalReceived+=len;
DataFlag++;
}else if ((DataFlag ==1)&&(BrowserFlag==1)) { ... }
FilenameOffset = 0;
for (i=0;i<len;i++)
{
if (strncmp ((char*)(data+i), "filename=", 9)==0)
{
FilenameOffset = i+10;
break;
}if (strncmp ((char*)(data+i), "filename=", 9)==0) { ... }
}for (i=0;i
i =0;
if (FilenameOffset)
{
while((*(data+FilenameOffset + i)!=0x22 )&&(i < 40))
{
filename[i] = *(data+FilenameOffset + i);
i++;
}while ((*(data+FilenameOffset + i)!=0x22 )&&(i < 40)) { ... }
filename[i] = 0x0;
}if (FilenameOffset) { ... }
if (i==0)
{
htmlpage = FileUploadPage;
fs_open("/upload.html", &file);
hs->file = file.data;
hs->left = file.len;
pbuf_free(p);
send_data(pcb, hs);
/* ... */
tcp_sent(pcb, http_sent);
DataFlag=0;
return ERR_OK;
}if (i==0) { ... }
#ifdef USE_LCD
LCD_UsrLog("IAP using HTTP\n");
sprintf(message, "File: %s",filename);
LCD_UsrLog("%s\n",message);
LCD_UsrLog(" State: Erasing...\n");/* ... */
#endif
TotalData =0;
FLASH_If_Init();
FLASH_If_Erase(USER_FLASH_FIRST_PAGE_ADDRESS);
FlashWriteAddress = USER_FLASH_FIRST_PAGE_ADDRESS;
#ifdef USE_LCD
LCD_UsrLog(" State: Programming...\n");/* ... */
#endif
}if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0))) { ... }
else
{
TotalReceived +=len;
}else { ... }
ptr = (char*)(data + DataOffset);
len-= DataOffset;
TotalData +=len;
if (TotalReceived == size)
{
i=4;
while (strncmp ((char*)(data+ p->tot_len -i),http_crnl_2 , 4) && (p->tot_len -i > 0))
{
i++;
}while (strncmp ((char*)(data+ p->tot_len -i),http_crnl_2 , 4) && (p->tot_len -i > 0)) { ... }
len-=i;
TotalData-=i;
if (len)
IAP_HTTP_writedata(ptr,len);
DataFlag=0;
#ifdef USE_LCD
sprintf(message, "%d bytes ",(int)TotalData);
LCD_UsrLog("Tot bytes Received: %s\n", message);
LCD_UsrLog(" State: Prog Finished ");/* ... */
#endif
htmlpage = UploadDonePage;
fs_open("/uploaddone.html", &file);
hs->file = file.data;
hs->left = file.len;
send_data(pcb, hs);
/* ... */
tcp_sent(pcb, http_sent);
}if (TotalReceived == size) { ... }
else
{
len = ptmp->len;
len-= DataOffset;
while(ptmp != NULL)
{
if(len)
IAP_HTTP_writedata(ptr,len);
ptmp = ptmp->next;
len = ptmp->len;
ptr = ptmp->payload;
}while (ptmp != NULL) { ... }
}else { ... }
pbuf_free(p);
}else if (((strncmp(data, "POST /upload.cgi",16)==0)||(DataFlag >=1))&&(htmlpage == FileUploadPage)) { ... }
else
{
#ifdef USE_LCD
LCD_ErrLog("Bad HTTP request\n");
#endif
close_conn(pcb, hs);
}else { ... }
}if (hs->file == NULL) { ... }
else
{
pbuf_free(p);
close_conn(pcb,hs);
}else { ... }
}if (err == ERR_OK && p != NULL) { ... }
if (err == ERR_OK && p == NULL)
{
close_conn(pcb, hs);
}if (err == ERR_OK && p == NULL) { ... }
return ERR_OK;
}{ ... }
/* ... */
static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
struct http_state *hs;
hs = mem_malloc(sizeof(struct http_state));
if (hs == NULL)
{
return ERR_MEM;
}if (hs == NULL) { ... }
hs->file = NULL;
hs->left = 0;
/* ... */
tcp_arg(pcb, hs);
/* ... */
tcp_recv(pcb, http_recv);
tcp_err(pcb, conn_err);
tcp_poll(pcb, http_poll, 10);
return ERR_OK;
}{ ... }
/* ... */
void IAP_httpd_init(void)
{
struct tcp_pcb *pcb;
pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, 80);
pcb = tcp_listen(pcb);
tcp_accept(pcb, http_accept);
}{ ... }
/* ... */
static int fs_open(char *name, struct fs_file *file)
{
struct fsdata_file_noconst *f;
for (f = (struct fsdata_file_noconst *)FS_ROOT; f != NULL; f = (struct fsdata_file_noconst *)f->next)
{
if (!strcmp(name, f->name))
{
file->data = f->data;
file->len = f->len;
return 1;
}if (!strcmp(name, f->name)) { ... }
}for (f = (struct fsdata_file_noconst *)FS_ROOT; f != NULL; f = (struct fsdata_file_noconst *)f->next) { ... }
return 0;
}{ ... }
/* ... */
static uint32_t Parse_Content_Length(char *data, uint32_t len)
{
uint32_t i=0,size=0, S=1;
int32_t j=0;
char sizestring[6], *ptr;
ContentLengthOffset =0;
for (i=0;i<len;i++)
{
if (strncmp ((char*)(data+i), Content_Length, 16)==0)
{
ContentLengthOffset = i+16;
break;
}if (strncmp ((char*)(data+i), Content_Length, 16)==0) { ... }
}for (i=0;i
if (ContentLengthOffset)
{
i=0;
ptr = (char*)(data + ContentLengthOffset);
while(*(ptr+i)!=0x0d)
{
sizestring[i] = *(ptr+i);
i++;
ContentLengthOffset++;
}while (*(ptr+i)!=0x0d) { ... }
if (i>0)
{
for(j=i-1;j>=0;j--)
{
size += (sizestring[j]-'0')*S;
S=S*10;
}for (j=i-1;j>=0;j--) { ... }
}if (i>0) { ... }
}if (ContentLengthOffset) { ... }
return size;
}{ ... }
/* ... */
static void IAP_HTTP_writedata(char * ptr, uint32_t len)
{
uint32_t count, i=0, j=0;
if (LeftBytes)
{
while(LeftBytes<=3)
{
if(len>(j+1))
{
LeftBytesTab[LeftBytes++] = *(ptr+j);
}if (len>(j+1)) { ... }
else
{
LeftBytesTab[LeftBytes++] = 0xFF;
}else { ... }
j++;
}while (LeftBytes<=3) { ... }
FLASH_If_Write(&FlashWriteAddress, (uint32_t*)(LeftBytesTab),1);
LeftBytes =0;
ptr = (char*)(ptr+j);
len = len -j;
}if (LeftBytes) { ... }
count = len/4;
i= len%4;
if (i>0)
{
if (TotalReceived != size)
{
LeftBytes=0;
for(;i>0;i--)
LeftBytesTab[LeftBytes++] = *(char*)(ptr+ len-i);
}if (TotalReceived != size) { ... }
else count++;
}if (i>0) { ... }
FLASH_If_Write(&FlashWriteAddress, (uint32_t*)ptr ,count);
}{ ... }
#endif/* ... */