Functions
Functions
Collaboration diagram for Functions:

Functions

NMI_API sint8 accept (SOCKET sock, struct sockaddr *addr, uint8 *addrlen)
 
NMI_API sint8 bind (SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
 
NMI_API sint8 close (SOCKET sock)
 
NMI_API sint8 connect (SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
 
NMI_API sint8 gethostbyname (uint8 *pcHostName)
 
NMI_API sint8 getsockopt (SOCKET sock, uint8 u8Level, uint8 u8OptName, const void *pvOptValue, uint8 *pu8OptLen)
 
NMI_API sint8 listen (SOCKET sock, uint8 backlog)
 
NMI_API sint8 m2m_ping_req (uint32 u32DstIP, uint8 u8TTL, tpfPingCb fpPingCb)
 
NMI_API uint32 nmi_inet_addr (char *pcIpAddr)
 
NMI_API sint16 recv (SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec)
 
NMI_API sint16 recvfrom (SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec)
 
NMI_API void registerSocketCallback (tpfAppSocketCb socket_cb, tpfAppResolveCb resolve_cb)
 
NMI_API sint16 send (SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 u16Flags)
 
NMI_API sint16 sendto (SOCKET sock, void *pvSendBuffer, uint16 u16SendLength, uint16 flags, struct sockaddr *pstrDestAddr, uint8 u8AddrLen)
 
NMI_API sint8 setsockopt (SOCKET socket, uint8 u8Level, uint8 option_name, const void *option_value, uint16 u16OptionLen)
 
NMI_API SOCKET socket (uint16 u16Domain, uint8 u8Type, uint8 u8Flags)
 
NMI_API void socketDeinit (void)
 Socket Layer De-initialization. More...
 
NMI_API void socketInit (void)
 

Detailed Description

Function Documentation

◆ accept()

NMI_API sint8 accept ( SOCKET  sock,
struct sockaddr addr,
uint8 addrlen 
)

The function has no current implementation. An empty deceleration is used to prevent errors when legacy application code is used. As it has no effect, it can be safely removed from any application using it.

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]addrNot used in the current implementation.
[in]addrlenNot used in the current implementation.
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Definition at line 645 of file socket.c.

◆ bind()

NMI_API sint8 bind ( SOCKET  sock,
struct sockaddr pstrAddr,
uint8  u8AddrLen 
)

Asynchronous bind function associates the provided address and local port to the socket. The function can be used with both TCP and UDP sockets. It is mandatory to call the bind function before starting any UDP or TCP server operation. Upon socket bind completion, the application will receive a SOCKET_MSG_BIND message in the socket callback.

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pstrAddrPointer to socket address structure "sockaddr_in" sockaddr_in
[in]u8AddrLenSize of the given socket address structure in bytes.
Precondition
The socket function must be called to allocate a socket before passing the socket ID to the bind function.
See also
socket
connect
listen
accept
recv
recvfrom
send
sendto
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

This example demonstrates the call of the bind socket operation after a successful socket operation.

struct sockaddr_in addr;
SOCKET udpServerSocket =-1;
int ret = -1;
if(udpServerSocket == -1)
{
udpServerSocket = socket(AF_INET,SOCK_DGRAM,0);
if(udpServerSocket >= 0)
{
addr.sin_family = AF_INET;
addr.sin_port = _htons(UDP_SERVER_PORT);
addr.sin_addr.s_addr = 0;
ret = bind(udpServerSocket,(struct sockaddr*)&addr,sizeof(addr));
if(ret == 0)
printf("Bind success!\n");
else
{
printf("Bind Failed. Error code = %d\n",ret);
close(udpServerSocket);
}
else
{
printf("UDP Server Socket Creation Failed\n");
return;
}
}

Definition at line 563 of file socket.c.

◆ close()

NMI_API sint8 close ( SOCKET  sock)

Synchronous close function, releases all the socket assigned resources.

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
Precondition
Sockets must be initialized through the call of the socketInit function. close is called only for valid socket identifiers created through the socket function.
Warning
If close is called while there are still pending messages (sent or received ) they will be discarded.
See also
socketInit socket
Returns
The function returned SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the error) otherwise.

Definition at line 879 of file socket.c.

◆ connect()

NMI_API sint8 connect ( SOCKET  sock,
struct sockaddr pstrAddr,
uint8  u8AddrLen 
)

Establishes a TCP connection with a remote server. The asynchronous connect function must be called after receiving a valid socket ID from the socket function. The application socket callback function is notified of the result of the connection attempt through the event SOCKET_MSG_CONNECT, along with a structure tstrSocketConnectMsg. If socket connection fails, the application should call close(). A successful connect means the TCP session is active. The application is then required to make a call to the recv function to receive any packets transmitted by the remote server, unless the application is interrupted by a notification of socket connection termination.

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pstrAddrAddress of the remote server.
[in]u8AddrLenSize of the given socket address structure in bytes. Not currently used, implemented for BSD compatibility only.
Precondition
The socket function must be called to allocate a TCP socket before passing the socket ID to the bind function. If the socket is not bound, you do NOT have to call bind before the "connect" function.
See also
socket recv send close
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

The example demonstrates a TCP application, showing how the asynchronous call to the connect function is made through the main function and how the callback function handles the SOCKET_MSG_CONNECT event.

Main Function

struct sockaddr_in Serv_Addr;
SOCKET TcpClientSocket =-1;
int ret = -1
TcpClientSocket = socket(AF_INET,SOCK_STREAM,0);
Serv_Addr.sin_family = AF_INET;
Serv_Addr.sin_port = _htons(1234);
Serv_Addr.sin_addr.s_addr = inet_addr(SERVER);
printf("Connected to server via socket %u\n",TcpClientSocket);
do
{
ret = connect(TcpClientSocket,(sockaddr_in*)&Serv_Addr,sizeof(Serv_Addr));
if(ret != 0)
{
printf("Connection Error\n");
}
else
{
printf("Connection successful.\n");
break;
}
}while(1)

Socket Callback

if(u8Msg == SOCKET_MSG_CONNECT)
{
if(pstrConnect->s8Error == 0)
{
uint8 acBuffer[GROWL_MSG_SIZE];
uint16 u16MsgSize;
printf("Connect success!\n");
u16MsgSize = FormatMsg(u8ClientID, acBuffer);
send(sock, acBuffer, u16MsgSize, 0);
recv(pstrNotification->Socket, (void*)au8Msg,GROWL_DESCRIPTION_MAX_LENGTH, GROWL_RX_TIMEOUT);
u8Retry = GROWL_CONNECT_RETRY;
}
else
{
M2M_DBG("Connection Failed, Error: %d\n",pstrConnect->s8Error");
close(pstrNotification->Socket);
}
}

Definition at line 674 of file socket.c.

◆ gethostbyname()

NMI_API sint8 gethostbyname ( uint8 pcHostName)

Asynchronous DNS resolving function. This function uses DNS to resolve a domain name to the corresponding IP address. A call to this function will cause a DNS request to be sent and the response will be delivered to the DNS callback function registered using registerSocketCallback()

Parameters
[in]pcHostNameNULL terminated string containing the domain name for the remote host. Its size must not exceed HOSTNAME_MAX_SIZE.
See also
registerSocketCallback
Warning
Successful completion of a call to gethostbyname() does not guarantee success of the DNS request, a negative return value indicates only locally-detected errors
Returns

Definition at line 1041 of file socket.c.

◆ getsockopt()

sint8 getsockopt ( SOCKET  sock,
uint8  u8Level,
uint8  u8OptName,
const void *  pvOptValue,
uint8 pu8OptLen 
)

Get socket options retrieves This Function isn't implemented yet but this is the form that will be released later.

Parameters
[in]sockSocket Identifie.
[in]u8LevelThe protocol level of the option.
[in]u8OptNameThe u8OptName argument specifies a single option to get.
[out]pvOptValueThe pvOptValue argument contains pointer to a buffer containing the option value.
[out]pu8OptLenOption value buffer length.
Returns
The function shall return ZERO for successful operation and a negative value otherwise.

Definition at line 1227 of file socket.c.

◆ listen()

NMI_API sint8 listen ( SOCKET  sock,
uint8  backlog 
)

After successfully binding a socket to an IP address and port on the system, start listening passively for incoming connections. The socket must be bound on a local port or the listen operation fails. Upon the call to the asynchronous listen function, response is received through the event SOCKET_MSG_LISTEN in the socket callback.

A successful listen means the TCP server operation is active. If a connection is accepted, then the application socket callback function is notified with the new connected socket through the event SOCKET_MSG_ACCEPT. Hence there is no need to call the accept function after calling listen.

After a connection is accepted, the user is then required to call recv to receive any packets transmitted by the remote host or to recieve notification of socket connection termination.

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]backlogNot used by the current implementation.
Precondition
The bind function must be called to assign the port number and IP address to the socket before the listen operation.
See also
bind
accept
recv
recvfrom
send
sendto
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

This example demonstrates the call of the listen socket operation after a successful socket operation.

static void TCP_Socketcallback(SOCKET sock, uint8 u8Msg, void * pvMsg)
{
int ret =-1;
switch(u8Msg)
{
{
tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg*)pvMsg;
if(pstrBind != NULL)
{
if(pstrBind->status == 0)
{
ret = listen(sock, 0);
if(ret <0)
printf("Listen failure! Error = %d\n",ret);
}
else
{
M2M_ERR("bind Failure!\n");
close(sock);
}
}
}
break;
{
if(pstrListen != NULL)
{
if(pstrListen->status == 0)
{
ret = accept(sock,NULL,0);
}
else
{
M2M_ERR("listen Failure!\n");
close(sock);
}
}
}
break;
{
if(pstrAccept->sock >= 0)
{
TcpNotificationSocket = pstrAccept->sock;
recv(pstrAccept->sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT);
}
else
{
M2M_ERR("accept failure\n");
}
}
break;
default:
break;
}
}

