coap.h
13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Copyright (c) 2015-2016 Ken Bannister. All rights reserved.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup net_gnrc_coap CoAP
* @ingroup net_gnrc
* @brief GNRC implementation of CoAP protocol, RFC 7252
*
* ## Architecture ##
* Requests and responses are exchanged via an asynchronous RIOT message
* processing thread. Depends on nanocoap for base level structs and
* functionality.
*
* Uses a single UDP port for communication to support RFC 6282 compression.
*
* ## Server Operation ##
*
* gcoap listens for requests on GCOAP_PORT, 5683 by default. You can redefine
* this by uncommenting the appropriate lines in gcoap's make file.
*
* gcoap allows an application to specify a collection of request resource paths
* it wants to be notified about. Create an array of resources, coap_resource_t
* structs. Use gcoap_register_listener() at application startup to pass in
* these resources, wrapped in a gcoap_listener_t.
*
* gcoap itself defines a resource for `/.well-known/core` discovery, which
* lists all of the registered paths.
*
* ### Creating a response ###
*
* An application resource includes a callback function, a coap_handler_t. After
* reading the request, the callback must use one or two functions provided by
* gcoap to format the response, as described below. The callback *must* read
* the request thoroughly before calling the functions, because the response
* buffer likely reuses the request buffer. See `examples/gcoap/gcoap_cli.c`
* for a simple example of a callback.
*
* Here is the expected sequence for a callback function:
*
* Read request completely and parse request payload, if any. Use the
* coap_pkt_t _payload_ and _payload_len_ attributes.
*
* If there is a payload, follow the three steps below.
*
* -# Call gcoap_resp_init() to initialize the response.
* -# Write the request payload, starting at the updated _payload_ pointer
* in the coap_pkt_t. If some error occurs, return a negative errno
* code from the handler, and gcoap will send a server error (5.00).
* -# Call gcoap_finish() to complete the PDU after writing the payload,
* and return the result. gcoap will send the message.
*
* If no payload, call only gcoap_response() to write the full response.
* Alternatively, you still can use gcoap_resp_init() and gcoap_finish(), as
* described above. In fact, the gcoap_response() function is inline, and uses
* those two functions.
*
* ## Client Operation ##
*
* gcoap uses RIOT's asynchronous messaging facility to send and receive
* messages. So, client operation includes two phases: creating and sending a
* request, and handling the response aynchronously in a client supplied
* callback. See `examples/gcoap/gcoap_cli.c` for a simple example of sending
* a request and reading the response.
*
* ### Creating a request ###
*
* Here is the expected sequence for preparing and sending a request:
*
* Allocate a buffer and a coap_pkt_t for the request.
*
* If there is a payload, follow the three steps below.
*
* -# Call gcoap_req_init() to initialize the request.
* -# Write the request payload, starting at the updated _payload_ pointer
* in the coap_pkt_t.
* -# Call gcoap_finish(), which updates the packet for the payload.
*
* If no payload, call only gcoap_request() to write the full request.
* Alternatively, you still can use gcoap_req_init() and gcoap_finish(),
* as described above. The gcoap_request() function is inline, and uses those
* two functions.
*
* Finally, call gcoap_req_send() with the destination host and port, as well
* as a callback function for the host's response.
*
* ### Handling the response ###
*
* When gcoap receives the response to a request, it executes the callback from
* the request. gcoap also executes the callback when a response is not
* received within GCOAP_RESPONSE_TIMEOUT.
*
* Here is the expected sequence for handling a response in the callback.
*
* -# Test for a server response or timeout in the _req_state_ callback
* parameter. See the GCOAP_MEMO... constants.
* -# Test the response with coap_get_code_class() and coap_get_code_detail().
* -# Test the response payload with the coap_pkt_t _payload_len_ and
* _content_type_ attributes.
* -# Read the payload, if any.
*
* ## Implementation Notes ##
*
* ### Building a packet ###
*
* The sequence and functions described above to build a request or response
* is designed to provide a relatively simple API for the user.
*
* The structure of a CoAP PDU requires that options are placed between the
* header and the payload. So, gcoap provides space in the buffer for them in
* the request/response ...init() function, and then writes them during
* gcoap_finish(). We trade some inefficiency/work in the buffer for
* simplicity for the user.
*
* ### Waiting for a response ###
*
* We take advantage of RIOT's GNRC stack by using an xtimer to wait for a
* response, so the gcoap thread does not block while waiting. The user is
* notified via the same callback whether the message is received or the wait
* times out. We track the response with an entry in the
* `_coap_state.open_reqs` array.
*
* @{
*
* @file
* @brief gcoap definition
*
* @author Ken Bannister <kb2ma@runbox.com>
*/
#ifndef GCOAP_H_
#define GCOAP_H_
#include "net/gnrc.h"
#include "net/gnrc/ipv6.h"
#include "net/gnrc/udp.h"
#include "nanocoap.h"
#include "xtimer.h"
#ifdef __cplusplus
extern "C" {
#endif
/** @brief Size for module message queue */
#define GCOAP_MSG_QUEUE_SIZE (4)
/** @brief Server port; use RFC 7252 default if not defined */
#ifndef GCOAP_PORT
#define GCOAP_PORT (5683)
#endif
/** @brief Size of the buffer used to build a CoAP request or response. */
#define GCOAP_PDU_BUF_SIZE (128)
/**
* @brief Size of the buffer used to write options, other than Uri-Path, in a
* request.
*
* Accommodates Content-Format.
*/
#define GCOAP_REQ_OPTIONS_BUF (8)
/**
* @brief Size of the buffer used to write options in a response.
*
* Accommodates Content-Format.
*/
#define GCOAP_RESP_OPTIONS_BUF (8)
/** @brief Maximum number of requests awaiting a response */
#define GCOAP_REQ_WAITING_MAX (2)
/** @brief Maximum length in bytes for a token */
#define GCOAP_TOKENLEN_MAX (8)
/** @brief Maximum length in bytes for a header, including the token */
#define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
/** @brief Length in bytes for a token; use 2 if not defined */
#ifndef GCOAP_TOKENLEN
#define GCOAP_TOKENLEN (2)
#endif
/** @brief Marks the boundary between header and payload */
#define GCOAP_PAYLOAD_MARKER (0xFF)
/**
* @name States for the memo used to track waiting for a response
* @{
*/
#define GCOAP_MEMO_UNUSED (0) /**< This memo is unused */
#define GCOAP_MEMO_WAIT (1) /**< Request sent; awaiting response */
#define GCOAP_MEMO_RESP (2) /**< Got response */
#define GCOAP_MEMO_TIMEOUT (3) /**< Timeout waiting for response */
#define GCOAP_MEMO_ERR (4) /**< Error processing response packet */
/** @} */
/**
* @brief Default time to wait for a non-confirmable response, in usec
*
* Set to 0 to disable timeout.
*/
#define GCOAP_NON_TIMEOUT (5000000U)
/** @brief Identifies a gcoap-specific timeout IPC message */
#define GCOAP_NETAPI_MSG_TYPE_TIMEOUT (0x1501)
/**
* @brief A modular collection of resources for a server
*/
typedef struct gcoap_listener {
coap_resource_t *resources; /**< First element in the array of resources;
must order alphabetically */
size_t resources_len; /**< Length of array */
struct gcoap_listener *next; /**< Next listener in list */
} gcoap_listener_t;
/**
* @brief Handler function for a server response, including the state for the
* originating request.
*
* If request timed out, the packet header is for the request.
*/
typedef void (*gcoap_resp_handler_t)(unsigned req_state, coap_pkt_t* pdu);
/**
* @brief Memo to handle a response for a request
*/
typedef struct {
unsigned state; /**< State of this memo, a GCOAP_MEMO... */
uint8_t hdr_buf[GCOAP_HEADER_MAXLEN];
/**< Stores a copy of the request header */
gcoap_resp_handler_t resp_handler; /**< Callback for the response */
xtimer_t response_timer; /**< Limits wait for response */
msg_t timeout_msg; /**< For response timer */
} gcoap_request_memo_t;
/**
* @brief Container for the state of gcoap itself
*/
typedef struct {
gnrc_netreg_entry_t netreg_port; /**< Registration for IP port */
gcoap_listener_t *listeners; /**< List of registered listeners */
gcoap_request_memo_t open_reqs[GCOAP_REQ_WAITING_MAX];
/**< Storage for open requests; if first
byte of an entry is zero, the entry
is available */
uint16_t last_message_id; /**< Last message ID used */
} gcoap_state_t;
/**
* @brief Initializes the gcoap thread and device.
*
* Must call once before first use.
*
* @return PID of the gcoap thread on success.
* @return -EEXIST, if thread already has been created.
* @return -EINVAL, if the IP port already is in use.
*/
kernel_pid_t gcoap_init(void);
/**
* @brief Starts listening for resource paths.
*
* @param listener Listener containing the resources.
*/
void gcoap_register_listener(gcoap_listener_t *listener);
/**
* @brief Initializes a CoAP request PDU on a buffer.
*
* @param[in] pdu Request metadata
* @param[in] buf Buffer containing the PDU
* @param[in] len Length of the buffer
* @param[in] code Request code
* @param[in] path Resource path
*
* @return 0 on success
* @return < 0 on error
*/
int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code,
char *path);
/**
* @brief Finishes formatting a CoAP PDU after the payload has been written.
*
* Assumes the PDU has been initialized with gcoap_req_init() or
* gcoap_resp_init().
*
* @param[in] pdu Request metadata
* @param[in] payload_len Length of the payload, or 0 if none
* @param[in] format Format code for the payload; use COAP_FORMAT_NONE if not
* specified
*
* @return size of the PDU
* @return < 0 on error
*/
ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format);
/**
* @brief Writes a complete CoAP request PDU when there is not a payload.
*
* @param[in] pdu Request metadata
* @param[in] buf Buffer containing the PDU
* @param[in] len Length of the buffer
* @param[in] code Request code
* @param[in] path Resource path
*
* @return size of the PDU within the buffer
* @return < 0 on error
*/
static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len,
unsigned code,
char *path)
{
return (gcoap_req_init(pdu, buf, len, code, path) == 0)
? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
: -1;
}
/**
* @brief Sends a buffer containing a CoAP request to the provided host/port.
*
* @param[in] buf Buffer containing the PDU
* @param[in] len Length of the buffer
* @param[in] addr Destination for the packet
* @param[in] port Port at the destination
* @param[in] resp_handler Callback when response received
*
* @return length of the packet
* @return 0 if cannot send
*/
size_t gcoap_req_send(uint8_t *buf, size_t len, ipv6_addr_t *addr, uint16_t port,
gcoap_resp_handler_t resp_handler);
/**
* @brief Initializes a CoAP response packet on a buffer.
*
* Initializes payload location within the buffer based on packet setup.
*
* @param[in] pdu Response metadata
* @param[in] buf Buffer containing the PDU
* @param[in] len Length of the buffer
* @param[in] code Response code
*
* @return 0 on success
* @return < 0 on error
*/
int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
/**
* @brief Writes a complete CoAP response PDU when there is no payload.
*
* @param[in] pdu Response metadata
* @param[in] buf Buffer containing the PDU
* @param[in] len Length of the buffer
* @param[in] code Response code
*
* @return size of the PDU within the buffer
* @return < 0 on error
*/
static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf, size_t len,
unsigned code)
{
return (gcoap_resp_init(pdu, buf, len, code) == 0)
? gcoap_finish(pdu, 0, COAP_FORMAT_NONE)
: -1;
}
/**
* @brief Provides important operational statistics.
*
* Useful for monitoring.
*
* @param[out] open_reqs Count of unanswered requests
*/
void gcoap_op_state(uint8_t *open_reqs);
#ifdef __cplusplus
}
#endif
#endif /* GCOAP_H_ */
/** @} */