Skip to content

Commit

Permalink
Merge pull request #16204 from leandrolanzieri/pr/pkg/wakaama_add_obj…
Browse files Browse the repository at this point in the history
…_light_ctrl

pkg/wakaama: add Light Control object implementation
  • Loading branch information
benpicco authored Mar 13, 2024
2 parents 102adbb + ea2440f commit d3ec7cf
Show file tree
Hide file tree
Showing 8 changed files with 1,049 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/lwm2m/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 16 additions & 5 deletions examples/lwm2m/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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`:
`lwm2m.h` as `CONFIG_LWM2M_BSSERVER_PORT`.

Download the jar file:
```shell
# download demo
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
Expand Down Expand Up @@ -111,3 +118,7 @@ BOARD=<board> make clean all flash term
#### Shell commands
- `lwm2m start`: Starts the LwM2M by configuring the module and registering to
the server.
- `lwm2m light <on|off> <dimmer> [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.
86 changes: 79 additions & 7 deletions examples/lwm2m/lwm2m_cli.c
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,23 +11,47 @@
* @{
*
* @file
* @brief Wakaama LwM2M Client CLI support
* @brief Wakaama LwM2M Client example CLI support
*
* @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de>
* @}
*/

#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 */
Expand All @@ -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 <on|off> <dimmer> [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) {
Expand All @@ -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 <start|mem>\n", argv[0]);
printf("usage: %s <start|mem|light>\n", argv[0]);
}
else {
printf("usage: %s <start>\n", argv[0]);
printf("usage: %s <start|light>\n", argv[0]);
}

return 1;
Expand Down
2 changes: 2 additions & 0 deletions pkg/wakaama/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ config LWM2M_TLSF_BUFFER
int "Allocation buffer size"
default 5120

rsource "contrib/objects/Kconfig"

endif # KCONFIG_USEPKG_WAKAAMA
8 changes: 8 additions & 0 deletions pkg/wakaama/contrib/objects/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2021 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
# directory for more details.
#

rsource "Kconfig.light_control"
22 changes: 22 additions & 0 deletions pkg/wakaama/contrib/objects/Kconfig.light_control
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2021 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
# directory for more details.
#

menu "Light Control object"

config LWM2M_LIGHT_INSTANCES_MAX
int "Maximum number of Light Control object instances"
default 3

config LWM2M_LIGHT_CONTROL_COLOR_MAX_SIZE
int "Maximum size of a Light Control color string"
default 16

config LWM2M_LIGHT_CONTROL_APP_TYPE_MAX_SIZE
int "Maximum size of a Light Control application type string"
default 16

endmenu # Light Control object
Loading

0 comments on commit d3ec7cf

Please sign in to comment.