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 |
RNWF_NET_IF_CONFIG | None | Not used in V1.0.0 release |
RNWF_NET_SOCK_SET_SRVC_CALLBACK | Callback function handler | Register callback function from any service using Net Service |
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 |
RNWF_NET_SOCK_EVENT_UNDEFINED | None | Miscellaneous error code |
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:
/* 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(); } }