Microchip® Advanced Software Framework

api.h File Reference
#include "lwip/opt.h"
#include <stddef.h>
#include "lwip/netbuf.h"
#include "lwip/sys.h"
#include "lwip/ip_addr.h"
#include "lwip/err.h"

Data Structures

struct  netconn
 A netconn descriptor. More...
 

Macros

#define API_EVENT(c, e, l)
 Register an Network connection event. More...
 
#define netconn_addr(c, i, p)   netconn_getaddr(c,i,p,1)
 
#define NETCONN_COPY   0x01
 
#define NETCONN_DONTBLOCK   0x04
 
#define netconn_err(conn)   ((conn)->last_err)
 
#define NETCONN_FLAG_CHECK_WRITESPACE   0x10
 If a nonblocking write has been rejected before, poll_tcp needs to check if the netconn is writable again. More...
 
#define NETCONN_FLAG_IN_NONBLOCKING_CONNECT   0x04
 Was the last connect action a non-blocking one? More...
 
#define NETCONN_FLAG_NO_AUTO_RECVED   0x08
 If this is set, a TCP netconn must call netconn_recved() to update the TCP receive window (done automatically if not set). More...
 
#define NETCONN_FLAG_NON_BLOCKING   0x02
 Should this netconn avoid blocking? More...
 
#define NETCONN_FLAG_WRITE_DELAYED   0x01
 TCP: when data passed to netconn_write doesn't fit into the send buffer, this temporarily stores whether to wake up the original application task if data couldn't be sent in the first try. More...
 
#define netconn_get_noautorecved(conn)   (((conn)->flags & NETCONN_FLAG_NO_AUTO_RECVED) != 0)
 TCP: Get the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED) More...
 
#define netconn_is_nonblocking(conn)   (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)
 Get the blocking status of netconn calls (. More...
 
#define netconn_listen(conn)   netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)
 
#define NETCONN_MORE   0x02
 
#define netconn_new(t)   netconn_new_with_proto_and_callback(t, 0, NULL)
 
#define netconn_new_with_callback(t, c)   netconn_new_with_proto_and_callback(t, 0, c)
 
#define NETCONN_NOCOPY   0x00 /* Only for source code compatibility */
 
#define NETCONN_NOFLAG   0x00
 
#define netconn_peer(c, i, p)   netconn_getaddr(c,i,p,0)
 
#define netconn_recv_bufsize(conn)   ((conn)->recv_bufsize)
 
#define netconn_set_noautorecved(conn, val)
 TCP: Set the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED) More...
 
#define netconn_set_nonblocking(conn, val)
 Set the blocking status of netconn calls (. More...
 
#define NETCONN_SET_SAFE_ERR(conn, err)
 Set conn->last_err to err but don't overwrite fatal errors. More...
 
#define netconn_type(conn)   (conn->type)
 Get the type of a netconn (as enum netconn_type). More...
 
#define netconn_write(conn, dataptr, size, apiflags)   netconn_write_partly(conn, dataptr, size, apiflags, NULL)
 
#define NETCONNTYPE_DATAGRAM(t)   (t&0xE0)
 
#define NETCONNTYPE_GROUP(t)   (t&0xF0)
 

Typedefs

typedef void(* netconn_callback )(struct netconn *, enum netconn_evt, u16_t len)
 A callback prototype to inform about events for a netconn. More...
 

Enumerations

enum  netconn_evt {
  NETCONN_EVT_RCVPLUS,
  NETCONN_EVT_RCVMINUS,
  NETCONN_EVT_SENDPLUS,
  NETCONN_EVT_SENDMINUS,
  NETCONN_EVT_ERROR
}
 Use to inform the callback function about changes. More...
 
enum  netconn_state {
  NETCONN_NONE,
  NETCONN_WRITE,
  NETCONN_LISTEN,
  NETCONN_CONNECT,
  NETCONN_CLOSE
}
 Current state of the netconn. More...
 
enum  netconn_type {
  NETCONN_INVALID = 0,
  NETCONN_TCP = 0x10,
  NETCONN_UDP = 0x20,
  NETCONN_UDPLITE = 0x21,
  NETCONN_UDPNOCHKSUM = 0x22,
  NETCONN_RAW = 0x40
}
 Protocol family and type of the netconn. More...
 

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...
 
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_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...
 
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_EVENT (   c,
  e,
 
)
Value:
if (c->callback) { \
(*c->callback)(c, e, l); \
}

Register an Network connection event.

Referenced by accept_function(), do_close_internal(), do_connected(), do_delconn(), do_writemore(), err_tcp(), netconn_accept(), netconn_recv_data(), poll_tcp(), recv_tcp(), recv_udp(), and sent_tcp().

#define netconn_addr (   c,
  i,
 
)    netconn_getaddr(c,i,p,1)
#define NETCONN_COPY   0x01
#define NETCONN_DONTBLOCK   0x04
#define netconn_err (   conn)    ((conn)->last_err)
#define NETCONN_FLAG_CHECK_WRITESPACE   0x10