Definition at line 607 of file socket.c.

◆ m2m_ping_req()

NMI_API sint8 m2m_ping_req ( uint32  u32DstIP,
uint8  u8TTL,
tpfPingCb  fpPingCb 
)

The function request to send ping request to the given IP Address.

Parameters
[in]u32DstIPTarget Destination IP Address for the ping request. It must be represented in Network byte order. The function nmi_inet_addr could be used to translate the dotted decimal notation IP to its Network bytes order integer represntative.
[in]u8TTLIP TTL value for the ping request. If set to ZERO, the default value SHALL be used.
[in]fpPingCbCallback will be called to deliver the ping statistics.
See also
nmi_inet_addr
Returns
The function returns M2M_SUCCESS for successful operations and a negative value otherwise.

Definition at line 1250 of file socket.c.

◆ nmi_inet_addr()

NMI_API uint32 nmi_inet_addr ( char *  pcIpAddr)

Synchronous function which returns a BSD socket compliant Internet Protocol (IPv4) socket address. This IPv4 address in the input string parameter could either be specified as a hostname, or as a numeric string representation like n.n.n.n known as the IPv4 dotted-decimal format (i.e. "192.168.10.1"). This function is used whenever an ip address needs to be set in the proper format (i.e. for the tstrM2MIPConfig structure).

