diff --git a/examples/lwm2m/Makefile b/examples/lwm2m/Makefile index efbecb6cd1..cc6324caac 100644 --- a/examples/lwm2m/Makefile +++ b/examples/lwm2m/Makefile @@ -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 diff --git a/examples/lwm2m/lwm2m_cli.c b/examples/lwm2m/lwm2m_cli.c index 9b9d63a39e..147dd3c8f8 100644 --- a/examples/lwm2m/lwm2m_cli.c +++ b/examples/lwm2m/lwm2m_cli.c @@ -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]; @@ -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 = { @@ -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, @@ -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 \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; +} + + int lwm2m_cli_cmd(int argc, char **argv) { if (argc == 1) { @@ -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 \n", argv[0]); + printf("usage: %s \n", argv[0]); } else { - printf("usage: %s \n", argv[0]); + printf("usage: %s \n", argv[0]); } return 1;