-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectserver.cpp
354 lines (295 loc) · 10.9 KB
/
objectserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//
// Created by sorhanp on 28.7.2019.
//
#include "objectserver.h"
ObjectServer::ObjectServer() {
m_serverObj = (lwm2m_object_t *)lwm2m_malloc(sizeof(lwm2m_object_t));
memset(m_serverObj, 0, sizeof(lwm2m_object_t));
}
void ObjectServer::free_server_object(lwm2m_object_t* object) {
while (object->instanceList != nullptr)
{
auto * serverInstance = object->instanceList;
object->instanceList = object->instanceList->next;
lwm2m_free(serverInstance);
}
lwm2m_free(object);
}
lwm2m_object_t *ObjectServer::get_server_object() {
if (nullptr != m_serverObj)
{
server_instance_t * serverInstance;
m_serverObj->objID = 1;
// Manually create an hardcoded server
serverInstance = (server_instance_t *)lwm2m_malloc(sizeof(server_instance_t));
if (nullptr == serverInstance)
{
lwm2m_free(m_serverObj);
return nullptr;
}
memset(serverInstance, 0, sizeof(server_instance_t));
serverInstance->instanceId = 0;
serverInstance->shortServerId = 123;
serverInstance->lifetime = 300;
serverInstance->storing = false;
serverInstance->binding[0] = 'U';
m_serverObj->instanceList = LWM2M_LIST_ADD(m_serverObj->instanceList, serverInstance);
m_serverObj->readFunc = prv_server_read;
m_serverObj->writeFunc = prv_server_write;
m_serverObj->createFunc = prv_server_create;
m_serverObj->deleteFunc = prv_server_delete;
m_serverObj->executeFunc = prv_server_execute;
m_serverObj->discoverFunc = prv_server_discover;
}
return m_serverObj;
}
uint8_t ObjectServer::prv_server_read(uint16_t instanceId, int *numDataP, lwm2m_data_t **dataArrayP, lwm2m_object_t *objectP) {
server_instance_t * targetP;
uint8_t result;
int i;
targetP = (server_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);
if (nullptr == targetP) return COAP_404_NOT_FOUND;
// is the server asking for the full instance ?
if (*numDataP == 0)
{
uint16_t resList[] = {
LWM2M_SERVER_SHORT_ID_ID,
LWM2M_SERVER_LIFETIME_ID,
LWM2M_SERVER_STORING_ID,
LWM2M_SERVER_BINDING_ID
};
int nbRes = sizeof(resList)/sizeof(uint16_t);
*dataArrayP = lwm2m_data_new(nbRes);
if (*dataArrayP == nullptr) return COAP_500_INTERNAL_SERVER_ERROR;
*numDataP = nbRes;
for (i = 0 ; i < nbRes ; i++)
{
(*dataArrayP)[i].id = resList[i];
}
}
i = 0;
do
{
result = prv_get_value((*dataArrayP) + i, targetP);
i++;
} while (i < *numDataP && result == COAP_205_CONTENT);
return result;
}
uint8_t ObjectServer::prv_get_value(lwm2m_data_t * dataP,
server_instance_t * targetP) {
switch (dataP->id)
{
case LWM2M_SERVER_SHORT_ID_ID:
lwm2m_data_encode_int(targetP->shortServerId, dataP);
return COAP_205_CONTENT;
case LWM2M_SERVER_LIFETIME_ID:
lwm2m_data_encode_int(targetP->lifetime, dataP);
return COAP_205_CONTENT;
case LWM2M_SERVER_DISABLE_ID:
return COAP_405_METHOD_NOT_ALLOWED;
case LWM2M_SERVER_STORING_ID:
lwm2m_data_encode_bool(targetP->storing, dataP);
return COAP_205_CONTENT;
case LWM2M_SERVER_BINDING_ID:
lwm2m_data_encode_string(targetP->binding, dataP);
return COAP_205_CONTENT;
case LWM2M_SERVER_UPDATE_ID:
return COAP_405_METHOD_NOT_ALLOWED;
default:
return COAP_404_NOT_FOUND;
}
}
uint8_t ObjectServer::prv_server_write(uint16_t instanceId, int numData, lwm2m_data_t *dataArray, lwm2m_object_t *objectP) {
server_instance_t * targetP;
int i;
uint8_t result;
targetP = (server_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);
if (nullptr == targetP)
{
return COAP_404_NOT_FOUND;
}
i = 0;
do
{
switch (dataArray[i].id)
{
case LWM2M_SERVER_SHORT_ID_ID:
{
uint32_t value;
result = prv_set_int_value(dataArray + i, &value);
if (COAP_204_CHANGED == result)
{
if (0 < value && value <= 0xFFFF)
{
targetP->shortServerId = value;
}
else
{
result = COAP_406_NOT_ACCEPTABLE;
}
}
}
break;
case LWM2M_SERVER_LIFETIME_ID:
result = prv_set_int_value(dataArray + i, (uint32_t *)&(targetP->lifetime));
break;
case LWM2M_SERVER_DISABLE_ID:
result = COAP_405_METHOD_NOT_ALLOWED;
break;
case LWM2M_SERVER_STORING_ID:
{
bool value;
if (1 == lwm2m_data_decode_bool(dataArray + i, &value))
{
targetP->storing = value;
result = COAP_204_CHANGED;
}
else
{
result = COAP_400_BAD_REQUEST;
}
}
break;
case LWM2M_SERVER_BINDING_ID:
if ((dataArray[i].type == LWM2M_TYPE_STRING || dataArray[i].type == LWM2M_TYPE_OPAQUE)
&& dataArray[i].value.asBuffer.length > 0 && dataArray[i].value.asBuffer.length <= 3
&& (strncmp((char*)dataArray[i].value.asBuffer.buffer, "U", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "UQ", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "S", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "SQ", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "US", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "UQS", dataArray[i].value.asBuffer.length) == 0))
{
strncpy(targetP->binding, (char*)dataArray[i].value.asBuffer.buffer, dataArray[i].value.asBuffer.length);
result = COAP_204_CHANGED;
}
else
{
result = COAP_400_BAD_REQUEST;
}
break;
case LWM2M_SERVER_UPDATE_ID:
result = COAP_405_METHOD_NOT_ALLOWED;
break;
default:
return COAP_404_NOT_FOUND;
}
i++;
} while (i < numData && result == COAP_204_CHANGED);
return result;
}
uint8_t ObjectServer::prv_set_int_value(lwm2m_data_t *dataArray, uint32_t *data) {
uint8_t result;
int64_t value;
if (1 == lwm2m_data_decode_int(dataArray, &value))
{
if (value >= 0 && value <= 0xFFFFFFFF)
{
*data = value;
result = COAP_204_CHANGED;
}
else
{
result = COAP_406_NOT_ACCEPTABLE;
}
}
else
{
result = COAP_400_BAD_REQUEST;
}
return result;
}
uint8_t ObjectServer::prv_server_create(uint16_t instanceId, int numData, lwm2m_data_t *dataArray, lwm2m_object_t *objectP) {
server_instance_t * serverInstance;
uint8_t result;
serverInstance = (server_instance_t *)lwm2m_malloc(sizeof(server_instance_t));
if (nullptr == serverInstance) return COAP_500_INTERNAL_SERVER_ERROR;
memset(serverInstance, 0, sizeof(server_instance_t));
serverInstance->instanceId = instanceId;
objectP->instanceList = LWM2M_LIST_ADD(objectP->instanceList, serverInstance);
result = prv_server_write(instanceId, numData, dataArray, objectP);
if (result != COAP_204_CHANGED)
{
(void)prv_server_delete(instanceId, objectP);
}
else
{
result = COAP_201_CREATED;
}
return result;
}
uint8_t ObjectServer::prv_server_delete(uint16_t id, lwm2m_object_t *objectP) {
server_instance_t * serverInstance;
objectP->instanceList = lwm2m_list_remove(objectP->instanceList, id, (lwm2m_list_t **)&serverInstance);
if (nullptr == serverInstance) return COAP_404_NOT_FOUND;
lwm2m_free(serverInstance);
return COAP_202_DELETED;
}
uint8_t ObjectServer::prv_server_execute(uint16_t instanceId, uint16_t resourceId, uint8_t *buffer, int length,
lwm2m_object_t *objectP) {
server_instance_t * targetP;
targetP = (server_instance_t *)lwm2m_list_find(objectP->instanceList, instanceId);
if (nullptr == targetP) return COAP_404_NOT_FOUND;
switch (resourceId)
{
case LWM2M_SERVER_DISABLE_ID:
// executed in core, if COAP_204_CHANGED is returned
return COAP_204_CHANGED;
case LWM2M_SERVER_UPDATE_ID:
// executed in core, if COAP_204_CHANGED is returned
return COAP_204_CHANGED;
default:
return COAP_405_METHOD_NOT_ALLOWED;
}
}
uint8_t ObjectServer::prv_server_discover(uint16_t instanceId, int *numDataP, lwm2m_data_t **dataArrayP,
lwm2m_object_t *objectP) {
uint8_t result;
int i;
result = COAP_205_CONTENT;
// is the server asking for the full object ?
if (*numDataP == 0)
{
uint16_t resList[] = {
LWM2M_SERVER_SHORT_ID_ID,
LWM2M_SERVER_LIFETIME_ID,
LWM2M_SERVER_MIN_PERIOD_ID,
LWM2M_SERVER_MAX_PERIOD_ID,
LWM2M_SERVER_DISABLE_ID,
LWM2M_SERVER_TIMEOUT_ID,
LWM2M_SERVER_STORING_ID,
LWM2M_SERVER_BINDING_ID,
LWM2M_SERVER_UPDATE_ID
};
int nbRes = sizeof(resList)/sizeof(uint16_t);
*dataArrayP = lwm2m_data_new(nbRes);
if (*dataArrayP == nullptr) return COAP_500_INTERNAL_SERVER_ERROR;
*numDataP = nbRes;
for (i = 0 ; i < nbRes ; i++)
{
(*dataArrayP)[i].id = resList[i];
}
}
else
{
for (i = 0; i < *numDataP && result == COAP_205_CONTENT; i++)
{
switch ((*dataArrayP)[i].id)
{
case LWM2M_SERVER_SHORT_ID_ID:
case LWM2M_SERVER_LIFETIME_ID:
case LWM2M_SERVER_MIN_PERIOD_ID:
case LWM2M_SERVER_MAX_PERIOD_ID:
case LWM2M_SERVER_DISABLE_ID:
case LWM2M_SERVER_TIMEOUT_ID:
case LWM2M_SERVER_STORING_ID:
case LWM2M_SERVER_BINDING_ID:
case LWM2M_SERVER_UPDATE_ID:
break;
default:
result = COAP_404_NOT_FOUND;
}
}
}
return result;
}