Parameters
[in]pcIpAddrA null terminated string containing the IP address in IPv4 dotted-decimal address.
Returns
Unsigned 32-bit integer representing the IP address in Network byte order (eg. "192.168.10.1" will be expressed as 0x010AA8C0).

Definition at line 983 of file socket.c.

◆ recv()

NMI_API sint16 recv ( SOCKET  sock,
void *  pvRecvBuf,
uint16  u16BufLen,
uint32  u32Timeoutmsec 
)

An asynchrnonous receive function, used to retrieve data from a TCP stream. Before calling the recv function, a successful socket connection status must have been received through any of the two socket events [SOCKET_MSG_CONNECT] or [SOCKET_MSG_ACCEPT], from the socket callback. Hence, indicating that the socket is already connected to a remote host. The application receives the required data in response to this asynchronous call through the reception of the event SOCKET_MSG_RECV in the socket callback.

Receiving the SOCKET_MSG_RECV message in the callback with zero or negative buffer length indicates the following:

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pvRecvBufPointer to a buffer that will hold the received data. The buffer is used in the recv callback to deliver the received data to the caller. The buffer must be resident in memory (heap or global buffer).
[in]u16BufLenThe buffer size in bytes.
[in]u32TimeoutmsecTimeout for the recv function in milli-seconds. If the value is set to ZERO, the timeout will be set to infinite (the recv function waits forever). If the timeout period is elapsed with no data received, the socket will get a timeout error.
Precondition
  • The socket function must be called to allocate a TCP socket before passing the socket ID to the recv function.
  • The socket in a connected state is expected to receive data through the socket interface.
