Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/wakaama: add on off switch #21109

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
return 0;
}

static int _parse_lwm2m_switch_cmd(int argc, char **argv)
{
if (argc < 3) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (argc < 3) {
if (argc != 3) {

Or, if the <on|off> is optional (in which case the follow up printf should be changed to [on|off] imo)

Suggested change
if (argc < 3) {
if ((argc != 3) && (argc != 2)) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

int lwm2m_cli_cmd(int argc, char **argv)
{
if (argc == 1) {
Expand All @@ -182,12 +214,16 @@
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
Loading
Loading