Microchip® Advanced Software Framework

api_lib.c File Reference

Sequential API External module.

#include "lwip/opt.h"
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/ip.h"
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include <string.h>

Macros

#define API_MSG_VAR_ALLOC(name)   API_VAR_ALLOC(struct api_msg, MEMP_API_MSG, name)
 
#define API_MSG_VAR_ALLOC_DONTFAIL(name)   API_VAR_ALLOC_DONTFAIL(struct api_msg, MEMP_API_MSG, name)
 
#define API_MSG_VAR_DECLARE(name)   API_VAR_DECLARE(struct api_msg, name)
 
#define API_MSG_VAR_FREE(name)   API_VAR_FREE(MEMP_API_MSG, name)
 
#define API_MSG_VAR_REF(name)   API_VAR_REF(name)
 

Functions

err_t netconn_accept (struct netconn *conn, struct netconn **new_conn)
 Accept a new connection on a TCP listening netconn. More...
 
err_t netconn_bind (struct netconn *conn, ip_addr_t *addr, u16_t port)
 Bind a netconn to a specific local IP address and port. More...
 
err_t netconn_close (struct netconn *conn)
 Close a TCP netconn (doesn't delete it). More...
 
static err_t netconn_close_shutdown (struct netconn *conn, u8_t how)
 Close or shutdown a TCP netconn (doesn't delete it). More...
 
err_t netconn_connect (struct netconn *conn, ip_addr_t *addr, u16_t port)
 Connect a netconn to a specific remote IP address and port. More...
 
err_t netconn_delete (struct netconn *conn)
 Close a netconn 'connection' and free its resources. More...
 
err_t netconn_disconnect (struct netconn *conn)
 Disconnect a netconn from its current peer (only valid for UDP netconns). More...
 
err_t netconn_getaddr (struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local)
 Get the local or remote IP address and port of a netconn. More...
 
err_t netconn_gethostbyname (const char *name, ip_addr_t *addr)
 Execute a DNS query, only one IP address is returned. More...
 
err_t netconn_join_leave_group (struct netconn *conn, ip_addr_t *multiaddr, ip_addr_t *netif_addr, enum netconn_igmp join_or_leave)
 Join multicast groups for UDP netconns. More...
 
err_t netconn_listen_with_backlog (struct netconn *conn, u8_t backlog)
 Set a TCP netconn into listen mode. More...
 
struct netconnnetconn_new_with_proto_and_callback (enum netconn_type t, u8_t proto, netconn_callback callback)
 Create a new netconn (of a specific type) that has a callback function. More...
 
err_t netconn_recv (struct netconn *conn, struct netbuf **new_buf)
 Receive data (in form of a netbuf containing a packet buffer) from a netconn. More...
 
static err_t netconn_recv_data (struct netconn *conn, void **new_buf)
 Receive data: actual implementation that doesn't care whether pbuf or netbuf is received. More...
 
err_t netconn_recv_tcp_pbuf (struct netconn *conn, struct pbuf **new_buf)
 Receive data (in form of a pbuf) from a TCP netconn. More...
 
void netconn_recved (struct netconn *conn, u32_t length)
 TCP: update the receive window: by calling this, the application tells the stack that it has processed data and is able to accept new data. More...
 
err_t netconn_send (struct netconn *conn, struct netbuf *buf)
 Send data over a UDP or RAW netconn (that is already connected). More...
 
err_t netconn_sendto (struct netconn *conn, struct netbuf *buf, ip_addr_t *addr, u16_t port)
 Send data (in form of a netbuf) to a specific remote IP address and port. More...
 
err_t netconn_shutdown (struct netconn *conn, u8_t shut_rx, u8_t shut_tx)
 Shut down one or both sides of a TCP netconn (doesn't delete it). More...
 
err_t netconn_write_partly (struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags, size_t *bytes_written)
 Send data over a TCP netconn. More...
 

#define API_MSG_VAR_ALLOC_DONTFAIL (   name)    API_VAR_ALLOC_DONTFAIL(struct api_msg, MEMP_API_MSG, name)

err_t netconn_accept ( struct netconn conn,
struct netconn **  new_conn 
)

Accept a new connection on a TCP listening netconn.

Parameters
connthe TCP listen netconn
new_connpointer where the new connection is stored
Returns
ERR_OK if a new connection has been received or an error code otherwise

References netconn::acceptmbox, API_EVENT, API_MSG_VAR_ALLOC_DONTFAIL, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ABRT, ERR_ARG, ERR_IS_FATAL, ERR_OK, ERR_TIMEOUT, netconn::last_err, LWIP_ERROR(), lwip_netconn_do_recv(), NETCONN_EVT_RCVMINUS, NETCONN_SET_SAFE_ERR, NULL, netconn::recv_timeout, sys_arch_mbox_fetch(), SYS_ARCH_TIMEOUT, sys_mbox_valid(), and TCPIP_APIMSG_NOERR.

Referenced by ap_task(), and lwip_accept().

err_t netconn_bind ( struct netconn conn,
ip_addr_t addr,
u16_t  port 
)

Bind a netconn to a specific local IP address and port.

Binding one netconn twice might not always be checked correctly!

Parameters
connthe netconn to bind
addrthe local IP address to bind the netconn to (use IP_ADDR_ANY to bind to all addresses)
portthe local port to bind the netconn to (not used for RAW)
Returns
ERR_OK if bound, any other err_t on failure

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, IP_ADDR_ANY, LWIP_ERROR(), lwip_netconn_do_bind(), NETCONN_SET_SAFE_ERR, NULL, and TCPIP_APIMSG.

Referenced by ap_task(), lwip_bind(), and sta_task().

err_t netconn_close ( struct netconn conn)

Close a TCP netconn (doesn't delete it).

Parameters
connthe TCP netconn to close
Returns
ERR_OK if the netconn was closed, any other err_t on error

References netconn_close_shutdown(), and NETCONN_SHUT_RDWR.

Referenced by http_request(), and sta_task().

static err_t netconn_close_shutdown ( struct netconn conn,
u8_t  how 
)
static

Close or shutdown a TCP netconn (doesn't delete it).

Parameters
connthe TCP netconn to close or shutdown
howfully close or only shutdown one side?
Returns
ERR_OK if the netconn was closed, any other err_t on error

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, LWIP_ERROR(), lwip_netconn_do_close(), NETCONN_SET_SAFE_ERR, NULL, and tcpip_apimsg().

Referenced by netconn_close(), and netconn_shutdown().

err_t netconn_connect ( struct netconn conn,
ip_addr_t addr,
u16_t  port 
)

Connect a netconn to a specific remote IP address and port.

Parameters
connthe netconn to connect
addrthe remote IP address to connect to
portthe remote port to connect to (no used for RAW)
Returns
ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, IP_ADDR_ANY, LWIP_ERROR(), lwip_netconn_do_connect(), NETCONN_SET_SAFE_ERR, NETCONN_TCP, NETCONNTYPE_GROUP, NULL, TCPIP_APIMSG, tcpip_apimsg(), and netconn::type.

Referenced by lwip_connect(), and sta_task().

err_t netconn_delete ( struct netconn conn)

Close a netconn 'connection' and free its resources.

UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate after this returns.

Parameters
connthe netconn to delete
Returns
ERR_OK if the connection was deleted

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_OK, lwip_netconn_do_delconn(), netconn_free(), NULL, and tcpip_apimsg().

Referenced by http_request(), lwip_accept(), lwip_close(), lwip_socket(), and sta_task().

err_t netconn_disconnect ( struct netconn conn)

Disconnect a netconn from its current peer (only valid for UDP netconns).

Parameters
connthe netconn to disconnect
Returns
TODO: return value is not set here...

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, LWIP_ERROR(), lwip_netconn_do_disconnect(), NETCONN_SET_SAFE_ERR, NULL, and TCPIP_APIMSG.

Referenced by lwip_connect().

err_t netconn_getaddr ( struct netconn conn,
ip_addr_t addr,
u16_t port,
u8_t  local 
)

Get the local or remote IP address and port of a netconn.

For RAW netconns, this returns the protocol instead of a port!

Parameters
connthe netconn to query
addra pointer to which to save the IP address
porta pointer to which to save the port (or protocol for RAW)
local1 to get the local IP address, 0 to get the remote one
Returns
ERR_CONN for invalid connections ERR_OK if the information was retrieved

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, ip_2_ipX, ipX_2_ip, LWIP_ERROR(), lwip_netconn_do_getaddr(), NETCONN_SET_SAFE_ERR, NULL, and TCPIP_APIMSG.

Referenced by lwip_getaddrname(), and lwip_recvfrom().

err_t netconn_gethostbyname ( const char *  name,
ip_addr_t addr 
)

Execute a DNS query, only one IP address is returned.

Parameters
namea string representation of the DNS host name to query
addra preallocated ip_addr_t where to store the resolved IP address
Returns
ERR_OK: resolving succeeded ERR_MEM: memory error, try again later ERR_ARG: dns client not initialized or invalid hostname ERR_VAL: dns server response was invalid

References API_EXPR_REF, API_VAR_ALLOC, API_VAR_DECLARE, API_VAR_FREE, API_VAR_REF, ERR_ARG, ERR_OK, LWIP_ERROR(), lwip_netconn_do_gethostbyname(), NULL, sys_sem_free(), sys_sem_new(), sys_sem_wait, and tcpip_callback.

Referenced by lwip_getaddrinfo(), lwip_gethostbyname(), and lwip_gethostbyname_r().

err_t netconn_join_leave_group ( struct netconn conn,
ip_addr_t multiaddr,
ip_addr_t netif_addr,
enum netconn_igmp  join_or_leave 
)

Join multicast groups for UDP netconns.

Parameters
connthe UDP netconn for which to change multicast addresses
multiaddrIP address of the multicast group to join or leave
netif_addrthe IP address of the network interface on which to send the igmp message
join_or_leaveflag whether to send a join- or leave-message
Returns
ERR_OK if the action was taken, any err_t on error

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, ip_2_ipX, IP_ADDR_ANY, LWIP_ERROR(), lwip_netconn_do_join_leave_group(), NETCONN_SET_SAFE_ERR, NULL, and TCPIP_APIMSG.

err_t netconn_listen_with_backlog ( struct netconn conn,
u8_t  backlog 
)

Set a TCP netconn into listen mode.

Parameters
connthe tcp netconn to set to listen mode
backlogthe listen backlog, only used if TCP_LISTEN_BACKLOG==1
Returns
ERR_OK if the netconn was set to listen (UDP and RAW netconns don't return any error (yet?))

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, LWIP_ERROR(), lwip_netconn_do_listen(), NETCONN_SET_SAFE_ERR, NULL, and TCPIP_APIMSG.

Referenced by lwip_listen().

struct netconn* netconn_new_with_proto_and_callback ( enum netconn_type  t,
u8_t  proto,
netconn_callback  callback 
)

Create a new netconn (of a specific type) that has a callback function.

The corresponding pcb is also created.

Parameters
tthe type of 'connection' to create (
See Also
enum netconn_type)
Parameters
protothe IP protocol for RAW IP pcbs
callbacka function to call on status changes (RX available, TX'ed)
Returns
a newly allocated struct netconn or NULL on memory error

References netconn::acceptmbox, API_MSG_VAR_ALLOC_DONTFAIL, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_OK, LWIP_ASSERT, lwip_netconn_do_newconn(), memp_free(), netconn_alloc(), NULL, netconn::op_completed, netconn::pcb, netconn::recvmbox, sys_mbox_free(), sys_mbox_valid(), sys_sem_free(), sys_sem_valid(), netconn::tcp, and TCPIP_APIMSG.

Referenced by lwip_socket().

err_t netconn_recv ( struct netconn conn,
struct netbuf **  new_buf 
)

Receive data (in form of a netbuf containing a packet buffer) from a netconn.

Parameters
connthe netconn from which to receive data
new_bufpointer where a new netbuf is stored when received data
Returns
ERR_OK if data has been received, an error code otherwise (timeout, memory error or another error)

References ERR_ARG, ERR_CONN, ERR_MEM, ERR_OK, ipX_addr_set_any, LWIP_ASSERT, LWIP_ERROR(), LWIP_IPV6, memp_free(), memp_malloc(), netconn_recv_data(), NETCONN_SET_SAFE_ERR, NETCONN_TCP, NETCONNTYPE_GROUP, NULL, netconn::recvmbox, sys_mbox_valid(), and netconn::type.

Referenced by http_request(), lwip_ioctl(), lwip_recvfrom(), and sta_task().

static err_t netconn_recv_data ( struct netconn conn,
void **  new_buf 
)
static

Receive data: actual implementation that doesn't care whether pbuf or netbuf is received.

Parameters
connthe netconn from which to receive data
new_bufpointer where a new pbuf/netbuf is stored when received data
Returns
ERR_OK if data has been received, an error code otherwise (timeout, memory error or another error)

References API_EVENT, API_LIB_DEBUG, API_MSG_VAR_ALLOC_DONTFAIL, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, ERR_CLSD, ERR_CONN, ERR_IS_FATAL, ERR_OK, ERR_TIMEOUT, netconn::last_err, LWIP_ASSERT, LWIP_DEBUGF, LWIP_ERROR(), lwip_netconn_do_recv(), NETCONN_EVT_RCVMINUS, netconn_get_noautorecved, NETCONN_SET_SAFE_ERR, NETCONN_TCP, NETCONNTYPE_GROUP, NULL, netconn::recv_timeout, netconn::recvmbox, SYS_ARCH_DEC, sys_arch_mbox_fetch(), SYS_ARCH_TIMEOUT, sys_mbox_valid(), TCPIP_APIMSG_NOERR, netconn::type, and U16_F.

Referenced by netconn_recv(), and netconn_recv_tcp_pbuf().

err_t netconn_recv_tcp_pbuf ( struct netconn conn,
struct pbuf **  new_buf 
)

Receive data (in form of a pbuf) from a TCP netconn.

Parameters
connthe netconn from which to receive data
new_bufpointer where a new pbuf is stored when received data
Returns
ERR_OK if data has been received, an error code otherwise (timeout, memory error or another error) ERR_ARG if conn is not a TCP netconn

References ERR_ARG, LWIP_ERROR(), netconn_recv_data(), NETCONN_TCP, NETCONNTYPE_GROUP, and NULL.

Referenced by lwip_recvfrom().

void netconn_recved ( struct netconn conn,
u32_t  length 
)

TCP: update the receive window: by calling this, the application tells the stack that it has processed data and is able to accept new data.

ATTENTION: use with care, this is mainly used for sockets! Can only be used when calling netconn_set_noautorecved(conn, 1) before.

Parameters
connthe netconn for which to update the receive window
lengthamount of data processed (ATTENTION: this must be accurate!)

References API_MSG_VAR_ALLOC_DONTFAIL, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, lwip_netconn_do_recv(), netconn_get_noautorecved, NETCONN_TCP, NETCONNTYPE_GROUP, NULL, TCPIP_APIMSG_NOERR, and netconn::type.

Referenced by lwip_recvfrom().

err_t netconn_send ( struct netconn conn,
struct netbuf *  buf 
)

Send data over a UDP or RAW netconn (that is already connected).

Parameters
connthe UDP or RAW netconn over which to send data
bufa netbuf containing the data to send
Returns
ERR_OK if data was sent, any other err_t on error

References API_LIB_DEBUG, API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, LWIP_DEBUGF, LWIP_ERROR(), lwip_netconn_do_send(), NETCONN_SET_SAFE_ERR, NULL, TCPIP_APIMSG, and U16_F.

Referenced by lwip_sendto(), and netconn_sendto().

err_t netconn_sendto ( struct netconn conn,
struct netbuf *  buf,
ip_addr_t addr,
u16_t  port 
)

Send data (in form of a netbuf) to a specific remote IP address and port.

Only to be used for UDP and RAW netconns (not TCP).

Parameters
connthe netconn over which to send data
bufa netbuf containing the data to send
addrthe remote IP address to which to send the data
portthe remote port to which to send the data
Returns
ERR_OK if data was sent, any other err_t on error

References ERR_VAL, netconn::ip, ipX_addr_set_ipaddr, netconn_send(), NULL, netconn::pcb, and PCB_ISIPV6.

err_t netconn_shutdown ( struct netconn conn,
u8_t  shut_rx,
u8_t  shut_tx 
)

Shut down one or both sides of a TCP netconn (doesn't delete it).

Parameters
connthe TCP netconn to shut down
Returns
ERR_OK if the netconn was closed, any other err_t on error

References netconn_close_shutdown(), NETCONN_SHUT_RD, and NETCONN_SHUT_WR.

Referenced by lwip_shutdown().

err_t netconn_write_partly ( struct netconn conn,
const void dataptr,
size_t  size,
u8_t  apiflags,
size_t *  bytes_written 
)

Send data over a TCP netconn.

Parameters
connthe TCP netconn over which to send data
dataptrpointer to the application buffer that contains the data to send
sizesize of the application data to send
apiflagscombination of following flags :
  • NETCONN_COPY: data will be copied into memory belonging to the stack
  • NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
  • NETCONN_DONTBLOCK: only write the data if all data can be written at once
bytes_writtenpointer to a location that receives the number of written bytes
Returns
ERR_OK if data was sent, any other err_t on error

References API_MSG_VAR_ALLOC, API_MSG_VAR_DECLARE, API_MSG_VAR_FREE, API_MSG_VAR_REF, ERR_ARG, ERR_OK, ERR_VAL, LWIP_ERROR(), lwip_netconn_do_write(), NETCONN_DONTBLOCK, netconn_is_nonblocking, NETCONN_SET_SAFE_ERR, NETCONN_TCP, NETCONNTYPE_GROUP, NULL, sys_now(), TCPIP_APIMSG, and netconn::type.

Referenced by lwip_send().