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 plain text reader / writer
|
|
|
|
* \author
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lwm2m-object.h"
|
|
|
|
#include "lwm2m-plain-text.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-12-01 01:58:44 +00:00
|
|
|
/* Log configuration */
|
|
|
|
#include "coap-log.h"
|
2018-05-04 09:02:25 +00:00
|
|
|
#define LOG_MODULE "lwm2m-text"
|
2017-12-01 01:58:44 +00:00
|
|
|
#define LOG_LEVEL LOG_LEVEL_NONE
|
2015-12-01 03:14:46 +00:00
|
|
|
|
2017-10-25 00:20:05 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
|
|
|
init_write(lwm2m_context_t *ctx)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
|
|
|
end_write(lwm2m_context_t *ctx)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-01 03:14:46 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
size_t
|
|
|
|
lwm2m_plain_text_read_int(const uint8_t *inbuf, size_t len, int32_t *value)
|
|
|
|
{
|
|
|
|
int i, neg = 0;
|
|
|
|
*value = 0;
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
if(inbuf[i] >= '0' && inbuf[i] <= '9') {
|
|
|
|
*value = *value * 10 + (inbuf[i] - '0');
|
|
|
|
} else if(inbuf[i] == '-' && i == 0) {
|
|
|
|
neg = 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(neg) {
|
|
|
|
*value = -*value;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
size_t
|
|
|
|
lwm2m_plain_text_read_float32fix(const uint8_t *inbuf, size_t len,
|
|
|
|
int32_t *value, int bits)
|
|
|
|
{
|
|
|
|
int i, dot = 0, neg = 0;
|
|
|
|
int32_t counter, integerpart, frac;
|
|
|
|
|
|
|
|
integerpart = 0;
|
|
|
|
counter = 0;
|
|
|
|
frac = 0;
|
|
|
|
for(i = 0; i < len; i++) {
|
|
|
|
if(inbuf[i] >= '0' && inbuf[i] <= '9') {
|
|
|
|
counter = counter * 10 + (inbuf[i] - '0');
|
|
|
|
frac = frac * 10;
|
|
|
|
} else if(inbuf[i] == '.' && dot == 0) {
|
|
|
|
integerpart = counter;
|
|
|
|
counter = 0;
|
|
|
|
frac = 1;
|
|
|
|
dot = 1;
|
|
|
|
} else if(inbuf[i] == '-' && i == 0) {
|
|
|
|
neg = 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 07:46:51 +00:00
|
|
|
if(dot == 0) {
|
|
|
|
integerpart = counter;
|
|
|
|
counter = 0;
|
|
|
|
frac = 1;
|
|
|
|
}
|
2015-12-01 03:14:46 +00:00
|
|
|
*value = integerpart << bits;
|
|
|
|
if(frac > 1) {
|
|
|
|
*value += ((counter << bits) / frac);
|
|
|
|
}
|
2017-12-01 01:58:44 +00:00
|
|
|
LOG_DBG("READ FLOATFIX: \"%.*s\" => int(%ld) frac(%ld) f=%ld Value=%ld\n",
|
|
|
|
(int)len, (char *)inbuf,
|
|
|
|
(long)integerpart,
|
|
|
|
(long)counter,
|
|
|
|
(long)frac,
|
|
|
|
(long)*value);
|
2015-12-01 03:14:46 +00:00
|
|
|
if(neg) {
|
|
|
|
*value = -*value;
|
|
|
|
}
|
2017-12-01 01:58:44 +00:00
|
|
|
|
2015-12-01 03:14:46 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
size_t
|
|
|
|
lwm2m_plain_text_write_float32fix(uint8_t *outbuf, size_t outlen,
|
|
|
|
int32_t value, int bits)
|
|
|
|
{
|
|
|
|
int64_t v;
|
|
|
|
unsigned long integer_part;
|
|
|
|
unsigned long frac_part;
|
|
|
|
int n, o = 0;
|
|
|
|
|
|
|
|
if(outlen == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(value < 0) {
|
|
|
|
*outbuf++ = '-';
|
|
|
|
outlen--;
|
|
|
|
o = 1;
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
|
|
|
|
integer_part = (unsigned long)(value >> bits);
|
|
|
|
v = value - (integer_part << bits);
|
|
|
|
v = (v * 100) >> bits;
|
|
|
|
frac_part = (unsigned long)v;
|
|
|
|
|
|
|
|
n = snprintf((char *)outbuf, outlen, "%lu.%02lu", integer_part, frac_part);
|
|
|
|
if(n < 0 || n >= outlen) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return n + o;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
2017-10-25 00:20:05 +00:00
|
|
|
write_boolean(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
2015-12-01 03:14:46 +00:00
|
|
|
int value)
|
|
|
|
{
|
|
|
|
if(outlen > 0) {
|
|
|
|
if(value) {
|
|
|
|
*outbuf = '1';
|
|
|
|
} else {
|
|
|
|
*outbuf = '0';
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
2017-10-25 00:20:05 +00:00
|
|
|
write_int(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
2015-12-01 03:14:46 +00:00
|
|
|
int32_t value)
|
|
|
|
{
|
|
|
|
int n = snprintf((char *)outbuf, outlen, "%ld", (long)value);
|
|
|
|
if(n < 0 || n >= outlen) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
2017-10-25 00:20:05 +00:00
|
|
|
write_float32fix(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
2015-12-01 03:14:46 +00:00
|
|
|
int32_t value, int bits)
|
|
|
|
{
|
|
|
|
return lwm2m_plain_text_write_float32fix(outbuf, outlen, value, bits);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static size_t
|
2017-10-25 00:20:05 +00:00
|
|
|
write_string(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
|
2015-12-01 03:14:46 +00:00
|
|
|
const char *value, size_t stringlen)
|
|
|
|
{
|
2017-10-25 00:20:05 +00:00
|
|
|
int totlen = stringlen;
|
|
|
|
if(stringlen >= outlen) {
|
2015-12-01 03:14:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-10-25 00:20:05 +00:00
|
|
|
memmove(outbuf, value, totlen);
|
|
|
|
outbuf[totlen] = 0;
|
|
|
|
return totlen;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const lwm2m_writer_t lwm2m_plain_text_writer = {
|
2017-10-25 00:20:05 +00:00
|
|
|
init_write,
|
|
|
|
end_write,
|
|
|
|
NULL, /* No support for sub resources here! */
|
|
|
|
NULL,
|
2015-12-01 03:14:46 +00:00
|
|
|
write_int,
|
|
|
|
write_string,
|
|
|
|
write_float32fix,
|
|
|
|
write_boolean
|
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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
|
|
|
int size = lwm2m_plain_text_read_int(inbuf, len, value);
|
|
|
|
ctx->last_value_len = size;
|
|
|
|
return size;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if(stringlen <= len) {
|
|
|
|
/* The outbuffer can not contain the full string including ending zero */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(value, inbuf, len);
|
|
|
|
value[len] = '\0';
|
2017-10-25 00:20:05 +00:00
|
|
|
ctx->last_value_len = len;
|
2015-12-01 03:14:46 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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
|
|
|
int size;
|
|
|
|
size = lwm2m_plain_text_read_float32fix(inbuf, len, value, bits);
|
|
|
|
ctx->last_value_len = size;
|
|
|
|
return size;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if(len > 0) {
|
|
|
|
if(*inbuf == '1' || *inbuf == '0') {
|
|
|
|
*value = *inbuf == '1' ? 1 : 0;
|
2017-10-25 00:20:05 +00:00
|
|
|
ctx->last_value_len = 1;
|
2015-12-01 03:14:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
const lwm2m_reader_t lwm2m_plain_text_reader = {
|
|
|
|
read_int,
|
|
|
|
read_string,
|
|
|
|
read_float32fix,
|
|
|
|
read_boolean
|
|
|
|
};
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|