2015-12-01 03:14:46 +00:00
|
|
|
/*
|
2017-12-21 17:54:28 +00:00
|
|
|
* Copyright (c) 2015-2018, Yanzi Networks AB.
|
2015-12-01 03:14:46 +00:00
|
|
|
* 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 copyright holder 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 COPYRIGHT HOLDER 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
|
|
|
|
* COPYRIGHT HOLDER 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-12-21 17:54:28 +00:00
|
|
|
* \addtogroup lwm2m
|
2015-12-01 03:14:46 +00:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Implementation of the Contiki OMA LWM2M TLV reader
|
|
|
|
* \author
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lwm2m-object.h"
|
2017-10-25 00:20:05 +00:00
|
|
|
#include "lwm2m-tlv-reader.h"
|
|
|
|
#include "lwm2m-tlv.h"
|
|
|
|
#include <string.h>
|
2015-12-01 03:14:46 +00:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
2017-10-25 00:20:05 +00:00
|
|
|
read_int(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t len,
|
2015-12-01 03:14:46 +00:00
|
|
|
int32_t *value)
|
|
|
|
{
|
2017-10-25 00:20:05 +00:00
|
|
|
lwm2m_tlv_t tlv;
|
2015-12-01 03:14:46 +00:00
|
|
|
size_t size;
|
2017-10-25 00:20:05 +00:00
|
|
|
size = lwm2m_tlv_read(&tlv, inbuf, len);
|
2015-12-01 03:14:46 +00:00
|
|
|
if(size > 0) {
|
2017-10-25 00:20:05 +00:00
|
|
|
*value = lwm2m_tlv_get_int32(&tlv);
|
|
|
|
ctx->last_value_len = tlv.length;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
2017-10-25 00:20:05 +00:00
|
|
|
read_string(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t len,
|
2015-12-01 03:14:46 +00:00
|
|
|
uint8_t *value, size_t stringlen)
|
|
|
|
{
|
2017-10-25 00:20:05 +00:00
|
|
|
lwm2m_tlv_t tlv;
|
2015-12-01 03:14:46 +00:00
|
|
|
size_t size;
|
2017-10-25 00:20:05 +00:00
|
|
|
size = lwm2m_tlv_read(&tlv, inbuf, len);
|
2015-12-01 03:14:46 +00:00
|
|
|
if(size > 0) {
|
|
|
|
if(stringlen <= tlv.length) {
|
|
|
|
/* The outbuffer can not contain the full string including ending zero */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(value, tlv.value, tlv.length);
|
|
|
|
value[tlv.length] = '\0';
|
2017-10-25 00:20:05 +00:00
|
|
|
ctx->last_value_len = tlv.length;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
2017-10-25 00:20:05 +00:00
|
|
|
read_float32fix(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t len,
|
2015-12-01 03:14:46 +00:00
|
|
|
int32_t *value, int bits)
|
|
|
|
{
|
2017-10-25 00:20:05 +00:00
|
|
|
lwm2m_tlv_t tlv;
|
2015-12-01 03:14:46 +00:00
|
|
|
size_t size;
|
2017-10-25 00:20:05 +00:00
|
|
|
size = lwm2m_tlv_read(&tlv, inbuf, len);
|
2015-12-01 03:14:46 +00:00
|
|
|
if(size > 0) {
|
2017-10-25 00:20:05 +00:00
|
|
|
lwm2m_tlv_float32_to_fix(&tlv, value, bits);
|
|
|
|
ctx->last_value_len = tlv.length;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
2017-10-25 00:20:05 +00:00
|
|
|
read_boolean(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t len,
|
2015-12-01 03:14:46 +00:00
|
|
|
int *value)
|
|
|
|
{
|
2017-10-25 00:20:05 +00:00
|
|
|
lwm2m_tlv_t tlv;
|
2015-12-01 03:14:46 +00:00
|
|
|
size_t size;
|
2017-10-25 00:20:05 +00:00
|
|
|
size = lwm2m_tlv_read(&tlv, inbuf, len);
|
2015-12-01 03:14:46 +00:00
|
|
|
if(size > 0) {
|
2017-10-25 00:20:05 +00:00
|
|
|
*value = lwm2m_tlv_get_int32(&tlv) != 0;
|
|
|
|
ctx->last_value_len = tlv.length;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-25 00:20:05 +00:00
|
|
|
const lwm2m_reader_t lwm2m_tlv_reader = {
|
2015-12-01 03:14:46 +00:00
|
|
|
read_int,
|
|
|
|
read_string,
|
|
|
|
read_float32fix,
|
|
|
|
read_boolean
|
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|