Skip to content

Commit e436f45

Browse files
committed
tests/net/nanocoap_cli: make use of XFA for shell commands
1 parent 06aaf64 commit e436f45

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

tests/net/nanocoap_cli/main.c

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@
2222
#include "msg.h"
2323

2424
#include "net/nanocoap_sock.h"
25-
#include "net/gnrc/netif.h"
26-
#include "net/ipv6/addr.h"
2725
#include "shell.h"
2826

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

3230
#if IS_USED(MODULE_NANOCOAP_DTLS)
3331
#include "net/credman.h"
34-
#include "net/dsm.h"
3532
#include "tinydtls_keys.h"
3633

3734
static const uint8_t psk_id_0[] = PSK_DEFAULT_IDENTITY;
@@ -48,25 +45,6 @@ static const credman_credential_t credential = {
4845
};
4946
#endif
5047

51-
extern int nanotest_client_cmd(int argc, char **argv);
52-
extern int nanotest_client_url_cmd(int argc, char **argv);
53-
extern int nanotest_server_cmd(int argc, char **argv);
54-
extern int nanotest_client_put_cmd(int argc, char **argv);
55-
extern int nanotest_client_put_non_cmd(int argc, char **argv);
56-
extern int nanotest_client_get_non_cmd(int argc, char **argv);
57-
static int _list_all_inet6(int argc, char **argv);
58-
59-
static const shell_command_t shell_commands[] = {
60-
{ "client", "CoAP client", nanotest_client_cmd },
61-
{ "url", "CoAP client URL request", nanotest_client_url_cmd },
62-
{ "put", "experimental put", nanotest_client_put_cmd },
63-
{ "put_non", "non-confirmable put", nanotest_client_put_non_cmd },
64-
{ "get_non", "non-confirmable get", nanotest_client_get_non_cmd },
65-
{ "server", "CoAP server", nanotest_server_cmd },
66-
{ "inet6", "IPv6 addresses", _list_all_inet6 },
67-
{ NULL, NULL, NULL }
68-
};
69-
7048
/* _list_all_inet6() and _print_addr() derived from sc_gnrc_netif.c */
7149

7250
static void _print_addr(ipv6_addr_t *addr, uint8_t flags)
@@ -105,7 +83,7 @@ static void _print_addr(ipv6_addr_t *addr, uint8_t flags)
10583
printf("\n");
10684
}
10785

108-
static int _list_all_inet6(int argc, char **argv)
86+
static int _cmd_inet6(int argc, char **argv)
10987
{
11088
(void)argc;
11189
(void)argv;
@@ -137,6 +115,8 @@ static int _list_all_inet6(int argc, char **argv)
137115
return 0;
138116
}
139117

