Skip to content

Commit 85d8397

Browse files
authored
Merge pull request #6117 from kb2ma/gcoap/sock
gcoap: rebase networking on sock
2 parents 9d55b3a + 342c3f2 commit 85d8397

File tree

14 files changed

+166
-201
lines changed

14 files changed

+166
-201
lines changed

Makefile.dep

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ ifneq (,$(filter gnrc_zep,$(USEMODULE)))
9696
USEMODULE += xtimer
9797
endif
9898

99-
ifneq (,$(filter gcoap,$(USEMODULE)))
100-
USEMODULE += gnrc_udp
101-
endif
102-
10399
ifneq (,$(filter gnrc_tftp,$(USEMODULE)))
104100
USEMODULE += gnrc_udp
105101
USEMODULE += xtimer

examples/gcoap/Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Default Makefile, for host native networking
1+
# Default Makefile, for host native GNRC-based networking
22

33
# name of your application
44
APPLICATION = gcoap
@@ -29,15 +29,14 @@ BOARD_BLACKLIST := nrf52dk
2929
USEPKG += nanocoap
3030
# Required by nanocoap, but only due to issue #5959.
3131
USEMODULE += posix
32-
# Required by nanocoap to compile nanocoap_sock.
33-
USEMODULE += gnrc_sock_udp
3432

3533
# Include packages that pull up and auto-init the link layer.
3634
# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present
3735
USEMODULE += gnrc_netdev_default
3836
USEMODULE += auto_init_gnrc_netif
3937
# Specify the mandatory networking modules
4038
USEMODULE += gnrc_ipv6_default
39+
USEMODULE += gnrc_sock_udp
4140
USEMODULE += gcoap
4241
# Additional networking modules that can be dropped if not needed
4342
USEMODULE += gnrc_icmpv6_echo

examples/gcoap/Makefile.slip

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Border router Makefile for SLIP based networking
1+
# Border router Makefile for GNRC and SLIP based networking
22
# Assumes use of SAMR21 board
33

44
# name of your application
@@ -45,14 +45,13 @@ CFLAGS += -DSLIP_BAUDRATE=$(SLIP_BAUDRATE)
4545
USEPKG += nanocoap
4646
# Required by nanocoap, but only due to issue #5959.
4747
USEMODULE += posix
48-
# Required by nanocoap to compile nanocoap_sock.
49-
USEMODULE += gnrc_sock_udp
5048

5149
# Include packages that pull up and auto-init the link layer.
5250
# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present
5351
USEMODULE += gnrc_netdev_default
5452
USEMODULE += auto_init_gnrc_netif
5553
# Specify the mandatory networking modules
54+
USEMODULE += gnrc_sock_udp
5655
USEMODULE += gcoap
5756
# Add a routing protocol
5857
USEMODULE += gnrc_rpl

examples/gcoap/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# gcoap Example
22

3-
This application provides command line access to gcoap, a GNRC implementation of CoAP. See the [CoAP spec][1] for background, and the Modules>Networking>GNRC>CoAP topic in the source documentation for the structure of the implementation.
3+
This application provides command line access to gcoap, a high-level API for CoAP messaging. See the [CoAP spec][1] for background, and the Modules>Networking>CoAP topic in the source documentation for detailed usage instructions and implementation notes.
44

55
We support two setup options for this example:
66

examples/gcoap/gcoap_cli.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <stdio.h>
2323
#include <stdlib.h>
2424
#include <string.h>
25-
#include "net/gnrc/coap.h"
25+
#include "net/gcoap.h"
2626
#include "od.h"
2727
#include "fmt.h"
2828

