Skip to content

Commit

Permalink
sys/shell: add 'ncput' shell command
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jun 6, 2022
1 parent 86c9a2c commit 639dce8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 39 additions & 1 deletion sys/shell/commands/sc_nanocoap_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static int _print_dir(const char *url, char *buf, size_t len)
int _nanocoap_get_handler(int argc, char **argv)
{
int res;
char buffer[CONFIG_NANOCOAP_QS_MAX];
char buffer[CONFIG_NANOCOAP_URI_MAX];
char *dst, *url = argv[1];

if (argc < 2) {
Expand Down Expand Up @@ -137,3 +137,41 @@ int _nanocoap_get_handler(int argc, char **argv)
}
return res;
}

int _nanocoap_put_handler(int argc, char **argv)
{
int res;
char *file, *url;
char buffer[CONFIG_NANOCOAP_URI_MAX];
char work_buf[coap_szx2size(CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT) + 1];

if (argc < 3) {
printf("Usage: %s <file> <url>\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;
}
if (snprintf(buffer, sizeof(buffer), "%s%s",
url, basename + 1) >= (int)sizeof(buffer)) {
puts("Constructed URI too long");
return -ENOBUFS;
}
url = buffer;
}

res = nanocoap_vfs_put_url(url, file, work_buf, sizeof(work_buf));
if (res < 0) {
printf("Upload failed: %s\n", strerror(-res));
}
else {
printf("Saved to %s\n", url);
}
return res;
}
2 changes: 2 additions & 0 deletions sys/shell/commands/shell_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -295,6 +296,7 @@ const shell_command_t _shell_command_list[] = {
#endif
#ifdef MODULE_NANOCOAP_VFS
{"ncget", "download a file from a CoAP server", _nanocoap_get_handler},
{"ncput", "upload a file to a CoAP server", _nanocoap_put_handler},
#endif
#ifdef MODULE_GNRC_IPV6_NIB
{"nib", "Configure neighbor information base", _gnrc_ipv6_nib},
Expand Down

0 comments on commit 639dce8

Please sign in to comment.