Skip to content

Commit

Permalink
examples/lwm2m: add on/off switch object
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrolanzieri committed Dec 25, 2024
1 parent 0b65445 commit 3621c59
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/lwm2m/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ DEVELHELP ?= 1
# NOTE: Add the package for wakaama
USEPKG += wakaama
USEMODULE += wakaama_objects_light_control
USEMODULE += wakaama_objects_on_off_switch

# add DTLS support
USEMODULE += wakaama_client_dtls
Expand Down
42 changes: 39 additions & 3 deletions examples/lwm2m/lwm2m_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
#include "objects/device.h"
#include "objects/security.h"
#include "objects/light_control.h"
#include "objects/on_off_switch.h"

#include "credentials.h"

#define LED_COLOR "FFFFFF"
#define LED_APP_TYPE "LED 0"
# define OBJ_COUNT (4)
# define OBJ_COUNT (5)

uint8_t connected = 0;
lwm2m_object_t *obj_list[OBJ_COUNT];
Expand Down Expand Up @@ -67,6 +68,7 @@ void lwm2m_cli_init(void)
obj_list[1] = lwm2m_client_get_server_object(&client_data, CONFIG_LWM2M_SERVER_SHORT_ID);
obj_list[2] = lwm2m_object_device_init(&client_data);
obj_list[3] = lwm2m_object_light_control_init(&client_data);
obj_list[4] = lwm2m_object_on_off_switch_init(&client_data);

/* create light control object instance */
lwm2m_obj_light_control_args_t light_args = {
Expand All @@ -83,6 +85,17 @@ void lwm2m_cli_init(void)
puts("Error instantiating light control");
}

/* create on/off switch object instance */
lwm2m_obj_on_off_switch_args_t switch_args = {
.app_type = "Switch 0",
.app_type_len = sizeof("Switch 0") - 1
};

res = lwm2m_object_on_off_switch_instance_create(&switch_args, 0);
if (res < 0) {
puts("Error instantiating on/off switch");
}

/* create security object instance */
lwm2m_obj_security_args_t security_args = {
.server_id = CONFIG_LWM2M_SERVER_SHORT_ID,
Expand Down Expand Up @@ -159,6 +172,25 @@ static int _parse_lwm2m_light_cmd(int argc, char **argv)
return 0;
}

static int _parse_lwm2m_switch_cmd(int argc, char **argv)
{
if (argc < 3) {
printf("usage: %s switch <on|off>\n", argv[0]);
return 1;
}

if (!connected) {
puts("LwM2M client not connected");
return 1;
}

bool status = !strcmp(argv[2], "on");
lwm2m_object_on_off_switch_update_status(0, status);

return 0;
}


Check warning on line 193 in examples/lwm2m/lwm2m_cli.c

View workflow job for this annotation

GitHub Actions / static-tests

too many consecutive empty lines
int lwm2m_cli_cmd(int argc, char **argv)
{
if (argc == 1) {
Expand All @@ -182,12 +214,16 @@ int lwm2m_cli_cmd(int argc, char **argv)
return _parse_lwm2m_light_cmd(argc, argv);
}

if (!strcmp(argv[1], "switch")) {
return _parse_lwm2m_switch_cmd(argc, argv);
}

help_error:
if (IS_ACTIVE(DEVELHELP)) {
printf("usage: %s <start|mem|light>\n", argv[0]);
printf("usage: %s <start|mem|light|switch>\n", argv[0]);
}
else {
printf("usage: %s <start|light>\n", argv[0]);
printf("usage: %s <start|light|switch>\n", argv[0]);
}

return 1;
Expand Down

0 comments on commit 3621c59

Please sign in to comment.