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) |
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.
[in] | sock | Socket 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] | addr | Not used in the current implementation. |
[in] | addrlen | Not used in the current implementation. |
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.
[in] | sock | Socket 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] | pstrAddr | Pointer to socket address structure "sockaddr_in" sockaddr_in |
[in] | u8AddrLen | Size of the given socket address structure in bytes. |
This example demonstrates the call of the bind socket operation after a successful socket operation.
Synchronous close function, releases all the socket assigned resources.
[in] | sock | Socket 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. |
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.
[in] | sock | Socket 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] | pstrAddr | Address of the remote server. |
[in] | u8AddrLen | Size of the given socket address structure in bytes. Not currently used, implemented for BSD compatibility only. |
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.
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()
[in] | pcHostName | NULL terminated string containing the domain name for the remote host. Its size must not exceed HOSTNAME_MAX_SIZE. |
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.
[in] | sock | Socket Identifie. |
[in] | u8Level | The protocol level of the option. |
[in] | u8OptName | The u8OptName argument specifies a single option to get. |
[out] | pvOptValue | The pvOptValue argument contains pointer to a buffer containing the option value. |
[out] | pu8OptLen | Option value buffer length. |
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.
[in] | sock | Socket 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] | backlog | Not used by the current implementation. |
This example demonstrates the call of the listen socket operation after a successful socket operation.
The function request to send ping request to the given IP Address.
[in] | u32DstIP | Target 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] | u8TTL | IP TTL value for the ping request. If set to ZERO, the default value SHALL be used. |
[in] | fpPingCb | Callback will be called to deliver the ping statistics. |
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).
[in] | pcIpAddr | A null terminated string containing the IP address in IPv4 dotted-decimal address. |
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:
[in] | sock | Socket 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] | pvRecvBuf | Pointer 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] | u16BufLen | The buffer size in bytes. |
[in] | u32Timeoutmsec | Timeout 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. |
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.
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)
[in] | sock | Socket 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] | pvRecvBuf | Pointer 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] | u16BufLen | The buffer size in bytes. |
[in] | u32TimeoutSeconds | Timeout 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). |
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.
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.
[in] | socket_cb | Assignment 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_cb | Assignment 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. |
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.
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.
[in] | sock | Socket 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] | pvSendBuffer | Pointer to a buffer holding data to be transmitted. |
[in] | u16SendLength | The buffer size in bytes. |
[in] | u16Flags | Not used in the current implementation. |
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.
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.
[in] | sock | Socket 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] | pvSendBuffer | Pointer 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] | u16SendLength | The buffer size in bytes. It must not exceed SOCKET_BUFFER_MAX_LENGTH. |
[in] | flags | Not used in the current implementation |
[in] | pstrDestAddr | The destination address. |
[in] | u8AddrLen | Destination address length in bytes. Not used in the current implementation, only included for BSD compatibility. |
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.
[in] | socket | Socket handler. |
[in] | u8Level | protocol level. always SOL_SOCKET for now. |
[in] | option_name | option to be set. |
[in] | option_value | pointer to user provided value. |
[in] | u16OptionLen | length of the option value. |
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.
[in] | u16Domain | Socket family. The only allowed value is AF_INET (IPv4.0) for TCP/UDP sockets. |
[in] | u8Type | Socket type. Allowed values are: |
[in] | u8Flags | Used 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. |
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.
NMI_API void socketDeinit | ( | void | ) |
NMI_API void socketInit | ( | void | ) |
The function performs the necessary initialization for the socket library through the following steps:
This example demonstrates the use of the socketInit for socket initialization for an mqtt chat application.