See also
socket
connect
bind
listen
recvfrom
close
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

The example demonstrates a code snippet for the calling of the recv function in the socket callback upon notification of the accept or connect events, and the parsing of the received data when the SOCKET_MSG_RECV event is received.

switch(u8Msg)
{
{
if(pstrAccept->sock >= 0)
{
recv(pstrAccept->sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT);
}
else
{
M2M_ERR("accept\n");
}
}
break;
{
if(pstrRx->s16BufferSize > 0)
{
recv(sock,gau8RxBuffer,sizeof(gau8RxBuffer),TEST_RECV_TIMEOUT);
}
else
{
printf("Socet recv Error: %d\n",pstrRx->s16BufferSize);
close(sock);
}
}
break;
default:
break;
}

Definition at line 822 of file socket.c.

◆ recvfrom()

NMI_API sint16 recvfrom ( SOCKET  sock,
void *  pvRecvBuf,
uint16  u16BufLen,
uint32  u32TimeoutSeconds 
)

Receives data from a UDP Scoket.

The asynchronous recvfrom function is used to retrieve data from a UDP socket. The socket must already be bound to a local port before a call to the recvfrom function is made (i.e message SOCKET_MSG_BIND is received with successful status in the socket callback).

Upon calling the recvfrom function with a successful return code, the application is expected to receive a notification in the socket callback whenever a message is received through the SOCKET_MSG_RECVFROM event.

Receiving the SOCKET_MSG_RECVFROM message in the callback with zero, indicates that the socket is closed. Whereby a negative buffer length indicates one of the socket error codes such as socket timeout error SOCK_ERR_TIMEOUT

The recvfrom callback can also be used to show the IP address of the remote host that sent the frame by using the "strRemoteAddr" element in the tstrSocketRecvMsg structure. (refer to the code example)

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pvRecvBufPointer to a buffer that will hold the received data. The buffer shall be used in the recv callback to deliver the received data to the caller. The buffer must be resident in memory (heap or global buffer).
[in]u16BufLenThe buffer size in bytes.
[in]u32TimeoutSecondsTimeout for the recv function in milli-seconds. If the value is set to ZERO, the timeout will be set to infinite (the recv function waits forever).
Precondition
  • The socket function must be called to allocate a TCP socket before passing the socket ID to the recv function.
  • The socket corresponding to the socket ID must be successfully bound to a local port through the call to a bind function.
See also
socket bind close
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

The example demonstrates a code snippet for the calling of the recvfrom function in the socket callback upon notification of a successful bind event, and the parsing of the received data when the SOCKET_MSG_RECVFROM event is received.

