19 #elif HAVE_SYS_UNISTD_H 20 #include <sys/unistd.h> 22 #ifdef HAVE_SYS_TYPES_H 23 #include <sys/types.h> 25 #ifdef HAVE_SYS_SOCKET_H 26 #include <sys/socket.h> 28 #ifdef HAVE_NETINET_IN_H 29 #include <netinet/in.h> 31 #ifdef HAVE_ARPA_INET_H 32 #include <arpa/inet.h> 34 #ifdef COAP_EPOLL_SUPPORT 35 #include <sys/epoll.h> 36 #include <sys/timerfd.h> 38 #ifdef HAVE_WS2TCPIP_H 47 #include <lwip/pbuf.h> 49 #include <lwip/timeouts.h> 53 #define min(a,b) ((a) < (b) ? (a) : (b)) 69 #error FRAC_BITS must be less or equal 8 73 #define Q(frac,fval) ((uint16_t)(((1 << (frac)) * fval.integer_part) + \ 74 ((1 << (frac)) * fval.fractional_part + 500)/1000)) 77 #define ACK_RANDOM_FACTOR \ 78 Q(FRAC_BITS, session->ack_random_factor) 81 #define ACK_TIMEOUT Q(FRAC_BITS, session->ack_timeout) 83 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI) 100 #include <lwip/memp.h> 102 static void coap_retransmittimer_execute(
void *arg);
112 memp_free(MEMP_COAP_NODE, node);
118 # define DEBUG DEBUG_PRINT 121 #include "net/ip/uip-debug.h" 123 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN]) 124 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN]) 126 void coap_resources_init();
128 unsigned char initialized = 0;
131 PROCESS(coap_retransmit_process,
"message retransmit process");
146 unsigned int result = 0;
195 if (node->
t < q->
t) {
207 }
while (q && q->
t <= node->
t);
257 memset(node, 0,
sizeof(*node));
288 const uint8_t *hint,
size_t hint_len,
289 uint8_t *identity,
size_t *identity_len,
size_t max_identity_len,
290 uint8_t *psk,
size_t max_psk_len
315 const uint8_t *identity,
size_t identity_len,
316 uint8_t *psk,
size_t max_psk_len
331 uint8_t *hint,
size_t max_hint_len
343 const uint8_t *key,
size_t key_len
352 size_t hint_len = strlen(hint);
355 memcpy(ctx->
psk_hint, hint, hint_len);
368 if (key && key_len > 0) {
371 memcpy(ctx->
psk_key, key, key_len);
390 coap_log(
LOG_ERR,
"coap_context_set_pki: Wrong version of setup_data\n");
414 #ifdef COAP_EPOLL_SUPPORT 415 return context->epfd;
445 coap_resources_init();
448 c = &the_coap_context;
454 #ifdef COAP_EPOLL_SUPPORT 455 c->epfd = epoll_create1(0);
457 coap_log(
LOG_ERR,
"coap_new_context: Unable to epoll_create: %s (%d)\n",
463 c->eptimerfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
464 if (c->eptimerfd == -1) {
465 coap_log(
LOG_ERR,
"coap_new_context: Unable to timerfd_create: %s (%d)\n",
472 struct epoll_event event;
475 memset(&event, 0,
sizeof(event));
476 event.events = EPOLLIN;
478 event.data.ptr = NULL;
480 ret = epoll_ctl(c->epfd, EPOLL_CTL_ADD, c->eptimerfd, &event);
483 "%s: epoll_ctl ADD failed: %s (%d)\n",
506 if (endpoint == NULL) {
511 #if !defined(WITH_LWIP) 521 process_start(&coap_retransmit_process, (
char *)c);
523 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
524 #ifndef WITHOUT_OBSERVE 528 etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
529 PROCESS_CONTEXT_END(&coap_retransmit_process);
563 coap_retransmittimer_restart(context);
585 #ifdef COAP_EPOLL_SUPPORT 586 if (context->eptimerfd != -1) {
588 struct epoll_event event;
591 ret = epoll_ctl(context->epfd, EPOLL_CTL_DEL, context->eptimerfd, &event);
594 "%s: epoll_ctl DEL failed: %s (%d)\n",
598 close(context->eptimerfd);
599 context->eptimerfd = -1;
601 if (context->epfd != -1) {
602 close(context->epfd);
634 if (opt_iter.
type & 0x01) {
636 switch (opt_iter.
type) {
684 ssize_t bytes_written = -1;
686 switch(session->
proto) {
707 return bytes_written;
712 ssize_t bytes_written;
724 bytes_written = coap_socket_send_pdu(sock, session, pdu);
794 coap_log(
LOG_ERR,
"Multicast requests cannot be Confirmable (RFC7252 8.1)\n");
814 return bytes_written;
866 #define FP1 Q(FRAC_BITS, ((coap_fixed_point_t){1,0})) 869 #define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac)) 916 coap_retransmittimer_restart(context);
927 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
928 etimer_set(&context->retransmit_timer, nextpdu->
t);
929 PROCESS_CONTEXT_END(&coap_retransmit_process);
937 #ifdef COAP_EPOLL_SUPPORT 938 if (context->eptimerfd != -1) {
940 if (context->next_timeout == 0 ||
942 struct itimerspec new_value;
946 memset(&new_value, 0,
sizeof(new_value));
949 new_value.it_value.tv_sec = rem_timeout / 1000;
950 new_value.it_value.tv_nsec = (rem_timeout % 1000) * 1000000;
951 ret = timerfd_settime(context->eptimerfd, 0, &new_value, NULL);
954 "%s: timerfd_settime failed: %s (%d)\n",
968 ssize_t bytes_written;
981 if (bytes_written < 0) {
987 (
size_t)bytes_written < pdu->used_size + pdu->
hdr_size) {
1009 node->
id = pdu->
tid;
1011 prng(&r,
sizeof(r));
1022 if (!context || !node)
1027 ssize_t bytes_written;
1042 coap_retransmittimer_restart(context);
1058 if (bytes_written < 0)
1059 return (
int)bytes_written;
1066 #ifndef WITH_CONTIKI 1071 #ifndef WITHOUT_OBSERVE 1124 else if (session->
tls)
1167 ssize_t bytes_written;
1172 switch (session->
proto) {
1191 if (bytes_written > 0)
1194 if (bytes_written > 0)
1206 #if COAP_CONSTRAINED_STACK 1207 static coap_mutex_t s_static_mutex = COAP_MUTEX_INITIALIZER;
1214 #if COAP_CONSTRAINED_STACK 1215 coap_mutex_lock(&s_static_mutex);
1225 if (bytes_read < 0) {
1226 if (bytes_read == -2)
1232 }
else if (bytes_read > 0) {
1241 ssize_t bytes_read = 0;
1246 size_t buf_len =
sizeof(packet->
payload);
1253 if (bytes_read > 0) {
1259 retry = bytes_read == (ssize_t)buf_len;
1260 while (bytes_read > 0) {
1265 size_t n =
min(len, (
size_t)bytes_read);
1285 size_t n =
min(len, (
size_t)bytes_read);
1327 }
while (bytes_read == 0 && retry);
1331 #if COAP_CONSTRAINED_STACK 1332 coap_mutex_unlock(&s_static_mutex);
1338 ssize_t bytes_read = -1;
1340 #if COAP_CONSTRAINED_STACK 1341 static coap_mutex_t e_static_mutex = COAP_MUTEX_INITIALIZER;
1351 #if COAP_CONSTRAINED_STACK 1352 coap_mutex_lock(&e_static_mutex);
1361 if (bytes_read < 0) {
1363 }
else if (bytes_read > 0) {
1373 #if COAP_CONSTRAINED_STACK 1374 coap_mutex_unlock(&e_static_mutex);
1393 return session != NULL;
1398 #ifdef COAP_EPOLL_SUPPORT 1402 "coap_read() requires libcoap not compiled for using epoll\n");
1459 #ifndef COAP_EPOLL_SUPPORT 1464 "coap_io_do_events() requires libcoap compiled for using epoll\n");
1470 for(j = 0; j < nevents; j++) {
1478 (events[j].events & EPOLLIN)) {
1484 (events[j].events & EPOLLOUT)) {
1495 (events[j].events & EPOLLIN)) {
1505 (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1515 (events[j].events & (EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1525 (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1540 else if (ctx->eptimerfd != -1) {
1547 read(ctx->eptimerfd, &count,
sizeof(count));
1558 uint8_t *msg,
size_t msg_len) {
1588 if (!queue || !*queue)
1593 if (session == (*queue)->session &&
id == (*queue)->id) {
1595 *queue = (*queue)->
next;
1597 (*queue)->
t += (*node)->t;
1599 (*node)->next = NULL;
1610 }
while (q && (session != q->
session ||
id != q->
id));
1630 const uint8_t *b,
size_t blen) {
1631 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1673 const uint8_t *token,
size_t token_length) {
1714 while (queue && queue->
session != session && queue->
id !=
id)
1715 queue = queue->
next;
1728 uint16_t opt_type = 0;
1730 #if COAP_ERROR_PHRASE_LENGTH > 0 1735 size += strlen(phrase) + 1;
1757 uint16_t delta = opt_iter.
type - opt_type;
1761 }
else if (delta < 269) {
1771 switch (*option & 0x0f) {
1782 opt_type = opt_iter.
type;
1804 #if COAP_ERROR_PHRASE_LENGTH > 0 1820 unsigned char buf[1];
1834 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4))) 1841 size_t len, wkc_len;
1844 int need_block2 = 0;
1880 offset = block.
num << (block.
szx + 4);
1881 if (block.
szx > 6) {
1886 block.
num = (
unsigned int)(offset >> (block.
szx + 4));
1910 if (block.
szx == 0) {
1912 "coap_wellknown_response: message to small even for szx == 0\n");
1944 "coap_wellknown_response: cannot add Block2 option\n");
1991 #ifndef WITHOUT_OBSERVE 1993 int num_cancelled = 0;
2006 return num_cancelled;
2052 unsigned int val = 0;
2107 if ((resource == NULL) || (resource->
is_unknown == 1)) {
2136 ((
size_t)pdu->
code - 1 <
2198 if ((
size_t)pdu->
code - 1 <
2237 subscription =
coap_add_observer(resource, session, &token, query, has_block2, block2);
2248 h(context, resource, session, pdu, &token, query, response);
2250 if (query && owns_query)
2264 && (response->
code == 0)) {
2272 (response->
type != COAP_MESSAGE_NON ||
2273 (response->
code >= 64
2310 assert(response == NULL);
2381 #ifndef INET6_ADDRSTRLEN 2382 #define INET6_ADDRSTRLEN 40 2397 switch (pdu->
type) {
2490 "coap_dispatch: cannot create error response\n");
2581 #if defined(HAVE_WINSOCK2_H) 2582 WORD wVersionRequested = MAKEWORD(2, 2);
2584 WSAStartup(wVersionRequested, &wsaData);
2595 #if defined(HAVE_WINSOCK2_H) 2600 #if ! defined WITH_CONTIKI && ! defined WITH_LWIP 2603 struct ipv6_mreq mreq;
2604 struct addrinfo *reslocal = NULL, *resmulti = NULL, hints, *ainfo;
2607 int mgroup_setup = 0;
2610 memset(&hints, 0,
sizeof(hints));
2611 hints.ai_family = AF_INET6;
2612 hints.ai_socktype = SOCK_DGRAM;
2614 result = getaddrinfo(
"::", NULL, &hints, &reslocal);
2617 "coap_join_mcast_group: cannot resolve link-local interface: %s\n",
2618 gai_strerror(result));
2623 for (ainfo = reslocal; ainfo != NULL; ainfo = ainfo->ai_next) {
2624 if (ainfo->ai_family == AF_INET6) {
2625 mreq.ipv6mr_interface =
2626 ((
struct sockaddr_in6 *)ainfo->ai_addr)->sin6_scope_id;
2631 memset(&hints, 0,
sizeof(hints));
2632 hints.ai_family = AF_INET6;
2633 hints.ai_socktype = SOCK_DGRAM;
2636 result = getaddrinfo(group_name, NULL, &hints, &resmulti);
2640 "coap_join_mcast_group: cannot resolve multicast address: %s\n",
2641 gai_strerror(result));
2645 for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) {
2646 if (ainfo->ai_family == AF_INET6) {
2647 mreq.ipv6mr_multiaddr =
2648 ((
struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr;
2656 result = setsockopt(endpoint->
sock.
fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
2657 (
char *)&mreq,
sizeof(mreq));
2660 "coap_join_mcast_group: setsockopt: %s: '%s'\n",
2668 if (!mgroup_setup) {
2673 freeaddrinfo(resmulti);
2674 freeaddrinfo(reslocal);
2692 PROCESS_THREAD(coap_retransmit_process, ev, data) {
2702 if (ev == PROCESS_EVENT_TIMER) {
2703 if (etimer_expired(&the_coap_context.retransmit_timer)) {
2708 while (nextpdu && nextpdu->
t <= now) {
2714 etimer_set(&the_coap_context.retransmit_timer,
2715 nextpdu ? nextpdu->
t - now : 0xFFFF);
2717 #ifndef WITHOUT_OBSERVE 2718 if (etimer_expired(&the_coap_context.notify_timer)) {
2720 etimer_reset(&the_coap_context.notify_timer);
2746 static void coap_retransmittimer_execute(
void *arg) {
2752 ctx->timer_configured = 0;
2759 while (nextinqueue != NULL) {
2760 if (nextinqueue->
t > elapsed) {
2761 nextinqueue->
t -= elapsed;
2764 elapsed -= nextinqueue->
t;
2772 coap_retransmittimer_restart(ctx);
2778 if (ctx->timer_configured) {
2779 printf(
"clearing\n");
2780 sys_untimeout(coap_retransmittimer_execute, (
void*)ctx);
2781 ctx->timer_configured = 0;
2803 printf(
"scheduling for %d ticks\n", delay);
2804 sys_timeout(delay, coap_retransmittimer_execute, (
void*)ctx);
2805 ctx->timer_configured = 1;
uint8_t code
request method (value 1–10) or response code (value 40-255)
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
#define LL_FOREACH(head, el)
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
int coap_dtls_hello(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED)
static ssize_t coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
void * coap_get_app_data(const coap_context_t *ctx)
Returns any application-specific data that has been stored with context using the function coap_set_a...
#define COAP_OPTION_IF_MATCH
coap_address_t remote
remote address and port
coap_tid_t last_ping_mid
the last keepalive message id that was used in this session
static int coap_accept_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
uint8_t con_active
Active CON request sent.
COAP_STATIC_INLINE int coap_option_clrb(coap_opt_filter_t filter, uint16_t type)
Clears the corresponding bit for type in filter.
#define COAP_SIGNALING_PING
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
coap_tid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
#define COAP_SIGNALING_CSM
int coap_context_get_coap_fd(coap_context_t *context)
Get the libcoap internal file descriptor for using in an application's select() or returned as an eve...
#define COAP_RESPONSE_CODE(N)
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
coap_ping_handler_t ping_handler
#define COAP_DEFAULT_URI_WELLKNOWN
well-known resources URI
struct coap_context_t * context
session's context
#define COAP_OPTION_PROXY_URI
#define COAP_OPTION_CONTENT_FORMAT
COAP_STATIC_INLINE void coap_free_node(coap_queue_t *node)
coap_session_t * coap_new_server_session(struct coap_context_t *ctx, coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_session_t *session, coap_tid_t id)
Retrieves transaction from the queue.
struct coap_dtls_context_t * coap_dtls_new_context(struct coap_context_t *coap_context)
Creates a new DTLS context for the given coap_context.
static void coap_connect_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
void * tls
security parameters
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
void coap_packet_get_memmapped(coap_packet_t *packet, unsigned char **address, size_t *length)
Given a packet, set msg and msg_len to an address and length of the packet's data in memory...
coap_endpoint_t * endpoint
the endpoints used for listening
#define COAP_SESSION_STATE_HANDSHAKE
int coap_dtls_receive(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED)
#define COAP_OPTION_NORESPONSE
#define COAP_SOCKET_CONNECTED
the socket is connected
#define COAP_SOCKET_BOUND
the socket is bound
struct coap_session_t * session
subscriber session
multi-purpose address abstraction
#define SHR_FP(val, frac)
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
uint8_t * coap_add_data_after(coap_pdu_t *pdu, size_t len)
Adds given data to the pdu that is passed as first parameter but does not copyt it.
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Marks an observer as alive.
ssize_t coap_tls_read(coap_session_t *session UNUSED, uint8_t *data UNUSED, size_t data_len UNUSED)
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
COAP_STATIC_INLINE int token_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
coap_string_t * coap_get_query(const coap_pdu_t *request)
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
coap_address_t local
local address and port
void * coap_dtls_new_client_session(coap_session_t *session UNUSED)
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Internal function invoked for server.
#define COAP_OPTION_OBSERVE
#define COAP_SOCKET_ERROR
void coap_clock_init(void)
Initializes the internal clock.
int coap_socket_connect_tcp1(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)
int coap_context_set_pki(coap_context_t *ctx, coap_dtls_pki_t *setup_data)
Set the context's default PKI information for a server.
int coap_dtls_is_supported(void)
Returns 1 if support for DTLS is enabled, or 0 otherwise.
int coap_dtls_context_set_pki(coap_context_t *ctx UNUSED, coap_dtls_pki_t *setup_data UNUSED, coap_dtls_role_t role UNUSED)
ssize_t coap_tls_write(coap_session_t *session UNUSED, const uint8_t *data UNUSED, size_t data_len UNUSED)
#define COAP_OPTION_BLOCK1
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
coap_address_t local_if
optional local interface address
int coap_delete_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
Removes any subscription for observer from resource and releases the allocated storage.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_EVENT_TCP_CONNECTED
TCP events for COAP_PROTO_TCP and COAP_PROTO_TLS.
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
int coap_option_filter_set(coap_opt_filter_t filter, uint16_t type)
Sets the corresponding entry for type in filter.
unsigned int is_unknown
resource created for unknown handler
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
#define prng_init(Value)
Called to set the PRNG seed.
coap_nack_handler_t nack_handler
coap_tid_t coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned char type)
Helper funktion to create and send a message with type (usually ACK or RST).
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
size_t(* get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len)
#define COAP_OPTION_CONTENT_TYPE
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
#define COAP_EVENT_TCP_FAILED
static int coap_handle_dgram_for_proto(coap_context_t *ctx, coap_session_t *session, coap_packet_t *packet)
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
ssize_t coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
coap_context_t * coap_new_context(const coap_address_t *listen_addr)
Creates a new coap_context_t object that will hold the CoAP stack status.
struct coap_context_t * context
endpoint's context
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
size_t length
length of string
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
ssize_t(* network_read)(coap_socket_t *sock, struct coap_packet_t *packet)
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
void * coap_tls_new_client_session(coap_session_t *session UNUSED, int *connected UNUSED)
#define COAP_OPTION_PROXY_SCHEME
Abstraction of virtual endpoint that can be attached to coap_context_t.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define COAP_RESOURCE_CHECK_TIME
The interval in seconds to check if resources have changed.
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
const char * coap_socket_strerror(void)
uint16_t type
decoded option type
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
struct coap_endpoint_t * endpoint
#define COAP_SESSION_STATE_ESTABLISHED
#define SZX_TO_BYTES(SZX)
coap_pong_handler_t pong_handler
coap_tid_t id
CoAP transaction id.
unsigned int max_retransmit
maximum re-transmit count (default 4)
#define SESSIONS_ITER_SAFE(e, el, rtmp)
int coap_dtls_send(struct coap_context_t *coap_context, struct coap_dtls_session_t *session, const coap_pdu_t *pdu)
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
int coap_context_set_pki_root_cas(coap_context_t *ctx, const char *ca_file, const char *ca_dir)
Set the context's default Root CA information for a client or server.
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Coap string data definition.
coap_pdu_t * coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts)
Creates a new ACK PDU with specified error code.
Coap string data definition with const data.
coap_pdu_t * pdu
the CoAP PDU to send
coap_tick_t t
when to send PDU for the next time
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
coap_opt_t * coap_check_option(coap_pdu_t *pdu, uint16_t type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
static int coap_write_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
#define FRAC_BITS
The number of bits for the fractional part of ACK_TIMEOUT and ACK_RANDOM_FACTOR.
#define ACK_TIMEOUT
creates a Qx.FRAC_BITS from session's 'ack_timeout'
#define COAP_PDU_IS_RESPONSE(pdu)
#define COAP_INVALID_TID
Indicates an invalid transaction id.
size_t token_length
actual length of token
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now)
#define RESOURCES_ITER(r, tmp)
#define COAP_SIGNALING_PONG
static coap_str_const_t coap_default_uri_wellknown
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r)
Calculates the initial timeout based on the session CoAP transmission parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
int coap_option_filter_get(coap_opt_filter_t filter, uint16_t type)
Checks if type is contained in filter.
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by variable t in node.
int coap_can_exit(coap_context_t *context)
Returns 1 if there are no messages to send or to dispatch in the context's queues.
static int coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
uint16_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
ssize_t(* network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
struct coap_queue_t * delayqueue
list of delayed messages waiting to be sent
static size_t coap_get_session_client_psk(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len)
coap_proto_t proto
protocol used
#define COAP_MAX_BLOCK_SZX
The largest value for the SZX component in a Block option.
coap_response_handler_t response_handler
#define COAP_SIGNALING_OPTION_CUSTODY
#define COAP_OPTION_URI_PORT
unsigned int observable
can be observed
#define COAP_PDU_IS_EMPTY(pdu)
coap_addr_tuple_t addr_info
key: remote/local address info
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
int coap_context_set_psk(coap_context_t *ctx, const char *hint, const uint8_t *key, size_t key_len)
Set the context's default PSK hint and/or key for a server.
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
coap_pdu_t * partial_pdu
incomplete incoming pdu
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
size_t used_size
used bytes of storage for token, options and payload
size_t alloc_size
allocated storage for token, options and payload
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
uint8_t * token
first byte of token, if any, or options
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
struct coap_queue_t * next
coap_socket_t sock
socket object for the session, if any
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
#define COAP_RESPONSE_CLASS(C)
coap_event_handler_t handle_event
Callback function that is used to signal events to the application.
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
#define COAP_OPTION_IF_NONE_MATCH
#define COAPS_DEFAULT_PORT
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *msg, size_t msg_len)
Parses and interprets a CoAP datagram with context ctx.
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET request indicates that the observe relationship for (sender ad...
Iterator to run through PDU options.
int coap_dtls_context_set_psk(coap_context_t *ctx UNUSED, const char *hint UNUSED, coap_dtls_role_t role UNUSED)
coap_proto_t proto
protocol used on this interface
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
unsigned int timeout
the randomized timeout value
#define coap_mcast_interface(Local)
Coap binary data definition.
int coap_join_mcast_group(coap_context_t *ctx, const char *group_name)
Function interface for joining a multicast group for listening.
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
coap_session_type_t type
client or server side socket
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
uint64_t coap_ticks_to_rt_us(coap_tick_t t)
Helper function that converts coap ticks to POSIX wallclock time in us.
static void handle_response(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd)
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server...
coap_session_state_t state
current state of relationaship with peer
uint16_t tid
transaction id, if any, in regular host byte order
size_t coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Adds option of given type to pdu that is passed as first parameter.
#define COAP_REQUEST_DELETE
coap_pdu_t * coap_wellknown_response(coap_context_t *context, coap_session_t *session, coap_pdu_t *request)
Creates a new response for given request with the contents of .well-known/core.
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
int coap_write_block_opt(coap_block_t *block, uint16_t type, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type type to message pdu.
COAP_STATIC_INLINE void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, const uint8_t *token, size_t token_length)
Cancels all outstanding messages for session session that have the specified token.
#define COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
unsigned int coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now)
Any now timed out delayed packet is transmitted, along with any packets associated with requested obs...
#define COAP_OPTION_SIZE2
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_tid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
#define COAP_STATIC_INLINE
size_t(* get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
#define COAP_DEFAULT_PORT
coap_tid_t coap_send_ack(coap_session_t *session, coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
#define COAP_PDU_IS_REQUEST(pdu)
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue ...
size_t length
length of string
#define COAP_EVENT_DTLS_ERROR
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
void coap_io_do_events(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
static size_t coap_get_context_server_hint(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
#define COAP_EVENT_DTLS_CONNECTED
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
#define COAP_OPTION_BLOCK2
unsigned int coap_event_t
Scalar type to represent different events, e.g.
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for stream data transmission.
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Structure of Block options.
const uint8_t * s
string data
unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int len)
Decodes multiple-length byte sequences.
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
static void coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
struct coap_resource_t * unknown_resource
can be used for handling unknown resources
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
int coap_pdu_parse(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
COAP_STATIC_INLINE coap_queue_t * coap_malloc_node(void)
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define LL_FOREACH_SAFE(head, el, tmp)
#define COAP_PROTO_RELIABLE(p)
#define COAP_SESSION_STATE_CONNECTING
int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown)
Verifies that pdu contains no unknown critical options.
The structure used for defining the PKI setup data to be used.
coap_resource_t * coap_get_resource_from_uri_path(coap_context_t *context, coap_str_const_t *uri_path)
Returns the resource identified by the unique string uri_path.
int coap_dtls_context_set_pki_root_cas(struct coap_context_t *ctx UNUSED, const char *ca_file UNUSED, const char *ca_path UNUSED)
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
coap_subscription_t * coap_add_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token, coap_string_t *query, int has_block2, coap_block_t block2)
Adds the specified peer as observer for resource.
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
void coap_read(coap_context_t *ctx, coap_tick_t now)
For applications with their own message loop, reads all data from the network.
#define COAP_OPTION_URI_PATH
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
static enum respond_t no_response(coap_pdu_t *request, coap_pdu_t *response)
Checks for No-Response option in given request and returns 1 if response should be suppressed accordi...
size_t(* get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len)
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.
int coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block)
Initializes block from pdu.
#define ACK_RANDOM_FACTOR
creates a Qx.FRAC_BITS from session's 'ack_random_factor'
#define LL_DELETE(head, del)
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
#define COAP_SET_STR(st, l, v)
#define COAP_PRINT_STATUS_ERROR
static size_t coap_get_context_server_psk(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len)
#define COAP_PROTO_NOT_RELIABLE(p)
coap_log_t coap_get_log_level(void)
Get the current logging level.
int64_t coap_tick_diff_t
This data type is used to represent the difference between two clock_tick_t values.
static void coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
struct coap_session_t * sessions
hash table or list of active sessions
uint8_t hdr_size
actaul size used for protocol-specific header
#define COAP_OPTION_URI_QUERY
void * app
application-specific data
void coap_dtls_free_context(struct coap_dtls_context_t *dtls_context)
Releases the storage allocated for dtls_context.
coap_socket_flags_t flags
#define COAP_SOCKET_CAN_WRITE
non blocking socket can now write without blocking
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.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
coap_session_t * sessions
client sessions
void coap_set_app_data(coap_context_t *ctx, void *app_data)
Stores data with the given CoAP context.
#define coap_log(level,...)
Logging function.
coap_addr_tuple_t addr_info
local and remote addresses
coap_tid_t coap_send_error(coap_session_t *session, coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts)
Sends an error response with code code for request request to dst.
#define COAP_OPTION_ACCEPT
COAP_STATIC_INLINE size_t get_wkc_len(coap_context_t *context, coap_opt_t *query_filter)
Quick hack to determine the size of the resource description for .well-known/core.
coap_print_status_t coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen, size_t offset, coap_opt_t *query_filter)
Prints the names of all known resources to buf.
coap_socket_t sock
socket object for the interface, if any
#define COAP_SIGNALING_RELEASE
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
coap_pdu_t * coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size...
void coap_memory_init(void)
Initializes libcoap's memory management.
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
#define COAP_PDU_IS_SIGNALING(pdu)
coap_session_t * session
the CoAP session
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
unsigned char token[8]
token used for subscription
coap_str_const_t * uri_path
Request URI Path for this resource.
ssize_t coap_network_read(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
size_t length
length of binary 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.
void coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_nack_reason_t reason)
Cancels all outstanding messages for session session.
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
struct coap_endpoint_t * endpoint
session's endpoint
void coap_free_endpoint(coap_endpoint_t *ep)
#define prng(Buf, Length)
Fills Buf with Length bytes of random data.
static void handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
uint8_t token_length
length of Token
static int coap_cancel(coap_context_t *context, const coap_queue_t *sent)
This function cancels outstanding messages for the session and token specified in sent...
#define COAP_SOCKET_CAN_CONNECT
non blocking client socket can now connect without blocking
#define MAX_BITS
The maximum number of bits for fixed point integers that are used for retransmission time calculation...
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, coap_session_t *, coap_pdu_t *, coap_binary_t *, coap_string_t *, coap_pdu_t *)
Definition of message handler function (.
#define COAP_SESSION_STATE_NONE
coap_session_state_t values
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list...
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
uint8_t read_header[8]
storage space for header of incoming message header
#define COAP_OPTION_URI_HOST
#define SESSIONS_ITER(e, el, rtmp)
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
#define COAP_SIGNALING_ABORT
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH...
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
coap_address_t bind_addr
local interface address
respond_t
Internal flags to control the treatment of responses (specifically in presence of the No-Response opt...
The CoAP stack's global state is stored in a coap_context_t object.
uint16_t tid
transaction id, if any, in regular host byte order
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
struct coap_resource_t * resources
hash table or list of known resources
struct coap_session_t * session
Pulls together all the internal only header files.
size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, size_t length)
Parses data to extract the message size.
static void handle_signaling(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
Set sendqueue_basetime in the given context object ctx to now.
#define COAP_SESSION_STATE_CSM
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
unsigned int m
1 if more blocks follow, 0 otherwise
unsigned int szx
block size
unsigned int num
block number
coap_opt_filter_t known_options
#define COAP_DROPPED_RESPONSE
Indicates that a response is suppressed.