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 
138 #ifndef NET_NANOCOAP_SOCK_H
139 #define NET_NANOCOAP_SOCK_H
140 
141 #include <stdint.h>
142 #include <unistd.h>
143 
144 #include "random.h"
145 #include "net/nanocoap.h"
146 #include "net/sock/udp.h"
147 #include "net/sock/util.h"
148 #if IS_USED(MODULE_NANOCOAP_DTLS)
149 #include "net/credman.h"
150 #include "net/sock/dtls.h"
151 #endif
152 
153 #ifdef __cplusplus
154 extern "C" {
155 #endif
156 
161 #ifndef CONFIG_NANOCOAP_SOCK_DTLS_TAG
162 #define CONFIG_NANOCOAP_SOCK_DTLS_TAG (0xc0ab)
163 #endif
164 
169 #ifndef CONFIG_NANOCOAP_SERVER_BUF_SIZE
170 #define CONFIG_NANOCOAP_SERVER_BUF_SIZE ((1 << (CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT + 3)) \
171  + CONFIG_NANOCOAP_URI_MAX + 16)
172 #endif
173 
177 #ifndef CONFIG_NANOCOAP_SERVER_STACK_SIZE
178 #define CONFIG_NANOCOAP_SERVER_STACK_SIZE THREAD_STACKSIZE_DEFAULT
179 #endif
180 
189 #ifndef CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN
190 #define CONFIG_NANOCOAP_SOCK_BLOCK_TOKEN (0)
191 #endif
192 
196 typedef enum {
200 
204 typedef struct {
206 #if IS_USED(MODULE_NANOCOAP_DTLS) || defined(DOXYGEN)
212 #endif
213  uint16_t msg_id;
215 
219 typedef struct {
221  const char *path;
222  uint32_t blknum;
224  uint8_t blksize;
226 
230 typedef struct {
233  uint8_t token[COAP_TOKEN_LENGTH_MAX];
234  uint8_t tkl;
235  uint8_t no_response;
237 
261  coap_pkt_t *pkt, const coap_request_ctx_t *req);
262 
274  const coap_request_ctx_t *req);
275 
300  unsigned code, unsigned type,
301  const void *payload, size_t len);
302 
329  void *buf, size_t buf_len,
330  unsigned code, unsigned type,
331  uint16_t msg_id);
332 
353  const iolist_t *reply);
354 
395 
407  const coap_pkt_t *req_pkt);
408 
415  uint16_t msg_id);
416 
432 
445 void nanocoap_notify_observers_simple(const coap_resource_t *res, uint32_t obs,
446  const void *payload, size_t payload_len);
447 
456 static inline uint16_t nanocoap_sock_next_msg_id(nanocoap_sock_t *sock)
457 {
458  return sock->msg_id++;
459 }
460 
473 int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
474 
486 
497 static inline int nanocoap_sock_connect(nanocoap_sock_t *sock,
498  const sock_udp_ep_t *local,
499  const sock_udp_ep_t *remote)
500 {
501 #if IS_USED(MODULE_NANOCOAP_DTLS)
502  sock->type = COAP_SOCKET_TYPE_UDP;
503 #endif
504  sock->msg_id = random_uint32();
505 
506  return sock_udp_create(&sock->udp, local, remote, 0);
507 }
508 
509 #if IS_USED(MODULE_NANOCOAP_DTLS) || DOXYGEN
523  const sock_udp_ep_t *remote, credman_tag_t tag);
524 #endif
525 
535 int nanocoap_sock_url_connect(const char *url, nanocoap_sock_t *sock);
536 
542 static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
543 {
544 #if IS_USED(MODULE_NANOCOAP_DTLS)
545  if (sock->type == COAP_SOCKET_TYPE_DTLS) {
547  sock_dtls_close(&sock->dtls);
548  }
549 #endif
550  sock_udp_close(&sock->udp);
551 }
552 
564 ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
565  size_t len);
566 
578 ssize_t nanocoap_sock_get_non(nanocoap_sock_t *sock, const char *path,
579  void *response, size_t len_max);
580 
594 ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
595  const void *request, size_t len,
596  void *response, size_t len_max);
597 
613 ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path,
614  const void *request, size_t len,
615  void *response, size_t len_max);
616 
629 ssize_t nanocoap_sock_put_url(const char *url,
630  const void *request, size_t len,
631  void *response, size_t len_max);
632 
646 ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
647  const void *request, size_t len,
648  void *response, size_t len_max);
649 
665 ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path,
666  const void *request, size_t len,
667  void *response, size_t len_max);
668 
681 ssize_t nanocoap_sock_post_url(const char *url,
682  const void *request, size_t len,
683  void *response, size_t len_max);
684 
699 ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path,
700  const void *request, size_t len,
701  void *response, size_t len_max);
702 
719 ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path,
720  const void *request, size_t len,
721  void *response, size_t len_max);
722 
736 ssize_t nanocoap_sock_fetch_url(const char *url,
737  const void *request, size_t len,
738  void *response, size_t len_max);
739 
749 ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path);
750 
759 ssize_t nanocoap_sock_delete_url(const char *url);
760 
777 int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path,
778  coap_blksize_t blksize,
779  coap_blockwise_cb_t callback, void *arg);
780 
796 int nanocoap_sock_get_slice(nanocoap_sock_t *sock, const char *path,
797  coap_blksize_t blksize, size_t offset,
798  void *dst, size_t len);
799 
817 int nanocoap_get_blockwise_url(const char *url,
818  coap_blksize_t blksize,
819  coap_blockwise_cb_t callback, void *arg);
820 
840 ssize_t nanocoap_get_blockwise_url_to_buf(const char *url,
841  coap_blksize_t blksize,
842  void *buf, size_t len);
843 
863 ssize_t nanocoap_get_blockwise_to_buf(nanocoap_sock_t *sock, const char *path,
864  coap_blksize_t blksize,
865  void *buf, size_t len);
866 
879 ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len);
880 
905  coap_request_cb_t cb, void *arg);
906 
920 ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
921  const sock_udp_ep_t *remote, size_t len);
922 
936  nanocoap_sock_t *sock,
937  const char *url,
938  coap_method_t method,
939  coap_blksize_t blksize)
940 {
941  ctx->sock = sock;
942  ctx->path = sock_urlpath(url);
943  ctx->blknum = 0;
944  ctx->method = method;
945  ctx->blksize = blksize;
946  return nanocoap_sock_url_connect(url, ctx->sock);
947 }
948 
970  const void *data, size_t len, bool more,
971  coap_request_cb_t cb, void *arg);
972 #ifdef __cplusplus
973 }
974 #endif
975 #endif /* NET_NANOCOAP_SOCK_H */
(D)TLS credentials management module definitions
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:139
coap_blksize_t
Coap block-wise-transfer size SZX.
Definition: coap.h:620
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:96
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:277
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition: nanocoap.h:289
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_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_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_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, size_t len)
Simple synchronous CoAP (confirmable) GET.
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.
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
void sock_dtls_close(sock_dtls_t *sock)
Closes a DTLS sock.
void sock_dtls_session_destroy(sock_dtls_t *sock, sock_dtls_session_t *remote)
Destroys an existing DTLS session.
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.
const char * sock_urlpath(const char *url)
Returns a pointer to the path component in url.
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.
CoAP resource request handler context.
Definition: nanocoap.h:328
Common IP-based transport layer end point.
Definition: sock.h:215
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
CoAP PDU parsing context structure.
Definition: nanocoap.h:222
Type for CoAP resource entry.
Definition: nanocoap.h:301
iolist structure definition
Definition: iolist.h:39
Context from CoAP request for separate response.
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
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)
Information about remote client connected to the server.
Information about DTLS sock.
UDP sock type.
Definition: sock_types.h:128
sock utility function definitions