From 4129bbd31724350fef6e1b4c759754fe289c3496 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Mon, 18 Apr 2022 03:10:09 +0200 Subject: [PATCH] sys/shell: add 'nput' shell command --- sys/shell/commands/sc_nanocoap_vfs.c | 32 ++++++++++++++++++++++++++++ sys/shell/commands/shell_commands.c | 2 ++ 2 files changed, 34 insertions(+) diff --git a/sys/shell/commands/sc_nanocoap_vfs.c b/sys/shell/commands/sc_nanocoap_vfs.c index e78688a7e5979..fa2797254cd67 100644 --- a/sys/shell/commands/sc_nanocoap_vfs.c +++ b/sys/shell/commands/sc_nanocoap_vfs.c @@ -96,3 +96,35 @@ int _nanocoap_get_handler(int argc, char **argv) } return res; } + +int _nanocoap_put_handler(int argc, char **argv) +{ + int res; + char buffer[64]; + char *file, *url; + + if (argc < 3) { + printf("Usage: %s \n", argv[0]); + return -EINVAL; + } + + file = argv[1]; + url = argv[2]; + + if (_is_dir(url)) { + const char *basename = strrchr(file, '/'); + if (basename == NULL) { + return -EINVAL; + } + snprintf(buffer, sizeof(buffer), "%s%s", url, basename + 1); + url = buffer; + } + + res = nanocoap_vfs_put(url, file); + if (res < 0) { + printf("Upload failed: %s\n", strerror(-res)); + } else { + printf("Saved to %s\n", url); + } + return res; +} diff --git a/sys/shell/commands/shell_commands.c b/sys/shell/commands/shell_commands.c index fbad2b153abf2..ba76c33f5c863 100644 --- a/sys/shell/commands/shell_commands.c +++ b/sys/shell/commands/shell_commands.c @@ -189,6 +189,7 @@ extern int _loramac_handler(int argc, char **argv); #ifdef MODULE_NANOCOAP_VFS extern int _nanocoap_get_handler(int argc, char **argv); +extern int _nanocoap_put_handler(int argc, char **argv); #endif #ifdef MODULE_NICE @@ -274,6 +275,7 @@ const shell_command_t _shell_command_list[] = { #endif #ifdef MODULE_NANOCOAP_VFS {"nget", "download a file from a CoAP server", _nanocoap_get_handler}, + {"nput", "upload a file to a CoAP server", _nanocoap_put_handler}, #endif #ifdef MODULE_GNRC_IPV6_NIB {"nib", "Configure neighbor information base", _gnrc_ipv6_nib},