3.2.6.3.1 Net Socket Service Functions

The basic net socket service sequence chart is provided below:

Figure 3-42. Basic Net Socket Service Sequence Chart

The following figure illustrates the Socket open sequence.

Figure 3-43. Socket Open Sequence

The following figure illustrates the Socket binding sequence.

Figure 3-44. Socket Binding Sequence Chart

The following figure illustrates the Socket closing sequence.

Figure 3-45. Socket Closing Sequence Chart
Socket callback example:
void APP_SOCKET_Callback(uint32_t socket, RNWF_NET_SOCK_EVENT_t event, uint8_t *p_str)
{
    switch(event)
    {
        case RNWF_NET_SOCK_EVENT_CONNECTED:            
            printf("Connected to server!\n");
            RNWF_NET_TCP_SOCK_Write(socket, sizeof(tcp_client_msg), tcp_client_msg);
            break;
        case RNWF_NET_SOCK_EVENT_DISCONNECTED:
            RNWF_NET_SOCK_SrvCtrl(RNWF_NET_SOCK_CLOSE, &socket);
            break;
        case RNWF_NET_SOCK_EVENT_ERROR:
            printf("Error: %s!\n", p_str);
            break;
        case RNWF_NET_SOCK_EVENT_READ:
        {         
            uint8_t rx_data[64];
            int32_t rcvd_len;
            uint16_t rx_len = *(uint16_t *)p_str;         
            if((rx_len < 64) && (rcvd_len = RNWF_NET_TCP_SOCK_Read(socket, rx_len, rx_data)) > 0)
            {                
                rx_data[rx_len] = '\0';
                printf("Rx->%s\r\n", rx_data);
                RNWF_NET_TCP_SOCK_Write(socket, rx_len, rx_data);                
            }            
            break; 
        }
        default:
            break;
    }    
}
void APP_WIFI_Callback(RNWF_WIFI_EVENT_t event, uint8_t *p_str)
{
    switch(event)
    {
        case RNWF_DHCP_DONE:
        {
            printf("DHCP IP:%s\n", &p_str[2]);  
            RNWF_NET_SOCK_SrvCtrl(RNWF_NET_SOCK_TCP_OPEN, &tcp_client_socket);
            break;
        }
        default:
            break;
    }    
}
int main(void)
{
    SYSTEM_Initialize();
    RNWF_IF_Init();    

    /* RNWF Application Callback register */
    RNWF_WIFI_SrvCtrl(RNWF_WIFI_SET_CALLBACK, APP_WIFI_Callback);
    RNWF_NET_SOCK_SrvCtrl(RNWF_NET_SOCK_SET_CALLBACK, APP_SOCKET_Callback);
        
    /* Wi-Fii Connectivity */
    RNWF_WIFI_PARAM_t wifi_sta_cfg = {RNWF_WIFI_MODE_STA, HOME_AP_SSID, HOME_AP_PASSPHRASE, HOME_AP_SECURITY, 1};
    
    RNWF_WIFI_SrvCtrl(RNWF_SET_WIFI_PARAMS, &wifi_sta_cfg);
    while(1)
    {                
        RNWF_EVENT_Handler();
    }    
}