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_get() / MSEC_PER_SEC) -
82 on_time_offset[i];
83 }
84
85 *data_len = sizeof(on_time_value[i]);
86 return &on_time_value[i];
87 }
88
89 return NULL;
90 }
91
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)92 static int on_time_post_write_cb(uint16_t obj_inst_id,
93 uint16_t res_id, uint16_t res_inst_id,
94 uint8_t *data, uint16_t data_len,
95 bool last_block, size_t total_size)
96 {
97 int i;
98
99 if (data_len != 4U) {
100 LOG_ERR("unknown size %u", data_len);
101 return -EINVAL;
102 }
103
104 int32_t counter = *(int32_t *) data;
105
106 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
107 if (!inst[i].obj || inst[i].obj_inst_id != obj_inst_id) {
108 continue;
109 }
110
111 if (counter == 0) {
112 on_time_offset[i] =
113 (int32_t)(k_uptime_get() / MSEC_PER_SEC);
114 }
115
116 return 0;
117 }
118
119 return -ENOENT;
120 }
121
light_control_create(uint16_t obj_inst_id)122 static struct lwm2m_engine_obj_inst *light_control_create(uint16_t obj_inst_id)
123 {
124 int index, avail = -1, i = 0, j = 0;
125
126 /* Check that there is no other instance with this ID */
127 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
128 if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
129 LOG_ERR("Can not create instance - "
130 "already existing: %u", obj_inst_id);
131 return NULL;
132 }
133
134 /* Save first available slot index */
135 if (avail < 0 && !inst[index].obj) {
136 avail = index;
137 }
138 }
139
140 if (avail < 0) {
141 LOG_ERR("Can not create instance - no more room: %u",
142 obj_inst_id);
143 return NULL;
144 }
145
146 /* Set default values */
147 on_off_value[avail] = false;
148 dimmer_value[avail] = 0U;
149 on_time_value[avail] = 0;
150 on_time_offset[avail] = 0U;
151 cumulative_active_value[avail] = 0;
152 power_factor_value[avail] = 0;
153 colour[avail][0] = '\0';
154 units[avail][0] = '\0';
155
156 (void)memset(res[avail], 0,
157 sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
158 init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
159
160 /* initialize instance resource data */
161 INIT_OBJ_RES_DATA(ON_OFF_RID, res[avail], i, res_inst[avail], j,
162 &on_off_value[avail], sizeof(*on_off_value));
163 INIT_OBJ_RES_DATA(DIMMER_RID, res[avail], i, res_inst[avail], j,
164 &dimmer_value[avail], sizeof(*dimmer_value));
165 INIT_OBJ_RES(ON_TIME_RID, res[avail], i, res_inst[avail], j, 1, false,
166 true, &on_time_value[avail], sizeof(*on_time_value),
167 on_time_read_cb, NULL, NULL, on_time_post_write_cb, NULL);
168 INIT_OBJ_RES_DATA(CUMULATIVE_ACTIVE_POWER_RID, res[avail], i,
169 res_inst[avail], j, &cumulative_active_value[avail],
170 sizeof(*cumulative_active_value));
171 INIT_OBJ_RES_DATA(POWER_FACTOR_RID, res[avail], i, res_inst[avail], j,
172 &power_factor_value[avail],
173 sizeof(*power_factor_value));
174 INIT_OBJ_RES_DATA_LEN(COLOUR_RID, res[avail], i, res_inst[avail], j,
175 colour[avail], LIGHT_STRING_LONG, 0);
176 INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[avail], i, res_inst[avail], j,
177 units[avail], LIGHT_STRING_SHORT, 0);
178 INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[avail], i,
179 res_inst[avail], j);
180
181 inst[avail].resources = res[avail];
182 inst[avail].resource_count = i;
183
184 LOG_DBG("Create IPSO Light Control instance: %d", obj_inst_id);
185
186 return &inst[avail];
187 }
188
ipso_light_control_init(void)189 static int ipso_light_control_init(void)
190 {
191 light_control.obj_id = IPSO_OBJECT_LIGHT_CONTROL_ID;
192 light_control.version_major = LIGHT_VERSION_MAJOR;
193 light_control.version_minor = LIGHT_VERSION_MINOR;
194 light_control.is_core = false;
195 light_control.fields = fields;
196 light_control.field_count = ARRAY_SIZE(fields);
197 light_control.max_instance_count = MAX_INSTANCE_COUNT;
198 light_control.create_cb = light_control_create;
199 lwm2m_register_obj(&light_control);
200
201 return 0;
202 }
203
204 LWM2M_OBJ_INIT(ipso_light_control_init);
205