-
Notifications
You must be signed in to change notification settings - Fork 0
/
coap_resources.c
513 lines (393 loc) · 11.3 KB
/
coap_resources.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
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
/*
* coap_resources.c
*
* Created on: Dec 5, 2013
* Author: poppe
*/
#include <coap.h>
void nullCB(coap_pkt_t *, coap_resource_t *);
SLL(coapRSrcList);
SLL(rSrcDirAttrList);
static coap_resource_t rsrcDir;
static char rsrcDirPath[] = "/.well-known/core";
/**
* Static Prototypes
*/
static int coap_findWithHash(coap_resource_t *rSrc, uint32_t *hash);
static void coap_buildRunningHash(void *data, void *arg1, void *arg2);
static void rSrcDirCB(coap_pkt_t *, coap_resource_t *);
static char *coap_printResource(coap_resource_t *rSrc, char *ptr);
static uint8_t coap_resourceContainsQuery(coap_resource_t *rSrc, coap_option_t *opt);
coap_resource_t *coap_resourceRegister(const char *URI)
{
coap_resource_t *newRSrc = coap_malloc(sizeof(coap_resource_t));
memset(newRSrc, '\0', sizeof(coap_resource_t));
char *newStr = coap_malloc(strlen(URI+1));
strcpy(newStr, URI);
sll_addData(coapRSrcList, newRSrc);
newRSrc->fullUri = newStr;
newRSrc->fullUriHash = coap_Hash(newStr);
newRSrc->attrList = (struct sll_list *)coap_malloc(sizeof(struct sll_list));
newRSrc->pathList = (struct sll_list *)coap_malloc(sizeof(struct sll_list));
sll_init(newRSrc->attrList);
sll_init(newRSrc->pathList);
LWIP_DEBUGF(COAP_DEBUG, ("Register URI: %s with Hash: %i\n", newRSrc->fullUri, newRSrc->fullUriHash));
return newRSrc;
}
void coap_resourceRegisterCB(coap_resource_t *rSrc, coap_resourceCB callback, coap_methods_t method )
{
RT_ASSERT(rSrc);
RT_ASSERT(callback);
switch(method)
{
case coap_get:
rSrc->getCB = callback;
break;
case coap_post:
rSrc->postCB = callback;
break;
case coap_put:
rSrc->putCB = callback;
break;
case coap_delete:
rSrc->deleteCB = callback;
break;
}
LWIP_DEBUGF(COAP_DEBUG, ("Registered callback: %x with method %i\n", callback, method));
}
void coap_resourceRegisterAttribute(coap_resource_t *rSrc, const char *key, const char *value)
{
RT_ASSERT(rSrc);
coap_attribute_t *newAttr = coap_malloc(sizeof(coap_attribute_t));
char *newKey, *newValue;
newKey = coap_malloc(strlen(key)+1);
newValue = coap_malloc(strlen(value)+1);
strcpy(newKey, key);
strcpy(newValue, value);
newAttr->key = newKey;
newAttr->value = newValue;
newAttr->next = NULL;
sll_addData(rSrc->attrList, newAttr);
LWIP_DEBUGF(COAP_DEBUG, ("Registered Key %s with Value: %s\n", newKey, newValue));
return;
}
coap_attribute_t *coap_resourceContainsAttribute(coap_resource_t *rSrc, const char *key)
{
}
uint32_t coap_buildURI(coap_pkt_t *pkt)
{
RT_ASSERT(pkt);
coap_option_t *uriOpt;
uint32_t hash;
uint8_t buf[128];
uint8_t *ptr = buf;
uint8_t multiQuery;
uriOpt = pkt->options;
while(uriOpt)
{
if(uriOpt->num == coap_Uri_path)
{
*ptr++ = '/';
memcpy(ptr, uriOpt->value, uriOpt->len);
ptr += uriOpt->len;
}
if(uriOpt->num == coap_uri_query)
{
if(!multiQuery)
*ptr++ = '?';
else
*ptr++ = '&';
multiQuery++;
memcpy(ptr, uriOpt->value, uriOpt->len);
}
SLL_NEXT(uriOpt);
}
*ptr = '\0';
hash = coap_Hash(buf);
LWIP_DEBUGF(COAP_DEBUG, ("incoming URI:%s with a hash of:%i\n", buf, hash));
return hash;
}
coap_err_t coap_resourceDiscovery(coap_pkt_t *pkt, coap_resource_t **foundRes)
{
RT_ASSERT(pkt);
coap_option_t *uriOpt;
uint32_t hash;
//TODO: move the option variable into the function
//uriOpt = coap_findOption(pkt, coap_Uri_path);
if(proxy_on)
{
coap_forwardPkt(pkt);
}
hash = coap_buildURI(pkt);
*foundRes = (coap_resource_t *)sll_searchWithin(coapRSrcList, coap_findWithHash, &hash);
LWIP_DEBUGF(COAP_DEBUG, ("Resource found: %s\n", (*foundRes)->fullUri));
//callback function for found resource method
//TODO:This might become its own function after we find the resource
//TODO: This needs to move to allow for block transfers
return coap_OK;
}
sll_list_t coap_breakupURI(coap_resource_t *rSrc, uint8_t *uri)
{
uint8_t *ptr = uri, *start, end;
uint32_t len = 0;
coap_option_t *opt, tempOpt;
SLL_MALLOC(optList);
LWIP_DEBUGF(COAP_DEBUG, ("Full URI of %s is being broken-up\n", uri));
while(*ptr)
{
if(('a'<= *ptr && *ptr <= 'z') || ('A'<= *ptr && *ptr <= 'Z'))
{
if(!start)
start = ptr;
len++;
} else if( *ptr == '/')
{
end = 1;
} else if(*ptr == '\%')
{
}
if(end)
{
end = 0;
opt = (coap_option_t *)malloc(sizeof(coap_option_t)+len+1);
opt->num = coap_Uri_path;
opt->len = len;
opt->value[opt->len] = '\0';
memcpy(opt->value, start, (opt->len));
start = NULL;
sll_addData(optList, opt);
LWIP_DEBUGF(COAP_DEBUG, ("URI section: %s of len %i added to list\n", opt->value, opt->len));
}
ptr++;
}
return optList;
}
void coap_resourceCallback(coap_resource_t *foundRes, coap_pkt_t *pkt)
{
switch(pkt->header.code_bits.type)
{
case coap_get:
LWIP_DEBUG(COAP_DEBUG, ("Get found:\n"));
if(foundRes->getCB)
foundRes->getCB(pkt, foundRes);
else coap_simpleReply(pkt, COAP_CODE(204));
break;
case coap_post:
LWIP_DEBUG(COAP_DEBUG, ("Post found:\n"));
if(foundRes->postCB)
foundRes->postCB(pkt, foundRes);
else coap_simpleReply(pkt, COAP_CODE(204));
break;
case coap_put:
LWIP_DEBUG(COAP_DEBUG, ("Put found:\n"));
if(foundRes->putCB)
foundRes->putCB(pkt, foundRes);
else coap_simpleReply(pkt, COAP_CODE(204));
break;
case coap_delete:
LWIP_DEBUG(COAP_DEBUG, ("Delete found:\n"));
if(foundRes->deleteCB)
foundRes->deleteCB(pkt, foundRes);
else coap_simpleReply(pkt, COAP_CODE(204));
break;
default:
LWIP_DEBUGF(COAP_DEBUG, ("Code Not Found\n"));
}
}
void coap_displayResources(void)
{
sll_node_t curRsrc = coapRSrcList->first;
coap_resource_t *cur = (coap_resource_t *)curRsrc;
coap_attribute_t *attr;
sll_list_t attrList;
LWIP_DEBUGF(COAP_DEBUG,
("+++++++++======My Coap implementation: ver: %i.%i===========++++++++++\n",
COAP_VER_MAJOR, COAP_VER_MINOR));
while(cur)
{
LWIP_DEBUGF(COAP_DEBUG, ("******URI: \"%s\" with hash: %i ********\n", cur->fullUri, cur->fullUriHash));
if(cur->getCB)
LWIP_DEBUGF(COAP_DEBUG, (" Has Get CB\n"));
if(cur->postCB)
LWIP_DEBUGF(COAP_DEBUG, (" Has Post CB\n"));
if(cur->deleteCB)
LWIP_DEBUGF(COAP_DEBUG, (" Has Delete CB\n"));
if(cur->putCB)
LWIP_DEBUGF(COAP_DEBUG, (" Has Put CB\n"));
attr = cur->attrList->first;
//sll_forEachData(coapRSrcList,
while(attr)
{
LWIP_DEBUGF(COAP_DEBUG, (" Attribute: %s Value: %s\n", attr->key, attr->value));
attr = (coap_attribute_t *)sll_next(attr);
}
LWIP_DEBUGF(COAP_DEBUG, ("\n"));
SLL_NEXT(cur);
}
}
void nullCB(coap_pkt_t *pkt, coap_resource_t *rSrc)
{
RT_ASSERT(pkt);
coap_pkt_t outPkt;
uint8_t *newPtr;
memset(&outPkt, 0, sizeof(coap_pkt_t));
newPtr = (uint8_t *)&outPkt.header;
LWIP_DEBUGF(COAP_DEBUG, ("Using Default Callback\n"));
outPkt.header.bits.ver = pkt->header.bits.ver;
outPkt.header.bits.t = COAP_ACK;
outPkt.header.bits.tkl = pkt->header.bits.tkl;
outPkt.header.msgID = pkt->header.msgID;
outPkt.header.code_bits.code = 4; //COAP_CODE(404);
outPkt.header.code_bits.type = 4;
outPkt.ip_addr = pkt->ip_addr;
outPkt.port = pkt->port;
if(outPkt.header.bits.tkl)
{
size_t numTks = (size_t)pkt->header.bits.tkl;
memcpy(outPkt.token, pkt->token, numTks);
}
LWIP_DEBUGF(COAP_DEBUG, ("4 Byte Header %o %o %o %o\n", *newPtr, *(newPtr+1), *(newPtr+2), *(newPtr+3)));
coap_sendPkt(&outPkt);
}
/**
* LINK FORMAT
*/
//TODO: Make special note that the old packet is now the reply packet.... Edit it if you want options and or payload, but just \
replay the packet code though
static void rSrcDirCB(coap_pkt_t *pkt, coap_resource_t *rSrc)
{
//FIXME: I need to be able to send this in blocks. Right now I am going to assume that it fits fine into the buffer but in the future that will not be the case
declareBlockVar(buf, 64, cnt);
LWIP_DEBUGF(COAP_DEBUG, ("Blockwise receive Block:%i Start:%i End:%i\n", pkt->block.num, cntStart, cntEnd));
coap_attribute_t *attrs;
coap_option_t *opt;
rSrc = (coap_resource_t *)coapRSrcList->first;
/**
* FIXME even numbers right now have quotes around it
* TODO implement query filter functionality... Looks tough
*/
LWIP_DEBUGF(COAP_DEBUG, ("/.well-known/core CB executed\n"));
opt = coap_findOption(pkt, coap_uri_query);
if(opt)
{
while(rSrc)
{
if(coap_resourceContainsQuery(rSrc, opt))
{
//coap_printResource(rSrc, ptr);
}
}
}
while(rSrc)
{
// This gives me the format ---> <URI>;attr="value";attr="value, <URI>;attr="value"
coap_attribute_t *attrs;
//URI-Reference
if(cntRunning != 0)
{
blockAddChar(buf, cnt, ',');
}
blockAddChar(buf, cnt, '<');
blockAddString(buf, cnt, rSrc->fullUri);
blockAddChar(buf, cnt, '>');
//blockReturn(cnt);
//link-params
attrs = rSrc->attrList->first;
while(attrs)
{
blockAddChar(buf, cnt, ';');
blockAddString(buf, cnt, attrs->key);
blockAddChar(buf, cnt, '=');
blockAddChar(buf, cnt, '"');
blockAddString(buf, cnt, attrs->value);
blockAddChar(buf, cnt, '"');
attrs = attrs->next;
}
rSrc = rSrc->next;
}
*bufPtr = '\0';
LWIP_DEBUGF(COAP_DEBUG,(".well-known reply: %s\n", buf));
uint8_t format = mime_application_link_format;
coap_option_t *optList = NULL;
coap_option_t *optBlock = NULL;
coap_addOptionList(&optList, coap_content_format, &format, 1, 0);
if(cntRunning > cntEnd || cntStart != 0)
{
optBlock = coap_blockCreateOpt(64, (cntStart/64), (cntRunning > cntEnd ? -1 : 0));
coap_addOptionToList(&optList, optBlock);
}
coap_reply(pkt, buf, rt_strlen(buf), COAP_CODE(205), optList);
}
static uint8_t coap_resourceContainsQuery(coap_resource_t *rSrc, coap_option_t *opt)
{
#if 0
char
sll_searchWithin(rSrc->attrList, coap_sllFindAttribute, opt->)
#endif
return 0;
}
static char *coap_printResource(coap_resource_t *rSrc, char *ptr)
{
// This gives me the format ---> <URI>;attr="value";attr="value, <URI>;attr="value"
coap_attribute_t *attrs;
//URI-Reference
*ptr++ = '<';
strcpy(ptr, rSrc->fullUri);
ptr += strlen(rSrc->fullUri);
*ptr++ = '>';
//link-params
attrs = rSrc->attrList->first;
while(attrs)
{
*ptr++ = ';';
strcpy(ptr, attrs->key);
ptr += strlen(attrs->key);
*ptr++ = '=';
*ptr++ = '"';
strcpy(ptr, attrs->value);
ptr += strlen(attrs->value);
*ptr++ = '"';
attrs = attrs->next;
}
if(rSrc = rSrc->next)
*ptr++ = ',';
return ptr;
}
static int coap_findWithHash(coap_resource_t *rSrc, uint32_t *hash)
{
LWIP_DEBUGF(COAP_DEBUG, ("Find Hash: %i compare Hash: %i\n", *hash, rSrc->fullUriHash));
if(rSrc->fullUriHash == *hash)
return -1;
return 0;
}
static void coap_buildRunningHash(void *data, void *arg1, void *arg2)
{
coap_option_t *uriOpt = (coap_option_t *)data;
enum coap_options *optNum = (enum coap_options *)(arg1);
uint32_t *hash = (uint32_t *)(arg2);
if(uriOpt->num == *optNum)
*hash = coap_runningHash(uriOpt->value, *hash);
}
void coap_resourceInit(void)
{
SLL_MALLOC(pathList);
rsrcDir.fullUri = rsrcDirPath;
rsrcDir.fullUriHash = coap_Hash(rsrcDirPath);
rsrcDir.attrList = rSrcDirAttrList;
/* Not sure if I need this so I left it as a malloc */
rsrcDir.pathList = pathList;
coap_resourceRegisterCB(&rsrcDir, rSrcDirCB, coap_get);
sll_addData(coapRSrcList, &rsrcDir);
LWIP_DEBUGF(COAP_DEBUG, ("Resource Init'd\n"));
}
static int coap_sllFindAttribute(void *arg1, void *arg2)
{
coap_attribute_t *attr = (coap_attribute_t *)arg1;
char *key = (char *)arg2;
//TODO: in the future I need to add *for general broader searches
if(!rt_strcmp(attr->key, key))
{
return -1;
}
return 0;
}