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 "net/nanocoap.h"
136 #include "net/sock/udp.h"
137 #include "net/sock/util.h"
138 
139 #ifdef __cplusplus
140 extern "C" {
141 #endif
142 
148 
152 typedef struct {
154  const char *path;
155  uint32_t blknum;
156  uint8_t method;
157  uint8_t blksize;
159 
172 int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
173 
184 static inline int nanocoap_sock_connect(nanocoap_sock_t *sock,
185  const sock_udp_ep_t *local,
186  const sock_udp_ep_t *remote)
187 {
188  return sock_udp_create(sock, local, remote, 0);
189 }
190 
200 int nanocoap_sock_url_connect(const char *url, nanocoap_sock_t *sock);
201 
207 static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
208 {
209  sock_udp_close(sock);
210 }
211 
223 ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
224  size_t len);
225 
239 ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
240  const void *request, size_t len,
241  void *response, size_t len_max);
242 
258 ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path,
259  const void *request, size_t len,
260  void *response, size_t len_max);
261 
274 ssize_t nanocoap_sock_put_url(const char *url,
275  const void *request, size_t len,
276  void *response, size_t len_max);
277 
291 ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
292  const void *request, size_t len,
293  void *response, size_t len_max);
294 
310 ssize_t nanocoap_sock_post_non(nanocoap_sock_t *sock, const char *path,
311  const void *request, size_t len,
312  void *response, size_t len_max);
313 
326 ssize_t nanocoap_sock_post_url(const char *url,
327  const void *request, size_t len,
328  void *response, size_t len_max);
329 
346 int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path,
347  coap_blksize_t blksize,
348  coap_blockwise_cb_t callback, void *arg);
349 
367 int nanocoap_get_blockwise_url(const char *url,
368  coap_blksize_t blksize,
369  coap_blockwise_cb_t callback, void *arg);
370 
390 ssize_t nanocoap_get_blockwise_url_to_buf(const char *url,
391  coap_blksize_t blksize,
392  void *buf, size_t len);
393 
406 ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len);
407 
424  coap_request_cb_t cb, void *arg);
425 
439 ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
440  const sock_udp_ep_t *remote, size_t len);
441 
453 ssize_t nanocoap_get(const sock_udp_ep_t *remote, const char *path,
454  void *buf, size_t len);
455 
469  nanocoap_sock_t *sock,
470  const char *url,
471  uint8_t method,
472  coap_blksize_t blksize)
473 {
474  ctx->sock = sock;
475  ctx->path = sock_urlpath(url);
476  ctx->blknum = 0;
477  ctx->method = method;
478  ctx->blksize = blksize;
479  return nanocoap_sock_url_connect(url, ctx->sock);
480 }
481 
503  const void *data, size_t len, bool more,
504  coap_request_cb_t cb, void *arg);
505 #ifdef __cplusplus
506 }
507 #endif
508 #endif /* NET_NANOCOAP_SOCK_H */
coap_blksize_t
Coap block-wise-transfer size SZX.
Definition: coap.h:282
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.
sock_udp_t nanocoap_sock_t
nanocoap socket type
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_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.
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_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.
static void nanocoap_sock_close(nanocoap_sock_t *sock)
Close a CoAP client socket.
ssize_t nanocoap_sock_request_cb(sock_udp_t *sock, coap_pkt_t *pkt, coap_request_cb_t cb, void *arg)
Simple synchronous CoAP request with callback.
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.
nanocoap API
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
UDP sock type.
Definition: sock_types.h:128
sock utility function definitions