If a nonblocking write has been rejected before, poll_tcp needs to check if the netconn is writable again.

Referenced by do_writemore(), poll_tcp(), and sent_tcp().

#define NETCONN_FLAG_IN_NONBLOCKING_CONNECT   0x04

Was the last connect action a non-blocking one?

#define NETCONN_FLAG_NO_AUTO_RECVED   0x08

If this is set, a TCP netconn must call netconn_recved() to update the TCP receive window (done automatically if not set).

#define NETCONN_FLAG_NON_BLOCKING   0x02

Should this netconn avoid blocking?

#define NETCONN_FLAG_WRITE_DELAYED   0x01

TCP: when data passed to netconn_write doesn't fit into the send buffer, this temporarily stores whether to wake up the original application task if data couldn't be sent in the first try.

Referenced by do_write(), and do_writemore().

#define netconn_get_noautorecved (   conn)    (((conn)->flags & NETCONN_FLAG_NO_AUTO_RECVED) != 0)

TCP: Get the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED)

Referenced by netconn_recv_data(), and netconn_recved().

#define netconn_is_nonblocking (   conn)    (((conn)->flags & NETCONN_FLAG_NON_BLOCKING) != 0)

Get the blocking status of netconn calls (.

Todo:
: write/send is missing)

Referenced by do_connect(), do_writemore(), and netconn_write_partly().

#define netconn_listen (   conn)    netconn_listen_with_backlog(conn, TCP_DEFAULT_LISTEN_BACKLOG)

Referenced by http_task().

#define NETCONN_MORE   0x02
#define netconn_new (   t)    netconn_new_with_proto_and_callback(t, 0, NULL)

Referenced by http_task().

#define netconn_new_with_callback (   t,
 
)    netconn_new_with_proto_and_callback(t, 0, c)
#define NETCONN_NOCOPY   0x00 /* Only for source code compatibility */
#define NETCONN_NOFLAG   0x00
#define netconn_peer (   c,
  i,
 
)    netconn_getaddr(c,i,p,0)
#define netconn_recv_bufsize (   conn)    ((conn)->recv_bufsize)
#define netconn_set_noautorecved (   conn,
  val 
)
Value:
do { if(val) { \
} else { \
(conn)->flags &= ~ NETCONN_FLAG_NO_AUTO_RECVED; }} while(0)
#define NETCONN_FLAG_NO_AUTO_RECVED
If this is set, a TCP netconn must call netconn_recved() to update the TCP receive window (done autom...
Definition: api.h:72
static u8_t flags
Definition: tcp_in.c:66

TCP: Set the no-auto-recved status of netconn calls (see NETCONN_FLAG_NO_AUTO_RECVED)

#define netconn_set_nonblocking (   conn,
  val 
)
Value:
do { if(val) { \
} else { \
(conn)->flags &= ~ NETCONN_FLAG_NON_BLOCKING; }} while(0)
#define NETCONN_FLAG_NON_BLOCKING
Should this netconn avoid blocking?
Definition: api.h:67
static u8_t flags
Definition: tcp_in.c:66

Set the blocking status of netconn calls (.

Todo:
: write/send is missing)
#define NETCONN_SET_SAFE_ERR (   conn,
  err 
)
Value:
do { \
if (!ERR_IS_FATAL((conn)->last_err)) { \
(conn)->last_err = err; \
} \
} while(0);
#define ERR_IS_FATAL(e)
Definition: err.h:63
if(timeout==NULL)
Definition: lwip_timers_141.c:274
void * SYS_ARCH_DECL_PROTECT(old_level)
Get an element from a specific pool.
SYS_ARCH_UNPROTECT(old_level)
SYS_ARCH_PROTECT(old_level)

Set conn->last_err to err but don't overwrite fatal errors.

Referenced by do_connected(), netconn_accept(), netconn_bind(), netconn_close_shutdown(), netconn_connect(), netconn_disconnect(), netconn_getaddr(), netconn_listen_with_backlog(), netconn_recv(), netconn_recv_data(), netconn_send(), netconn_write_partly(), and recv_tcp().

#define netconn_type (   conn)    (conn->type)

Get the type of a netconn (as enum netconn_type).

#define netconn_write (   conn,
  dataptr,
  size,
  apiflags 
)    netconn_write_partly(conn, dataptr, size, apiflags, NULL)
#define NETCONNTYPE_DATAGRAM (   t)    (t&0xE0)
#define NETCONNTYPE_GROUP (   t)    (t&0xF0)

typedef void(* netconn_callback)(struct netconn *, enum netconn_evt, u16_t len)

A callback prototype to inform about events for a netconn.

