Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/net/nanocoap_cli: make use of XFA for shell commands #20961

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions tests/net/nanocoap_cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@
#include "msg.h"

#include "net/nanocoap_sock.h"
#include "net/gnrc/netif.h"
#include "net/ipv6/addr.h"
#include "shell.h"

#define MAIN_QUEUE_SIZE (4)
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];

#if IS_USED(MODULE_NANOCOAP_DTLS)
#include "net/credman.h"
#include "net/dsm.h"
#include "tinydtls_keys.h"

static const uint8_t psk_id_0[] = PSK_DEFAULT_IDENTITY;
Expand All @@ -48,25 +45,6 @@ static const credman_credential_t credential = {
};
#endif

extern int nanotest_client_cmd(int argc, char **argv);
extern int nanotest_client_url_cmd(int argc, char **argv);
extern int nanotest_server_cmd(int argc, char **argv);
extern int nanotest_client_put_cmd(int argc, char **argv);
extern int nanotest_client_put_non_cmd(int argc, char **argv);
extern int nanotest_client_get_non_cmd(int argc, char **argv);
static int _list_all_inet6(int argc, char **argv);

static const shell_command_t shell_commands[] = {
{ "client", "CoAP client", nanotest_client_cmd },
{ "url", "CoAP client URL request", nanotest_client_url_cmd },
{ "put", "experimental put", nanotest_client_put_cmd },
{ "put_non", "non-confirmable put", nanotest_client_put_non_cmd },
{ "get_non", "non-confirmable get", nanotest_client_get_non_cmd },
{ "server", "CoAP server", nanotest_server_cmd },
{ "inet6", "IPv6 addresses", _list_all_inet6 },
{ NULL, NULL, NULL }
};

/* _list_all_inet6() and _print_addr() derived from sc_gnrc_netif.c */

static void _print_addr(ipv6_addr_t *addr, uint8_t flags)
Expand Down Expand Up @@ -105,7 +83,7 @@ static void _print_addr(ipv6_addr_t *addr, uint8_t flags)
printf("\n");
}

static int _list_all_inet6(int argc, char **argv)
static int _cmd_inet6(int argc, char **argv)
{
(void)argc;
(void)argv;
Expand Down Expand Up @@ -137,6 +115,8 @@ static int _list_all_inet6(int argc, char **argv)
return 0;
}

SHELL_COMMAND(inet6, "IPv6 addresses", _cmd_inet6);

int main(void)
{
/* for the thread running the shell */
Expand All @@ -154,7 +134,7 @@ int main(void)
/* start shell */
puts("All up, running the shell now");
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);

/* should never be reached */
return 0;
Expand Down
30 changes: 20 additions & 10 deletions tests/net/nanocoap_cli/nanocli_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@

#include "net/coap.h"
#include "net/gnrc/netif.h"
#include "net/ipv6.h"
#include "net/nanocoap.h"
#include "net/nanocoap_sock.h"
#include "net/sock/udp.h"
#include "net/sock/util.h"

#include "od.h"
#include "shell.h"

static ssize_t _send(coap_pkt_t *pkt, size_t len, char *addr_str, char *port_str)
static ssize_t _send(coap_pkt_t *pkt, size_t len,
char *addr_str, const char *port_str)
{
ipv6_addr_t addr;
sock_udp_ep_t remote;
Expand Down Expand Up @@ -81,7 +80,7 @@
return nanocoap_request(pkt, NULL, &remote, len);
}

int nanotest_client_cmd(int argc, char **argv)
static int _cmd_client(int argc, char **argv)
{
/* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */
const char *method_codes[] = {"get", "post", "put"};
Expand Down Expand Up @@ -166,7 +165,10 @@
return 1;
}

static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
SHELL_COMMAND(client, "CoAP client", _cmd_client);

static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf,
size_t len, int more)
{
(void)arg;
(void)more;
Expand All @@ -180,7 +182,7 @@
return 0;
}

int nanotest_client_url_cmd(int argc, char **argv)
static int _cmd_url(int argc, char **argv)
{
/* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */
const char *method_codes[] = { "get", "post", "put", "delete" };
Expand Down Expand Up @@ -208,7 +210,7 @@
break;
case COAP_METHOD_POST - 1:
case COAP_METHOD_PUT - 1:
;

Check warning on line 213 in tests/net/nanocoap_cli/nanocli_client.c

View workflow job for this annotation

GitHub Actions / static-tests

semicolon is isolated from other tokens
char response[32];
nanocoap_sock_t sock;
res = nanocoap_sock_url_connect(argv[2], &sock);
Expand Down Expand Up @@ -248,6 +250,8 @@
return -1;
}

SHELL_COMMAND(url, "CoAP client URL request", _cmd_url);

static const char song[] =
"Join us now and share the software;\n"
"You'll be free, hackers, you'll be free.\n"
Expand All @@ -269,7 +273,7 @@
"Join us now and share the software;\n"
"You'll be free, hackers, you'll be free.\n";

int nanotest_client_put_cmd(int argc, char **argv)
static int _cmd_put(int argc, char **argv)
{
int res;
nanocoap_sock_t sock;
Expand Down Expand Up @@ -304,7 +308,9 @@
return res;
}

int nanotest_client_put_non_cmd(int argc, char **argv)
SHELL_COMMAND(put, "experimental put", _cmd_put);

static int _cmd_put_non(int argc, char **argv)
{
int res;

Expand All @@ -323,7 +329,9 @@
return res;
}

int nanotest_client_get_non_cmd(int argc, char **argv)
SHELL_COMMAND(put_non, "non-confirmable put", _cmd_put_non);

static int _cmd_get_non(int argc, char **argv)
{
int res;

Expand All @@ -348,3 +356,5 @@
}
return res;
}

SHELL_COMMAND(get_non, "non-confirmable get", _cmd_get_non);
11 changes: 9 additions & 2 deletions tests/net/nanocoap_cli/nanocli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include <stdio.h>
#include <string.h>

#include "net/nanocoap_sock.h"
#include "net/coap.h"
#include "net/nanocoap.h"
#include "net/sock/udp.h"
#include "shell.h"

#define ENABLE_DEBUG 0
#include "debug.h"
Expand Down Expand Up @@ -69,6 +71,9 @@ static int _nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize,
}
if ((res = coap_handle_req(&pkt, buf, bufsize, &ctx)) > 0) {
res = sock_udp_send(&sock, buf, res, &remote);
if (res < 0) {
DEBUG("nanocoap: failed to send: %" PRIdSIZE "\n", res);
}
}
}
}
Expand All @@ -83,7 +88,7 @@ static void _start_server(uint16_t port, int ignore_count)
_nanocoap_server(&local, buf, sizeof(buf), ignore_count);
}

int nanotest_server_cmd(int argc, char **argv)
static int _cmd_server(int argc, char **argv)
{
if (argc < 2) {
goto error;
Expand Down Expand Up @@ -132,3 +137,5 @@ int nanotest_server_cmd(int argc, char **argv)
printf(" port defaults to %u\n", COAP_PORT);
return 1;
}

SHELL_COMMAND(server, "CoAP server", _cmd_server);
Loading