Skip to content

Commit

Permalink
examples/wakaama: use light control object when LED0 is present
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrolanzieri committed Feb 1, 2024
1 parent 6161302 commit 414226c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/wakaama/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
8 changes: 6 additions & 2 deletions examples/wakaama/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 Down Expand Up @@ -111,3 +111,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: 81 additions & 5 deletions examples/wakaama/lwm2m_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,41 @@
* @}
*/

#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 @@ -38,11 +62,59 @@ void lwm2m_cli_init(void)
obj_list[1] = lwm2m_client_get_server_object(&client_data);
obj_list[2] = lwm2m_client_get_device_object(&client_data);

#ifdef LED0_ON
obj_list[3] = lwm2m_object_light_control_get();
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(obj_list[3], 0, &args);

if (res < 0) {
puts("Error instantiating light control");
}
#endif

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;
}

bool status = !strcmp(argv[2], "on");
uint8_t dimmer = atoi(argv[3]);

lwm2m_uri_t uri;
uri.flag = LWM2M_URI_FLAG_OBJECT_ID | LWM2M_URI_FLAG_INSTANCE_ID | LWM2M_URI_FLAG_RESOURCE_ID;
uri.objectId = LWM2M_LIGHT_CONTROL_OBJECT_ID;
uri.instanceId = 0;

uri.resourceId = LWM2M_LIGHT_CONTROL_ON_OFF_ID;
lwm2m_set_bool(&client_data, &uri, status);

uri.resourceId = LWM2M_LIGHT_CONTROL_DIMMER_ID;
lwm2m_set_int(&client_data, &uri, dimmer);

if (argc > 4) {
char* color = argv[4];
uri.resourceId = LWM2M_LIGHT_CONTROL_COLOUR_ID;
lwm2m_set_string(&client_data, &uri, color, strlen(color));
}

return 0;
}

int lwm2m_cli_cmd(int argc, char **argv)
{
if (argc == 1) {
Expand All @@ -57,17 +129,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

0 comments on commit 414226c

Please sign in to comment.