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
|
|
|
|
* An implementation of the Constrained Application Protocol (RFC).
|
|
|
|
* \author
|
|
|
|
* Matthias Kovatsch <kovatsch@inf.ethz.ch>
|
2017-11-14 20:32:43 +00:00
|
|
|
*
|
|
|
|
* Joakim Eriksson, joakim.eriksson@ri.se
|
|
|
|
* Niclas Finne, niclas.finne@ri.se
|
2014-04-22 08:31:43 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-14 20:32:43 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup coap
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2014-04-22 08:31:43 +00:00
|
|
|
#include <string.h>
|
2017-10-24 23:46:28 +00:00
|
|
|
#include <stdlib.h>
|
2018-07-26 09:54:57 +00:00
|
|
|
#include <inttypes.h>
|
2014-12-01 12:58:34 +00:00
|
|
|
#include "sys/cc.h"
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-09-01 14:37:51 +00:00
|
|
|
#include "coap.h"
|
|
|
|
#include "coap-transactions.h"
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "coap-log.h"
|
|
|
|
#define LOG_MODULE "coap"
|
|
|
|
#define LOG_LEVEL LOG_LEVEL_COAP
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Variables ---------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static uint16_t current_mid = 0;
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_status_t coap_status_code = NO_ERROR;
|
|
|
|
const char *coap_error_message = "";
|
2014-04-22 08:31:43 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Local helper functions --------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static uint16_t
|
|
|
|
coap_log_2(uint16_t value)
|
|
|
|
{
|
|
|
|
uint16_t result = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
value = value >> 1;
|
|
|
|
result++;
|
|
|
|
} while(value);
|
|
|
|
|
|
|
|
return result ? result - 1 : result;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static uint32_t
|
|
|
|
coap_parse_int_option(uint8_t *bytes, size_t length)
|
|
|
|
{
|
|
|
|
uint32_t var = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while(i < length) {
|
|
|
|
var <<= 8;
|
|
|
|
var |= bytes[i++];
|
|
|
|
}
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static uint8_t
|
|
|
|
coap_option_nibble(unsigned int value)
|
|
|
|
{
|
|
|
|
if(value < 13) {
|
|
|
|
return value;
|
|
|
|
} else if(value <= 0xFF + 13) {
|
|
|
|
return 13;
|
|
|
|
} else {
|
|
|
|
return 14;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
|
|
|
coap_set_option_header(unsigned int delta, size_t length, uint8_t *buffer)
|
|
|
|
{
|
|
|
|
size_t written = 0;
|
|
|
|
|
|
|
|
buffer[0] = coap_option_nibble(delta) << 4 | coap_option_nibble(length);
|
|
|
|
|
2015-04-30 10:16:04 +00:00
|
|
|
if(delta > 268) {
|
|
|
|
buffer[++written] = ((delta - 269) >> 8) & 0xff;
|
|
|
|
buffer[++written] = (delta - 269) & 0xff;
|
|
|
|
} else if(delta > 12) {
|
|
|
|
buffer[++written] = (delta - 13);
|
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2015-04-30 10:16:04 +00:00
|
|
|
if(length > 268) {
|
|
|
|
buffer[++written] = ((length - 269) >> 8) & 0xff;
|
|
|
|
buffer[++written] = (length - 269) & 0xff;
|
|
|
|
} else if(length > 12) {
|
|
|
|
buffer[++written] = (length - 13);
|
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("WRITTEN %zu B opt header\n", 1 + written);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
return ++written;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
|
|
|
coap_serialize_int_option(unsigned int number, unsigned int current_number,
|
|
|
|
uint8_t *buffer, uint32_t value)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
if(0xFF000000 & value) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if(0xFFFF0000 & value) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if(0xFFFFFF00 & value) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if(0xFFFFFFFF & value) {
|
|
|
|
++i;
|
|
|
|
}
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("OPTION %u (delta %u, len %zu)\n", number, number - current_number,
|
|
|
|
i);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
i = coap_set_option_header(number - current_number, i, buffer);
|
|
|
|
|
|
|
|
if(0xFF000000 & value) {
|
|
|
|
buffer[i++] = (uint8_t)(value >> 24);
|
|
|
|
}
|
|
|
|
if(0xFFFF0000 & value) {
|
|
|
|
buffer[i++] = (uint8_t)(value >> 16);
|
|
|
|
}
|
|
|
|
if(0xFFFFFF00 & value) {
|
|
|
|
buffer[i++] = (uint8_t)(value >> 8);
|
|
|
|
}
|
|
|
|
if(0xFFFFFFFF & value) {
|
|
|
|
buffer[i++] = (uint8_t)(value);
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
|
|
|
coap_serialize_array_option(unsigned int number, unsigned int current_number,
|
|
|
|
uint8_t *buffer, uint8_t *array, size_t length,
|
|
|
|
char split_char)
|
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("ARRAY type %u, len %zu, full [", number, length);
|
|
|
|
LOG_DBG_COAP_STRING((const char *)array, length);
|
|
|
|
LOG_DBG_("]\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
if(split_char != '\0') {
|
|
|
|
int j;
|
|
|
|
uint8_t *part_start = array;
|
|
|
|
uint8_t *part_end = NULL;
|
|
|
|
size_t temp_length;
|
|
|
|
|
|
|
|
for(j = 0; j <= length + 1; ++j) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("STEP %u/%zu (%c)\n", j, length, array[j]);
|
2014-04-22 08:31:43 +00:00
|
|
|
if(array[j] == split_char || j == length) {
|
|
|
|
part_end = array + j;
|
|
|
|
temp_length = part_end - part_start;
|
|
|
|
|
|
|
|
i += coap_set_option_header(number - current_number, temp_length,
|
|
|
|
&buffer[i]);
|
|
|
|
memcpy(&buffer[i], part_start, temp_length);
|
|
|
|
i += temp_length;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("OPTION type %u, delta %u, len %zu, part [", number,
|
|
|
|
number - current_number, i);
|
|
|
|
LOG_DBG_COAP_STRING((const char *)part_start, temp_length);
|
|
|
|
LOG_DBG_("]\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
++j; /* skip the splitter */
|
|
|
|
current_number = number;
|
|
|
|
part_start = array + j;
|
|
|
|
}
|
|
|
|
} /* for */
|
|
|
|
} else {
|
|
|
|
i += coap_set_option_header(number - current_number, length, &buffer[i]);
|
|
|
|
memcpy(&buffer[i], array, length);
|
|
|
|
i += length;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("OPTION type %u, delta %u, len %zu\n", number,
|
|
|
|
number - current_number, length);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
coap_merge_multi_option(char **dst, size_t *dst_len, uint8_t *option,
|
|
|
|
size_t option_len, char separator)
|
|
|
|
{
|
|
|
|
/* merge multiple options */
|
|
|
|
if(*dst_len > 0) {
|
|
|
|
/* dst already contains an option: concatenate */
|
|
|
|
(*dst)[*dst_len] = separator;
|
|
|
|
*dst_len += 1;
|
|
|
|
|
|
|
|
/* memmove handles 2-byte option headers */
|
|
|
|
memmove((*dst) + (*dst_len), option, option_len);
|
|
|
|
|
|
|
|
*dst_len += option_len;
|
|
|
|
} else {
|
|
|
|
/* dst is empty: set to option */
|
|
|
|
*dst = (char *)option;
|
|
|
|
*dst_len = option_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
coap_get_variable(const char *buffer, size_t length, const char *name,
|
|
|
|
const char **output)
|
|
|
|
{
|
|
|
|
const char *start = NULL;
|
|
|
|
const char *end = NULL;
|
|
|
|
const char *value_end = NULL;
|
|
|
|
size_t name_len = 0;
|
|
|
|
|
|
|
|
/*initialize the output buffer first */
|
|
|
|
*output = 0;
|
|
|
|
|
|
|
|
name_len = strlen(name);
|
|
|
|
end = buffer + length;
|
|
|
|
|
|
|
|
for(start = buffer; start + name_len < end; ++start) {
|
|
|
|
if((start == buffer || start[-1] == '&') && start[name_len] == '='
|
|
|
|
&& strncmp(name, start, name_len) == 0) {
|
|
|
|
|
|
|
|
/* Point start to variable value */
|
|
|
|
start += name_len + 1;
|
|
|
|
|
|
|
|
/* Point end to the end of the value */
|
|
|
|
value_end = (const char *)memchr(start, '&', end - start);
|
|
|
|
if(value_end == NULL) {
|
|
|
|
value_end = end;
|
|
|
|
}
|
|
|
|
*output = start;
|
|
|
|
|
|
|
|
return value_end - start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*- Internal API ------------------------------------------------------------*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_init_connection(void)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
/* initialize transaction ID */
|
2017-10-24 23:46:28 +00:00
|
|
|
current_mid = rand();
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
uint16_t
|
|
|
|
coap_get_mid()
|
|
|
|
{
|
|
|
|
return ++current_mid;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_init_message(coap_message_t *coap_pkt, coap_message_type_t type,
|
2017-10-24 23:46:28 +00:00
|
|
|
uint8_t code, uint16_t mid)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
/* Important thing */
|
2017-11-14 20:32:43 +00:00
|
|
|
memset(coap_pkt, 0, sizeof(coap_message_t));
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_pkt->type = type;
|
|
|
|
coap_pkt->code = code;
|
|
|
|
coap_pkt->mid = mid;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
size_t
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_serialize_message(coap_message_t *coap_pkt, uint8_t *buffer)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
uint8_t *option;
|
|
|
|
unsigned int current_number = 0;
|
|
|
|
|
|
|
|
/* Initialize */
|
|
|
|
coap_pkt->buffer = buffer;
|
|
|
|
coap_pkt->version = 1;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("-Serializing MID %u to %p, ", coap_pkt->mid, coap_pkt->buffer);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* set header fields */
|
|
|
|
coap_pkt->buffer[0] = 0x00;
|
|
|
|
coap_pkt->buffer[0] |= COAP_HEADER_VERSION_MASK
|
|
|
|
& (coap_pkt->version) << COAP_HEADER_VERSION_POSITION;
|
|
|
|
coap_pkt->buffer[0] |= COAP_HEADER_TYPE_MASK
|
|
|
|
& (coap_pkt->type) << COAP_HEADER_TYPE_POSITION;
|
|
|
|
coap_pkt->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK
|
|
|
|
& (coap_pkt->token_len) << COAP_HEADER_TOKEN_LEN_POSITION;
|
|
|
|
coap_pkt->buffer[1] = coap_pkt->code;
|
|
|
|
coap_pkt->buffer[2] = (uint8_t)((coap_pkt->mid) >> 8);
|
|
|
|
coap_pkt->buffer[3] = (uint8_t)(coap_pkt->mid);
|
|
|
|
|
2017-11-14 20:32:43 +00:00
|
|
|
/* empty message, dont need to do more stuff */
|
2014-04-23 08:13:10 +00:00
|
|
|
if(!coap_pkt->code) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("-Done serializing empty message at %p-\n", coap_pkt->buffer);
|
2014-04-23 08:13:10 +00:00
|
|
|
return 4;
|
|
|
|
}
|
2014-05-15 17:56:29 +00:00
|
|
|
|
2014-04-22 08:31:43 +00:00
|
|
|
/* set Token */
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Token (len %u)", coap_pkt->token_len);
|
2014-04-22 08:31:43 +00:00
|
|
|
option = coap_pkt->buffer + COAP_HEADER_LEN;
|
|
|
|
for(current_number = 0; current_number < coap_pkt->token_len;
|
|
|
|
++current_number) {
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_(" %02X", coap_pkt->token[current_number]);
|
2014-04-22 08:31:43 +00:00
|
|
|
*option = coap_pkt->token[current_number];
|
|
|
|
++option;
|
|
|
|
}
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("-\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* Serialize options */
|
|
|
|
current_number = 0;
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("-Serializing options at %p-\n", option);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* The options must be serialized in the order of their number */
|
|
|
|
COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_IF_MATCH, if_match, "If-Match");
|
|
|
|
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_HOST, uri_host, '\0',
|
|
|
|
"Uri-Host");
|
|
|
|
COAP_SERIALIZE_BYTE_OPTION(COAP_OPTION_ETAG, etag, "ETag");
|
|
|
|
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_IF_NONE_MATCH,
|
|
|
|
content_format -
|
|
|
|
coap_pkt->
|
|
|
|
content_format /* hack to get a zero field */,
|
|
|
|
"If-None-Match");
|
|
|
|
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_OBSERVE, observe, "Observe");
|
|
|
|
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_URI_PORT, uri_port, "Uri-Port");
|
|
|
|
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_PATH, location_path, '/',
|
|
|
|
"Location-Path");
|
|
|
|
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_PATH, uri_path, '/',
|
|
|
|
"Uri-Path");
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Serialize content format: %d\n", coap_pkt->content_format);
|
2014-04-22 08:31:43 +00:00
|
|
|
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_CONTENT_FORMAT, content_format,
|
|
|
|
"Content-Format");
|
|
|
|
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_MAX_AGE, max_age, "Max-Age");
|
|
|
|
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_URI_QUERY, uri_query, '&',
|
|
|
|
"Uri-Query");
|
|
|
|
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_ACCEPT, accept, "Accept");
|
|
|
|
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_LOCATION_QUERY, location_query,
|
|
|
|
'&', "Location-Query");
|
|
|
|
COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK2, block2, "Block2");
|
|
|
|
COAP_SERIALIZE_BLOCK_OPTION(COAP_OPTION_BLOCK1, block1, "Block1");
|
|
|
|
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE2, size2, "Size2");
|
|
|
|
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_URI, proxy_uri, '\0',
|
|
|
|
"Proxy-Uri");
|
|
|
|
COAP_SERIALIZE_STRING_OPTION(COAP_OPTION_PROXY_SCHEME, proxy_scheme, '\0',
|
|
|
|
"Proxy-Scheme");
|
|
|
|
COAP_SERIALIZE_INT_OPTION(COAP_OPTION_SIZE1, size1, "Size1");
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("-Done serializing at %p----\n", option);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* Pack payload */
|
|
|
|
if((option - coap_pkt->buffer) <= COAP_MAX_HEADER_SIZE) {
|
|
|
|
/* Payload marker */
|
|
|
|
if(coap_pkt->payload_len) {
|
|
|
|
*option = 0xFF;
|
|
|
|
++option;
|
|
|
|
}
|
|
|
|
memmove(option, coap_pkt->payload, coap_pkt->payload_len);
|
|
|
|
} else {
|
|
|
|
/* an error occurred: caller must check for !=0 */
|
|
|
|
coap_pkt->buffer = NULL;
|
|
|
|
coap_error_message = "Serialized header exceeds COAP_MAX_HEADER_SIZE";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("-Done %u B (header len %u, payload len %u)-\n",
|
|
|
|
(unsigned int)(coap_pkt->payload_len + option - buffer),
|
|
|
|
(unsigned int)(option - buffer),
|
|
|
|
(unsigned int)coap_pkt->payload_len);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Dump [0x%02X %02X %02X %02X %02X %02X %02X %02X]\n",
|
|
|
|
coap_pkt->buffer[0], coap_pkt->buffer[1], coap_pkt->buffer[2],
|
|
|
|
coap_pkt->buffer[3], coap_pkt->buffer[4], coap_pkt->buffer[5],
|
|
|
|
coap_pkt->buffer[6], coap_pkt->buffer[7]);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-11-14 20:32:43 +00:00
|
|
|
return (option - buffer) + coap_pkt->payload_len; /* message length */
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
coap_status_t
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_parse_message(coap_message_t *coap_pkt, uint8_t *data, uint16_t data_len)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-11-14 20:32:43 +00:00
|
|
|
/* initialize message */
|
|
|
|
memset(coap_pkt, 0, sizeof(coap_message_t));
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-11-14 20:32:43 +00:00
|
|
|
/* pointer to message bytes */
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_pkt->buffer = data;
|
|
|
|
|
|
|
|
/* parse header fields */
|
|
|
|
coap_pkt->version = (COAP_HEADER_VERSION_MASK & coap_pkt->buffer[0])
|
|
|
|
>> COAP_HEADER_VERSION_POSITION;
|
|
|
|
coap_pkt->type = (COAP_HEADER_TYPE_MASK & coap_pkt->buffer[0])
|
|
|
|
>> COAP_HEADER_TYPE_POSITION;
|
2015-09-05 15:16:29 +00:00
|
|
|
coap_pkt->token_len = (COAP_HEADER_TOKEN_LEN_MASK & coap_pkt->buffer[0])
|
|
|
|
>> COAP_HEADER_TOKEN_LEN_POSITION;
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_pkt->code = coap_pkt->buffer[1];
|
|
|
|
coap_pkt->mid = coap_pkt->buffer[2] << 8 | coap_pkt->buffer[3];
|
|
|
|
|
|
|
|
if(coap_pkt->version != 1) {
|
|
|
|
coap_error_message = "CoAP version must be 1";
|
|
|
|
return BAD_REQUEST_4_00;
|
|
|
|
}
|
|
|
|
|
2015-09-05 15:16:29 +00:00
|
|
|
if(coap_pkt->token_len > COAP_TOKEN_LEN) {
|
|
|
|
coap_error_message = "Token Length must not be more than 8";
|
|
|
|
return BAD_REQUEST_4_00;
|
|
|
|
}
|
|
|
|
|
2014-04-22 08:31:43 +00:00
|
|
|
uint8_t *current_option = data + COAP_HEADER_LEN;
|
|
|
|
|
|
|
|
memcpy(coap_pkt->token, current_option, coap_pkt->token_len);
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("Token (len %u) [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
|
|
|
|
coap_pkt->token_len, coap_pkt->token[0], coap_pkt->token[1],
|
|
|
|
coap_pkt->token[2], coap_pkt->token[3], coap_pkt->token[4],
|
|
|
|
coap_pkt->token[5], coap_pkt->token[6], coap_pkt->token[7]
|
|
|
|
); /* FIXME always prints 8 bytes */
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
/* parse options */
|
|
|
|
memset(coap_pkt->options, 0, sizeof(coap_pkt->options));
|
|
|
|
current_option += coap_pkt->token_len;
|
|
|
|
|
|
|
|
unsigned int option_number = 0;
|
|
|
|
unsigned int option_delta = 0;
|
|
|
|
size_t option_length = 0;
|
|
|
|
|
|
|
|
while(current_option < data + data_len) {
|
|
|
|
/* payload marker 0xFF, currently only checking for 0xF* because rest is reserved */
|
|
|
|
if((current_option[0] & 0xF0) == 0xF0) {
|
|
|
|
coap_pkt->payload = ++current_option;
|
|
|
|
coap_pkt->payload_len = data_len - (coap_pkt->payload - data);
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
/* also for receiving, the Erbium upper bound is COAP_MAX_CHUNK_SIZE */
|
|
|
|
if(coap_pkt->payload_len > COAP_MAX_CHUNK_SIZE) {
|
|
|
|
coap_pkt->payload_len = COAP_MAX_CHUNK_SIZE;
|
2014-04-22 08:31:43 +00:00
|
|
|
/* null-terminate payload */
|
|
|
|
}
|
|
|
|
coap_pkt->payload[coap_pkt->payload_len] = '\0';
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
option_delta = current_option[0] >> 4;
|
|
|
|
option_length = current_option[0] & 0x0F;
|
|
|
|
++current_option;
|
|
|
|
|
2015-04-30 10:16:04 +00:00
|
|
|
if(option_delta == 13) {
|
|
|
|
option_delta += current_option[0];
|
|
|
|
++current_option;
|
|
|
|
} else if(option_delta == 14) {
|
|
|
|
option_delta += 255;
|
|
|
|
option_delta += current_option[0] << 8;
|
|
|
|
++current_option;
|
|
|
|
option_delta += current_option[0];
|
|
|
|
++current_option;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(option_length == 13) {
|
|
|
|
option_length += current_option[0];
|
|
|
|
++current_option;
|
|
|
|
} else if(option_length == 14) {
|
|
|
|
option_length += 255;
|
|
|
|
option_length += current_option[0] << 8;
|
|
|
|
++current_option;
|
|
|
|
option_length += current_option[0];
|
|
|
|
++current_option;
|
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-06-01 15:33:34 +00:00
|
|
|
if(current_option + option_length > data + data_len) {
|
|
|
|
/* Malformed CoAP - out of bounds */
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_WARN("BAD REQUEST: options outside data message: %u > %u\n",
|
|
|
|
(unsigned)(current_option + option_length - data), data_len);
|
2017-06-01 15:33:34 +00:00
|
|
|
return BAD_REQUEST_4_00;
|
|
|
|
}
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
option_number += option_delta;
|
2017-06-01 15:33:34 +00:00
|
|
|
|
|
|
|
if(option_number > COAP_OPTION_SIZE1) {
|
|
|
|
/* Malformed CoAP - out of bounds */
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_WARN("BAD REQUEST: option number too large: %u\n", option_number);
|
2017-06-01 15:33:34 +00:00
|
|
|
return BAD_REQUEST_4_00;
|
|
|
|
}
|
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("OPTION %u (delta %u, len %zu): ", option_number, option_delta,
|
|
|
|
option_length);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, option_number);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
switch(option_number) {
|
|
|
|
case COAP_OPTION_CONTENT_FORMAT:
|
|
|
|
coap_pkt->content_format = coap_parse_int_option(current_option,
|
|
|
|
option_length);
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Content-Format [%u]\n", coap_pkt->content_format);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_MAX_AGE:
|
|
|
|
coap_pkt->max_age = coap_parse_int_option(current_option,
|
|
|
|
option_length);
|
2018-02-23 12:47:18 +00:00
|
|
|
LOG_DBG_("Max-Age [%"PRIu32"]\n", coap_pkt->max_age);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_ETAG:
|
|
|
|
coap_pkt->etag_len = MIN(COAP_ETAG_LEN, option_length);
|
|
|
|
memcpy(coap_pkt->etag, current_option, coap_pkt->etag_len);
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("ETag %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
|
|
|
|
coap_pkt->etag_len, coap_pkt->etag[0], coap_pkt->etag[1],
|
|
|
|
coap_pkt->etag[2], coap_pkt->etag[3], coap_pkt->etag[4],
|
|
|
|
coap_pkt->etag[5], coap_pkt->etag[6], coap_pkt->etag[7]
|
|
|
|
); /*FIXME always prints 8 bytes */
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_ACCEPT:
|
|
|
|
coap_pkt->accept = coap_parse_int_option(current_option, option_length);
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Accept [%u]\n", coap_pkt->accept);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_IF_MATCH:
|
|
|
|
/* TODO support multiple ETags */
|
|
|
|
coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, option_length);
|
|
|
|
memcpy(coap_pkt->if_match, current_option, coap_pkt->if_match_len);
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("If-Match %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]\n",
|
|
|
|
coap_pkt->if_match_len, coap_pkt->if_match[0],
|
|
|
|
coap_pkt->if_match[1], coap_pkt->if_match[2],
|
|
|
|
coap_pkt->if_match[3], coap_pkt->if_match[4],
|
|
|
|
coap_pkt->if_match[5], coap_pkt->if_match[6],
|
|
|
|
coap_pkt->if_match[7]
|
|
|
|
); /* FIXME always prints 8 bytes */
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_IF_NONE_MATCH:
|
|
|
|
coap_pkt->if_none_match = 1;
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("If-None-Match\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COAP_OPTION_PROXY_URI:
|
|
|
|
#if COAP_PROXY_OPTION_PROCESSING
|
|
|
|
coap_pkt->proxy_uri = (char *)current_option;
|
|
|
|
coap_pkt->proxy_uri_len = option_length;
|
2017-11-30 23:41:58 +00:00
|
|
|
#endif /* COAP_PROXY_OPTION_PROCESSING */
|
|
|
|
LOG_DBG_("Proxy-Uri NOT IMPLEMENTED [");
|
|
|
|
LOG_DBG_COAP_STRING(coap_pkt->proxy_uri, coap_pkt->proxy_uri_len);
|
|
|
|
LOG_DBG_("]\n");
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_error_message = "This is a constrained server (Contiki)";
|
|
|
|
return PROXYING_NOT_SUPPORTED_5_05;
|
|
|
|
break;
|
|
|
|
case COAP_OPTION_PROXY_SCHEME:
|
|
|
|
#if COAP_PROXY_OPTION_PROCESSING
|
|
|
|
coap_pkt->proxy_scheme = (char *)current_option;
|
|
|
|
coap_pkt->proxy_scheme_len = option_length;
|
|
|
|
#endif
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Proxy-Scheme NOT IMPLEMENTED [");
|
|
|
|
LOG_DBG_COAP_STRING(coap_pkt->proxy_scheme, coap_pkt->proxy_scheme_len);
|
|
|
|
LOG_DBG_("]\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_error_message = "This is a constrained server (Contiki)";
|
|
|
|
return PROXYING_NOT_SUPPORTED_5_05;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COAP_OPTION_URI_HOST:
|
|
|
|
coap_pkt->uri_host = (char *)current_option;
|
|
|
|
coap_pkt->uri_host_len = option_length;
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Uri-Host [");
|
|
|
|
LOG_DBG_COAP_STRING(coap_pkt->uri_host, coap_pkt->uri_host_len);
|
|
|
|
LOG_DBG_("]\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_URI_PORT:
|
|
|
|
coap_pkt->uri_port = coap_parse_int_option(current_option,
|
|
|
|
option_length);
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Uri-Port [%u]\n", coap_pkt->uri_port);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_URI_PATH:
|
2017-11-14 20:32:43 +00:00
|
|
|
/* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_merge_multi_option((char **)&(coap_pkt->uri_path),
|
|
|
|
&(coap_pkt->uri_path_len), current_option,
|
|
|
|
option_length, '/');
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Uri-Path [");
|
|
|
|
LOG_DBG_COAP_STRING(coap_pkt->uri_path, coap_pkt->uri_path_len);
|
|
|
|
LOG_DBG_("]\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_URI_QUERY:
|
2017-11-14 20:32:43 +00:00
|
|
|
/* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_merge_multi_option((char **)&(coap_pkt->uri_query),
|
|
|
|
&(coap_pkt->uri_query_len), current_option,
|
|
|
|
option_length, '&');
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Uri-Query[");
|
|
|
|
LOG_DBG_COAP_STRING(coap_pkt->uri_query, coap_pkt->uri_query_len);
|
|
|
|
LOG_DBG_("]\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COAP_OPTION_LOCATION_PATH:
|
2017-11-14 20:32:43 +00:00
|
|
|
/* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_merge_multi_option((char **)&(coap_pkt->location_path),
|
|
|
|
&(coap_pkt->location_path_len), current_option,
|
|
|
|
option_length, '/');
|
2017-10-24 23:46:28 +00:00
|
|
|
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Location-Path [");
|
|
|
|
LOG_DBG_COAP_STRING(coap_pkt->location_path, coap_pkt->location_path_len);
|
|
|
|
LOG_DBG_("]\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_LOCATION_QUERY:
|
2017-11-14 20:32:43 +00:00
|
|
|
/* coap_merge_multi_option() operates in-place on the IPBUF, but final message field should be const string -> cast to string */
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_merge_multi_option((char **)&(coap_pkt->location_query),
|
|
|
|
&(coap_pkt->location_query_len), current_option,
|
|
|
|
option_length, '&');
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Location-Query [");
|
|
|
|
LOG_DBG_COAP_STRING(coap_pkt->location_query, coap_pkt->location_query_len);
|
|
|
|
LOG_DBG_("]\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COAP_OPTION_OBSERVE:
|
|
|
|
coap_pkt->observe = coap_parse_int_option(current_option,
|
|
|
|
option_length);
|
2018-02-23 12:47:18 +00:00
|
|
|
LOG_DBG_("Observe [%"PRId32"]\n", coap_pkt->observe);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_BLOCK2:
|
|
|
|
coap_pkt->block2_num = coap_parse_int_option(current_option,
|
|
|
|
option_length);
|
|
|
|
coap_pkt->block2_more = (coap_pkt->block2_num & 0x08) >> 3;
|
|
|
|
coap_pkt->block2_size = 16 << (coap_pkt->block2_num & 0x07);
|
|
|
|
coap_pkt->block2_offset = (coap_pkt->block2_num & ~0x0000000F)
|
|
|
|
<< (coap_pkt->block2_num & 0x07);
|
|
|
|
coap_pkt->block2_num >>= 4;
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Block2 [%lu%s (%u B/blk)]\n",
|
|
|
|
(unsigned long)coap_pkt->block2_num,
|
|
|
|
coap_pkt->block2_more ? "+" : "", coap_pkt->block2_size);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_BLOCK1:
|
|
|
|
coap_pkt->block1_num = coap_parse_int_option(current_option,
|
|
|
|
option_length);
|
|
|
|
coap_pkt->block1_more = (coap_pkt->block1_num & 0x08) >> 3;
|
|
|
|
coap_pkt->block1_size = 16 << (coap_pkt->block1_num & 0x07);
|
|
|
|
coap_pkt->block1_offset = (coap_pkt->block1_num & ~0x0000000F)
|
|
|
|
<< (coap_pkt->block1_num & 0x07);
|
|
|
|
coap_pkt->block1_num >>= 4;
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("Block1 [%lu%s (%u B/blk)]\n",
|
|
|
|
(unsigned long)coap_pkt->block1_num,
|
|
|
|
coap_pkt->block1_more ? "+" : "", coap_pkt->block1_size);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_SIZE2:
|
|
|
|
coap_pkt->size2 = coap_parse_int_option(current_option, option_length);
|
2018-02-23 12:47:18 +00:00
|
|
|
LOG_DBG_("Size2 [%"PRIu32"]\n", coap_pkt->size2);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
case COAP_OPTION_SIZE1:
|
|
|
|
coap_pkt->size1 = coap_parse_int_option(current_option, option_length);
|
2018-02-23 12:47:18 +00:00
|
|
|
LOG_DBG_("Size1 [%"PRIu32"]\n", coap_pkt->size1);
|
2014-04-22 08:31:43 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG_("unknown (%u)\n", option_number);
|
2014-04-22 08:31:43 +00:00
|
|
|
/* check if critical (odd) */
|
|
|
|
if(option_number & 1) {
|
|
|
|
coap_error_message = "Unsupported critical option";
|
|
|
|
return BAD_OPTION_4_02;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current_option += option_length;
|
|
|
|
} /* for */
|
2017-11-30 23:41:58 +00:00
|
|
|
LOG_DBG("-Done parsing-------\n");
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
/*- CoAP Engine API ---------------------------------------------------------*/
|
2014-04-22 08:31:43 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_query_variable(coap_message_t *coap_pkt,
|
2017-10-24 23:46:28 +00:00
|
|
|
const char *name, const char **output)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(coap_is_option(coap_pkt, COAP_OPTION_URI_QUERY)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return coap_get_variable(coap_pkt->uri_query, coap_pkt->uri_query_len,
|
|
|
|
name, output);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_post_variable(coap_message_t *coap_pkt,
|
2017-10-24 23:46:28 +00:00
|
|
|
const char *name, const char **output)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
if(coap_pkt->payload_len) {
|
|
|
|
return coap_get_variable((const char *)coap_pkt->payload,
|
|
|
|
coap_pkt->payload_len, name, output);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_status_code(coap_message_t *message, unsigned int code)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
if(code <= 0xFF) {
|
2017-11-14 20:32:43 +00:00
|
|
|
message->code = (uint8_t)code;
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_token(coap_message_t *coap_pkt, const uint8_t *token, size_t token_len)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->token_len = MIN(COAP_TOKEN_LEN, token_len);
|
|
|
|
memcpy(coap_pkt->token, token, coap_pkt->token_len);
|
|
|
|
|
|
|
|
return coap_pkt->token_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-24 23:46:28 +00:00
|
|
|
/*- CoAP Implementation API -------------------------------------------------*/
|
2014-04-22 08:31:43 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_content_format(coap_message_t *coap_pkt, unsigned int *format)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_CONTENT_FORMAT)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*format = coap_pkt->content_format;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_content_format(coap_message_t *coap_pkt, unsigned int format)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2015-05-11 23:18:36 +00:00
|
|
|
coap_pkt->content_format = format;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_CONTENT_FORMAT);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_accept(coap_message_t *coap_pkt, unsigned int *accept)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_ACCEPT)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*accept = coap_pkt->accept;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_accept(coap_message_t *coap_pkt, unsigned int accept)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2015-05-11 23:18:36 +00:00
|
|
|
coap_pkt->accept = accept;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_ACCEPT);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_max_age(coap_message_t *coap_pkt, uint32_t *age)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_MAX_AGE)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
*age = COAP_DEFAULT_MAX_AGE;
|
|
|
|
} else {
|
|
|
|
*age = coap_pkt->max_age;
|
|
|
|
} return 1;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_max_age(coap_message_t *coap_pkt, uint32_t age)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->max_age = age;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_MAX_AGE);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_etag(coap_message_t *coap_pkt, const uint8_t **etag)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_ETAG)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*etag = coap_pkt->etag;
|
|
|
|
return coap_pkt->etag_len;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_etag(coap_message_t *coap_pkt, const uint8_t *etag, size_t etag_len)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->etag_len = MIN(COAP_ETAG_LEN, etag_len);
|
|
|
|
memcpy(coap_pkt->etag, etag, coap_pkt->etag_len);
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_ETAG);
|
2014-04-22 08:31:43 +00:00
|
|
|
return coap_pkt->etag_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/*FIXME support multiple ETags */
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_if_match(coap_message_t *coap_pkt, const uint8_t **etag)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_IF_MATCH)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*etag = coap_pkt->if_match;
|
|
|
|
return coap_pkt->if_match_len;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_if_match(coap_message_t *coap_pkt, const uint8_t *etag, size_t etag_len)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->if_match_len = MIN(COAP_ETAG_LEN, etag_len);
|
|
|
|
memcpy(coap_pkt->if_match, etag, coap_pkt->if_match_len);
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_IF_MATCH);
|
2014-04-22 08:31:43 +00:00
|
|
|
return coap_pkt->if_match_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_if_none_match(coap_message_t *message)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-11-14 20:32:43 +00:00
|
|
|
return coap_is_option(message, COAP_OPTION_IF_NONE_MATCH) ? 1 : 0;
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_if_none_match(coap_message_t *message)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_option(message, COAP_OPTION_IF_NONE_MATCH);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_proxy_uri(coap_message_t *coap_pkt, const char **uri)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_PROXY_URI)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*uri = coap_pkt->proxy_uri;
|
|
|
|
return coap_pkt->proxy_uri_len;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_proxy_uri(coap_message_t *coap_pkt, const char *uri)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
/* TODO Provide alternative that sets Proxy-Scheme and Uri-* options and provide coap-conf define */
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_pkt->proxy_uri = uri;
|
|
|
|
coap_pkt->proxy_uri_len = strlen(uri);
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_PROXY_URI);
|
2014-04-22 08:31:43 +00:00
|
|
|
return coap_pkt->proxy_uri_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_uri_host(coap_message_t *coap_pkt, const char **host)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_URI_HOST)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*host = coap_pkt->uri_host;
|
|
|
|
return coap_pkt->uri_host_len;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_uri_host(coap_message_t *coap_pkt, const char *host)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->uri_host = host;
|
|
|
|
coap_pkt->uri_host_len = strlen(host);
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_URI_HOST);
|
2014-04-22 08:31:43 +00:00
|
|
|
return coap_pkt->uri_host_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_uri_path(coap_message_t *coap_pkt, const char **path)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_URI_PATH)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*path = coap_pkt->uri_path;
|
|
|
|
return coap_pkt->uri_path_len;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_uri_path(coap_message_t *coap_pkt, const char *path)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
while(path[0] == '/') {
|
2014-04-22 08:31:43 +00:00
|
|
|
++path;
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_pkt->uri_path = path;
|
|
|
|
coap_pkt->uri_path_len = strlen(path);
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_URI_PATH);
|
2014-04-22 08:31:43 +00:00
|
|
|
return coap_pkt->uri_path_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_uri_query(coap_message_t *coap_pkt, const char **query)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_URI_QUERY)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*query = coap_pkt->uri_query;
|
|
|
|
return coap_pkt->uri_query_len;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_uri_query(coap_message_t *coap_pkt, const char *query)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
while(query[0] == '?') {
|
2014-04-22 08:31:43 +00:00
|
|
|
++query;
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_pkt->uri_query = query;
|
|
|
|
coap_pkt->uri_query_len = strlen(query);
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_URI_QUERY);
|
2014-04-22 08:31:43 +00:00
|
|
|
return coap_pkt->uri_query_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_location_path(coap_message_t *coap_pkt, const char **path)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_LOCATION_PATH)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*path = coap_pkt->location_path;
|
|
|
|
return coap_pkt->location_path_len;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_location_path(coap_message_t *coap_pkt, const char *path)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
char *query;
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
while(path[0] == '/') {
|
2014-04-22 08:31:43 +00:00
|
|
|
++path;
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
if((query = strchr(path, '?'))) {
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_header_location_query(coap_pkt, query + 1);
|
2014-04-22 08:31:43 +00:00
|
|
|
coap_pkt->location_path_len = query - path;
|
|
|
|
} else {
|
|
|
|
coap_pkt->location_path_len = strlen(path);
|
|
|
|
} coap_pkt->location_path = path;
|
|
|
|
|
|
|
|
if(coap_pkt->location_path_len > 0) {
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_LOCATION_PATH);
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
return coap_pkt->location_path_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_location_query(coap_message_t *coap_pkt, const char **query)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_LOCATION_QUERY)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*query = coap_pkt->location_query;
|
|
|
|
return coap_pkt->location_query_len;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_location_query(coap_message_t *coap_pkt, const char *query)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
while(query[0] == '?') {
|
2014-04-22 08:31:43 +00:00
|
|
|
++query;
|
2017-10-24 23:46:28 +00:00
|
|
|
}
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
coap_pkt->location_query = query;
|
|
|
|
coap_pkt->location_query_len = strlen(query);
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_LOCATION_QUERY);
|
2014-04-22 08:31:43 +00:00
|
|
|
return coap_pkt->location_query_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_observe(coap_message_t *coap_pkt, uint32_t *observe)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_OBSERVE)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*observe = coap_pkt->observe;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_observe(coap_message_t *coap_pkt, uint32_t observe)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->observe = observe;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_OBSERVE);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_block2(coap_message_t *coap_pkt, uint32_t *num, uint8_t *more,
|
2014-04-22 08:31:43 +00:00
|
|
|
uint16_t *size, uint32_t *offset)
|
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_BLOCK2)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-15 17:56:29 +00:00
|
|
|
/* pointers may be NULL to get only specific block parameters */
|
2014-04-22 08:31:43 +00:00
|
|
|
if(num != NULL) {
|
|
|
|
*num = coap_pkt->block2_num;
|
|
|
|
}
|
|
|
|
if(more != NULL) {
|
|
|
|
*more = coap_pkt->block2_more;
|
|
|
|
}
|
|
|
|
if(size != NULL) {
|
|
|
|
*size = coap_pkt->block2_size;
|
|
|
|
}
|
|
|
|
if(offset != NULL) {
|
|
|
|
*offset = coap_pkt->block2_offset;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_block2(coap_message_t *coap_pkt, uint32_t num, uint8_t more,
|
2014-04-22 08:31:43 +00:00
|
|
|
uint16_t size)
|
|
|
|
{
|
|
|
|
if(size < 16) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(size > 2048) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(num > 0x0FFFFF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
coap_pkt->block2_num = num;
|
|
|
|
coap_pkt->block2_more = more ? 1 : 0;
|
|
|
|
coap_pkt->block2_size = size;
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_BLOCK2);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_block1(coap_message_t *coap_pkt, uint32_t *num, uint8_t *more,
|
2014-04-22 08:31:43 +00:00
|
|
|
uint16_t *size, uint32_t *offset)
|
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_BLOCK1)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-15 17:56:29 +00:00
|
|
|
/* pointers may be NULL to get only specific block parameters */
|
2014-04-22 08:31:43 +00:00
|
|
|
if(num != NULL) {
|
|
|
|
*num = coap_pkt->block1_num;
|
|
|
|
}
|
|
|
|
if(more != NULL) {
|
|
|
|
*more = coap_pkt->block1_more;
|
|
|
|
}
|
|
|
|
if(size != NULL) {
|
|
|
|
*size = coap_pkt->block1_size;
|
|
|
|
}
|
|
|
|
if(offset != NULL) {
|
|
|
|
*offset = coap_pkt->block1_offset;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_block1(coap_message_t *coap_pkt, uint32_t num, uint8_t more,
|
2014-04-22 08:31:43 +00:00
|
|
|
uint16_t size)
|
|
|
|
{
|
|
|
|
if(size < 16) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(size > 2048) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(num > 0x0FFFFF) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
coap_pkt->block1_num = num;
|
|
|
|
coap_pkt->block1_more = more;
|
|
|
|
coap_pkt->block1_size = size;
|
|
|
|
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_BLOCK1);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_size2(coap_message_t *coap_pkt, uint32_t *size)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_SIZE2)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*size = coap_pkt->size2;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_size2(coap_message_t *coap_pkt, uint32_t size)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->size2 = size;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_SIZE2);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_header_size1(coap_message_t *coap_pkt, uint32_t *size)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2017-10-24 23:46:28 +00:00
|
|
|
if(!coap_is_option(coap_pkt, COAP_OPTION_SIZE1)) {
|
2014-04-22 08:31:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*size = coap_pkt->size1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_header_size1(coap_message_t *coap_pkt, uint32_t size)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->size1 = size;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_set_option(coap_pkt, COAP_OPTION_SIZE1);
|
2014-04-22 08:31:43 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_get_payload(coap_message_t *coap_pkt, const uint8_t **payload)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
2018-04-05 12:35:27 +00:00
|
|
|
if(payload != NULL) {
|
2014-04-22 08:31:43 +00:00
|
|
|
*payload = coap_pkt->payload;
|
|
|
|
}
|
2018-04-05 12:35:27 +00:00
|
|
|
return coap_pkt->payload != NULL ? coap_pkt->payload_len : 0;
|
2014-04-22 08:31:43 +00:00
|
|
|
}
|
|
|
|
int
|
2017-11-14 20:32:43 +00:00
|
|
|
coap_set_payload(coap_message_t *coap_pkt, const void *payload, size_t length)
|
2014-04-22 08:31:43 +00:00
|
|
|
{
|
|
|
|
coap_pkt->payload = (uint8_t *)payload;
|
2017-10-24 23:46:28 +00:00
|
|
|
coap_pkt->payload_len = MIN(COAP_MAX_CHUNK_SIZE, length);
|
2014-04-22 08:31:43 +00:00
|
|
|
|
|
|
|
return coap_pkt->payload_len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-11-14 20:32:43 +00:00
|
|
|
/** @} */
|