gcoap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016 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 #include "net/sock/udp.h"
216 #include "mutex.h"
217 #include "nanocoap.h"
218 #include "xtimer.h"
219 
220 #ifdef __cplusplus
221 extern "C" {
222 #endif
223 
227 #define GCOAP_MSG_QUEUE_SIZE (4)
228 
232 #ifndef GCOAP_PORT
233 #define GCOAP_PORT (5683)
234 #endif
235 
239 #ifndef GCOAP_PDU_BUF_SIZE
240 #define GCOAP_PDU_BUF_SIZE (128)
241 #endif
242 
249 #define GCOAP_REQ_OPTIONS_BUF (40)
250 
256 #define GCOAP_RESP_OPTIONS_BUF (8)
257 
263 #define GCOAP_OBS_OPTIONS_BUF (8)
264 
268 #ifndef GCOAP_REQ_WAITING_MAX
269 #define GCOAP_REQ_WAITING_MAX (2)
270 #endif
271 
275 #define GCOAP_TOKENLEN_MAX (8)
276 
280 #define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
281 
285 #ifndef GCOAP_TOKENLEN
286 #define GCOAP_TOKENLEN (2)
287 #endif
288 
292 #define GCOAP_PAYLOAD_MARKER (0xFF)
293 
298 #define GCOAP_MEMO_UNUSED (0)
299 #define GCOAP_MEMO_WAIT (1)
300 #define GCOAP_MEMO_RESP (2)
301 #define GCOAP_MEMO_TIMEOUT (3)
302 #define GCOAP_MEMO_ERR (4)
308 #ifndef GCOAP_RECV_TIMEOUT
309 #define GCOAP_RECV_TIMEOUT (1 * US_PER_SEC)
310 #endif
311 
317 #ifndef GCOAP_NON_TIMEOUT
318 #define GCOAP_NON_TIMEOUT (5000000U)
319 #endif
320 
324 #define GCOAP_MSG_TYPE_TIMEOUT (0x1501)
325 
332 #define GCOAP_MSG_TYPE_INTR (0x1502)
333 
337 #ifndef GCOAP_OBS_CLIENTS_MAX
338 #define GCOAP_OBS_CLIENTS_MAX (2)
339 #endif
340 
345 #ifndef GCOAP_OBS_REGISTRATIONS_MAX
346 #define GCOAP_OBS_REGISTRATIONS_MAX (2)
347 #endif
348 
353 #define GCOAP_OBS_MEMO_UNUSED (0)
354 #define GCOAP_OBS_MEMO_IDLE (1)
355 #define GCOAP_OBS_MEMO_PENDING (2)
377 #ifndef GCOAP_OBS_VALUE_WIDTH
378 #define GCOAP_OBS_VALUE_WIDTH (3)
379 #endif
380 
384 #if (GCOAP_OBS_VALUE_WIDTH == 3)
385 #define GCOAP_OBS_TICK_EXPONENT (5)
386 #elif (GCOAP_OBS_VALUE_WIDTH == 2)
387 #define GCOAP_OBS_TICK_EXPONENT (16)
388 #elif (GCOAP_OBS_VALUE_WIDTH == 1)
389 #define GCOAP_OBS_TICK_EXPONENT (24)
390 #endif
391 
396 #define GCOAP_OBS_INIT_OK (0)
397 #define GCOAP_OBS_INIT_ERR (-1)
398 #define GCOAP_OBS_INIT_UNUSED (-2)
399 
404 #ifndef GCOAP_STACK_SIZE
405 #define GCOAP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT + DEBUG_EXTRA_STACKSIZE)
406 #endif
407 
411 typedef struct gcoap_listener {
412  coap_resource_t *resources;
414  size_t resources_len;
417 
424 typedef void (*gcoap_resp_handler_t)(unsigned req_state, coap_pkt_t* pdu,
425  sock_udp_ep_t *remote);
426 
430 typedef struct {
431  unsigned state;
432  uint8_t hdr_buf[GCOAP_HEADER_MAXLEN];
438 
442 typedef struct {
444  coap_resource_t *resource;
445  uint8_t token[GCOAP_TOKENLEN_MAX];
446  unsigned token_len;
448 
452 typedef struct {
459  atomic_uint next_message_id;
465 } gcoap_state_t;
466 
477 
484 
497 int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
498  unsigned code, char *path);
499 
514 ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format);
515 
528 static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len,
529  unsigned code, char *path)
530 {
531  return (gcoap_req_init(pdu, buf, len, code, path) == 0)
532  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
533  : -1;
534 }
535 
547 size_t gcoap_req_send2(const uint8_t *buf, size_t len,
548  const sock_udp_ep_t *remote,
549  gcoap_resp_handler_t resp_handler);
550 
565 size_t gcoap_req_send(const uint8_t *buf, size_t len, const ipv6_addr_t *addr,
566  uint16_t port, gcoap_resp_handler_t resp_handler);
567 
581 int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
582 
594 static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf,
595  size_t len, unsigned code)
596 {
597  return (gcoap_resp_init(pdu, buf, len, code) == 0)
598  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
599  : -1;
600 }
601 
617 int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
618  const coap_resource_t *resource);
619 
633 size_t gcoap_obs_send(const uint8_t *buf, size_t len,
634  const coap_resource_t *resource);
635 
643 uint8_t gcoap_op_state(void);
644 
663 int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf);
664 
680 int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val);
681 
682 #ifdef __cplusplus
683 }
684 #endif
685 
686 #endif /* NET_GCOAP_H */
687 
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:444
struct gcoap_listener * next
Next listener in list.
Definition: gcoap.h:415
#define GCOAP_HEADER_MAXLEN
Maximum length in bytes for a header, including the token.
Definition: gcoap.h:280
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
Memo for Observe registration and notifications.
Definition: gcoap.h:442
xtimer_t response_timer
Limits wait for response.
Definition: gcoap.h:435
UDP sock definitions.
mutex_t lock
Shares state attributes safely.
Definition: gcoap.h:453
uint8_t gcoap_op_state(void)
Provides important operational statistics.
size_t resources_len
Length of array.
Definition: gcoap.h:414
#define GCOAP_TOKENLEN_MAX
Maximum length in bytes for a token.
Definition: gcoap.h:275
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:528
Container for the state of gcoap itself.
Definition: gcoap.h:452
#define GCOAP_OBS_REGISTRATIONS_MAX
Maximum number of registrations for Observable resources; use 2 if not defined.
Definition: gcoap.h:346
#define GCOAP_OBS_CLIENTS_MAX
Maximum number of Observe clients; use 2 if not defined.
Definition: gcoap.h:338
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.
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:431
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:424
coap_resource_t * resources
First element in the array of resources; must order alphabetically.
Definition: gcoap.h:412
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:411
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:459
Common IP-based transport layer end point.
Definition: sock.h:195
xtimer interface definitions
Describes a message object which can be sent between threads.
Definition: msg.h:184
#define GCOAP_REQ_WAITING_MAX
Maximum number of requests awaiting a response.
Definition: gcoap.h:269
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:446
Memo to handle a response for a request.
Definition: gcoap.h:430
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:594
gcoap_listener_t * listeners
List of registered listeners.
Definition: gcoap.h:454
sock_udp_ep_t * observer
Client endpoint; unused if null.
Definition: gcoap.h:443
gcoap_resp_handler_t resp_handler
Callback for the response.
Definition: gcoap.h:434
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.
kernel_pid_t gcoap_init(void)
Initializes the gcoap thread and device.
msg_t timeout_msg
For response timer.
Definition: gcoap.h:436
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.
int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val)
Adds a single Uri-Query option to a CoAP request.