Loading...
Searching...
No Matches
nanocoap_sock.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
3 * 2018 Inria
4 * 2018 Freie Universität Berlin
5 *
6 * This file is subject to the terms and conditions of the GNU Lesser
7 * General Public License v2.1. See the file LICENSE in the top level
8 * directory for more details.
9 */
10
11#pragma once
12
139
140#include <stdint.h>
141#include <unistd.h>
142
143#include "random.h"
144#include "net/nanocoap.h"
145#include "net/sock/udp.h"
146#include "net/sock/util.h"
147#if IS_USED(MODULE_NANOCOAP_DTLS)
148#include "net/credman.h"
149#include "net/sock/dtls.h"
150#endif
151
152#ifdef __cplusplus
153extern "C" {
154#endif
155
160#ifndef CONFIG_NANOCOAP_SOCK_DTLS_TAG
161#define CONFIG_NANOCOAP_SOCK_DTLS_TAG (0xc0ab)
162#endif
163
168#ifndef CONFIG_NANOCOAP_SERVER_BUF_SIZE
169#define CONFIG_NANOCOAP_SERVER_BUF_SIZE ((1 << (CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT + 3)) \
170 + CONFIG_NANOCOAP_URI_MAX + 16)
171#endif
172
176#ifndef CONFIG_NANOCOAP_SERVER_STACK_SIZE
177#define CONFIG_NANOCOAP_SERVER_STACK_SIZE THREAD_STACKSIZE_DEFAULT
178#endif
179
188#ifndef CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
189#define CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN (0)
190#endif
191
195#ifndef CONFIG_NANOCOAP_SOCK_EVENT_PRIO
196# define CONFIG_NANOCOAP_SOCK_EVENT_PRIO EVENT_PRIO_MEDIUM
197#endif
198
206
210typedef struct {
212#if IS_USED(MODULE_NANOCOAP_DTLS) || defined(DOXYGEN)
218#endif
219 uint16_t msg_id;
223
227typedef struct {
229 const char *path;
230 uint32_t blknum;
232 uint8_t blksize;
234
243
247typedef struct {
250 uint8_t token[COAP_TOKEN_LENGTH_MAX];
251 uint8_t tkl;
252 uint8_t no_response;
254
278 coap_pkt_t *pkt, const coap_request_ctx_t *req);
279
291 const coap_request_ctx_t *req);
292
317 unsigned code, unsigned type,
318 const void *payload, size_t len);
319
346 void *buf, size_t buf_len,
347 unsigned code, unsigned type,
348 uint16_t msg_id);
349
370 const iolist_t *reply);
371
412
424 const coap_pkt_t *req_pkt);
425
432 uint16_t msg_id);
433
449
463 const void *payload, size_t payload_len);
464
473static inline uint16_t nanocoap_sock_next_msg_id(nanocoap_sock_t *sock)
474{
475 return sock->msg_id++;
476}
477
490int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
491
503
515 const sock_udp_ep_t *local,
516 const sock_udp_ep_t *remote)
517{
518#if IS_USED(MODULE_NANOCOAP_DTLS)
520#endif
521 sock->msg_id = random_uint32();
522
523 return sock_udp_create(&sock->udp, local, remote, 0);
524}
525
526#if IS_USED(MODULE_NANOCOAP_DTLS) || DOXYGEN
540 const sock_udp_ep_t *remote, credman_tag_t tag);
541#endif
542
552int nanocoap_sock_url_connect(const char *url, nanocoap_sock_t *sock);
553
559static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
560{
561#if IS_USED(MODULE_NANOCOAP_DTLS)
562 if (sock->type == COAP_SOCKET_TYPE_DTLS) {
563 sock_dtls_session_destroy(&sock->dtls, &sock->dtls_session);
564 sock_dtls_close(&sock->dtls);
565 }
566#endif
567 sock_udp_close(&sock->udp);
568}
569
586 coap_request_cb_t cb, void *arg);
603
618ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path,
619 void *response, size_t len_max);
620
635ssize_t nanocoap_sock_get_non(nanocoap_sock_t *sock, const char *path,
636 void *response, size_t len_max);
637
654ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
655 const void *request, size_t len,
656 void *response, size_t len_max);
657
674ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path,
675 const void *request, size_t len,
676 void *response, size_t len_max);
677
690ssize_t nanocoap_sock_put_url(const char *url,
691 const void *request, size_t len,
692 void *response, size_t len_max);
693
711ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
712 const void *request, size_t len,
713 void *response, size_t len_max);
714
732ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path,
733 const void *request, size_t len,
734 void *response, size_t len_max);
735
748ssize_t nanocoap_sock_post_url(const char *url,
749 const void *request, size_t len,
750 void *response, size_t len_max);
751
769ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path,
770 const void *request, size_t len,
771 void *response, size_t len_max);
772
790ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path,
791 const void *request, size_t len,
792 void *response, size_t len_max);
793
807ssize_t nanocoap_sock_fetch_url(const char *url,
808 const void *request, size_t len,
809 void *response, size_t len_max);
810
822ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path);
823
832ssize_t nanocoap_sock_delete_url(const char *url);
833
851 coap_blksize_t blksize,
852 coap_blockwise_cb_t callback, void *arg);
853
869int nanocoap_sock_get_slice(nanocoap_sock_t *sock, const char *path,
870 coap_blksize_t blksize, size_t offset,
871 void *dst, size_t len);
872
890int nanocoap_get_blockwise_url(const char *url,
891 coap_blksize_t blksize,
892 coap_blockwise_cb_t callback, void *arg);
893
913ssize_t nanocoap_get_blockwise_url_to_buf(const char *url,
914 coap_blksize_t blksize,
915 void *buf, size_t len);
916
936ssize_t nanocoap_get_blockwise_to_buf(nanocoap_sock_t *sock, const char *path,
937 coap_blksize_t blksize,
938 void *buf, size_t len);
939
952ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len);
953
978 coap_request_cb_t cb, void *arg);
979
996ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
997 const sock_udp_ep_t *remote, size_t len);
998
1012 nanocoap_sock_t *sock,
1013 const char *url,
1014 coap_method_t method,
1015 coap_blksize_t blksize)
1016{
1017 ctx->sock = sock;
1018 ctx->path = sock_urlpath(url);
1019 ctx->blknum = 0;
1020 ctx->method = method;
1021 ctx->blksize = blksize;
1022 return nanocoap_sock_url_connect(url, ctx->sock);
1023}
1024
1046 const void *data, size_t len, bool more,
1047 coap_request_cb_t cb, void *arg);
1048#ifdef __cplusplus
1049}
1050#endif
(D)TLS credentials management module definitions
int16_t kernel_pid_t
Unique process identifier.
Definition sched.h:138
coap_blksize_t
Coap block-wise-transfer size SZX.
Definition coap.h:619
coap_method_t
CoAP method codes used in request.
Definition coap.h:170
uint16_t credman_tag_t
Tag of the credential.
Definition credman.h:95
#define CONFIG_NANOCOAP_BLOCK_HEADER_MAX
Maximum length of a CoAP header for a blockwise message.
Definition nanocoap.h:172
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:276
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:288
struct _coap_request_ctx coap_request_ctx_t
Forward declaration of internal CoAP resource request handler context.
Definition nanocoap.h:237
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.
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.
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.
int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
Start a nanocoap server instance.
int nanocoap_sock_block_request(coap_block_request_t *ctx, const void *data, size_t len, bool more, coap_request_cb_t cb, void *arg)
Do a block-wise request, send a single block.
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.
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.
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:139
struct sock_dtls sock_dtls_t
forward declare for async
Definition types.h:47
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:293
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:33
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.
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:221
Type for CoAP resource entry.
Definition nanocoap.h:300
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)
sock utility function definitions