1 /*
2 * Copyright (c) 2019 Foundries.io
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /*
8 * Source material for IPSO Buzzer object (3338):
9 * http://www.openmobilealliance.org/tech/profiles/lwm2m/3338.xml
10 */
11
12 #define LOG_MODULE_NAME net_ipso_buzzer
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 BUZZER_VERSION_MAJOR 1
26
27
28 #if defined(CONFIG_LWM2M_IPSO_BUZZER_VERSION_1_1)
29 #define BUZZER_VERSION_MINOR 1
30 #define BUZZER_MAX_ID 8
31 #else
32 #define BUZZER_VERSION_MINOR 0
33 #define BUZZER_MAX_ID 6
34 #endif /* defined(CONFIG_LWM2M_IPSO_BUZZER_VERSION_1_1) */
35
36 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_BUZZER_INSTANCE_COUNT
37
38 /*
39 * Calculate resource instances as follows:
40 * start with BUZZER_MAX_ID
41 */
42 #define RESOURCE_INSTANCE_COUNT (BUZZER_MAX_ID)
43
44 /* resource state */
45 struct ipso_buzzer_data {
46 double level;
47 double delay_duration;
48 double min_off_time;
49
50 uint64_t trigger_offset;
51
52 struct k_work_delayable buzzer_work;
53
54 uint16_t obj_inst_id;
55 bool onoff; /* toggle from resource */
56 bool active; /* digital state */
57 };
58
59 static struct ipso_buzzer_data buzzer_data[MAX_INSTANCE_COUNT];
60
61 static struct lwm2m_engine_obj ipso_buzzer;
62 static struct lwm2m_engine_obj_field fields[] = {
63 OBJ_FIELD_DATA(ON_OFF_RID, RW, BOOL),
64 OBJ_FIELD_DATA(LEVEL_RID, RW_OPT, FLOAT),
65 OBJ_FIELD_DATA(DELAY_DURATION_RID, RW_OPT, FLOAT),
66 OBJ_FIELD_DATA(MINIMUM_OFF_TIME_RID, RW, FLOAT),
67 OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
68 /* This field is actually not in the spec, so nothing sets it except
69 * here users can listen for post_write events to it for buzzer on/off
70 * events
71 */
72 OBJ_FIELD_DATA(DIGITAL_INPUT_STATE_RID, R, BOOL),
73 #if defined(CONFIG_LWM2M_IPSO_BUZZER_VERSION_1_1)
74 OBJ_FIELD_DATA(TIMESTAMP_RID, R_OPT, TIME),
75 OBJ_FIELD_DATA(FRACTIONAL_TIMESTAMP_RID, R_OPT, FLOAT),
76 #endif
77 };
78
79 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
80 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][BUZZER_MAX_ID];
81 static struct lwm2m_engine_res_inst
82 res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
83
get_buzzer_index(uint16_t obj_inst_id)84 static int get_buzzer_index(uint16_t obj_inst_id)
85 {
86 int i, ret = -ENOENT;
87
88 for (i = 0; i < ARRAY_SIZE(inst); i++) {
89 if (!inst[i].obj || inst[i].obj_inst_id != obj_inst_id) {
90 continue;
91 }
92
93 ret = i;
94 break;
95 }
96
97 return ret;
98 }
99
start_buzzer(struct ipso_buzzer_data * buzzer)100 static int start_buzzer(struct ipso_buzzer_data *buzzer)
101 {
102 uint32_t temp = 0U;
103 struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_BUZZER_ID, buzzer->obj_inst_id,
104 DIGITAL_INPUT_STATE_RID);
105
106 /* make sure buzzer is currently not active */
107 if (buzzer->active) {
108 return -EINVAL;
109 }
110
111 /* check min off time from last trigger_offset */
112 temp = (uint32_t)(buzzer->min_off_time * MSEC_PER_SEC);
113 if (k_uptime_get() < buzzer->trigger_offset + temp) {
114 return -EINVAL;
115 }
116
117 /* TODO: check delay_duration > 0 */
118
119 buzzer->trigger_offset = k_uptime_get();
120 lwm2m_set_bool(&path, true);
121
122 temp = (uint32_t)(buzzer->delay_duration * MSEC_PER_SEC);
123 k_work_reschedule(&buzzer->buzzer_work, K_MSEC(temp));
124
125 return 0;
126 }
127
stop_buzzer(struct ipso_buzzer_data * buzzer,bool cancel)128 static int stop_buzzer(struct ipso_buzzer_data *buzzer, bool cancel)
129 {
130 struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_BUZZER_ID, buzzer->obj_inst_id,
131 DIGITAL_INPUT_STATE_RID);
132
133 /* make sure buzzer is currently active */
134 if (!buzzer->active) {
135 return -EINVAL;
136 }
137
138 lwm2m_set_bool(&path, false);
139
140 if (cancel) {
141 k_work_cancel_delayable(&buzzer->buzzer_work);
142 }
143
144 return 0;
145 }
146
onoff_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)147 static int onoff_post_write_cb(uint16_t obj_inst_id,
148 uint16_t res_id, uint16_t res_inst_id,
149 uint8_t *data, uint16_t data_len,
150 bool last_block, size_t total_size)
151 {
152 int i;
153
154 i = get_buzzer_index(obj_inst_id);
155 if (i < 0) {
156 return i;
157 }
158
159 if (!buzzer_data[i].onoff && buzzer_data[i].active) {
160 return stop_buzzer(&buzzer_data[i], true);
161 } else if (buzzer_data[i].onoff && !buzzer_data[i].active) {
162 return start_buzzer(&buzzer_data[i]);
163 }
164
165 return 0;
166 }
167
buzzer_work_cb(struct k_work * work)168 static void buzzer_work_cb(struct k_work *work)
169 {
170 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
171 struct ipso_buzzer_data *buzzer = CONTAINER_OF(dwork,
172 struct ipso_buzzer_data,
173 buzzer_work);
174 stop_buzzer(buzzer, false);
175 }
176
buzzer_create(uint16_t obj_inst_id)177 static struct lwm2m_engine_obj_inst *buzzer_create(uint16_t obj_inst_id)
178 {
179 int index, avail = -1, i = 0, j = 0;
180
181 /* Check that there is no other instance with this ID */
182 for (index = 0; index < ARRAY_SIZE(inst); index++) {
183 if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
184 LOG_ERR("Can not create instance - "
185 "already existing: %u", obj_inst_id);
186 return NULL;
187 }
188
189 /* Save first available slot index */
190 if (avail < 0 && !inst[index].obj) {
191 avail = index;
192 }
193 }
194
195 if (avail < 0) {
196 LOG_ERR("Can not create instance - no more room: %u",
197 obj_inst_id);
198 return NULL;
199 }
200
201 /* Set default values */
202 (void)memset(&buzzer_data[avail], 0, sizeof(buzzer_data[avail]));
203 k_work_init_delayable(&buzzer_data[avail].buzzer_work, buzzer_work_cb);
204 buzzer_data[avail].level = 50; /* 50% */
205 buzzer_data[avail].delay_duration = 1; /* 1 seconds */
206 buzzer_data[avail].obj_inst_id = obj_inst_id;
207
208 (void)memset(res[avail], 0,
209 sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
210 init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
211
212 /* initialize instance resource data */
213 INIT_OBJ_RES(ON_OFF_RID, res[avail], i, res_inst[avail], j, 1, false,
214 true, &buzzer_data[avail].onoff,
215 sizeof(buzzer_data[avail].onoff),
216 NULL, NULL, NULL, onoff_post_write_cb, NULL);
217 INIT_OBJ_RES_DATA(LEVEL_RID, res[avail], i, res_inst[avail], j,
218 &buzzer_data[avail].level,
219 sizeof(buzzer_data[avail].level));
220 INIT_OBJ_RES_DATA(DELAY_DURATION_RID, res[avail], i, res_inst[avail], j,
221 &buzzer_data[avail].delay_duration,
222 sizeof(buzzer_data[avail].delay_duration));
223 INIT_OBJ_RES_DATA(MINIMUM_OFF_TIME_RID, res[avail], i, res_inst[avail],
224 j, &buzzer_data[avail].min_off_time,
225 sizeof(buzzer_data[avail].min_off_time));
226 INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[avail], i,
227 res_inst[avail], j);
228 INIT_OBJ_RES_DATA(DIGITAL_INPUT_STATE_RID, res[avail], i,
229 res_inst[avail], j, &buzzer_data[avail].active,
230 sizeof(buzzer_data[avail].active));
231 #if defined(CONFIG_LWM2M_IPSO_BUZZER_VERSION_1_1)
232 INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[avail], i, res_inst[avail], j);
233 INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[avail], i,
234 res_inst[avail], j);
235 #endif
236
237
238 inst[avail].resources = res[avail];
239 inst[avail].resource_count = i;
240
241 LOG_DBG("Create IPSO Buzzer instance: %d", obj_inst_id);
242
243 return &inst[avail];
244 }
245
ipso_buzzer_init(void)246 static int ipso_buzzer_init(void)
247 {
248 ipso_buzzer.obj_id = IPSO_OBJECT_BUZZER_ID;
249 ipso_buzzer.version_major = BUZZER_VERSION_MAJOR;
250 ipso_buzzer.version_minor = BUZZER_VERSION_MINOR;
251 ipso_buzzer.is_core = false;
252 ipso_buzzer.fields = fields;
253 ipso_buzzer.field_count = ARRAY_SIZE(fields);
254 ipso_buzzer.max_instance_count = ARRAY_SIZE(inst);
255 ipso_buzzer.create_cb = buzzer_create;
256 lwm2m_register_obj(&ipso_buzzer);
257
258 return 0;
259 }
260
261 LWM2M_OBJ_INIT(ipso_buzzer_init);
262