Select one of the symbols to view example projects that use it.
 
Outline
#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>
DataFlag
size
FlashWriteAddress
TotalReceived
LeftBytesTab
LeftBytes
resetpage
ContentLengthOffset
BrowserFlag
TotalData
checklogin
http_state
htmlpageState
htmlpage
http_crnl_2
octet_stream
Content_Length
conn_err(void *, err_t)
close_conn(struct tcp_pcb *, struct http_state *)
send_data(struct tcp_pcb *, struct http_state *)
http_poll(void *, struct tcp_pcb *)
http_sent(void *, struct tcp_pcb *, u16_t)
http_recv(void *, struct tcp_pcb *, struct pbuf *, err_t)
http_accept(void *, struct tcp_pcb *, err_t)
IAP_httpd_init()
fs_open(char *, struct fs_file *)
Parse_Content_Length(char *, uint32_t)
IAP_HTTP_writedata(char *, uint32_t)
Files
loading...
CodeScopeSTM32 Libraries and SamplesLwIP_IAPSrc/httpserver.c
 
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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* Copyright (c) 2001, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * httpd.c * * Author : Adam Dunkels <adam@sics.se> * *//* ... */ #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] = /* "\r\n--" */ {0xd, 0xa,0x2d,0x2d}; static const char octet_stream[14] = /* "octet-stream" */ {0x6f, 0x63, 0x74, 0x65, 0x74, 0x2d, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d,0x0d, }; static const char Content_Length[17] = /* Content Length */ {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); /* file must be allocated by caller and will be filled in by the function. *//* ... */ static int fs_open(char *name, struct fs_file *file); /** * @brief callback function for handling connection errors * @param arg: pointer to an argument to be passed to callback function * @param err: LwIP error code * @retval None *//* ... */ static void conn_err(void *arg, err_t err) { struct http_state *hs; hs = arg; mem_free(hs); }{ ... } /** * @brief closes tcp connection * @param pcb: pointer to a tcp_pcb struct * @param hs: pointer to a http_state struct * @retval *//* ... */ 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); }{ ... } /** * @brief sends data found in member "file" of a http_state struct * @param pcb: pointer to a tcp_pcb struct * @param hs: pointer to a http_state struct * @retval None *//* ... */ static void send_data(struct tcp_pcb *pcb, struct http_state *hs) { err_t err; u16_t len; /* We cannot send more data than space available in the send buffer *//* ... */ 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) { ... } }{ ... } /** * @brief tcp poll callback function * @param arg: pointer to an argument to be passed to callback function * @param pcb: pointer on tcp_pcb structure * @retval err_t *//* ... */ 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; }{ ... } /** * @brief callback function called after a successful TCP data packet transmission * @param arg: pointer to an argument to be passed to callback function * @param pcb: pointer on tcp_pcb structure * @param len * @retval err : LwIP error code *//* ... */ 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) { /* Generate a software reset */ NVIC_SystemReset(); }if (resetpage ==1) { ... } }else { ... } return ERR_OK; }{ ... } /** * @brief callback function for handling TCP HTTP traffic * @param arg: pointer to an argument structure to be passed to callback function * @param pcb: pointer to a tcp_pcb structure * @param p: pointer to a packet buffer * @param err: LwIP error code * @retval err *//* ... */ 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) { /* Inform TCP that we have taken the data */ tcp_recved(pcb, p->tot_len); if (hs->file == NULL) { data = p->payload; len = p->tot_len; /* process HTTP GET Login page requests */ if (strncmp(data, "GET / HTTP", 10) == 0) { /*send the login page (which is the index page) */ htmlpage = LoginPage; fs_open("/index.html", &file); hs->file = file.data; hs->left = file.len; pbuf_free(p); /* send index.html page */ send_data(pcb, hs); /* Tell TCP that we wish be to informed of data that has been successfully sent by a call to the http_sent() function. *//* ... */ tcp_sent(pcb, http_sent); }if (strncmp(data, "GET / HTTP", 10) == 0) { ... } /* process HTTP GET reset mcu requests */ 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 reset.html page */ send_data(pcb, hs); resetpage = 1; /* Tell TCP that we wish be to informed of data that has been successfully sent by a call to the http_sent() function. *//* ... */ tcp_sent(pcb, http_sent); }else if ((strncmp(data, "GET /resetmcu.cgi", 17) ==0)&&(htmlpage == UploadDonePage)) { ... } /* process POST request for checking login */ else if ((strncmp(data, "POST /checklogin.cgi",20)==0)&&(htmlpage== LoginPage)) { /* parse packet for the username & password */ 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; /* reload index.html */ fs_open("/index.html", &file); }else { ... } hs->file = file.data; hs->left = file.len; pbuf_free(p); /* send index.html page */ send_data(pcb, hs); /* Tell TCP that we wish be to informed of data that has been successfully sent by a call to the http_sent() function. *//* ... */ 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)) { ... } /* process POST request for file upload and incoming data packets after POST request*/ else if (((strncmp(data, "POST /upload.cgi",16)==0)||(DataFlag >=1))&&(htmlpage == FileUploadPage)) { DataOffset =0; /* POST Packet received */ if (DataFlag ==0) { BrowserFlag=0; TotalReceived =0; /* parse packet for Content-length field */ size = Parse_Content_Length(data, (uint32_t)(p->tot_len)); /* parse packet for the octet-stream field */ 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 /* case of MSIE8 : we do not receive data in the POST packet */ if (DataOffset==0) { DataFlag++; BrowserFlag = 1; pbuf_free(p); return ERR_OK; }if (DataOffset==0) { ... } /* case of Mozilla Firefox : we receive data in the POST packet*/ 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)) { /* parse packet for the octet-stream field */ 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)) { ... } /* parse packet for the filename field */ 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; /* no filename, in this case reload upload page */ fs_open("/upload.html", &file); hs->file = file.data; hs->left = file.len; pbuf_free(p); /* send index.html page */ send_data(pcb, hs); /* Tell TCP that we wish be to informed of data that has been successfully sent by a call to the http_sent() function. *//* ... */ 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 /* USE_LCD */ TotalData =0; /* init flash */ FLASH_If_Init(); /* erase user flash area */ FLASH_If_Erase(USER_FLASH_FIRST_PAGE_ADDRESS); FlashWriteAddress = USER_FLASH_FIRST_PAGE_ADDRESS; #ifdef USE_LCD /*indicate start of flash programming */ LCD_UsrLog(" State: Programming...\n");/* ... */ #endif }if (((DataFlag ==1)&&(BrowserFlag==1)) || ((DataFlag ==0)&&(BrowserFlag==0))) { ... } /* DataFlag >1 => the packet is data only */ else { TotalReceived +=len; }else { ... } ptr = (char*)(data + DataOffset); len-= DataOffset; /* update Total data received counter */ TotalData +=len; /* check if last data packet */ if (TotalReceived == size) { /* if last packet need to remove the http boundary tag */ /* parse packet for "\r\n--" starting from end of data */ 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; /* write data in Flash */ 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; /* send uploaddone.html page */ fs_open("/uploaddone.html", &file); hs->file = file.data; hs->left = file.len; send_data(pcb, hs); /* Tell TCP that we wish be to informed of data that has been successfully sent by a call to the http_sent() function. *//* ... */ tcp_sent(pcb, http_sent); }if (TotalReceived == size) { ... } /* not last data packet */ else { /* write data in flash */ len = ptmp->len; len-= DataOffset; /* write data in flash */ 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 { /* Bad HTTP requests */ #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) { /* received empty frame */ close_conn(pcb, hs); }if (err == ERR_OK && p == NULL) { ... } return ERR_OK; }{ ... } /** * @brief callback function on TCP connection setup ( on port 80) * @param arg: pointer to an argument structure to be passed to callback function * @param pcb: pointer to a tcp_pcb structure * &param err: Lwip stack error code * @retval err *//* ... */ static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err) { struct http_state *hs; /* Allocate memory for the structure that holds the state of the connection */ hs = mem_malloc(sizeof(struct http_state)); if (hs == NULL) { return ERR_MEM; }if (hs == NULL) { ... } /* Initialize the structure. */ hs->file = NULL; hs->left = 0; /* Tell TCP that this is the structure we wish to be passed for our callbacks. *//* ... */ tcp_arg(pcb, hs); /* Tell TCP that we wish to be informed of incoming data by a call to the http_recv() function. *//* ... */ tcp_recv(pcb, http_recv); tcp_err(pcb, conn_err); tcp_poll(pcb, http_poll, 10); return ERR_OK; }{ ... } /** * @brief initialize HTTP webserver * @param none * @retval None *//* ... */ void IAP_httpd_init(void) { struct tcp_pcb *pcb; /*create new pcb*/ pcb = tcp_new(); /* bind HTTP traffic to pcb */ tcp_bind(pcb, IP_ADDR_ANY, 80); /* start listening on port 80 */ pcb = tcp_listen(pcb); /* define callback function for TCP connection setup */ tcp_accept(pcb, http_accept); }{ ... } /** * @brief Opens a file defined in fsdata.c ROM filesystem * @param name : pointer to a file name * @param file : pointer to a fs_file structure * @retval 1 if success, 0 if fail *//* ... */ 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; }{ ... } /** * @brief Extract the Content_Length data from HTML data * @param data : pointer on receive packet buffer * @param len : buffer length * @retval size : Content_length in numeric format *//* ... */ 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; /* find Content-Length data in packet buffer */ 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 /* read Content-Length value */ 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) { /* transform string data into numeric format */ 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; }{ ... } /** * @brief writes received data in flash * @param ptr: data pointer * @param len: data length * @retval None *//* ... */ static void IAP_HTTP_writedata(char * ptr, uint32_t len) { uint32_t count, i=0, j=0; /* check if any left bytes from previous packet transfer*/ /* if it is the case do a concat with new data to create a 32-bit word */ 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; /* update data pointer */ ptr = (char*)(ptr+j); len = len -j; }if (LeftBytes) { ... } /* write received bytes into flash */ count = len/4; /* check if remaining bytes < 4 */ i= len%4; if (i>0) { if (TotalReceived != size) { /* store bytes in LeftBytesTab */ 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/* ... */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/