diff --git a/examples/lwm2m/Makefile b/examples/lwm2m/Makefile index 741773e8e4..a214c2079c 100644 --- a/examples/lwm2m/Makefile +++ b/examples/lwm2m/Makefile @@ -32,6 +32,7 @@ SERVER_URI ?= '"coap://[fd00:dead:beef::1]"' # NOTE: Add the package for wakaama USEPKG += wakaama +USEMODULE += wakaama_objects_light_control # Uncomment to enable Wakaama debug log #CFLAGS += -DCONFIG_LWM2M_WITH_LOGS=1 diff --git a/examples/lwm2m/README.md b/examples/lwm2m/README.md index 1f51a119f6..8a722ca85f 100644 --- a/examples/lwm2m/README.md +++ b/examples/lwm2m/README.md @@ -6,10 +6,10 @@ on the node with instances of the following objects: - [Security object](http://www.openmobilealliance.org/tech/profiles/LWM2M_Security-v1_0.xml) - [Server object](http://www.openmobilealliance.org/tech/profiles/LWM2M_Server-v1_0.xml) - [Device object](http://www.openmobilealliance.org/tech/profiles/LWM2M_Device-v1_0_3.xml) +- [Light control object](https://raw.githubusercontent.com/OpenMobileAlliance/lwm2m-registry/prod/3311.xml) The application is based on the Eclipse Wakaama -[example client](https://github.com/eclipse/wakaama/tree/master/examples/client) -. +[example client](https://github.com/eclipse/wakaama/tree/master/examples/client). ## Usage @@ -18,10 +18,13 @@ To test the client a LwM2M server where to register is needed. [Eclipse Leshan](https://github.com/eclipse/leshan) demo is a good option for running one locally. -To run the demo server: +To run the demo server, download the jar file: ```shell wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-server-demo.jar +``` +And run it: +```shell java -jar ./leshan-server-demo.jar ``` It will output the addresses where it is listening: @@ -48,11 +51,15 @@ By default the bootstrap server option is disabled, it can be enabled by definin To run the bootstrap server, make sure that the ports it uses are different from the ones of previous server (default are 5683 for CoAP, 5684 for CoAPs, and 8080 for the webserver), and that it corresponds to the one set in -`lwm2m.h` as `CONFIG_LWM2M_BSSERVER_PORT`: -```shell -# download demo -wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-bsserver-demo.jar +`lwm2m.h` as `CONFIG_LWM2M_BSSERVER_PORT`. +Download the jar file: +```shell +wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-bsserver-demo.jar +``` + +And run it: +```shell# download demo # set CoAP, CoAPs and webserver ports for bootstrap server BS_COAPPORT=5685 BS_COAPSPORT=5686 @@ -111,3 +118,7 @@ BOARD= make clean all flash term #### Shell commands - `lwm2m start`: Starts the LwM2M by configuring the module and registering to the server. +- `lwm2m light [color]`: Sets the light state to on or off, + with a given dimmer value and optional color. +- `lwm2m mem`: (Only available if `DEVELHELP` is enabled in your Makefile) Prints the memory + usage of the LwM2M module. diff --git a/examples/lwm2m/lwm2m_cli.c b/examples/lwm2m/lwm2m_cli.c index 696a9b153e..b203084669 100644 --- a/examples/lwm2m/lwm2m_cli.c +++ b/examples/lwm2m/lwm2m_cli.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 HAW Hamburg + * Copyright (C) 2024 HAW Hamburg * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level @@ -11,23 +11,47 @@ * @{ * * @file - * @brief Wakaama LwM2M Client CLI support + * @brief Wakaama LwM2M Client example CLI support * * @author Leandro Lanzieri * @} */ -#include "kernel_defines.h" +#include "board.h" #include "lwm2m_client.h" #include "lwm2m_client_objects.h" #include "lwm2m_platform.h" +#include "objects/light_control.h" +#include "objects/common.h" -#define OBJ_COUNT (3) +#define LED_COLOR "FFFFFF" +#define LED_APP_TYPE "LED 0" +# define OBJ_COUNT (4) uint8_t connected = 0; lwm2m_object_t *obj_list[OBJ_COUNT]; lwm2m_client_data_t client_data; +void _light_cb(lwm2m_object_t *object, uint16_t instance_id, bool status, uint8_t dimmer, + const char* color, const char* app_type, void *arg) +{ + (void)object; + (void)instance_id; + (void)arg; + + printf("LED (%s) is now %s, with color %s and intensity %d%%\n", app_type, status? "ON":"OFF", + color, dimmer); + +#ifdef LED0_ON + if (status) { + LED0_ON; + } + else { + LED0_OFF; + } +#endif +} + void lwm2m_cli_init(void) { /* this call is needed before creating any objects */ @@ -37,12 +61,56 @@ void lwm2m_cli_init(void) obj_list[0] = lwm2m_client_get_security_object(&client_data); obj_list[1] = lwm2m_client_get_server_object(&client_data); obj_list[2] = lwm2m_client_get_device_object(&client_data); + obj_list[3] = lwm2m_object_light_control_init(&client_data); + + lwm2m_obj_light_control_args_t args = { + .cb = _light_cb, + .cb_arg = NULL, + .color = LED_COLOR, + .color_len = sizeof(LED_COLOR) - 1, + .app_type = LED_APP_TYPE, + .app_type_len = sizeof(LED_APP_TYPE) - 1 + }; + + int res = lwm2m_object_light_control_instance_create(&args, 0); + + if (res < 0) { + puts("Error instantiating light control"); + } if (!obj_list[0] || !obj_list[1] || !obj_list[2]) { puts("Could not create mandatory objects"); } } +static int _parse_lwm2m_light_cmd(int argc, char **argv) +{ + if (argc < 4) { + printf("usage: %s light [color]\n", argv[0]); + return 1; + } + + if (!connected) { + puts("LwM2M client not connected"); + return 1; + } + + bool status = !strcmp(argv[2], "on"); + uint8_t dimmer = atoi(argv[3]); + + if (argc > 4) { + char* color = argv[4]; + lwm2m_object_light_control_update_color(0, color, strlen(color), false); + } + + lwm2m_object_light_control_update_status(0, status, false); + + /* call the callback now to actually update the light */ + lwm2m_object_light_control_update_dimmer(0, dimmer, true); + + return 0; +} + int lwm2m_cli_cmd(int argc, char **argv) { if (argc == 1) { @@ -57,17 +125,21 @@ int lwm2m_cli_cmd(int argc, char **argv) return 0; } - if (IS_ACTIVE(DEVELHELP) && !strcmp(argv[1],"mem")) { + if (IS_ACTIVE(DEVELHELP) && !strcmp(argv[1], "mem")) { lwm2m_tlsf_status(); return 0; } + if (!strcmp(argv[1], "light")) { + return _parse_lwm2m_light_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;