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
|
2017-10-24 23:46:28 +00:00
|
|
|
* CoAP implementation Engine.
|
2014-04-22 08:31:43 +00:00
|
|
|
* \author
|
|
|
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
|
|
|
*/
|
|
|
|
|
2017-12-21 17:54:28 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup coap
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
#include "coap-engine.h"
|
2014-12-01 12:58:34 +00:00
|
|
|
#include "sys/cc.h"
|
2017-10-24 23:46:28 +00:00
|
|
|
#include "lib/list.h"
|
2014-04-22 08:31:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-10-24 23:46:28 +00:00
|
|
|
#include <inttypes.h>
|
2014-04-22 08:31:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "coap-log.h"
|
2018-04-05 12:34:50 +00:00
|
|
|
#define LOG_MODULE "coap-eng"
|
2017-11-30 23:41:58 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_COAP
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
static void process_callback(coap_timer_t *t);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To be called by HTTP/COAP server as a callback function when a new service
|
|
|
|
* request appears. This function dispatches the corresponding CoAP service.
|
|
|
|
*/
|
2017-11-14 20:32:43 +00:00
|
|
|
static int invoke_coap_resource_service(coap_message_t *request,
|
|
|
|
coap_message_t *response,
|
2017-10-24 23:46:28 +00:00
|
|
|
uint8_t *buffer, uint16_t buffer_size,
|
|
|
|
int32_t *offset);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Variables ---------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
LIST(coap_handlers);
|
|
|
|
LIST(coap_resource_services);
|
|
|
|
static uint8_t is_initialized = 0;
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
/*- CoAP service handlers---------------------------------------------------*/
|
2014-04-22 08:31:43 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
void
|
|
|
|
coap_add_handler(coap_handler_t *handler)
|
|
|
|
{
|
|
|
|
list_add(coap_handlers, handler);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
coap_remove_handler(coap_handler_t *handler)
|
|
|
|
{
|
|
|
|
list_remove(coap_handlers, handler);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
coap_handler_status_t
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_call_handlers(coap_message_t *request, coap_message_t *response,
|
2017-10-24 23:46:28 +00:00
|
|
|
uint8_t *buffer, uint16_t buffer_size, int32_t *offset)
|
|
|
|
{
|
|
|
|
coap_handler_status_t status;
|
|
|
|
coap_handler_t *r;
|
|
|
|
for(r = list_head(coap_handlers); r != NULL; r = r->next) {
|
|
|
|
if(r->handler) {
|
|
|
|
status = r->handler(request, response, buffer, buffer_size, offset);
|
|
|
|
if(status != COAP_HANDLER_STATUS_CONTINUE) {
|
|
|
|
/* Request handled. */
|
|
|
|
|
|
|
|
/* Check response code before doing observe! */
|
|
|
|
if(request->code == COAP_GET) {
|
|
|
|
coap_observe_handler(NULL, request, response);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return COAP_HANDLER_STATUS_CONTINUE;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static CC_INLINE coap_handler_status_t
|
2017-11-14 20:32:43 +00:00
|
|
|
call_service(coap_message_t *request, coap_message_t *response,
|
2017-10-24 23:46:28 +00:00
|
|
|
uint8_t *buffer, uint16_t buffer_size, int32_t *offset)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_handler_status_t status;
|
|
|
|
status = coap_call_handlers(request, response, buffer, buffer_size, offset);
|
|
|
|
if(status != COAP_HANDLER_STATUS_CONTINUE) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
status = invoke_coap_resource_service(request, response, buffer, buffer_size, offset);
|
|
|
|
if(status != COAP_HANDLER_STATUS_CONTINUE) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
coap_set_status_code(response, NOT_FOUND_4_04);
|
|
|
|
|
|
|
|
return COAP_HANDLER_STATUS_CONTINUE;
|
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Server Part -------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* the discover resource is automatically included for CoAP */
|
|
|
|
extern coap_resource_t res_well_known_core;
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Internal API ------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
coap_receive(const coap_endpoint_t *src,
|
|
|
|
uint8_t *payload, uint16_t payload_length)
|
|
|
|
{
|
2014-04-22 08:31:43 +00:00
|
|
|
/* static declaration reduces stack peaks and program code size */
|
2017-11-14 20:32:43 +00:00
|
|
|
static coap_message_t message[1]; /* this way the message can be treated as pointer as usual */
|
|
|
|
static coap_message_t response[1];
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_transaction_t *transaction = NULL;
|
|
|
|
coap_handler_status_t status;
|
|
|
|
|
|
|
|
coap_status_code = coap_parse_message(message, payload, payload_length);
|
|
|
|
coap_set_src_endpoint(message, src);
|
|
|
|
|
|
|
|
if(coap_status_code == NO_ERROR) {
|
|
|
|
|
|
|
|
/*TODO duplicates suppression, if required by application */
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG(" Parsed: v %u, t %u, tkl %u, c %u, mid %u\n", message->version,
|
|
|
|
message->type, message->token_len, message->code, message->mid);
|
|
|
|
LOG_DBG(" URL:");
|
|
|
|
LOG_DBG_COAP_STRING(message->uri_path, message->uri_path_len);
|
|
|
|
LOG_DBG_("\n");
|
|
|
|
LOG_DBG(" Payload: ");
|
|
|
|
LOG_DBG_COAP_STRING((const char *)message->payload, message->payload_len);
|
|
|
|
LOG_DBG_("\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
/* handle requests */
|
|
|
|
if(message->code >= COAP_GET && message->code <= COAP_DELETE) {
|
|
|
|
|
|
|
|
/* use transaction buffer for response to confirmable request */
|
|
|
|
if((transaction = coap_new_transaction(message->mid, src))) {
|
|
|
|
uint32_t block_num = 0;
|
|
|
|
uint16_t block_size = COAP_MAX_BLOCK_SIZE;
|
|
|
|
uint32_t block_offset = 0;
|
|
|
|
int32_t new_offset = 0;
|
|
|
|
|
|
|
|
/* prepare response */
|
|
|
|
if(message->type == COAP_TYPE_CON) {
|
|
|
|
/* reliable CON requests are answered with an ACK */
|
|
|
|
coap_init_message(response, COAP_TYPE_ACK, CONTENT_2_05,
|
|
|
|
message->mid);
|
|
|
|
} else {
|
|
|
|
/* unreliable NON requests are answered with a NON as well */
|
|
|
|
coap_init_message(response, COAP_TYPE_NON, CONTENT_2_05,
|
|
|
|
coap_get_mid());
|
|
|
|
/* mirror token */
|
|
|
|
}
|
|
|
|
if(message->token_len) {
|
|
|
|
coap_set_token(response, message->token, message->token_len);
|
|
|
|
/* get offset for blockwise transfers */
|
|
|
|
}
|
|
|
|
if(coap_get_header_block2
|
|
|
|
(message, &block_num, NULL, &block_size, &block_offset)) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Blockwise: block request %"PRIu32" (%u/%u) @ %"PRIu32" bytes\n",
|
|
|
|
block_num, block_size, COAP_MAX_BLOCK_SIZE, block_offset);
|
2017-10-24 23:46:28 +00:00
|
|
|
block_size = MIN(block_size, COAP_MAX_BLOCK_SIZE);
|
|
|
|
new_offset = block_offset;
|
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* call CoAP framework and check if found and allowed */
|
|
|
|
status = call_service(message, response,
|
2017-11-14 20:32:43 +00:00
|
|
|
transaction->message + COAP_MAX_HEADER_SIZE,
|
2017-10-24 23:46:28 +00:00
|
|
|
block_size, &new_offset);
|
|
|
|
if(status != COAP_HANDLER_STATUS_CONTINUE) {
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
if(coap_status_code == NO_ERROR) {
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* TODO coap_handle_blockwise(request, response, start_offset, end_offset); */
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* resource is unaware of Block1 */
|
|
|
|
if(coap_is_option(message, COAP_OPTION_BLOCK1)
|
|
|
|
&& response->code < BAD_REQUEST_4_00
|
|
|
|
&& !coap_is_option(response, COAP_OPTION_BLOCK1)) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Block1 NOT IMPLEMENTED\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_status_code = NOT_IMPLEMENTED_5_01;
|
|
|
|
coap_error_message = "NoBlock1Support";
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* client requested Block2 transfer */
|
|
|
|
} else if(coap_is_option(message, COAP_OPTION_BLOCK2)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* unchanged new_offset indicates that resource is unaware of blockwise transfer */
|
|
|
|
if(new_offset == block_offset) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Blockwise: unaware resource with payload length %u/%u\n",
|
|
|
|
response->payload_len, block_size);
|
2017-10-24 23:46:28 +00:00
|
|
|
if(block_offset >= response->payload_len) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("handle_incoming_data(): block_offset >= response->payload_len\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
response->code = BAD_OPTION_4_02;
|
|
|
|
coap_set_payload(response, "BlockOutOfScope", 15); /* a const char str[] and sizeof(str) produces larger code size */
|
2014-04-22 08:31:43 +00:00
|
|
|
} else {
|
|
|
|
coap_set_header_block2(response, block_num,
|
2017-10-24 23:46:28 +00:00
|
|
|
response->payload_len -
|
|
|
|
block_offset > block_size,
|
|
|
|
block_size);
|
|
|
|
coap_set_payload(response,
|
|
|
|
response->payload + block_offset,
|
|
|
|
MIN(response->payload_len -
|
|
|
|
block_offset, block_size));
|
|
|
|
} /* if(valid offset) */
|
|
|
|
|
|
|
|
/* resource provides chunk-wise data */
|
|
|
|
} else {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Blockwise: blockwise resource, new offset %"PRId32"\n",
|
|
|
|
new_offset);
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_header_block2(response, block_num,
|
|
|
|
new_offset != -1
|
|
|
|
|| response->payload_len >
|
|
|
|
block_size, block_size);
|
|
|
|
|
|
|
|
if(response->payload_len > block_size) {
|
|
|
|
coap_set_payload(response, response->payload,
|
|
|
|
block_size);
|
|
|
|
}
|
|
|
|
} /* if(resource aware of blockwise) */
|
|
|
|
|
|
|
|
/* Resource requested Block2 transfer */
|
|
|
|
} else if(new_offset != 0) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Blockwise: no block option for blockwise resource, using block size %u\n",
|
|
|
|
COAP_MAX_BLOCK_SIZE);
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
coap_set_header_block2(response, 0, new_offset != -1,
|
|
|
|
COAP_MAX_BLOCK_SIZE);
|
|
|
|
coap_set_payload(response, response->payload,
|
|
|
|
MIN(response->payload_len,
|
|
|
|
COAP_MAX_BLOCK_SIZE));
|
|
|
|
} /* blockwise transfer handling */
|
|
|
|
} /* no errors/hooks */
|
|
|
|
/* successful service callback */
|
|
|
|
/* serialize response */
|
|
|
|
}
|
|
|
|
if(coap_status_code == NO_ERROR) {
|
2017-11-14 20:32:43 +00:00
|
|
|
if((transaction->message_len = coap_serialize_message(response,
|
2017-10-24 23:46:28 +00:00
|
|
|
transaction->
|
2017-11-14 20:32:43 +00:00
|
|
|
message)) ==
|
2017-10-24 23:46:28 +00:00
|
|
|
0) {
|
|
|
|
coap_status_code = PACKET_SERIALIZATION_ERROR;
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
} else {
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_status_code = SERVICE_UNAVAILABLE_5_03;
|
|
|
|
coap_error_message = "NoFreeTraBuffer";
|
|
|
|
} /* if(transaction buffer) */
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* handle responses */
|
|
|
|
} else {
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
if(message->type == COAP_TYPE_CON && message->code == 0) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_INFO("Received Ping\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_status_code = PING_RESPONSE;
|
|
|
|
} else if(message->type == COAP_TYPE_ACK) {
|
|
|
|
/* transactions are closed through lookup below */
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Received ACK\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
} else if(message->type == COAP_TYPE_RST) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_INFO("Received RST\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
/* cancel possible subscriptions */
|
|
|
|
coap_remove_observer_by_mid(src, message->mid);
|
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
if((transaction = coap_get_transaction_by_mid(message->mid))) {
|
|
|
|
/* free transaction memory before callback, as it may create a new transaction */
|
|
|
|
coap_resource_response_handler_t callback = transaction->callback;
|
|
|
|
void *callback_data = transaction->callback_data;
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_clear_transaction(transaction);
|
|
|
|
|
|
|
|
/* check if someone registered for the response */
|
|
|
|
if(callback) {
|
|
|
|
callback(callback_data, message);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
|
|
|
/* if(ACKed transaction) */
|
|
|
|
transaction = NULL;
|
2015-02-22 16:01:46 +00:00
|
|
|
|
|
|
|
#if COAP_OBSERVE_CLIENT
|
2017-10-24 23:46:28 +00:00
|
|
|
/* if observe notification */
|
|
|
|
if((message->type == COAP_TYPE_CON || message->type == COAP_TYPE_NON)
|
|
|
|
&& coap_is_option(message, COAP_OPTION_OBSERVE)) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Observe [%"PRId32"]\n", message->observe);
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_handle_notification(src, message);
|
|
|
|
}
|
2015-02-22 16:01:46 +00:00
|
|
|
#endif /* COAP_OBSERVE_CLIENT */
|
2017-10-24 23:46:28 +00:00
|
|
|
} /* request or response */
|
|
|
|
} /* parsed correctly */
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* if(parsed correctly) */
|
2017-10-24 23:46:28 +00:00
|
|
|
if(coap_status_code == NO_ERROR) {
|
|
|
|
if(transaction) {
|
|
|
|
coap_send_transaction(transaction);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
} else if(coap_status_code == MANUAL_RESPONSE) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Clearing transaction for manual response");
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_clear_transaction(transaction);
|
|
|
|
} else {
|
|
|
|
coap_message_type_t reply_type = COAP_TYPE_ACK;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_WARN("ERROR %u: %s\n", coap_status_code, coap_error_message);
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_clear_transaction(transaction);
|
|
|
|
|
|
|
|
if(coap_status_code == PING_RESPONSE) {
|
|
|
|
coap_status_code = 0;
|
|
|
|
reply_type = COAP_TYPE_RST;
|
|
|
|
} else if(coap_status_code >= 192) {
|
|
|
|
/* set to sendable error code */
|
|
|
|
coap_status_code = INTERNAL_SERVER_ERROR_5_00;
|
|
|
|
/* reuse input buffer for error message */
|
|
|
|
}
|
|
|
|
coap_init_message(message, reply_type, coap_status_code,
|
|
|
|
message->mid);
|
|
|
|
coap_set_payload(message, coap_error_message,
|
|
|
|
strlen(coap_error_message));
|
2017-11-09 13:42:45 +00:00
|
|
|
coap_sendto(src, payload, coap_serialize_message(message, payload));
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if(new data) */
|
2017-10-24 23:46:28 +00:00
|
|
|
return coap_status_code;
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_engine_init(void)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
/* avoid initializing twice */
|
|
|
|
if(is_initialized) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("already running - double initialization?\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
is_initialized = 1;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_INFO("Starting CoAP engine...\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
list_init(coap_handlers);
|
|
|
|
list_init(coap_resource_services);
|
|
|
|
|
|
|
|
coap_activate_resource(&res_well_known_core, ".well-known/core");
|
|
|
|
|
|
|
|
coap_transport_init();
|
|
|
|
coap_init_connection();
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
/**
|
|
|
|
* \brief Makes a resource available under the given URI path
|
|
|
|
* \param resource A pointer to a resource implementation
|
|
|
|
* \param path The URI path string for this resource
|
|
|
|
*
|
|
|
|
* The resource implementation must be imported first using the
|
|
|
|
* extern keyword. The build system takes care of compiling every
|
|
|
|
* *.c file in the ./resources/ sub-directory (see example Makefile).
|
|
|
|
*/
|
2014-04-22 08:31:43 +00:00
|
|
|
void
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_activate_resource(coap_resource_t *resource, const char *path)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_periodic_resource_t *periodic;
|
|
|
|
resource->url = path;
|
|
|
|
list_add(coap_resource_services, resource);
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_INFO("Activating: %s\n", resource->url);
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
/* Only add periodic resources with a periodic_handler and a period > 0. */
|
|
|
|
if(resource->flags & IS_PERIODIC && resource->periodic
|
|
|
|
&& resource->periodic->periodic_handler
|
|
|
|
&& resource->periodic->period) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Periodic resource: %p (%s)\n", resource->periodic, path);
|
2017-10-24 23:46:28 +00:00
|
|
|
periodic = resource->periodic;
|
|
|
|
coap_timer_set_callback(&periodic->periodic_timer, process_callback);
|
|
|
|
coap_timer_set_user_data(&periodic->periodic_timer, resource);
|
|
|
|
coap_timer_set(&periodic->periodic_timer, periodic->period);
|
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
/*- Internal API ------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
coap_resource_t *
|
|
|
|
coap_get_first_resource(void)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
return list_head(coap_resource_services);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_resource_t *
|
|
|
|
coap_get_next_resource(coap_resource_t *resource)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
return list_item_next(resource);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
static int
|
2017-11-14 20:32:43 +00:00
|
|
|
invoke_coap_resource_service(coap_message_t *request, coap_message_t *response,
|
2017-10-24 23:46:28 +00:00
|
|
|
uint8_t *buffer, uint16_t buffer_size,
|
|
|
|
int32_t *offset)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
uint8_t found = 0;
|
|
|
|
uint8_t allowed = 1;
|
|
|
|
|
|
|
|
coap_resource_t *resource = NULL;
|
|
|
|
const char *url = NULL;
|
|
|
|
int url_len, res_url_len;
|
|
|
|
|
|
|
|
url_len = coap_get_header_uri_path(request, &url);
|
|
|
|
for(resource = list_head(coap_resource_services);
|
|
|
|
resource; resource = resource->next) {
|
|
|
|
|
|
|
|
/* if the web service handles that kind of requests and urls matches */
|
|
|
|
res_url_len = strlen(resource->url);
|
|
|
|
if((url_len == res_url_len
|
|
|
|
|| (url_len > res_url_len
|
|
|
|
&& (resource->flags & HAS_SUB_RESOURCES)
|
|
|
|
&& url[res_url_len] == '/'))
|
|
|
|
&& strncmp(resource->url, url, res_url_len) == 0) {
|
|
|
|
coap_resource_flags_t method = coap_get_method_type(request);
|
|
|
|
found = 1;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_INFO("/%s, method %u, resource->flags %u\n", resource->url,
|
|
|
|
(uint16_t)method, resource->flags);
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
if((method & METHOD_GET) && resource->get_handler != NULL) {
|
|
|
|
/* call handler function */
|
|
|
|
resource->get_handler(request, response, buffer, buffer_size, offset);
|
|
|
|
} else if((method & METHOD_POST) && resource->post_handler != NULL) {
|
|
|
|
/* call handler function */
|
|
|
|
resource->post_handler(request, response, buffer, buffer_size,
|
|
|
|
offset);
|
|
|
|
} else if((method & METHOD_PUT) && resource->put_handler != NULL) {
|
|
|
|
/* call handler function */
|
|
|
|
resource->put_handler(request, response, buffer, buffer_size, offset);
|
|
|
|
} else if((method & METHOD_DELETE) && resource->delete_handler != NULL) {
|
|
|
|
/* call handler function */
|
|
|
|
resource->delete_handler(request, response, buffer, buffer_size,
|
|
|
|
offset);
|
2014-04-22 08:31:43 +00:00
|
|
|
} else {
|
2017-10-24 23:46:28 +00:00
|
|
|
allowed = 0;
|
|
|
|
coap_set_status_code(response, METHOD_NOT_ALLOWED_4_05);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
break;
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
|
|
|
if(!found) {
|
|
|
|
coap_set_status_code(response, NOT_FOUND_4_04);
|
|
|
|
} else if(allowed) {
|
|
|
|
/* final handler for special flags */
|
|
|
|
if(resource->flags & IS_OBSERVABLE) {
|
|
|
|
coap_observe_handler(resource, request, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return found & allowed;
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
/* This callback occurs when t is expired */
|
|
|
|
static void
|
|
|
|
process_callback(coap_timer_t *t)
|
|
|
|
{
|
|
|
|
coap_resource_t *resource;
|
|
|
|
resource = coap_timer_get_user_data(t);
|
|
|
|
if(resource != NULL && (resource->flags & IS_PERIODIC)
|
|
|
|
&& resource->periodic != NULL && resource->periodic->period) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Periodic: timer expired for /%s (period: %"PRIu32")\n",
|
|
|
|
resource->url, resource->periodic->period);
|
2017-10-24 23:46:28 +00:00
|
|
|
|
|
|
|
if(!is_initialized) {
|
|
|
|
/* CoAP has not yet been initialized. */
|
|
|
|
} else if(resource->periodic->periodic_handler) {
|
|
|
|
/* Call the periodic_handler function. */
|
|
|
|
resource->periodic->periodic_handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
coap_timer_set(t, resource->periodic->period);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-12-21 17:54:28 +00:00
|
|
|
/** @} */
|