diff --git a/pkg/wakaama/Makefile.dep b/pkg/wakaama/Makefile.dep index 5d65ee5b5e..ae0f142ef4 100644 --- a/pkg/wakaama/Makefile.dep +++ b/pkg/wakaama/Makefile.dep @@ -25,6 +25,10 @@ ifneq (,$(filter wakaama_objects_humidity,$(USEMODULE))) USEMODULE += wakaama_objects_ipso_sensor_base endif +ifneq (,$(filter wakaama_objects_illuminance,$(USEMODULE))) + USEMODULE += wakaama_objects_ipso_sensor_base +endif + USEMODULE += ztimer USEMODULE += ztimer_sec USEPKG += tlsf diff --git a/pkg/wakaama/contrib/objects/Kconfig.ipso b/pkg/wakaama/contrib/objects/Kconfig.ipso index 2c979da6bf..24fb2b19e8 100644 --- a/pkg/wakaama/contrib/objects/Kconfig.ipso +++ b/pkg/wakaama/contrib/objects/Kconfig.ipso @@ -32,4 +32,13 @@ config LWM2M_HUMIDITY_INSTANCES_MAX endmenu +menu "IPSO illuminance object" + depends on USEMODULE_WAKAAMA_OBJECTS_ILLUMINANCE + +config LWM2M_ILLUMINANCE_INSTANCES_MAX + int "Maximum number of illuminance object instances" + default 1 + +endmenu + endmenu # "IPSO objects" diff --git a/pkg/wakaama/contrib/objects/illuminance.c b/pkg/wakaama/contrib/objects/illuminance.c new file mode 100644 index 0000000000..7c56b97d99 --- /dev/null +++ b/pkg/wakaama/contrib/objects/illuminance.c @@ -0,0 +1,70 @@ +/* + * 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 + * directory for more details. + */ +/** + * @{ + * @ingroup lwm2m_objects_illuminance + * + * @file + * @brief Illuminance Sensor object implementation for LwM2M client using Wakaama + * + * @author Leandro Lanzieri + * @} + */ + +#include "mutex.h" +#include "liblwm2m.h" +#include "lwm2m_client.h" +#include "objects/illuminance.h" +#include "objects/ipso_sensor_base.h" + +#define ENABLE_DEBUG 0 +#include "debug.h" + +/** + * @brief Illuminance Sensor object implementation descriptor. + */ +static lwm2m_obj_ipso_sensor_base_t _illuminance_object; + +/** + * @brief Pool of object instances. + */ +static lwm2m_obj_ipso_sensor_base_inst_t _instances[CONFIG_LWM2M_ILLUMINANCE_INSTANCES_MAX]; + +lwm2m_object_t *lwm2m_object_illuminance_init(lwm2m_client_data_t *client_data) +{ + assert(client_data); + int res = lwm2m_object_ipso_sensor_base_init_derived(client_data, &_illuminance_object, + LWM2M_ILLUMINANCE_OBJECT_ID, + _instances, + CONFIG_LWM2M_ILLUMINANCE_INSTANCES_MAX); + + if (res) { + DEBUG("[lwm2m:illuminance]: failed to create object\n"); + return NULL; + } + + return &_illuminance_object.object; +} + +int32_t lwm2m_object_illuminance_instance_create(const lwm2m_obj_illuminance_args_t *args) +{ + int32_t result = lwm2m_object_ipso_sensor_base_instance_create(&_illuminance_object, args); + + if (result) { + DEBUG("[lwm2m:illuminance]: failed to create instance\n"); + } + + return result; +} + +void lwm2m_object_illuminance_update_value(const lwm2m_client_data_t *client_data, + uint16_t instance_id, int16_t value) +{ + lwm2m_object_ipso_sensor_base_update_value(client_data, &_illuminance_object, instance_id, + value); +} diff --git a/pkg/wakaama/include/objects/illuminance.h b/pkg/wakaama/include/objects/illuminance.h new file mode 100644 index 0000000000..665ed16b14 --- /dev/null +++ b/pkg/wakaama/include/objects/illuminance.h @@ -0,0 +1,158 @@ +/* + * 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 + * directory for more details. + */ + +/** + * @ingroup lwm2m_objects + * @defgroup lwm2m_objects_illuminance Illuminance + * @brief Illuminance Sensor object implementation for LwM2M client using Wakaama + * + * @experimental + * + * This implements the LwM2M Illuminance Sensor object (ID 3301) as specified in the LwM2M registry. + * + * The sensor value can be updated by the application using the + * @ref lwm2m_object_illuminance_update_value function, or polled when required if a callback is + * registered upon object instantiation via @ref lwm2m_obj_illuminance_args_t::read_cb. + * + * To use this object add `USEMODULE += wakaama_objects_illuminance` to the application Makefile. + * + * ## Resources + * + * For an XML description of the object see + * https://raw.githubusercontent.com/OpenMobileAlliance/lwm2m-registry/prod/version_history/3301-1_0.xml + * + * This object is based on the @ref lwm2m_objects_ipso_sensor_base, therefore it shares the same + * resources. + * + * ## Usage + * + * 1. Initialize the LwM2M object with an initialized client pointer. + * + * ~~~~~~~~~~~~~~~{.c} + * lwm2m_object_t *obj = lwm2m_object_illuminance_init(&client_data); + * ~~~~~~~~~~~~~~~ + * + * 2. Create a new instance of the object with a given configuration (@ref lwm2m_obj_illuminance_args_t). + * Here, you can decide the way of updating the sensor values: polling or pushing. In this case, + * we register a callback function that is called whenever the sensor value is read. + * + * ~~~~~~~~~~~~~~~{.c} + * + * // callback function to read the sensor value + * int _read_cb(void *arg, int16_t *value) + * { + * (void)arg; + * + * // generate new value + * *value = 100; + * + * return 0; + * } + * + * // initialize an instance of illuminance sensor object + * const lwm2m_obj_illuminance_args_t illuminance_args = { + * .min_range_value = 0.0, + * .max_range_value = 4000.0, + * .units = "lx", + * .units_len = sizeof("lx") - 1, + * .instance_id = 0, + * .read_cb = _read_cb, + * .read_cb_arg = NULL + * }; + * + * int32_t res = lwm2m_object_illuminance_instance_create(&illuminance_args); + * + * if (res < 0) { + * puts("Could not create illuminance object instance"); + * } + * ~~~~~~~~~~~~~~~ + * + * 3. You can now update the sensor values using the @ref lwm2m_object_illuminance_update_value function. + * + * ~~~~~~~~~~~~~~~{.c} + * uint16_t instance_id = (uint16_t)res; + * lwm2m_object_illuminance_update_value(&client_data, instance_id, new_value); + * ~~~~~~~~~~~~~~~ + * @{ + * + * @file + * + * @author Leandro Lanzieri + */ + +#ifndef OBJECTS_ILLUMINANCE_H +#define OBJECTS_ILLUMINANCE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "liblwm2m.h" +#include "lwm2m_client.h" +#include "objects/ipso_sensor_base.h" + +/** + * @defgroup lwm2m_objects_illuminance_config LwM2M Illuminance Sensor object compile configurations + * @ingroup lwm2m_client_config + * @{ + */ +/** + * @brief Maximum number of instances of the object + */ +#ifndef CONFIG_LWM2M_ILLUMINANCE_INSTANCES_MAX +#define CONFIG_LWM2M_ILLUMINANCE_INSTANCES_MAX (1U) +#endif +/** @} */ + +/** + * @brief LwM2M Illuminance Sensor object ID + */ +#define LWM2M_ILLUMINANCE_OBJECT_ID 3301 + +/** + * @brief Arguments for the creation of a Illuminance Sensor object instance. + */ +typedef lwm2m_obj_ipso_base_sensor_args_t lwm2m_obj_illuminance_args_t; + +/** + * @brief Initialize the Illuminance Sensor object handle + * + * @param[in] client_data Pointer to the LwM2M client data. + * + * @return Pointer to the global handle of the Illuminance Sensor object. + */ +lwm2m_object_t *lwm2m_object_illuminance_init(lwm2m_client_data_t *client_data); + +/** + * @brief Create a new Illuminance Sensor instance. + * + * @param[in] args Initialize structure with the parameter for the instance. Must + * not be NULL. + * + * @return > 0 value representing the instance ID if the instance was created successfully. + * @return <0 otherwise + */ +int32_t lwm2m_object_illuminance_instance_create(const lwm2m_obj_illuminance_args_t *args); + +/** + * @brief Update the value of the illuminance sensor and trigger a notification + * to the observing servers, if any. + * + * @param[in] client_data Pointer to the LwM2M client. + * @param[in] instance_id ID of the instance to update. + * @param[in] value New value for the sensor. + */ +void lwm2m_object_illuminance_update_value(const lwm2m_client_data_t *client_data, + uint16_t instance_id, int16_t value); + +#ifdef __cplusplus +} +#endif + +#endif /* OBJECTS_ILLUMINANCE_H */ +/** @} */