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:
Services/Options | Input Parameters | Description |
---|---|---|
RNWF_NET_TLS_CONFIG_1 | TLS configuration list: CA name, Certificate name, Key name, Key password, server name | Use the TLS configuration 1 |
RNWF_NET_TLS_CONFIG_2 | TLS configuration list: CA name, Certificate name, Key name, Key password, server name | Use the TLS configuration 2 |
RNWF_NET_DHCP_SERVER_ENABLE | DHCP Configuration: Set IP, Pool start,
Parameter ID (Int) | Enable the DHCP server |
RNWF_NET_DHCP_SERVER_DISABLE | None | Disable the DHCP server |
RNWF_NET_SOCK_TCP_OPEN | None | Open TCP socket. Returns socket ID. |
RNWF_NET_SOCK_UDP_OPEN | None | Open UDP socket. Returns socket ID. |
RNWF_NET_SOCK_CLOSE | Socket ID (Int) | Close the socket |
RNWF_NET_SOCK_CONFIG | Socket ID | Configures the socket settings |
RNWF_NET_SOCK_SET_CALLBACK | Callback function handler | Register application callback for socket |
The events that are returned in the Net socket service are provided below:
Events | Response Components | Description |
---|---|---|
RNWF_NET_SOCK_EVENT_CONNECTED |
Socket ID (Integer) | Reports the socket connected event |
RNWF_NET_SOCK_EVENT_TLS_DONE | Socket ID (Integer) | TLS handshake done, on this event the TLS configuration instance can be re used for other TLS sessions |
RNWF_NET_SOCK_EVENT_DISCONNECTED | Socket ID (Integer) | Socket disconnected |
RNWF_NET_SOCK_EVENT_READ | Socket ID
(Integer) Length (Integer) | Reports the length of data available on the given socket ID |
RNWF_NET_SOCK_EVENT_ERROR |
Socket ID (Integer) | Reports the socket error events |
The basic net socket service sequence chart is provided below:
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(); } }