gcoap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2017 Ken Bannister. All rights reserved.
3  * 2017 Freie Universit├Ąt Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
210 #ifndef NET_GCOAP_H
211 #define NET_GCOAP_H
212 
213 #include <stdint.h>
214 #include <stdatomic.h>
215 
216 #include "net/ipv6/addr.h"
217 #include "net/sock/udp.h"
218 #include "mutex.h"
219 #include "net/nanocoap.h"
220 #include "xtimer.h"
221 
222 #ifdef __cplusplus
223 extern "C" {
224 #endif
225 
229 #define GCOAP_MSG_QUEUE_SIZE (4)
230 
234 #ifndef GCOAP_PORT
235 #define GCOAP_PORT (5683)
236 #endif
237 
241 #ifndef GCOAP_PDU_BUF_SIZE
242 #define GCOAP_PDU_BUF_SIZE (128)
243 #endif
244 
251 #define GCOAP_REQ_OPTIONS_BUF (40)
252 
258 #define GCOAP_RESP_OPTIONS_BUF (8)
259 
265 #define GCOAP_OBS_OPTIONS_BUF (8)
266 
270 #ifndef GCOAP_REQ_WAITING_MAX
271 #define GCOAP_REQ_WAITING_MAX (2)
272 #endif
273 
277 #define GCOAP_TOKENLEN_MAX (8)
278 
282 #define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
283 
287 #ifndef GCOAP_TOKENLEN
288 #define GCOAP_TOKENLEN (2)
289 #endif
290 
294 #define GCOAP_PAYLOAD_MARKER (0xFF)
295 
300 #define GCOAP_MEMO_UNUSED (0)
301 #define GCOAP_MEMO_WAIT (1)
302 #define GCOAP_MEMO_RESP (2)
303 #define GCOAP_MEMO_TIMEOUT (3)
304 #define GCOAP_MEMO_ERR (4)
310 #define GCOAP_SEND_LIMIT_NON (-1)
311 
315 #ifndef GCOAP_RECV_TIMEOUT
316 #define GCOAP_RECV_TIMEOUT (1 * US_PER_SEC)
317 #endif
318 
324 #ifndef GCOAP_NON_TIMEOUT
325 #define GCOAP_NON_TIMEOUT (5000000U)
326 #endif
327 
331 #define GCOAP_MSG_TYPE_TIMEOUT (0x1501)
332 
339 #define GCOAP_MSG_TYPE_INTR (0x1502)
340 
344 #ifndef GCOAP_OBS_CLIENTS_MAX
345 #define GCOAP_OBS_CLIENTS_MAX (2)
346 #endif
347 
352 #ifndef GCOAP_OBS_REGISTRATIONS_MAX
353 #define GCOAP_OBS_REGISTRATIONS_MAX (2)
354 #endif
355 
360 #define GCOAP_OBS_MEMO_UNUSED (0)
361 #define GCOAP_OBS_MEMO_IDLE (1)
362 #define GCOAP_OBS_MEMO_PENDING (2)
384 #ifndef GCOAP_OBS_VALUE_WIDTH
385 #define GCOAP_OBS_VALUE_WIDTH (3)
386 #endif
387 
391 #if (GCOAP_OBS_VALUE_WIDTH == 3)
392 #define GCOAP_OBS_TICK_EXPONENT (5)
393 #elif (GCOAP_OBS_VALUE_WIDTH == 2)
394 #define GCOAP_OBS_TICK_EXPONENT (16)
395 #elif (GCOAP_OBS_VALUE_WIDTH == 1)
396 #define GCOAP_OBS_TICK_EXPONENT (24)
397 #endif
398 
403 #define GCOAP_OBS_INIT_OK (0)
404 #define GCOAP_OBS_INIT_ERR (-1)
405 #define GCOAP_OBS_INIT_UNUSED (-2)
406 
411 #ifndef GCOAP_STACK_SIZE
412 #define GCOAP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT + DEBUG_EXTRA_STACKSIZE)
413 #endif
414 
418 typedef struct gcoap_listener {
421  size_t resources_len;
424 
431 typedef void (*gcoap_resp_handler_t)(unsigned req_state, coap_pkt_t* pdu,
432  sock_udp_ep_t *remote);
433 
437 typedef struct {
439  uint8_t *pdu_buf;
440  size_t pdu_len;
442 
446 typedef struct {
447  unsigned state;
450  union {
451  uint8_t hdr_buf[GCOAP_HEADER_MAXLEN];
454  } msg;
460 
464 typedef struct {
467  uint8_t token[GCOAP_TOKENLEN_MAX];
468  unsigned token_len;
470 
474 typedef struct {
481  atomic_uint next_message_id;
487 } gcoap_state_t;
488 
499 
506 
519 int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
520  unsigned code, char *path);
521 
536 ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format);
537 
550 static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len,
551  unsigned code, char *path)
552 {
553  return (gcoap_req_init(pdu, buf, len, code, path) == 0)
554  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
555  : -1;
556 }
557 
569 size_t gcoap_req_send2(const uint8_t *buf, size_t len,
570  const sock_udp_ep_t *remote,
571  gcoap_resp_handler_t resp_handler);
572 
587 size_t gcoap_req_send(const uint8_t *buf, size_t len, const ipv6_addr_t *addr,
588  uint16_t port, gcoap_resp_handler_t resp_handler);
589 
603 int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
604 
616 static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf,
617  size_t len, unsigned code)
618 {
619  return (gcoap_resp_init(pdu, buf, len, code) == 0)
620  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
621  : -1;
622 }
623 
639 int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
640  const coap_resource_t *resource);
641 
655 size_t gcoap_obs_send(const uint8_t *buf, size_t len,
656  const coap_resource_t *resource);
657 
665 uint8_t gcoap_op_state(void);
666 
685 int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf);
686 
702 int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val);
703 
704 #ifdef __cplusplus
705 }
706 #endif
707 
708 #endif /* NET_GCOAP_H */
709 
int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, const coap_resource_t *resource)
Initializes a CoAP Observe notification packet on a buffer, for the observer registered for a resourc...
coap_resource_t * resource
Entity being observed.
Definition: gcoap.h:466
struct gcoap_listener * next
Next listener in list.
Definition: gcoap.h:422
Definitions for IPv6 addresses.
#define GCOAP_HEADER_MAXLEN
Maximum length in bytes for a header, including the token.
Definition: gcoap.h:282
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
Memo for Observe registration and notifications.
Definition: gcoap.h:464
xtimer_t response_timer
Limits wait for response.
Definition: gcoap.h:457
UDP sock definitions.
mutex_t lock
Shares state attributes safely.
Definition: gcoap.h:475
uint8_t gcoap_op_state(void)
Provides important operational statistics.
size_t resources_len
Length of array.
Definition: gcoap.h:421
#define GCOAP_TOKENLEN_MAX
Maximum length in bytes for a token.
Definition: gcoap.h:277
size_t pdu_len
Length of pdu_buf.
Definition: gcoap.h:440
static ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code, char *path)
Writes a complete CoAP request PDU when there is not a payload.
Definition: gcoap.h:550
Container for the state of gcoap itself.
Definition: gcoap.h:474
#define GCOAP_OBS_REGISTRATIONS_MAX
Maximum number of registrations for Observable resources; use 2 if not defined.
Definition: gcoap.h:353
#define GCOAP_OBS_CLIENTS_MAX
Maximum number of Observe clients; use 2 if not defined.
Definition: gcoap.h:345
ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format)
Finishes formatting a CoAP PDU after the payload has been written.
Type for CoAP resource entry.
Definition: nanocoap.h:232
void gcoap_register_listener(gcoap_listener_t *listener)
Starts listening for resource paths.
unsigned state
State of this memo, a GCOAP_MEMO...
Definition: gcoap.h:447
void(* gcoap_resp_handler_t)(unsigned req_state, coap_pkt_t *pdu, sock_udp_ep_t *remote)
Handler function for a server response, including the state for the originating request.
Definition: gcoap.h:431
coap_resource_t * resources
First element in the array of resources; must order alphabetically.
Definition: gcoap.h:419
size_t gcoap_req_send(const uint8_t *buf, size_t len, const ipv6_addr_t *addr, uint16_t port, gcoap_resp_handler_t resp_handler)
Sends a buffer containing a CoAP request to the provided host/port.
A modular collection of resources for a server.
Definition: gcoap.h:418
signed int ssize_t
Used for a count of bytes or an error indication.
Definition: msp430_types.h:89
int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
Initializes a CoAP response packet on a buffer.
atomic_uint next_message_id
Next message ID to use.
Definition: gcoap.h:481
nanocoap API
uint8_t * pdu_buf
Buffer containing the PDU.
Definition: gcoap.h:439
Common IP-based transport layer end point.
Definition: sock.h:195
sock_udp_ep_t remote_ep
Remote endpoint.
Definition: gcoap.h:438
xtimer interface definitions
Describes a message object which can be sent between threads.
Definition: msg.h:184
Extends request memo for resending a confirmable request.
Definition: gcoap.h:437
#define COAP_FORMAT_NONE
nanocoap-specific value to indicate no format specified.
Definition: nanocoap.h:178
#define GCOAP_REQ_WAITING_MAX
Maximum number of requests awaiting a response.
Definition: gcoap.h:271
RIOT synchronization API.
struct gcoap_listener gcoap_listener_t
A modular collection of resources for a server.
unsigned token_len
Actual length of token attribute.
Definition: gcoap.h:468
Memo to handle a response for a request.
Definition: gcoap.h:446
static ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
Writes a complete CoAP response PDU when there is no payload.
Definition: gcoap.h:616
gcoap_listener_t * listeners
List of registered listeners.
Definition: gcoap.h:476
sock_udp_ep_t * observer
Client endpoint; unused if null.
Definition: gcoap.h:465
gcoap_resp_handler_t resp_handler
Callback for the response.
Definition: gcoap.h:456
gcoap_resend_t data
Endpoint and PDU buffer, for resend.
Definition: gcoap.h:453
size_t gcoap_obs_send(const uint8_t *buf, size_t len, const coap_resource_t *resource)
Sends a buffer containing a CoAP Observe notification to the observer registered for a resource...
Data type to represent an IPv6 address.
Definition: ipv6/addr.h:74
xtimer timer structure
Definition: xtimer.h:70
Mutex structure.
Definition: mutex.h:36
int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code, char *path)
Initializes a CoAP request PDU on a buffer.
int send_limit
Remaining resends, 0 if none; GCOAP_SEND_LIMIT_NON if non-confirmable.
Definition: gcoap.h:448
kernel_pid_t gcoap_init(void)
Initializes the gcoap thread and device.
msg_t timeout_msg
For response timer.
Definition: gcoap.h:458
size_t gcoap_req_send2(const uint8_t *buf, size_t len, const sock_udp_ep_t *remote, gcoap_resp_handler_t resp_handler)
Sends a buffer containing a CoAP request to the provided endpoint.
int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf)
Get the resource list, currently only CoRE Link Format (COAP_FORMAT_LINK) supported.
CoAP option array entry.
Definition: nanocoap.h:213
int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val)
Adds a single Uri-Query option to a CoAP request.