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-6. 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
RNWF_NET_IF_CONFIGNoneNot used in V1.0.0 release
RNWF_NET_SOCK_SET_SRVC_CALLBACKCallback function handlerRegister callback function from any service using Net Service

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

Table 3-7. 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
RNWF_NET_SOCK_EVENT_UNDEFINEDNoneMiscellaneous error code
The basic net socket service sequence chart is provided below:
Figure 3-16. 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:
/*
    Main application
*/

/* Wi-Fi Configuration */
#define HOME_AP_SSID        "HOME_AP"
#define HOME_AP_PASSPHRASE  "12345678"
#define HOME_AP_SECURITY    RNWF_WPA2_MIXED

uint8_t tcp_client_msg[] = "Type here and receive its echo!\r\n";

/* TCP Socket */
RNWF_NET_SOCKET_t tcp_client_socket = {
        .bind_type = RNWF_BIND_REMOTE,
        .sock_port = 6666,
        .sock_type = RNWF_SOCK_TCP,
        .sock_addr = "192.168.1.163",
        };

void APP_WIFI_Callback(RNWF_WIFI_EVENT_t event, uint8_t *p_str)
{
    switch(event)
    {
        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_DONE:
        {
            printf("DHCP IP:%s\n", &p_str[2]);  
            RNWF_NET_SOCK_SrvCtrl(RNWF_NET_SOCK_TCP_OPEN, &tcp_client_socket);
            break;
        }
        default:
            break;          
    }    
}

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;  
    }    
}

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();
    }    
}