3.1.7.3 Net Socket Service

The Net Socket service provides network and socket services to the user application. It includes DHCP server configuration for the Wi-Fi interface and API's for socket operations such as open, read, write and close. It also provides 2 simultaneous TLS configuration instances which can be used with a given socket communication tunnel. The Net service API call syntax is provided below:
RNWF_RESULT_t RNWF_NET_SOCK_SrvCtrl( RNWF_NET_SOCK_SERVICE_t request, void *input)
The Net service provides the following services for the user:
Table 3-7. Net Socket Services
Services/OptionsInput ParametersDescription
RNWF_NET_TLS_CONFIG_1TLS configuration list: CA name, Certificate name, Key name, Key password, server nameUse the TLS configuration 1
RNWF_NET_TLS_CONFIG_2TLS configuration list: CA name, Certificate name, Key name, Key password, server nameUse the TLS configuration 2
RNWF_NET_DHCP_SERVER_ENABLEDHCP Configuration: Set IP, Pool start,

Parameter ID (Int)
Parameter value  (String)

Enable the DHCP server
RNWF_NET_DHCP_SERVER_DISABLENoneDisable the DHCP server
RNWF_NET_SOCK_TCP_OPENNoneOpen TCP socket. Returns socket ID.
RNWF_NET_SOCK_UDP_OPENNoneOpen UDP socket. Returns socket ID.
RNWF_NET_SOCK_CLOSESocket ID (Int)Close the socket
RNWF_NET_SOCK_CONFIGSocket ID

No delay, Keep alive

Configures the socket settings
RNWF_NET_SOCK_SET_CALLBACKCallback function handlerRegister application callback for socket

The events that are returned in the Net socket service are provided below:

Table 3-8. Net Sock Events List
EventsResponse ComponentsDescription
RNWF_NET_SOCK_EVENT_CONNECTED

Socket ID (Integer)
Local Address (String)
Local port (Integer)
Remote address (String)
Remote port (Integer)

Reports the socket connected event
RNWF_NET_SOCK_EVENT_TLS_DONESocket ID (Integer)TLS handshake done, on this event the TLS configuration instance can be re used for other TLS sessions
RNWF_NET_SOCK_EVENT_DISCONNECTEDSocket ID (Integer)Socket disconnected
RNWF_NET_SOCK_EVENT_READSocket ID (Integer)

Length (Integer)

Reports the length of data available on the given socket ID
RNWF_NET_SOCK_EVENT_ERROR

Socket ID (Integer)
Error code (Integer)

Reports the socket error events
The basic net socket service sequence chart is provided below:
Figure 3-35. Basic Net Socket Service Sequence Chart

Socket Write

The socket service provides the write API for the TCP and UDP sockets. Following are the API prototypes:
RNWF_RESULT_t RNWF_NET_TCP_SOCK_Write( uint32_t socket, uint16_t length, uint8_t *input) 
RNWF_RESULT_t RNWF_NET_UDP_SOCK_Write( uint32_t socket, uint8_t *addr, uint32_t port, uint16_t length, uint8_t *input)
Socket Read

The socket service provides the read API for the TCP and UDP sockets. Following are the API prototypes:

int16_t RNWF_NET_TCP_SOCK_Read( uint32_t socket, uint16_t length, uint8_t *buffer)
int16_t RNWF_NET_TCP_SOCK_Read( uint32_t socket, uint16_t length, uint8_t *buffer)
The sample TCP socket example is provided below:
/*
    This is ONLY an example for TCP Socket
*/
 
//-------------------------------------
#include <stdio.h>
 
/* This section lists other files that are included in this file.*/
#include "rnwf_app.h"
#include "rnwf_wifi_service.h"
#include "rnwf_net_service.h"
#include "rnwf_interface.h"
 
/* Wi-Fi Configuration */
#define HOME_AP_SSID                 "HOME_AP"
#define HOME_AP_PASSPHRASE  "12345678"
#define HOME_AP_SECURITY        RNWF_WPA2_MIXED
#define STA_AUTOCONNECT         1
 
/* feature additions from FW v2.0.0*/
    static uint8_t isSockOpen = 0;      //guard condition to open a socket
 
/* user-defined tcp socket data to send/write */
    uint8_t tcp_client_msg[] = "Type here and receive its echo!!\r\n";
 
RNWF_NET_SOCKET_t tcp_client_sock_6666 = {
        .bind_type = RNWF_BIND_REMOTE,
        .sock_port = 6666,
        .sock_type = RNWF_SOCK_TCP,
        .sock_addr = "192.168.0.177",
    /* feature addition from FW v2.0.0*/
        .sockIP = RNWF_NET_SOCK_IPv4,
        };
 
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");
	     /* An example to write data on a non-TLS socket */
	     RNWF_NET_TCP_SOCK_Write(socket, sizeof(tcp_client_msg), tcp_client_msg);
            break;
        }
        case RNWF_NET_SOCK_EVENT_DISCONNECTED:
        {
            printf("Socket Closed!\n"); 
            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:
        {   
        /* An example to read/write data from/to a socket */            
            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_SNTP_UP:
        {   
            break;
        }
        case RNWF_CONNECTED:
        {
            printf("Wi-Fi Connected\n");
            break;
        }
        case RNWF_DISCONNECTED:
        {
            printf("Wi-Fi Disconnected\nReconnecting... \n");
            RNWF_WIFI_SrvCtrl(RNWF_STA_CONNECT, NULL);
            break;
        }
	case RNWF_DHCP_IPV4_DONE:
        {
            printf("DHCP IPv4: %s\n", &p_str[2]);
	     if(isSockOpen == 0)
            {
                isSockOpen = 1;
                RNWF_NET_SOCK_SrvCtrl(RNWF_NET_SOCK_TCP_OPEN, &tcp_client_sock_6666);
            }
            break;       
        }
        case RNWF_DHCP_LINK_LOCAL_IPV6_DONE:
        {
            printf("DHCP link-local IPv6:%s\n", &p_str[2]);
            break;
        }
        case RNWF_DHCP_GLOBAL_IPV6_DONE:
        {
            printf("DHCP global IPv6:%s\n", &p_str[2]);
            break;
        }
        case RNWF_SET_REGDOM:
        {
            RNWF_WIFI_SrvCtrl(RNWF_SET_WIFI_REGDOM, (void *)COUNTRY_CODE);
            break;
        }
        case RNWF_SCAN_INDICATION:
        {
            break;
        }
        case RNWF_SCAN_DONE:
        {
            break;
        }
        case RNWF_CONNECT_FAILED:
        {
            break;
        }
        default:
        {
            break;
        }
    }
}
 
int main(void)
{   
    SYSTEM_Initialize();
    RNWF_IF_Init();
 
    RNWF_WIFI_PARAM_t wifi_sta_cfg = {RNWF_WIFI_MODE_STA, HOME_AP_SSID, HOME_AP_PASSPHRASE, HOME_AP_SECURITY, STA_AUTOCONNECT};    
 
    /* RNWF Application Callback register */
    RNWF_WIFI_SrvCtrl(RNWF_WIFI_SET_CALLBACK, APP_WIFI_Callback);
    RNWF_WIFI_SrvCtrl(RNWF_SET_WIFI_PARAMS, &wifi_sta_cfg);
    RNWF_NET_SOCK_SrvCtrl(RNWF_NET_SOCK_SET_CALLBACK, APP_SOCKET_Callback);  
 
    while(1)
    {  
        RNWF_EVENT_Handler();
    }
}