@@ -96,22 +96,27 @@ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len)
9696
static size_t _send(uint8_t *buf, size_t len, char *addr_str, char *port_str)
9797
{
9898
ipv6_addr_t addr;
99-
uint16_t port;
10099
size_t bytes_sent;
100+
sock_udp_ep_t remote;
101+
102+
remote.family = AF_INET6;
103+
remote.netif = SOCK_ADDR_ANY_NETIF;
101104

102105
/* parse destination address */
103106
if (ipv6_addr_from_str(&addr, addr_str) == NULL) {
104107
puts("gcoap_cli: unable to parse destination address");
105108
return 0;
106109
}
110+
memcpy(&remote.addr.ipv6[0], &addr.u8[0], sizeof(addr.u8));
111+
107112
/* parse port */
108-
port = (uint16_t)atoi(port_str);
109-
if (port == 0) {
113+
remote.port = (uint16_t)atoi(port_str);
114+
if (remote.port == 0) {
110115
puts("gcoap_cli: unable to parse destination port");
111116
return 0;
112117
}
113118

114-
bytes_sent = gcoap_req_send(buf, len, &addr, port, _resp_handler);
119+
bytes_sent = gcoap_req_send2(buf, len, &remote, _resp_handler);
115120
if (bytes_sent > 0) {
116121
req_count++;
117122
}

examples/gcoap/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <stdio.h>
2222
#include "msg.h"
2323

24-
#include "net/gnrc/coap.h"
24+
#include "net/gcoap.h"
2525
#include "kernel_types.h"
2626
#include "shell.h"
2727

sys/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ endif
9999
ifneq (,$(filter sema,$(USEMODULE)))
100100
DIRS += sema
101101
endif
102+
ifneq (,$(filter gcoap,$(USEMODULE)))
103+
DIRS += net/application_layer/coap
104+
endif
102105

103106
DIRS += $(dir $(wildcard $(addsuffix /Makefile, ${USEMODULE})))
104107

sys/auto_init/auto_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
#endif
9090

9191
#ifdef MODULE_GCOAP
92-
#include "net/gnrc/coap.h"
92+
#include "net/gcoap.h"
9393
#endif
9494

9595
#define ENABLE_DEBUG (0)

sys/include/net/gnrc/coap.h renamed to sys/include/net/gcoap.h

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77
*/
88

99
/**
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.
2027
*
2128
* ## Server Operation ##
2229
*
@@ -61,11 +68,10 @@
6168
*
6269
* ## Client Operation ##
6370
*
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.
6975
*
7076
* ### Creating a request ###
7177
*
@@ -85,8 +91,8 @@
8591
* as described above. The gcoap_request() function is inline, and uses those
8692
* two functions.
8793
*
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.
9096
*
9197
* ### Handling the response ###
9298
*
@@ -114,13 +120,13 @@
114120
* header and the payload. So, gcoap provides space in the buffer for them in
115121
* the request/response ...init() function, and then writes them during
116122
* gcoap_finish(). We trade some inefficiency/work in the buffer for
117-
* simplicity for the user.
123+
* simplicity in the API.
118124
*
119125
* ### Waiting for a response ###
120126
*
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
124130
* times out. We track the response with an entry in the
125131
* `_coap_state.open_reqs` array.
126132
*
@@ -135,9 +141,7 @@
135141
#ifndef GCOAP_H
136142
#define GCOAP_H
137143

138-
#include "net/gnrc.h"
139-
#include "net/gnrc/ipv6.h"
140-
#include "net/gnrc/udp.h"
144+
#include "net/sock/udp.h"
141145
#include "nanocoap.h"
142146
#include "xtimer.h"
143147

@@ -199,15 +203,27 @@ extern "C" {
199203
#define GCOAP_MEMO_ERR (4) /**< Error processing response packet */
200204
/** @} */
201205

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+
202209
/**
210+
*
203211
* @brief Default time to wait for a non-confirmable response, in usec
204212
*
205213
* Set to 0 to disable timeout.
206214
*/
207215
#define GCOAP_NON_TIMEOUT (5000000U)
208216

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)
211227

212228
/**
213229
* @brief A modular collection of resources for a server
@@ -243,7 +259,6 @@ typedef struct {
243259
* @brief Container for the state of gcoap itself
244260
*/
245261
typedef struct {
246-
gnrc_netreg_entry_t netreg_port; /**< Registration for IP port */
247262
gcoap_listener_t *listeners; /**< List of registered listeners */
248263
gcoap_request_memo_t open_reqs[GCOAP_REQ_WAITING_MAX];
249264
/**< 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,
322337
: -1;
323338
}
324339

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+
325354
/**
326355
* @brief Sends a buffer containing a CoAP request to the provided host/port.
327356
*
357+
* @deprecated Please use @ref gcoap_req_send2() instead
358+
*
328359
* @param[in] buf Buffer containing the PDU
329360
* @param[in] len Length of the buffer
330361
* @param[in] addr Destination for the packet

0 commit comments

Comments
 (0)