-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoneM2M.ino
518 lines (408 loc) · 12.3 KB
/
oneM2M.ino
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* oneM2M.ino
*
* copyright (c) Andreas Kraft 2018
* Licensed under the BSD 3-Clause License. See the LICENSE file for further details.
*
* Implementation of a small subset oneM2M's resources and Mca interface.
*/
// TODO: support labels
#include <ArduinoJson.h>
#include "oneM2M.h"
int OneM2M::_jsonSize = 1024;
HttpServer *OneM2M::_notificationServer = NULL;
String OneM2M::_notificationURL;
LinkedList<OneM2M::NotificationCBStruct *> OneM2M::_notificationCallbacks;
OneM2M::OneM2M(String host, int port, String basePath, String originator) {
_host = host;
_port = port;
_basePath = basePath;
_originator = originator;
}
//
// CSE
//
String OneM2M::getCSE(void) {
return getResource("", ResourceType::CSE);
}
//
// AE
//
String OneM2M::getAE(String path, String appID) {
String result = retrieveAE(path);
if (result.length() == 0) {
return createAE(path, appID);
}
return result;
}
String OneM2M::retrieveAE(String path) {
return getResource(path, ResourceType::AE);
}
String OneM2M::createAE(String path, String appID) {
PathElements elements = _splitPath(path);
return createResource(elements.path,
ResourceType::AE,
"{\"m2m:ae\":{\"rn\":\"" + elements.rn + "\",\"api\":\"" + appID + "\", \"rr\":true}}");
}
//
// Container
//
String OneM2M::getContainer(String path) {
String result = retrieveContainer(path);
if (result.length() == 0) {
return createContainer(path);
}
return result;
}
String OneM2M::retrieveContainer(String path) {
return getResource(path, ResourceType::CONTAINER);
}
String OneM2M::createContainer(String path) {
PathElements elements = _splitPath(path);
return createResource(elements.path,
ResourceType::CONTAINER,
"{\"m2m:cnt\":{\"rn\":\"" + elements.rn + "\"}}");
}
//
// ContentInstance
//
String OneM2M::addContentInstance(String path, String content) {
return addContentInstance(path, content, "text/plain:0");
}
String OneM2M::addContentInstance(String path, String content, String contentType) {
return createResource(path, // no split here. CIN does not provide an rn
ResourceType::CONTENTINSTANCE,
"{\"m2m:cin\":{\"cnf\":\"" + contentType + "\",\"con\":\"" + _escapeJSON(content) + "\"}}");
}
OneM2M::Content OneM2M::getLatestContentInstance(String path) {
return contentFromContentInstance(getResource(path + "/la", ResourceType::CONTENTINSTANCE));
}
OneM2M::Content OneM2M::contentFromContentInstance(String resource) {
OneM2M::Content result;
DynamicJsonBuffer jsonBuffer(_jsonSize);
result.state = false;
if (resource.length() == 0) {
return result;
}
JsonObject& cin = jsonBuffer.parseObject(resource);
if (cin.success()) {
result.resourceIdentifier = cin["m2m:cin"]["ri"].as<String>();
result.content = cin["m2m:cin"]["con"].as<String>();
result.contentFormat = cin["m2m:cin"]["cnf"].as<String>();
result.creationTime = cin["m2m:cin"]["ct"].as<String>();
result.state = true;
}
return result;
}
//
// Subscription
//
String OneM2M::getSubscription(String path) {
String result = retrieveSubscription(path);
if (result.length() == 0) {
return addSubscription(path);
}
return result;
}
String OneM2M::retrieveSubscription(String path) {
return getResource(path, ResourceType::SUBSCRIPTION);
}
String OneM2M::addSubscription(String path) {
if (_notificationServer == NULL) {
return "";
}
PathElements elements = _splitPath(path);
return createResource(elements.path,
ResourceType::SUBSCRIPTION,
"{\"m2m:sub\":{\"rn\":\"" + elements.rn + "\",\"nu\":\"" + _notificationURL + "\",\"nct\":2}}");
}
String OneM2M::getSubscriptionNotify(String path, NotificationCallback callback) {
String sc = getSubscription(path);
if (sc.length() > 0 and callback != NULL) {
addNotificationCallback(getResourceIdentifier(sc), callback);
}
return sc;
}
//
// General OneM2M functions
//
String OneM2M::createResource(String path, int type, String content) {
WiFiClient client;
if ( ! client.connect(_host.c_str(), _port)) {
Serial.println("connection failed");
client.stop();
return "";
}
String request = "POST " + path + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"X-M2M-Origin: " + _originator + "\r\n" +
"Content-Type: application/json;ty=" + type + "\r\n" +
"Content-Length: " + content.length() + "\r\n" +
"Accept: application/json\r\n" +
"Connection: close\r\n\r\n" +
content;
//Serial.println(request + "\n");
client.print(request);
return _getRequestContent(client, 201);
}
String OneM2M::getResource(String path, int type) {
WiFiClient client;
if ( ! client.connect(_host.c_str(), _port)) {
Serial.println("connection failed");
client.stop();
return "";
}
String request = "GET " + _getPath(path) + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"X-M2M-Origin: " + _originator + "\r\n" +
"Content-Type: application/json;ty=" + type + "\r\n" +
"Accept: application/json\r\n" +
"Connection: close\r\n\r\n";
client.print(request);
return _getRequestContent(client, 200);
}
String OneM2M::updateResource(String path, int type, String content) {
WiFiClient client;
if ( ! client.connect(_host.c_str(), _port)) {
Serial.println("connection failed");
client.stop();
return "";
}
String request = "PUT " + path + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"X-M2M-Origin: " + _originator + "\r\n" +
"Content-Type: application/json;ty=" + type + "\r\n" +
"Content-Length: " + content.length() + "\r\n" +
"Accept: application/json\r\n" +
"Connection: close\r\n\r\n" +
content;
// Serial.println(request + "\n");
client.print(request);
return _getRequestContent(client, 200);
}
String OneM2M::deleteResource(String path) {
WiFiClient client;
if ( ! client.connect(_host.c_str(), _port)) {
Serial.println("connection failed");
client.stop();
return "";
}
String request = "DELETE " + path + " HTTP/1.1\r\n" +
"Host: " + _host + "\r\n" +
"X-M2M-Origin: " + _originator + "\r\n" +
"Content-Type: application/json\r\n" +
"Connection: close\r\n\r\n";
// Serial.println(request + "\n");
client.print(request);
return _getRequestContent(client, 200);
}
//
// Internal helper functions
//
// Normalize path
String OneM2M::_getPath(String resourceName) {
String t = _basePath + resourceName;
t.replace("//", "/");
return t;
}
//////////////////////////////////////////////////////////////////////////////
//
// Static class function
//
//
// Miscellaneous functions
//
void OneM2M::setJsonMaxSize(int value) {
_jsonSize = value;
}
int OneM2M::jsonMaxSize(void) {
return _jsonSize;
}
String OneM2M::getResourceIdentifier(String resource) {
DynamicJsonBuffer jsonBuffer(OneM2M::jsonMaxSize());
JsonObject& json = jsonBuffer.parseObject(resource);
if (json.success()) {
JsonObject::iterator it=json.begin(); // only first one
return json[it->key]["ri"].as<String>() ;
}
return "";
}
// Escape JSON special chars
String OneM2M::_escapeJSON(String value) {
String result = value;
result.replace("\\", "\\\\");
result.replace("\n", "\\n");
result.replace("\r", "\\r");
result.replace("\t", "\\t");
result.replace("\"", "\\\"");
// a bit of optimization: don't handle chars that are likely not used at all.
// \b Backspace (ascii code 08)
// \f Form feed (ascii code 0C)
return result;
}
// Split the path in the resource path and the name
OneM2M::PathElements OneM2M::_splitPath(String path) {
PathElements elements;
int l = path.lastIndexOf("/");
elements.path = path.substring(0,l);
elements.rn = path.substring(l+1);
return elements;
}
// Get the content of a request answer
String OneM2M::_getRequestContent(WiFiClient client, int expectedReturnCode) {
String result = "";
String status = "";
unsigned long timeout = millis() + 10000; // TODO Config
while (client.available() == 0) {
if (OneM2M::_notificationServer != NULL) { // if notifications enabled call the server to receive srq requests.
checkNotifications();
}
if (timeout < millis()) {
Serial.println(">>> Client Timeout !");
client.stop();
return "";
}
}
if (client.available()){
status = client.readStringUntil('\n');
//Serial.println(status);
}
if (status.indexOf(String(expectedReturnCode)) == -1) { // error
client.stop();
return "";
}
// read header
while(client.available()){
String x = client.readStringUntil('\n');
x.trim();
if (x.length() == 0) { // end of header reached
break;
}
}
// read content
while(client.available()){
String x = client.readStringUntil('\n');
x.trim();
result += x;
}
client.stop();
// Serial.println("Result:");
// Serial.println(result);
// The following is a hack to fill the buffer in case the call was successul
// but the request returns an empty answer. This happens, for example, when
// a resource is deleted.
if (result.length() == 0) {
result = "ok";
}
return result;
}
//////////////////////////////////////////////////////////////////////////////
//
// Notification Functions
//
void OneM2M::setupNotifications() {
OneM2M::setupNotifications(WiFi.localIP().toString(), 1440, "/onem2m/Notifications");
}
void OneM2M::setupNotifications(String host, int port, String path) {
if (_notificationServer) {
return;
}
_notificationURL = "http://" + host + ":" + String(port) + path;
_notificationServer = new HttpServer(port);
_notificationServer->addHandler(path, HttpServer::POST, _notificationRequestHandler);
}
void OneM2M::shutdownNotifications(void) {
if (_notificationServer == NULL) {
return;
}
delete _notificationServer;
_notificationCallbacks.clear();
_notificationServer = NULL;
}
bool OneM2M::addNotificationCallback(String subscriptionResourceID, NotificationCallback callback) {
if (subscriptionResourceID != NULL && subscriptionResourceID.length() == 0) {
return false;
}
NotificationCBStruct *cb = _getCallback(subscriptionResourceID);
if (cb) {
cb->callback = callback;
return true;
}
cb = new NotificationCBStruct();
cb->subscriptionResourceID = subscriptionResourceID;
cb->callback = callback;
_notificationCallbacks.add(cb);
return true;
}
bool OneM2M::removeNotificationCallback(String subscriptionResourceID) {
if (subscriptionResourceID != NULL && subscriptionResourceID.length() == 0) {
return false;
}
for (int i = 0; i < _notificationCallbacks.size(); i++) {
NotificationCBStruct *cb = _notificationCallbacks.get(i);
if (cb->subscriptionResourceID == subscriptionResourceID) {
_notificationCallbacks.remove(i);
return true;
}
}
return false;
}
// Check for notification server activities
void OneM2M::checkNotifications(void) {
if (_notificationServer != NULL) {
_notificationServer->check();
}
}
// Handle the internal interpretation and routing of notifications
HttpServer::RequestResult OneM2M::_notificationRequestHandler(String path,
HttpServer::Method method,
long length,
String type,
char *content) {
DynamicJsonBuffer jsonBuffer(OneM2M::jsonMaxSize());
HttpServer::RequestResult result;
result.returnCode = 200;
result.attributes = "X-M2M-RSC: 2000";
JsonObject& json = jsonBuffer.parseObject(content);
if (json.success()) {
if (json.containsKey("m2m:sgn")) {
// verification request
if (json["m2m:sgn"]["m2m:vrq"]) {
return result;
}
// notification request
String sur = json["m2m:sgn"]["m2m:sur"];
String rep = json["m2m:sgn"]["m2m:nev"]["m2m:rep"];
if (sur && rep) {
NotificationCBStruct *cb = _getCallback(sur);
if (cb && cb->callback) {
(*cb->callback)(sur, _getCallbackType(rep), rep);
}
return result;
}
}
}
result.returnCode = 400;
result.attributes = "X-M2M-RSC: 4000";
return result;
}
// search and return a callback struct
OneM2M::NotificationCBStruct *OneM2M::_getCallback(String resourceIdentifier) {
for (int i = 0; i < _notificationCallbacks.size(); i++) {
NotificationCBStruct *cb = _notificationCallbacks.get(i);
if (cb->subscriptionResourceID == resourceIdentifier) {
return cb;
}
}
return NULL;
}
// search and return a callback struct
OneM2M::ResourceType OneM2M::_getCallbackType(String resource) {
DynamicJsonBuffer jsonBuffer(OneM2M::jsonMaxSize());
JsonObject& json = jsonBuffer.parseObject(resource);
if (json.success()) {
JsonObject::iterator it=json.begin(); // only first one
return (OneM2M::ResourceType)json[it->key]["ty"].as<int>() ;
}
return OneM2M::ResourceType::UNKNOWN;
}