Skip to content

Commit

Permalink
OpenThread CoAP sample: Add shell commands
Browse files Browse the repository at this point in the history
This adds shell commands to easily get or set LED.

Signed-off-by: Alexandre Bailon <[email protected]>
  • Loading branch information
anobli committed Nov 29, 2024
1 parent 6af6998 commit b6dbf0c
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 1 deletion.
1 change: 1 addition & 0 deletions samples/net/openthread/coap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_include_directories(app PRIVATE src)
target_sources(app PRIVATE
src/main.c
src/coap_utils.c
src/coap_shell.c
)

target_sources_ifdef(CONFIG_OT_COAP_SAMPLE_LED app PRIVATE src/led.c)
Expand Down
22 changes: 21 additions & 1 deletion samples/net/openthread/coap/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,24 @@ Then, send a request to the server to toggle the LED:
The LED state should have changed.


.. _OpenThread Border Router: https://openthread.io/codelabs/openthread-border-router-nat64?hl=fr#1
.. _OpenThread Border Router: https://openthread.io/codelabs/openthread-border-router-nat64?hl=fr#1

Controlling server board's LED using shell command
**************************************************

The example also provides a shell command to control the LED on the server from the client.

To toggle the LED:

.. code-block::
$client:~$ coap led set 0 toggle
The LED state should have changed.

Same as for the button, this uses the broadcast address by default.
To control the LED of a specific server, we can use it IPv6 address:

.. code-block::
$client:~$ coap led set 0 toggle fd6f:cb3a:802:1:f0ec:c1e2:c1bb:744
148 changes: 148 additions & 0 deletions samples/net/openthread/coap/src/coap_shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright (c) 2024 Alexandre Bailon
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/shell/shell.h>

#include <led.h>
#include <button.h>

static int coap_cmd_led_set(const struct shell *sh, size_t argc, char **argv)
{
int led_id;
int led_state;
const char *addr;
int err = 0;

led_id = shell_strtol(argv[1], 10, &err);
if (err) {
shell_error(sh, "Failed to get a valid LED id: %s", argv[1]);
return err;
}

if (!strcmp(argv[2], "on")) {
led_state = LED_MSG_STATE_ON;
} else if (!strcmp(argv[2], "off")) {
led_state = LED_MSG_STATE_OFF;
} else if (!strcmp(argv[2], "toggle")) {
led_state = LED_MSG_STATE_TOGGLE;
} else {
shell_error(sh, "Failed to get a valid LED state");
return -EINVAL;
}

if (argc <= 3) {
addr = "ff03::1";
} else {
addr = argv[3];
}

return coap_led_set_state(addr, led_id, led_state);
}

static int coap_cmd_led_get(const struct shell *sh, size_t argc, char **argv)
{
int led_id;
int led_state;
const char *addr;
int err = 0;

led_id = shell_strtol(argv[1], 10, &err);
if (err) {
shell_error(sh, "Failed to get a valid LED id: %s", argv[1]);
return err;
}

if (argc <= 3) {
addr = "ff03::1";
} else {
addr = argv[3];
}

err = coap_led_get_state(addr, led_id, &led_state);
if (err) {
return err;
}

if (led_state == LED_MSG_STATE_ON) {
shell_info(sh, "on");
} else {
shell_info(sh, "off");
}

return 0;
}

static int coap_cmd_btn_get(const struct shell *sh, size_t argc, char **argv)
{
int btn_id;
int btn_state;
const char *addr;
int err = 0;

btn_id = shell_strtol(argv[1], 10, &err);
if (err) {
shell_error(sh, "Failed to get a valid button id: %s", argv[1]);
return err;
}

if (argc <= 3) {
addr = "ff03::1";
} else {
addr = argv[3];
}

err = coap_btn_get_state(addr, btn_id, &btn_state);
if (err) {
return err;
}

if (btn_state == BTN_MSG_STATE_ON) {
shell_info(sh, "on");
} else {
shell_info(sh, "off");
}

return 0;
}

static const char coap_cmd_led_set_help[] =
"Set a LED state using CoAP\n"
"led set <led_id> <led_state> [addr]\n"
"\tled_id: a number defining the LED to control from CoAP server\n"
"\tled_state: on, off or toggle\n"
"\taddr: the IPv6 address of CoAP server. If not defined, it will broadcast to all CoAP "
"servers\n";

static const char coap_cmd_led_get_help[] =
"Get a LED state using CoAP\n"
"led get <led_id> [addr]\n"
"\tled_id: a number defining the LED to get from CoAP server\n"
"\taddr: the IPv6 address of CoAP server. If not defined, it "
"will broadcast to all CoAP servers\n";

SHELL_STATIC_SUBCMD_SET_CREATE(coap_led_subcmd,
SHELL_CMD_ARG(set, NULL, coap_cmd_led_set_help, coap_cmd_led_set, 3, 1),
SHELL_CMD_ARG(get, NULL, coap_cmd_led_get_help, coap_cmd_led_get, 2, 1),
SHELL_SUBCMD_SET_END);

static const char coap_cmd_btn_get_help[] =
"Get a button state using CoAP\n"
"btn get <btn_id> [addr]\n"
"\tbtn_id: a number defining the button to get from CoAP server\n"
"\taddr: the IPv6 address of CoAP server. If not defined, it "
"will broadcast to all CoAP servers\n";

SHELL_STATIC_SUBCMD_SET_CREATE(coap_btn_subcmd,
SHELL_CMD_ARG(get, NULL, coap_cmd_btn_get_help, coap_cmd_btn_get, 2, 1),
SHELL_SUBCMD_SET_END);


SHELL_STATIC_SUBCMD_SET_CREATE(coap_subcmd,
SHELL_CMD(led, &coap_led_subcmd, "Manage a LED using CoAP", NULL),
SHELL_CMD(btn, &coap_btn_subcmd, "Manage a button using CoAP", NULL),
SHELL_SUBCMD_SET_END);

SHELL_CMD_REGISTER(coap, &coap_subcmd, "CoAP sample client", NULL);
5 changes: 5 additions & 0 deletions samples/net/openthread/coap/src/coap_shell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* Copyright (c) 2024 Alexandre Bailon
*
* SPDX-License-Identifier: Apache-2.0
*/

0 comments on commit b6dbf0c

Please sign in to comment.