From 3d3af7114eb0f6e477da8a62f4ef3360295bb6a0 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Mon, 29 Jul 2019 10:15:00 -0700 Subject: [PATCH] net: lwm2m: add IPSO Accelerometer object support This IPSO object can be used to represent a 1-3 axis accelerometer. Signed-off-by: Michael Scott --- include/net/lwm2m.h | 1 + subsys/net/lib/lwm2m/CMakeLists.txt | 3 + subsys/net/lib/lwm2m/Kconfig.ipso | 13 ++ subsys/net/lib/lwm2m/ipso_accelerometer.c | 140 ++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 subsys/net/lib/lwm2m/ipso_accelerometer.c diff --git a/include/net/lwm2m.h b/include/net/lwm2m.h index b5e4b404795598..d2cbc2c045f64e 100644 --- a/include/net/lwm2m.h +++ b/include/net/lwm2m.h @@ -51,6 +51,7 @@ #define IPSO_OBJECT_TEMP_SENSOR_ID 3303 #define IPSO_OBJECT_LIGHT_CONTROL_ID 3311 +#define IPSO_OBJECT_ACCELEROMETER_ID 3313 #define IPSO_OBJECT_BUZZER_ID 3338 #define IPSO_OBJECT_TIMER_ID 3340 #define IPSO_OBJECT_ONOFF_SWITCH_ID 3342 diff --git a/subsys/net/lib/lwm2m/CMakeLists.txt b/subsys/net/lib/lwm2m/CMakeLists.txt index 4a3fc9ae6576ea..ea864540318632 100644 --- a/subsys/net/lib/lwm2m/CMakeLists.txt +++ b/subsys/net/lib/lwm2m/CMakeLists.txt @@ -45,6 +45,9 @@ zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_TEMP_SENSOR zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_LIGHT_CONTROL ipso_light_control.c ) +zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_ACCELEROMETER + ipso_accelerometer.c + ) zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_BUZZER ipso_buzzer.c ) diff --git a/subsys/net/lib/lwm2m/Kconfig.ipso b/subsys/net/lib/lwm2m/Kconfig.ipso index e4c80f98a8f211..8975561f164b47 100644 --- a/subsys/net/lib/lwm2m/Kconfig.ipso +++ b/subsys/net/lib/lwm2m/Kconfig.ipso @@ -43,6 +43,19 @@ config LWM2M_IPSO_LIGHT_CONTROL_INSTANCE_COUNT This setting establishes the total count of IPSO Light Control instances available to the LWM2M client. +config LWM2M_IPSO_ACCELEROMETER + bool "IPSO Accelerometer Support" + help + This Object is used to used to represent a 1-3 axis accelerometer. + +config LWM2M_IPSO_ACCELEROMETER_INSTANCE_COUNT + int "Maximum # of IPSO Accelerometer object instances" + default 1 + depends on LWM2M_IPSO_ACCELEROMETER + help + This setting establishes the total count of IPSO Accelerometer + instances available to the LWM2M client. + config LWM2M_IPSO_BUZZER bool "IPSO Buzzer Support" help diff --git a/subsys/net/lib/lwm2m/ipso_accelerometer.c b/subsys/net/lib/lwm2m/ipso_accelerometer.c new file mode 100644 index 00000000000000..fcb44e846cc821 --- /dev/null +++ b/subsys/net/lib/lwm2m/ipso_accelerometer.c @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2019 Foundries.io + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/* + * Source material for IPSO Accelerometer object (3313): + * http://www.openmobilealliance.org/tech/profiles/lwm2m/3313.xml + */ + +#define LOG_MODULE_NAME net_ipso_accel +#define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL + +#include +LOG_MODULE_REGISTER(LOG_MODULE_NAME); + +#include +#include + +#include "lwm2m_object.h" +#include "lwm2m_engine.h" + +/* Server resource IDs */ +#define ACCEL_X_VALUE_ID 5702 +#define ACCEL_Y_VALUE_ID 5703 +#define ACCEL_Z_VALUE_ID 5704 +#define ACCEL_SENSOR_UNITS_ID 5701 +#define ACCEL_MIN_RANGE_VALUE_ID 5603 +#define ACCEL_MAX_RANGE_VALUE_ID 5604 + +#define ACCEL_MAX_ID 6 + +#define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_ACCELEROMETER_INSTANCE_COUNT + +/* + * Calculate resource instances as follows: + * start with ACCEL_MAX_ID + */ +#define RESOURCE_INSTANCE_COUNT (ACCEL_MAX_ID) + +/* resource state */ +struct ipso_accel_data { + float32_value_t x_value; + float32_value_t y_value; + float32_value_t z_value; + float32_value_t min_range; + float32_value_t max_range; +}; + +static struct ipso_accel_data accel_data[MAX_INSTANCE_COUNT]; + +static struct lwm2m_engine_obj accel; +static struct lwm2m_engine_obj_field fields[] = { + OBJ_FIELD_DATA(ACCEL_X_VALUE_ID, R, FLOAT32), + OBJ_FIELD_DATA(ACCEL_Y_VALUE_ID, R_OPT, FLOAT32), + OBJ_FIELD_DATA(ACCEL_Z_VALUE_ID, R_OPT, FLOAT32), + OBJ_FIELD_DATA(ACCEL_SENSOR_UNITS_ID, R_OPT, STRING), + OBJ_FIELD_DATA(ACCEL_MIN_RANGE_VALUE_ID, R_OPT, FLOAT32), + OBJ_FIELD_DATA(ACCEL_MAX_RANGE_VALUE_ID, R_OPT, FLOAT32), +}; + +static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT]; +static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][ACCEL_MAX_ID]; +static struct lwm2m_engine_res_inst + res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT]; + +static struct lwm2m_engine_obj_inst *accel_create(u16_t obj_inst_id) +{ + int index, avail = -1, i = 0, j = 0; + + /* Check that there is no other instance with this ID */ + for (index = 0; index < ARRAY_SIZE(inst); index++) { + if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) { + LOG_ERR("Can not create instance - " + "already existing: %u", obj_inst_id); + return NULL; + } + + /* Save first available slot index */ + if (avail < 0 && !inst[index].obj) { + avail = index; + } + } + + if (avail < 0) { + LOG_ERR("Can not create instance - no more room: %u", + obj_inst_id); + return NULL; + } + + /* Set default values */ + (void)memset(&accel_data[avail], 0, sizeof(accel_data[avail])); + + (void)memset(res[avail], 0, + sizeof(res[avail][0]) * ARRAY_SIZE(res[avail])); + init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail])); + + /* initialize instance resource data */ + INIT_OBJ_RES_DATA(ACCEL_X_VALUE_ID, res[avail], i, res_inst[avail], j, + &accel_data[avail].x_value, + sizeof(accel_data[avail].x_value)); + INIT_OBJ_RES_DATA(ACCEL_Y_VALUE_ID, res[avail], i, res_inst[avail], j, + &accel_data[avail].y_value, + sizeof(accel_data[avail].y_value)); + INIT_OBJ_RES_DATA(ACCEL_Z_VALUE_ID, res[avail], i, res_inst[avail], j, + &accel_data[avail].z_value, + sizeof(accel_data[avail].z_value)); + INIT_OBJ_RES_OPTDATA(ACCEL_SENSOR_UNITS_ID, res[avail], i, + res_inst[avail], j); + INIT_OBJ_RES_DATA(ACCEL_MIN_RANGE_VALUE_ID, res[avail], i, + res_inst[avail], j, + &accel_data[avail].min_range, + sizeof(accel_data[avail].min_range)); + INIT_OBJ_RES_DATA(ACCEL_MAX_RANGE_VALUE_ID, res[avail], i, + res_inst[avail], j, + &accel_data[avail].max_range, + sizeof(accel_data[avail].max_range)); + + inst[avail].resources = res[avail]; + inst[avail].resource_count = i; + + LOG_DBG("Create IPSO Accelerometer instance: %d", obj_inst_id); + + return &inst[avail]; +} + +static int ipso_accel_init(struct device *dev) +{ + accel.obj_id = IPSO_OBJECT_ACCELEROMETER_ID; + accel.fields = fields; + accel.field_count = ARRAY_SIZE(fields); + accel.max_instance_count = ARRAY_SIZE(inst); + accel.create_cb = accel_create; + lwm2m_register_obj(&accel); + + return 0; +} + +SYS_INIT(ipso_accel_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);