2014-04-22 08:31:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
|
|
|
|
* 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 Institute 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 INSTITUTE 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 INSTITUTE 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.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* /.well-known/core resource implementation.
|
|
|
|
* \author
|
|
|
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
|
|
|
*/
|
|
|
|
|
2017-12-21 17:54:28 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup coap
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2017-09-01 14:37:51 +00:00
|
|
|
#include "coap-engine.h"
|
2017-10-24 23:46:28 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "coap-log.h"
|
2018-05-04 08:58:58 +00:00
|
|
|
#define LOG_MODULE "coap"
|
2017-11-30 23:41:58 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_COAP
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2014-04-23 08:13:10 +00:00
|
|
|
#define ADD_CHAR_IF_POSSIBLE(char) \
|
|
|
|
if(strpos >= *offset && bufpos < preferred_size) { \
|
|
|
|
buffer[bufpos++] = char; \
|
|
|
|
} \
|
|
|
|
++strpos
|
|
|
|
|
|
|
|
#define ADD_STRING_IF_POSSIBLE(string, op) \
|
|
|
|
tmplen = strlen(string); \
|
|
|
|
if(strpos + tmplen > *offset) { \
|
|
|
|
bufpos += snprintf((char *)buffer + bufpos, \
|
|
|
|
preferred_size - bufpos + 1, \
|
|
|
|
"%s", \
|
|
|
|
string \
|
|
|
|
+ (*offset - (int32_t)strpos > 0 ? \
|
|
|
|
*offset - (int32_t)strpos : 0)); \
|
|
|
|
if(bufpos op preferred_size) { \
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("BREAK at %s (%p)\n", string, resource); \
|
2014-04-23 08:13:10 +00:00
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
strpos += tmplen
|
|
|
|
|
2014-04-22 08:31:43 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Resource Handlers -------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
static void
|
2017-11-14 20:32:43 +00:00
|
|
|
well_known_core_get_handler(coap_message_t *request, coap_message_t *response,
|
2017-10-24 23:46:28 +00:00
|
|
|
uint8_t *buffer, uint16_t preferred_size,
|
|
|
|
int32_t *offset)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
size_t strpos = 0; /* position in overall string (which is larger than the buffer) */
|
|
|
|
size_t bufpos = 0; /* position within buffer (bytes written) */
|
|
|
|
size_t tmplen = 0;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_resource_t *resource = NULL;
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
#if COAP_LINK_FORMAT_FILTERING
|
|
|
|
/* For filtering. */
|
|
|
|
const char *filter = NULL;
|
|
|
|
const char *attrib = NULL;
|
|
|
|
const char *found = NULL;
|
|
|
|
const char *end = NULL;
|
|
|
|
char *value = NULL;
|
|
|
|
char lastchar = '\0';
|
|
|
|
int len = coap_get_header_uri_query(request, &filter);
|
|
|
|
|
|
|
|
if(len) {
|
|
|
|
value = strchr(filter, '=');
|
|
|
|
value[0] = '\0';
|
|
|
|
++value;
|
|
|
|
len -= strlen(filter) + 1;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Filter %s = ", filter);
|
|
|
|
LOG_DBG_COAP_STRING(value, len);
|
|
|
|
LOG_DBG_("\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
if(strcmp(filter, "href") == 0 && value[0] == '/') {
|
|
|
|
++value;
|
|
|
|
--len;
|
|
|
|
}
|
|
|
|
|
|
|
|
lastchar = value[len - 1];
|
|
|
|
value[len - 1] = '\0';
|
|
|
|
}
|
2017-11-30 23:41:58 +00:00
|
|
|
#endif /* COAP_LINK_FORMAT_FILTERING */
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
for(resource = coap_get_first_resource(); resource;
|
|
|
|
resource = coap_get_next_resource(resource)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
#if COAP_LINK_FORMAT_FILTERING
|
|
|
|
/* Filtering */
|
|
|
|
if(len) {
|
|
|
|
if(strcmp(filter, "href") == 0) {
|
|
|
|
attrib = strstr(resource->url, value);
|
|
|
|
if(attrib == NULL || (value[-1] == '/' && attrib != resource->url)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
end = attrib + strlen(attrib);
|
2015-04-30 11:29:31 +00:00
|
|
|
} else if(resource->attributes != NULL) {
|
2014-04-22 08:31:43 +00:00
|
|
|
attrib = strstr(resource->attributes, filter);
|
|
|
|
if(attrib == NULL
|
|
|
|
|| (attrib[strlen(filter)] != '='
|
|
|
|
&& attrib[strlen(filter)] != '"')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
attrib += strlen(filter) + 2;
|
|
|
|
end = strchr(attrib, '"');
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Filter: res has attrib %s (%s)\n", attrib, value);
|
2014-04-22 08:31:43 +00:00
|
|
|
found = attrib;
|
|
|
|
while((found = strstr(found, value)) != NULL) {
|
|
|
|
if(found > end) {
|
|
|
|
found = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(lastchar == found[len - 1] || lastchar == '*') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++found;
|
|
|
|
}
|
|
|
|
if(found == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Filter: res has prefix %s\n", found);
|
2014-04-22 08:31:43 +00:00
|
|
|
if(lastchar != '*'
|
|
|
|
&& (found[len] != '"' && found[len] != ' ' && found[len] != '\0')) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Filter: res has match\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("/%s (%p)\npos: s%zu, o%ld, b%zu\n", resource->url, resource,
|
|
|
|
strpos, (long)*offset, bufpos);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
if(strpos > 0) {
|
2014-04-23 08:13:10 +00:00
|
|
|
ADD_CHAR_IF_POSSIBLE(',');
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
2014-04-23 08:13:10 +00:00
|
|
|
ADD_CHAR_IF_POSSIBLE('<');
|
|
|
|
ADD_CHAR_IF_POSSIBLE('/');
|
|
|
|
ADD_STRING_IF_POSSIBLE(resource->url, >=);
|
|
|
|
ADD_CHAR_IF_POSSIBLE('>');
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2015-04-30 11:29:31 +00:00
|
|
|
if(resource->attributes != NULL && resource->attributes[0]) {
|
2014-04-23 08:13:10 +00:00
|
|
|
ADD_CHAR_IF_POSSIBLE(';');
|
|
|
|
ADD_STRING_IF_POSSIBLE(resource->attributes, >);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* buffer full, but resource not completed yet; or: do not break if resource exactly fills buffer. */
|
|
|
|
if(bufpos > preferred_size && strpos - bufpos > *offset) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("BREAK at %s (%p)\n", resource->url, resource);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bufpos > 0) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("BUF %zu: ", bufpos);
|
|
|
|
LOG_DBG_COAP_STRING((char *)buffer, bufpos);
|
|
|
|
LOG_DBG_("\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_set_payload(response, buffer, bufpos);
|
|
|
|
coap_set_header_content_format(response, APPLICATION_LINK_FORMAT);
|
|
|
|
} else if(strpos > 0) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("well_known_core_handler(): bufpos<=0\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_set_status_code(response, BAD_OPTION_4_02);
|
|
|
|
coap_set_payload(response, "BlockOutOfScope", 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resource == NULL) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("DONE\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
*offset = -1;
|
|
|
|
} else {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("MORE at %s (%p)\n", resource->url, resource);
|
2014-04-22 08:31:43 +00:00
|
|
|
*offset += preferred_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
RESOURCE(res_well_known_core, "ct=40", well_known_core_get_handler, NULL,
|
|
|
|
NULL, NULL);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-21 17:54:28 +00:00
|
|
|
/** @} */
|