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 
129 #ifndef NET_NANOCOAP_SOCK_H
130 #define NET_NANOCOAP_SOCK_H
131 
132 #include <stdint.h>
133 #include <unistd.h>
134 
135 #include "random.h"
136 #include "net/nanocoap.h"
137 #include "net/sock/udp.h"
138 #include "net/sock/util.h"
139 #if IS_USED(MODULE_NANOCOAP_DTLS)
140 #include "net/credman.h"
141 #include "net/sock/dtls.h"
142 #endif
143 
144 #ifdef __cplusplus
145 extern "C" {
146 #endif
147 
152 #ifndef CONFIG_NANOCOAP_SOCK_DTLS_TAG
153 #define CONFIG_NANOCOAP_SOCK_DTLS_TAG (0xc0ab)
154 #endif
155 
159 typedef enum {
163 
167 typedef struct {
169 #if IS_USED(MODULE_NANOCOAP_DTLS) || defined(DOXYGEN)
175 #endif
176  uint16_t msg_id;
178 
182 typedef struct {
184  const char *path;
185  uint32_t blknum;
186  uint8_t method;
187  uint8_t blksize;
189 
198 static inline uint16_t nanocoap_sock_next_msg_id(nanocoap_sock_t *sock)
199 {
200  return sock->msg_id++;
201 }
202 
215 int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
216 
227 static inline int nanocoap_sock_connect(nanocoap_sock_t *sock,
228  const sock_udp_ep_t *local,
229  const sock_udp_ep_t *remote)
230 {
231 #if IS_USED(MODULE_NANOCOAP_DTLS)
232  sock->type = COAP_SOCKET_TYPE_UDP;
233 #endif
234  sock->msg_id = random_uint32();
235 
236  return sock_udp_create(&sock->udp, local, remote, 0);
237 }
238 
239 #if IS_USED(MODULE_NANOCOAP_DTLS) || DOXYGEN
253  const sock_udp_ep_t *remote, credman_tag_t tag);
254 #endif
255 
265 int nanocoap_sock_url_connect(const char *url, nanocoap_sock_t *sock);
266 
272 static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
273 {
274 #if IS_USED(MODULE_NANOCOAP_DTLS)
275  if (sock->type == COAP_SOCKET_TYPE_DTLS) {
277  sock_dtls_close(&sock->dtls);
278  }
279 #endif
280  sock_udp_close(&sock->udp);
281 }
282 
294 ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
295  size_t len);
296 
310 ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
311  const void *request, size_t len,
312  void *response, size_t len_max);
313 
329 ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path,
330  const void *request, size_t len,
331  void *response, size_t len_max);
332 
345 ssize_t nanocoap_sock_put_url(const char *url,
346  const void *request, size_t len,
347  void *response, size_t len_max);
348 
362 ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
363  const void *request, size_t len,
364  void *response, size_t len_max);
365 
381 ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path,
382  const void *request, size_t len,
383  void *response, size_t len_max);
384 
397 ssize_t nanocoap_sock_post_url(const char *url,
398  const void *request, size_t len,
399  void *response, size_t len_max);
400 
410 ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path);
411 
420 ssize_t nanocoap_sock_delete_url(const char *url);
421 
438 int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path,
439  coap_blksize_t blksize,
440  coap_blockwise_cb_t callback, void *arg);
441 
459 int nanocoap_get_blockwise_url(const char *url,
460  coap_blksize_t blksize,
461  coap_blockwise_cb_t callback, void *arg);
462 
482 ssize_t nanocoap_get_blockwise_url_to_buf(const char *url,
483  coap_blksize_t blksize,
484  void *buf, size_t len);
485 
498 ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len);
499 
516  coap_request_cb_t cb, void *arg);
517 
531 ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
532  const sock_udp_ep_t *remote, size_t len);
533 
548 ssize_t nanocoap_get(const sock_udp_ep_t *remote, const char *path,
549  void *buf, size_t len);
550 
564  nanocoap_sock_t *sock,
565  const char *url,
566  uint8_t method,
567  coap_blksize_t blksize)
568 {
569  ctx->sock = sock;
570  ctx->path = sock_urlpath(url);
571  ctx->blknum = 0;
572  ctx->method = method;
573  ctx->blksize = blksize;
574  return nanocoap_sock_url_connect(url, ctx->sock);
575 }
576 
598  const void *data, size_t len, bool more,
599  coap_request_cb_t cb, void *arg);
600 #ifdef __cplusplus
601 }
602 #endif
603 #endif /* NET_NANOCOAP_SOCK_H */
(D)TLS credentials management module definitions
coap_blksize_t
Coap block-wise-transfer size SZX.
Definition: coap.h:296
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:281
int(* coap_request_cb_t)(void *arg, coap_pkt_t *pkt)
Coap equest callback descriptor.
Definition: nanocoap.h:293
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.
ssize_t nanocoap_get(const sock_udp_ep_t *remote, const char *path, void *buf, size_t len)
Simple synchronous CoAP (confirmable) get.
ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len)
Simple synchronous CoAP request.
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.
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.
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.
static int nanocoap_block_request_connect_url(coap_block_request_t *ctx, nanocoap_sock_t *sock, const char *url, uint8_t method, coap_blksize_t blksize)
Initialize block request context by URL and connect a 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
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.
Common IP-based transport layer end point.
Definition: sock.h:215
Blockwise request helper struct.
uint8_t method
request method (GET, POST, PUT)
nanocoap_sock_t * sock
socket used for the request
uint32_t blknum
current block number
const char * path
path on the server
uint8_t blksize
CoAP blocksize exponent
CoAP PDU parsing context structure.
Definition: nanocoap.h:226
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