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 Dec 11, 2022
1 parent 27122a4 commit bdabc7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
1 change: 1 addition & 0 deletions sys/shell/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ endif
ifneq (,$(filter shell_cmd_nanocoap_vfs,$(USEMODULE)))
USEMODULE += nanocoap_vfs
USEMODULE += vfs_util
USEMODULE += nanocoap_link_format
endif
ifneq (,$(filter shell_cmd_netstats_neighbor,$(USEMODULE)))
USEMODULE += netstats_neighbor
Expand Down
52 changes: 17 additions & 35 deletions sys/shell/cmds/nanocoap_vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
#include <string.h>
#include <inttypes.h>

#include "net/nanocoap/link_format.h"
#include "net/nanocoap_sock.h"
#include "net/nanocoap_vfs.h"

#include "shell.h"
#include "vfs_default.h"
#include "vfs_util.h"
Expand All @@ -48,54 +50,34 @@ static bool _is_dir(const char *url)
return url[len - 1] == '/';
}

static int _print_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
static int _resource_cb(char *entry, void *ctx)
{
(void)arg;
(void)offset;
(void)ctx;

write(STDOUT_FILENO, buf, len);
if (!more) {
puts("");
char *start = strchr(entry, '<');
if (start) {
char *end = strchr(entry, '>');
*end = '\0';
entry = start + 1;
}

puts(entry);
return 0;
}

static int _print_dir_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
static int _print_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
{
(void)arg;
(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;
}

write(STDOUT_FILENO, buf, len);
if (!more) {
puts("");
}

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);
}

static int _nanocoap_get_handler(int argc, char **argv)
{
int res;
Expand All @@ -109,7 +91,7 @@ static int _nanocoap_get_handler(int argc, char **argv)
}

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

0 comments on commit bdabc7b

Please sign in to comment.