mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-14 17:13:50 +01:00
examples/lwm2m: add on/off switch object
This commit is contained in:
parent
04d25590c8
commit
a57d5cc024
@ -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
|
||||
|
||||
@ -28,19 +28,20 @@
|
||||
#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];
|
||||
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)
|
||||
const char *color, const char *app_type, void *arg)
|
||||
{
|
||||
(void)object;
|
||||
(void)instance_id;
|
||||
@ -69,6 +70,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 = {
|
||||
@ -85,6 +87,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,
|
||||
@ -133,10 +146,17 @@ void lwm2m_cli_init(void)
|
||||
}
|
||||
}
|
||||
|
||||
static void _print_usage_lwm2m_light_cmd(const char *cmd)
|
||||
{
|
||||
assert(cmd);
|
||||
printf("usage: %s light <on|off> <dimmer> [color]\n", cmd);
|
||||
}
|
||||
|
||||
static int _parse_lwm2m_light_cmd(int argc, char **argv)
|
||||
{
|
||||
if (argc < 4) {
|
||||
printf("usage: %s light <on|off> <dimmer> [color]\n", argv[0]);
|
||||
if (argc < 4 || argc > 5) {
|
||||
printf("Error: invalid number of arguments\n");
|
||||
_print_usage_lwm2m_light_cmd(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -145,11 +165,23 @@ static int _parse_lwm2m_light_cmd(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool status = !strcmp(argv[2], "on");
|
||||
bool status;
|
||||
if (!strcmp(argv[2], "on")) {
|
||||
status = true;
|
||||
}
|
||||
else if (!strcmp(argv[2], "off")) {
|
||||
status = false;
|
||||
}
|
||||
else {
|
||||
printf("Error: light status can only be 'on' or 'off'\n");
|
||||
_print_usage_lwm2m_light_cmd(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t dimmer = atoi(argv[3]);
|
||||
|
||||
if (argc > 4) {
|
||||
char* color = argv[4];
|
||||
char *color = argv[4];
|
||||
lwm2m_object_light_control_update_color(0, color, strlen(color), false);
|
||||
}
|
||||
|
||||
@ -161,6 +193,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 <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;
|
||||
}
|
||||
|
||||
|
||||
static int _cli_cmd(int argc, char **argv)
|
||||
{
|
||||
if (argc == 1) {
|
||||
@ -184,12 +235,16 @@ static int _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 <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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user