118+
SHELL_COMMAND(inet6, "IPv6 addresses", _cmd_inet6);
119+
140120
int main(void)
141121
{
142122
/* for the thread running the shell */
@@ -154,7 +134,7 @@ int main(void)
154134
/* start shell */
155135
puts("All up, running the shell now");
156136
char line_buf[SHELL_DEFAULT_BUFSIZE];
157-
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
137+
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
158138

159139
/* should never be reached */
160140
return 0;

tests/net/nanocoap_cli/nanocli_client.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525

2626
#include "net/coap.h"
2727
#include "net/gnrc/netif.h"
28-
#include "net/ipv6.h"
2928
#include "net/nanocoap.h"
3029
#include "net/nanocoap_sock.h"
31-
#include "net/sock/udp.h"
3230
#include "net/sock/util.h"
33-
3431
#include "od.h"
32+
#include "shell.h"
3533

36-
static ssize_t _send(coap_pkt_t *pkt, size_t len, char *addr_str, char *port_str)
34+
static ssize_t _send(coap_pkt_t *pkt, size_t len,
35+
char *addr_str, const char *port_str)
3736
{
3837
ipv6_addr_t addr;
3938
sock_udp_ep_t remote;
@@ -81,7 +80,7 @@ static ssize_t _send(coap_pkt_t *pkt, size_t len, char *addr_str, char *port_str
8180
return nanocoap_request(pkt, NULL, &remote, len);
8281
}
8382

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

169-
static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
168+
SHELL_COMMAND(client, "CoAP client", _cmd_client);
169+
170+
static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf,
171+
size_t len, int more)
170172
{
171173
(void)arg;
172174
(void)more;
@@ -180,7 +182,7 @@ static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int
180182
return 0;
181183
}
182184

183-
int nanotest_client_url_cmd(int argc, char **argv)
185+
static int _cmd_url(int argc, char **argv)
184186
{
185187
/* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */
186188
const char *method_codes[] = { "get", "post", "put", "delete" };
@@ -248,6 +250,8 @@ int nanotest_client_url_cmd(int argc, char **argv)
248250
return -1;
249251
}
250252

253+
SHELL_COMMAND(url, "CoAP client URL request", _cmd_url);
254+
251255
static const char song[] =
252256
"Join us now and share the software;\n"
253257
"You'll be free, hackers, you'll be free.\n"
@@ -269,7 +273,7 @@ static const char song[] =
269273
"Join us now and share the software;\n"
270274
"You'll be free, hackers, you'll be free.\n";
271275

272-
int nanotest_client_put_cmd(int argc, char **argv)
276+
static int _cmd_put(int argc, char **argv)
273277
{
274278
int res;
275279
nanocoap_sock_t sock;
@@ -304,7 +308,9 @@ int nanotest_client_put_cmd(int argc, char **argv)
304308
return res;
305309
}
306310

307-
int nanotest_client_put_non_cmd(int argc, char **argv)
311+
SHELL_COMMAND(put, "experimental put", _cmd_put);
312+
313+
static int _cmd_put_non(int argc, char **argv)
308314
{
309315
int res;
310316

@@ -323,7 +329,9 @@ int nanotest_client_put_non_cmd(int argc, char **argv)
323329
return res;
324330
}
325331

326-
int nanotest_client_get_non_cmd(int argc, char **argv)
332+
SHELL_COMMAND(put_non, "non-confirmable put", _cmd_put_non);
333+
334+
static int _cmd_get_non(int argc, char **argv)
327335
{
328336
int res;
329337

@@ -348,3 +356,5 @@ int nanotest_client_get_non_cmd(int argc, char **argv)
348356
}
349357
return res;
350358
}
359+
360+
SHELL_COMMAND(get_non, "non-confirmable get", _cmd_get_non);

tests/net/nanocoap_cli/nanocli_server.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
#include <stdio.h>
2222
#include <string.h>
2323

24-
#include "net/nanocoap_sock.h"
24+
#include "net/coap.h"
25+
#include "net/nanocoap.h"
2526
#include "net/sock/udp.h"
27+
#include "shell.h"
2628

2729
#define ENABLE_DEBUG 0
2830
#include "debug.h"
@@ -69,6 +71,9 @@ static int _nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize,
6971
}
7072
if ((res = coap_handle_req(&pkt, buf, bufsize, &ctx)) > 0) {
7173
res = sock_udp_send(&sock, buf, res, &remote);
74+
if (res < 0) {
75+
DEBUG("nanocoap: failed to send: %" PRIdSIZE "\n", res);
76+
}
7277
}
7378
}
7479
}
@@ -83,7 +88,7 @@ static void _start_server(uint16_t port, int ignore_count)
8388
_nanocoap_server(&local, buf, sizeof(buf), ignore_count);
8489
}
8590

86-
int nanotest_server_cmd(int argc, char **argv)
91+
static int _cmd_server(int argc, char **argv)
8792
{
8893
if (argc < 2) {
8994
goto error;
@@ -132,3 +137,5 @@ int nanotest_server_cmd(int argc, char **argv)
132137
printf(" port defaults to %u\n", COAP_PORT);
133138
return 1;
134139
}
140+
141+
SHELL_COMMAND(server, "CoAP server", _cmd_server);

0 commit comments

Comments
 (0)