forked from RIOT-Makers/YaCoAP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoap_ext.c
executable file
·37 lines (34 loc) · 1.29 KB
/
coap_ext.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
#include <string.h>
#include "coap_ext.h"
int coap_build_resource_path(coap_resource_path_t* resource_path, char* path)
{
// COAP_MAX_PATHITEMS defined at coap.h
// be careful with real segments size, it must be less then COAP_MAX_PATHITEMS, otherwise it will returned not whole path
uint8_t max_segments = sizeof(resource_path->items) / sizeof(const char*);
char* pch = strtok (path, "/");
uint8_t i = 0;
for ( ;(i < max_segments) && (pch != NULL); ++i) {
resource_path->items[i] = pch;
pch = strtok (NULL, "/");
}
resource_path->count = i;
if (pch != NULL) {
return COAP_ERR_BUFFER_TOO_SMALL;
}
return COAP_ERR_NONE;
}
int coap_check_resource(const coap_resource_ext_t *resource,
const coap_option_t *options, uint8_t options_count)
{
if (options_count == resource->path->count) {
for (uint8_t i = 0; i < options_count; ++i) {
if (strlen(resource->path->items[i]) == options[i].buf.len) {
if (memcmp(resource->path->items[i], options[i].buf.p, options[i].buf.len)) {
return COAP_ERR_OPTION_NOT_FOUND;
}
return COAP_ERR_NONE;
}
}
}
return COAP_ERR_OPTION_LEN_INVALID;
}