-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.c
1320 lines (1087 loc) · 37.9 KB
/
main2.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
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
/*
* main.c - HTTP server sample example
*
* Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 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.
*
* Neither the name of Texas Instruments Incorporated 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 COPYRIGHT HOLDERS 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 COPYRIGHT
* OWNER 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.
*
*/
/*
* Application Name - HTTP Server
* Application Overview - This is a sample application demonstrating the
* capability of CC3100 device to work as a
* web-server and allowing the end-users to
* communicate w/ it using standard web-browsers.
* Application Details - http://processors.wiki.ti.com/index.php/CC31xx_HTTP_Server
* doc\examples\http_server.pdf
*/
#include "simplelink.h"
#include "protocol.h"
#include "sl_common.h"
#define APPLICATION_VERSION "1.1.0"
#define SL_STOP_TIMEOUT 0xFF
/* Application specific status/error codes */
typedef enum{
DEVICE_NOT_IN_STATION_MODE = -0x7D0, /* Choosing this number to avoid overlap w/ host-driver's error codes */
STATUS_CODE_MAX = -0xBB8
}e_AppStatusCodes;
/* The tokens cna be declared as const to be placed in Flash. Declared as variables in RAM for faster access */
/* definition of http put events */
_u8 POST_token[] = "__SL_P_L.D";
_u8 GET_token[] = "__SL_G_LED";
_u8 POST_token0[] = "__SL_P_U00"; /* turn on LED */
_u8 POST_token1[] = "__SL_P_U01"; /* turn off LED */
_u8 POST_token2[] = "__SL_P_U02"; /* turn on LED toggle */
_u8 POST_token3[] = "__SL_P_U03"; /* turn off LED toggle */
_u8 POST_token4[] = "__SL_P_U04"; /* set LED toggling speed */
_u8 POST_token5[] = "__SL_P_U05"; /* clear button 1 count */
_u8 POST_token6[] = "__SL_P_U06"; /* clear button 2 count */
_u8 POST_token7[] = "__SL_P_U07"; /* update temperature reading */
_u8 POST_token8[] = "__SL_P_U08"; /* set temperature unit in C */
_u8 POST_token9[] = "__SL_P_U09"; /* set temperature unit in F */
/* definition of http get events */
_u8 GET_token0[] = "__SL_G_U00"; /* return LED on */
_u8 GET_token1[] = "__SL_G_U01"; /* return LED off */
_u8 GET_token2[] = "__SL_G_U02"; /* return LED toggle on */
_u8 GET_token3[] = "__SL_G_U03"; /* return LED toggle off */
_u8 GET_token4[] = "__SL_G_U04"; /* return LED toogle speed */
_u8 GET_token5[] = "__SL_G_U05"; /* return button 1 count */
_u8 GET_token6[] = "__SL_G_U06"; /* return button 2 count */
_u8 GET_token7[] = "__SL_G_U07"; /* return temperature */
_u8 GET_token8[] = "__SL_G_U08"; /* return temperature unit in C */
_u8 GET_token9[] = "__SL_G_U09"; /* return temperature unit in F */
_u32 g_Status = 0;
_u8 g_auth_name[MAX_AUTH_NAME_LEN+1];
_u8 g_auth_password[MAX_AUTH_PASSWORD_LEN+1];
_u8 g_auth_realm[MAX_AUTH_REALM_LEN+1];
_u8 g_domain_name[MAX_DOMAIN_NAME_LEN];
_u8 g_device_urn[MAX_DEVICE_URN_LEN];
/*
* STATIC FUNCTION DEFINITIONS -- Start
*/
static _i32 configureSimpleLinkToDefaultState();
static _i32 initializeAppVariables();
static void displayBanner();
static _i32 set_authentication_check (_u8 enable);
static _i32 get_auth_name (_u8 *auth_name);
static _i32 get_auth_password (_u8 *auth_password);
static _i32 get_auth_realm (_u8 *auth_realm);
static _i32 get_device_urn (_u8 *device_urn);
static _i32 get_domain_name (_u8 *domain_name);
_i32 set_port_number(_u16 num);
void HttpServer();
extern Semaphore_Handle Semaphore_CC3100_Req;
/*
* ASYNCHRONOUS EVENT HANDLERS -- Start
*/
/*!
\brief This function handles WLAN events
\param[in] pWlanEvents is the event passed to the handler
\return none
\note
\warning
*/
void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent)
{
if(pWlanEvent == NULL)
CLI_Write(" [WLAN EVENT] NULL Pointer Error \n\r");
switch(pWlanEvent->Event)
{
case SL_WLAN_CONNECT_EVENT:
{
SET_STATUS_BIT(g_Status, STATUS_BIT_CONNECTION);
/*
* Information about the connected AP (like name, MAC etc) will be
* available in 'slWlanConnectAsyncResponse_t' - Applications
* can use it if required
*
* slWlanConnectAsyncResponse_t *pEventData = NULL;
* pEventData = &pWlanEvent->EventData.STAandP2PModeWlanConnected;
*
*/
}
break;
case SL_WLAN_DISCONNECT_EVENT:
{
slWlanConnectAsyncResponse_t* pEventData = NULL;
CLR_STATUS_BIT(g_Status, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED);
pEventData = &pWlanEvent->EventData.STAandP2PModeDisconnected;
/* If the user has initiated 'Disconnect' request, 'reason_code' is SL_USER_INITIATED_DISCONNECTION */
if(SL_USER_INITIATED_DISCONNECTION == pEventData->reason_code)
{
CLI_Write((_u8 *)" Device disconnected from the AP on application's request \n\r");
}
else
{
CLI_Write((_u8 *)" Device disconnected from the AP on an ERROR..!! \n\r");
}
}
break;
case SL_WLAN_STA_CONNECTED_EVENT:
{
SET_STATUS_BIT(g_Status, STATUS_BIT_STA_CONNECTED);
}
break;
case SL_WLAN_STA_DISCONNECTED_EVENT:
{
CLR_STATUS_BIT(g_Status, STATUS_BIT_STA_CONNECTED);
CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_LEASED);
}
break;
default:
{
CLI_Write((_u8 *)" [WLAN EVENT] Unexpected event \n\r");
}
break;
}
}
/*!
\brief This function handles callback for the HTTP server events
\param[in] pEvent - Contains the relevant event information
\param[in] pResponse - Should be filled by the user with the
relevant response information
\return None
\note
\warning
*/
void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pEvent,
SlHttpServerResponse_t *pResponse)
{
unsigned int ulSpeed, n;
if(pEvent == NULL || pResponse == NULL)
CLI_Write(" [HTTP EVENT] NULL Pointer Error \n\r");
switch (pEvent->Event)
{
case SL_NETAPP_HTTPGETTOKENVALUE_EVENT:
{
_u8 status = 0;
_u8 *ptr = 0;
ptr = pResponse->ResponseData.token_value.data;
pResponse->ResponseData.token_value.len = 0;
/* Check if the token is for the original CC3100 demo */
if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token,
pal_Strlen(GET_token)) == 0)
{
//status = GetLEDStatus();
pal_Memcpy(ptr, "LED1_", pal_Strlen("LED1_"));
ptr += 5;
pResponse->ResponseData.token_value.len += 5;
if(status & 0x01)
{
pal_Memcpy(ptr, "ON", pal_Strlen("ON"));
ptr += 2;
pResponse->ResponseData.token_value.len += 2;
}
else
{
pal_Memcpy(ptr, "OFF", pal_Strlen("OFF"));
ptr += 3;
pResponse->ResponseData.token_value.len += 3;
}
pal_Memcpy(ptr,",LED2_", pal_Strlen(",LED2_"));
ptr += 6;
pResponse->ResponseData.token_value.len += 6;
if(status & 0x02)
{
pal_Memcpy(ptr, "ON", pal_Strlen("ON"));
ptr += 2;
pResponse->ResponseData.token_value.len += 2;
}
else
{
pal_Memcpy(ptr, "OFF", pal_Strlen("OFF"));
ptr += 3;
pResponse->ResponseData.token_value.len += 3;
}
*ptr = '\0';
}
/* Check if the token is for the added MCU LED toggle demo */
/* check if the get LED on event, return the LED on status */
if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token0,
pal_Strlen(GET_token0)) == 0)
{
if(g_ui32LED_Status)
{
pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* return the on status to update the "LED ON" button */
ptr += 7;
pResponse->ResponseData.token_value.len += 7;
}
else
{
pal_Memcpy(ptr, " ", pal_Strlen(" ")); /* do not update the "LED ON" button */
ptr += 1;
pResponse->ResponseData.token_value.len += 1;
}
*ptr = '\0';
}
/* check if the get LED off event, return the LED off status */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token1,
pal_Strlen(GET_token1)) == 0)
{
if(!g_ui32LED_Status)
{
pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* return the off status to update the "LED OFF" button */
ptr += 7;
pResponse->ResponseData.token_value.len += 7;
}
else
{
pal_Memcpy(ptr, " ", pal_Strlen(" ")); /* do not update the "LED OFF" button */
ptr += 1;
pResponse->ResponseData.token_value.len += 1;
}
*ptr = '\0';
}
/* check if the get LED toggle on event */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token2,
pal_Strlen(GET_token2)) == 0)
{
if(g_ui32LED_Toggle_Status)
{
pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* return the on status to update the "LED toggle on" button */
ptr += 7;
pResponse->ResponseData.token_value.len += 7;
}
else
{
pal_Memcpy(ptr, " ", pal_Strlen(" ")); /* do not update the "LED toggle on" button */
ptr += 1;
pResponse->ResponseData.token_value.len += 1;
}
*ptr = '\0';
}
/* check if the get LED toggle off event */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token3,
pal_Strlen(GET_token3)) == 0)
{
if(!g_ui32LED_Toggle_Status)
{
pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* return the off status to update the "LED toggle off" button */
ptr += 7;
pResponse->ResponseData.token_value.len += 7;
}
else
{
pal_Memcpy(ptr, " ", pal_Strlen(" ")); /* do not update the "LED toggle off" button */
ptr += 1;
pResponse->ResponseData.token_value.len += 1;
}
*ptr = '\0';
}
/* check if the get toggle speed event */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token4,
pal_Strlen(GET_token4)) == 0)
{
n = Decimal2String(ptr, g_ui32AnimSpeed); /* write speed to the buffer and update the string length */
ptr += n;
pResponse->ResponseData.token_value.len += n;
*ptr = '\0';
}
/* check if the get button 1 count event */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token5,
pal_Strlen(GET_token5)) == 0)
{
n = Decimal2String(ptr, g_ui32SW1Presses); /* write number of button press to the buffer and update the string length */
ptr += n;
pResponse->ResponseData.token_value.len += n;
*ptr = '\0';
}
/* check if the get button 2 count event */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token6,
pal_Strlen(GET_token6)) == 0)
{
n = Decimal2String(ptr, g_ui32SW2Presses); /* write number of button press to the buffer and update the string length */
ptr += n;
pResponse->ResponseData.token_value.len += n;
*ptr = '\0';
}
/* check if the get temperature event */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token7,
pal_Strlen(GET_token7)) == 0)
{
if(!g_ui32Temperature_U)
{
n = Decimal2String(ptr, g_ui32InternalTempC); /* write temperature in C to the buffer and update the string length */
}
else
{
n = Decimal2String(ptr, g_ui32InternalTempF); /* write temperature in F to the buffer and update the string length */
}
ptr += n;
pResponse->ResponseData.token_value.len += n;
*ptr = '\0';
}
/* check if the temperature in C event */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token8,
pal_Strlen(GET_token8)) == 0)
{
if(!g_ui32Temperature_U)
{
pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* update the temperature in C button and update the string length */
ptr += 7;
pResponse->ResponseData.token_value.len += 7;
}
else
{
pal_Memcpy(ptr, " ", pal_Strlen(" "));
ptr += 1;
pResponse->ResponseData.token_value.len += 1;
}
*ptr = '\0';
}
/* check if the temperature in F event */
else if(pal_Memcmp(pEvent->EventData.httpTokenName.data, GET_token9,
pal_Strlen(GET_token9)) == 0)
{
if(g_ui32Temperature_U)
{
pal_Memcpy(ptr, "checked", pal_Strlen("checked")); /* update the temperature in F button and update the string length */
ptr += 7;
pResponse->ResponseData.token_value.len += 7;
}
else
{
pal_Memcpy(ptr, " ", pal_Strlen(" "));
ptr += 1;
pResponse->ResponseData.token_value.len += 1;
}
*ptr = '\0';
}
}
break;
case SL_NETAPP_HTTPPOSTTOKENVALUE_EVENT:
{
_u8 led = 0;
_u8 *ptr = pEvent->EventData.httpPostData.token_name.data;
/* Check if the post command is for original CC310 demo */
if(pal_Memcmp(ptr, POST_token, pal_Strlen(POST_token)) == 0)
{
ptr = pEvent->EventData.httpPostData.token_value.data;
if(pal_Memcmp(ptr, "LED", 3) != 0)
break;
ptr += 3;
led = *ptr;
ptr += 2;
if(led == '1')
{
if(pal_Memcmp(ptr, "ON", 2) == 0)
{
turnLedOn(LED1);
}
else
{
turnLedOff(LED1);
}
}
else if(led == '2')
{
if(pal_Memcmp(ptr, "ON", 2) == 0)
{
turnLedOn(LED2);
}
else
{
turnLedOff(LED2);
}
}
}
/* Check if the post command is for add MCU demo: LED on */
if(pal_Memcmp(ptr, POST_token0, pal_Strlen(POST_token0)) == 0)
{
g_ui32LED_Status =1; /* turn the LED on */
turnLedOn(LED1);
if(g_ui32LED_Toggle_Status) /* reenable LED toggling */
{
IoSetTimer(g_ui32AnimSpeed);
}
}
/* Check if the post command is for add MCU demo: LED off */
else if(pal_Memcmp(ptr, POST_token1, pal_Strlen(POST_token1)) == 0)
{
g_ui32LED_Status =0;
turnLedOff(LED1);
if(g_ui32LED_Toggle_Status)
{
IoSetTimer(0); /*reenable LED toggling */
}
}
/* Check if the post command is for add MCU demo: LED toggle on */
else if(pal_Memcmp(ptr, POST_token2, pal_Strlen(POST_token2)) == 0)
{
g_ui32LED_Toggle_Status = 1;
if(g_ui32LED_Status)
{
IoSetTimer(g_ui32AnimSpeed); /* enable LED toggling */
}
}
/* Check if the post command is for add MCU demo: LED toggle off */
else if(pal_Memcmp(ptr, POST_token3, pal_Strlen(POST_token3)) == 0)
{
g_ui32LED_Toggle_Status = 0;
IoSetTimer(0); /* disable toggling */
if(g_ui32LED_Status)
{
turnLedOn(LED1);
}
else
{
turnLedOff(LED1);
}
}
/* Check if the post command is for add MCU demo: set LED toggling speed */
else if(pal_Memcmp(ptr, POST_token4, pal_Strlen(POST_token4)) == 0)
{
ptr = pEvent->EventData.httpPostData.token_value.data;
ulSpeed = String2Decimal(ptr); /* get speed value from string */
g_ui32AnimSpeed = ulSpeed;
if (g_ui32LED_Toggle_Status && g_ui32LED_Status)
{
IoSetTimer(ulSpeed);
}
}
/* Check if the post command is for add MCU demo: Clear button 1 count */
else if(pal_Memcmp(ptr, POST_token5, pal_Strlen(POST_token5)) == 0)
{
g_ui32SW1Presses = 0;
}
/* Check if the post command is for add MCU demo: Clear button 2 count */
else if(pal_Memcmp(ptr, POST_token6, pal_Strlen(POST_token6)) == 0)
{
g_ui32SW2Presses = 0;
}
/* Check if the post command is for add MCU demo: temperature in C */
else if(pal_Memcmp(ptr, POST_token8, pal_Strlen(POST_token8)) == 0)
{
g_ui32Temperature_U = 0;
}
/* Check if the post command is for add MCU demo: temperature in F */
else if(pal_Memcmp(ptr, POST_token9, pal_Strlen(POST_token9)) == 0)
{
g_ui32Temperature_U = 1;
}
}
break;
default:
break;
}
}
/*!
\brief This function handles events for IP address acquisition via DHCP
indication
\param[in] pNetAppEvent is the event passed to the handler
\return None
\note
\warning
*/
void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent)
{
if(pNetAppEvent == NULL)
CLI_Write(" [NETAPP EVENT] NULL Pointer Error \n\r");
switch(pNetAppEvent->Event)
{
case SL_NETAPP_IPV4_IPACQUIRED_EVENT:
{
SET_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED);
/*
* Information about the connected AP's IP, gateway, DNS etc
* will be available in 'SlIpV4AcquiredAsync_t' - Applications
* can use it if required
*
* SlIpV4AcquiredAsync_t *pEventData = NULL;
* pEventData = &pNetAppEvent->EventData.ipAcquiredV4;
* <gateway_ip> = pEventData->gateway;
*
*/
}
break;
case SL_NETAPP_IP_LEASED_EVENT:
{
SET_STATUS_BIT(g_Status, STATUS_BIT_IP_LEASED);
}
break;
default:
{
CLI_Write(" [NETAPP EVENT] Unexpected event \n\r");
}
break;
}
}
/*!
\brief This function handles general error events indication
\param[in] pDevEvent is the event passed to the handler
\return None
*/
void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent)
{
/*
* Most of the general errors are not FATAL are are to be handled
* appropriately by the application
*/
CLI_Write(" [GENERAL EVENT] \n\r");
}
/*!
\brief This function handles socket events indication
\param[in] pSock is the event passed to the handler
\return None
*/
void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
{
/*
* This application doesn't work with socket - Hence these
* events are not handled here
*/
CLI_Write(" [SOCK EVENT] Unexpected event \n\r");
}
/*
* ASYNCHRONOUS EVENT HANDLERS -- End
*/
/*
* Application's entry point
*/
int main(int argc, char** argv)
{
_u8 SecType = 0;
_i32 retVal = -1;
_i32 mode = ROLE_STA;
retVal = initializeAppVariables();
ASSERT_ON_ERROR(retVal);
/* Stop WDT and initialize the system-clock of the MCU */
stopWDT();//WDT-Watch Dog Timer
initClk();
initLEDs();
/* Configure command line interface */
CLI_Configure();
displayBanner();
/*
* Following function configures the device to default state by cleaning
* the persistent settings stored in NVMEM (viz. connection profiles &
* policies, power policy etc)
*
* Applications may choose to skip this step if the developer is sure
* that the device is in its default state at start of application
*
* Note that all profiles and persistent settings that were done on the
* device will be lost
*/
retVal = configureSimpleLinkToDefaultState();
if(retVal < 0)
{
if (DEVICE_NOT_IN_STATION_MODE == retVal)
CLI_Write(" Failed to configure the device in its default state \n\r");
LOOP_FOREVER();
}
CLI_Write(" Device is configured in default state \n\r");
/*
* Assumption is that the device is configured in station mode already
* and it is in its default state
*/
mode = sl_Start(0, 0, 0);
if(mode < 0)
{
LOOP_FOREVER();
}
else
{
if (ROLE_AP == mode)
{
/* If the device is in AP mode, we need to wait for this
* event before doing anything */
while(!IS_IP_ACQUIRED(g_Status)) { _SlNonOsMainLoopTask(); }
}
else
{
/* Configure CC3100 to start in AP mode */
retVal = sl_WlanSetMode(ROLE_AP);
if(retVal < 0)
LOOP_FOREVER();
}
}
/* Configure AP mode without security */
retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID,
pal_Strlen(SSID_AP_MODE), (_u8 *)SSID_AP_MODE);
if(retVal < 0)
LOOP_FOREVER();
SecType = SEC_TYPE_AP_MODE;
/* Configure the Security parameter in the AP mode */
retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1,
(_u8 *)&SecType);
if(retVal < 0)
LOOP_FOREVER();
retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, pal_Strlen(PASSWORD_AP_MODE),
(_u8 *)PASSWORD_AP_MODE);
if(retVal < 0)
LOOP_FOREVER();
/* Restart the CC3100 */
retVal = sl_Stop(SL_STOP_TIMEOUT);
if(retVal < 0)
LOOP_FOREVER();
g_Status = 0;
mode = sl_Start(0, 0, 0);
if (ROLE_AP == mode)
{
/* If the device is in AP mode, we need to wait for this event before doing anything */
while(!IS_IP_ACQUIRED(g_Status)) { _SlNonOsMainLoopTask(); }
}
else
{
CLI_Write(" Device couldn't come in AP mode \n\r");
LOOP_FOREVER();
}
CLI_Write(" \r\n Device is configured in AP mode \n\r");
CLI_Write(" Waiting for client to connect\n\r");
/* wait for client to connect */
while((!IS_IP_LEASED(g_Status)) || (!IS_STA_CONNECTED(g_Status))) { _SlNonOsMainLoopTask(); }
CLI_Write(" Client connected\n\r");
/* Enable the HTTP Authentication */
retVal = set_authentication_check(TRUE);
if(retVal < 0)
LOOP_FOREVER();
/* Get authentication parameters */
retVal = get_auth_name(g_auth_name);
if(retVal < 0)
LOOP_FOREVER();
retVal = get_auth_password(g_auth_password);
if(retVal < 0)
LOOP_FOREVER();
retVal = get_auth_realm(g_auth_realm);
if(retVal < 0)
LOOP_FOREVER();
CLI_Write((_u8 *)"\r\n Authentication parameters: ");
CLI_Write((_u8 *)"\r\n Name = ");
CLI_Write(g_auth_name);
CLI_Write((_u8 *)"\r\n Password = ");
CLI_Write(g_auth_password);
CLI_Write((_u8 *)"\r\n Realm = ");
CLI_Write(g_auth_realm);
/* Get the domain name */
retVal = get_domain_name(g_domain_name);
if(retVal < 0)
LOOP_FOREVER();
CLI_Write((_u8 *)"\r\n\r\n Domain name = ");
CLI_Write(g_domain_name);
/* Get URN */
retVal = get_device_urn(g_device_urn);
if(retVal < 0)
LOOP_FOREVER();
CLI_Write((_u8 *)"\r\n Device URN = ");
CLI_Write(g_device_urn);
CLI_Write((_u8 *)"\r\n");
/* Process the async events from the NWP */
while(1)
{
_SlNonOsMainLoopTask();
}
}
void HttpServer(){
_u8 SecType = 0;
_i32 retVal = -1;
_i32 mode = ROLE_STA;
/*
* Following function configures the device to default state by cleaning
* the persistent settings stored in NVMEM (viz. connection profiles &
* policies, power policy etc)
*
* Applications may choose to skip this step if the developer is sure
* that the device is in its default state at start of application
*
* Note that all profiles and persistent settings that were done on the
* device will be lost
*/
retVal = configureSimpleLinkToDefaultState();
if(retVal < 0)
{
if (DEVICE_NOT_IN_STATION_MODE == retVal)
CLI_Write(" Failed to configure the device in its default state \n\r");
LOOP_FOREVER();
}
CLI_Write(" Device is configured in default state \n\r");
/*
* Assumption is that the device is configured in station mode already
* and it is in its default state
*/
mode = sl_Start(0, 0, 0);
if(mode < 0)
{
LOOP_FOREVER();
}
else
{
if (ROLE_AP == mode)
{
/* If the device is in AP mode, we need to wait for this
* event before doing anything */
while(!IS_IP_ACQUIRED(g_Status)) { _SlNonOsMainLoopTask(); }
}
else
{
/* Configure CC3100 to start in AP mode */
retVal = sl_WlanSetMode(ROLE_AP);
if(retVal < 0)
LOOP_FOREVER();
}
}
/* Configure AP mode without security */
retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID,
pal_Strlen(SSID_AP_MODE), (_u8 *)SSID_AP_MODE);
if(retVal < 0)
LOOP_FOREVER();
SecType = SEC_TYPE_AP_MODE;
/* Configure the Security parameter in the AP mode */
retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1,
(_u8 *)&SecType);
if(retVal < 0)
LOOP_FOREVER();
retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, pal_Strlen(PASSWORD_AP_MODE),
(_u8 *)PASSWORD_AP_MODE);
if(retVal < 0)
LOOP_FOREVER();
/* Restart the CC3100 */
retVal = sl_Stop(SL_STOP_TIMEOUT);
if(retVal < 0)
LOOP_FOREVER();
g_Status = 0;
mode = sl_Start(0, 0, 0);
if (ROLE_AP == mode)
{
/* If the device is in AP mode, we need to wait for this event before doing anything */
while(!IS_IP_ACQUIRED(g_Status)) { _SlNonOsMainLoopTask(); }
}
else
{
CLI_Write(" Device couldn't come in AP mode \n\r");
LOOP_FOREVER();
}
CLI_Write(" \r\n Device is configured in AP mode \n\r");
CLI_Write(" Waiting for client to connect\n\r");
/* wait for client to connect */
while((!IS_IP_LEASED(g_Status)) || (!IS_STA_CONNECTED(g_Status))) { _SlNonOsMainLoopTask(); }
CLI_Write(" Client connected\n\r");
/* Enable the HTTP Authentication */
retVal = set_authentication_check(TRUE);
if(retVal < 0)
LOOP_FOREVER();
/* Get authentication parameters */
retVal = get_auth_name(g_auth_name);
if(retVal < 0)
LOOP_FOREVER();
retVal = get_auth_password(g_auth_password);
if(retVal < 0)
LOOP_FOREVER();
retVal = get_auth_realm(g_auth_realm);
if(retVal < 0)
LOOP_FOREVER();
CLI_Write((_u8 *)"\r\n Authentication parameters: ");
CLI_Write((_u8 *)"\r\n Name = ");
CLI_Write(g_auth_name);
CLI_Write((_u8 *)"\r\n Password = ");
CLI_Write(g_auth_password);
CLI_Write((_u8 *)"\r\n Realm = ");
CLI_Write(g_auth_realm);
/* Get the domain name */
retVal = get_domain_name(g_domain_name);
if(retVal < 0)
LOOP_FOREVER();
CLI_Write((_u8 *)"\r\n\r\n Domain name = ");
CLI_Write(g_domain_name);
/* Get URN */
retVal = get_device_urn(g_device_urn);
if(retVal < 0)
LOOP_FOREVER();
CLI_Write((_u8 *)"\r\n Device URN = ");
CLI_Write(g_device_urn);
CLI_Write((_u8 *)"\r\n");
/* Process the async events from the NWP */
while(1)
{
Semaphore_pend(Semaphore_CC3100_Req, BIOS_WAIT_FOREVER);
_SlNonOsMainLoopTask();
}
}
/*!
\brief Set the HTTP port
This function can be used to change the default port (80) for HTTP request
\param[in] num- contains the port number to be set
\return None
\note
\warning
*/
_i32 set_port_number(_u16 num)
{
_NetAppHttpServerGetSet_port_num_t port_num;
_i32 status = -1;
port_num.port_number = num;
/*Need to restart the server in order for the new port number configuration
*to take place */
status = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID);
ASSERT_ON_ERROR(status);
status = sl_NetAppSet (SL_NET_APP_HTTP_SERVER_ID, NETAPP_SET_GET_HTTP_OPT_PORT_NUMBER,
sizeof(_NetAppHttpServerGetSet_port_num_t), (_u8 *)&port_num);
ASSERT_ON_ERROR(status);