switch(u8Msg)
{
{
tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg*)pvMsg;
if(pstrBind != NULL)
{
if(pstrBind->status == 0)
{
recvfrom(sock, gau8SocketTestBuffer, TEST_BUFFER_SIZE, 0);
}
else
{
M2M_ERR("bind\n");
}
}
}
break;
{
if(pstrRx->s16BufferSize > 0)
{
//get the remote host address and port number
uint16 u16port = pstrRx->strRemoteAddr.sin_port;
uint32 strRemoteHostAddr = pstrRx->strRemoteAddr.sin_addr.s_addr;
printf("Received frame with size = %d.\tHost address=%x, Port number = %d\n\n",pstrRx->s16BufferSize,strRemoteHostAddr, u16port);
ret = recvfrom(sock,gau8SocketTestBuffer,sizeof(gau8SocketTestBuffer),TEST_RECV_TIMEOUT);
}
else
{
printf("Socket recv Error: %d\n",pstrRx->s16BufferSize);
ret = close(sock);
}
}
break;
default:
break;
}

Definition at line 924 of file socket.c.

◆ registerSocketCallback()

NMI_API void registerSocketCallback ( tpfAppSocketCb  socket_cb,
tpfAppResolveCb  resolve_cb 
)

Register two callback functions one for asynchronous socket events and the other one for DNS callback registering function. The registered callback functions are used to retrieve information in response to the asynchronous socket functions called.

Parameters
[in]socket_cbAssignment of callback function to the global callback tpfAppSocketCb gpfAppSocketCb. Delivers socket messages to the host application. In response to the asynchronous function calls, such as bind listen accept connect
[in]resolve_cbAssignment of callback function to the global callback tpfAppResolveCb gpfAppResolveCb. Used for DNS resolving functionality. The DNS resolving technique is determined by the application registering the callback. NULL is assigned when DNS resolution is not required.
Returns
void
Remarks
If any of the socket functionality is not to be used, NULL is passed in as a parameter. It must be invoked after socketInit and before other socket layer operations.

Example

This example demonstrates the use of the registerSocketCallback to register a socket callback function with DNS resolution CB set to null for a simple UDP server example.

int8_t ret;
struct sockaddr_in addr;
// Initialize the board
system_init();
//Initialize the UART console.
configure_console();
// Initialize the BSP.
// Initialize socket address structure.
addr.sin_family = AF_INET;
addr.sin_port = _htons(MAIN_WIFI_M2M_SERVER_PORT);
addr.sin_addr.s_addr = _htonl(MAIN_WIFI_M2M_SERVER_IP);
// Initialize Wi-Fi parameters structure.
memset((uint8_t *)&param, 0, sizeof(tstrWifiInitParam));
// Initialize Wi-Fi driver with data and status callbacks.
param.pfAppWifiCb = wifi_cb;
ret = m2m_wifi_init(&param);
if (M2M_SUCCESS != ret) {
printf("main: m2m_wifi_init call error!(%d)\r\n", ret);
while (1) {
}
}
// Initialize socket module
// Connect to router.
m2m_wifi_connect((char *)DEFAULT_SSID, sizeof(DEFAULT_SSID), DEFAULT_AUTH, (char *)DEFAULT_KEY, M2M_WIFI_CH_ALL);

Definition at line 451 of file socket.c.

◆ send()

NMI_API sint16 send ( SOCKET  sock,
void *  pvSendBuffer,
uint16  u16SendLength,
uint16  u16Flags 
)

Asynchronous sending function, used to send data on a TCP/UDP socket.

Called by the application code when there is outgoing data available required to be sent on a specific socket handler. The only difference between this function and the similar sendto function, is the type of socket the data is sent on and the parameters passed in. send function is most commonly called for sockets in a connected state. After the data is sent, the socket callback function registered using registerSocketCallback(), is expected to receive an event of type SOCKET_MSG_SEND holding information containing the number of data bytes sent.

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pvSendBufferPointer to a buffer holding data to be transmitted.
[in]u16SendLengthThe buffer size in bytes.
[in]u16FlagsNot used in the current implementation.
Precondition
Sockets must be initialized using socketInit.

