-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectdevice.cpp
199 lines (167 loc) · 5.22 KB
/
objectdevice.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
//
// Created by sorhanp on 3.8.2019.
//
#include "objectdevice.h"
ObjectDevice::ObjectDevice() {
m_deviceObj = (lwm2m_object_t *)lwm2m_malloc(sizeof(lwm2m_object_t));
}
void ObjectDevice::free_object_device(lwm2m_object_t *objectP) {
if (NULL != objectP->userData)
{
lwm2m_free(objectP->userData);
objectP->userData = NULL;
}
if (NULL != objectP->instanceList)
{
lwm2m_free(objectP->instanceList);
objectP->instanceList = NULL;
}
lwm2m_free(objectP);
}
lwm2m_object_t *ObjectDevice::get_object_device() {
if (nullptr != m_deviceObj)
{
memset(m_deviceObj, 0, sizeof(lwm2m_object_t));
/*
* It assigns his unique ID
* The 3 is the standard ID for the mandatory object "Object device".
*/
m_deviceObj->objID = LWM2M_DEVICE_OBJECT_ID;
/*
* and its unique instance
*
*/
m_deviceObj->instanceList = (lwm2m_list_t *)lwm2m_malloc(sizeof(lwm2m_list_t));
if (nullptr != m_deviceObj->instanceList)
{
memset(m_deviceObj->instanceList, 0, sizeof(lwm2m_list_t));
}
else
{
lwm2m_free(m_deviceObj);
return nullptr;
}
/*
* And the private function that will access the object.
* Those function will be called when a read/write/execute query is made by the server. In fact the library don't need to
* know the resources of the object, only the server does.
*/
m_deviceObj->readFunc = prv_device_read;
m_deviceObj->executeFunc = prv_device_execute;
m_deviceObj->discoverFunc = prv_device_discover;
}
return m_deviceObj;
}
uint8_t ObjectDevice::prv_device_read(uint16_t instanceId, int *numDataP, lwm2m_data_t **dataArrayP, lwm2m_object_t *objectP) {
uint8_t result;
int i;
// this is a single instance object
if (instanceId != 0)
{
return COAP_404_NOT_FOUND;
}
// is the server asking for the full object ?
if (*numDataP == 0)
{
uint16_t resList[] = {
RES_O_MANUFACTURER,
RES_O_MODEL_NUMBER,
RES_M_BINDING_MODES
};
int nbRes = sizeof(resList)/sizeof(uint16_t);
*dataArrayP = lwm2m_data_new(nbRes);
if (*dataArrayP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
*numDataP = nbRes;
for (i = 0 ; i < nbRes ; i++)
{
(*dataArrayP)[i].id = resList[i];
}
}
i = 0;
do
{
result = prv_set_value((*dataArrayP) + i);
i++;
} while (i < *numDataP && result == COAP_205_CONTENT);
return result;
}
uint8_t ObjectDevice::prv_set_value(lwm2m_data_t *dataP) {
// a simple switch structure is used to respond at the specified resource asked
switch (dataP->id)
{
case RES_O_MANUFACTURER:
lwm2m_data_encode_string(PRV_MANUFACTURER, dataP);
return COAP_205_CONTENT;
case RES_O_MODEL_NUMBER:
lwm2m_data_encode_string(PRV_MODEL_NUMBER, dataP);
return COAP_205_CONTENT;
case RES_M_REBOOT:
return COAP_405_METHOD_NOT_ALLOWED;
case RES_M_BINDING_MODES:
lwm2m_data_encode_string(PRV_BINDING_MODE, dataP);
return COAP_205_CONTENT;
default:
return COAP_404_NOT_FOUND;
}
}
uint8_t ObjectDevice::prv_device_execute(uint16_t instanceId, uint16_t resourceId, uint8_t *buffer, int length,
lwm2m_object_t *objectP) {
// this is a single instance object
if (instanceId != 0)
{
return COAP_404_NOT_FOUND;
}
if (length != 0) return COAP_400_BAD_REQUEST;
if (resourceId == RES_M_REBOOT)
{
fprintf(stdout, "\n\t REBOOT\r\n\n");
return COAP_204_CHANGED;
}
return COAP_405_METHOD_NOT_ALLOWED;
}
uint8_t ObjectDevice::prv_device_discover(uint16_t instanceId, int *numDataP, lwm2m_data_t **dataArrayP,
lwm2m_object_t *objectP) {
uint8_t result;
int i;
// this is a single instance object
if (instanceId != 0)
{
return COAP_404_NOT_FOUND;
}
result = COAP_205_CONTENT;
// is the server asking for the full object ?
if (*numDataP == 0)
{
uint16_t resList[] = {
RES_O_MANUFACTURER,
RES_O_MODEL_NUMBER,
RES_M_BINDING_MODES,
RES_M_REBOOT
};
int nbRes = sizeof(resList)/sizeof(uint16_t);
*dataArrayP = lwm2m_data_new(nbRes);
if (*dataArrayP == NULL) 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 RES_O_MANUFACTURER:
case RES_O_MODEL_NUMBER:
case RES_M_BINDING_MODES:
case RES_M_REBOOT:
break;
default:
result = COAP_404_NOT_FOUND;
}
}
}
return result;
}