SWRU368C May 2018 – January 2021 CC3100 , CC3100MOD , CC3200 , CC3200MOD
When implementing a networking application, consider the different application blocks, the host driver software concepts described above, and system aspects such as hardware and operating system.
Figure 2-1 shows a state machine diagram that describes the basic software design.
Figure 2-1 shows the different states described in this chapter, the host driver events that trigger the code to move between different states, and basic error-handling events.
An example of the state machine is implemented in the following code:
• Init state – The following is an example of initializing the Wi-Fi subsystem as a WLAN station:
case INIT:
status = sl_Start(0, 0, 0);
if (status == ROLE_STA)
{
g_State = CONFIG;
}
else
{
g_State = SIMPLELINK_ERR;
}
break;
• WLAN connection – The following is an example of WLAN and network event handlers, demonstrating the WLAN connection, waiting for a successful connection, and acquiring an IP address:
/* SimpleLink WLAN event handler */
void SimpleLinkWlanEventHandler(void *pWlanEvents)
{
SlWlanEvent_t *pWlan = (SlWlanEvent_t *)pWlanEvents;
switch(pWlan->Event)
{
case SL_WLAN_CONNECT_EVENT:
g_Event |= EVENT_CONNECTED;
memcpy(g_AP_Name, pWlan->EventData.STAandP2PModeWlanConnected.ssid_name, pWlan->EventData.STAandP2PModeWlanConnected.ssid_len);
break;
case SL_WLAN_DISCONNECT_EVENT:
g_DisconnectionCntr++;
g_Event |= EVENT_DISCONNECTED;
g_DisconnectionReason = pWlan->EventData.STAandP2PModeDisconnected.reason_code;
memcpy(g_AP_Name, pWlan->EventData.STAandP2PModeWlanConnected.ssid_name, pWlan->EventData.STAandP2PModeWlanConnected.ssid_len);
break;
default:
break;
}
}
/* SimpleLink Networking event handler */
void SimpleLinkNetAppEventHandler(void *pNetAppEvent)
{
SlNetAppEvent_t *pNetApp = (SlNetAppEvent_t *)pNetAppEvent;
switch( pNetApp->Event )
{
case SL_NETAPP_IPV4_ACQUIRED:
g_Event |= EVENT_IP_ACQUIRED;
g_Station_Ip = pNetApp->EventData.ipAcquiredV4.ip;
g_GW_Ip = pNetApp->EventData.ipAcquiredV4.gateway;
g_DNS_Ip = pNetApp->EventData.ipAcquiredV4.dns;
break;
default:
break;
}
}
.
.
.
/* initiating the WLAN connection */
case WLAN_CONNECTION:
status = sl_WlanConnect(User.SSID,strlen(User.SSID),0,
&secParams, 0);
if (status == 0)
{
g_State = WLAN_CONNECTING;
}
else
{
g_State = SIMPLELINK_ERR;
}
/* waiting for SL_WLAN_CONNECT_EVENT to notify on a successful connection */
case WLAN_CONNECTING:
if (g_Event
&EVENT_CONNECTED)
{
printf("Connected to %s\n", g_AP_Name);
g_State = WLAN_CONNECTED;
}
break;
/* waiting for SL_NETAPP_IPV4_ACQUIRED to notify on a receiving an IP address */
case WLAN_CONNECTED:
if (g_Event
&EVENT_IP_ACQUIRED)
{
printf("Received IP address:%d.%d.%d.%d\n", (g_Station_Ip>>24)&0xFF,(g_Station_Ip>>16)&0xFF,(g_Station_Ip>>8)&0xFF,(g_Station_Ip&0xFF));
g_State = GET_SERVER_ADDR;
}
break;
• Socket connection – The following is an example of querying for the remote server IP address by using the server name, creating a TCP socket, and connecting to the remote server socket:
case GET_SERVER_ADDR:
status = sl_NetAppDnsGetHostByName(appData.HostName,
strlen(appData.HostName),
&appData.DestinationIP, SL_AF_INET);
if (status == 0)
{
g_State = SOCKET_CONNECTION;
}
else
{
printf("Unable to reach Host\n");
g_State = SIMPLELINK_ERR;
}
break;
case SOCKET_CONNECTION:
Addr.sin_family = SL_AF_INET;
Addr.sin_port = sl_Htons(80);
/* Change the DestinationIP endianity, to big endian */
Addr.sin_addr.s_addr = sl_Htonl(appData.DestinationIP);
AddrSize = sizeof(SlSockAddrIn_t);
SockId = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
if( SockId < 0 )
{
printf("Error creating socket\n\r");
status = SockId;
g_State = SIMPLELINK_ERR;
}
if (SockId >= 0)
{
status = sl_Connect(SockId, ( SlSockAddr_t *)
&Addr, AddrSize);
if( status >= 0 )
{
g_State = SOCKET_CONNECTED;
}
else
{
printf("Error connecting to socket\n\r");
g_State = SIMPLELINK_ERR;
}
}
break;
• Data transactions – The following is an example of sending and receiving TCP data over the open socket:
case SOCKET_CONNECTED:
/* Send data to the remote server */
sl_Send(appData.SockID, appData.SendBuff, strlen(appData.SendBuff), 0);
/* Receive data from the remote server */
sl_Recv(appData.SockID, &appData.Recvbuff[0], MAX_RECV_BUFF_SIZE, 0);
break;
• Socket disconnection – The following is an example of closing a socket:
case SOCKET_DISCONNECT:
sl_Close(appData.SockID);
/* Reopening the socket */
g_State = SOCKET_CONNECTION;
break;
• Device hibernate – The following is an example of putting the Wi-Fi subsystem into hibernate state:
case SIMPLELINK_HIBERNATE:
sl_Stop();
g_State = …
break;