|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | /** |
10 | | - * @defgroup net_gnrc_coap CoAP |
11 | | - * @ingroup net_gnrc |
12 | | - * @brief GNRC implementation of CoAP protocol, RFC 7252 |
13 | | - * |
14 | | - * ## Architecture ## |
15 | | - * Requests and responses are exchanged via an asynchronous RIOT message |
16 | | - * processing thread. Depends on nanocoap for base level structs and |
17 | | - * functionality. |
18 | | - * |
19 | | - * Uses a single UDP port for communication to support RFC 6282 compression. |
| 10 | + * @defgroup net_gcoap CoAP |
| 11 | + * @ingroup net |
| 12 | + * @brief High-level interface to CoAP messaging |
| 13 | + * |
| 14 | + * gcoap provides a high-level interface for writing CoAP messages via RIOT's |
| 15 | + * sock networking API. gcoap internalizes network event processing so an |
| 16 | + * application only needs to focus on request/response handling. For a server, |
| 17 | + * gcoap accepts a list of resource paths with callbacks for writing the |
| 18 | + * response. For a client, gcoap provides a function to send a request, with a |
| 19 | + * callback for reading the server response. Generation of the request or |
| 20 | + * response requires from one to three well-defined steps, depending on |
| 21 | + * inclusion of a payload. |
| 22 | + * |
| 23 | + * gcoap allocates a RIOT message processing thread, so a single instance can |
| 24 | + * serve multiple applications. This approach also means gcoap uses a single UDP |
| 25 | + * port, which supports RFC 6282 compression. Internally, gcoap depends on the |
| 26 | + * nanocoap package for base level structs and functionality. |
20 | 27 | * |
21 | 28 | * ## Server Operation ## |
22 | 29 | * |
|
61 | 68 | * |
62 | 69 | * ## Client Operation ## |
63 | 70 | * |
64 | | - * gcoap uses RIOT's asynchronous messaging facility to send and receive |
65 | | - * messages. So, client operation includes two phases: creating and sending a |
66 | | - * request, and handling the response aynchronously in a client supplied |
67 | | - * callback. See `examples/gcoap/gcoap_cli.c` for a simple example of sending |
68 | | - * a request and reading the response. |
| 71 | + * Client operation includes two phases: creating and sending a request, and |
| 72 | + * handling the response aynchronously in a client supplied callback. See |
| 73 | + * `examples/gcoap/gcoap_cli.c` for a simple example of sending a request and |
| 74 | + * reading the response. |
69 | 75 | * |
70 | 76 | * ### Creating a request ### |
71 | 77 | * |
|
85 | 91 | * as described above. The gcoap_request() function is inline, and uses those |
86 | 92 | * two functions. |
87 | 93 | * |
88 | | - * Finally, call gcoap_req_send() with the destination host and port, as well |
89 | | - * as a callback function for the host's response. |
| 94 | + * Finally, call gcoap_req_send2() for the destination endpoint, as well as a |
| 95 | + * callback function for the host's response. |
90 | 96 | * |
91 | 97 | * ### Handling the response ### |
92 | 98 | * |
|
114 | 120 | * header and the payload. So, gcoap provides space in the buffer for them in |
115 | 121 | * the request/response ...init() function, and then writes them during |
116 | 122 | * gcoap_finish(). We trade some inefficiency/work in the buffer for |
117 | | - * simplicity for the user. |
| 123 | + * simplicity in the API. |
118 | 124 | * |
119 | 125 | * ### Waiting for a response ### |
120 | 126 | * |
121 | | - * We take advantage of RIOT's GNRC stack by using an xtimer to wait for a |
122 | | - * response, so the gcoap thread does not block while waiting. The user is |
123 | | - * notified via the same callback whether the message is received or the wait |
| 127 | + * We take advantage of RIOT's asynchronous messaging by using an xtimer to wait |
| 128 | + * for a response, so the gcoap thread does not block while waiting. The user is |
| 129 | + * notified via the same callback, whether the message is received or the wait |
124 | 130 | * times out. We track the response with an entry in the |
125 | 131 | * `_coap_state.open_reqs` array. |
126 | 132 | * |
|
135 | 141 | #ifndef GCOAP_H |
136 | 142 | #define GCOAP_H |
137 | 143 |
|
138 | | -#include "net/gnrc.h" |
139 | | -#include "net/gnrc/ipv6.h" |
140 | | -#include "net/gnrc/udp.h" |
| 144 | +#include "net/sock/udp.h" |
141 | 145 | #include "nanocoap.h" |
142 | 146 | #include "xtimer.h" |
143 | 147 |
|
@@ -199,15 +203,27 @@ extern "C" { |
199 | 203 | #define GCOAP_MEMO_ERR (4) /**< Error processing response packet */ |
200 | 204 | /** @} */ |
201 | 205 |
|
| 206 | +/** @brief Time in usec that the event loop waits for an incoming CoAP message */ |
| 207 | +#define GCOAP_RECV_TIMEOUT (1 * US_PER_SEC) |
| 208 | + |
202 | 209 | /** |
| 210 | + * |
203 | 211 | * @brief Default time to wait for a non-confirmable response, in usec |
204 | 212 | * |
205 | 213 | * Set to 0 to disable timeout. |
206 | 214 | */ |
207 | 215 | #define GCOAP_NON_TIMEOUT (5000000U) |
208 | 216 |
|
209 | | -/** @brief Identifies a gcoap-specific timeout IPC message */ |
210 | | -#define GCOAP_NETAPI_MSG_TYPE_TIMEOUT (0x1501) |
| 217 | +/** @brief Identifies waiting timed out for a response to a sent message. */ |
| 218 | +#define GCOAP_MSG_TYPE_TIMEOUT (0x1501) |
| 219 | + |
| 220 | +/** |
| 221 | + * @brief Identifies a request to interrupt listening for an incoming message |
| 222 | + * on a sock. |
| 223 | + * |
| 224 | + * Allows the event loop to process IPC messages. |
| 225 | + */ |
| 226 | +#define GCOAP_MSG_TYPE_INTR (0x1502) |
211 | 227 |
|
212 | 228 | /** |
213 | 229 | * @brief A modular collection of resources for a server |
@@ -243,7 +259,6 @@ typedef struct { |
243 | 259 | * @brief Container for the state of gcoap itself |
244 | 260 | */ |
245 | 261 | typedef struct { |
246 | | - gnrc_netreg_entry_t netreg_port; /**< Registration for IP port */ |
247 | 262 | gcoap_listener_t *listeners; /**< List of registered listeners */ |
248 | 263 | gcoap_request_memo_t open_reqs[GCOAP_REQ_WAITING_MAX]; |
249 | 264 | /**< Storage for open requests; if first |
@@ -322,9 +337,25 @@ static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len, |
322 | 337 | : -1; |
323 | 338 | } |
324 | 339 |
|
| 340 | +/** |
| 341 | + * @brief Sends a buffer containing a CoAP request to the provided endpoint. |
| 342 | + * |
| 343 | + * @param[in] buf Buffer containing the PDU |
| 344 | + * @param[in] len Length of the buffer |
| 345 | + * @param[in] remote Destination for the packet |
| 346 | + * @param[in] resp_handler Callback when response received |
| 347 | + * |
| 348 | + * @return length of the packet |
| 349 | + * @return 0 if cannot send |
| 350 | + */ |
| 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 | + |
325 | 354 | /** |
326 | 355 | * @brief Sends a buffer containing a CoAP request to the provided host/port. |
327 | 356 | * |
| 357 | + * @deprecated Please use @ref gcoap_req_send2() instead |
| 358 | + * |
328 | 359 | * @param[in] buf Buffer containing the PDU |
329 | 360 | * @param[in] len Length of the buffer |
330 | 361 | * @param[in] addr Destination for the packet |
|
0 commit comments