1 /*
2 * Copyright (c) 2019 Foundries.io
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /*
8 * Source material for IPSO On/Off Switch object (3347):
9 * http://www.openmobilealliance.org/tech/profiles/lwm2m/3347.xml
10 */
11
12 #define LOG_MODULE_NAME net_ipso_button
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 BUTTON_VERSION_MAJOR 1
26
27 #if defined(CONFIG_LWM2M_IPSO_PUSH_BUTTON_VERSION_1_1)
28 #define BUTTON_VERSION_MINOR 1
29 #define BUTTON_MAX_ID 5
30 #else
31 #define BUTTON_VERSION_MINOR 0
32 #define BUTTON_MAX_ID 3
33 #endif /* defined(CONFIG_LWM2M_IPSO_PUSH_BUTTON_VERSION_1_1) */
34
35 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_PUSH_BUTTON_INSTANCE_COUNT
36
37 /*
38 * Calculate resource instances as follows:
39 * start with BUTTON_MAX_ID
40 */
41 #define RESOURCE_INSTANCE_COUNT (BUTTON_MAX_ID)
42
43 /* resource state */
44 struct ipso_button_data {
45 int64_t counter;
46 uint16_t obj_inst_id;
47 bool last_state;
48 bool state;
49 };
50
51 static struct ipso_button_data button_data[MAX_INSTANCE_COUNT];
52
53 static struct lwm2m_engine_obj onoff_switch;
54 static struct lwm2m_engine_obj_field fields[] = {
55 OBJ_FIELD_DATA(DIGITAL_INPUT_STATE_RID, R, BOOL),
56 OBJ_FIELD_DATA(DIGITAL_INPUT_COUNTER_RID, R_OPT, S64),
57 OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
58 #if defined(CONFIG_LWM2M_IPSO_PUSH_BUTTON_VERSION_1_1)
59 OBJ_FIELD_DATA(TIMESTAMP_RID, R_OPT, TIME),
60 OBJ_FIELD_DATA(FRACTIONAL_TIMESTAMP_RID, R_OPT, FLOAT),
61 #endif
62 };
63
64 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
65 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][BUTTON_MAX_ID];
66 static struct lwm2m_engine_res_inst
67 res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
68
get_button_index(uint16_t obj_inst_id)69 static int get_button_index(uint16_t obj_inst_id)
70 {
71 int i, ret = -ENOENT;
72
73 for (i = 0; i < ARRAY_SIZE(inst); i++) {
74 if (!inst[i].obj || inst[i].obj_inst_id != obj_inst_id) {
75 continue;
76 }
77
78 ret = i;
79 break;
80 }
81
82 return ret;
83 }
84
state_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)85 static int state_post_write_cb(uint16_t obj_inst_id,
86 uint16_t res_id, uint16_t res_inst_id,
87 uint8_t *data, uint16_t data_len,
88 bool last_block, size_t total_size)
89 {
90 int i;
91
92 i = get_button_index(obj_inst_id);
93 if (i < 0) {
94 return i;
95 }
96
97 if (button_data[i].state && !button_data[i].last_state) {
98 /* off to on transition, increment the counter */
99 int64_t counter = button_data[i].counter + 1;
100 struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_PUSH_BUTTON_ID, obj_inst_id,
101 DIGITAL_INPUT_COUNTER_RID);
102
103 if (counter < 0) {
104 counter = 0;
105 }
106
107 if (lwm2m_set_s64(&path, counter) < 0) {
108 LOG_ERR("Failed to increment counter resource %d/%d/%d", path.obj_id,
109 path.obj_inst_id, path.res_id);
110 }
111 }
112
113 button_data[i].last_state = button_data[i].state;
114 return 0;
115 }
116
button_create(uint16_t obj_inst_id)117 static struct lwm2m_engine_obj_inst *button_create(uint16_t obj_inst_id)
118 {
119 int index, avail = -1, i = 0, j = 0;
120
121 /* Check that there is no other instance with this ID */
122 for (index = 0; index < ARRAY_SIZE(inst); index++) {
123 if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
124 LOG_ERR("Can not create instance - "
125 "already existing: %u", obj_inst_id);
126 return NULL;
127 }
128
129 /* Save first available slot index */
130 if (avail < 0 && !inst[index].obj) {
131 avail = index;
132 }
133 }
134
135 if (avail < 0) {
136 LOG_ERR("Can not create instance - no more room: %u",
137 obj_inst_id);
138 return NULL;
139 }
140
141 /* Set default values */
142 (void)memset(&button_data[avail], 0, sizeof(button_data[avail]));
143 button_data[avail].obj_inst_id = obj_inst_id;
144
145 (void)memset(res[avail], 0,
146 sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
147 init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
148
149 /* initialize instance resource data */
150 INIT_OBJ_RES(DIGITAL_INPUT_STATE_RID, res[avail], i, res_inst[avail],
151 j, 1, false, true, &button_data[avail].state,
152 sizeof(button_data[avail].state),
153 NULL, NULL, NULL, state_post_write_cb, NULL);
154 INIT_OBJ_RES_DATA(DIGITAL_INPUT_COUNTER_RID, res[avail], i,
155 res_inst[avail], j,
156 &button_data[avail].counter,
157 sizeof(button_data[avail].counter));
158 INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[avail], i,
159 res_inst[avail], j);
160 #if defined(CONFIG_LWM2M_IPSO_PUSH_BUTTON_VERSION_1_1)
161 INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[avail], i, res_inst[avail], j);
162 INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[avail], i,
163 res_inst[avail], j);
164 #endif
165
166 inst[avail].resources = res[avail];
167 inst[avail].resource_count = i;
168
169 LOG_DBG("Create IPSO Button instance: %d", obj_inst_id);
170
171 return &inst[avail];
172 }
173
ipso_button_init(void)174 static int ipso_button_init(void)
175 {
176 onoff_switch.obj_id = IPSO_OBJECT_PUSH_BUTTON_ID;
177 onoff_switch.version_major = BUTTON_VERSION_MAJOR;
178 onoff_switch.version_minor = BUTTON_VERSION_MINOR;
179 onoff_switch.is_core = false;
180 onoff_switch.fields = fields;
181 onoff_switch.field_count = ARRAY_SIZE(fields);
182 onoff_switch.max_instance_count = ARRAY_SIZE(inst);
183 onoff_switch.create_cb = button_create;
184 lwm2m_register_obj(&onoff_switch);
185
186 return 0;
187 }
188
189 LWM2M_OBJ_INIT(ipso_button_init);
190