45 #define PRINTF(...) printf(__VA_ARGS__)
46 #define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
47 #define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
50 #define PRINT6ADDR(addr)
51 #define PRINTLLADDR(addr)
54 #define ADD_CHAR_IF_POSSIBLE(char) \
55 if(strpos >= *offset && bufpos < preferred_size) { \
56 buffer[bufpos++] = char; \
60 #define ADD_STRING_IF_POSSIBLE(string, op) \
61 tmplen = strlen(string); \
62 if(strpos + tmplen > *offset) { \
63 bufpos += snprintf((char *)buffer + bufpos, \
64 preferred_size - bufpos + 1, \
67 + (*offset - (int32_t)strpos > 0 ? \
68 *offset - (int32_t)strpos : 0)); \
69 if(bufpos op preferred_size) { \
70 PRINTF("res: BREAK at %s (%p)\n", string, resource); \
80 well_known_core_get_handler(
void *request,
void *response, uint8_t *buffer,
81 uint16_t preferred_size, int32_t *offset)
86 resource_t *resource =
NULL;
88 #if COAP_LINK_FORMAT_FILTERING
90 const char *filter =
NULL;
91 const char *attrib =
NULL;
92 const char *found =
NULL;
93 const char *end =
NULL;
96 int len = coap_get_header_uri_query(request, &filter);
99 value = strchr(filter,
'=');
102 len -= strlen(filter) + 1;
104 PRINTF(
"Filter %s = %.*s\n", filter, len, value);
106 if(strcmp(filter,
"href") == 0 && value[0] ==
'/') {
111 lastchar = value[len - 1];
112 value[len - 1] =
'\0';
117 resource = resource->next) {
118 #if COAP_LINK_FORMAT_FILTERING
121 if(strcmp(filter,
"href") == 0) {
122 attrib = strstr(resource->url, value);
123 if(attrib ==
NULL || (value[-1] ==
'/' && attrib != resource->url)) {
126 end = attrib + strlen(attrib);
128 attrib = strstr(resource->attributes, filter);
130 || (attrib[strlen(filter)] !=
'='
131 && attrib[strlen(filter)] !=
'"')) {
134 attrib += strlen(filter) + 2;
135 end = strchr(attrib,
'"');
138 PRINTF(
"Filter: res has attrib %s (%s)\n", attrib, value);
140 while((found = strstr(found, value)) !=
NULL) {
145 if(lastchar == found[len - 1] || lastchar ==
'*') {
153 PRINTF(
"Filter: res has prefix %s\n", found);
155 && (found[len] !=
'"' && found[len] !=
' ' && found[len] !=
'\0')) {
158 PRINTF(
"Filter: res has match\n");
162 PRINTF(
"res: /%s (%p)\npos: s%d, o%ld, b%d\n", resource->url, resource,
163 strpos, *offset, bufpos);
166 ADD_CHAR_IF_POSSIBLE(
',');
168 ADD_CHAR_IF_POSSIBLE(
'<');
169 ADD_CHAR_IF_POSSIBLE(
'/');
170 ADD_STRING_IF_POSSIBLE(resource->url, >=);
171 ADD_CHAR_IF_POSSIBLE(
'>');
173 if(resource->attributes[0]) {
174 ADD_CHAR_IF_POSSIBLE(
';');
175 ADD_STRING_IF_POSSIBLE(resource->attributes, >);
179 if(bufpos > preferred_size && strpos - bufpos > *offset) {
180 PRINTF(
"res: BREAK at %s (%p)\n", resource->url, resource);
186 PRINTF(
"BUF %d: %.*s\n", bufpos, bufpos, (
char *)buffer);
188 coap_set_payload(response, buffer, bufpos);
189 coap_set_header_content_format(response, APPLICATION_LINK_FORMAT);
190 }
else if(strpos > 0) {
191 PRINTF(
"well_known_core_handler(): bufpos<=0\n");
193 coap_set_status_code(response, BAD_OPTION_4_02);
194 coap_set_payload(response,
"BlockOutOfScope", 15);
197 if(resource ==
NULL) {
198 PRINTF(
"res: DONE\n");
201 PRINTF(
"res: MORE at %s (%p)\n", resource->url, resource);
202 *offset += preferred_size;
206 RESOURCE(res_well_known_core,
"ct=40", well_known_core_get_handler,
NULL,
list_t rest_get_resources(void)
Returns the list of registered RESTful resources.
#define NULL
The null pointer.
CoAP implementation for the REST Engine.
void * list_head(list_t list)
Get a pointer to the first element of a list.