Skip to content

Commit c8526bc

Browse files
nixwardaescolar
authored andcommitted
net: lwm2m: shell: add observations cmd
Outputs observation configurations. Signed-off-by: Nick Ward <[email protected]>
1 parent 0645609 commit c8526bc

File tree

1 file changed

+88
-1
lines changed

1 file changed

+88
-1
lines changed

subsys/net/lib/lwm2m/lwm2m_shell.c

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
5353
#define LWM2M_HELP_RESUME "LwM2M engine thread resume"
5454
#define LWM2M_HELP_LOCK "Lock the LwM2M registry"
5555
#define LWM2M_HELP_UNLOCK "Unlock the LwM2M registry"
56+
#define LWM2M_HELP_OBSERV "List observations"
5657
#define LWM2M_HELP_CACHE "cache PATH NUM\nEnable data cache for resource\n" \
5758
"PATH is LwM2M path\n" \
5859
"NUM how many elements to cache\n" \
@@ -625,6 +626,92 @@ static int cmd_cache(const struct shell *sh, size_t argc, char **argv)
625626
#endif
626627
}
627628

629+
static void shell_print_attr(const struct shell *sh, void *ref)
630+
{
631+
struct lwm2m_attr *attr = NULL;
632+
bool found;
633+
634+
for (uint8_t type = 0; type < NR_LWM2M_ATTR; type++) {
635+
found = false;
636+
while ((attr = lwm2m_engine_get_next_attr(ref, attr)) != NULL) {
637+
if (attr->type == type) {
638+
found = true;
639+
break;
640+
}
641+
}
642+
if (found) {
643+
switch (type) {
644+
case LWM2M_ATTR_PMIN:
645+
/* fall through */
646+
case LWM2M_ATTR_PMAX:
647+
shell_fprintf(sh, SHELL_NORMAL, "%10u", attr->int_val);
648+
break;
649+
case LWM2M_ATTR_GT:
650+
/* fall through */
651+
case LWM2M_ATTR_LT:
652+
/* fall through */
653+
case LWM2M_ATTR_STEP:
654+
shell_fprintf(sh, SHELL_NORMAL, "%10f", attr->float_val);
655+
break;
656+
}
657+
} else {
658+
shell_fprintf(sh, SHELL_NORMAL, "%10s", "");
659+
}
660+
}
661+
}
662+
663+
static int cmd_observations(const struct shell *sh, size_t argc, char **argv)
664+
{
665+
char buf[LWM2M_MAX_PATH_STR_SIZE];
666+
struct lwm2m_obj_path_list *o_p;
667+
struct observe_node *obs;
668+
uint32_t i = 0, path_i;
669+
struct lwm2m_ctx *ctx;
670+
void *ref;
671+
int ret;
672+
673+
ctx = lwm2m_rd_client_ctx();
674+
if (ctx == NULL) {
675+
shell_error(sh, "no lwm2m context yet\n");
676+
return -ENOEXEC;
677+
}
678+
679+
ARG_UNUSED(argc);
680+
ARG_UNUSED(argv);
681+
682+
shell_fprintf(sh, SHELL_INFO, " # %10s %18s", "composite", "path");
683+
for (i = 0; i < NR_LWM2M_ATTR; i++) {
684+
shell_fprintf(sh, SHELL_INFO, "%10s", lwm2m_attr_to_str(i));
685+
}
686+
shell_print(sh, "");
687+
688+
lwm2m_registry_lock();
689+
i = 0;
690+
SYS_SLIST_FOR_EACH_CONTAINER(&ctx->observer, obs, node) {
691+
shell_fprintf(sh, SHELL_NORMAL, "%2u %10c ", i, obs->composite ? 'y' : 'n');
692+
path_i = 0;
693+
SYS_SLIST_FOR_EACH_CONTAINER(&obs->path_list, o_p, node) {
694+
if (path_i > 0) {
695+
shell_fprintf(sh, SHELL_NORMAL, "%14s", "");
696+
}
697+
shell_fprintf(sh, SHELL_NORMAL, "%-18s",
698+
lwm2m_path_log_buf(buf, &o_p->path));
699+
ret = lwm2m_get_path_reference_ptr(NULL, &o_p->path, &ref);
700+
if (ret < 0) {
701+
continue;
702+
}
703+
shell_print_attr(sh, ref);
704+
path_i++;
705+
shell_print(sh, "");
706+
}
707+
i++;
708+
709+
}
710+
lwm2m_registry_unlock();
711+
712+
return 0;
713+
}
714+
628715
SHELL_STATIC_SUBCMD_SET_CREATE(
629716
sub_lwm2m,
630717
SHELL_COND_CMD_ARG(CONFIG_LWM2M_VERSION_1_1, send, NULL,
@@ -642,7 +729,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
642729
SHELL_CMD_ARG(resume, NULL, LWM2M_HELP_RESUME, cmd_resume, 1, 0),
643730
SHELL_CMD_ARG(lock, NULL, LWM2M_HELP_LOCK, cmd_lock, 1, 0),
644731
SHELL_CMD_ARG(unlock, NULL, LWM2M_HELP_UNLOCK, cmd_unlock, 1, 0),
645-
732+
SHELL_CMD_ARG(obs, NULL, LWM2M_HELP_OBSERV, cmd_observations, 1, 0),
646733
SHELL_SUBCMD_SET_END);
647734
SHELL_COND_CMD_ARG_REGISTER(CONFIG_LWM2M_SHELL, lwm2m, &sub_lwm2m,
648735
LWM2M_HELP_CMD, NULL, 1, 0);

0 commit comments

Comments
 (0)