gcoap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016 Ken Bannister. All rights reserved.
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
141 #ifndef GCOAP_H
142 #define GCOAP_H
143 
144 #include "net/sock/udp.h"
145 #include "nanocoap.h"
146 #include "xtimer.h"
147 
148 #ifdef __cplusplus
149 extern "C" {
150 #endif
151 
153 #define GCOAP_MSG_QUEUE_SIZE (4)
154 
156 #ifndef GCOAP_PORT
157 #define GCOAP_PORT (5683)
158 #endif
159 
161 #define GCOAP_PDU_BUF_SIZE (128)
162 
169 #define GCOAP_REQ_OPTIONS_BUF (8)
170 
176 #define GCOAP_RESP_OPTIONS_BUF (8)
177 
179 #define GCOAP_REQ_WAITING_MAX (2)
180 
182 #define GCOAP_TOKENLEN_MAX (8)
183 
185 #define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
186 
188 #ifndef GCOAP_TOKENLEN
189 #define GCOAP_TOKENLEN (2)
190 #endif
191 
193 #define GCOAP_PAYLOAD_MARKER (0xFF)
194 
199 #define GCOAP_MEMO_UNUSED (0)
200 #define GCOAP_MEMO_WAIT (1)
201 #define GCOAP_MEMO_RESP (2)
202 #define GCOAP_MEMO_TIMEOUT (3)
203 #define GCOAP_MEMO_ERR (4)
207 #define GCOAP_RECV_TIMEOUT (1 * US_PER_SEC)
208 
215 #define GCOAP_NON_TIMEOUT (5000000U)
216 
218 #define GCOAP_MSG_TYPE_TIMEOUT (0x1501)
219 
226 #define GCOAP_MSG_TYPE_INTR (0x1502)
227 
231 typedef struct gcoap_listener {
232  coap_resource_t *resources;
234  size_t resources_len;
237 
244 typedef void (*gcoap_resp_handler_t)(unsigned req_state, coap_pkt_t* pdu);
245 
249 typedef struct {
250  unsigned state;
251  uint8_t hdr_buf[GCOAP_HEADER_MAXLEN];
257 
261 typedef struct {
267  uint16_t last_message_id;
268 } gcoap_state_t;
269 
280 
287 
300 int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code,
301  char *path);
302 
317 ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format);
318 
331 static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len,
332  unsigned code,
333  char *path)
334 {
335  return (gcoap_req_init(pdu, buf, len, code, path) == 0)
336  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
337  : -1;
338 }
339 
351 size_t gcoap_req_send2(uint8_t *buf, size_t len, sock_udp_ep_t *remote,
352  gcoap_resp_handler_t resp_handler);
353 
368 size_t gcoap_req_send(uint8_t *buf, size_t len, ipv6_addr_t *addr, uint16_t port,
369  gcoap_resp_handler_t resp_handler);
370 
384 int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
385 
397 static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf, size_t len,
398  unsigned code)
399 {
400  return (gcoap_resp_init(pdu, buf, len, code) == 0)
401  ? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
402  : -1;
403 }
404 
412 void gcoap_op_state(uint8_t *open_reqs);
413 
414 #ifdef __cplusplus
415 }
416 #endif
417 
418 #endif /* GCOAP_H */
419 
struct gcoap_listener * next
Next listener in list.
Definition: gcoap.h:235
#define GCOAP_HEADER_MAXLEN
Maximum length in bytes for a header, including the token.
Definition: gcoap.h:185
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
xtimer_t response_timer
Limits wait for response.
Definition: gcoap.h:254
void gcoap_op_state(uint8_t *open_reqs)
Provides important operational statistics.
UDP sock definitions.
size_t resources_len
Length of array.
Definition: gcoap.h:234
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:331
Container for the state of gcoap itself.
Definition: gcoap.h:261
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.
size_t gcoap_req_send(uint8_t *buf, size_t len, 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.
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:250
coap_resource_t * resources
First element in the array of resources; must order alphabetically.
Definition: gcoap.h:232
A modular collection of resources for a server.
Definition: gcoap.h:231
int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
Initializes a CoAP response packet on a buffer.
size_t gcoap_req_send2(uint8_t *buf, size_t len, sock_udp_ep_t *remote, gcoap_resp_handler_t resp_handler)
Sends a buffer containing a CoAP request to the provided endpoint.
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:179
uint16_t last_message_id
Last message ID used.
Definition: gcoap.h:267
struct gcoap_listener gcoap_listener_t
A modular collection of resources for a server.
Memo to handle a response for a request.
Definition: gcoap.h:249
void(* gcoap_resp_handler_t)(unsigned req_state, coap_pkt_t *pdu)
Handler function for a server response, including the state for the originating request.
Definition: gcoap.h:244
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:397
gcoap_listener_t * listeners
List of registered listeners.
Definition: gcoap.h:262
gcoap_resp_handler_t resp_handler
Callback for the response.
Definition: gcoap.h:253
Data type to represent an IPv6 address.
Definition: ipv6/addr.h:74
xtimer timer structure
Definition: xtimer.h:70
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:255