Skip to content

Commit

Permalink
net: lwm2m: Add shell support for deleting object and resource instances
Browse files Browse the repository at this point in the history
Extend the previous support of creating object instances by allowing
creation of resource instances as well.
Similarly, add support for deleting.

Signed-off-by: Seppo Takalo <[email protected]>
  • Loading branch information
SeppoTakalo authored and carlescufi committed Nov 2, 2023
1 parent 9cf07bb commit 2eb804a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
5 changes: 4 additions & 1 deletion doc/connectivity/networking/api/lwm2m.rst
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,10 @@ required actions from the server side.
-t Write value as time_t
create :create PATH
Create object instance
Create object or resource instance
delete :delete PATH
Delete object or resource instance
cache :cache PATH NUM
Enable data cache for resource
Expand Down
47 changes: 37 additions & 10 deletions subsys/net/lib/lwm2m/lwm2m_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
"-sX\tWrite value as intX_t\n" \
"-f \tWrite value as float\n" \
"-t \tWrite value as time_t\n"
#define LWM2M_HELP_CREATE "create PATH\nCreate object instance\n"
#define LWM2M_HELP_CREATE "create PATH\nCreate object or resource instance\n"
#define LWM2M_HELP_DELETE "delete PATH\nDelete object or resource instance\n"
#define LWM2M_HELP_START "start EP_NAME [BOOTSTRAP FLAG]\n" \
"Start the LwM2M RD (Registration / Discovery) Client\n" \
"-b \tSet the bootstrap flag (default 0)\n"
Expand Down Expand Up @@ -371,10 +372,9 @@ static int cmd_write(const struct shell *sh, size_t argc, char **argv)
return 0;
}

static int cmd_create(const struct shell *sh, size_t argc, char **argv)
static int cmd_create_or_delete(const struct shell *sh, bool delete, size_t argc, char **argv)
{
struct lwm2m_obj_path path;
struct lwm2m_engine_obj_inst *obj_inst;
int ret;

if (argc < 2) {
Expand All @@ -389,22 +389,48 @@ static int cmd_create(const struct shell *sh, size_t argc, char **argv)
return -ENOEXEC;
}

if (path.level != LWM2M_PATH_LEVEL_OBJECT_INST) {
shell_error(sh, "path is not an object instance\n");
shell_help(sh);
return -EINVAL;
if (delete) {
switch (path.level) {
case LWM2M_PATH_LEVEL_RESOURCE_INST:
ret = lwm2m_delete_res_inst(&path);
break;
case LWM2M_PATH_LEVEL_OBJECT_INST:
ret = lwm2m_delete_object_inst(&path);
break;
default:
return -ENOEXEC;
}
} else {
switch (path.level) {
case LWM2M_PATH_LEVEL_RESOURCE_INST:
ret = lwm2m_create_res_inst(&path);
break;
case LWM2M_PATH_LEVEL_OBJECT_INST:
ret = lwm2m_create_object_inst(&path);
break;
default:
return -ENOEXEC;
}
}

ret = lwm2m_create_obj_inst(path.obj_id, path.obj_inst_id, &obj_inst);
if (ret < 0) {
shell_error(sh, "Failed to create object instance %d/%d, ret=%d", path.obj_id,
path.obj_inst_id, ret);
shell_error(sh, "operation failed, %d\n", ret);
return -ENOEXEC;
}

return 0;
}

static int cmd_create(const struct shell *sh, size_t argc, char **argv)
{
return cmd_create_or_delete(sh, false, argc, argv);
}

static int cmd_delete(const struct shell *sh, size_t argc, char **argv)
{
return cmd_create_or_delete(sh, true, argc, argv);
}

static int cmd_start(const struct shell *sh, size_t argc, char **argv)
{
struct lwm2m_ctx *ctx = lwm2m_rd_client_ctx();
Expand Down Expand Up @@ -595,6 +621,7 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
SHELL_CMD_ARG(read, NULL, LWM2M_HELP_READ, cmd_read, 2, 1),
SHELL_CMD_ARG(write, NULL, LWM2M_HELP_WRITE, cmd_write, 3, 1),
SHELL_CMD_ARG(create, NULL, LWM2M_HELP_CREATE, cmd_create, 2, 0),
SHELL_CMD_ARG(delete, NULL, LWM2M_HELP_DELETE, cmd_delete, 2, 0),
SHELL_CMD_ARG(cache, NULL, LWM2M_HELP_CACHE, cmd_cache, 3, 0),
SHELL_CMD_ARG(start, NULL, LWM2M_HELP_START, cmd_start, 2, 2),
SHELL_CMD_ARG(stop, NULL, LWM2M_HELP_STOP, cmd_stop, 1, 1),
Expand Down

0 comments on commit 2eb804a

Please sign in to comment.