1 /*
2  * Copyright (c) 2019 Foundries.io
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * Source material for IPSO Accelerometer object (3313):
9  * http://www.openmobilealliance.org/tech/profiles/lwm2m/3313.xml
10  */
11 
12 #define LOG_MODULE_NAME net_ipso_accel
13 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
14 
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
17 
18 #include <stdint.h>
19 #include <zephyr/init.h>
20 
21 #include "lwm2m_object.h"
22 #include "lwm2m_engine.h"
23 #include "lwm2m_resource_ids.h"
24 
25 #define ACCEL_VERSION_MAJOR 1
26 
27 #if defined(CONFIG_LWM2M_IPSO_ACCELEROMETER_VERSION_1_1)
28 #define ACCEL_VERSION_MINOR 1
29 #define ACCEL_MAX_ID 11
30 #else
31 #define ACCEL_VERSION_MINOR 0
32 #define ACCEL_MAX_ID 6
33 #endif /* defined(CONFIG_LWM2M_IPSO_ACCELEROMETER_VERSION_1_1) */
34 
35 #define MAX_INSTANCE_COUNT	CONFIG_LWM2M_IPSO_ACCELEROMETER_INSTANCE_COUNT
36 
37 /*
38  * Calculate resource instances as follows:
39  * start with ACCEL_MAX_ID
40  */
41 #define RESOURCE_INSTANCE_COUNT        (ACCEL_MAX_ID)
42 
43 /* resource state */
44 struct ipso_accel_data {
45 	double x_value;
46 	double y_value;
47 	double z_value;
48 	double min_range;
49 	double max_range;
50 };
51 
52 static struct ipso_accel_data accel_data[MAX_INSTANCE_COUNT];
53 
54 static struct lwm2m_engine_obj accel;
55 static struct lwm2m_engine_obj_field fields[] = {
56 	OBJ_FIELD_DATA(X_VALUE_RID, R, FLOAT),
57 	OBJ_FIELD_DATA(Y_VALUE_RID, R_OPT, FLOAT),
58 	OBJ_FIELD_DATA(Z_VALUE_RID, R_OPT, FLOAT),
59 	OBJ_FIELD_DATA(SENSOR_UNITS_RID, R_OPT, STRING),
60 	OBJ_FIELD_DATA(MIN_RANGE_VALUE_RID, R_OPT, FLOAT),
61 	OBJ_FIELD_DATA(MAX_RANGE_VALUE_RID, R_OPT, FLOAT),
62 #if defined(CONFIG_LWM2M_IPSO_ACCELEROMETER_VERSION_1_1)
63 	OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
64 	OBJ_FIELD_DATA(TIMESTAMP_RID, R_OPT, TIME),
65 	OBJ_FIELD_DATA(FRACTIONAL_TIMESTAMP_RID, R_OPT, FLOAT),
66 	OBJ_FIELD_DATA(MEASUREMENT_QUALITY_INDICATOR_RID, R_OPT, U8),
67 	OBJ_FIELD_DATA(MEASUREMENT_QUALITY_LEVEL_RID, R_OPT, U8),
68 #endif
69 };
70 
71 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
72 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][ACCEL_MAX_ID];
73 static struct lwm2m_engine_res_inst
74 		res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
75 
accel_create(uint16_t obj_inst_id)76 static struct lwm2m_engine_obj_inst *accel_create(uint16_t obj_inst_id)
77 {
78 	int index, avail = -1, i = 0, j = 0;
79 
80 	/* Check that there is no other instance with this ID */
81 	for (index = 0; index < ARRAY_SIZE(inst); index++) {
82 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
83 			LOG_ERR("Can not create instance - "
84 				"already existing: %u", obj_inst_id);
85 			return NULL;
86 		}
87 
88 		/* Save first available slot index */
89 		if (avail < 0 && !inst[index].obj) {
90 			avail = index;
91 		}
92 	}
93 
94 	if (avail < 0) {
95 		LOG_ERR("Can not create instance - no more room: %u",
96 			obj_inst_id);
97 		return NULL;
98 	}
99 
100 	/* Set default values */
101 	(void)memset(&accel_data[avail], 0, sizeof(accel_data[avail]));
102 
103 	(void)memset(res[avail], 0,
104 		     sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
105 	init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
106 
107 	/* initialize instance resource data */
108 	INIT_OBJ_RES_DATA(X_VALUE_RID, res[avail], i, res_inst[avail], j,
109 			  &accel_data[avail].x_value,
110 			  sizeof(accel_data[avail].x_value));
111 	INIT_OBJ_RES_DATA(Y_VALUE_RID, res[avail], i, res_inst[avail], j,
112 			  &accel_data[avail].y_value,
113 			  sizeof(accel_data[avail].y_value));
114 	INIT_OBJ_RES_DATA(Z_VALUE_RID, res[avail], i, res_inst[avail], j,
115 			  &accel_data[avail].z_value,
116 			  sizeof(accel_data[avail].z_value));
117 	INIT_OBJ_RES_OPTDATA(SENSOR_UNITS_RID, res[avail], i,
118 			     res_inst[avail], j);
119 	INIT_OBJ_RES_DATA(MIN_RANGE_VALUE_RID, res[avail], i, res_inst[avail],
120 			  j, &accel_data[avail].min_range,
121 			  sizeof(accel_data[avail].min_range));
122 	INIT_OBJ_RES_DATA(MAX_RANGE_VALUE_RID, res[avail], i, res_inst[avail],
123 			  j, &accel_data[avail].max_range,
124 			  sizeof(accel_data[avail].max_range));
125 #if defined(CONFIG_LWM2M_IPSO_ACCELEROMETER_VERSION_1_1)
126 	INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[avail], i,
127 			     res_inst[avail], j);
128 	INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[avail], i, res_inst[avail], j);
129 	INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[avail], i,
130 			     res_inst[avail], j);
131 	INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_INDICATOR_RID, res[avail],
132 			     i, res_inst[avail], j);
133 	INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_LEVEL_RID, res[avail], i,
134 			     res_inst[avail], j);
135 #endif
136 
137 	inst[avail].resources = res[avail];
138 	inst[avail].resource_count = i;
139 
140 	LOG_DBG("Create IPSO Accelerometer instance: %d", obj_inst_id);
141 
142 	return &inst[avail];
143 }
144 
ipso_accel_init(void)145 static int ipso_accel_init(void)
146 {
147 	accel.obj_id = IPSO_OBJECT_ACCELEROMETER_ID;
148 	accel.version_major = ACCEL_VERSION_MAJOR;
149 	accel.version_minor = ACCEL_VERSION_MINOR;
150 	accel.is_core = false;
151 	accel.fields = fields;
152 	accel.field_count = ARRAY_SIZE(fields);
153 	accel.max_instance_count = ARRAY_SIZE(inst);
154 	accel.create_cb = accel_create;
155 	lwm2m_register_obj(&accel);
156 
157 	return 0;
158 }
159 
160 LWM2M_OBJ_INIT(ipso_accel_init);
161