For TCP Socket:
Must use a successfully connected Socket (so that the intended recipient address is known ahead of sending the data). Hence this function is expected to be called after a successful socket connect operation(in client case or accept in the the server case).
For UDP Socket:
UDP sockets most commonly use sendto function, where the destination address is defined. However, in-order to send outgoing data using the send function, at least one successful call must be made to the sendto function before consecutive calls to the send function, to ensure that the destination address is saved in the firmware.

See also
socketInit recv sendto socket connect accept sendto
Warning
u16SendLength must not exceed SOCKET_BUFFER_MAX_LENGTH.
Use a valid socket identifer through the aprior call to the socket function. Must use a valid buffer pointer. Successful completion of a call to send() does not guarantee delivery of the message, A negative return value indicates only locally-detected errors
Returns
The function shall return SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the error) otherwise.

Definition at line 715 of file socket.c.

◆ sendto()

NMI_API sint16 sendto ( SOCKET  sock,
void *  pvSendBuffer,
uint16  u16SendLength,
uint16  flags,
struct sockaddr pstrDestAddr,
uint8  u8AddrLen 
)

Asynchronous sending function, used to send data on a UDP socket. Called by the application code when there is data required to be sent on a UDP socket. The application code is expected to receive data from a successfully bound socket node. The only difference between this function and the similar send function, is the type of socket the data is received on. This function works only with UDP sockets. After the data is sent, the socket callback function registered using registerSocketCallback(), is expected to receive an event of type SOCKET_MSG_SENDTO.

Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pvSendBufferPointer to a buffer holding data to be transmitted. A NULL value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]u16SendLengthThe buffer size in bytes. It must not exceed SOCKET_BUFFER_MAX_LENGTH.
[in]flagsNot used in the current implementation
[in]pstrDestAddrThe destination address.
[in]u8AddrLenDestination address length in bytes. Not used in the current implementation, only included for BSD compatibility.
Precondition
Sockets must be initialized using socketInit.
See also
socketInit recvfrom sendto socket connect accept send
Warning
u16SendLength must not exceed SOCKET_BUFFER_MAX_LENGTH.
Use a valid socket (returned from socket ). A valid buffer pointer must be used (not NULL).
Successful completion of a call to sendto() does not guarantee delivery of the message, A negative return value indicates only locally-detected errors
Returns
The function returns SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the error) otherwise.

Definition at line 767 of file socket.c.

◆ setsockopt()

NMI_API sint8 setsockopt ( SOCKET  socket,
uint8  u8Level,
uint8  option_name,
const void *  option_value,
uint16  u16OptionLen 
)

The setsockopt() function shall set the option specified by the option_name argument, at the protocol level specified by the level argument, to the value pointed to by the option_value argument for the socke specified by the socket argument.

Possible Options: SO_SET_UDP_SEND_CALLBACK: Enable/Disable callback messages for sendto().Since UDP is unreliable by default the user maybe interested (or not) in receiving a message of /ref SOCKET_MSG_SENDTO for each call of sendto(). Enabled if option_value equals /ref BTRUE Disabled otherwise. IP_ADD_MEMBERSHIP: valid for UDP sockets,this option is used to receive frames sent to a multicast group. option_value shall be a pointer to Unsigned 32 bit integer containing the Multicast ipv4 address. IP_DROP_MEMBERSHIP: valid for UDP sockets,this option is used to Stop receiving frames sent to a multicast group. option_value shall be a pointer to Unsigned 32 bit integer containing the Multicast ipv4 address.

Possible values for s32Level: This argument is ignored.

Parameters
[in]socketSocket handler.
[in]u8Levelprotocol level. always SOL_SOCKET for now.
[in]option_nameoption to be set.
[in]option_valuepointer to user provided value.
[in]u16OptionLenlength of the option value.
Warning
-Note that sending IGMP packets to Join/Leave multicast groups is not currently implemented.
Calling this function will Pass/Filter packets sent to the Multicast address provided in the option_value
Returns
The function shall return SOCK_ERR_NO_ERROR for successful operation and a negative value (indicating the error) otherwise.

