22 #ifdef HAVE_SYS_UNISTD_H
23 #include <sys/unistd.h>
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 #include <sys/socket.h>
32 #ifdef HAVE_SYS_IOCTL_H
33 #include <sys/ioctl.h>
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
38 #ifdef HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
44 #ifdef COAP_EPOLL_SUPPORT
45 #include <sys/epoll.h>
46 #include <sys/timerfd.h>
48 #ifdef HAVE_WS2TCPIP_H
57 #include <lwip/pbuf.h>
59 #include <lwip/timeouts.h>
62 #ifndef INET6_ADDRSTRLEN
63 #define INET6_ADDRSTRLEN 40
67 #define min(a,b) ((a) < (b) ? (a) : (b))
83 #error FRAC_BITS must be less or equal 8
87 #define Q(frac,fval) ((uint16_t)(((1 << (frac)) * fval.integer_part) + \
88 ((1 << (frac)) * fval.fractional_part + 500)/1000))
91 #define ACK_RANDOM_FACTOR \
92 Q(FRAC_BITS, session->ack_random_factor)
95 #define ACK_TIMEOUT Q(FRAC_BITS, session->ack_timeout)
97 #if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
112 #include <lwip/memp.h>
114 static void coap_retransmittimer_execute(
void *arg);
124 memp_free(MEMP_COAP_NODE, node);
130 # define DEBUG DEBUG_PRINT
133 #include "net/ip/uip-debug.h"
135 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
136 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
138 void coap_resources_init();
140 unsigned char initialized = 0;
143 PROCESS(coap_retransmit_process,
"message retransmit process");
158 unsigned int result = 0;
207 if (node->
t < q->
t) {
219 }
while (q && q->
t <= node->
t);
269 memset(node, 0,
sizeof(*node));
300 const uint8_t *hint,
size_t hint_len,
301 uint8_t *identity,
size_t *identity_len,
size_t max_identity_len,
302 uint8_t *psk,
size_t max_psk_len
336 const uint8_t *identity,
size_t identity_len,
337 uint8_t *psk,
size_t max_psk_len
364 uint8_t *hint,
size_t max_hint_len
378 if (psk_info->
hint.
s &&
390 const uint8_t *key,
size_t key_len
394 memset (&setup_data, 0,
sizeof(setup_data));
400 if (key && key_len > 0) {
428 coap_log(
LOG_ERR,
"coap_context_set_pki: Wrong version of setup_data\n");
453 unsigned int max_idle_sessions) {
464 unsigned int max_handshake_sessions) {
475 unsigned int csm_timeout) {
486 unsigned int session_timeout) {
496 #ifdef COAP_EPOLL_SUPPORT
497 return context->epfd;
527 coap_resources_init();
529 c = &the_coap_context;
535 #ifdef COAP_EPOLL_SUPPORT
536 c->epfd = epoll_create1(0);
538 coap_log(
LOG_ERR,
"coap_new_context: Unable to epoll_create: %s (%d)\n",
544 c->eptimerfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK);
545 if (c->eptimerfd == -1) {
546 coap_log(
LOG_ERR,
"coap_new_context: Unable to timerfd_create: %s (%d)\n",
553 struct epoll_event event;
556 memset(&event, 0,
sizeof(event));
557 event.events = EPOLLIN;
559 event.data.ptr = NULL;
561 ret = epoll_ctl(c->epfd, EPOLL_CTL_ADD, c->eptimerfd, &event);
564 "%s: epoll_ctl ADD failed: %s (%d)\n",
587 if (endpoint == NULL) {
592 #if !defined(WITH_LWIP)
602 process_start(&coap_retransmit_process, (
char *)c);
604 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
607 etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
608 PROCESS_CONTEXT_END(&coap_retransmit_process);
646 coap_retransmittimer_restart(context);
649 #ifndef WITHOUT_ASYNC
652 HASH_ITER(hh, context->
cache, cp, ctmp) {
659 LL_FOREACH_SAFE(context->
endpoint, ep, tmp) {
669 #ifdef COAP_EPOLL_SUPPORT
670 if (context->eptimerfd != -1) {
672 struct epoll_event event;
675 ret = epoll_ctl(context->epfd, EPOLL_CTL_DEL, context->eptimerfd, &event);
678 "%s: epoll_ctl DEL failed: %s (%d)\n",
682 close(context->eptimerfd);
683 context->eptimerfd = -1;
685 if (context->epfd != -1) {
686 close(context->epfd);
717 if (opt_iter.
number & 0x01) {
719 switch (opt_iter.
number) {
767 ssize_t bytes_written = -1;
769 switch(session->
proto) {
779 #if !COAP_DISABLE_TCP
785 #if !COAP_DISABLE_TCP
795 return bytes_written;
800 ssize_t bytes_written;
810 bytes_written = coap_socket_send_pdu(sock, session, pdu);
831 #if !COAP_DISABLE_TCP
878 coap_log(
LOG_ERR,
"Multicast requests cannot be Confirmable (RFC7252 8.1)\n");
898 return bytes_written;
951 #define FP1 Q(FRAC_BITS, ((coap_fixed_point_t){1,0}))
954 #define SHR_FP(val,frac) (((val) + (1 << ((frac) - 1))) >> (frac))
1001 coap_retransmittimer_restart(context);
1012 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
1013 etimer_set(&context->retransmit_timer, nextpdu->
t);
1014 PROCESS_CONTEXT_END(&coap_retransmit_process);
1022 #ifdef COAP_EPOLL_SUPPORT
1023 if (context->eptimerfd != -1) {
1025 if (context->next_timeout == 0 ||
1027 struct itimerspec new_value;
1031 memset(&new_value, 0,
sizeof(new_value));
1034 new_value.it_value.tv_sec = rem_timeout / 1000;
1035 new_value.it_value.tv_nsec = (rem_timeout % 1000) * 1000000;
1036 ret = timerfd_settime(context->eptimerfd, 0, &new_value, NULL);
1039 "%s: timerfd_settime failed: %s (%d)\n",
1052 const uint8_t *b,
size_t blen) {
1053 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1061 int observe_action = -1;
1062 int have_block1 = 0;
1090 if (observe_action != -1 || have_block1 ||
1094 LL_FOREACH(session->
lg_crcv, lg_crcv) {
1108 LL_DELETE(session->
lg_crcv, lg_crcv);
1113 LL_DELETE(session->
lg_crcv, lg_crcv);
1120 if (lg_crcv == NULL)
1122 if (have_block1 && session->
lg_xmit) {
1125 LL_FOREACH(session->
lg_xmit, lg_xmit) {
1144 LL_PREPEND(session->
lg_crcv, lg_crcv);
1156 ssize_t bytes_written;
1168 addr_str[
sizeof(addr_str)-1] =
'\000';
1170 sizeof(addr_str) - 1)) {
1174 if (addr_str[0] ==
'[') {
1175 cp = strchr(addr_str,
']');
1176 if (cp) *cp =
'\000';
1177 if (memcmp(&addr_str[1],
"::ffff:", 7) == 0) {
1186 cp = strchr(addr_str,
':');
1187 if (cp) *cp =
'\000';
1199 if (hop_limit == 1) {
1205 else if (hop_limit < 1 || hop_limit > 255) {
1219 if (pdu->
data && opt == NULL) {
1223 pdu->
data[data_len] =
'\000';
1224 a_match = strstr((
char*)pdu->
data, cp);
1225 if (a_match && (a_match == (
char*)pdu->
data || a_match[-1] ==
' ') &&
1226 ((
size_t)(a_match - (
char*)pdu->
data + len) == data_len ||
1227 a_match[len] ==
' ')) {
1238 if (pdu->
data == NULL) {
1246 (uint8_t *)&hop_limit);
1251 memmove(pdu->
data + len + 1, pdu->
data,
1253 memcpy(pdu->
data, cp, len);
1254 pdu->
data[len] =
' ';
1266 #if !COAP_DISABLE_TCP
1278 "Remote end did not indicate CSM support for BLOCK1 enabled\n");
1282 "Remote end did not indicate CSM support for BLOCK2 enabled\n");
1294 if (bytes_written < 0) {
1299 #if !COAP_DISABLE_TCP
1301 (
size_t)bytes_written < pdu->used_size + pdu->
hdr_size) {
1325 node->
id = pdu->
mid;
1338 if (!context || !node)
1343 ssize_t bytes_written;
1358 coap_retransmittimer_restart(context);
1374 if (bytes_written < 0)
1375 return (
int)bytes_written;
1382 #ifndef WITH_CONTIKI
1438 else if (session->
tls)
1451 #if COAP_DISABLE_TCP
1489 ssize_t bytes_written;
1494 switch (session->
proto) {
1496 #if !COAP_DISABLE_TCP
1505 #if !COAP_DISABLE_TCP
1520 if (bytes_written > 0)
1523 if (bytes_written > 0)
1535 #if COAP_CONSTRAINED_STACK
1536 static coap_mutex_t s_static_mutex = COAP_MUTEX_INITIALIZER;
1543 #if COAP_CONSTRAINED_STACK
1544 coap_mutex_lock(&s_static_mutex);
1554 if (bytes_read < 0) {
1555 if (bytes_read == -2)
1561 }
else if (bytes_read > 0) {
1569 #if !COAP_DISABLE_TCP
1571 ssize_t bytes_read = 0;
1575 uint8_t *buf = packet->
payload;
1576 size_t buf_len =
sizeof(packet->
payload);
1583 if (bytes_read > 0) {
1589 retry = bytes_read == (ssize_t)buf_len;
1590 while (bytes_read > 0) {
1595 size_t n =
min(len, (
size_t)bytes_read);
1615 size_t n =
min(len, (
size_t)bytes_read);
1624 "** %s: incoming PDU length too large (%zu > %lu)\n",
1667 }
while (bytes_read == 0 && retry);
1672 #if COAP_CONSTRAINED_STACK
1673 coap_mutex_unlock(&s_static_mutex);
1679 ssize_t bytes_read = -1;
1681 #if COAP_CONSTRAINED_STACK
1682 static coap_mutex_t e_static_mutex = COAP_MUTEX_INITIALIZER;
1692 #if COAP_CONSTRAINED_STACK
1693 coap_mutex_lock(&e_static_mutex);
1702 if (bytes_read < 0) {
1704 }
else if (bytes_read > 0) {
1714 #if COAP_CONSTRAINED_STACK
1715 coap_mutex_unlock(&e_static_mutex);
1734 return session != NULL;
1739 #ifdef COAP_EPOLL_SUPPORT
1743 "coap_io_do_io() requires libcoap not compiled for using epoll\n");
1748 LL_FOREACH_SAFE(ctx->
endpoint, ep, tmp) {
1800 #ifndef COAP_EPOLL_SUPPORT
1805 "coap_io_do_epoll() requires libcoap compiled for using epoll\n");
1811 for(j = 0; j < nevents; j++) {
1819 (events[j].events & EPOLLIN)) {
1825 (events[j].events & EPOLLOUT)) {
1836 (events[j].events & EPOLLIN)) {
1849 (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1858 (events[j].events & (EPOLLIN|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1864 (events[j].events & (EPOLLOUT|EPOLLERR|EPOLLHUP|EPOLLRDHUP))) {
1877 else if (ctx->eptimerfd != -1) {
1886 if (read(ctx->eptimerfd, &count,
sizeof(count)) == -1) {
1899 uint8_t *msg,
size_t msg_len) {
1938 if (!queue || !*queue)
1943 if (session == (*queue)->session &&
id == (*queue)->id) {
1945 *queue = (*queue)->
next;
1947 (*queue)->
t += (*node)->t;
1949 (*node)->next = NULL;
1960 }
while (q && (session != q->
session ||
id != q->
id));
2017 const uint8_t *token,
size_t token_length) {
2066 #if COAP_ERROR_PHRASE_LENGTH > 0
2073 size += strlen(phrase) + 1;
2108 uint16_t delta = opt_iter.
number - opt_num;
2112 }
else if (delta < 269) {
2122 switch (*option & 0x0f) {
2133 opt_num = opt_iter.
number;
2155 #if COAP_ERROR_PHRASE_LENGTH > 0
2158 coap_add_data(response, (
size_t)strlen(phrase), (
const uint8_t *)phrase);
2171 unsigned char buf[1];
2185 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
2192 size_t len, wkc_len;
2195 int need_block2 = 0;
2231 offset = block.
num << (block.
szx + 4);
2232 if (block.
szx > 6) {
2237 block.
num = (
unsigned int)(offset >> (block.
szx + 4));
2261 if (block.
szx == 0) {
2263 "coap_wellknown_response: message to small even for szx == 0\n");
2295 "coap_wellknown_response: cannot add Block2 option\n");
2343 int num_cancelled = 0;
2356 return num_cancelled;
2415 unsigned int val = 0;
2495 int is_proxy_uri = 0;
2496 int is_proxy_scheme = 0;
2497 int skip_hop_limit_check = 0;
2500 #ifndef WITHOUT_ASYNC
2511 #ifndef WITHOUT_ASYNC
2517 if (async->
delay == 0 || async->
delay > now) {
2542 is_proxy_scheme = 1;
2548 if (is_proxy_scheme || is_proxy_uri) {
2554 is_proxy_scheme ?
"Scheme" :
"Uri");
2558 if (((
size_t)pdu->
code - 1 <
2563 is_proxy_scheme ?
"Scheme" :
"Uri",
2580 memset(&uri, 0,
sizeof(uri));
2598 is_proxy_scheme = 0;
2599 skip_hop_limit_check = 1;
2605 if (!skip_hop_limit_check) {
2613 if (hop_limit == 1) {
2618 else if (hop_limit < 1 || hop_limit > 255) {
2635 if (!is_proxy_uri && !is_proxy_scheme) {
2641 if ((resource == NULL) || (resource->
is_unknown == 1) ||
2673 }
else if (is_proxy_uri || is_proxy_scheme) {
2676 ((
size_t)pdu->
code - 1 <
2738 if ((
size_t)pdu->
code - 1 <
2758 int added_block = 0;
2774 if (block.
num != 0) {
2802 resource, uri_path, observe,
2803 query, h, &added_block)) {
2816 h(resource, session, pdu, query, response);
2842 && (response->
code == 0)) {
2882 assert(response == NULL);
2937 #if !COAP_DISABLE_TCP
3001 switch (pdu->
type) {
3024 if (pdu->
code == 0) {
3074 LL_FOREACH_SAFE(r->subscribers, obs, tmp) {
3105 "coap_dispatch: cannot create error response\n");
3122 #if !COAP_DISABLE_TCP
3181 LL_FOREACH(context->
endpoint, ep) {
3197 #ifndef WITHOUT_ASYNC
3203 LL_FOREACH_SAFE(context->
async_state, async, tmp) {
3204 if (async->
delay <= now) {
3212 if (next_due == 0 || next_due > async->
delay - now)
3213 next_due = async->
delay - now;
3228 #if defined(HAVE_WINSOCK2_H)
3229 WORD wVersionRequested = MAKEWORD(2, 2);
3231 WSAStartup(wVersionRequested, &wsaData);
3243 #if defined(HAVE_WINSOCK2_H)
3278 #if ! defined WITH_CONTIKI && ! defined WITH_LWIP && ! defined RIOT_VERSION
3281 const char *ifname) {
3282 struct ip_mreq mreq4;
3283 struct ipv6_mreq mreq6;
3284 struct addrinfo *resmulti = NULL, hints, *ainfo;
3287 int mgroup_setup = 0;
3295 mreq6.ipv6mr_interface = 0;
3296 mreq4.imr_interface.s_addr = INADDR_ANY;
3298 memset(&hints, 0,
sizeof(hints));
3299 hints.ai_socktype = SOCK_DGRAM;
3302 result = getaddrinfo(group_name, NULL, &hints, &resmulti);
3306 "coap_join_mcast_group_intf: %s: "
3307 "Cannot resolve multicast address: %s\n",
3308 group_name, gai_strerror(result));
3318 #if defined(ESPIDF_VERSION)
3319 struct netif *netif;
3326 for (ainfo = resmulti; ainfo != NULL && !(done_ip4 == 1 && done_ip6 == 1);
3327 ainfo = ainfo->ai_next) {
3328 switch (ainfo->ai_family) {
3333 #if defined(ESPIDF_VERSION)
3334 netif = netif_find(ifname);
3336 mreq6.ipv6mr_interface = netif_get_index(netif);
3339 "coap_join_mcast_group_intf: %s: "
3340 "Cannot get IPv4 address: %s\n",
3343 memset (&ifr, 0,
sizeof(ifr));
3344 strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
3345 ifr.ifr_name[IFNAMSIZ - 1] =
'\000';
3347 #ifdef HAVE_IF_NAMETOINDEX
3348 mreq6.ipv6mr_interface = if_nametoindex(ifr.ifr_name);
3349 if (mreq6.ipv6mr_interface == 0) {
3351 "cannot get interface index for '%s'\n",
3358 "cannot get interface index for '%s': %s\n",
3363 mreq6.ipv6mr_interface = ifr.ifr_ifindex;
3372 #if defined(ESPIDF_VERSION)
3373 netif = netif_find(ifname);
3375 mreq4.imr_interface.s_addr = netif_ip4_addr(netif)->addr;
3378 "coap_join_mcast_group_intf: %s: "
3379 "Cannot get IPv4 address: %s\n",
3386 ip4fd = socket(AF_INET, SOCK_DGRAM, 0);
3389 "coap_join_mcast_group_intf: %s: socket: %s\n",
3393 memset (&ifr, 0,
sizeof(ifr));
3394 strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
3395 ifr.ifr_name[IFNAMSIZ - 1] =
'\000';
3396 result = ioctl(ip4fd, SIOCGIFADDR, &ifr);
3399 "coap_join_mcast_group_intf: %s: "
3400 "Cannot get IPv4 address: %s\n",
3405 mreq4.imr_interface = ((
struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;
3418 for (ainfo = resmulti; ainfo != NULL; ainfo = ainfo->ai_next) {
3419 LL_FOREACH(ctx->
endpoint, endpoint) {
3425 if (ainfo->ai_family == AF_INET6) {
3432 mreq6.ipv6mr_interface =
3436 mreq6.ipv6mr_interface = 0;
3439 gaddr.
addr.
sin6.sin6_family = AF_INET6;
3441 gaddr.
addr.
sin6.sin6_addr = mreq6.ipv6mr_multiaddr =
3442 ((
struct sockaddr_in6 *)ainfo->ai_addr)->sin6_addr;
3443 result = setsockopt(endpoint->
sock.
fd, IPPROTO_IPV6, IPV6_JOIN_GROUP,
3444 (
char *)&mreq6,
sizeof(mreq6));
3446 else if (ainfo->ai_family == AF_INET) {
3456 mreq4.imr_interface.s_addr = INADDR_ANY;
3459 gaddr.
addr.
sin.sin_family = AF_INET;
3461 gaddr.
addr.
sin.sin_addr.s_addr = mreq4.imr_multiaddr.s_addr =
3462 ((
struct sockaddr_in *)ainfo->ai_addr)->sin_addr.s_addr;
3463 result = setsockopt(endpoint->
sock.
fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
3464 (
char *)&mreq4,
sizeof(mreq4));
3472 "coap_join_mcast_group_intf: %s: setsockopt: %s\n",
3478 addr_str[
sizeof(addr_str)-1] =
'\000';
3480 sizeof(addr_str) - 1)) {
3492 if (!mgroup_setup) {
3497 freeaddrinfo(resmulti);
3507 if (setsockopt(session->
sock.
fd, IPPROTO_IP, IP_MULTICAST_TTL,
3508 (
const char *)&hops,
sizeof(hops)) < 0) {
3515 if (setsockopt(session->
sock.
fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
3516 (
const char *)&hops,
sizeof(hops)) < 0) {
3547 PROCESS_THREAD(coap_retransmit_process, ev, data) {
3557 if (ev == PROCESS_EVENT_TIMER) {
3558 if (etimer_expired(&the_coap_context.retransmit_timer)) {
3563 while (nextpdu && nextpdu->
t <= now) {
3569 etimer_set(&the_coap_context.retransmit_timer,
3570 nextpdu ? nextpdu->
t - now : 0xFFFF);
3572 if (etimer_expired(&the_coap_context.notify_timer)) {
3574 etimer_reset(&the_coap_context.notify_timer);
3599 static void coap_retransmittimer_execute(
void *arg) {
3605 ctx->timer_configured = 0;
3612 while (nextinqueue != NULL) {
3613 if (nextinqueue->
t > elapsed) {
3614 nextinqueue->
t -= elapsed;
3617 elapsed -= nextinqueue->
t;
3625 coap_retransmittimer_restart(ctx);
3631 if (ctx->timer_configured) {
3632 printf(
"clearing\n");
3633 sys_untimeout(coap_retransmittimer_execute, (
void*)ctx);
3634 ctx->timer_configured = 0;
3656 printf(
"scheduling for %d ticks\n", delay);
3657 sys_timeout(delay, coap_retransmittimer_execute, (
void*)ctx);
3658 ctx->timer_configured = 1;
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Pulls together all the internal only header files.
ssize_t coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len)
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.
ssize_t coap_network_read(coap_socket_t *sock, coap_packet_t *packet)
Function interface for reading data.
ssize_t coap_network_send(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for data transmission.
const char * coap_socket_strerror(void)
#define COAP_SOCKET_ERROR
@ COAP_NACK_NOT_DELIVERABLE
@ COAP_NACK_TOO_MANY_RETRIES
void coap_free_endpoint(coap_endpoint_t *ep)
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
#define COAP_SOCKET_CAN_WRITE
non blocking socket can now write without blocking
#define COAP_SOCKET_BOUND
the socket is bound
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_SOCKET_CAN_ACCEPT
non blocking server socket can now accept without blocking
#define COAP_SOCKET_WANT_WRITE
non blocking socket is waiting for writing
#define COAP_SOCKET_CAN_CONNECT
non blocking client socket can now connect without blocking
void coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func)
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
#define COAP_SOCKET_CONNECTED
the socket is connected
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
int coap_dtls_context_set_spsk(coap_context_t *ctx COAP_UNUSED, coap_dtls_spsk_t *setup_data COAP_UNUSED)
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
void * coap_dtls_new_context(coap_context_t *coap_context COAP_UNUSED)
int coap_dtls_send(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_dtls_new_client_session(coap_session_t *session COAP_UNUSED)
ssize_t coap_tls_read(coap_session_t *session COAP_UNUSED, uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
int coap_dtls_receive(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
void * coap_tls_new_client_session(coap_session_t *session COAP_UNUSED, int *connected COAP_UNUSED)
int coap_dtls_hello(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
ssize_t coap_tls_write(coap_session_t *session COAP_UNUSED, const uint8_t *data COAP_UNUSED, size_t data_len COAP_UNUSED)
int coap_dtls_context_set_pki_root_cas(coap_context_t *ctx COAP_UNUSED, const char *ca_file COAP_UNUSED, const char *ca_path COAP_UNUSED)
void coap_dtls_free_context(void *handle COAP_UNUSED)
#define SESSIONS_ITER_SAFE(e, el, rtmp)
#define SESSIONS_ITER(e, el, rtmp)
void coap_io_do_io(coap_context_t *ctx, coap_tick_t now)
Processes any outstanding read, write, accept or connect I/O as indicated in the coap_socket_t struct...
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...
void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event *events, size_t nevents)
Process all the epoll events.
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
int coap_handle_response_get_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, coap_recurse_t recursive)
void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
int coap_handle_request_send_block(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
int coap_handle_request_put_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *uri_path, coap_opt_t *observe, coap_string_t *query, coap_method_handler_t h, int *added_block)
coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu)
int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *rcvd)
#define COAP_MAX_BLOCK_SZX
The largest value for the SZX component in a Block option.
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
int coap_write_block_opt(coap_block_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
#define COAP_BLOCK_USE_LIBCOAP
void coap_delete_cache_entry(coap_context_t *ctx, coap_cache_entry_t *cache_entry)
Remove a cache-entry from the hash list and free off all the appropriate contents apart from app_data...
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
#define COAP_DEFAULT_LEISURE
The maximum number of seconds before sending back a response to a multicast request.
int64_t coap_tick_diff_t
This data type is used to represent the difference between two clock_tick_t values.
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
void coap_clock_init(void)
Initializes the internal clock.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
uint64_t coap_ticks_to_rt_us(coap_tick_t t)
Helper function that converts coap ticks to POSIX wallclock time in us.
coap_tick_t coap_check_async(coap_context_t *context, coap_tick_t now)
Checks if there are any pending Async requests - if so, send them off.
void coap_delete_all_async(coap_context_t *context)
Removes and frees off all of the async entries for the given context.
void coap_free_async(coap_session_t *session, coap_async_t *s)
Releases the memory that was allocated by coap_register_async() for the object async.
coap_async_t * coap_register_async(coap_session_t *session, const coap_pdu_t *request, coap_tick_t delay)
Allocates a new coap_async_t object and fills its fields according to the given request.
coap_async_t * coap_find_async(coap_session_t *session, coap_bin_const_t token)
Retrieves the object identified by token from the list of asynchronous transactions that are register...
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
void coap_prng_init(unsigned int seed)
Seeds the default random number generation function with the given seed.
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.
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
#define RESOURCES_ITER(r, tmp)
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.
void(* coap_method_handler_t)(coap_resource_t *, coap_session_t *, const coap_pdu_t *, const coap_string_t *, coap_pdu_t *)
Definition of message handler function.
#define COAP_PRINT_STATUS_ERROR
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
Set sendqueue_basetime in the given context object ctx to now.
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
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_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)
Sends a CoAP message to given peer.
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by variable t in node.
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',...
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.
coap_mid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
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.
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
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.
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.
coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
void coap_context_set_session_timeout(coap_context_t *context, unsigned int session_timeout)
Set the session timeout value.
int coap_context_set_psk2(coap_context_t *ctx, coap_dtls_spsk_t *setup_data)
Set the context's default PSK hint and/or key for a server.
unsigned int coap_context_get_max_handshake_sessions(const coap_context_t *context)
Get the session timeout value.
void(* coap_pong_handler_t)(coap_session_t *session, const coap_pdu_t *received, const coap_mid_t mid)
Received Pong handler that is used as callback in coap_context_t.
unsigned int coap_context_get_max_idle_sessions(const coap_context_t *context)
Get the maximum idle sessions count.
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.
coap_response_t(* coap_response_handler_t)(coap_session_t *session, const coap_pdu_t *sent, const coap_pdu_t *received, const coap_mid_t mid)
Response handler that is used as callback in coap_context_t.
void coap_register_response_handler(coap_context_t *context, coap_response_handler_t handler)
Registers a new message handler that is called whenever a response is received.
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...
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
void coap_context_set_max_handshake_sessions(coap_context_t *context, unsigned int max_handshake_sessions)
Set the maximum number of sessions in (D)TLS handshake value.
int coap_context_get_coap_fd(const coap_context_t *context)
Get the libcoap internal file descriptor for using in an application's select() or returned as an eve...
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.
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.
int coap_mcast_set_hops(coap_session_t *session, size_t hops)
Function interface for defining the hop count (ttl) for sending multicast traffic.
void(* coap_ping_handler_t)(coap_session_t *session, const coap_pdu_t *received, const coap_mid_t mid)
Received Ping handler that is used as callback in coap_context_t.
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.
void(* coap_nack_handler_t)(coap_session_t *session, const coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
Negative Acknowedge handler that is used as callback in coap_context_t.
COAP_STATIC_INLINE coap_mid_t coap_send_rst(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
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.
coap_mid_t coap_send_message_type(coap_session_t *session, const coap_pdu_t *request, coap_pdu_type_t type)
Helper function to create and send a message with type (usually ACK or RST).
unsigned int coap_context_get_session_timeout(const coap_context_t *context)
Get the session timeout value.
coap_mid_t coap_send_error(coap_session_t *session, const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts)
Sends an error response with code code for request request to dst.
int coap_context_set_pki(coap_context_t *ctx, const coap_dtls_pki_t *setup_data)
Set the context's default PKI information for a server.
void coap_register_ping_handler(coap_context_t *context, coap_ping_handler_t handler)
Registers a new message handler that is called whenever a CoAP Ping message is received.
void coap_register_option(coap_context_t *ctx, uint16_t type)
Registers the option type type with the given context object ctx.
int coap_join_mcast_group_intf(coap_context_t *ctx, const char *group_name, const char *ifname)
Function interface for joining a multicast group for listening for the currently defined endpoints th...
void coap_context_set_max_idle_sessions(coap_context_t *context, unsigned int max_idle_sessions)
Set the maximum idle sessions count.
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_context_set_keepalive(coap_context_t *context, unsigned int seconds)
Set the context keepalive timer for sessions.
void coap_set_app_data(coap_context_t *ctx, void *app_data)
Stores data with the given CoAP context.
unsigned int coap_context_get_csm_timeout(const coap_context_t *context)
Get the CSM timeout value.
void coap_register_pong_handler(coap_context_t *context, coap_pong_handler_t handler)
Registers a new message handler that is called whenever a CoAP Pong message is received.
coap_mid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
void coap_register_nack_handler(coap_context_t *context, coap_nack_handler_t handler)
Registers a new message handler that is called whenever a confirmable message (request or response) i...
void coap_context_set_csm_timeout(coap_context_t *context, unsigned int csm_timeout)
Set the CSM timeout value.
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
void coap_dtls_startup(void)
Initialize the underlying (D)TLS Library layer.
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
void coap_dtls_shutdown(void)
Close down the underlying (D)TLS Library layer.
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
@ COAP_DTLS_ROLE_SERVER
Internal function invoked for server.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
#define COAP_EVENT_DTLS_ERROR
unsigned int coap_event_t
Scalar type to represent different events, e.g.
#define COAP_EVENT_TCP_FAILED
#define COAP_EVENT_DTLS_CONNECTED
#define COAP_EVENT_TCP_CONNECTED
TCP events for COAP_PROTO_TCP and COAP_PROTO_TLS.
coap_log_t coap_get_log_level(void)
Get the current logging level.
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
const char * coap_session_str(const coap_session_t *session)
Get session description.
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
#define coap_log(level,...)
Logging function.
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
int coap_option_filter_unset(coap_opt_filter_t *filter, coap_option_num_t option)
Clears the corresponding entry for number in filter.
void coap_option_filter_clear(coap_opt_filter_t *filter)
Clears filter filter.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
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.
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t option)
Checks if number is contained in filter.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
#define COAP_PDU_IS_RESPONSE(pdu)
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Inserts option of given number in the pdu with the appropriate data.
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
#define COAP_DROPPED_RESPONSE
Indicates that a response is suppressed.
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
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.
#define COAP_PDU_IS_EMPTY(pdu)
#define COAP_DEFAULT_MAX_PDU_RX_SIZE
#define COAP_PDU_IS_SIGNALING(pdu)
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
size_t coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Updates existing first option of given number in the pdu with the new data.
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
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.
size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, size_t length)
Parses data to extract the message size.
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
#define COAP_PDU_IS_REQUEST(pdu)
#define COAP_OPTION_HOP_LIMIT
#define COAP_OPTION_NORESPONSE
#define COAP_OPTION_URI_HOST
#define COAP_OPTION_IF_MATCH
#define COAP_OPTION_BLOCK2
#define COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_SIZE2
#define COAP_OPTION_BLOCK1
#define COAP_OPTION_PROXY_SCHEME
#define COAP_DEFAULT_PORT
#define COAP_OPTION_URI_QUERY
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_OPTION_IF_NONE_MATCH
#define COAP_OPTION_URI_PATH
#define COAP_RESPONSE_CODE(N)
#define COAP_RESPONSE_CLASS(C)
coap_pdu_code_t
Set of codes available for a PDU.
coap_pdu_type_t
CoAP PDU message type definitions.
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
#define COAP_OPTION_CONTENT_TYPE
size_t coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_SIGNALING_OPTION_CUSTODY
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 copy it.
#define COAPS_DEFAULT_PORT
#define COAP_OPTION_URI_PORT
#define COAP_OPTION_ACCEPT
#define COAP_INVALID_MID
Indicates an invalid message id.
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
#define COAP_OPTION_PROXY_URI
#define COAP_OPTION_OBSERVE
#define COAP_DEFAULT_URI_WELLKNOWN
well-known resources URI
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
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_SIGNALING_CODE_ABORT
@ COAP_SIGNALING_CODE_CSM
@ COAP_SIGNALING_CODE_PING
@ COAP_REQUEST_CODE_DELETE
@ COAP_SIGNALING_CODE_PONG
@ COAP_SIGNALING_CODE_RELEASE
@ COAP_REQUEST_CODE_FETCH
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.
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
coap_session_t * coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for stream data transmission.
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_PROTO_NOT_RELIABLE(p)
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
#define COAP_PROTO_RELIABLE(p)
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
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.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_ESTABLISHED
@ COAP_SESSION_STATE_NONE
@ COAP_SESSION_STATE_CONNECTING
#define COAP_SET_STR(st, l, v)
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
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.
coap_subscription_t * coap_add_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token, const coap_pdu_t *request)
Adds the specified peer as observer for resource.
void coap_check_notify(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Handles a failed observe notify.
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Flags that data is ready to be sent to observers.
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)
Create a new TCP socket and initiate the connection.
int coap_socket_connect_tcp2(coap_socket_t *sock, coap_address_t *local_addr, coap_address_t *remote_addr)
Complete the TCP Connection.
coap_string_t * coap_get_uri_path(const coap_pdu_t *request)
Extract uri_path string from request PDU.
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri)
Parses a given string into URI components.
#define COAP_STATIC_INLINE
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.
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
void coap_memory_init(void)
Initializes libcoap's memory management.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
#define FRAC_BITS
The number of bits for the fractional part of ACK_TIMEOUT and ACK_RANDOM_FACTOR.
static ssize_t coap_send_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
COAP_STATIC_INLINE int token_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
respond_t
Internal flags to control the treatment of responses (specifically in presence of the No-Response opt...
static void handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
static coap_str_const_t coap_default_uri_wellknown
static int coap_read_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
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.
#define MAX_BITS
The maximum number of bits for fixed point integers that are used for retransmission time calculation...
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)
#define ACK_TIMEOUT
creates a Qx.FRAC_BITS from session's 'ack_timeout'
#define SZX_TO_BYTES(SZX)
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)
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.
static int coap_accept_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
static size_t coap_get_context_server_hint(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
static int coap_write_endpoint(coap_context_t *ctx, coap_endpoint_t *endpoint, coap_tick_t now)
static int coap_handle_dgram_for_proto(coap_context_t *ctx, coap_session_t *session, coap_packet_t *packet)
static void coap_write_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
static void coap_connect_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
COAP_STATIC_INLINE void coap_free_node(coap_queue_t *node)
#define SHR_FP(val, frac)
static enum respond_t no_response(coap_pdu_t *request, coap_pdu_t *response, coap_session_t *session)
static void handle_signaling(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu)
static void coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
#define ACK_RANDOM_FACTOR
creates a Qx.FRAC_BITS from session's 'ack_random_factor'
COAP_STATIC_INLINE coap_queue_t * coap_malloc_node(void)
static void handle_response(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd)
uint16_t coap_option_num_t
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
#define COAP_RESOURCE_CHECK_TIME
The interval in seconds to check if resources have changed.
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
coap_session_t * session
transaction session
coap_pdu_t * pdu
copy of request pdu
coap_tick_t delay
When to delay to before triggering the response 0 indicates never trigger.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
Structure of Block options.
unsigned int num
block number
unsigned int szx
block size
unsigned int m
1 if more blocks follow, 0 otherwise
The CoAP stack's global state is stored in a coap_context_t object.
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
coap_pong_handler_t pong_handler
unsigned int csm_timeout
Timeout for waiting for a CSM from the remote side.
void * app
application-specific data
coap_async_t * async_state
list of asynchronous message ids
coap_session_t * sessions
client sessions
coap_nack_handler_t nack_handler
unsigned int ping_timeout
Minimum inactivity time before sending a ping message.
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)
coap_resource_t * resources
hash table or list of known resources
ssize_t(* network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen)
uint16_t * cache_ignore_options
CoAP options to ignore when creating a cache-key.
coap_opt_filter_t known_options
coap_ping_handler_t ping_handler
size_t cache_ignore_count
The number of CoAP options to ignore when creating a cache-key.
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
coap_response_handler_t response_handler
coap_cache_entry_t * cache
CoAP cache-entry cache.
coap_endpoint_t * endpoint
the endpoints used for listening
size_t(* get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len)
coap_event_handler_t handle_event
Callback function that is used to signal events to the application.
unsigned int session_timeout
Number of seconds of inactivity after which an unused session will be closed.
ssize_t(* network_read)(coap_socket_t *sock, coap_packet_t *packet)
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)
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_dtls_spsk_t spsk_setup_data
Contains the initial PSK server setup data.
coap_resource_t * unknown_resource
can be used for handling unknown resources
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
The structure that holds the Client PSK information.
coap_bin_const_t identity
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
The structure used for defining the PKI setup data to be used.
The structure that holds the Server Pre-Shared Key and Identity Hint information.
The structure used for defining the Server PSK setup data to be used.
coap_dtls_spsk_info_t psk_info
Server PSK definition.
Abstraction of virtual endpoint that can be attached to coap_context_t.
coap_context_t * context
endpoint's context
coap_session_t * sessions
hash table or list of active sessions
coap_address_t bind_addr
local interface address
coap_socket_t sock
socket object for the interface, if any
coap_proto_t proto
protocol used on this interface
size_t token_length
length of token
coap_binary_t * app_token
original PDU token
uint8_t token[8]
last used token
Structure to hold large body (many blocks) client receive information.
uint8_t initial
If set, has not been used yet.
coap_binary_t * app_token
app requesting PDU token
uint8_t token[8]
last used token
uint8_t base_token[8]
established base PDU token
size_t token_length
length of token
size_t base_token_length
length of token
uint8_t observe_set
Set if this is an observe receive PDU.
Structure to hold large body (many blocks) transmission information.
union coap_lg_xmit_t::@1 b
coap_pdu_t pdu
skeletal PDU
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
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
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t token_length
length of Token
uint8_t hdr_size
actual size used for protocol-specific header
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
size_t alloc_size
allocated storage for token, options and payload
coap_pdu_type_t type
message type
coap_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
unsigned int timeout
the randomized timeout value
struct coap_queue_t * next
coap_mid_t id
CoAP message id.
coap_tick_t t
when to send PDU for the next time
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Abstraction of resource that can be attached to coap_context_t.
coap_str_const_t ** proxy_name_list
Array valid names this host is known by (proxy support)
coap_str_const_t * uri_path
Request URI Path for this resource.
unsigned int observe
The next value for the Observe option.
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH.
unsigned int is_proxy_uri
resource created for proxy URI handler
unsigned int is_unknown
resource created for unknown handler
unsigned int observable
can be observed
size_t proxy_name_count
Count of valid names this host is known by (proxy support)
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
coap_endpoint_t * endpoint
session's endpoint
coap_socket_t sock
socket object for the session, if any
unsigned int max_retransmit
maximum re-transmit count (default 4)
coap_pdu_t * partial_pdu
incomplete incoming pdu
coap_bin_const_t * psk_identity
If client, this field contains the current identity for server; When this field is NULL,...
coap_session_state_t state
current state of relationaship with peer
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint8_t read_header[8]
storage space for header of incoming message header
coap_addr_tuple_t addr_info
key: remote/local address info
coap_proto_t proto
protocol used
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
void * tls
security parameters
uint8_t csm_block_supported
CSM TCP blocks supported.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
coap_endpoint_t * endpoint
coap_socket_flags_t flags
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
CoAP string data definition.
size_t length
length of string
struct coap_session_t * session
subscriber session
coap_pdu_t * pdu
cache_key to identify requester
Representation of parsed URI.
coap_str_const_t host
host part of the URI