1 /*
2  * wlan.c - CC31xx/CC32xx Host Driver Implementation
3  *
4  * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
5  *
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *
11  *    Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  *    Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the
17  *    distribution.
18  *
19  *    Neither the name of Texas Instruments Incorporated nor the names of
20  *    its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35 */
36 
37 
38 /*****************************************************************************/
39 /* Include files                                                             */
40 /*****************************************************************************/
41 #include <ti/drivers/net/wifi/simplelink.h>
42 
43 
44 extern SlWifiCC32XXConfig_t SimpleLinkWifiCC32XX_config;
45 
sl_WifiConfig()46 _i32 sl_WifiConfig()
47 {
48      _u8                              ucConfigOpt;
49      _u16                             uIPMode;
50      _i32                             RetVal = -1;
51      _i32                             RetVal_stop = -1;
52      _i32                             Mode = -1;
53      SlNetCfgIpV4Args_t               *ipV4 = NULL;
54      SlNetCfgIpV4Args_t               localIpV4;
55      _u16                             ipConfigSize = 0;
56 
57 
58      /* Turn NWP on */
59      Mode = sl_Start(NULL, NULL, NULL);
60      if (Mode < 0)
61      {
62          return Mode;
63      }
64 
65     while (1)
66     {
67         if ((SimpleLinkWifiCC32XX_config.Mode != SL_DEVICE_SYSCONFIG_AS_CONFIGURED) && (SimpleLinkWifiCC32XX_config.Mode != Mode))
68         {
69             /* Set NWP role */
70             RetVal = sl_WlanSetMode(SimpleLinkWifiCC32XX_config.Mode);
71             if (RetVal < 0)
72             {
73                 break;
74             }
75 
76             /* For changes to take affect, we restart the NWP */
77             RetVal = sl_Stop(200);
78             if (RetVal < 0)
79             {
80                 break;
81             }
82 
83             RetVal = sl_Start(NULL, NULL, NULL);
84             if (RetVal < 0)
85             {
86                 break;
87             }
88 
89             if(SimpleLinkWifiCC32XX_config.Mode != RetVal)
90             {
91                 RetVal = -1;
92                 break;
93             }
94         }
95 
96         /* Set connection policy */
97         if (SimpleLinkWifiCC32XX_config.ConnectionPolicy != SL_DEVICE_SYSCONFIG_AS_CONFIGURED)
98         {
99             RetVal =
100                 sl_WlanPolicySet(SL_WLAN_POLICY_CONNECTION,
101                                  SimpleLinkWifiCC32XX_config.ConnectionPolicy,
102                                  NULL,0);
103             if (RetVal < 0)
104             {
105                 break;
106             }
107         }
108         if (SimpleLinkWifiCC32XX_config.ProvisioningStop)
109         {
110             /* Stop Provisioning */
111             RetVal = sl_WlanProvisioning(SL_WLAN_PROVISIONING_CMD_STOP,
112                                          0xFF,
113                                          0,
114                                          NULL,
115                                          0x0);
116             if (RetVal < 0)
117             {
118                 break;
119             }
120         }
121         if (SimpleLinkWifiCC32XX_config.DeleteAllProfile)
122         {
123             /* Delete existing profiles */
124             RetVal = sl_WlanProfileDel(SL_WLAN_DEL_ALL_PROFILES);
125             if (RetVal < 0)
126             {
127                 break;
128             }
129         }
130 
131         /* Configure ipv4/DHCP */
132         if (SimpleLinkWifiCC32XX_config.Ipv4Config != SL_DEVICE_SYSCONFIG_AS_CONFIGURED)
133         {
134             if (SimpleLinkWifiCC32XX_config.Mode == ROLE_STA)
135             {
136                 uIPMode = SL_NETCFG_IPV4_STA_ADDR_MODE;
137             }
138             else if (SimpleLinkWifiCC32XX_config.Mode == ROLE_AP)
139             {
140                 uIPMode = SL_NETCFG_IPV4_AP_ADDR_MODE;
141             }
142             if (SimpleLinkWifiCC32XX_config.Ipv4Config == SL_NETCFG_ADDR_STATIC)
143             {
144                 ipV4 = &localIpV4;
145                 ipConfigSize = sizeof(SlNetCfgIpV4Args_t);
146 
147                 localIpV4.Ip          = (_u32)SimpleLinkWifiCC32XX_config.Ipv4;                // _u32 IP address
148                 localIpV4.IpMask      = (_u32)SimpleLinkWifiCC32XX_config.IpMask;              // _u32 Subnet mask for this AP/P2P
149                 localIpV4.IpGateway   = (_u32)SimpleLinkWifiCC32XX_config.IpGateway;           // _u32 Default gateway address
150                 localIpV4.IpDnsServer = (_u32)SimpleLinkWifiCC32XX_config.IpDnsServer;         // _u32 DNS server address
151             }
152             else if (SimpleLinkWifiCC32XX_config.Ipv4Config != SL_NETCFG_ADDR_DHCP)
153             {
154                 RetVal = -1;
155                 if (RetVal < 0)
156                 {
157                     break;
158                 }
159             }
160 
161             RetVal = sl_NetCfgSet(uIPMode, SimpleLinkWifiCC32XX_config.Ipv4Config, ipConfigSize, (_u8 *)ipV4);
162             if (RetVal < 0)
163             {
164                 break;
165             }
166         }
167 
168         /* Set scan policy */
169         if (SimpleLinkWifiCC32XX_config.ScanPolicy != SL_DEVICE_SYSCONFIG_AS_CONFIGURED)
170         {
171             _u32 intervalInSeconds = SimpleLinkWifiCC32XX_config.ScanIntervalInSeconds;
172             ucConfigOpt = SimpleLinkWifiCC32XX_config.ScanPolicy;
173             RetVal = sl_WlanPolicySet(SL_WLAN_POLICY_SCAN, ucConfigOpt, (_u8 *)&intervalInSeconds, sizeof(intervalInSeconds));
174             if (RetVal < 0)
175             {
176                 break;
177             }
178         }
179 
180         /* Set NWP Power Management Policy */
181         if (SimpleLinkWifiCC32XX_config.PMPolicy != SL_DEVICE_SYSCONFIG_AS_CONFIGURED)
182         {
183             if (SimpleLinkWifiCC32XX_config.PMPolicy != SL_WLAN_LONG_SLEEP_INTERVAL_POLICY)
184             {
185                 RetVal = sl_WlanPolicySet(SL_WLAN_POLICY_PM, SimpleLinkWifiCC32XX_config.PMPolicy, NULL,0);
186                 if (RetVal < 0)
187                 {
188                     break;
189                 }
190             }
191             else
192             {
193                 SlWlanPmPolicyParams_t PmPolicyParams;
194                 memset(&PmPolicyParams,0,sizeof(SlWlanPmPolicyParams_t));
195                 PmPolicyParams.MaxSleepTimeMs = SimpleLinkWifiCC32XX_config.MaxSleepTimeMS;  //max sleep time in mSec
196                 RetVal = sl_WlanPolicySet(SL_WLAN_POLICY_PM, SimpleLinkWifiCC32XX_config.PMPolicy, (_u8*)&PmPolicyParams, sizeof(PmPolicyParams));
197                 if (RetVal < 0)
198                 {
199                     break;
200                 }
201             }
202         }
203 
204         /* Set DHCP Server Configuration */
205         if (SimpleLinkWifiCC32XX_config.DHCPServer)
206         {
207             SlNetAppDhcpServerBasicOpt_t dhcpParams;
208             _u8 outLen = sizeof(SlNetAppDhcpServerBasicOpt_t);
209             dhcpParams.lease_time      = SimpleLinkWifiCC32XX_config.LeaseTime;           // lease time (in seconds) of the IP Address
210             dhcpParams.ipv4_addr_start = (_u32)SimpleLinkWifiCC32XX_config.StartAddress;  // first IP Address for allocation. IP Address should be set as Hex number - i.e. 0A0B0C01 for (10.11.12.1)
211             dhcpParams.ipv4_addr_last  = (_u32)SimpleLinkWifiCC32XX_config.LastAddress;   // last IP Address for allocation. IP Address should be set as Hex number - i.e. 0A0B0C01 for (10.11.12.1)
212             RetVal = sl_NetAppStop(SL_NETAPP_DHCP_SERVER_ID);                             // Stop DHCP server before settings
213             if (RetVal < 0)
214             {
215                 break;
216             }
217             RetVal = sl_NetAppSet(SL_NETAPP_DHCP_SERVER_ID, SL_NETAPP_DHCP_SRV_BASIC_OPT, outLen, (_u8* )&dhcpParams);  // set parameters
218             if (RetVal < 0)
219             {
220                 break;
221             }
222             if (SimpleLinkWifiCC32XX_config.Mode == ROLE_AP)
223             {
224                 RetVal = sl_NetAppStart(SL_NETAPP_DHCP_SERVER_ID);                  // Start DHCP server with new settings
225                 if (RetVal < 0)
226                 {
227                     break;
228                 }
229             }
230         }
231         else
232         {
233             sl_NetAppStop(SL_NETAPP_DHCP_SERVER_ID);
234         }
235         break;
236     }
237     /* Jump here if error occurred or after all the configurations was set successfully */
238     /* For changes to take affect, we restart the NWP - sl_start will be call by application */
239     RetVal_stop = sl_Stop(200);
240     if (RetVal_stop < 0)
241     {
242         return RetVal_stop;
243     }
244     return(RetVal);
245 }
246 
247