Skip to content

Commit

Permalink
gcoap/fileserver: make PUT and DELETE pseudomodules
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian18 committed Aug 3, 2022
1 parent f357d99 commit 9333970
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/gcoap_fileserver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ USEMODULE += shell_commands

# enable the fileserver module
USEMODULE += gcoap_fileserver
USEMODULE += gcoap_fileserver_delete
USEMODULE += gcoap_fileserver_put

# select network modules
USEMODULE += gnrc_ipv6_default
Expand Down
11 changes: 10 additions & 1 deletion examples/gcoap_fileserver/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @}
*/

#include "kernel_defines.h"
#include "net/gcoap.h"
#include "net/gcoap/fileserver.h"
#include "shell.h"
Expand All @@ -27,7 +28,15 @@ static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];

/* CoAP resources. Must be sorted by path (ASCII order). */
static const coap_resource_t _resources[] = {
{ "/vfs", COAP_GET | COAP_PUT | COAP_DELETE | COAP_MATCH_SUBTREE,
{ "/vfs",
COAP_GET |
#if IS_USED(MODULE_GCOAP_FILESERVER_PUT)
COAP_PUT |
#endif
#if IS_USED(MODULE_GCOAP_FILESERVER_DELETE)
COAP_DELETE |
#endif
COAP_MATCH_SUBTREE,
gcoap_fileserver_handler, VFS_DEFAULT_DATA },
};

Expand Down
2 changes: 2 additions & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ PSEUDOMODULES += fatfs_vfs_format
PSEUDOMODULES += fmt_%
PSEUDOMODULES += gcoap_forward_proxy
PSEUDOMODULES += gcoap_fileserver
PSEUDOMODULES += gcoap_fileserver_delete
PSEUDOMODULES += gcoap_fileserver_put
PSEUDOMODULES += gcoap_dtls
## Enable @ref net_gcoap_dns
PSEUDOMODULES += gcoap_dns
Expand Down
9 changes: 9 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,15 @@ ifneq (,$(filter gcoap_fileserver,$(USEMODULE)))
USEMODULE += vfs
endif

ifneq (,$(filter gcoap_fileserver_delete,$(USEMODULE)))
USEMODULE += gcoap_fileserver
USEMODULE += vfs_util
endif

ifneq (,$(filter gcoap_fileserver_put,$(USEMODULE)))
USEMODULE += gcoap_fileserver
endif

ifneq (,$(filter gcoap_forward_proxy,$(USEMODULE)))
USEMODULE += gcoap
USEMODULE += uri_parser
Expand Down
2 changes: 2 additions & 0 deletions sys/include/net/gcoap/fileserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
*
* The allowed methods dictate whether it's read-only (``COAP_GET``) or
* read-write (``COAP_GET | COAP_PUT | COAP_DELETE``).
* If you want to support ``PUT`` and `DELETE`, you need to enable the modules
* ``gcoap_fileserver_put`` and ``gcoap_fileserver_delete``.
*
* @{
*
Expand Down
17 changes: 17 additions & 0 deletions sys/net/application_layer/gcoap/fileserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <unistd.h>
#include <fcntl.h>

#include "kernel_defines.h"
#include "checksum/fletcher32.h"
#include "net/gcoap/fileserver.h"
#include "net/gcoap.h"
Expand Down Expand Up @@ -225,6 +226,7 @@ static ssize_t _get_file(coap_pkt_t *pdu, uint8_t *buf, size_t len,
return coap_get_total_hdr_len(pdu);
}

#if IS_USED(MODULE_GCOAP_FILESERVER_PUT)
static ssize_t _put_file(coap_pkt_t *pdu, uint8_t *buf, size_t len,
struct requestdata *request)
{
Expand Down Expand Up @@ -327,7 +329,9 @@ static ssize_t _put_file(coap_pkt_t *pdu, uint8_t *buf, size_t len,
}
return gcoap_fileserver_error_handler(pdu, buf, len, ret);
}
#endif

#if IS_USED(MODULE_GCOAP_FILESERVER_DELETE)
static ssize_t _delete_file(coap_pkt_t *pdu, uint8_t *buf, size_t len,
struct requestdata *request)
{
Expand All @@ -349,17 +353,22 @@ static ssize_t _delete_file(coap_pkt_t *pdu, uint8_t *buf, size_t len,
gcoap_resp_init(pdu, buf, len, COAP_CODE_DELETED);
return coap_opt_finish(pdu, COAP_OPT_FINISH_NONE);
}
#endif

static ssize_t gcoap_fileserver_file_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len,
struct requestdata *request)
{
switch (coap_get_code(pdu)) {
case COAP_METHOD_GET:
return _get_file(pdu, buf, len, request);
#if IS_USED(MODULE_GCOAP_FILESERVER_PUT)
case COAP_METHOD_PUT:
return _put_file(pdu, buf, len, request);
#endif
#if IS_USED(MODULE_GCOAP_FILESERVER_DELETE)
case COAP_METHOD_DELETE:
return _delete_file(pdu, buf, len, request);
#endif
default:
return gcoap_fileserver_error_handler(pdu, buf, len, COAP_CODE_METHOD_NOT_ALLOWED);
}
Expand Down Expand Up @@ -428,6 +437,7 @@ static ssize_t _get_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len,
return (uintptr_t)buf - (uintptr_t)pdu->hdr;
}

#if IS_USED(MODULE_GCOAP_FILESERVER_PUT)
static ssize_t _put_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len,
struct requestdata *request)
{
Expand All @@ -451,7 +461,9 @@ static ssize_t _put_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len,
}
return coap_opt_finish(pdu, COAP_OPT_FINISH_NONE);
}
#endif

#if IS_USED(MODULE_GCOAP_FILESERVER_DELETE)
static ssize_t _delete_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len,
struct requestdata *request)
{
Expand All @@ -474,6 +486,7 @@ static ssize_t _delete_directory(coap_pkt_t *pdu, uint8_t *buf, size_t len,
gcoap_resp_init(pdu, buf, len, COAP_CODE_DELETED);
return coap_opt_finish(pdu, COAP_OPT_FINISH_NONE);
}
#endif

static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len,
struct requestdata *request,
Expand All @@ -482,10 +495,14 @@ static ssize_t gcoap_fileserver_directory_handler(coap_pkt_t *pdu, uint8_t *buf,
switch (coap_get_code(pdu)) {
case COAP_METHOD_GET:
return _get_directory(pdu, buf, len, request, root, resource_dir);
#if IS_USED(MODULE_GCOAP_FILESERVER_PUT)
case COAP_METHOD_PUT:
return _put_directory(pdu, buf, len, request);
#endif
#if IS_USED(MODULE_GCOAP_FILESERVER_DELETE)
case COAP_METHOD_DELETE:
return _delete_directory(pdu, buf, len, request);
#endif
default:
return gcoap_fileserver_error_handler(pdu, buf, len, COAP_CODE_METHOD_NOT_ALLOWED);
}
Expand Down

0 comments on commit 9333970

Please sign in to comment.