Skip to content

Commit

Permalink
examples/nanocoap_server: add endpoint with separate response
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed May 21, 2024
1 parent dd458cb commit 63d5a5a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
7 changes: 7 additions & 0 deletions examples/nanocoap_server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ endif
HIGH_MEMORY_BOARDS := native same54-xpro mcb2388

ifneq (,$(filter $(BOARD),$(HIGH_MEMORY_BOARDS)))
# enable separate response
USEMODULE += nanocoap_server_separate
USEMODULE += event_callback
USEMODULE += event_thread
USEMODULE += event_timeout_ztimer

# enable fileserver
USEMODULE += nanocoap_fileserver
USEMODULE += vfs_default

Expand Down
15 changes: 8 additions & 7 deletions examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
#include <stdio.h>
#include <string.h>

#include "event/callback.h"
#include "event/timeout.h"
#include "event/thread.h"
#include "fmt.h"
#include "net/nanocoap.h"
#include "net/nanocoap_sock.h"
#include "hashes/sha256.h"
#include "kernel_defines.h"

Expand Down Expand Up @@ -185,38 +189,35 @@ NANOCOAP_RESOURCE(sha256) {
/* separate response requires an event thread to execute it */
#ifdef MODULE_EVENT_THREAD
static nanocoap_server_response_ctx_t _separate_ctx;
static bool _separate_in_progress;

static void _send_response(void *ctx)
{
const char response[] = "This is a delayed response.";

puts("_separate_handler(): send delayed response");
nanocoap_sock_send_separate(ctx, COAP_CODE_CONTENT, COAP_TYPE_NON,
nanocoap_server_send_separate(ctx, COAP_CODE_CONTENT, COAP_TYPE_NON,
response, sizeof(response));
_separate_in_progress = false;
}

static ssize_t _separate_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)

Check warning on line 202 in examples/nanocoap_server/coap_handler.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
{
static event_timeout_t event_timeout;
static event_callback_t event_timed = EVENT_CALLBACK_INIT(_send_response, &_separate_ctx);

if (_separate_in_progress) {
if (event_timeout_is_pending(&event_timeout)) {
puts("_separate_handler(): response already scheduled");
return coap_build_reply(pkt, COAP_CODE_SERVICE_UNAVAILABLE, buf, len, 0);
}

puts("_separate_handler(): send ACK, schedule response");

nanocoap_sock_prepare_separate(&_separate_ctx, pkt, context);
_separate_in_progress = true;
nanocoap_server_prepare_separate(&_separate_ctx, pkt, context);

event_timeout_ztimer_init(&event_timeout, ZTIMER_MSEC, EVENT_PRIO_MEDIUM,
&event_timed.super);
event_timeout_set(&event_timeout, 1 * MS_PER_SEC);

return coap_build_empty_ack(pkt, buf, len);
return coap_build_empty_ack(pkt, (void *)buf);
}

NANOCOAP_RESOURCE(separate) {
Expand Down

0 comments on commit 63d5a5a

Please sign in to comment.