22#include <lwip/timeouts.h>
23#include <lwip/tcpip.h>
30extern sys_sem_t coap_io_timeout_sem;
44#if COAP_CLIENT_SUPPORT
54coap_recvc(
void *arg,
struct udp_pcb *upcb,
struct pbuf *p,
55 const ip_addr_t *addr, u16_t port) {
84 pdu = coap_pdu_from_pbuf(p);
97 coap_log_warn(
"coap_handle_dgram: cannot create error response\n");
100 coap_log_warn(
"coap_handle_dgram: error sending response\n");
105 sys_sem_signal(&coap_io_timeout_sem);
115 sys_sem_signal(&coap_io_timeout_sem);
131 sys_sem_signal(&coap_io_timeout_sem);
137#if COAP_SERVER_SUPPORT
153coap_udp_recvs(
void *arg,
struct udp_pcb *upcb,
struct pbuf *p,
154 const ip_addr_t *addr, u16_t port) {
164 goto error_free_pbuf;
171 LWIP_ASSERT(
"Insufficient coap_packet_t resources.", packet != NULL);
180 packet->
ifindex = netif_get_index(ip_current_netif());
187 goto error_free_pbuf;
196 else if (session->
tls)
204 pdu = coap_pdu_from_pbuf(p);
217 coap_log_warn(
"coap_handle_dgram: cannot create error response\n");
220 coap_log_warn(
"coap_handle_dgram: error sending response\n");
231 coap_free_packet(packet);
234 sys_sem_signal(&coap_io_timeout_sem);
250 coap_free_packet(packet);
253 sys_sem_signal(&coap_io_timeout_sem);
270 pdu->pbuf->payload));
274 pbuf = pbuf_clone(PBUF_TRANSPORT, PBUF_RAM, pdu->pbuf);
293 const uint8_t *data,
size_t data_len) {
298 pbuf = pbuf_alloc(PBUF_TRANSPORT, data_len, PBUF_RAM);
301 memcpy(pbuf->payload, data, data_len);
304 UNLOCK_TCPIP_CORE();
return -1);
313 if (err == ERR_RTE) {
326#if COAP_SERVER_SUPPORT
334 sock->udp_pcb = udp_new_ip_type(IPADDR_TYPE_ANY);
335 if (sock->udp_pcb == NULL)
338#if LWIP_IPV6 && LWIP_IPV4
339 if (l_listen.
addr.type == IPADDR_TYPE_V6)
340 l_listen.
addr.type = IPADDR_TYPE_ANY;
342 udp_recv(sock->udp_pcb, coap_udp_recvs, (
void *)sock->
endpoint);
343 err = udp_bind(sock->udp_pcb, &l_listen.
addr, l_listen.port);
345 udp_remove(sock->udp_pcb);
346 sock->udp_pcb = NULL;
348 *bound_addr = l_listen;
353#if COAP_CLIENT_SUPPORT
367 if (connect_addr.port == 0)
368 connect_addr.port = default_port;
380 pcb->local_ip = local_if->
addr;
381 pcb->local_port = local_if->port;
383 err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
385 LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS,
386 (
"coap_socket_connect_udp: port bind failed\n"));
391 local_addr->
addr = pcb->local_ip;
392 local_addr->port = pcb->local_port;
404 err = udp_connect(pcb, &connect_addr.
addr, connect_addr.port);
410#if LWIP_IPV6 && LWIP_IPV4
411 pcb->local_ip.type = pcb->remote_ip.type;
416 udp_recv(sock->udp_pcb, coap_recvc, (
void *)sock->
session);
435 UNLOCK_TCPIP_CORE();
return);
439 udp_remove(sock->udp_pcb);
441 sock->udp_pcb = NULL;
452#pragma GCC diagnostic ignored "-Wunused-function"
void coap_address_set_port(coap_address_t *addr, uint16_t port)
Set the port field of addr to port (in host byte order).
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_debug_send_packet(void)
Check to see whether a packet should be sent or not.
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Library specific build wrapper for coap_internal.h.
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
void coap_dispatch(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
Dispatches the PDUs from the receive queue in given context.
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
coap_pdu_t * coap_new_error_response(const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
Creates a new ACK PDU with specified error code.
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
int coap_socket_connect_udp(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
Create a new UDP socket and 'connect' it to the address tuple.
ssize_t coap_socket_recv(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
ssize_t coap_socket_send(coap_socket_t *sock, coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for data transmission.
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Create a new UDP socket and then listen for new incoming UDP sessions to the specified IP address and...
void coap_socket_dgrm_close(coap_socket_t *sock)
Function interface to close off a datagram socket.
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
int coap_dtls_hello(coap_session_t *coap_session, const uint8_t *data, size_t data_len)
Handling client HELLO messages from a new candiate peer.
@ COAP_EVENT_BAD_PACKET
Triggered when badly formatted packet received.
#define coap_lock_invert(alt_lock, failed)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_warn(...)
void coap_option_filter_clear(coap_opt_filter_t *filter)
Clears filter filter.
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
size_t coap_pdu_parse_header_size(coap_proto_t proto, const uint8_t *data)
Interprets data to determine the number of bytes in the header.
int coap_pdu_parse2(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu, coap_opt_filter_t *error_opts)
Parses data into the CoAP PDU structure given in result.
#define COAP_PDU_IS_REQUEST(pdu)
#define COAP_RESPONSE_CODE(N)
#define COAP_INVALID_MID
Indicates an invalid message id.
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now)
Lookup the server session for the packet received on an endpoint, or create a new one.
#define COAP_PROTO_NOT_RELIABLE(p)
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
coap_address_t remote
remote address and port
coap_address_t local
local address and port
Multi-purpose address abstraction.
union coap_address_t::@0 addr
Abstraction of virtual endpoint that can be attached to coap_context_t.
coap_context_t * context
endpoint's context
coap_proto_t proto
protocol used on this interface
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char * payload
payload
int ifindex
the interface index
size_t used_size
used bytes of storage for token, options and payload
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
void * tls
security parameters
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_session_t * session
Used to determine session owner.
coap_endpoint_t * endpoint
Used by the epoll logic for a listening endpoint.
coap_address_t mcast_addr
remote address and port (multicast track)
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values