Use to inform the callback function about changes.

Enumerator
NETCONN_EVT_RCVPLUS 
NETCONN_EVT_RCVMINUS 
NETCONN_EVT_SENDPLUS 
NETCONN_EVT_SENDMINUS 
NETCONN_EVT_ERROR 

Current state of the netconn.

Non-TCP netconns are always in state NETCONN_NONE!

Enumerator
NETCONN_NONE 
NETCONN_WRITE 
NETCONN_LISTEN 
NETCONN_CONNECT 
NETCONN_CLOSE 

Protocol family and type of the netconn.

Enumerator
NETCONN_INVALID 
NETCONN_TCP 
NETCONN_UDP 
NETCONN_UDPLITE 
NETCONN_UDPNOCHKSUM 
NETCONN_RAW 

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_msg::conn, do_recv(), ERR_ABRT, ERR_ARG, ERR_IS_FATAL, ERR_OK, ERR_TIMEOUT, api_msg::function, netconn::last_err, LWIP_ERROR(), LWIP_UNUSED_ARG, api_msg::msg, NETCONN_EVT_RCVMINUS, NETCONN_SET_SAFE_ERR, NULL, sys_arch_mbox_fetch(), SYS_ARCH_TIMEOUT, sys_mbox_valid(), and TCPIP_APIMSG.

Referenced by http_task().

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_msg::bc, api_msg_msg::conn, do_bind(), ERR_ARG, api_msg::function, LWIP_ERROR(), api_msg_msg::msg, api_msg::msg, NETCONN_SET_SAFE_ERR, NULL, and TCPIP_APIMSG.

Referenced by http_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 http_task().

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_msg::bc, api_msg_msg::conn, do_connect(), ERR_ARG, api_msg::function, LWIP_ERROR(), api_msg_msg::msg, api_msg::msg, NETCONN_SET_SAFE_ERR, NULL, and tcpip_apimsg().

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_msg::conn, do_delconn(), ERR_OK, api_msg::function, api_msg::msg, netconn_free(), NULL, and tcpip_apimsg().

Referenced by http_request(), and http_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_msg::conn, do_disconnect(), ERR_ARG, api_msg::function, LWIP_ERROR(), api_msg::msg, NETCONN_SET_SAFE_ERR, NULL, and TCPIP_APIMSG.

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_msg::ad, api_msg_msg::conn, do_getaddr(), ERR_ARG, api_msg::function, LWIP_ERROR(), api_msg_msg::msg, api_msg::msg, 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_msg::conn, do_listen(), ERR_ARG, api_msg::function, LWIP_ERROR(), LWIP_UNUSED_ARG, api_msg_msg::msg, api_msg::msg, NETCONN_SET_SAFE_ERR, NULL, and TCPIP_APIMSG.

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 api_msg_msg::conn, do_newconn(), ERR_OK, api_msg::function, LWIP_ASSERT, memp_free(), api_msg_msg::msg, api_msg::msg, api_msg_msg::n, netconn_alloc(), NULL, sys_mbox_free(), sys_mbox_valid(), sys_sem_free(), sys_sem_valid(), and TCPIP_APIMSG.

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 netbuf::addr, ERR_ARG, ERR_CONN, ERR_MEM, ERR_OK, ip_addr_set_any, LWIP_ASSERT, LWIP_ERROR(), memp_free(), memp_malloc(), netconn_recv_data(), NETCONN_SET_SAFE_ERR, NETCONN_TCP, NULL, netbuf::p, netbuf::port, netbuf::ptr, netconn::recvmbox, sys_mbox_valid(), and netconn::type.

Referenced by http_request().

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, and NULL.

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_msg::conn, do_recv(), api_msg::function, LWIP_UNUSED_ARG, api_msg_msg::msg, api_msg::msg, netconn_get_noautorecved, NETCONN_TCP, NULL, api_msg_msg::r, TCPIP_APIMSG, and netconn::type.

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_msg::b, api_msg_msg::conn, do_send(), ERR_ARG, api_msg::function, LWIP_DEBUGF, LWIP_ERROR(), api_msg_msg::msg, api_msg::msg, NETCONN_SET_SAFE_ERR, NULL, netbuf::p, TCPIP_APIMSG, pbuf::tot_len, and U16_F.

Referenced by 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 netbuf::addr, ERR_VAL, ip_addr_set, netconn_send(), NULL, and netbuf::port.

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.

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 dat 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_msg::conn, do_write(), ERR_ARG, ERR_OK, ERR_VAL, api_msg::function, LWIP_ERROR(), api_msg_msg::msg, api_msg::msg, NETCONN_DONTBLOCK, netconn_is_nonblocking, NETCONN_SET_SAFE_ERR, NETCONN_TCP, NULL, sys_now(), TCPIP_APIMSG, netconn::type, and api_msg_msg::w.