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 
220 #ifndef NET_GCOAP_H
221 #define NET_GCOAP_H
222 
223 #include <stdint.h>
224 
225 #include "net/ipv6/addr.h"
226 #include "net/sock/udp.h"
227 #include "net/nanocoap.h"
228 #include "xtimer.h"
229 
230 #ifdef __cplusplus
231 extern "C" {
232 #endif
233 
237 #ifndef GCOAP_MSG_QUEUE_SIZE
238 #define GCOAP_MSG_QUEUE_SIZE (4)
239 #endif
240 
244 #ifndef GCOAP_PORT
245 #define GCOAP_PORT (5683)
246 #endif
247 
251 #ifndef GCOAP_PDU_BUF_SIZE
252 #define GCOAP_PDU_BUF_SIZE (128)
253 #endif
254 
261 #define GCOAP_REQ_OPTIONS_BUF (40)
262 
268 #define GCOAP_RESP_OPTIONS_BUF (8)
269 
275 #define GCOAP_OBS_OPTIONS_BUF (8)
276 
280 #ifndef GCOAP_REQ_WAITING_MAX
281 #define GCOAP_REQ_WAITING_MAX (2)
282 #endif
283 
287 #define GCOAP_TOKENLEN_MAX (8)
288 
292 #define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
293 
297 #ifndef GCOAP_TOKENLEN
298 #define GCOAP_TOKENLEN (2)
299 #endif
300 
304 #define GCOAP_PAYLOAD_MARKER (0xFF)
305 
310 #define GCOAP_MEMO_UNUSED (0)
311 #define GCOAP_MEMO_WAIT (1)
312 #define GCOAP_MEMO_RESP (2)
313 #define GCOAP_MEMO_TIMEOUT (3)
314 #define GCOAP_MEMO_ERR (4)
320 #define GCOAP_SEND_LIMIT_NON (-1)
321 
325 #ifndef GCOAP_RECV_TIMEOUT
326 #define GCOAP_RECV_TIMEOUT (1 * US_PER_SEC)
327 #endif
328 
334 #ifndef GCOAP_NON_TIMEOUT
335 #define GCOAP_NON_TIMEOUT (5000000U)
336 #endif
337 
341 #define GCOAP_MSG_TYPE_TIMEOUT (0x1501)
342 
349 #define GCOAP_MSG_TYPE_INTR (0x1502)
350 
354 #ifndef GCOAP_OBS_CLIENTS_MAX
355 #define GCOAP_OBS_CLIENTS_MAX (2)
356 #endif
357 
362 #ifndef GCOAP_OBS_REGISTRATIONS_MAX
363 #define GCOAP_OBS_REGISTRATIONS_MAX (2)
364 #endif
365 
370 #define GCOAP_OBS_MEMO_UNUSED (0)
371 #define GCOAP_OBS_MEMO_IDLE (1)
372 #define GCOAP_OBS_MEMO_PENDING (2)
394 #ifndef GCOAP_OBS_VALUE_WIDTH
395 #define GCOAP_OBS_VALUE_WIDTH (3)
396 #endif
397 
401 #if (GCOAP_OBS_VALUE_WIDTH == 3)
402 #define GCOAP_OBS_TICK_EXPONENT (5)
403 #elif (GCOAP_OBS_VALUE_WIDTH == 2)
404 #define GCOAP_OBS_TICK_EXPONENT (16)
405 #elif (GCOAP_OBS_VALUE_WIDTH == 1)
406 #define GCOAP_OBS_TICK_EXPONENT (24)
407 #endif
408 
413 #define GCOAP_OBS_INIT_OK (0)
414 #define GCOAP_OBS_INIT_ERR (-1)
415 #define GCOAP_OBS_INIT_UNUSED (-2)
416 
421 #ifndef GCOAP_STACK_SIZE
422 #define GCOAP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT + DEBUG_EXTRA_STACKSIZE \
423  + sizeof(coap_pkt_t))
424 #endif
425 
429 #ifndef GCOAP_RESEND_BUFS_MAX
430 #define GCOAP_RESEND_BUFS_MAX (1)
431 #endif
432 
436 typedef struct gcoap_listener {
439  size_t resources_len;
442 
449 typedef void (*gcoap_resp_handler_t)(unsigned req_state, coap_pkt_t* pdu,
450  sock_udp_ep_t *remote);
451 
455 typedef struct {
456  uint8_t *pdu_buf;
457  size_t pdu_len;
459 
463 typedef struct {
464  unsigned state;
467  union {
468  uint8_t hdr_buf[GCOAP_HEADER_MAXLEN];
471  } msg;
478 
482 typedef struct {
485  uint8_t token[GCOAP_TOKENLEN_MAX];
486  unsigned token_len;
488 
499 
506 
522 int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
523  unsigned code, const char *path);
524 
539 ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format);
540 
553 static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len,
554  unsigned code, char *path)
555 {
556  return (gcoap_req_init(pdu, buf, len, code, path) == 0)
557  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
558  : -1;
559 }
560 
572 size_t gcoap_req_send2(const uint8_t *buf, size_t len,
573  const sock_udp_ep_t *remote,
574  gcoap_resp_handler_t resp_handler);
575 
590 size_t gcoap_req_send(const uint8_t *buf, size_t len, const ipv6_addr_t *addr,
591  uint16_t port, gcoap_resp_handler_t resp_handler);
592 
606 int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
607 
619 static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf,
620  size_t len, unsigned code)
621 {
622  return (gcoap_resp_init(pdu, buf, len, code) == 0)
623  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
624  : -1;
625 }
626 
642 int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
643  const coap_resource_t *resource);
644 
658 size_t gcoap_obs_send(const uint8_t *buf, size_t len,
659  const coap_resource_t *resource);
660 
668 uint8_t gcoap_op_state(void);
669 
688 int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf);
689 
705 int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val);
706 
707 #ifdef __cplusplus
708 }
709 #endif
710 
711 #endif /* NET_GCOAP_H */
712 
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...
struct gcoap_listener * next
Next listener in list.
Definition: gcoap.h:440
Definitions for IPv6 addresses.
#define GCOAP_HEADER_MAXLEN
Maximum length in bytes for a header, including the token.
Definition: gcoap.h:292
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
Memo for Observe registration and notifications.
Definition: gcoap.h:482
xtimer_t response_timer
Limits wait for response.
Definition: gcoap.h:475
UDP sock definitions.
uint8_t gcoap_op_state(void)
Provides important operational statistics.
size_t resources_len
Length of array.
Definition: gcoap.h:439
#define GCOAP_TOKENLEN_MAX
Maximum length in bytes for a token.
Definition: gcoap.h:287
size_t pdu_len
Length of pdu_buf.
Definition: gcoap.h:457
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:553
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:251
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:464
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:449
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:436
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.
nanocoap API
uint8_t * pdu_buf
Buffer containing the PDU.
Definition: gcoap.h:456
Common IP-based transport layer end point.
Definition: sock.h:195
int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code, const char *path)
Initializes a CoAP request PDU on a buffer.
xtimer interface definitions
Describes a message object which can be sent between threads.
Definition: msg.h:184
const coap_resource_t * resources
First element in the array of resources; must order alphabetically.
Definition: gcoap.h:437
Extends request memo for resending a confirmable request.
Definition: gcoap.h:455
#define COAP_FORMAT_NONE
Nanocoap-specific value to indicate no format specified.
Definition: nanocoap.h:178
const coap_resource_t * resource
Entity being observed.
Definition: gcoap.h:484
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:486
Memo to handle a response for a request.
Definition: gcoap.h:463
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:619
sock_udp_ep_t remote_ep
Remote endpoint.
Definition: gcoap.h:473
sock_udp_ep_t * observer
Client endpoint; unused if null.
Definition: gcoap.h:483
gcoap_resp_handler_t resp_handler
Callback for the response.
Definition: gcoap.h:474
gcoap_resend_t data
Endpoint and PDU buffer, for resend.
Definition: gcoap.h:470
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
int send_limit
Remaining resends, 0 if none; GCOAP_SEND_LIMIT_NON if non-confirmable.
Definition: gcoap.h:465
kernel_pid_t gcoap_init(void)
Initializes the gcoap thread and device.
msg_t timeout_msg
For response timer.
Definition: gcoap.h:476
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 PDU parsing context structure.
Definition: nanocoap.h:228
int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val)
Adds a single Uri-Query option to a CoAP request.