Loading...
Searching...
No Matches
nanocoap_sock.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Kaspar Schleiser <kaspar@schleiser.de>
3 * SPDX-FileCopyrightText: 2018 Inria
4 * SPDX-FileCopyrightText: 2018 Freie Universität Berlin
5 * SPDX-License-Identifier: LGPL-2.1-only
6 */
7
8#pragma once
9
135
136#include <stdint.h>
137#include <unistd.h>
138
139#include "event/thread.h"
140#include "net/nanocoap.h"
141#include "net/sock/udp.h"
142#include "net/sock/util.h"
143#include "random.h"
144
145#if IS_USED(MODULE_NANOCOAP_DTLS)
146# include "net/credman.h"
147# include "net/sock/dtls.h"
148#endif
149
150#ifdef __cplusplus
151extern "C" {
152#endif
153
158#ifndef CONFIG_NANOCOAP_SOCK_DTLS_TAG
159#define CONFIG_NANOCOAP_SOCK_DTLS_TAG (0xc0ab)
160#endif
161
166#ifndef CONFIG_NANOCOAP_SERVER_BUF_SIZE
167#define CONFIG_NANOCOAP_SERVER_BUF_SIZE ((1 << (CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT + 3)) \
168 + CONFIG_NANOCOAP_URI_MAX + 16)
169#endif
170
174#ifndef CONFIG_NANOCOAP_SERVER_STACK_SIZE
175#define CONFIG_NANOCOAP_SERVER_STACK_SIZE THREAD_STACKSIZE_DEFAULT
176#endif
177
186#ifndef CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
187#define CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN (0)
188#endif
189
193#ifndef CONFIG_NANOCOAP_SOCK_EVENT_PRIO
194# define CONFIG_NANOCOAP_SOCK_EVENT_PRIO EVENT_PRIO_MEDIUM
195#endif
196
204
208typedef struct {
210#if IS_USED(MODULE_NANOCOAP_DTLS) || defined(DOXYGEN)
216#endif
217 uint16_t msg_id;
221
225typedef struct {
227 const char *path;
228 uint32_t blknum;
230 uint8_t blksize;
232
241
245typedef struct {
248 uint8_t token[COAP_TOKEN_LENGTH_MAX];
249 uint8_t tkl;
250 uint8_t no_response;
252
276 coap_pkt_t *pkt, const coap_request_ctx_t *req);
277
289 const coap_request_ctx_t *req);
290
315 unsigned code, unsigned type,
316 const void *payload, size_t len);
317
344 void *buf, size_t buf_len,
345 unsigned code, unsigned type,
346 uint16_t msg_id);
347
368 const iolist_t *reply);
369
410
422 const coap_pkt_t *req_pkt);
423
430 uint16_t msg_id);
431
447
461 const void *payload, size_t payload_len);
462
471static inline uint16_t nanocoap_sock_next_msg_id(nanocoap_sock_t *sock)
472{
473 return sock->msg_id++;
474}
475
487int nanocoap_server(sock_udp_ep_t *local, void *buf, size_t bufsize);
488
500
512 const sock_udp_ep_t *local,
513 const sock_udp_ep_t *remote)
514{
515#if IS_USED(MODULE_NANOCOAP_DTLS)
517#endif
518 sock->msg_id = random_uint32();
519
520 return sock_udp_create(&sock->udp, local, remote, 0);
521}
522
523#if IS_USED(MODULE_NANOCOAP_DTLS) || DOXYGEN
537 const sock_udp_ep_t *remote, credman_tag_t tag);
538#endif
539
549int nanocoap_sock_url_connect(const char *url, nanocoap_sock_t *sock);
550
556static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
557{
558#if IS_USED(MODULE_NANOCOAP_DTLS)
559 if (sock->type == COAP_SOCKET_TYPE_DTLS) {
560 sock_dtls_session_destroy(&sock->dtls, &sock->dtls_session);
561 sock_dtls_close(&sock->dtls);
562 }
563#endif
564 sock_udp_close(&sock->udp);
565}
566
592 uint8_t type, uint8_t method,
593 const void *token, size_t token_len);
594
618 uint8_t type, uint8_t method)
619{
620 return nanocoap_sock_builder_init_token(sock, state, type, method, NULL, 0);
621}
622
639 coap_request_cb_t cb, void *arg);
656
671ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path,
672 void *response, size_t len_max);
673
688ssize_t nanocoap_sock_get_non(nanocoap_sock_t *sock, const char *path,
689 void *response, size_t len_max);
690
707ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
708 const void *request, size_t len,
709 void *response, size_t len_max);
710
727ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path,
728 const void *request, size_t len,
729 void *response, size_t len_max);
730
743ssize_t nanocoap_sock_put_url(const char *url,
744 const void *request, size_t len,
745 void *response, size_t len_max);
746
764ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
765 const void *request, size_t len,
766 void *response, size_t len_max);
767
785ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path,
786 const void *request, size_t len,
787 void *response, size_t len_max);
788
801ssize_t nanocoap_sock_post_url(const char *url,
802 const void *request, size_t len,
803 void *response, size_t len_max);
804
822ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path,
823 const void *request, size_t len,
824 void *response, size_t len_max);
825
843ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path,
844 const void *request, size_t len,
845 void *response, size_t len_max);
846
860ssize_t nanocoap_sock_fetch_url(const char *url,
861 const void *request, size_t len,
862 void *response, size_t len_max);
863
875ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path);
876
885ssize_t nanocoap_sock_delete_url(const char *url);
886
904 coap_blksize_t blksize,
905 coap_blockwise_cb_t callback, void *arg);
906
922int nanocoap_sock_get_slice(nanocoap_sock_t *sock, const char *path,
923 coap_blksize_t blksize, size_t offset,
924 void *dst, size_t len);
925
943int nanocoap_get_blockwise_url(const char *url,
944 coap_blksize_t blksize,
945 coap_blockwise_cb_t callback, void *arg);
946
966ssize_t nanocoap_get_blockwise_url_to_buf(const char *url,
967 coap_blksize_t blksize,
968 void *buf, size_t len);
969
989ssize_t nanocoap_get_blockwise_to_buf(nanocoap_sock_t *sock, const char *path,
990 coap_blksize_t blksize,
991 void *buf, size_t len);
992
1005ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len);
1006
1031 coap_request_cb_t cb, void *arg);
1032
1049ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
1050 const sock_udp_ep_t *remote, size_t len);
1051
1065 nanocoap_sock_t *sock,
1066 const char *url,
1067 coap_method_t method,
1068 coap_blksize_t blksize)
1069{
1070 ctx->sock = sock;
1071 ctx->path = sock_urlpath(url);
1072 ctx->blknum = 0;
1073 ctx->method = method;
1074 ctx->blksize = blksize;
1075 return nanocoap_sock_url_connect(url, ctx->sock);
1076}
1077
1099 const void *data, size_t len, bool more,
1100 coap_request_cb_t callback, void *arg);
1101#ifdef __cplusplus
1102}
1103#endif
#define WARN_UNUSED_RESULT
Attribute to add to a function whose return value should not silently be discarded.
(D)TLS credentials management module definitions
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:134
coap_blksize_t
CoAP block-wise-transfer size SZX.
Definition coap.h:632
coap_method_t
CoAP method codes used in request.
Definition coap.h:171
uint16_t credman_tag_t
Tag of the credential.
Definition credman.h:92
#define CONFIG_NANOCOAP_BLOCK_HEADER_MAX
Maximum length of a CoAP header for a blockwise message.
Definition nanocoap.h:168
int(* coap_blockwise_cb_t)(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
Coap blockwise request callback descriptor.
Definition nanocoap.h:300
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:312
struct _coap_request_ctx coap_request_ctx_t
Forward declaration of internal CoAP resource request handler context.
Definition nanocoap.h:261
ssize_t nanocoap_server_build_separate(const nanocoap_server_response_ctx_t *ctx, void *buf, size_t buf_len, unsigned code, unsigned type, uint16_t msg_id)
Build a separate response header to a CoAP request.
int nanocoap_register_observer(const coap_request_ctx_t *req_ctx, coap_pkt_t *req_pkt)
Register an observer.
ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path, const void *request, size_t len, void *response, size_t len_max)
Simple non-confirmable PUT.
ssize_t nanocoap_sock_get_non(nanocoap_sock_t *sock, const char *path, void *response, size_t len_max)
Simple non-confirmable GET.
ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *response, size_t len_max)
Simple synchronous CoAP (confirmable) GET.
ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path)
Simple synchronous CoAP (confirmable) DELETE.
void nanocoap_notify_observers(const coap_resource_t *res, const iolist_t *iol)
Notify all currently registered observers of the given resource.
ssize_t nanocoap_sock_observe_url(const char *url, coap_observe_client_t *ctx, coap_request_cb_t cb, void *arg)
Observe a CoAP resource behind a URL (via GET)
ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path, const void *request, size_t len, void *response, size_t len_max)
Simple synchronous CoAP (confirmable) PUT.
void nanocoap_unregister_observer_due_to_reset(const sock_udp_ep_t *ep, uint16_t msg_id)
Unregister a stale observation due to a reset message received.
int nanocoap_sock_url_connect(const char *url, nanocoap_sock_t *sock)
Create a CoAP client socket by URL.
ssize_t nanocoap_sock_post_url(const char *url, const void *request, size_t len, void *response, size_t len_max)
Simple synchronous CoAP (confirmable) POST to URL.
ssize_t nanocoap_sock_delete_url(const char *url)
Simple synchronous CoAP (confirmable) DELETE for URL.
int nanocoap_sock_get_slice(nanocoap_sock_t *sock, const char *path, coap_blksize_t blksize, size_t offset, void *dst, size_t len)
Performs a blockwise coap get request to the specified url, store the response in a buffer.
static WARN_UNUSED_RESULT int nanocoap_sock_builder_init(nanocoap_sock_t *sock, coap_builder_t *state, uint8_t type, uint8_t method)
Initialize state for building a request.
ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local, const sock_udp_ep_t *remote, size_t len)
Simple synchronous CoAP request.
WARN_UNUSED_RESULT int nanocoap_sock_builder_init_token(nanocoap_sock_t *sock, coap_builder_t *state, uint8_t type, uint8_t method, const void *token, size_t token_len)
Initialize state for building a request.
int nanocoap_server_prepare_separate(nanocoap_server_response_ctx_t *ctx, coap_pkt_t *pkt, const coap_request_ctx_t *req)
Prepare the context for a separate response.
void nanocoap_notify_observers_simple(const coap_resource_t *res, uint32_t obs, const void *payload, size_t payload_len)
Build and send notification to observers registered to a specific resource.
kernel_pid_t nanocoap_server_start(const sock_udp_ep_t *local)
Create and start the nanoCoAP server thread.
ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len)
Simple synchronous CoAP request.
int nanocoap_server_sendv_separate(const nanocoap_server_response_ctx_t *ctx, const iolist_t *reply)
Send an already build separate response.
ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path, const void *request, size_t len, void *response, size_t len_max)
Simple synchronous CoAP (confirmable) FETCH (RFC 8132)
int nanocoap_sock_dtls_connect(nanocoap_sock_t *sock, sock_udp_ep_t *local, const sock_udp_ep_t *remote, credman_tag_t tag)
Create a DTLS secured CoAP client socket.
ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path, const void *request, size_t len, void *response, size_t len_max)
Simple non-confirmable FETCH (RFC 8132)
ssize_t nanocoap_sock_fetch_url(const char *url, const void *request, size_t len, void *response, size_t len_max)
Simple synchronous CoAP (confirmable) FETCH to URL (RFC 8132)
static uint16_t nanocoap_sock_next_msg_id(nanocoap_sock_t *sock)
Get next consecutive message ID for use when building a new CoAP request.
ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path, const void *request, size_t len, void *response, size_t len_max)
Simple synchronous CoAP (confirmable) POST.
ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path, const void *request, size_t len, void *response, size_t len_max)
Simple non-confirmable POST.
int nanocoap_server_send_separate(const nanocoap_server_response_ctx_t *ctx, unsigned code, unsigned type, const void *payload, size_t len)
Build and send a separate response to a CoAP request.
void nanocoap_unregister_observer(const coap_request_ctx_t *req_ctx, const coap_pkt_t *req_pkt)
Unregister an observer.
ssize_t nanocoap_sock_request_cb(nanocoap_sock_t *sock, coap_pkt_t *pkt, coap_request_cb_t cb, void *arg)
Simple synchronous CoAP request with callback.
static int nanocoap_block_request_connect_url(coap_block_request_t *ctx, nanocoap_sock_t *sock, const char *url, coap_method_t method, coap_blksize_t blksize)
Initialize block request context by URL and connect a socket.
ssize_t nanocoap_get_blockwise_url_to_buf(const char *url, coap_blksize_t blksize, void *buf, size_t len)
Performs a blockwise coap get request to the specified url, store the response in a buffer.
int nanocoap_get_blockwise_url(const char *url, coap_blksize_t blksize, coap_blockwise_cb_t callback, void *arg)
Performs a blockwise coap get request to the specified url.
int nanocoap_sock_block_request(coap_block_request_t *req, const void *data, size_t len, bool more, coap_request_cb_t callback, void *arg)
Do a block-wise request, send a single block.
ssize_t nanocoap_sock_put_url(const char *url, const void *request, size_t len, void *response, size_t len_max)
Simple synchronous CoAP (confirmable) PUT to URL.
static int nanocoap_sock_connect(nanocoap_sock_t *sock, const sock_udp_ep_t *local, const sock_udp_ep_t *remote)
Create a CoAP client socket.
ssize_t nanocoap_sock_unobserve_url(const char *url, coap_observe_client_t *ctx)
Stop observing a CoAP resource.
int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path, coap_blksize_t blksize, coap_blockwise_cb_t callback, void *arg)
Performs a blockwise coap get request on a socket.
bool nanocoap_server_is_remote_in_response_ctx(const nanocoap_server_response_ctx_t *ctx, const coap_request_ctx_t *req)
Check if a given separate response context was prepared for the remote endpoint of a given request.
nanocoap_socket_type_t
NanoCoAP socket types.
static void nanocoap_sock_close(nanocoap_sock_t *sock)
Close a CoAP client socket.
int nanocoap_server(sock_udp_ep_t *local, void *buf, size_t bufsize)
Start a nanoCoAP server instance.
ssize_t nanocoap_get_blockwise_to_buf(nanocoap_sock_t *sock, const char *path, coap_blksize_t blksize, void *buf, size_t len)
Performs a blockwise CoAP GET request, store the response in a buffer.
@ COAP_SOCKET_TYPE_UDP
transport is plain UDP
@ COAP_SOCKET_TYPE_DTLS
transport is DTLS
struct sock_udp sock_udp_t
forward declare for async
Definition types.h:136
struct sock_dtls sock_dtls_t
forward declare for async
Definition types.h:44
struct sock_dtls_session sock_dtls_session_t
Information about a created session.
Definition dtls.h:635
int sock_udp_create(sock_udp_t *sock, const sock_udp_ep_t *local, const sock_udp_ep_t *remote, uint16_t flags)
Creates a new UDP sock object.
void sock_udp_close(sock_udp_t *sock)
Closes a UDP sock object.
struct _sock_tl_ep sock_udp_ep_t
An end point for a UDP sock object.
Definition udp.h:292
const char * sock_urlpath(const char *url)
Returns a pointer to the path component in url.
struct iolist iolist_t
iolist forward declaration
Definition iolist.h:30
uint32_t random_uint32(void)
generates a random number on [0,0xffffffff]-interval
nanocoap API
Common interface to the software PRNG.
DTLS sock definitions.
UDP sock definitions.
Blockwise request helper struct.
nanocoap_sock_t * sock
socket used for the request
uint32_t blknum
current block number
const char * path
path on the server
coap_method_t method
request method (GET, POST, PUT)
uint8_t blksize
CoAP blocksize exponent.
Structure to hold the state for building a CoAP message.
Definition nanocoap.h:451
Observe Client helper struct.
coap_request_cb_t cb
user callback function
nanocoap_sock_t sock
socket used for the request
void * arg
callback function argument
CoAP PDU parsing context structure.
Definition nanocoap.h:226
Type for CoAP resource entry.
Definition nanocoap.h:324
Context from CoAP request for separate response.
uint8_t token[COAP_TOKEN_LENGTH_MAX]
request token
sock_udp_ep_t local
local from which to send response
uint8_t tkl
request token length
sock_udp_ep_t remote
remote to send response to
uint8_t no_response
no-response bitmap
NanoCoAP socket struct.
uint16_t msg_id
next CoAP message ID
uint8_t hdr_buf[CONFIG_NANOCOAP_BLOCK_HEADER_MAX]
buffer for CoAP header with options, token and payload marker
sock_dtls_session_t dtls_session
Session object for the stored socket.
sock_dtls_t dtls
DTLS socket.
sock_udp_t udp
UDP socket.
nanocoap_socket_type_t type
Socket type (UDP, DTLS)
Provides utility functions for event handler threads.
sock utility function definitions