Skip to content

Commit

Permalink
sys/shell: ncget: make use of nanocoap_rd_get_url()
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed May 24, 2022
1 parent f6047aa commit dc852c2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
4 changes: 4 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ ifneq (,$(filter shell_commands,$(USEMODULE)))
USEMODULE += netutils
endif

ifneq (,$(filter nanocoap_vfs,$(USEMODULE)))
USEMODULE += nanocoap_rd
endif

ifneq (,$(filter nimble_netif,$(USEMODULE)))
USEMODULE += nimble_scanner
USEMODULE += nimble_scanlist
Expand Down
46 changes: 10 additions & 36 deletions sys/shell/commands/sc_nanocoap_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "vfs_default.h"
#include "net/nanocoap_vfs.h"
#include "net/nanocoap_sock.h"
#include "net/nanocoap_rd.h"

struct dir_list_ctx {
char *buf;
Expand All @@ -39,6 +40,14 @@ static bool _is_dir(const char *url)
return url[len - 1] == '/';
}

static int _resource_cb(char *path, void *ctx)
{
(void)ctx;

puts(path);
return 0;
}

static int _print_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
{
(void)arg;
Expand All @@ -52,41 +61,6 @@ static int _print_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int mor
return 0;
}

static int _print_dir_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
{
(void)offset;
(void)more;

struct dir_list_ctx *ctx = arg;

char *end = (char *)buf + len;
for (char *c = (char *)buf; c < end; ++c) {
if (ctx->cur) {
if (*c == '>' || ctx->cur == ctx->end) {
*ctx->cur = 0;
puts(ctx->buf);
ctx->cur = NULL;
} else {
*ctx->cur++ = *c;
}
} else if (*c == '<') {
ctx->cur = ctx->buf;
}
}

return 0;
}

static int _print_dir(const char *url, char *buf, size_t len)
{
struct dir_list_ctx ctx = {
.buf = buf,
.end = buf + len,
};
return nanocoap_get_blockwise_url(url, CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT,
_print_dir_cb, &ctx);
}

int _nanocoap_get_handler(int argc, char **argv)
{
int res;
Expand All @@ -100,7 +74,7 @@ int _nanocoap_get_handler(int argc, char **argv)
}

if (_is_dir(url) && argc < 3) {
res = _print_dir(url, buffer, sizeof(buffer));
res = nanocoap_rd_get_url(url, _resource_cb, NULL);
if (res) {
printf("Request failed: %s\n", strerror(-res));
}
Expand Down

0 comments on commit dc852c2

Please sign in to comment.