1 /*
2 * Copyright (c) 2017 Linaro Limited
3 * Copyright (c) 2019 Foundries.io
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 /*
9 * Source material for IPSO Light Control object (3311):
10 * https://github.com/IPSO-Alliance/pub/blob/master/docs/IPSO-Smart-Objects.pdf
11 * Section: "16. IPSO Object: Light Control"
12 */
13
14 #define LOG_MODULE_NAME net_ipso_light_control
15 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
19
20 #include <stdint.h>
21 #include <zephyr/init.h>
22
23 #include "lwm2m_object.h"
24 #include "lwm2m_engine.h"
25 #include "lwm2m_resource_ids.h"
26
27 #define LIGHT_VERSION_MAJOR 1
28 #define LIGHT_VERSION_MINOR 0
29
30 #define LIGHT_MAX_ID 8
31
32 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_LIGHT_CONTROL_INSTANCE_COUNT
33
34 #define LIGHT_STRING_SHORT 8
35 #define LIGHT_STRING_LONG 64
36
37 /*
38 * Calculate resource instances as follows:
39 * start with LIGHT_MAX_ID
40 */
41 #define RESOURCE_INSTANCE_COUNT (LIGHT_MAX_ID)
42
43 /* resource state variables */
44 static bool on_off_value[MAX_INSTANCE_COUNT];
45 static uint8_t dimmer_value[MAX_INSTANCE_COUNT];
46 static int32_t on_time_value[MAX_INSTANCE_COUNT];
47 static uint32_t on_time_offset[MAX_INSTANCE_COUNT];
48 static double cumulative_active_value[MAX_INSTANCE_COUNT];
49 static double power_factor_value[MAX_INSTANCE_COUNT];
50 static char colour[MAX_INSTANCE_COUNT][LIGHT_STRING_LONG];
51 static char units[MAX_INSTANCE_COUNT][LIGHT_STRING_SHORT];
52
53 static struct lwm2m_engine_obj light_control;
54 static struct lwm2m_engine_obj_field fields[] = {
55 OBJ_FIELD_DATA(ON_OFF_RID, RW, BOOL),
56 OBJ_FIELD_DATA(DIMMER_RID, RW_OPT, U8),
57 OBJ_FIELD_DATA(ON_TIME_RID, RW_OPT, S32),
58 OBJ_FIELD_DATA(CUMULATIVE_ACTIVE_POWER_RID, R_OPT, FLOAT),
59 OBJ_FIELD_DATA(POWER_FACTOR_RID, R_OPT, FLOAT),
60 OBJ_FIELD_DATA(COLOUR_RID, RW_OPT, STRING),
61 OBJ_FIELD_DATA(SENSOR_UNITS_RID, R_OPT, STRING),
62 OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
63 };
64
65 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
66 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][LIGHT_MAX_ID];
67 static struct lwm2m_engine_res_inst
68 res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
69
on_time_read_cb(uint16_t obj_inst_id,uint16_t res_id,uint16_t res_inst_id,size_t * data_len)70 static void *on_time_read_cb(uint16_t obj_inst_id, uint16_t res_id, uint16_t res_inst_id,
71 size_t *data_len)
72 {
73 int i;
74
75 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
76 if (!inst[i].obj || inst[i].obj_inst_id != obj_inst_id) {
77 continue;
78 }
79
80 if (on_off_value[i]) {
81 on_time_value[i] = k_uptime_seconds() - on_time_offset[i];
82 }
83
84 *data_len = sizeof(on_time_value[i]);
85 return &on_time_value[i];
86 }
87
88 return NULL;
89 }
90
on_time_post_write_cb(uint16_t obj_inst_id,uint16_t res_id,uint16_t res_inst_id,uint8_t * data,uint16_t data_len,bool last_block,size_t total_size,size_t offset)91 static int on_time_post_write_cb(uint16_t obj_inst_id, uint16_t res_id,
92 uint16_t res_inst_id, uint8_t *data,
93 uint16_t data_len, bool last_block,
94 size_t total_size, size_t offset)
95 {
96 int i;
97
98 if (data_len != 4U) {
99 LOG_ERR("unknown size %u", data_len);
100 return -EINVAL;
101 }
102
103 int32_t counter = *(int32_t *) data;
104
105 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
106 if (!inst[i].obj || inst[i].obj_inst_id != obj_inst_id) {
107 continue;
108 }
109
110 if (counter == 0) {
111 on_time_offset[i] = k_uptime_seconds();
112 }
113
114 return 0;
115 }
116
117 return -ENOENT;
118 }
119
light_control_create(uint16_t obj_inst_id)120 static struct lwm2m_engine_obj_inst *light_control_create(uint16_t obj_inst_id)
121 {
122 int index, avail = -1, i = 0, j = 0;
123
124 /* Check that there is no other instance with this ID */
125 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
126 if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
127 LOG_ERR("Can not create instance - "
128 "already existing: %u", obj_inst_id);
129 return NULL;
130 }
131
132 /* Save first available slot index */
133 if (avail < 0 && !inst[index].obj) {
134 avail = index;
135 }
136 }
137
138 if (avail < 0) {
139 LOG_ERR("Can not create instance - no more room: %u",
140 obj_inst_id);
141 return NULL;
142 }
143
144 /* Set default values */
145 on_off_value[avail] = false;
146 dimmer_value[avail] = 0U;
147 on_time_value[avail] = 0;
148 on_time_offset[avail] = 0U;
149 cumulative_active_value[avail] = 0;
150 power_factor_value[avail] = 0;
151 colour[avail][0] = '\0';
152 units[avail][0] = '\0';
153
154 (void)memset(res[avail], 0,
155 sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
156 init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
157
158 /* initialize instance resource data */
159 INIT_OBJ_RES_DATA(ON_OFF_RID, res[avail], i, res_inst[avail], j,
160 &on_off_value[avail], sizeof(*on_off_value));
161 INIT_OBJ_RES_DATA(DIMMER_RID, res[avail], i, res_inst[avail], j,
162 &dimmer_value[avail], sizeof(*dimmer_value));
163 INIT_OBJ_RES(ON_TIME_RID, res[avail], i, res_inst[avail], j, 1, false,
164 true, &on_time_value[avail], sizeof(*on_time_value),
165 on_time_read_cb, NULL, NULL, on_time_post_write_cb, NULL);
166 INIT_OBJ_RES_DATA(CUMULATIVE_ACTIVE_POWER_RID, res[avail], i,
167 res_inst[avail], j, &cumulative_active_value[avail],
168 sizeof(*cumulative_active_value));
169 INIT_OBJ_RES_DATA(POWER_FACTOR_RID, res[avail], i, res_inst[avail], j,
170 &power_factor_value[avail],
171 sizeof(*power_factor_value));
172 INIT_OBJ_RES_DATA_LEN(COLOUR_RID, res[avail], i, res_inst[avail], j,
173 colour[avail], LIGHT_STRING_LONG, 0);
174 INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[avail], i, res_inst[avail], j,
175 units[avail], LIGHT_STRING_SHORT, 0);
176 INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[avail], i,
177 res_inst[avail], j);
178
179 inst[avail].resources = res[avail];
180 inst[avail].resource_count = i;
181
182 LOG_DBG("Create IPSO Light Control instance: %d", obj_inst_id);
183
184 return &inst[avail];
185 }
186
ipso_light_control_init(void)187 static int ipso_light_control_init(void)
188 {
189 light_control.obj_id = IPSO_OBJECT_LIGHT_CONTROL_ID;
190 light_control.version_major = LIGHT_VERSION_MAJOR;
191 light_control.version_minor = LIGHT_VERSION_MINOR;
192 light_control.is_core = false;
193 light_control.fields = fields;
194 light_control.field_count = ARRAY_SIZE(fields);
195 light_control.max_instance_count = MAX_INSTANCE_COUNT;
196 light_control.create_cb = light_control_create;
197 lwm2m_register_obj(&light_control);
198
199 return 0;
200 }
201
202 LWM2M_OBJ_INIT(ipso_light_control_init);
203