-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCustomHTTPApp.c
More file actions
1050 lines (883 loc) · 29.2 KB
/
Copy pathCustomHTTPApp.c
File metadata and controls
1050 lines (883 loc) · 29.2 KB
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
*
* Application to Demo HTTP2 Server
* Support for HTTP2 module in Microchip TCP/IP Stack
* -Implements the application
* -Reference: RFC 1002
*
*********************************************************************
* FileName: CustomHTTPApp.c
* Dependencies: TCP/IP stack
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
* Compiler: Microchip C32 v1.05 or higher
* Microchip C30 v3.12 or higher
* Microchip C18 v3.30 or higher
* HI-TECH PICC-18 PRO 9.63PL2 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright (C) 2002-2010 Microchip Technology Inc. All rights
* reserved.
*
* Microchip licenses to you the right to use, modify, copy, and
* distribute:
* (i) the Software when embedded on a Microchip microcontroller or
* digital signal controller product ("Device") which is
* integrated into Licensee's product; or
* (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
* ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
* used in conjunction with a Microchip ethernet controller for
* the sole purpose of interfacing with the ethernet controller.
*
* You should refer to the license agreement accompanying this
* Software for additional information regarding your rights and
* obligations.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Elliott Wood 6/18/07 Original
********************************************************************/
#define __CUSTOMHTTPAPP_C
#include "TCPIPConfig.h"
#include "BTnic_Comm.h"
#include "eeprom.h"
#include "sram.h"
#if defined(STACK_USE_HTTP2_SERVER)
#include "TCPIP Stack/TCPIP.h"
#include "Main.h" // Needed for SaveAppConfig() prototype
static HTTP_IO_RESULT HTTPPostConfig(void);
static HTTP_IO_RESULT HTTPPostWebConf(void);
static HTTP_IO_RESULT HTTPPostBTCom(void);
// RAM allocated for DDNS parameters
#if defined(STACK_USE_DYNAMICDNS_CLIENT)
static BYTE DDNSData[100];
#endif
// Sticky status message variable.
// This is used to indicated whether or not the previous POST operation was
// successful. The application uses these to store status messages when a
// POST operation redirects. This lets the application provide status messages
// after a redirect, when connection instance data has already been lost.
static BOOL lastSuccess = FALSE;
// Stick status message variable. See lastSuccess for details.
static BOOL lastFailure = FALSE;
/****************************************************************************
Section:
Authorization Handlers
***************************************************************************/
/*****************************************************************************
Function:
BYTE HTTPNeedsAuth(BYTE* cFile)
Internal:
See documentation in the TCP/IP Stack API or HTTP2.h for details.
***************************************************************************/
#if defined(HTTP_USE_AUTHENTICATION)
BYTE HTTPNeedsAuth(BYTE* cFile)
{
// If the filename begins with the folder "protect", then require auth
if(memcmppgm2ram(cFile, (ROM void*)"protect", 7) == 0)
return 0x00; // Authentication will be needed later
if(WebSrvConfig.Flags.DataRequireAuth && (!memcmppgm2ram(cFile, "btnic.cgi", 9)))
return 0x00; // Authentication will be needed later
#if defined(HTTP_MPFS_UPLOAD_REQUIRES_AUTH)
if(memcmppgm2ram(cFile, (ROM void*)"mpfsupload", 10) == 0)
return 0x00;
#endif
// You can match additional strings here to password protect other files.
// You could switch this and exclude files from authentication.
// You could also always return 0x00 to require auth for all files.
// You can return different values (0x00 to 0x79) to track "realms" for below.
return 0x80; // No authentication required
}
#endif
/*****************************************************************************
Function:
BYTE HTTPCheckAuth(BYTE* cUser, BYTE* cPass)
Internal:
See documentation in the TCP/IP Stack API or HTTP2.h for details.
***************************************************************************/
#if defined(HTTP_USE_AUTHENTICATION)
BYTE HTTPCheckAuth(BYTE* cUser, BYTE* cPass)
{
if(strcmp((char *)cUser,WebSrvConfig.AuthUser) == 0
&& strcmp((char *)cPass, WebSrvConfig.AuthPwd) == 0)
return 0x80; // We accept this combination
// You can add additional user/pass combos here.
// If you return specific "realm" values above, you can base this
// decision on what specific file or folder is being accessed.
// You could return different values (0x80 to 0xff) to indicate
// various users or groups, and base future processing decisions
// in HTTPExecuteGet/Post or HTTPPrint callbacks on this value.
return 0x00; // Provided user/pass is invalid
}
#endif
/****************************************************************************
Section:
GET Form Handlers
***************************************************************************/
/*****************************************************************************
Function:
HTTP_IO_RESULT HTTPExecuteGet(void)
Internal:
See documentation in the TCP/IP Stack API or HTTP2.h for details.
***************************************************************************/
HTTP_IO_RESULT HTTPExecuteGet(void)
{
BYTE *ptr;
BYTE filename[21]; //Single folder (8 char) deep, + 1 x "/" + 8 x filename + 1 x "." + 3 x ext
int retValue;
// Load the file name
// Make sure BYTE filename[] above is large enough for your longest name
MPFSGetFilename(curHTTP.file, filename, 21);
if(!memcmppgm2ram(filename, "btnic.cgi", 9))
{
switch(curHTTP.smPost)
{
default:
curHTTP.smPost = SM_BTNIC_START;
case SM_BTNIC_START:
if (BTCommGetState() != COMMSTATE_IDLE) return HTTP_IO_WAITING;
if (BTCommRequest(curHTTP.data)) return HTTP_IO_WAITING;
curHTTP.smPost = SM_BTNIC_WAIT_FOR_RESP;
case SM_BTNIC_WAIT_FOR_RESP:
switch (BTCommGetState())
{
case COMMSTATE_MSG:
return HTTP_IO_DONE;
case COMMSTATE_IDLE:
//BTCommSetRsp("WAIT TIMEOUT");
return HTTP_IO_DONE;
default:
return HTTP_IO_WAITING;
}
}
}
return HTTP_IO_DONE;
}
/****************************************************************************
Section:
POST Form Handlers
***************************************************************************/
#if defined(HTTP_USE_POST)
/*****************************************************************************
Function:
HTTP_IO_RESULT HTTPExecutePost(void)
Internal:
See documentation in the TCP/IP Stack API or HTTP2.h for details.
***************************************************************************/
HTTP_IO_RESULT HTTPExecutePost(void)
{
// Resolve which function to use and pass along
BYTE filename[20];
// Load the file name
// Make sure BYTE filename[] above is large enough for your longest name
MPFSGetFilename(curHTTP.file, filename, sizeof(filename));
#if defined(STACK_USE_HTTP_APP_RECONFIG)
if(!memcmppgm2ram(filename, "protect/config.htm", 18))
return HTTPPostConfig();
if(!memcmppgm2ram(filename, "protect/webconf.htm", 18))
return HTTPPostWebConf();
#endif
if(!memcmppgm2ram(filename, "protect/btcom.htm", 17))
return HTTPPostBTCom();
return HTTP_IO_DONE;
}
/*****************************************************************************
Function:
static HTTP_IO_RESULT HTTPPostConfig(void)
Summary:
Processes the configuration form on config/index.htm
Description:
Accepts configuration parameters from the form, saves them to a
temporary location in RAM, then eventually saves the data to EEPROM or
external Flash.
When complete, this function redirects to config/reboot.htm, which will
display information on reconnecting to the board.
This function creates a shadow copy of the AppConfig structure in
RAM and then overwrites incoming data there as it arrives. For each
name/value pair, the name is first read to curHTTP.data[0:5]. Next, the
value is read to newAppConfig. Once all data has been read, the new
AppConfig is saved back to EEPROM and the browser is redirected to
reboot.htm. That file includes an AJAX call to reboot.cgi, which
performs the actual reboot of the machine.
If an IP address cannot be parsed, too much data is POSTed, or any other
parsing error occurs, the browser reloads config.htm and displays an error
message at the top.
Precondition:
None
Parameters:
None
Return Values:
HTTP_IO_DONE - all parameters have been processed
HTTP_IO_NEED_DATA - data needed by this function has not yet arrived
***************************************************************************/
#if defined(STACK_USE_HTTP_APP_RECONFIG)
static HTTP_IO_RESULT HTTPPostConfig(void)
{
APP_CONFIG newAppConfig;
BYTE *ptr;
BYTE i;
// Check to see if the browser is attempting to submit more data than we
// can parse at once. This function needs to receive all updated
// parameters and validate them all before committing them to memory so that
// orphaned configuration parameters do not get written (for example, if a
// static IP address is given, but the subnet mask fails parsing, we
// should not use the static IP address). Everything needs to be processed
// in a single transaction. If this is impossible, fail and notify the user.
// As a web devloper, if you add parameters to AppConfig and run into this
// problem, you could fix this by to splitting your update web page into two
// seperate web pages (causing two transactional writes). Alternatively,
// you could fix it by storing a static shadow copy of AppConfig someplace
// in memory and using it instead of newAppConfig. Lastly, you could
// increase the TCP RX FIFO size for the HTTP server. This will allow more
// data to be POSTed by the web browser before hitting this limit.
if(curHTTP.byteCount > TCPIsGetReady(sktHTTP) + TCPGetRxFIFOFree(sktHTTP))
goto ConfigFailure;
// Ensure that all data is waiting to be parsed. If not, keep waiting for
// all of it to arrive.
if(TCPIsGetReady(sktHTTP) < curHTTP.byteCount)
return HTTP_IO_NEED_DATA;
eepromReadBytes((BYTE*)&newAppConfig, EEPROM_MAP_APPCONFIG, sizeof(newAppConfig));
// Start out assuming that DHCP is disabled. This is necessary since the
// browser doesn't submit this field if it is unchecked (meaning zero).
// However, if it is checked, this will be overridden since it will be
// submitted.
newAppConfig.Flags.bIsDHCPEnabled = 0;
// Read all browser POST data
while(curHTTP.byteCount)
{
// Read a form field name
if(HTTPReadPostName(curHTTP.data, 6) != HTTP_READ_OK)
goto ConfigFailure;
// Read a form field value
if(HTTPReadPostValue(curHTTP.data + 6, sizeof(curHTTP.data)-6-2) != HTTP_READ_OK)
goto ConfigFailure;
// Parse the value that was read
if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"ip"))
{// Read new static IP Address
if(!StringToIPAddress(curHTTP.data+6, &newAppConfig.MyIPAddr))
goto ConfigFailure;
newAppConfig.DefaultIPAddr.Val = newAppConfig.MyIPAddr.Val;
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"gw"))
{// Read new gateway address
if(!StringToIPAddress(curHTTP.data+6, &newAppConfig.MyGateway))
goto ConfigFailure;
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"sub"))
{// Read new static subnet
if(!StringToIPAddress(curHTTP.data+6, &newAppConfig.MyMask))
goto ConfigFailure;
newAppConfig.DefaultMask.Val = newAppConfig.MyMask.Val;
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"dns1"))
{// Read new primary DNS server
if(!StringToIPAddress(curHTTP.data+6, &newAppConfig.PrimaryDNSServer))
goto ConfigFailure;
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"dns2"))
{// Read new secondary DNS server
if(!StringToIPAddress(curHTTP.data+6, &newAppConfig.SecondaryDNSServer))
goto ConfigFailure;
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"mac"))
{
// Read new MAC address
WORD w;
BYTE i;
ptr = curHTTP.data+6;
for(i = 0; i < 12u; i++)
{// Read the MAC address
// Skip non-hex bytes
while( *ptr != 0x00u && !(*ptr >= '0' && *ptr <= '9') && !(*ptr >= 'A' && *ptr <= 'F') && !(*ptr >= 'a' && *ptr <= 'f') )
ptr++;
// MAC string is over, so zeroize the rest
if(*ptr == 0x00u)
{
for(; i < 12u; i++)
curHTTP.data[i] = '0';
break;
}
// Save the MAC byte
curHTTP.data[i] = *ptr++;
}
// Read MAC Address, one byte at a time
for(i = 0; i < 6u; i++)
{
((BYTE*)&w)[1] = curHTTP.data[i*2];
((BYTE*)&w)[0] = curHTTP.data[i*2+1];
newAppConfig.MyMACAddr.v[i] = hexatob(*((WORD_VAL*)&w));
}
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"host"))
{// Read new hostname
FormatNetBIOSName(&curHTTP.data[6]);
memcpy((void*)newAppConfig.NetBIOSName, (void*)curHTTP.data+6, 16);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"dhcp"))
{// Read new DHCP Enabled flag
if(curHTTP.data[6] == '1')
newAppConfig.Flags.bIsDHCPEnabled = 1;
}
}
// All parsing complete! Save new settings and force a reboot
SaveAppConfig(&newAppConfig);
// Set the board to reboot and display reconnecting information
strcpypgm2ram((char*)curHTTP.data, "/protect/reboot.htm?");
memcpy((void*)(curHTTP.data+20), (void*)newAppConfig.NetBIOSName, 16);
curHTTP.data[20+16] = 0x00; // Force null termination
for(i = 20; i < 20u+16u; i++)
{
if(curHTTP.data[i] == ' ')
curHTTP.data[i] = 0x00;
}
curHTTP.httpStatus = HTTP_REDIRECT;
return HTTP_IO_DONE;
ConfigFailure:
lastFailure = TRUE;
strcpypgm2ram((char*)curHTTP.data, "/protect/config.htm");
curHTTP.httpStatus = HTTP_REDIRECT;
return HTTP_IO_DONE;
}
static HTTP_IO_RESULT HTTPPostWebConf(void)
{
WEBSRV_CONFIG newWebSrvConfig;
BYTE *ptr;
BYTE i;
if(curHTTP.byteCount > TCPIsGetReady(sktHTTP) + TCPGetRxFIFOFree(sktHTTP))
goto WebConfigFailure;
// Ensure that all data is waiting to be parsed. If not, keep waiting for
// all of it to arrive.
if(TCPIsGetReady(sktHTTP) < curHTTP.byteCount)
return HTTP_IO_NEED_DATA;
eepromReadBytes((BYTE*)&newWebSrvConfig, EEPROM_MAP_WEBSRVCONFIG, sizeof(newWebSrvConfig));
// Start out assuming flags are disabled. This is necessary since the
// browser doesn't submit this field if it is unchecked (meaning zero).
// However, if it is checked, this will be overridden since it will be
// submitted.
newWebSrvConfig.Flags.DataRequireHTTPS = 0;
newWebSrvConfig.Flags.DataRequireAuth = 0;
// Read all browser POST data
while(curHTTP.byteCount)
{
// Read a form field name
if(HTTPReadPostName(curHTTP.data, 6) != HTTP_READ_OK)
goto WebConfigFailure;
// Read a form field value
if(HTTPReadPostValue(curHTTP.data + 6, sizeof(curHTTP.data)-6-2) != HTTP_READ_OK)
goto WebConfigFailure;
// Parse the value that was read
if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"http"))
{
newWebSrvConfig.HTTPPort = atol(curHTTP.data + 6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"ssl"))
{
newWebSrvConfig.HTTPSPort = atol(curHTTP.data + 6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"user"))
{
strcpy((void*)newWebSrvConfig.AuthUser, (void*)curHTTP.data+6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"pass"))
{
strcpy((void*)newWebSrvConfig.AuthPwd, (void*)curHTTP.data+6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"reqS"))
{
if(curHTTP.data[6] == '1')
newWebSrvConfig.Flags.DataRequireHTTPS = 1;
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"reqA"))
{
if(curHTTP.data[6] == '1')
newWebSrvConfig.Flags.DataRequireAuth = 1;
}
//Timeout on Idle is not supported
/* else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"idle"))
{
newWebSrvConfig.StateTimeout[COMMSTATE_IDLE] = atol(curHTTP.data + 6);
} */
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"buff"))
{
newWebSrvConfig.StateTimeout[COMMSTATE_BUFFERING] = atol(curHTTP.data + 6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"txr"))
{
newWebSrvConfig.StateTimeout[COMMSTATE_TXREADY] = atol(curHTTP.data + 6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"tx"))
{
newWebSrvConfig.StateTimeout[COMMSTATE_TX] = atol(curHTTP.data + 6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"wait"))
{
newWebSrvConfig.StateTimeout[COMMSTATE_WAIT] = atol(curHTTP.data + 6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"rx"))
{
newWebSrvConfig.StateTimeout[COMMSTATE_RX] = atol(curHTTP.data + 6);
}
else if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"msg"))
{
newWebSrvConfig.StateTimeout[COMMSTATE_MSG] = atol(curHTTP.data + 6);
}
}
// All parsing complete! Save new settings and force a reboot
SaveWebSrvConfig(&newWebSrvConfig);
// Set the board to reboot and display reconnecting information
strcpypgm2ram((char*)curHTTP.data, "/protect/reboot.htm?");
curHTTP.data[20] = 0x00; // Force null termination
for(i = 20; i < 20u+16u; i++)
curHTTP.httpStatus = HTTP_REDIRECT;
return HTTP_IO_DONE;
WebConfigFailure:
lastFailure = TRUE;
strcpypgm2ram((char*)curHTTP.data, "/protect/webconf.htm");
curHTTP.httpStatus = HTTP_REDIRECT;
return HTTP_IO_DONE;
}
static HTTP_IO_RESULT HTTPPostBTCom(void)
{
BYTE *ptr;
BYTE i;
// Ensure that all data is waiting to be parsed. If not, keep waiting for
// all of it to arrive.
if(TCPIsGetReady(sktHTTP) < curHTTP.byteCount)
return HTTP_IO_NEED_DATA;
// Read all browser POST data
while(curHTTP.byteCount)
{
// Read a form field name
HTTPReadPostName(curHTTP.data, 6);
HTTPReadPostValue(curHTTP.data + 6, sizeof(curHTTP.data)-6-2);
// Parse the value that was read
if(!strcmppgm2ram((char*)curHTTP.data, (ROM char*)"rst"))
{
BTCommInit();
}
}
return HTTP_IO_DONE;
}
#endif //(use_post)
#endif
void HTTPPrint_BTVer(void)
{
TCPPutROMString(sktHTTP, (ROM void*)"2.5 Build 1");
}
void HTTPPrint_BTState(void)
{
switch (BTCommGetState())
{
case COMMSTATE_IDLE:
TCPPutROMString(sktHTTP, (ROM void*)"IDLE");
break;
case COMMSTATE_BUFFERING:
TCPPutROMString(sktHTTP, (ROM void*)"BUFFERING");
break;
case COMMSTATE_TXREADY:
TCPPutROMString(sktHTTP, (ROM void*)"TXREADY");
break;
case COMMSTATE_TX:
TCPPutROMString(sktHTTP, (ROM void*)"TX");
break;
case COMMSTATE_WAIT:
TCPPutROMString(sktHTTP, (ROM void*)"WAIT");
break;
case COMMSTATE_RX:
TCPPutROMString(sktHTTP, (ROM void*)"RX");
break;
case COMMSTATE_MSG:
TCPPutROMString(sktHTTP, (ROM void*)"MSG");
break;
default:
TCPPutROMString(sktHTTP, (ROM void*)"UNDEFINED");
break;
}
}
void HTTPPrint_BTNic_CGI(void)
{
WORD len;
len = TCPIsPutReady(sktHTTP);
if(curHTTP.callbackPos == 0u)
{
curHTTP.callbackPos = BTCommGetRspLen();
TCPPutROMString(sktHTTP, (ROM BYTE*)"[\"");
len -= 2;
}
while(len > 5 && curHTTP.callbackPos)
{
char byteOut = BTCommGetRsp();
if (byteOut == '\t')
{
TCPPutROMString(sktHTTP, (ROM BYTE*)"\",\"");
len -= 3;
}
else len -= TCPPut(sktHTTP, byteOut);
curHTTP.callbackPos--;
}
if(curHTTP.callbackPos == 0u) TCPPutROMString(sktHTTP, (ROM BYTE*)"\"]");
return;
}
void HTTPPrint_SSP1CON1_WCOL(void)
{
TCPPut(sktHTTP, SSP1CON1bits.WCOL ? '1' : '0');
}
void HTTPPrint_SSP1CON1_SSPOV(void)
{
TCPPut(sktHTTP, SSP1CON1bits.SSPOV ? '1' : '0');
}
void HTTPPrint_SSP1CON1_SSPEN(void)
{
TCPPut(sktHTTP, SSP1CON1bits.SSPEN ? '1' : '0');
}
void HTTPPrint_SSP1CON1_CKP(void)
{
TCPPut(sktHTTP, SSP1CON1bits.CKP ? '1' : '0');
}
void HTTPPrint_SSP1CON1_SSPM3(void)
{
TCPPut(sktHTTP, SSP1CON1bits.SSPM3 ? '1' : '0');
}
void HTTPPrint_SSP1CON1_SSPM2(void)
{
TCPPut(sktHTTP, SSP1CON1bits.SSPM2 ? '1' : '0');
}
void HTTPPrint_SSP1CON1_SSPM1(void)
{
TCPPut(sktHTTP, SSP1CON1bits.SSPM1 ? '1' : '0');
}
void HTTPPrint_SSP1CON1_SSPM0(void)
{
TCPPut(sktHTTP, SSP1CON1bits.SSPM0 ? '1' : '0');
}
void HTTPPrint_SSP1CON2_GCEN(void)
{
TCPPut(sktHTTP, SSP1CON2bits.GCEN ? '1' : '0');
}
void HTTPPrint_SSP1CON2_ACKSTAT(void)
{
TCPPut(sktHTTP, SSP1CON2bits.ACKSTAT ? '1' : '0');
}
void HTTPPrint_SSP1CON2_ADMSK5(void)
{
TCPPut(sktHTTP, SSP1CON2bits.ADMSK5 ? '1' : '0');
}
void HTTPPrint_SSP1CON2_ADMSK4(void)
{
TCPPut(sktHTTP, SSP1CON2bits.ADMSK4 ? '1' : '0');
}
void HTTPPrint_SSP1CON2_ADMSK3(void)
{
TCPPut(sktHTTP, SSP1CON2bits.ADMSK3 ? '1' : '0');
}
void HTTPPrint_SSP1CON2_ADMSK2(void)
{
TCPPut(sktHTTP, SSP1CON2bits.ADMSK2 ? '1' : '0');
}
void HTTPPrint_SSP1CON2_ADMSK1(void)
{
TCPPut(sktHTTP, SSP1CON2bits.ADMSK1 ? '1' : '0');
}
void HTTPPrint_SSP1CON2_SEN(void)
{
TCPPut(sktHTTP, SSP1CON2bits.SEN ? '1' : '0');
}
void HTTPPrint_SSP1STAT_SMP(void)
{
TCPPut(sktHTTP, SSP1STATbits.SMP ? '1' : '0');
}
void HTTPPrint_SSP1STAT_CKE(void)
{
TCPPut(sktHTTP, SSP1STATbits.CKE ? '1' : '0');
}
void HTTPPrint_SSP1STAT_P(void)
{
TCPPut(sktHTTP, SSP1STATbits.P ? '1' : '0');
}
void HTTPPrint_SSP1STAT_S(void)
{
TCPPut(sktHTTP, SSP1STATbits.S ? '1' : '0');
}
void HTTPPrint_SSP1STAT_UA(void)
{
TCPPut(sktHTTP, SSP1STATbits.UA ? '1' : '0');
}
void HTTPPrint_SSP1STAT_BF(void)
{
TCPPut(sktHTTP, SSP1STATbits.BF ? '1' : '0');
}
void HTTPPrint_SSP1STAT_DA(void)
{
TCPPut(sktHTTP, SSP1STATbits.D_A ? '1' : '0');
}
void HTTPPrint_SSP1STAT_RW(void)
{
TCPPut(sktHTTP, SSP1STATbits.R_W ? '1' : '0');
}
void HTTPPrint_BTCommTimer(void)
{
unsigned char string[20];
ultoa(BTCommGetTimer() / (TICK_SECOND / 1000), string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_TickGet(void)
{
unsigned char string[20];
ultoa(TickGet() / (TICK_SECOND / 1000), string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_BTResponseLen(void)
{
unsigned char string[9];
itoa(BTCommGetRspLen(), string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_BTRequestLen(void)
{
unsigned char string[9];
itoa(BTCommGetReqLen(), string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_BTResponseBuffer(void)
{
WORD len;
len = TCPIsPutReady(sktHTTP);
if(curHTTP.callbackPos == 0u) curHTTP.callbackPos = BTCommGetRspLen();
while(len && curHTTP.callbackPos)
{
len -= TCPPut(sktHTTP, BTCommGetRspBuffer(BTCommGetRspLen() - curHTTP.callbackPos));
curHTTP.callbackPos--;
}
return;
}
void HTTPPrint_BTRequestBuffer(void)
{
WORD len;
len = TCPIsPutReady(sktHTTP);
if(curHTTP.callbackPos == 0u) curHTTP.callbackPos = BTCommGetReqLen();
while(len && curHTTP.callbackPos)
{
len -= TCPPut(sktHTTP, BTCommGetReqBuffer(BTCommGetReqLen() - curHTTP.callbackPos));
curHTTP.callbackPos--;
}
return;
}
extern APP_CONFIG AppConfig;
void HTTPPrintIP(IP_ADDR ip)
{
BYTE digits[4];
BYTE i;
for(i = 0; i < 4u; i++)
{
if(i)
TCPPut(sktHTTP, '.');
uitoa(ip.v[i], digits);
TCPPutString(sktHTTP, digits);
}
}
void HTTPPrint_config_hostname(void)
{
TCPPutString(sktHTTP, AppConfig.NetBIOSName);
return;
}
void HTTPPrint_config_dhcpchecked(void)
{
if(AppConfig.Flags.bIsDHCPEnabled)
TCPPutROMString(sktHTTP, (ROM BYTE*)"checked");
return;
}
void HTTPPrint_config_ip(void)
{
HTTPPrintIP(AppConfig.MyIPAddr);
return;
}
void HTTPPrint_config_gw(void)
{
HTTPPrintIP(AppConfig.MyGateway);
return;
}
void HTTPPrint_config_subnet(void)
{
HTTPPrintIP(AppConfig.MyMask);
return;
}
void HTTPPrint_config_dns1(void)
{
HTTPPrintIP(AppConfig.PrimaryDNSServer);
return;
}
void HTTPPrint_config_dns2(void)
{
HTTPPrintIP(AppConfig.SecondaryDNSServer);
return;
}
void HTTPPrint_config_mac(void)
{
BYTE i;
if(TCPIsPutReady(sktHTTP) < 18u)
{//need 17 bytes to write a MAC
curHTTP.callbackPos = 0x01;
return;
}
// Write each byte
for(i = 0; i < 6u; i++)
{
if(i)
TCPPut(sktHTTP, ':');
TCPPut(sktHTTP, btohexa_high(AppConfig.MyMACAddr.v[i]));
TCPPut(sktHTTP, btohexa_low(AppConfig.MyMACAddr.v[i]));
}
// Indicate that we're done
curHTTP.callbackPos = 0x00;
return;
}
void HTTPPrint_config_stateTimeIdle(void)
{
unsigned char string[20];
ultoa(WebSrvConfig.StateTimeout[COMMSTATE_IDLE], string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_config_stateTimeBuffering(void)
{
unsigned char string[20];
ultoa(WebSrvConfig.StateTimeout[COMMSTATE_BUFFERING], string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_config_stateTimeTXReady(void)
{
unsigned char string[20];
ultoa(WebSrvConfig.StateTimeout[COMMSTATE_TXREADY], string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_config_stateTimeTX(void)
{
unsigned char string[20];
ultoa(WebSrvConfig.StateTimeout[COMMSTATE_TX], string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_config_stateTimeWait(void)
{
unsigned char string[20];
ultoa(WebSrvConfig.StateTimeout[COMMSTATE_WAIT], string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_config_stateTimeRX(void)
{
unsigned char string[20];
ultoa(WebSrvConfig.StateTimeout[COMMSTATE_RX], string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_config_stateTimeMsg(void)
{
unsigned char string[20];
ultoa(WebSrvConfig.StateTimeout[COMMSTATE_MSG], string);
TCPPutString(sktHTTP, string);
}
void HTTPPrint_reboot(void)
{
// This is not so much a print function, but causes the board to reboot
// when the configuration is changed. If called via an AJAX call, this
// will gracefully reset the board and bring it back online immediately
Reset();
}
void HTTPPrint_rebootaddr(void)
{// This is the expected address of the board upon rebooting
TCPPutString(sktHTTP, curHTTP.data);
}
void HTTPPrint_status_ok(void)
{
if(lastSuccess)
TCPPutROMString(sktHTTP, (ROM BYTE*)"block");
else
TCPPutROMString(sktHTTP, (ROM BYTE*)"none");
lastSuccess = FALSE;
}
void HTTPPrint_status_fail(void)
{
if(lastFailure)
TCPPutROMString(sktHTTP, (ROM BYTE*)"block");
else
TCPPutROMString(sktHTTP, (ROM BYTE*)"none");
lastFailure = FALSE;
}
void HTTPPrint_MEMDUMP(WORD memType)
{
DWORD len;
unsigned short long size;
len = TCPIsPutReady(sktHTTP);
if (memType == 0u) size = 256ul; //EEPROM 256 Bytes
else if (memType == 1u) size = 4194304ul; //Flash xxx bytes
else if (memType == 2u) size = 32768ul; //SRAM xxx bytes
else return;
if(curHTTP.callbackPos == 0u) curHTTP.callbackPos = size;
while(len > 12 && curHTTP.callbackPos)
{
char data[1];
if ((curHTTP.callbackPos & 63) == 0) {
char lineNumber[7];
sprintf(lineNumber, "%06lx", size - curHTTP.callbackPos);
//Start of line
TCPPutROMString(sktHTTP, (ROM BYTE*)"<br>");
TCPPutString(sktHTTP, lineNumber);
len -= 10;
}
//Continue line
TCPPutROMString(sktHTTP, (ROM BYTE*)" ");
len--;
if (memType == 0) data[0] = eepromReadByte(size - curHTTP.callbackPos);
else if (memType == 1) SPIFlashReadArray(size - curHTTP.callbackPos, data, 1);
else if (memType == 2) data[0] = sramReadByte(size - curHTTP.callbackPos);