Skip to content

Commit f93ab27

Browse files
PavelVPVcarlescufi
authored andcommitted
Bluetooth: Mesh: Add macro for instance set/get-all mesh shell cmds
This code is a boulerplate that will be needed for many models in mesh shell module. This commit adds a special macro designed to improve readability of the code and helps to avoid potential bugs when copy-pasting identical code. Signed-off-by: Pavel Vasilyev <[email protected]>
1 parent 8e5ff49 commit f93ab27

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

subsys/bluetooth/mesh/shell/health_cli.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -345,28 +345,7 @@ static int cmd_attention_set_unack(const struct shell *sh, size_t argc, char *ar
345345
return attention_set(sh, argc, argv, false);
346346
}
347347

348-
static int cmd_instance_get_all(const struct shell *sh, size_t argc, char *argv[])
349-
{
350-
return bt_mesh_shell_mdl_print_all(sh, BT_MESH_MODEL_ID_HEALTH_CLI);
351-
}
352-
353-
static int cmd_instance_set(const struct shell *sh, size_t argc, char *argv[])
354-
{
355-
int err = 0;
356-
uint8_t elem_idx = shell_strtoul(argv[1], 0, &err);
357-
358-
if (err) {
359-
shell_warn(sh, "Unable to parse input string arg");
360-
return err;
361-
}
362-
363-
return bt_mesh_shell_mdl_instance_set(sh, &mod, BT_MESH_MODEL_ID_HEALTH_CLI, elem_idx);
364-
}
365-
366-
SHELL_STATIC_SUBCMD_SET_CREATE(instance_cmds,
367-
SHELL_CMD_ARG(set, NULL, "<elem_idx> ", cmd_instance_set, 2, 0),
368-
SHELL_CMD_ARG(get-all, NULL, NULL, cmd_instance_get_all, 1, 0),
369-
SHELL_SUBCMD_SET_END);
348+
BT_MESH_SHELL_MDL_INSTANCE_CMDS(instance_cmds, BT_MESH_MODEL_ID_HEALTH_CLI, mod);
370349

371350
SHELL_STATIC_SUBCMD_SET_CREATE(
372351
health_cli_cmds,

subsys/bluetooth/mesh/shell/utils.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@
77
#include <stdint.h>
88
#include <zephyr/shell/shell.h>
99

10+
#define BT_MESH_SHELL_MDL_INSTANCE_CMDS(cmd_set_name, mod_id, mod_ptr) \
11+
static int cmd_##cmd_set_name##_get_all(const struct shell *sh, size_t argc, char *argv[]) \
12+
{ \
13+
return bt_mesh_shell_mdl_print_all(sh, (mod_id)); \
14+
} \
15+
\
16+
static int cmd_##cmd_set_name##_set(const struct shell *sh, size_t argc, \
17+
char *argv[]) \
18+
{ \
19+
int err = 0; \
20+
uint8_t elem_idx = shell_strtoul(argv[1], 0, &err); \
21+
\
22+
if (err) { \
23+
shell_warn(sh, "Unable to parse input string arg"); \
24+
return err; \
25+
} \
26+
\
27+
return bt_mesh_shell_mdl_instance_set(sh, &(mod_ptr), (mod_id), elem_idx); \
28+
} \
29+
\
30+
SHELL_STATIC_SUBCMD_SET_CREATE(cmd_set_name, \
31+
SHELL_CMD_ARG(set, NULL, "<elem_idx>", cmd_##cmd_set_name##_set, 2,\
32+
0), \
33+
SHELL_CMD_ARG(get-all, NULL, NULL, cmd_##cmd_set_name##_get_all, 1,\
34+
0), \
35+
SHELL_SUBCMD_SET_END);
36+
1037
bool bt_mesh_shell_mdl_first_get(uint16_t id, struct bt_mesh_model **mod);
1138

1239
int bt_mesh_shell_mdl_instance_set(const struct shell *shell, struct bt_mesh_model **mod,

0 commit comments

Comments
 (0)