2017-07-26 02:04:16 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Linaro Limited
|
2018-08-30 04:35:50 +08:00
|
|
|
* Copyright (c) 2018 Foundries.io
|
2017-07-26 02:04:16 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2017-07-08 02:04:03 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, Yanzi Networks AB.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Original Authors:
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Zephyr Contribution by Michael Scott <michael.scott@linaro.org>
|
|
|
|
* - Zephyr code style changes / code cleanup
|
|
|
|
* - Move to Zephyr APIs where possible
|
|
|
|
* - Convert to Zephyr int/uint types
|
|
|
|
* - Remove engine dependency (replace with writer context)
|
|
|
|
* - Add write int64 function
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* - Type cleanups
|
|
|
|
* - Cleanup integer parsing
|
|
|
|
*/
|
|
|
|
|
2018-09-19 16:22:19 +08:00
|
|
|
#define LOG_MODULE_NAME net_lwm2m_plain_text
|
|
|
|
#define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
|
|
|
|
|
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
#include <stdarg.h>
|
2017-07-08 02:04:03 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "lwm2m_object.h"
|
|
|
|
#include "lwm2m_rw_plain_text.h"
|
|
|
|
#include "lwm2m_engine.h"
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
/* some temporary buffer space for format conversions */
|
|
|
|
static char pt_buffer[42]; /* can handle float64 format */
|
2017-07-08 02:04:03 +08:00
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
size_t plain_text_put_format(struct lwm2m_output_context *out,
|
|
|
|
const char *format, ...)
|
2017-07-08 02:04:03 +08:00
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
va_list vargs;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
va_start(vargs, format);
|
|
|
|
n = vsnprintk(pt_buffer, sizeof(pt_buffer), format, vargs);
|
|
|
|
va_end(vargs);
|
|
|
|
if (n < 0) {
|
|
|
|
/* TODO: generate an error? */
|
2017-07-08 02:04:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
out->frag = net_pkt_write(out->out_cpkt->pkt, out->frag,
|
|
|
|
out->offset, &out->offset,
|
|
|
|
strlen(pt_buffer), pt_buffer,
|
|
|
|
BUF_ALLOC_TIMEOUT);
|
|
|
|
if (!out->frag) {
|
2017-07-08 02:04:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
return strlen(pt_buffer);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t put_s32(struct lwm2m_output_context *out,
|
|
|
|
struct lwm2m_obj_path *path, s32_t value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
return plain_text_put_format(out, "%d", value);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t put_s8(struct lwm2m_output_context *out,
|
|
|
|
struct lwm2m_obj_path *path, s8_t value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
return plain_text_put_format(out, "%d", value);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t put_s16(struct lwm2m_output_context *out,
|
|
|
|
struct lwm2m_obj_path *path, s16_t value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
return plain_text_put_format(out, "%d", value);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t put_s64(struct lwm2m_output_context *out,
|
|
|
|
struct lwm2m_obj_path *path, s64_t value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
return plain_text_put_format(out, "%lld", value);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t put_float32fix(struct lwm2m_output_context *out,
|
|
|
|
struct lwm2m_obj_path *path,
|
|
|
|
float32_value_t *value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
return plain_text_put_format(out, "%d.%d",
|
|
|
|
value->val1, value->val2);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t put_float64fix(struct lwm2m_output_context *out,
|
|
|
|
struct lwm2m_obj_path *path,
|
|
|
|
float64_value_t *value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
return plain_text_put_format(out, "%lld.%lld",
|
|
|
|
value->val1, value->val2);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t put_string(struct lwm2m_output_context *out,
|
|
|
|
struct lwm2m_obj_path *path,
|
2017-10-12 03:48:06 +08:00
|
|
|
char *buf, size_t buflen)
|
2017-07-08 02:04:03 +08:00
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
out->frag = net_pkt_write(out->out_cpkt->pkt, out->frag,
|
|
|
|
out->offset, &out->offset,
|
|
|
|
buflen, buf,
|
|
|
|
BUF_ALLOC_TIMEOUT);
|
|
|
|
if (!out->frag) {
|
|
|
|
return -ENOMEM;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
2017-10-12 03:48:06 +08:00
|
|
|
return buflen;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t put_bool(struct lwm2m_output_context *out,
|
|
|
|
struct lwm2m_obj_path *path,
|
|
|
|
bool value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
if (value) {
|
|
|
|
return plain_text_put_format(out, "%u", 1);
|
|
|
|
} else {
|
|
|
|
return plain_text_put_format(out, "%u", 0);
|
|
|
|
}
|
|
|
|
}
|
2017-07-08 02:04:03 +08:00
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
static int pkt_length_left(struct lwm2m_input_context *in)
|
|
|
|
{
|
|
|
|
struct net_buf *frag = in->frag;
|
|
|
|
int total_left = -in->offset;
|
|
|
|
|
|
|
|
while (frag) {
|
|
|
|
total_left += frag->len;
|
|
|
|
frag = frag->frags;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
return total_left;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
static size_t plain_text_read_number(struct lwm2m_input_context *in,
|
|
|
|
s64_t *value1,
|
|
|
|
s64_t *value2,
|
|
|
|
bool accept_sign, bool accept_dot)
|
2017-07-08 02:04:03 +08:00
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
s64_t *counter = value1;
|
|
|
|
int i = 0;
|
2017-07-08 02:04:03 +08:00
|
|
|
bool neg = false;
|
2017-10-20 07:27:32 +08:00
|
|
|
bool dot_found = false;
|
|
|
|
u8_t tmp;
|
|
|
|
|
|
|
|
/* initialize values to 0 */
|
|
|
|
value1 = 0;
|
|
|
|
if (value2) {
|
|
|
|
value2 = 0;
|
|
|
|
}
|
2017-07-08 02:04:03 +08:00
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
while (in->frag && in->offset != 0xffff) {
|
|
|
|
in->frag = net_frag_read_u8(in->frag, in->offset, &in->offset,
|
|
|
|
&tmp);
|
|
|
|
if (!in->frag && in->offset == 0xffff) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp == '-' && accept_sign && i == 0) {
|
2017-07-08 02:04:03 +08:00
|
|
|
neg = true;
|
2017-10-20 07:27:32 +08:00
|
|
|
} else if (tmp == '.' && i > 0 && accept_dot && !dot_found &&
|
|
|
|
value2) {
|
|
|
|
dot_found = true;
|
|
|
|
counter = value2;
|
|
|
|
} else if (isdigit(tmp)) {
|
|
|
|
*counter = *counter * 10 + (tmp - '0');
|
2017-07-08 02:04:03 +08:00
|
|
|
} else {
|
2017-10-20 07:27:32 +08:00
|
|
|
/* anything else stop reading */
|
|
|
|
in->offset--;
|
2017-07-08 02:04:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-10-20 07:27:32 +08:00
|
|
|
|
|
|
|
i++;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (neg) {
|
2017-10-20 07:27:32 +08:00
|
|
|
*value1 = -*value1;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
static size_t get_s32(struct lwm2m_input_context *in, s32_t *value)
|
2017-07-08 02:04:03 +08:00
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
s64_t tmp = 0;
|
|
|
|
size_t len = 0;
|
2017-07-08 02:04:03 +08:00
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
len = plain_text_read_number(in, &tmp, NULL, true, false);
|
|
|
|
if (len > 0) {
|
|
|
|
*value = (s32_t)tmp;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
return len;
|
|
|
|
}
|
2017-07-08 02:04:03 +08:00
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
static size_t get_s64(struct lwm2m_input_context *in, s64_t *value)
|
|
|
|
{
|
|
|
|
return plain_text_read_number(in, value, NULL, true, false);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_string(struct lwm2m_input_context *in,
|
2017-10-12 03:48:06 +08:00
|
|
|
u8_t *value, size_t buflen)
|
2017-07-08 02:04:03 +08:00
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
u16_t in_len = pkt_length_left(in);
|
|
|
|
|
|
|
|
if (in_len > buflen) {
|
|
|
|
/* TODO: generate warning? */
|
|
|
|
in_len = buflen - 1;
|
|
|
|
}
|
|
|
|
in->frag = net_frag_read(in->frag, in->offset, &in->offset,
|
|
|
|
in_len, value);
|
|
|
|
if (!in->frag && in->offset == 0xffff) {
|
|
|
|
value[0] = '\0';
|
2017-07-08 02:04:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
value[in_len] = '\0';
|
|
|
|
return (size_t)in_len;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_float32fix(struct lwm2m_input_context *in,
|
|
|
|
float32_value_t *value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
s64_t tmp1, tmp2;
|
|
|
|
size_t len = 0;
|
2017-07-08 02:04:03 +08:00
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
len = plain_text_read_number(in, &tmp1, &tmp2, true, true);
|
|
|
|
if (len > 0) {
|
|
|
|
value->val1 = (s32_t)tmp1;
|
|
|
|
value->val2 = (s32_t)tmp2;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
2017-10-20 07:27:32 +08:00
|
|
|
return len;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_float64fix(struct lwm2m_input_context *in,
|
|
|
|
float64_value_t *value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
return plain_text_read_number(in, &value->val1, &value->val2,
|
|
|
|
true, true);
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_bool(struct lwm2m_input_context *in,
|
|
|
|
bool *value)
|
|
|
|
{
|
2017-10-20 07:27:32 +08:00
|
|
|
u8_t tmp;
|
|
|
|
|
|
|
|
in->frag = net_frag_read_u8(in->frag, in->offset, &in->offset, &tmp);
|
|
|
|
if (!in->frag && in->offset == 0xffff) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp == '1' || tmp == '0') {
|
|
|
|
*value = (tmp == '1') ? true : false;
|
|
|
|
return 1;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-10-27 14:32:47 +08:00
|
|
|
static size_t get_opaque(struct lwm2m_input_context *in,
|
|
|
|
u8_t *value, size_t buflen, bool *last_block)
|
|
|
|
{
|
|
|
|
in->opaque_len = pkt_length_left(in);
|
|
|
|
return lwm2m_engine_get_opaque_more(in, value, buflen, last_block);
|
|
|
|
}
|
|
|
|
|
2017-07-08 02:04:03 +08:00
|
|
|
const struct lwm2m_writer plain_text_writer = {
|
2018-08-30 23:59:23 +08:00
|
|
|
.put_s8 = put_s8,
|
|
|
|
.put_s16 = put_s16,
|
|
|
|
.put_s32 = put_s32,
|
|
|
|
.put_s64 = put_s64,
|
|
|
|
.put_string = put_string,
|
|
|
|
.put_float32fix = put_float32fix,
|
|
|
|
.put_float64fix = put_float64fix,
|
|
|
|
.put_bool = put_bool,
|
2017-07-08 02:04:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const struct lwm2m_reader plain_text_reader = {
|
2018-08-30 23:59:23 +08:00
|
|
|
.get_s32 = get_s32,
|
|
|
|
.get_s64 = get_s64,
|
|
|
|
.get_string = get_string,
|
|
|
|
.get_float32fix = get_float32fix,
|
|
|
|
.get_float64fix = get_float64fix,
|
|
|
|
.get_bool = get_bool,
|
|
|
|
.get_opaque = get_opaque,
|
2017-07-08 02:04:03 +08:00
|
|
|
};
|
|
|
|
|
2018-08-30 04:35:50 +08:00
|
|
|
int do_read_op_plain_text(struct lwm2m_engine_obj *obj,
|
|
|
|
struct lwm2m_engine_context *context,
|
|
|
|
int content_format)
|
|
|
|
{
|
2018-08-30 04:46:43 +08:00
|
|
|
/* Plain text can only return single resource */
|
|
|
|
if (context->path->level != 3) {
|
|
|
|
return -EPERM; /* NOT_ALLOWED */
|
|
|
|
}
|
|
|
|
|
2018-08-30 04:35:50 +08:00
|
|
|
return lwm2m_perform_read_op(obj, context, content_format);
|
|
|
|
}
|
|
|
|
|
2017-07-08 02:04:03 +08:00
|
|
|
int do_write_op_plain_text(struct lwm2m_engine_obj *obj,
|
|
|
|
struct lwm2m_engine_context *context)
|
|
|
|
{
|
|
|
|
struct lwm2m_obj_path *path = context->path;
|
|
|
|
struct lwm2m_engine_obj_inst *obj_inst = NULL;
|
|
|
|
struct lwm2m_engine_obj_field *obj_field;
|
|
|
|
struct lwm2m_engine_res_inst *res = NULL;
|
|
|
|
int ret, i;
|
|
|
|
u8_t created = 0;
|
|
|
|
|
|
|
|
ret = lwm2m_get_or_create_engine_obj(context, &obj_inst, &created);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj_field = lwm2m_get_engine_obj_field(obj, path->res_id);
|
|
|
|
if (!obj_field) {
|
2017-08-18 03:04:13 +08:00
|
|
|
return -ENOENT;
|
2017-07-08 02:04:03 +08:00
|
|
|
}
|
|
|
|
|
2018-05-01 06:30:12 +08:00
|
|
|
if (!LWM2M_HAS_PERM(obj_field, LWM2M_PERM_W)) {
|
2017-07-08 02:04:03 +08:00
|
|
|
return -EPERM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!obj_inst->resources || obj_inst->resource_count == 0) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < obj_inst->resource_count; i++) {
|
|
|
|
if (obj_inst->resources[i].res_id == path->res_id) {
|
|
|
|
res = &obj_inst->resources[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->path->level = 3;
|
|
|
|
return lwm2m_write_handler(obj_inst, res, obj_field, context);
|
|
|
|
}
|