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
135#ifndef NET_NANOCOAP_SOCK_H
136#define NET_NANOCOAP_SOCK_H
137
138#include <stdint.h>
139#include <unistd.h>
140
141#include "random.h"
142#include "net/nanocoap.h"
143#include "net/sock/udp.h"
144#include "net/sock/util.h"
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
197
201typedef struct {
203#if IS_USED(MODULE_NANOCOAP_DTLS) || defined(DOXYGEN)
209#endif
210 uint16_t msg_id;
212
216typedef struct {
218 const char *path;
219 uint32_t blknum;
221 uint8_t blksize;
223
232static inline uint16_t nanocoap_sock_next_msg_id(nanocoap_sock_t *sock)
233{
234 return sock->msg_id++;
235}
236
249int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
250
262
274 const sock_udp_ep_t *local,
275 const sock_udp_ep_t *remote)
276{
277#if IS_USED(MODULE_NANOCOAP_DTLS)
279#endif
280 sock->msg_id = random_uint32();
281
282 return sock_udp_create(&sock->udp, local, remote, 0);
283}
284
285#if IS_USED(MODULE_NANOCOAP_DTLS) || DOXYGEN
299 const sock_udp_ep_t *remote, credman_tag_t tag);
300#endif
301
311int nanocoap_sock_url_connect(const char *url, nanocoap_sock_t *sock);
312
318static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
319{
320#if IS_USED(MODULE_NANOCOAP_DTLS)
321 if (sock->type == COAP_SOCKET_TYPE_DTLS) {
322 sock_dtls_session_destroy(&sock->dtls, &sock->dtls_session);
323 sock_dtls_close(&sock->dtls);
324 }
325#endif
326 sock_udp_close(&sock->udp);
327}
328
340ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
341 size_t len);
342
356ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
357 const void *request, size_t len,
358 void *response, size_t len_max);
359
375ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path,
376 const void *request, size_t len,
377 void *response, size_t len_max);
378
391ssize_t nanocoap_sock_put_url(const char *url,
392 const void *request, size_t len,
393 void *response, size_t len_max);
394
408ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
409 const void *request, size_t len,
410 void *response, size_t len_max);
411
427ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path,
428 const void *request, size_t len,
429 void *response, size_t len_max);
430
443ssize_t nanocoap_sock_post_url(const char *url,
444 const void *request, size_t len,
445 void *response, size_t len_max);
446
461ssize_t nanocoap_sock_fetch(nanocoap_sock_t *sock, const char *path,
462 const void *request, size_t len,
463 void *response, size_t len_max);
464
481ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path,
482 const void *request, size_t len,
483 void *response, size_t len_max);
484
498ssize_t nanocoap_sock_fetch_url(const char *url,
499 const void *request, size_t len,
500 void *response, size_t len_max);
501
511ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path);
512
521ssize_t nanocoap_sock_delete_url(const char *url);
522
540 coap_blksize_t blksize,
541 coap_blockwise_cb_t callback, void *arg);
542
560int nanocoap_get_blockwise_url(const char *url,
561 coap_blksize_t blksize,
562 coap_blockwise_cb_t callback, void *arg);
563
583ssize_t nanocoap_get_blockwise_url_to_buf(const char *url,
584 coap_blksize_t blksize,
585 void *buf, size_t len);
586
599ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len);
600
617 coap_request_cb_t cb, void *arg);
618
632ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
633 const sock_udp_ep_t *remote, size_t len);
634
648 nanocoap_sock_t *sock,
649 const char *url,
650 coap_method_t method,
651 coap_blksize_t blksize)
652{
653 ctx->sock = sock;
654 ctx->path = sock_urlpath(url);
655 ctx->blknum = 0;
656 ctx->method = method;
657 ctx->blksize = blksize;
658 return nanocoap_sock_url_connect(url, ctx->sock);
659}
660
682 const void *data, size_t len, bool more,
683 coap_request_cb_t cb, void *arg);
684#ifdef __cplusplus
685}
686#endif
687#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:618
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:287
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap request callback descriptor.
Definition nanocoap.h:299
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_delete(nanocoap_sock_t *sock, const char *path)
Simple synchronous CoAP (confirmable) DELETE.
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.
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.
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.
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.
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.
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.
nanocoap_socket_type_t
NanoCoAP socket types.
static void nanocoap_sock_close(nanocoap_sock_t *sock)
Close a CoAP client socket.
@ COAP_SOCKET_TYPE_UDP
transport is plain UDP
@ COAP_SOCKET_TYPE_DTLS
transport is DTLS
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.
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:232
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