Repository navigation
Expand file tree
/
Copy pathnetwork_if.c
More file actions
852 lines (760 loc) · 28.6 KB
/
Copy pathnetwork_if.c
File metadata and controls
852 lines (760 loc) · 28.6 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
/*
* Copyright (c) 2016, Texas Instruments Incorporated
* All rights reserved.
*
* 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.
*/
//*****************************************************************************
// network_if.c
//
// Networking interface functions for CC32xx device
//
//*****************************************************************************
//*****************************************************************************
//
//! \addtogroup common_interface
//! @{
//
//*****************************************************************************
/* Standard includes */
#include <string.h>
#include <stdlib.h>
/* Kernel (Non OS/Free-RTOS/TI-RTOS) includes */
#include <pthread.h>
#include <mqueue.h>
#include <unistd.h>
/* Simplelink includes */
#include <ti/drivers/net/wifi/simplelink.h>
/* Common interface includes */
#include "network_if.h"
#include "uart_term.h"
//*****************************************************************************
// LOCAL DEFINES
//*****************************************************************************
/* Network App specific status/error codes which are */
/* used only in this file. */
typedef enum
{
/* Choosing this number to avoid overlap with host-driver's error codes. */
DEVICE_NOT_IN_STATION_MODE = -0x7F0,
DEVICE_NOT_IN_AP_MODE = DEVICE_NOT_IN_STATION_MODE - 1,
DEVICE_NOT_IN_P2P_MODE = DEVICE_NOT_IN_AP_MODE - 1,
STATUS_CODE_MAX = -0xBB8
} e_NetAppStatusCodes;
//*****************************************************************************
// GLOBAL VARIABLES
//*****************************************************************************
/* Station IP address */
unsigned long g_ulStaIp = 0;
/* Network Gateway IP address */
unsigned long g_ulGatewayIP = 0;
/* Connection SSID */
unsigned char g_ucConnectionSSID[SL_WLAN_SSID_MAX_LENGTH + 1];
/* Connection BSSID */
unsigned char g_ucConnectionBSSID[SL_WLAN_BSSID_LENGTH ];
/* SimpleLink Status */
volatile unsigned long g_ulStatus = 0;
/* Connection time delay index */
volatile unsigned short g_usConnectIndex;
//*****************************************************************************
// SimpleLink Asynchronous Event Handlers -- Start
//*****************************************************************************
void SimpleLinkHttpServerEventHandler(
SlNetAppHttpServerEvent_t *pSlHttpServerEvent,
SlNetAppHttpServerResponse_t *
pSlHttpServerResponse)
{
}
void SimpleLinkNetAppRequestEventHandler(SlNetAppRequest_t *pNetAppRequest,
SlNetAppResponse_t *pNetAppResponse)
{
}
void SimpleLinkNetAppRequestMemFreeEventHandler(uint8_t *buffer)
{
}
//*****************************************************************************
//!
//! On Successful completion of Wlan Connect, This function triggers connection
//! status to be set.
//!
//! \param[in] pSlWlanEvent - pointer indicating Event type
//!
//! \return None
//!
//*****************************************************************************
void SimpleLinkWlanEventHandler(SlWlanEvent_t *pSlWlanEvent)
{
SlWlanEventDisconnect_t* pEventData = NULL;
switch(pSlWlanEvent->Id)
{
case SL_WLAN_EVENT_CONNECT:
SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
memcpy(g_ucConnectionSSID, pSlWlanEvent->Data.Connect.SsidName,
pSlWlanEvent->Data.Connect.SsidLen);
memcpy(g_ucConnectionBSSID, pSlWlanEvent->Data.Connect.Bssid,
SL_WLAN_BSSID_LENGTH);
UART_PRINT(
"[WLAN EVENT] STA Connected to the AP: %s , BSSID: "
"%x:%x:%x:%x:%x:%x\n\r", g_ucConnectionSSID,
g_ucConnectionBSSID[0], g_ucConnectionBSSID[1],
g_ucConnectionBSSID[2],
g_ucConnectionBSSID[3], g_ucConnectionBSSID[4],
g_ucConnectionBSSID[5]);
break;
case SL_WLAN_EVENT_DISCONNECT:
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_ACQUIRED);
pEventData = &pSlWlanEvent->Data.Disconnect;
/* If the user has initiated 'Disconnect' request, 'reason_code' */
/* is SL_WLAN_DISCONNECT_USER_INITIATED */
if(SL_WLAN_DISCONNECT_USER_INITIATED == pEventData->ReasonCode)
{
UART_PRINT("Device disconnected from the AP on application's "
"request \n\r");
}
else
{
UART_PRINT("Device disconnected from the AP on an ERROR..!! \n\r");
}
break;
case SL_WLAN_EVENT_STA_ADDED:
/* when device is in AP mode and any client connects to it. */
SET_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
break;
case SL_WLAN_EVENT_STA_REMOVED:
/* when device is in AP mode and any client disconnects from it. */
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_LEASED);
break;
default:
UART_PRINT("[WLAN EVENT] Unexpected event %d\n\r", pSlWlanEvent->Id);
break;
}
}
//*****************************************************************************
//
//! The Function Handles the Fatal errors
//!
//! \param[in] slFatalErrorEvent - Pointer to Fatal Error Event info
//!
//! \return None
//!
//*****************************************************************************
void SimpleLinkFatalErrorEventHandler(SlDeviceFatal_t *slFatalErrorEvent)
{
switch(slFatalErrorEvent->Id)
{
case SL_DEVICE_EVENT_FATAL_DEVICE_ABORT:
UART_PRINT(
"[ERROR] - FATAL ERROR: Abort NWP event detected: "
"AbortType=%d, AbortData=0x%x\n\r",
slFatalErrorEvent->Data.DeviceAssert.Code,
slFatalErrorEvent->Data.DeviceAssert.Value);
break;
case SL_DEVICE_EVENT_FATAL_DRIVER_ABORT:
UART_PRINT("[ERROR] - FATAL ERROR: Driver Abort detected. \n\r");
break;
case SL_DEVICE_EVENT_FATAL_NO_CMD_ACK:
UART_PRINT(
"[ERROR] - FATAL ERROR: No Cmd Ack detected "
"[cmd opcode = 0x%x] \n\r",
slFatalErrorEvent->Data.NoCmdAck.Code);
break;
case SL_DEVICE_EVENT_FATAL_SYNC_LOSS:
UART_PRINT("[ERROR] - FATAL ERROR: Sync loss detected n\r");
break;
case SL_DEVICE_EVENT_FATAL_CMD_TIMEOUT:
UART_PRINT(
"[ERROR] - FATAL ERROR: Async event timeout detected "
"[event opcode =0x%x] \n\r",
slFatalErrorEvent->Data.CmdTimeout.Code);
break;
default:
UART_PRINT("[ERROR] - FATAL ERROR: Unspecified error detected \n\r");
break;
}
}
//*****************************************************************************
//
//! This function handles network events such as IP acquisition, IP leased, IP
//! released etc.
//!
//! \param[in] pNetAppEvent - Pointer to NetApp Event Info
//!
//! \return None
//!
//*****************************************************************************
void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent)
{
switch(pNetAppEvent->Id)
{
case SL_NETAPP_EVENT_IPV4_ACQUIRED:
case SL_NETAPP_EVENT_IPV6_ACQUIRED:
SET_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_ACQUIRED);
UART_PRINT("[NETAPP EVENT] IP acquired by the device\n\r");
break;
case SL_NETAPP_EVENT_DHCPV4_LEASED:
SET_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_LEASED);
g_ulStaIp = (pNetAppEvent)->Data.IpLeased.IpAddress;
UART_PRINT("[NETAPP EVENT] IP Leased to Client: IP=%d.%d.%d.%d , ",
SL_IPV4_BYTE(g_ulStaIp,
3),
SL_IPV4_BYTE(g_ulStaIp,
2),
SL_IPV4_BYTE(g_ulStaIp, 1), SL_IPV4_BYTE(g_ulStaIp, 0));
break;
case SL_NETAPP_EVENT_DHCPV4_RELEASED:
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_LEASED);
UART_PRINT("[NETAPP EVENT] IP Released for Client: "
"IP=%d.%d.%d.%d , ", SL_IPV4_BYTE(g_ulStaIp,
3),
SL_IPV4_BYTE(g_ulStaIp,
2),
SL_IPV4_BYTE(g_ulStaIp, 1), SL_IPV4_BYTE(g_ulStaIp, 0));
break;
default:
UART_PRINT("[NETAPP EVENT] Unexpected event [0x%x] \n\r",
pNetAppEvent->Id);
break;
}
}
//*****************************************************************************
//
//! This function handles resource request
//!
//! \param[in] pNetAppRequest - Contains the resource requests
//! \param[in] pNetAppResponse - Should be filled by the user with the
//! relevant response information
//!
//! \return None
//!
//*****************************************************************************
void SimpleLinkNetAppRequestHandler(SlNetAppRequest_t *pNetAppRequest,
SlNetAppResponse_t *pNetAppResponse)
{
/* Unused in this application */
}
//*****************************************************************************
//
//! This function handles HTTP server events
//!
//! \param[in] pServerEvent - Contains the relevant event information
//! \param[in] pServerResponse - Should be filled by the user with the
//! relevant response information
//!
//! \return None
//!
//****************************************************************************
void SimpleLinkHttpServerCallback(SlNetAppHttpServerEvent_t *pHttpEvent,
SlNetAppHttpServerResponse_t *pHttpResponse)
{
/* Unused in this application */
}
//*****************************************************************************
//
//! This function handles General Events
//!
//! \param[in] pDevEvent - Pointer to General Event Info
//!
//! \return None
//!
//*****************************************************************************
void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent)
{
/* Most of the general errors are not FATAL. are to be handled */
/* appropriately by the application. */
UART_PRINT("[GENERAL EVENT] - ID=[%d] Sender=[%d]\n\n",
pDevEvent->Data.Error.Code,
pDevEvent->Data.Error.Source);
}
//*****************************************************************************
//
//! This function handles socket events indication
//!
//! \param[in] pSock - Pointer to Socket Event Info
//!
//! \return None
//!
//*****************************************************************************
void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
{
/* This application doesn't work w/ socket - Events are not expected */
switch(pSock->Event)
{
case SL_SOCKET_TX_FAILED_EVENT:
switch(pSock->SocketAsyncEvent.SockTxFailData.Status)
{
case SL_ERROR_BSD_ECLOSE:
UART_PRINT(
"[SOCK ERROR] - close socket (%d) operation "
"failed to transmit all queued packets\n\r",
pSock->SocketAsyncEvent.SockTxFailData.Sd);
break;
default:
UART_PRINT(
"[SOCK ERROR] - TX FAILED : socket %d , "
"reason (%d) \n\n",
pSock->SocketAsyncEvent.SockTxFailData.Sd,
pSock->SocketAsyncEvent.SockTxFailData.Status);
break;
}
break;
case SL_SOCKET_ASYNC_EVENT:
{
UART_PRINT("[SOCK ERROR] an event received on socket %d\r\n",
pSock->SocketAsyncEvent.SockAsyncData.Sd);
switch(pSock->SocketAsyncEvent.SockAsyncData.Type)
{
case SL_SSL_NOTIFICATION_CONNECTED_SECURED:
UART_PRINT("[SOCK ERROR] SSL handshake done");
break;
case SL_SSL_NOTIFICATION_HANDSHAKE_FAILED:
UART_PRINT("[SOCK ERROR] SSL handshake failed with error %d\r\n",
pSock->SocketAsyncEvent.SockAsyncData.Val);
break;
case SL_SSL_ACCEPT:
UART_PRINT(
"[SOCK ERROR] Recoverable error occurred "
"during the handshake %d\r\n",
pSock->SocketAsyncEvent.SockAsyncData.Val);
break;
case SL_OTHER_SIDE_CLOSE_SSL_DATA_NOT_ENCRYPTED:
UART_PRINT("[SOCK ERROR] Other peer terminated the SSL layer.\r\n");
break;
case SL_SSL_NOTIFICATION_WRONG_ROOT_CA:
UART_PRINT("[SOCK ERROR] Used wrong CA to verify the peer.\r\n");
break;
default:
break;
}
break;
}
default:
UART_PRINT("[SOCK EVENT] - Unexpected Event [%x0x]\n\n", pSock->Event);
break;
}
}
//*****************************************************************************
// SimpleLink Asynchronous Event Handlers -- End
//*****************************************************************************
//*****************************************************************************
//
//! This function initializes the application variables
//!
//! \param None
//!
//! \return 0 on success, negative error-code on error
//
//*****************************************************************************
void InitializeAppVariables(void)
{
g_ulStatus = 0;
g_ulStaIp = 0;
g_ulGatewayIP = 0;
memset(g_ucConnectionSSID, 0, sizeof(g_ucConnectionSSID));
memset(g_ucConnectionBSSID, 0, sizeof(g_ucConnectionBSSID));
}
//*****************************************************************************
//
//! The function initializes a CC32xx device and triggers it to start operation
//!
//! \param[in] uiMode - device mode in which device will be configured
//!
//! \return 0 : success, -ve : failure
//
//*****************************************************************************
long Network_IF_InitDriver(uint32_t uiMode)
{
long lRetVal = -1;
/* Reset CC32xx Network State Machine */
InitializeAppVariables();
/* Following function configure 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. */
lRetVal = sl_Start(NULL, NULL, NULL);
if(lRetVal < 0)
{
UART_PRINT("Failed to start the device \n\r");
LOOP_FOREVER();
}
switch(lRetVal)
{
case ROLE_STA:
UART_PRINT("Device came up in Station mode\n\r");
break;
case ROLE_AP:
UART_PRINT("Device came up in Access-Point mode\n\r");
break;
case ROLE_P2P:
UART_PRINT("Device came up in P2P mode\n\r");
break;
default:
UART_PRINT("Error:unknown mode\n\r");
break;
}
if(uiMode != lRetVal)
{
UART_PRINT("Switching Networking mode on application request\n\r");
/* Switch to AP role and restart */
lRetVal = sl_WlanSetMode(uiMode);
ASSERT_ON_ERROR(lRetVal);
lRetVal = sl_Stop(SL_STOP_TIMEOUT);
lRetVal = sl_Start(0, 0, 0);
ASSERT_ON_ERROR(lRetVal);
if(lRetVal == uiMode)
{
switch(lRetVal)
{
case ROLE_STA:
UART_PRINT("Device came up in Station mode\n\r");
break;
case ROLE_AP:
UART_PRINT("Device came up in Access-Point mode\n\r");
/* If the device is in AP mode, we need to wait for this */
/* event before doing anything. */
while(!IS_IP_ACQUIRED(g_ulStatus))
{
usleep(1000);
}
break;
case ROLE_P2P:
UART_PRINT("Device came up in P2P mode\n\r");
break;
default:
UART_PRINT("Error:unknown mode\n\r");
break;
}
}
else
{
UART_PRINT("could not configure correct networking mode\n\r");
LOOP_FOREVER();
}
}
else
{
if(lRetVal == ROLE_AP)
{
while(!IS_IP_ACQUIRED(g_ulStatus))
{
usleep(1000);
}
}
}
return(0);
}
//*****************************************************************************
//
//! The function de-initializes a CC32xx device
//!
//! \param None
//!
//! \return On success, zero is returned. On error, other
//
//*****************************************************************************
long Network_IF_DeInitDriver(void)
{
long lRetVal = -1;
UART_PRINT("SL Disconnect...\n\r");
/* Disconnect from the AP */
lRetVal = Network_IF_DisconnectFromAP();
/* Stop the simplelink host */
sl_Stop(SL_STOP_TIMEOUT);
/* Reset the state to uninitialized */
Network_IF_ResetMCUStateMachine();
return(lRetVal);
}
//*****************************************************************************
//
//! Connect to an Access Point using the specified SSID
//!
//! \param[in] pcSsid - is a string of the AP's SSID
//! \param[in] SecurityParams - is Security parameter for AP
//!
//! \return On success, zero is returned. On error, -ve value is returned
//
//*****************************************************************************
long Network_IF_ConnectAP(char *pcSsid,
SlWlanSecParams_t SecurityParams)
{
char acCmdStore[128];
unsigned short usConnTimeout;
unsigned char ucRecvdAPDetails;
long lRetVal;
unsigned long ulIP = 0;
unsigned long ulSubMask = 0;
unsigned long ulDefGateway = 0;
unsigned long ulDns = 0;
g_usConnectIndex = 0;
/* Disconnect from the AP */
Network_IF_DisconnectFromAP();
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_ACQUIRED);
/* Continue only if SSID is not empty */
if(pcSsid != NULL)
{
/* This triggers the CC32xx to connect to a specific AP. */
lRetVal =
sl_WlanConnect((signed char *) pcSsid, strlen((const char *) pcSsid),
NULL, &SecurityParams, NULL);
ASSERT_ON_ERROR(lRetVal);
/* Wait for ~10 sec to check if connection to desire AP succeeds */
while(g_usConnectIndex < 10)
{
sleep(1);
if(IS_CONNECTED(g_ulStatus) && IS_IP_ACQUIRED(g_ulStatus))
{
break;
}
g_usConnectIndex++;
}
}
else
{
UART_PRINT("Empty SSID, Could not connect\n\r");
return(-1);
}
/* Check and loop until AP connection successful, else ask new AP SSID */
while(!(IS_CONNECTED(g_ulStatus)) || !(IS_IP_ACQUIRED(g_ulStatus)))
{
/* Disconnect the previous attempt */
Network_IF_DisconnectFromAP();
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_ACQUIRED);
UART_PRINT("Device could not connect to %s\n\r", pcSsid);
do
{
ucRecvdAPDetails = 0;
UART_PRINT("\n\r\n\rPlease enter the AP(open) SSID name # ");
/* Get the AP name to connect over the UART */
lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore));
if(lRetVal > 0)
{
/* remove start/end spaces if any */
lRetVal = TrimSpace(acCmdStore);
if(lRetVal)
{
/* Parse the AP name */
strncpy(pcSsid, acCmdStore, lRetVal);
if(pcSsid != NULL)
{
ucRecvdAPDetails = 1;
pcSsid[lRetVal] = '\0';
}
}
}
}
while(ucRecvdAPDetails == 0);
/* Set Security Parameters to OPEN security type. */
SecurityParams.Key = (signed char *) "";
SecurityParams.KeyLen = 0;
SecurityParams.Type = SL_WLAN_SEC_TYPE_OPEN;
UART_PRINT("\n\rTrying to connect to AP: %s ...\n\r", pcSsid);
/* Get the current timer tick and setup the timeout accordingly. */
usConnTimeout = g_usConnectIndex + 15;
/* This triggers the CC32xx to connect to specific AP. */
lRetVal =
sl_WlanConnect((signed char *) pcSsid, strlen((const char *) pcSsid),
NULL, &SecurityParams,NULL);
ASSERT_ON_ERROR(lRetVal);
/* Wait ~10 sec to check if connection to specified AP succeeds */
while(!(IS_CONNECTED(g_ulStatus)) || !(IS_IP_ACQUIRED(g_ulStatus)))
{
sleep(1);
if(g_usConnectIndex >= usConnTimeout)
{
break;
}
g_usConnectIndex++;
}
}
/* Put message on UART */
UART_PRINT("\n\rDevice has connected to %s\n\r", pcSsid);
/* Get IP address */
lRetVal = Network_IF_IpConfigGet(&ulIP, &ulSubMask, &ulDefGateway, &ulDns);
ASSERT_ON_ERROR(lRetVal);
/* Send the information */
UART_PRINT("Device IP Address is %d.%d.%d.%d \n\r\n\r",
SL_IPV4_BYTE(ulIP, 3), SL_IPV4_BYTE(ulIP, 2), SL_IPV4_BYTE(ulIP,
1),
SL_IPV4_BYTE(ulIP, 0));
return(0);
}
//*****************************************************************************
//
//! Disconnects from an Access Point
//!
//! \param none
//!
//! \return 0 disconnected done, other already disconnected
//
//*****************************************************************************
long Network_IF_DisconnectFromAP(void)
{
long lRetVal = 0;
if(IS_CONNECTED(g_ulStatus))
{
lRetVal = sl_WlanDisconnect();
if(0 == lRetVal)
{
while(IS_CONNECTED(g_ulStatus))
{
usleep(1000);
}
return(lRetVal);
}
else
{
return(lRetVal);
}
}
else
{
return(lRetVal);
}
}
//*****************************************************************************
//
//! Get the IP Address of the device.
//!
//! \param[in] pulIP - IP Address of Device
//! \param[in] pulSubnetMask - Subnetmask of Device
//! \param[in] pulDefaultGateway - Default Gateway value
//! \param[in] pulDNSServer - DNS Server
//!
//! \return On success, zero is returned. On error, -1 is returned
//
//*****************************************************************************
long Network_IF_IpConfigGet(unsigned long *pulIP,
unsigned long *pulSubnetMask,
unsigned long *pulDefaultGateway,
unsigned long *pulDNSServer)
{
unsigned short usDHCP = 0;
long lRetVal = -1;
unsigned short len = sizeof(SlNetCfgIpV4Args_t);
SlNetCfgIpV4Args_t ipV4 = { 0 };
/* get network configuration */
lRetVal =
sl_NetCfgGet(SL_NETCFG_IPV4_STA_ADDR_MODE, &usDHCP, &len,
(unsigned char *) &ipV4);
ASSERT_ON_ERROR(lRetVal);
*pulIP = ipV4.Ip;
*pulSubnetMask = ipV4.IpMask;
*pulDefaultGateway = ipV4.IpGateway;
*pulDefaultGateway = ipV4.IpDnsServer;
return(lRetVal);
}
//*****************************************************************************
//
//! This function obtains the server IP address using a DNS lookup
//!
//! \param[in] pcHostName The server hostname
//! \param[out] pDestinationIP This parameter is filled with host IP address
//!
//! \return On success, +ve value is returned. On error, -ve value is returned
//
//*****************************************************************************
long Network_IF_GetHostIP(char* pcHostName,
unsigned long * pDestinationIP)
{
long lStatus = 0;
lStatus =
sl_NetAppDnsGetHostByName((signed char *) pcHostName, strlen(
pcHostName), pDestinationIP, SL_AF_INET);
ASSERT_ON_ERROR(lStatus);
UART_PRINT("Get Host IP succeeded.\n\rHost: %s IP: %d.%d.%d.%d \n\r\n\r",
pcHostName, SL_IPV4_BYTE(*pDestinationIP,
3), SL_IPV4_BYTE(*pDestinationIP,
2),
SL_IPV4_BYTE(*pDestinationIP, 1),
SL_IPV4_BYTE(*pDestinationIP, 0));
return(lStatus);
}
//*****************************************************************************
//
//! Reset state from the state machine
//!
//! \param None
//!
//! \return none
//!
//*****************************************************************************
void Network_IF_ResetMCUStateMachine()
{
g_ulStatus = 0;
}
//*****************************************************************************
//
//! Return the current state bits
//!
//! \param None
//!
//! \return none
//!
//
//*****************************************************************************
unsigned long Network_IF_CurrentMCUState()
{
return(g_ulStatus);
}
//*****************************************************************************
//
//! Sets a state from the state machine
//!
//! \param[in] cStat - Status of State Machine defined in e_StatusBits
//!
//! \return none
//!
//*****************************************************************************
void Network_IF_SetMCUMachineState(char cStat)
{
SET_STATUS_BIT(g_ulStatus, cStat);
}
//*****************************************************************************
//
//! Unsets a state from the state machine
//!
//! \param[in] cStat - Status of State Machine defined in e_StatusBits
//!
//! \return none
//!
//*****************************************************************************
void Network_IF_UnsetMCUMachineState(char cStat)
{
CLR_STATUS_BIT(g_ulStatus, cStat);
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************