1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

gcoap/fileserver: make PUT and DELETE pseudomodules

This commit is contained in:
Fabian Hüßler 2022-08-01 13:51:52 +02:00
parent f357d99396
commit 9333970b77
6 changed files with 42 additions and 1 deletions

View File

@ -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

View File

@ -17,6 +17,7 @@
* @}
*/
#include "kernel_defines.h"
#include "net/gcoap.h"
#include "net/gcoap/fileserver.h"
#include "shell.h"
@ -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 },
};

View File

@ -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

View File

@ -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

View File

@ -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``.
*
* @{
*

View File

@ -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"
@ -225,6 +226,7 @@ late_err:
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)
{
@ -327,7 +329,9 @@ unlink_on_error:
}
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)
{
@ -349,6 +353,7 @@ 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)
@ -356,10 +361,14 @@ static ssize_t gcoap_fileserver_file_handler(coap_pkt_t *pdu, uint8_t *buf, size
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);
}
@ -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)
{
@ -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)
{
@ -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,
@ -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);
}