Definition at line 1175 of file socket.c.

◆ socket()

NMI_API SOCKET socket ( uint16  u16Domain,
uint8  u8Type,
uint8  u8Flags 
)

Synchronous socket allocation function based on the specified socket type. Created sockets are non-blocking and their possible types are either TCP or a UDP sockets. The maximum allowed number of TCP sockets is TCP_SOCK_MAX sockets while the maximum number of UDP sockets that can be created simultaneously is UDP_SOCK_MAX sockets.

Parameters
[in]u16DomainSocket family. The only allowed value is AF_INET (IPv4.0) for TCP/UDP sockets.
[in]u8TypeSocket type. Allowed values are:
[in]u8FlagsUsed to specify the socket creation flags. It shall be set to zero for normal TCP/UDP sockets, or SOCKET_FLAGS_SSL if the socket is used for SSL session. The use of the flag SOCKET_FLAGS_SSL has no meaning in case of UDP sockets.
Precondition
The socketInit function must be called once at the beginning of the application to initialize the socket handler. before any call to the socket function can be made.
See also
connect bind listen accept recv recvfrom send sendto close setsockopt getsockopt
Returns
On successful socket creation, a non-blocking socket type is created and a socket ID is returned In case of failure the function returns a negative value, identifying one of the socket error codes defined. For example: SOCK_ERR_INVALID for invalid argument or SOCK_ERR_MAX_TCP_SOCK if the number of TCP allocated sockets exceeds the number of available sockets.
Remarks
The socket function must be called before any other related socket functions "e.g. send, recv, close ..etc"

Example

This example demonstrates the use of the socket function to allocate the socket, returning the socket handler to be used for other socket operations. Socket creation is dependent on the socket type.

UDP example

SOCKET UdpServerSocket = -1;
UdpServerSocket = socket(AF_INET, SOCK_DGRAM, 0);

TCP example

tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0));

Definition at line 477 of file socket.c.

◆ socketDeinit()

NMI_API void socketDeinit ( void  )

Socket Layer De-initialization.

The function performs the necessary cleanup for the socket library static data It must be invoked only after all desired socket operations have been performed on any active sockets.

Definition at line 423 of file socket.c.

◆ socketInit()

NMI_API void socketInit ( void  )

The function performs the necessary initialization for the socket library through the following steps:

  • A check made by the global variable gbSocketInit, ensuring that initialization for sockets is performed only once, in-order to prevent reseting the socket instances already created in the global socket array (gastrSockets).
  • Zero initializations to the global socket array (gastrSockets), which holds the list of TCP sockets.
  • Registers the socket (Host Interface)hif callback function through the call to the hif_register_cb function. This facilitates handling all of the socket related functions received through interrupts from the firmware.
Returns
void
Remarks
This initialization function must be invoked before any socket operation is performed. No error codes from this initialization function since the socket array is statically allocated based on the maximum number of sockets MAX_SOCKET which is tuned to the systems capability.

Example

This example demonstrates the use of the socketInit for socket initialization for an mqtt chat application.

int8_t ret;
char topic[strlen(MAIN_CHAT_TOPIC) + MAIN_CHAT_USER_NAME_SIZE + 1];
//Initialize the board.
system_init();
//Initialize the UART console.
configure_console();
// Initialize the BSP.
----------
// Initialize socket interface.
registerSocketCallback(socket_event_handler, socket_resolve_handler);
// Connect to router.
m2m_wifi_connect((char *)DEFAULT_SSID, sizeof(DEFAULT_SSID),
DEFAULT_AUTH, (char *)DEFAULT_KEY, M2M_WIFI_CH_ALL);

Definition at line 394 of file socket.c.



inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:18:00