From 65a8b716e3aa9748d5909a726abdd40eab002b6f Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 7 Aug 2022 22:35:33 +0200 Subject: [PATCH] examples/gcoap_fileserver: make use of callbacks --- examples/gcoap_fileserver/Makefile | 1 + examples/gcoap_fileserver/main.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/examples/gcoap_fileserver/Makefile b/examples/gcoap_fileserver/Makefile index 96a6aac045..3b3e0caed4 100644 --- a/examples/gcoap_fileserver/Makefile +++ b/examples/gcoap_fileserver/Makefile @@ -21,6 +21,7 @@ USEMODULE += shell_commands # enable the fileserver module USEMODULE += gcoap_fileserver +USEMODULE += gcoap_fileserver_callback USEMODULE += gcoap_fileserver_delete USEMODULE += gcoap_fileserver_put diff --git a/examples/gcoap_fileserver/main.c b/examples/gcoap_fileserver/main.c index dd081e5e9f..e736315726 100644 --- a/examples/gcoap_fileserver/main.c +++ b/examples/gcoap_fileserver/main.c @@ -17,6 +17,7 @@ * @} */ +#include #include "kernel_defines.h" #include "net/gcoap.h" #include "net/gcoap/fileserver.h" @@ -45,11 +46,36 @@ static gcoap_listener_t _listener = { .resources_len = ARRAY_SIZE(_resources), }; +static void _event_cb(gcoap_fileserver_event_t event, gcoap_fileserver_event_ctx_t *ctx) +{ + switch (event) { + case GCOAP_FILESERVER_GET_FILE_START: + printf("gcoap fileserver: Download started: %s\n", ctx->path); + break; + case GCOAP_FILESERVER_GET_FILE_END: + printf("gcoap fileserver: Download finished: %s\n", ctx->path); + break; + case GCOAP_FILESERVER_PUT_FILE_START: + printf("gcoap fileserver: Upload started: %s\n", ctx->path); + break; + case GCOAP_FILESERVER_PUT_FILE_END: + printf("gcoap fileserver: Upload finished: %s\n", ctx->path); + break; + case GCOAP_FILESERVER_DELETE_FILE: + printf("gcoap fileserver: Delete %s\n", ctx->path); + break; + } +} + int main(void) { msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); gcoap_register_listener(&_listener); + if (IS_USED(MODULE_GCOAP_FILESERVER_CALLBACK)) { + gcoap_fileserver_set_event_cb(_event_cb, NULL); + } + char line_buf[SHELL_DEFAULT_BUFSIZE]; shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);