net: lwm2m: Added execute arguments support

A dedicated LwM2M execute callback type has been implemented which
supports execute arguments. The lwm2m engine, lwm2m_client sample and
lwm2m objects have been updated accordingly. Also the API change has
been documented, and the lwm2m engine reference has been updated.

Fixes #30551.

Signed-off-by: Maik Vermeulen <maik.vermeulen@innotractor.com>
This commit is contained in:
Maik Vermeulen 2020-12-10 10:17:14 +01:00 committed by Anas Nashif
commit 4cfd2a1943
14 changed files with 66 additions and 28 deletions

View file

@ -254,12 +254,12 @@ To use the LwM2M library, start by creating an LwM2M client context
/* LwM2M client context */
static struct lwm2m_ctx client;
Create callback functions for LwM2M resources that you wish to have actions
for:
Create callback functions for LwM2M resource exuctions:
.. code-block:: c
static int device_reboot_cb(uint16_t obj_inst_id)
static int device_reboot_cb(uint16_t obj_inst_id, uint8_t *args,
uint16_t args_len)
{
LOG_INF("Device rebooting.");
LOG_PANIC();

View file

@ -39,6 +39,11 @@ API Changes
``flags`` parameter, which allows to configure current LwM2M client session,
for instance enable bootstrap procedure in the curent session.
* LwM2M execute now supports arguments. The execute callback
`lwm2m_engine_execute_cb_t` is extended with an ``args`` parameter which points
to the CoAP payload that comprises the arguments, and an ``args_len`` parameter
to indicate the length of the ``args`` data.
* Changed vcnl4040 dts binding default for property 'proximity-trigger'.
Changed the default to match the HW POR state for this property.

View file

@ -186,10 +186,9 @@ typedef int (*lwm2m_engine_set_data_cb_t)(uint16_t obj_inst_id,
*
* Various object instance and resource-based events in the LwM2M engine
* can trigger a callback of this function type: object instance create,
* object instance delete and resource execute.
* and object instance delete.
*
* Register a function of this type via:
* lwm2m_engine_register_exec_callback()
* lwm2m_engine_register_create_callback()
* lwm2m_engine_register_delete_callback()
*
@ -200,6 +199,25 @@ typedef int (*lwm2m_engine_set_data_cb_t)(uint16_t obj_inst_id,
*/
typedef int (*lwm2m_engine_user_cb_t)(uint16_t obj_inst_id);
/**
* @brief Asynchronous execute notification callback.
*
* Resource executes trigger a callback of this type.
*
* Register a function of this type via:
* lwm2m_engine_register_exec_callback()
*
* @param[in] obj_inst_id Object instance ID generating the callback.
* @param[in] args Pointer to execute arguments payload. (This can be
* NULL if no arguments are provided)
* @param[in] args_len Length of argument payload in bytes.
*
* @return Callback returns a negative error code (errno.h) indicating
* reason of failure or 0 for success.
*/
typedef int (*lwm2m_engine_execute_cb_t)(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len);
/**
* @brief Power source types used for the "Available Power Sources" resource of
* the LwM2M Device object.
@ -308,14 +326,14 @@ lwm2m_engine_set_data_cb_t lwm2m_firmware_get_write_cb(void);
*
* @param[in] cb A callback function to receive the execute event.
*/
void lwm2m_firmware_set_update_cb(lwm2m_engine_user_cb_t cb);
void lwm2m_firmware_set_update_cb(lwm2m_engine_execute_cb_t cb);
/**
* @brief Get the event callback for firmware update execute events.
*
* @return A registered callback function to receive the execute event.
*/
lwm2m_engine_user_cb_t lwm2m_firmware_get_update_cb(void);
lwm2m_engine_execute_cb_t lwm2m_firmware_get_update_cb(void);
/**
* @brief Get the block context of the current firmware block.
@ -726,7 +744,7 @@ int lwm2m_engine_register_post_write_callback(char *pathstr,
* @return 0 for success or negative in case of error.
*/
int lwm2m_engine_register_exec_callback(char *pathstr,
lwm2m_engine_user_cb_t cb);
lwm2m_engine_execute_cb_t cb);
/**
* @brief Set object instance create event callback

View file

@ -144,7 +144,8 @@ static int init_led_device(void)
return 0;
}
static int device_reboot_cb(uint16_t obj_inst_id)
static int device_reboot_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
LOG_INF("DEVICE: REBOOT");
/* Add an error for testing */
@ -155,7 +156,8 @@ static int device_reboot_cb(uint16_t obj_inst_id)
return 0;
}
static int device_factory_default_cb(uint16_t obj_inst_id)
static int device_factory_default_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
LOG_INF("DEVICE: FACTORY DEFAULT");
/* Add an error for testing */
@ -167,7 +169,8 @@ static int device_factory_default_cb(uint16_t obj_inst_id)
}
#if defined(CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_SUPPORT)
static int firmware_update_cb(uint16_t obj_inst_id)
static int firmware_update_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
LOG_DBG("UPDATE");

View file

@ -98,7 +98,8 @@ static void update_max_measured(uint16_t obj_inst_id, int index)
NOTIFY_OBSERVER(IPSO_OBJECT_ID, obj_inst_id, MAX_MEASURED_VALUE_RID);
}
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id)
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
int i;

