1 /*
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 
34 /*****************************************************************************/
35 /* Include files                                                             */
36 /*****************************************************************************/
37 
38 #include <ti/drivers/net/wifi/slnetifwifi.h>
39 
40 /*****************************************************************************/
41 /* Macro declarations                                                        */
42 /*****************************************************************************/
43 
44 /* Macro which split the 8bit security flags from the input flags            */
45 #define SPLIT_SEC_AND_INPUT_FLAGS(inputFlags, secFlags) (secFlags = inputFlags >> 24)
46 
47 /* Disable the 8bit security flags                                           */
48 #define SECURITY_FLAGS_IN_32BIT_REPRESENTATION          (0xFF000000)
49 #define DISABLE_SEC_BITS_FROM_INPUT_FLAGS(inputFlags)   (inputFlags &= ~SECURITY_FLAGS_IN_32BIT_REPRESENTATION)
50 
51 
52 /*****************************************************************************/
53 /* Structure/Enum declarations                                               */
54 /*****************************************************************************/
55 
56 /*****************************************************************************/
57 /* Global declarations                                                       */
58 /*****************************************************************************/
59 
60 /*!
61     SlNetIfConfigWifi structure contains all the function callbacks that are expected to be filled by the relevant network stack interface
62     Each interface has different capabilities, so not all the API's must be supported.
63     Interface that is not supporting a non-mandatory API are set to NULL
64 */
65 SlNetIf_Config_t SlNetIfConfigWifi =
66 {
67     SlNetIfWifi_socket,              // Callback function sockCreate in slnetif module
68     SlNetIfWifi_close,               // Callback function sockClose in slnetif module
69     NULL,                            // Callback function sockShutdown in slnetif module
70     SlNetIfWifi_accept,              // Callback function sockAccept in slnetif module
71     SlNetIfWifi_bind,                // Callback function sockBind in slnetif module
72     SlNetIfWifi_listen,              // Callback function sockListen in slnetif module
73     SlNetIfWifi_connect,             // Callback function sockConnect in slnetif module
74     NULL,                            // Callback function sockGetPeerName in slnetif module
75     NULL,                            // Callback function sockGetLocalName in slnetif module
76     SlNetIfWifi_select,              // Callback function sockSelect in slnetif module
77     SlNetIfWifi_setSockOpt,          // Callback function sockSetOpt in slnetif module
78     SlNetIfWifi_getSockOpt,          // Callback function sockGetOpt in slnetif module
79     SlNetIfWifi_recv,                // Callback function sockRecv in slnetif module
80     SlNetIfWifi_recvFrom,            // Callback function sockRecvFrom in slnetif module
81     SlNetIfWifi_send,                // Callback function sockSend in slnetif module
82     SlNetIfWifi_sendTo,              // Callback function sockSendTo in slnetif module
83     SlNetIfWifi_sockstartSec,        // Callback function sockstartSec in slnetif module
84     SlNetIfWifi_getHostByName,       // Callback function utilGetHostByName in slnetif module
85     SlNetIfWifi_getIPAddr,           // Callback function ifGetIPAddr in slnetif module
86     SlNetIfWifi_getConnectionStatus, // Callback function ifGetConnectionStatus in slnetif module
87     SlNetIfWifi_loadSecObj,          // Callback function ifLoadSecObj in slnetif module
88     NULL                             // Callback function ifCreateContext in slnetif module
89 };
90 
91 static const int16_t StartSecOptName[10] =
92 {
93     SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME,
94     SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME,
95     SL_SO_SECURE_FILES_CA_FILE_NAME,
96     SL_SO_SECURE_FILES_PEER_CERT_OR_DH_KEY_FILE_NAME,
97     SL_SO_SECMETHOD,
98     SL_SO_SECURE_MASK,
99     SL_SO_SECURE_ALPN,
100     SL_SO_SECURE_EXT_CLIENT_CHLNG_RESP,
101     SL_SO_SECURE_DOMAIN_NAME_VERIFICATION,
102     SL_SO_SECURE_DISABLE_CERTIFICATE_STORE
103 };
104 
105 static const int16_t socketType[8] =
106 {
107     SL_SOCK_STREAM,
108     SL_SOCK_DGRAM,
109     SL_SOCK_RAW,
110     SLNETSOCK_SOCK_RX_MTR,
111     SL_SOCK_DGRAM,
112     SL_SOCK_RAW,
113     SLNETSOCK_SOCK_BRIDGE,
114     SLNETSOCK_SOCK_ROUTER,
115 };
116 
117 /*****************************************************************************/
118 /* Function prototypes                                                       */
119 /*****************************************************************************/
120 
121 //*****************************************************************************
122 //
123 // SlNetIfWifi_socket - Create an endpoint for communication
124 //
125 //*****************************************************************************
SlNetIfWifi_socket(void * ifContext,int16_t Domain,int16_t Type,int16_t Protocol,void ** sdContext)126 int16_t SlNetIfWifi_socket(void *ifContext, int16_t Domain, int16_t Type, int16_t Protocol, void **sdContext)
127 {
128     /* Create socket and return the return value of the function             */
129     int16_t mappedSocketType = socketType[Type - 1];
130     return (sl_Socket(Domain, mappedSocketType, Protocol));
131 }
132 
133 
134 //*****************************************************************************
135 //
136 // SlNetIfWifi_close - Gracefully close socket
137 //
138 //*****************************************************************************
SlNetIfWifi_close(int16_t sd,void * sdContext)139 int32_t SlNetIfWifi_close(int16_t sd, void *sdContext)
140 {
141     /* Close socket and return the return value of the function                */
142     return sl_Close(sd);
143 }
144 
145 
146 //*****************************************************************************
147 //
148 // SlNetIfWifi_accept - Accept a connection on a socket
149 //
150 //*****************************************************************************
SlNetIfWifi_accept(int16_t sd,void * sdContext,SlNetSock_Addr_t * addr,SlNetSocklen_t * addrlen,uint8_t flags,void ** acceptedSdContext)151 int16_t SlNetIfWifi_accept(int16_t sd, void *sdContext, SlNetSock_Addr_t *addr, SlNetSocklen_t *addrlen, uint8_t flags, void **acceptedSdContext)
152 {
153     return sl_Accept(sd, (SlSockAddr_t *)addr, addrlen);
154 }
155 
156 
157 //*****************************************************************************
158 //
159 // SlNetIfWifi_bind - Assign a name to a socket
160 //
161 //*****************************************************************************
SlNetIfWifi_bind(int16_t sd,void * sdContext,const SlNetSock_Addr_t * addr,int16_t addrlen)162 int32_t SlNetIfWifi_bind(int16_t sd, void *sdContext, const SlNetSock_Addr_t *addr, int16_t addrlen)
163 {
164     return sl_Bind(sd, (const SlSockAddr_t *)addr, addrlen);
165 }
166 
167 
168 //*****************************************************************************
169 //
170 // SlNetIfWifi_listen - Listen for connections on a socket
171 //
172 //*****************************************************************************
SlNetIfWifi_listen(int16_t sd,void * sdContext,int16_t backlog)173 int32_t SlNetIfWifi_listen(int16_t sd, void *sdContext, int16_t backlog)
174 {
175     return sl_Listen(sd, backlog);
176 }
177 
178 
179 //*****************************************************************************
180 //
181 // SlNetIfWifi_connect - Initiate a connection on a socket
182 //
183 //*****************************************************************************
SlNetIfWifi_connect(int16_t sd,void * sdContext,const SlNetSock_Addr_t * addr,SlNetSocklen_t addrlen,uint8_t flags)184 int32_t SlNetIfWifi_connect(int16_t sd, void *sdContext, const SlNetSock_Addr_t *addr, SlNetSocklen_t addrlen, uint8_t flags)
185 {
186     return sl_Connect(sd, (const SlSockAddr_t *)addr, addrlen);
187 }
188 
189 
190 //*****************************************************************************
191 //
192 // SlNetIfWifi_getSockName - Returns the local address info of the socket
193 //                         descriptor
194 //
195 //*****************************************************************************
SlNetIfWifi_getSockName(int16_t sd,void * sdContext,SlNetSock_Addr_t * addr,SlNetSocklen_t * addrlen)196 int32_t SlNetIfWifi_getSockName(int16_t sd, void *sdContext, SlNetSock_Addr_t *addr, SlNetSocklen_t *addrlen)
197 {
198 // Not implemented in NWP
199     return SLNETERR_INVALPARAM;
200 }
201 
202 
203 //*****************************************************************************
204 //
205 // SlNetIfWifi_select - Monitor socket activity
206 //
207 //*****************************************************************************
SlNetIfWifi_select(void * ifContext,int16_t nfds,SlNetSock_SdSet_t * readsds,SlNetSock_SdSet_t * writesds,SlNetSock_SdSet_t * exceptsds,SlNetSock_Timeval_t * timeout)208 int32_t SlNetIfWifi_select(void *ifContext, int16_t nfds, SlNetSock_SdSet_t *readsds, SlNetSock_SdSet_t *writesds, SlNetSock_SdSet_t *exceptsds, SlNetSock_Timeval_t *timeout)
209 {
210     SlNetSock_Timeval_t *slNetSockTimeVal;
211     SlTimeval_t tv;
212     /* Translate from SlNetSock_Timeval_t into SlTimeval_t */
213     slNetSockTimeVal = (SlNetSock_Timeval_t *)timeout;
214     tv.tv_sec = slNetSockTimeVal->tv_sec;
215     tv.tv_usec = slNetSockTimeVal->tv_usec;
216     return sl_Select(nfds, (SlFdSet_t *)readsds, (SlFdSet_t *)writesds, (SlFdSet_t *)exceptsds, &tv);
217 }
218 
219 
220 //*****************************************************************************
221 //
222 // SlNetIfWifi_setSockOpt - Set socket options
223 //
224 //*****************************************************************************
SlNetIfWifi_setSockOpt(int16_t sd,void * sdContext,int16_t level,int16_t optname,void * optval,SlNetSocklen_t optlen)225 int32_t SlNetIfWifi_setSockOpt(int16_t sd, void *sdContext, int16_t level, int16_t optname, void *optval, SlNetSocklen_t optlen)
226 {
227     SlNetSock_Timeval_t *slNetSockTimeVal;
228     SlTimeval_t tv;
229 
230     switch (level)
231     {
232         case SLNETSOCK_LVL_SOCKET:
233         {
234             switch (optname)
235             {
236                 case SLNETSOCK_OPSOCK_RCV_TIMEO:
237                 {
238                     /* Translate from SlNetSock_Timeval_t into SlTimeval_t */
239                     slNetSockTimeVal = (SlNetSock_Timeval_t *)optval;
240                     tv.tv_sec = slNetSockTimeVal->tv_sec;
241                     tv.tv_usec = slNetSockTimeVal->tv_usec;
242                     optval = (void *)&tv;
243                     optlen = sizeof(SlTimeval_t);
244                     break;
245                 }
246                 default:
247                     /* Pass values into sl_SetSockOpt directly */
248                     break;
249             }
250             break;
251         }
252         default:
253             /* Pass values into sl_SetSockOpt directly */
254             break;
255     }
256 
257     return sl_SetSockOpt(sd, level, optname, optval, optlen);
258 }
259 
260 
261 //*****************************************************************************
262 //
263 // SlNetIfWifi_getSockOpt - Get socket options
264 //
265 //*****************************************************************************
SlNetIfWifi_getSockOpt(int16_t sd,void * sdContext,int16_t level,int16_t optname,void * optval,SlNetSocklen_t * optlen)266 int32_t SlNetIfWifi_getSockOpt(int16_t sd, void *sdContext, int16_t level, int16_t optname, void *optval, SlNetSocklen_t *optlen)
267 {
268     SlSocklen_t len;
269     int32_t status = 0;
270     SlNetSock_Timeval_t *slNetSockTimeVal;
271     SlTimeval_t tv;
272 
273     switch (level)
274     {
275         case SLNETSOCK_LVL_SOCKET:
276         {
277             switch (optname)
278             {
279                 case SLNETSOCK_OPSOCK_RCV_TIMEO:
280                 {
281                     if (*optlen < sizeof(SlNetSock_Timeval_t))
282                     {
283                         return (SLNETERR_RET_CODE_INVALID_INPUT);
284                     }
285                     len = sizeof(SlTimeval_t);
286                     status =
287                         sl_GetSockOpt(sd, level, optname, (void *)&tv, &len);
288 
289                     slNetSockTimeVal = (SlNetSock_Timeval_t *)optval;
290                     slNetSockTimeVal->tv_sec = tv.tv_sec;
291                     slNetSockTimeVal->tv_usec = tv.tv_usec;
292                     *optlen = sizeof(SlNetSock_Timeval_t);
293                     break;
294                 }
295 
296                 default:
297                 {
298                     /* Pass values into sl_SetSockOpt directly */
299                     status = sl_GetSockOpt(sd, level, optname, optval, optlen);
300                     break;
301                 }
302 
303             }
304             break;
305         }
306         default:
307         {
308             /* Pass values into sl_SetSockOpt directly */
309             status = sl_GetSockOpt(sd, level, optname, optval, optlen);
310             break;
311         }
312     }
313 
314     return (status);
315 }
316 
317 
318 //*****************************************************************************
319 //
320 // SlNetIfWifi_recv - Read data from TCP socket
321 //
322 //*****************************************************************************
SlNetIfWifi_recv(int16_t sd,void * sdContext,void * buf,uint32_t len,uint32_t flags)323 int32_t SlNetIfWifi_recv(int16_t sd, void *sdContext, void *buf, uint32_t len, uint32_t flags)
324 {
325     DISABLE_SEC_BITS_FROM_INPUT_FLAGS(flags);
326     return sl_Recv(sd, buf, len, flags);
327 }
328 
329 
330 //*****************************************************************************
331 //
332 // SlNetIfWifi_recvFrom - Read data from socket
333 //
334 //*****************************************************************************
SlNetIfWifi_recvFrom(int16_t sd,void * sdContext,void * buf,uint32_t len,uint32_t flags,SlNetSock_Addr_t * from,SlNetSocklen_t * fromlen)335 int32_t SlNetIfWifi_recvFrom(int16_t sd, void *sdContext, void *buf, uint32_t len, uint32_t flags, SlNetSock_Addr_t *from, SlNetSocklen_t *fromlen)
336 {
337     DISABLE_SEC_BITS_FROM_INPUT_FLAGS(flags);
338     return sl_RecvFrom(sd, buf, len, flags, (SlSockAddr_t *)from, fromlen);
339 }
340 
341 
342 //*****************************************************************************
343 //
344 // SlNetIfWifi_send - Write data to TCP socket
345 //
346 //*****************************************************************************
SlNetIfWifi_send(int16_t sd,void * sdContext,const void * buf,uint32_t len,uint32_t flags)347 int32_t SlNetIfWifi_send(int16_t sd, void *sdContext, const void *buf, uint32_t len, uint32_t flags)
348 {
349     DISABLE_SEC_BITS_FROM_INPUT_FLAGS(flags);
350     return sl_Send(sd, buf, len, flags);
351 }
352 
353 
354 //*****************************************************************************
355 //
356 // SlNetIfWifi_sendTo - Write data to socket
357 //
358 //*****************************************************************************
SlNetIfWifi_sendTo(int16_t sd,void * sdContext,const void * buf,uint32_t len,uint32_t flags,const SlNetSock_Addr_t * to,SlNetSocklen_t tolen)359 int32_t SlNetIfWifi_sendTo(int16_t sd, void *sdContext, const void *buf, uint32_t len, uint32_t flags, const SlNetSock_Addr_t *to, SlNetSocklen_t tolen)
360 {
361     DISABLE_SEC_BITS_FROM_INPUT_FLAGS(flags);
362     return sl_SendTo(sd, buf, len, flags, (const SlSockAddr_t *)to, tolen);
363 }
364 
365 
366 //*****************************************************************************
367 //
368 // SlNetIfWifi_sockstartSec - Start a security session on an opened socket
369 //
370 //*****************************************************************************
SlNetIfWifi_sockstartSec(int16_t sd,void * sdContext,SlNetSockSecAttrib_t * secAttrib,uint8_t flags)371 int32_t SlNetIfWifi_sockstartSec(int16_t sd, void *sdContext, SlNetSockSecAttrib_t *secAttrib, uint8_t flags)
372 {
373     SlNetSock_SecAttribNode_t *tempSecAttrib = *secAttrib;
374     int32_t                   retVal         = SLNETERR_RET_CODE_OK;
375 
376     if ( 0 != (flags & SLNETSOCK_SEC_BIND_CONTEXT_ONLY)  )
377     {
378         /* run over all attributes and set them                              */
379         while (NULL != tempSecAttrib)
380         {
381             if ( tempSecAttrib->attribName <= SLNETSOCK_SEC_ATTRIB_DISABLE_CERT_STORE)
382             {
383                 retVal = sl_SetSockOpt(sd, SL_SOL_SOCKET, StartSecOptName[tempSecAttrib->attribName], tempSecAttrib->attribBuff, tempSecAttrib->attribBuffLen);
384             }
385             else
386             {
387                 return SLNETERR_RET_CODE_INVALID_INPUT;
388             }
389             tempSecAttrib = tempSecAttrib->next;
390         }
391     }
392 
393     if ( 0 != (flags & SLNETSOCK_SEC_START_SECURITY_SESSION_ONLY)  )
394     {
395         /* Start TLS session                                                 */
396         retVal = sl_StartTLS(sd);
397     }
398 
399     return retVal;
400 }
401 
402 
403 //*****************************************************************************
404 //
405 // SlNetIfWifi_getHostByName - Obtain the IP Address of machine on network, by
406 //                             machine name
407 //
408 //*****************************************************************************
SlNetIfWifi_getHostByName(void * ifContext,char * name,const uint16_t nameLen,uint32_t * ipAddr,uint16_t * ipAddrLen,const uint8_t family)409 int32_t SlNetIfWifi_getHostByName(void *ifContext, char *name, const uint16_t nameLen, uint32_t *ipAddr, uint16_t *ipAddrLen, const uint8_t family)
410 {
411     int32_t  retVal = SLNETERR_RET_CODE_OK;
412 
413     /* sl_NetAppDnsGetHostByName can receive only one ipAddr variable, so
414        only the first slot of the array will be used and the ipAddrLen will
415        be updated to 1 when function is successfully                         */
416     retVal = sl_NetAppDnsGetHostByName((signed char *)name, nameLen, (_u32 *)ipAddr, family);
417 
418     if (retVal == SLNETERR_RET_CODE_OK)
419     {
420         *ipAddrLen = 1;
421     }
422 
423     return retVal;
424 
425 }
426 
427 
428 //*****************************************************************************
429 //
430 // matchModeByRole - Service function used by SlNetIfWifi_getIPAddr for matching SlNetIfAddressType_e to SlNetCfg_e
431 //
432 //*****************************************************************************
matchModeByRole(SlNetIfAddressType_e addrType,SlNetCfg_e * newAddrType,uint16_t * ipAddrLen)433 int32_t matchModeByRole(SlNetIfAddressType_e addrType,
434                         SlNetCfg_e *newAddrType      ,
435                         uint16_t   *ipAddrLen        )
436 {
437     SlWlanConnStatusParam_t WlanConnectInfo;
438     uint16_t Len;
439     int32_t retVal = SLNETERR_RET_CODE_OK; /* prevent build warning */
440 
441     switch(addrType)
442     {
443 
444         case SLNETIF_IPV6_ADDR_LOCAL:
445             *newAddrType = SL_NETCFG_IPV6_ADDR_LOCAL;
446             *ipAddrLen   = sizeof(SlNetCfgIpV6Args_t);
447             retVal = SLNETERR_RET_CODE_OK;
448             break;
449 
450         case SLNETIF_IPV6_ADDR_GLOBAL:
451             *newAddrType = SL_NETCFG_IPV6_ADDR_GLOBAL;
452             *ipAddrLen   = sizeof(SlNetCfgIpV6Args_t);
453             retVal = SLNETERR_RET_CODE_OK;
454             break;
455 
456         /* IPv4 or P2P (GO and CL) */
457         case SLNETIF_IPV4_ADDR:
458             Len = sizeof(SlWlanConnStatusParam_t);
459             *ipAddrLen = sizeof(SlNetCfgIpV4Args_t);
460             retVal = sl_WlanGet(SL_WLAN_CONNECTION_INFO, NULL , &Len, (uint8_t*)&WlanConnectInfo);
461             if(retVal != SLNETERR_RET_CODE_OK)
462             {
463                 return retVal;
464             }
465             if(WlanConnectInfo.ConnStatus == SL_WLAN_CONNECTED_STA || WlanConnectInfo.ConnStatus == SL_WLAN_CONNECTED_P2PCL)
466             {
467                 *newAddrType = SL_NETCFG_IPV4_STA_ADDR_MODE;
468                 retVal = SLNETERR_RET_CODE_OK;
469             }
470             else if(WlanConnectInfo.ConnStatus == SL_WLAN_CONNECTED_P2PGO || WlanConnectInfo.ConnStatus == SL_WLAN_AP_CONNECTED_STATIONS)
471             {
472                 *newAddrType = SL_NETCFG_IPV4_AP_ADDR_MODE;
473                 retVal = SLNETERR_RET_CODE_OK;
474             }
475             else
476             {
477                 retVal = SLNETERR_BSD_ENOTCONN;
478             }
479 
480     }
481     return retVal;
482 }
483 
484 
485 //*****************************************************************************
486 //
487 // SlNetIfWifi_getIPAddr - Get IP Address of specific interface
488 //
489 //*****************************************************************************
SlNetIfWifi_getIPAddr(void * ifContext,SlNetIfAddressType_e addrType,uint16_t * addrConfig,uint32_t * ipAddr)490 int32_t SlNetIfWifi_getIPAddr(void *ifContext, SlNetIfAddressType_e addrType, uint16_t *addrConfig, uint32_t *ipAddr)
491 {
492     int32_t    retVal;
493     uint16_t   ipAddrLen;
494     SlNetCfg_e newAddrType;
495 
496     /* Translate the addrType of SlNetSock type to addrType of SlNetSockIfWifi type */
497     retVal = matchModeByRole(addrType, &newAddrType, &ipAddrLen);
498     if(retVal == SLNETERR_RET_CODE_OK)
499     {
500         retVal = sl_NetCfgGet(newAddrType, addrConfig, &ipAddrLen, (unsigned char *)ipAddr);
501     }
502     return retVal;
503 }
504 
505 
506 //*****************************************************************************
507 //
508 // SlNetIfWifi_getConnectionStatus - Get interface connection status
509 //
510 //*****************************************************************************
SlNetIfWifi_getConnectionStatus(void * ifContext)511 int32_t SlNetIfWifi_getConnectionStatus(void *ifContext)
512 {
513     SlWlanConnStatusParam_t connectionParams;
514     uint16_t                Opt    = 0;
515     int32_t                 retVal = 0;
516     uint16_t                Size   = 0;
517 
518     Size = sizeof(SlWlanConnStatusParam_t);
519     memset(&connectionParams, 0, Size);
520 
521     retVal = sl_WlanGet(SL_WLAN_CONNECTION_INFO, &Opt, &Size, (uint8_t *)&connectionParams);
522 
523     /* Check if the function returned an error                               */
524     if (retVal < SLNETERR_RET_CODE_OK)
525     {
526         /* Return error code                                                 */
527         return retVal;
528     }
529     return connectionParams.ConnStatus;
530 }
531 
532 
533 //*****************************************************************************
534 //
535 // SlNetIfWifi_loadSecObj - Load secured buffer to the network stack
536 //
537 //*****************************************************************************
SlNetIfWifi_loadSecObj(void * ifContext,uint16_t objType,char * objName,int16_t objNameLen,uint8_t * objBuff,int16_t objBuffLen)538 int32_t SlNetIfWifi_loadSecObj(void *ifContext, uint16_t objType, char *objName, int16_t objNameLen, uint8_t *objBuff, int16_t objBuffLen)
539 {
540     int32_t   retVal;    /* negative retVal is an error */
541     uint16_t  i;
542     uint32_t  Offset            = 0;
543     uint32_t  MasterToken       = 0;
544     int32_t   OpenFlags         = 0;
545     int32_t   DeviceFileHandle  = (-1);
546     uint16_t  macAddressLen     = SL_MAC_ADDR_LEN;
547     char      *deviceFileName   = objName;
548     uint8_t   macAddress[SL_MAC_ADDR_LEN];
549 
550     /* Check if the inputs exists                                            */
551     if ((NULL == objName) || (NULL == objBuff))
552     {
553         /* input not valid, return error code                                */
554         return SLNETERR_RET_CODE_INVALID_INPUT;
555     }
556     /* Print device MAC address                                              */
557     retVal = sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen, &macAddress[0]);
558 
559     /* Generating Random MasterPassword but constant per deviceFileName      */
560     for (i = 0; i < strlen(deviceFileName); i++)
561     {
562         MasterToken = ((MasterToken << 8) ^ deviceFileName[i]);
563     }
564 
565     /* Create a file and write data. The file is secured, without
566        signature and with a fail safe commit, with vendor token which is
567        a XOR combination between the MAC address of the device and the
568        object file name                                                      */
569     OpenFlags = SL_FS_CREATE;
570     OpenFlags |= SL_FS_OVERWRITE;
571     OpenFlags |= SL_FS_CREATE_SECURE;
572     OpenFlags |= SL_FS_CREATE_VENDOR_TOKEN;
573     OpenFlags |= SL_FS_CREATE_NOSIGNATURE;
574     OpenFlags |= SL_FS_CREATE_FAILSAFE;
575 
576     /* Create a secure file if not exists and open it for write.             */
577     DeviceFileHandle =  sl_FsOpen((unsigned char *)deviceFileName, OpenFlags | SL_FS_CREATE_MAX_SIZE( objBuffLen ), (unsigned long *)&MasterToken);
578 
579     /* Check if file created successfully                                    */
580     if ( DeviceFileHandle < SLNETERR_RET_CODE_OK )
581     {
582         return DeviceFileHandle;
583     }
584 
585     Offset = 0;
586     /* Write the buffer to the new file                                      */
587     retVal = sl_FsWrite(DeviceFileHandle, Offset, (unsigned char *)objBuff, objBuffLen);
588 
589     /* Close the file                                                        */
590     retVal = sl_FsClose(DeviceFileHandle, NULL, NULL , 0);
591 
592     return retVal;
593 }
594 
595 
596 //*****************************************************************************
597 //
598 // SlNetIfWifi_CreateContext - Allocate and store interface data
599 //
600 //*****************************************************************************
SlNetIfWifi_CreateContext(uint16_t ifID,const char * ifName,void ** context)601 int32_t SlNetIfWifi_CreateContext(uint16_t ifID, const char *ifName, void **context)
602 {
603     return SLNETERR_RET_CODE_OK;
604 }
605