Microchip® Advanced Software Framework

api_msg.c File Reference

Sequential API Internal module.

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

Macros

#define IN_NONBLOCKING_CONNECT(conn)   (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)
 
#define SET_NONBLOCKING_CONNECT(conn, val)
 

Functions

static err_t accept_function (void *arg, struct tcp_pcb *newpcb, err_t err)
 Accept callback function for TCP netconns. More...
 
void do_bind (struct api_msg_msg *msg)
 Bind a pcb contained in a netconn Called from netconn_bind. More...
 
void do_close (struct api_msg_msg *msg)
 Close a TCP pcb contained in a netconn Called from netconn_close. More...
 
static void do_close_internal (struct netconn *conn)
 Internal helper function to close a TCP netconn: since this sometimes doesn't work at the first attempt, this function is called from multiple places. More...
 
void do_connect (struct api_msg_msg *msg)
 Connect a pcb contained inside a netconn Called from netconn_connect. More...
 
static err_t do_connected (void *arg, struct tcp_pcb *pcb, err_t err)
 TCP callback function if a connection (opened by tcp_connect/do_connect) has been established (or reset by the remote host). More...
 
void do_delconn (struct api_msg_msg *msg)
 Delete the pcb inside a netconn. More...
 
void do_disconnect (struct api_msg_msg *msg)
 Connect a pcb contained inside a netconn Only used for UDP netconns. More...
 
void do_getaddr (struct api_msg_msg *msg)
 Return a connection's local or remote address Called from netconn_getaddr. More...
 
void do_listen (struct api_msg_msg *msg)
 Set a TCP pcb contained in a netconn into listen mode Called from netconn_listen. More...
 
void do_newconn (struct api_msg_msg *msg)
 Create a new pcb of a specific type inside a netconn. More...
 
void do_recv (struct api_msg_msg *msg)
 Indicate data has been received from a TCP pcb contained in a netconn Called from netconn_recv. More...
 
void do_send (struct api_msg_msg *msg)
 Send some data on a RAW or UDP pcb contained in a netconn Called from netconn_send. More...
 
void do_write (struct api_msg_msg *msg)
 Send some data on a TCP pcb contained in a netconn Called from netconn_write. More...
 
static err_t do_writemore (struct netconn *conn)
 See if more data needs to be written from a previous call to netconn_write. More...
 
static void err_tcp (void *arg, err_t err)
 Error callback function for TCP netconns. More...
 
struct netconnnetconn_alloc (enum netconn_type t, netconn_callback callback)
 Create a new netconn (of a specific type) that has a callback function. More...
 
static void netconn_drain (struct netconn *conn)
 Delete rcvmbox and acceptmbox of a netconn and free the left-over data in these mboxes. More...
 
void netconn_free (struct netconn *conn)
 Delete a netconn and all its resources. More...
 
static void pcb_new (struct api_msg_msg *msg)
 Create a new pcb of a specific type. More...
 
static err_t poll_tcp (void *arg, struct tcp_pcb *pcb)
 Poll callback function for TCP netconns. More...
 
