forked from CreatorDev/libobjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lwm2m-client-device-object.c
260 lines (219 loc) · 15.9 KB
/
lwm2m-client-device-object.c
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
/**
* @file
* LightWeightM2M LWM2M Flow Access object.
*
* @author Imagination Technologies
*
* @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group
* companies and/or licensors.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/***************************************************************************************************
* Includes
**************************************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "coap_abstraction.h"
#include <awa/static.h>
/***************************************************************************************************
* Definitions
**************************************************************************************************/
#define LWM2M_DEVICE_OBJECT 3
#define LWM2M_DEVICE_OBJECT_MANUFACTURER 0
#define LWM2M_DEVICE_OBJECT_MODEL_NUMBER 1
#define LWM2M_DEVICE_OBJECT_SERIAL_NUMBER 2
#define LWM2M_DEVICE_OBJECT_FIRMWARE_VERSION 3
#define LWM2M_DEVICE_OBJECT_REBOOT 4
#define LWM2M_DEVICE_OBJECT_FACTORY_RESET 5
#define LWM2M_DEVICE_OBJECT_AVAILABLE_POWER_SOURCES 6
#define LWM2M_DEVICE_OBJECT_POWER_SOURCE_VOLTAGE 7
#define LWM2M_DEVICE_OBJECT_POWER_SOURCE_CURRENT 8
#define LWM2M_DEVICE_OBJECT_BATTERY_LEVEL 9
#define LWM2M_DEVICE_OBJECT_MEMORY_FREE 10
#define LWM2M_DEVICE_OBJECT_ERROR_CODE 11
#define LWM2M_DEVICE_OBJECT_RESET_ERROR_CODE 12
#define LWM2M_DEVICE_OBJECT_CURRENT_TIME 13
#define LWM2M_DEVICE_OBJECT_UTC_OFFSET 14
#define LWM2M_DEVICE_OBJECT_TIME_ZONE 15
#define LWM2M_DEVICE_OBJECT_SUPPORTED_BINDING_AND_MODES 16
#define LWM2M_DEVICE_OBJECT_DEVICE_TYPE 17
#define LWM2M_DEVICE_OBJECT_HARDWARE_VERSION 18
#define LWM2M_DEVICE_OBJECT_SOFTWARE_VERSION 19
#define LWM2M_DEVICE_OBJECT_BATTERY_STATUS 20
#define LWM2M_DEVICE_OBJECT_MEMORY_TOTAL 21
#define LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT 7
#define LWM2M_DEVICE_OBJECT_ERROR_CODE_COUNT 2
typedef struct
{
char Manufacturer[64];
char ModelNumber[64];
char SerialNumber[64];
char FirmwareVersion[64];
AwaInteger AvailablePowerSources[LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT];
AwaInteger PowerSourceVoltage[LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT];
AwaInteger PowerSourceCurrent[LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT];
AwaInteger BatteryLevel;
AwaInteger MemoryFree;
AwaInteger ErrorCode[LWM2M_DEVICE_OBJECT_ERROR_CODE_COUNT];
AwaTime CurrentTime;
char UTCOffset[16];
char Timezone[16];
char SupportedBindingandModes[4];
char DeviceType[64];
char HardwareVersion[64];
char SoftwareVersion[64];
AwaInteger BatteryStatus;
AwaInteger MemoryTotal;
} DeviceObject;
DeviceObject DeviceObjectStorage = { .SupportedBindingandModes = "U",
.SoftwareVersion = VERSION};
/***************************************************************************************************
* Implementation - Public
**************************************************************************************************/
AwaResult executeHandler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID,
AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed)
{
AwaResult result = AwaResult_Unspecified;
printf("EXECUTE handler operation %d\n", operation);
switch(operation)
{
case AwaOperation_CreateResource:
result = AwaResult_SuccessCreated;
break;
}
return result;
}
int DefineDeviceObject(AwaStaticClient *awaClient)
{
AwaStaticClient_DefineObject(awaClient, LWM2M_DEVICE_OBJECT, "Device", 1, 1);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MANUFACTURER, "Manufacturer", AwaResourceType_String, 0, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MANUFACTURER, DeviceObjectStorage.Manufacturer,
sizeof(DeviceObjectStorage.Manufacturer), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MODEL_NUMBER, "ModelNumber", AwaResourceType_String, 0, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MODEL_NUMBER, DeviceObjectStorage.ModelNumber,
sizeof(DeviceObjectStorage.ModelNumber), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SERIAL_NUMBER, "SerialNumber", AwaResourceType_String, 0, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SERIAL_NUMBER, DeviceObjectStorage.SerialNumber,
sizeof(DeviceObjectStorage.SerialNumber), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_FIRMWARE_VERSION, "FirmwareVersion", AwaResourceType_String, 0, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_FIRMWARE_VERSION, DeviceObjectStorage.FirmwareVersion,
sizeof(DeviceObjectStorage.FirmwareVersion), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_AVAILABLE_POWER_SOURCES, "AvailablePowerSources",
AwaResourceType_Integer, 0, LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT, AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_AVAILABLE_POWER_SOURCES,
DeviceObjectStorage.AvailablePowerSources, sizeof(AwaInteger), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_POWER_SOURCE_VOLTAGE, "PowerSourceVoltage", AwaResourceType_Integer, 0,
LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT, AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_POWER_SOURCE_VOLTAGE,
DeviceObjectStorage.PowerSourceVoltage, sizeof(AwaInteger), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_POWER_SOURCE_CURRENT, "PowerSourceCurrent", AwaResourceType_Integer, 0,
LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT, AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_POWER_SOURCE_CURRENT,
DeviceObjectStorage.PowerSourceCurrent, sizeof(AwaInteger), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_BATTERY_LEVEL, "BatteryLevel", AwaResourceType_Integer, 0,
1, AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_BATTERY_LEVEL, &DeviceObjectStorage.BatteryLevel,
sizeof(AwaInteger), 0 );
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MEMORY_FREE, "MemoryFree", AwaResourceType_Integer, 0,
1, AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MEMORY_FREE, &DeviceObjectStorage.MemoryFree,
sizeof(AwaInteger), 0 );
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_ERROR_CODE, "ErrorCode", AwaResourceType_Integer, 0,
LWM2M_DEVICE_OBJECT_ERROR_CODE_COUNT, AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_ERROR_CODE, DeviceObjectStorage.ErrorCode,
sizeof(AwaInteger), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_CURRENT_TIME, "CurrentTime", AwaResourceType_Time, 0,
1, AwaResourceOperations_ReadWrite);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_CURRENT_TIME, &DeviceObjectStorage.CurrentTime,
sizeof(AwaTime), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_UTC_OFFSET, "UTCOffset", AwaResourceType_String, 0, 1,
AwaResourceOperations_ReadWrite);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_UTC_OFFSET, DeviceObjectStorage.UTCOffset,
sizeof(DeviceObjectStorage.UTCOffset), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_TIME_ZONE, "Timezone", AwaResourceType_String, 0, 1,
AwaResourceOperations_ReadWrite);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_TIME_ZONE, DeviceObjectStorage.Timezone,
sizeof(DeviceObjectStorage.Timezone), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SUPPORTED_BINDING_AND_MODES, "SupportedBindingandModes",
AwaResourceType_String, 1, 1, AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SUPPORTED_BINDING_AND_MODES,
DeviceObjectStorage.SupportedBindingandModes, sizeof(DeviceObjectStorage.SupportedBindingandModes), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_DEVICE_TYPE, "DeviceType", AwaResourceType_String, 1, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_DEVICE_TYPE, DeviceObjectStorage.DeviceType,
sizeof(DeviceObjectStorage.DeviceType), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_HARDWARE_VERSION, "HardwareVersion", AwaResourceType_String, 0, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_HARDWARE_VERSION, DeviceObjectStorage.HardwareVersion,
sizeof(DeviceObjectStorage.HardwareVersion), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SOFTWARE_VERSION, "SoftwareVersion", AwaResourceType_String, 0, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SOFTWARE_VERSION, DeviceObjectStorage.SoftwareVersion,
sizeof(DeviceObjectStorage.SoftwareVersion), 0);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_BATTERY_STATUS, "BatteryStatus", AwaResourceType_Integer, 0, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_BATTERY_STATUS, &DeviceObjectStorage.BatteryStatus ,
sizeof(AwaInteger), 0 );
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MEMORY_TOTAL, "MemoryTotal", AwaResourceType_Integer, 0, 1,
AwaResourceOperations_ReadOnly);
AwaStaticClient_SetResourceStorageWithPointer(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MEMORY_TOTAL, &DeviceObjectStorage.MemoryTotal ,
sizeof(AwaInteger), 0 );
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_REBOOT, "Reboot", AwaResourceType_None, 0, 1,
AwaResourceOperations_Execute);
AwaStaticClient_SetResourceOperationHandler(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_REBOOT, executeHandler);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_FACTORY_RESET, "FactoryReset", AwaResourceType_None, 0, 1,
AwaResourceOperations_Execute);
AwaStaticClient_SetResourceOperationHandler(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_FACTORY_RESET, executeHandler);
AwaStaticClient_DefineResource(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_RESET_ERROR_CODE, "ResetErrorCode", AwaResourceType_None, 0, 1,
AwaResourceOperations_Execute);
AwaStaticClient_SetResourceOperationHandler(awaClient, LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_RESET_ERROR_CODE, executeHandler);
AwaStaticClient_CreateObjectInstance(awaClient, LWM2M_DEVICE_OBJECT, 0);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_MANUFACTURER);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_MODEL_NUMBER);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_SERIAL_NUMBER);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_FIRMWARE_VERSION);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_AVAILABLE_POWER_SOURCES);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_POWER_SOURCE_VOLTAGE);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_POWER_SOURCE_CURRENT);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_BATTERY_LEVEL);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_MEMORY_FREE);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_CURRENT_TIME);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_UTC_OFFSET);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_TIME_ZONE);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_DEVICE_TYPE);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_HARDWARE_VERSION);
AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_SOFTWARE_VERSION);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_BATTERY_STATUS);
// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_MEMORY_TOTAL);
return 0;
}