View file

@ -83,7 +83,8 @@ static void update_max_measured(uint16_t obj_inst_id, int index)
NOTIFY_OBSERVER(IPSO_OBJECT_ID, obj_inst_id, MAX_MEASURED_VALUE_RID);
}
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id)
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
int i;

View file

@ -83,7 +83,8 @@ static void update_max_measured(uint16_t obj_inst_id, int index)
NOTIFY_OBSERVER(IPSO_OBJECT_ID, obj_inst_id, MAX_MEASURED_VALUE_RID);
}
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id)
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
int i;

View file

@ -99,7 +99,8 @@ static void update_max_measured(uint16_t obj_inst_id, int index)
TEMP_MAX_MEASURED_VALUE_ID);
}
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id)
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
int i;

View file

@ -287,7 +287,8 @@ static void timer_work_cb(struct k_work *work)
stop_timer(timer, false);
}
static int timer_trigger_cb(uint16_t obj_inst_id)
static int timer_trigger_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
int i;

View file

@ -2065,7 +2065,7 @@ int lwm2m_engine_register_post_write_callback(char *pathstr,
}
int lwm2m_engine_register_exec_callback(char *pathstr,
lwm2m_engine_user_cb_t cb)
lwm2m_engine_execute_cb_t cb)
{
int ret;
struct lwm2m_engine_res *res = NULL;
@ -2839,6 +2839,8 @@ static int lwm2m_exec_handler(struct lwm2m_message *msg)
struct lwm2m_engine_obj_inst *obj_inst;
struct lwm2m_engine_res *res = NULL;
int ret;
uint8_t *args;
uint16_t args_len;
if (!msg) {
return -EINVAL;
@ -2849,8 +2851,10 @@ static int lwm2m_exec_handler(struct lwm2m_message *msg)
return ret;
}
args = (uint8_t *)coap_packet_get_payload(msg->in.in_cpkt, &args_len);
if (res->execute_cb) {
return res->execute_cb(obj_inst->obj_inst_id);
return res->execute_cb(obj_inst->obj_inst_id, args, args_len);
}
/* TODO: something else to handle for execute? */

View file

@ -128,7 +128,8 @@ static struct lwm2m_engine_res_inst *error_code_ri;
/* callbacks */
static int reset_error_list_cb(uint16_t obj_inst_id)
static int reset_error_list_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
int i;

View file

@ -68,7 +68,7 @@ static struct lwm2m_engine_res res[FIRMWARE_MAX_ID];
static struct lwm2m_engine_res_inst res_inst[RESOURCE_INSTANCE_COUNT];
static lwm2m_engine_set_data_cb_t write_cb;
static lwm2m_engine_user_cb_t update_cb;
static lwm2m_engine_execute_cb_t update_cb;
#ifdef CONFIG_LWM2M_FIRMWARE_UPDATE_PULL_SUPPORT
extern int lwm2m_firmware_start_transfer(char *package_uri);
@ -272,19 +272,20 @@ lwm2m_engine_set_data_cb_t lwm2m_firmware_get_write_cb(void)
return write_cb;
}
void lwm2m_firmware_set_update_cb(lwm2m_engine_user_cb_t cb)
void lwm2m_firmware_set_update_cb(lwm2m_engine_execute_cb_t cb)
{
update_cb = cb;
}
lwm2m_engine_user_cb_t lwm2m_firmware_get_update_cb(void)
lwm2m_engine_execute_cb_t lwm2m_firmware_get_update_cb(void)
{
return update_cb;
}
static int firmware_update_cb(uint16_t obj_inst_id)
static int firmware_update_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
lwm2m_engine_user_cb_t callback;
lwm2m_engine_execute_cb_t callback;
uint8_t state;
int ret;
@ -298,7 +299,7 @@ static int firmware_update_cb(uint16_t obj_inst_id)
callback = lwm2m_firmware_get_update_cb();
if (callback) {
ret = callback(obj_inst_id);
ret = callback(obj_inst_id, args, args_len);
if (ret < 0) {
LOG_ERR("Failed to update firmware: %d", ret);
lwm2m_firmware_set_update_result(

View file

@ -82,7 +82,7 @@ static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][SERVER_MAX_ID];
static struct lwm2m_engine_res_inst
res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
static int disable_cb(uint16_t obj_inst_id)
static int disable_cb(uint16_t obj_inst_id, uint8_t *args, uint16_t args_len)
{
int i;
@ -97,7 +97,8 @@ static int disable_cb(uint16_t obj_inst_id)
return -ENOENT;
}
static int update_trigger_cb(uint16_t obj_inst_id)
static int update_trigger_cb(uint16_t obj_inst_id,
uint8_t *args, uint16_t args_len)
{
#ifdef CONFIG_LWM2M_RD_CLIENT_SUPPORT
engine_trigger_update(false);

View file

@ -336,7 +336,7 @@ struct lwm2m_engine_res {
lwm2m_engine_get_data_cb_t read_cb;
lwm2m_engine_get_data_cb_t pre_write_cb;
lwm2m_engine_set_data_cb_t post_write_cb;
lwm2m_engine_user_cb_t execute_cb;
lwm2m_engine_execute_cb_t execute_cb;
struct lwm2m_engine_res_inst *res_instances;
uint16_t res_id;