static err_t recv_tcp (void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
 Receive callback function for TCP netconns. More...
 
static void recv_udp (void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
 Receive callback function for UDP netconns. More...
 
static err_t sent_tcp (void *arg, struct tcp_pcb *pcb, u16_t len)
 Sent callback function for TCP netconns. More...
 
static void setup_tcp (struct netconn *conn)
 Setup a tcp_pcb with the correct callback function pointers and their arguments. More...
 

#define IN_NONBLOCKING_CONNECT (   conn)    (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)

Referenced by do_connected(), do_delconn(), and err_tcp().

#define SET_NONBLOCKING_CONNECT (   conn,
  val 
)
Value:
do { if(val) { \
} else { \
(conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0)
static u8_t flags
Definition: tcp_in.c:66
#define NETCONN_FLAG_IN_NONBLOCKING_CONNECT
Was the last connect action a non-blocking one?
Definition: api.h:69

Referenced by do_connect(), do_connected(), and err_tcp().

static err_t accept_function ( void arg,
struct tcp_pcb newpcb,
err_t  err 
)
static

Accept callback function for TCP netconns.

Allocates a new netconn and posts that to conn->acceptmbox.

See Also
tcp.h (struct tcp_pcb_listen.accept) for parameters and return value

References netconn::acceptmbox, API_EVENT, API_MSG_DEBUG, netconn::callback, ERR_MEM, ERR_OK, ERR_VAL, netconn::last_err, LWIP_DEBUGF, netconn_alloc(), NETCONN_EVT_RCVPLUS, netconn_free(), NULL, netconn::pcb, netconn::recvmbox, setup_tcp(), sys_mbox_free(), sys_mbox_set_invalid(), sys_mbox_trypost(), sys_mbox_valid(), netconn::tcp, tcp_arg(), tcp_debug_state_str(), tcp_err(), tcp_poll(), tcp_recv(), tcp_sent(), and netconn::type.

Referenced by do_listen().

void do_bind ( struct api_msg_msg msg)

Bind a pcb contained in a netconn Called from netconn_bind.

Parameters
msgthe api_msg_msg pointing to the connection and containing the IP address and port to bind to

References api_msg_msg::bc, api_msg_msg::conn, api_msg_msg::err, ERR_IS_FATAL, ERR_VAL, netconn::last_err, api_msg_msg::msg, NETCONN_RAW, NETCONN_TCP, NETCONN_UDP, NETCONNTYPE_GROUP, NULL, netconn::pcb, netconn::raw, netconn::tcp, tcp_bind(), TCPIP_APIMSG_ACK, netconn::type, netconn::udp, and udp_bind().

Referenced by netconn_bind().

static void do_close_internal ( struct netconn conn)
static
static err_t do_connected ( void arg,
struct tcp_pcb pcb,
err_t  err 
)
static

TCP callback function if a connection (opened by tcp_connect/do_connect) has been established (or reset by the remote host).

See Also
tcp.h (struct tcp_pcb.connected) for parameters and return values

References API_EVENT, netconn::current_msg, api_msg_msg::err, ERR_OK, ERR_VAL, IN_NONBLOCKING_CONNECT, LWIP_ASSERT, LWIP_UNUSED_ARG, NETCONN_CONNECT, NETCONN_EVT_SENDPLUS, NETCONN_NONE, NETCONN_SET_SAFE_ERR, NETCONN_TCP, NULL, netconn::op_completed, SET_NONBLOCKING_CONNECT, setup_tcp(), netconn::state, sys_sem_signal(), and netconn::type.

Referenced by do_connect().

void do_disconnect ( struct api_msg_msg msg)

Connect a pcb contained inside a netconn Only used for UDP netconns.

Called from netconn_disconnect.

Parameters
msgthe api_msg_msg pointing to the connection to disconnect

References api_msg_msg::conn, api_msg_msg::err, ERR_OK, ERR_VAL, NETCONN_UDP, NETCONNTYPE_GROUP, netconn::pcb, TCPIP_APIMSG_ACK, netconn::type, netconn::udp, and udp_disconnect().

Referenced by netconn_disconnect().

void do_newconn ( struct api_msg_msg msg)

Create a new pcb of a specific type inside a netconn.

Called from netconn_new_with_proto_and_callback.

Parameters
msgthe api_msg_msg describing the connection type

References api_msg_msg::conn, api_msg_msg::err, ERR_OK, NULL, netconn::pcb, pcb_new(), netconn::tcp, and TCPIP_APIMSG_ACK.

Referenced by netconn_new_with_proto_and_callback().

void do_recv ( struct api_msg_msg msg)

Indicate data has been received from a TCP pcb contained in a netconn Called from netconn_recv.

Parameters
msgthe api_msg_msg pointing to the connection

References api_msg_msg::conn, api_msg_msg::err, ERR_OK, LISTEN, api_msg_msg::msg, NETCONN_TCP, NULL, netconn::pcb, api_msg_msg::r, netconn::tcp, tcp_accepted, tcp_recved(), TCPIP_APIMSG_ACK, and netconn::type.

Referenced by netconn_accept(), netconn_recv_data(), and netconn_recved().

void do_send ( struct api_msg_msg msg)
static err_t do_writemore ( struct netconn conn)
static

See if more data needs to be written from a previous call to netconn_write.

Called initially from do_write. If the first call can't send all data (because of low memory or empty send-buffer), this function is called again from sent_tcp() or poll_tcp() to send more data. If all data is sent, the blocking application thread (waiting in netconn_write) is released.

Parameters
connnetconn (that is currently in state NETCONN_WRITE) to process
Returns
ERR_OK ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished

References API_EVENT, netconn::current_msg, api_msg_msg::err, ERR_MEM, ERR_OK, ERR_WOULDBLOCK, netconn::flags, if(), LWIP_ASSERT, api_msg_msg::msg, NETCONN_DONTBLOCK, NETCONN_EVT_SENDMINUS, NETCONN_FLAG_CHECK_WRITESPACE, NETCONN_FLAG_WRITE_DELAYED, netconn_is_nonblocking, NETCONN_NONE, NETCONN_WRITE, NULL, netconn::op_completed, netconn::pcb, netconn::state, sys_now(), sys_sem_signal(), netconn::tcp, tcp_output(), tcp_sndbuf, tcp_sndqueuelen, tcp_write(), TCP_WRITE_FLAG_MORE, api_msg_msg::w, and netconn::write_offset.

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

static void err_tcp ( void arg,
err_t  err 
)
static
struct netconn* netconn_alloc ( enum netconn_type  t,
netconn_callback  callback 
)

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

The corresponding pcb is NOT 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, netconn::callback, netconn::current_msg, DEFAULT_RAW_RECVMBOX_SIZE, DEFAULT_TCP_RECVMBOX_SIZE, ERR_OK, netconn::flags, netconn::last_err, LWIP_ASSERT, memp_free(), memp_malloc(), NETCONN_NONE, NETCONN_RAW, NETCONN_TCP, NETCONN_UDP, NETCONNTYPE_GROUP, NULL, netconn::op_completed, netconn::pcb, netconn::recvmbox, netconn::state, sys_mbox_new(), sys_mbox_set_invalid(), sys_sem_free(), sys_sem_new(), netconn::tcp, netconn::type, and netconn::write_offset.

Referenced by accept_function(), and netconn_new_with_proto_and_callback().

static void netconn_drain ( struct netconn conn)
static

Delete rcvmbox and acceptmbox of a netconn and free the left-over data in these mboxes.

Parameters
connthe netconn to free bytes drained from recvmbox pending connections drained from acceptmbox

References netconn::acceptmbox, netbuf_delete(), netconn_free(), NETCONN_TCP, NULL, pbuf_free(), netconn::pcb, netconn::recvmbox, SYS_MBOX_EMPTY, sys_mbox_free(), sys_mbox_set_invalid(), sys_mbox_tryfetch, sys_mbox_valid(), netconn::tcp, tcp_abort(), tcp_accepted, tcp_recved(), pbuf::tot_len, and netconn::type.

Referenced by do_close(), and do_delconn().

void netconn_free ( struct netconn conn)

Delete a netconn and all its resources.

The pcb is NOT freed (since we might not be in the right thread context do this).

Parameters
connthe netconn to free

References netconn::acceptmbox, LWIP_ASSERT, memp_free(), NULL, netconn::op_completed, netconn::pcb, netconn::recvmbox, sys_mbox_valid(), sys_sem_free(), sys_sem_set_invalid(), and netconn::tcp.

Referenced by accept_function(), netconn_delete(), and netconn_drain().

static void pcb_new ( struct api_msg_msg msg)
static

Create a new pcb of a specific type.

Called from do_newconn().

Parameters
msgthe api_msg_msg describing the connection type
Returns
msg->conn->err, but the return value is currently ignored

References api_msg_msg::conn, api_msg_msg::err, ERR_MEM, ERR_VAL, LWIP_ASSERT, api_msg_msg::msg, api_msg_msg::n, NETCONN_RAW, NETCONN_TCP, NETCONN_UDP, NETCONN_UDPLITE, NETCONN_UDPNOCHKSUM, NETCONNTYPE_GROUP, NULL, netconn::pcb, netconn::raw, recv_udp(), setup_tcp(), netconn::tcp, tcp_new(), netconn::type, netconn::udp, UDP_FLAGS_NOCHKSUM, UDP_FLAGS_UDPLITE, udp_new(), udp_recv(), and udp_setflags.

Referenced by do_newconn().

static err_t poll_tcp ( void arg,
struct tcp_pcb pcb 
)
static

Poll callback function for TCP netconns.

Wakes up an application thread that waits for a connection to close or data to be sent. The application thread then takes the appropriate action to go on.

Signals the conn->sem. netconn_close waits for conn->sem if closing failed.

See Also
tcp.h (struct tcp_pcb.poll) for parameters and return value

References API_EVENT, do_close_internal(), do_writemore(), ERR_OK, netconn::flags, LWIP_ASSERT, LWIP_UNUSED_ARG, NETCONN_CLOSE, NETCONN_EVT_SENDPLUS, NETCONN_FLAG_CHECK_WRITESPACE, NETCONN_WRITE, NULL, netconn::pcb, netconn::state, netconn::tcp, tcp_sndbuf, and tcp_sndqueuelen.

Referenced by do_close_internal(), and setup_tcp().

static err_t recv_tcp ( void arg,
struct tcp_pcb pcb,
struct pbuf p,
err_t  err 
)
static

Receive callback function for TCP netconns.

Posts the packet to conn->recvmbox, but doesn't delete it on errors.

See Also
tcp.h (struct tcp_pcb.recv) for parameters and return value

References API_EVENT, ERR_MEM, ERR_OK, ERR_VAL, LWIP_ASSERT, LWIP_UNUSED_ARG, NETCONN_EVT_RCVPLUS, NETCONN_SET_SAFE_ERR, NULL, pbuf_free(), netconn::pcb, netconn::recvmbox, SYS_ARCH_INC, sys_mbox_trypost(), sys_mbox_valid(), netconn::tcp, tcp_recved(), and pbuf::tot_len.

Referenced by setup_tcp().

static void recv_udp ( void arg,
struct udp_pcb pcb,
struct pbuf p,
ip_addr_t addr,
u16_t  port 
)
static
static err_t sent_tcp ( void arg,
struct tcp_pcb pcb,
u16_t  len 
)
static

Sent callback function for TCP netconns.

Signals the conn->sem and calls API_EVENT. netconn_write waits for conn->sem if send buffer is low.

See Also
tcp.h (struct tcp_pcb.sent) for parameters and return value

References API_EVENT, do_close_internal(), do_writemore(), ERR_OK, netconn::flags, LWIP_ASSERT, LWIP_UNUSED_ARG, NETCONN_CLOSE, NETCONN_EVT_SENDPLUS, NETCONN_FLAG_CHECK_WRITESPACE, NETCONN_WRITE, NULL, netconn::pcb, netconn::state, netconn::tcp, tcp_sndbuf, and tcp_sndqueuelen.

Referenced by do_close_internal(), and setup_tcp().

static void setup_tcp ( struct netconn conn)
static

Setup a tcp_pcb with the correct callback function pointers and their arguments.

Parameters
connthe TCP netconn to setup

References err_tcp(), netconn::pcb, poll_tcp(), recv_tcp(), sent_tcp(), netconn::tcp, tcp_arg(), tcp_err(), tcp_poll(), tcp_recv(), and tcp_sent().

Referenced by accept_function(), do_connect(), do_connected(), and pcb_new().