Contiki-NG
Loading...
Searching...
No Matches
lwm2m-tlv-reader.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015-2018, Yanzi Networks AB.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * \addtogroup lwm2m
33 * @{
34 *
35 */
36
37/**
38 * \file
39 * Implementation of the Contiki OMA LWM2M TLV reader
40 * \author
41 * Joakim Eriksson <joakime@sics.se>
42 * Niclas Finne <nfi@sics.se>
43 */
44
45#include "lwm2m-object.h"
46#include "lwm2m-tlv-reader.h"
47#include "lwm2m-tlv.h"
48#include <string.h>
49
50/*---------------------------------------------------------------------------*/
51static size_t
52read_int(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t len,
53 int32_t *value)
54{
55 lwm2m_tlv_t tlv;
56 size_t size;
57 size = lwm2m_tlv_read(&tlv, inbuf, len);
58 if(size > 0) {
59 *value = lwm2m_tlv_get_int32(&tlv);
60 ctx->last_value_len = tlv.length;
61 }
62 return size;
63}
64/*---------------------------------------------------------------------------*/
65static size_t
66read_string(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t len,
67 uint8_t *value, size_t stringlen)
68{
69 lwm2m_tlv_t tlv;
70 size_t size;
71 size = lwm2m_tlv_read(&tlv, inbuf, len);
72 if(size > 0) {
73 if(stringlen <= tlv.length) {
74 /* The outbuffer can not contain the full string including ending zero */
75 return 0;
76 }
77 memcpy(value, tlv.value, tlv.length);
78 value[tlv.length] = '\0';
79 ctx->last_value_len = tlv.length;
80 }
81 return size;
82}
83/*---------------------------------------------------------------------------*/
84static size_t
85read_float32fix(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t len,
86 int32_t *value, int bits)
87{
88 lwm2m_tlv_t tlv;
89 size_t size;
90 size = lwm2m_tlv_read(&tlv, inbuf, len);
91 if(size > 0) {
92 lwm2m_tlv_float32_to_fix(&tlv, value, bits);
93 ctx->last_value_len = tlv.length;
94 }
95 return size;
96}
97/*---------------------------------------------------------------------------*/
98static size_t
99read_boolean(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t len,
100 int *value)
101{
102 lwm2m_tlv_t tlv;
103 size_t size;
104 size = lwm2m_tlv_read(&tlv, inbuf, len);
105 if(size > 0) {
106 *value = lwm2m_tlv_get_int32(&tlv) != 0;
107 ctx->last_value_len = tlv.length;
108 }
109 return size;
110}
111/*---------------------------------------------------------------------------*/
112const lwm2m_reader_t lwm2m_tlv_reader = {
113 read_int,
114 read_string,
115 read_float32fix,
116 read_boolean
117};
118/*---------------------------------------------------------------------------*/
119/** @} */
Header file for the LWM2M object API.
Header file for the Contiki OMA LWM2M TLV reader.
Header file for the Contiki OMA LWM2M TLV.