The example code below shows the usage of the Quessence Wireless and Networking APIs to open, send data and close a TCP client socket.

//! WLAN include file to refer wlan APIs

#include “rsi_wlan_apis.h”

//! socket include file to refer socket APIs

#include “rsi_socket.h”

//! Access point SSID to connect

#define SSID              “REDPINE_AP”

//! Security type

#define SECURITY_TYPE     0

//! Password

#define PSK               “”

//! DHCP mode 1- Enable 0- Disable

#define DHCP_MODE         1

//! If DHCP mode is disabled given IP statically

#if !(DHCP_MODE)

//! IP address of the module

//! E.g: 0x650AA8C0 == 192.168.10.101

#define DEVICE_IP         0x650AA8C0

//! IP address of Gateway

//! E.g: 0x010AA8C0 == 192.168.10.1

#define GATEWAY           0x010AA8C0

//! IP address of netmask

//! E.g: 0x00FFFFFF == 255.255.255.0

#define NETMASK           0x00FFFFFF

#endif

//! Device port number

#define DEVICE_PORT       5001

//! Server port number

#define SERVER_PORT       5001

//! Server IP address. Should be in reverse long format

//! E.g: 0x640AA8C0 == 192.168.10.100

#define SERVER_IP_ADDRESS 0x640AA8C0

//! Number of packet to send or receive

#define NUMBER_OF_PACKETS 1000

//! Memory length for driver

#define GLOBAL_BUFF_LEN   8000

//! Memory to initialize driver

uint8_t global_buf[GLOBAL_BUFF_LEN];

int32_t rsi_tcp_client()

{

int32_t     client_socket;

struct      rsi_sockaddr_in server_addr, client_addr;

int32_t     status       = RSI_SUCCESS;

int32_t     packet_count = 0;

#if !(DHCP_MODE)

uint32_t    ip_addr      = DEVICE_IP;

uint32_t    network_mask = NETMASK;

uint32_t    gateway      = GATEWAY;

#endif

//! Driver initialization

status = rsi_driver_init(global_buf, GLOBAL_BUFF_LEN);

if(status != RSI_SUCCESS)

{

return status;

}

    //! Redpine module intialisation

    status = rsi_device_init(RSI_LOAD_IMAGE_I_FW);

    if(status != RSI_SUCCESS)

    {

      return status;

    }

 

//! WC initialization

status = rsi_wireless_init(0, 0);

if(status != RSI_SUCCESS)

{

return status;

}

//! Scan for Access points

status = rsi_wlan_scan(SSID, 0, NULL, 0);

if(status != RSI_SUCCESS)

{

return status;

}

//! Connect to an Access point

status = rsi_wlan_connect(SSID, RSI_OPEN, NULL, NULL);

if(status != RSI_SUCCESS)

{

return status;

}

//! Configure IP

#if DHCP_MODE

status = rsi_config_ipaddress(RSI_IP_VERSION_4, RSI_DHCP, 0, 0, 0, NULL, 0);

#else

status = rsi_config_ipaddress(RSI_IP_VERSION_4, RSI_STATIC, (uint8_t *)&ip_addr, (uint8_t *)&network_mask, (uint8_t *)&gateway, NULL, 0);

#endif

if(status != RSI_SUCCESS)

{

return status;

}

//! Create socket

client_socket = rsi_socket(AF_INET, SOCK_STREAM, 0);

if(client_socket < 0)

{

status = rsi_wlan_get_status();

return status;

}

//! Set family type

client_addr.sin_family= AF_INET;

//! Set local port number

client_addr.sin_port = htons(DEVICE_PORT);

//! Set all bits of the padding field to 0

memset(client_addr.sin_zero, ‘\0’, sizeof(client_addr));

//! Bind socket

status = rsi_bind(client_socket, (struct rsi_sockaddr*) &client_addr, sizeof(client_addr));

if(status != RSI_SUCCESS)

{

    status = rsi_wlan_get_status();

    rsi_shutdown(client_socket, 0);

    return status;

 

}

//! Set server address family

server_addr.sin_family = AF_INET;

//! Set server port number, using htons function to use proper byte order

server_addr.sin_port = htons(SERVER_PORT);

//! Set IP address to localhost

server_addr.sin_addr.s_addr = SERVER_IP_ADDRESS;

//! Set all bits of the padding field to 0

memset(server_addr.sin_zero, ‘\0’, sizeof(server_addr));

//! Connect to server socket

status = rsi_connect(client_socket, (struct rsi_sockaddr*) &server_addr, sizeof(server_addr));

if(status != RSI_SUCCESS)

{

status = rsi_wlan_get_status();

rsi_shutdown(client_socket, 0);

return status;

}

while(packet_count < NUMBER_OF_PACKETS)

{

//! Send data on socket

status = rsi_send(client_socket,“Hello from TCP client!!!”, (sizeof(“Hello from TCP client!!!”)  1), 0);

if(status < 0)

{

status = rsi_wlan_get_status();

rsi_shutdown(client_socket, 0);

return status;

}

packet_count++;

}

return 0;

}

int main()

{

int32_t status;

//! Call TCP client application

status = rsi_tcp_client();

return status;

}