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 
219 #ifndef NET_GCOAP_H
220 #define NET_GCOAP_H
221 
222 #include <stdint.h>
223 
224 #include "net/ipv6/addr.h"
225 #include "net/sock/udp.h"
226 #include "net/nanocoap.h"
227 #include "xtimer.h"
228 
229 #ifdef __cplusplus
230 extern "C" {
231 #endif
232 
236 #ifndef GCOAP_MSG_QUEUE_SIZE
237 #define GCOAP_MSG_QUEUE_SIZE (4)
238 #endif
239 
243 #ifndef GCOAP_PORT
244 #define GCOAP_PORT (5683)
245 #endif
246 
250 #ifndef GCOAP_PDU_BUF_SIZE
251 #define GCOAP_PDU_BUF_SIZE (128)
252 #endif
253 
260 #define GCOAP_REQ_OPTIONS_BUF (40)
261 
267 #define GCOAP_RESP_OPTIONS_BUF (8)
268 
274 #define GCOAP_OBS_OPTIONS_BUF (8)
275 
279 #ifndef GCOAP_REQ_WAITING_MAX
280 #define GCOAP_REQ_WAITING_MAX (2)
281 #endif
282 
286 #define GCOAP_TOKENLEN_MAX (8)
287 
291 #define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
292 
296 #ifndef GCOAP_TOKENLEN
297 #define GCOAP_TOKENLEN (2)
298 #endif
299 
303 #define GCOAP_PAYLOAD_MARKER (0xFF)
304 
309 #define GCOAP_MEMO_UNUSED (0)
310 #define GCOAP_MEMO_WAIT (1)
311 #define GCOAP_MEMO_RESP (2)
312 #define GCOAP_MEMO_TIMEOUT (3)
313 #define GCOAP_MEMO_ERR (4)
319 #define GCOAP_SEND_LIMIT_NON (-1)
320 
324 #ifndef GCOAP_RECV_TIMEOUT
325 #define GCOAP_RECV_TIMEOUT (1 * US_PER_SEC)
326 #endif
327 
333 #ifndef GCOAP_NON_TIMEOUT
334 #define GCOAP_NON_TIMEOUT (5000000U)
335 #endif
336 
340 #define GCOAP_MSG_TYPE_TIMEOUT (0x1501)
341 
348 #define GCOAP_MSG_TYPE_INTR (0x1502)
349 
353 #ifndef GCOAP_OBS_CLIENTS_MAX
354 #define GCOAP_OBS_CLIENTS_MAX (2)
355 #endif
356 
361 #ifndef GCOAP_OBS_REGISTRATIONS_MAX
362 #define GCOAP_OBS_REGISTRATIONS_MAX (2)
363 #endif
364 
369 #define GCOAP_OBS_MEMO_UNUSED (0)
370 #define GCOAP_OBS_MEMO_IDLE (1)
371 #define GCOAP_OBS_MEMO_PENDING (2)
393 #ifndef GCOAP_OBS_VALUE_WIDTH
394 #define GCOAP_OBS_VALUE_WIDTH (3)
395 #endif
396 
400 #if (GCOAP_OBS_VALUE_WIDTH == 3)
401 #define GCOAP_OBS_TICK_EXPONENT (5)
402 #elif (GCOAP_OBS_VALUE_WIDTH == 2)
403 #define GCOAP_OBS_TICK_EXPONENT (16)
404 #elif (GCOAP_OBS_VALUE_WIDTH == 1)
405 #define GCOAP_OBS_TICK_EXPONENT (24)
406 #endif
407 
412 #define GCOAP_OBS_INIT_OK (0)
413 #define GCOAP_OBS_INIT_ERR (-1)
414 #define GCOAP_OBS_INIT_UNUSED (-2)
415 
420 #ifndef GCOAP_STACK_SIZE
421 #define GCOAP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT + DEBUG_EXTRA_STACKSIZE \
422  + sizeof(coap_pkt_t))
423 #endif
424 
428 #ifndef GCOAP_RESEND_BUFS_MAX
429 #define GCOAP_RESEND_BUFS_MAX (1)
430 #endif
431 
435 typedef struct gcoap_listener {
438  size_t resources_len;
441 
448 typedef void (*gcoap_resp_handler_t)(unsigned req_state, coap_pkt_t* pdu,
449  sock_udp_ep_t *remote);
450 
454 typedef struct {
455  uint8_t *pdu_buf;
456  size_t pdu_len;
458 
462 typedef struct {
463  unsigned state;
466  union {
467  uint8_t hdr_buf[GCOAP_HEADER_MAXLEN];
470  } msg;
477 
481 typedef struct {
484  uint8_t token[GCOAP_TOKENLEN_MAX];
485  unsigned token_len;
487 
498 
505 
521 int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
522  unsigned code, const char *path);
523 
538 ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format);
539 
552 static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len,
553  unsigned code, char *path)
554 {
555  return (gcoap_req_init(pdu, buf, len, code, path) == 0)
556  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
557  : -1;
558 }
559 
571 size_t gcoap_req_send2(const uint8_t *buf, size_t len,
572  const sock_udp_ep_t *remote,
573  gcoap_resp_handler_t resp_handler);
574 
589 size_t gcoap_req_send(const uint8_t *buf, size_t len, const ipv6_addr_t *addr,
590  uint16_t port, gcoap_resp_handler_t resp_handler);
591 
605 int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
606 
618 static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf,
619  size_t len, unsigned code)
620 {
621  return (gcoap_resp_init(pdu, buf, len, code) == 0)
622  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
623  : -1;
624 }
625 
641 int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
642  const coap_resource_t *resource);
643 
657 size_t gcoap_obs_send(const uint8_t *buf, size_t len,
658  const coap_resource_t *resource);
659 
667 uint8_t gcoap_op_state(void);
668 
687 int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf);
688 
704 int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val);
705 
706 #ifdef __cplusplus
707 }
708 #endif
709 
710 #endif /* NET_GCOAP_H */
711 
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:439
Definitions for IPv6 addresses.
#define GCOAP_HEADER_MAXLEN
Maximum length in bytes for a header, including the token.
Definition: gcoap.h:291
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
Memo for Observe registration and notifications.
Definition: gcoap.h:481
xtimer_t response_timer
Limits wait for response.
Definition: gcoap.h:474
UDP sock definitions.
uint8_t gcoap_op_state(void)
Provides important operational statistics.
size_t resources_len
Length of array.
Definition: gcoap.h:438
#define GCOAP_TOKENLEN_MAX
Maximum length in bytes for a token.
Definition: gcoap.h:286
size_t pdu_len
Length of pdu_buf.
Definition: gcoap.h:456
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:552
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:284
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:463
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:448
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:435
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:455
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:436
Extends request memo for resending a confirmable request.
Definition: gcoap.h:454
#define COAP_FORMAT_NONE
nanocoap-specific value to indicate no format specified.
Definition: nanocoap.h:187
const coap_resource_t * resource
Entity being observed.
Definition: gcoap.h:483
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:485
Memo to handle a response for a request.
Definition: gcoap.h:462
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:618
sock_udp_ep_t remote_ep
Remote endpoint.
Definition: gcoap.h:472
sock_udp_ep_t * observer
Client endpoint; unused if null.
Definition: gcoap.h:482
gcoap_resp_handler_t resp_handler
Callback for the response.
Definition: gcoap.h:473
gcoap_resend_t data
Endpoint and PDU buffer, for resend.
Definition: gcoap.h:469
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:464
kernel_pid_t gcoap_init(void)
Initializes the gcoap thread and device.
msg_t timeout_msg
For response timer.
Definition: gcoap.h:475
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:261
int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val)
Adds a single Uri-Query option to a CoAP request.