Contiki-NG
Loading...
Searching...
No Matches
lwm2m-tlv.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
40 * \author
41 * Joakim Eriksson <joakime@sics.se>
42 * Niclas Finne <nfi@sics.se>
43 */
44
45#include <string.h>
46#include <stdint.h>
47#include <inttypes.h>
48#include "lwm2m-tlv.h"
49
50/* Log configuration */
51#include "coap-log.h"
52#define LOG_MODULE "lwm2m-tlv"
53#define LOG_LEVEL LOG_LEVEL_NONE
54
55/*---------------------------------------------------------------------------*/
56static inline uint8_t
57get_len_type(const lwm2m_tlv_t *tlv)
58{
59 if(tlv->length < 8) {
60 return 0;
61 } else if(tlv->length < 256) {
62 return 1;
63 } else if(tlv->length < 0x10000) {
64 return 2;
65 } else {
66 return 3;
67 }
68}
69/*---------------------------------------------------------------------------*/
70size_t
71lwm2m_tlv_read(lwm2m_tlv_t *tlv, const uint8_t *buffer, size_t len)
72{
73 uint8_t len_type;
74 uint8_t len_pos = 1;
75 size_t tlv_len;
76
77 /* A TLV needs at least the type byte and a single ID byte. */
78 if(len < 2) {
79 LOG_WARN("Truncated TLV header (%u bytes available).\n", (unsigned)len);
80 return 0;
81 }
82
83 tlv->type = (buffer[0] >> 6) & 3;
84 len_type = (buffer[0] >> 3) & 3;
85 len_pos = 1 + (((buffer[0] & (1 << 5)) != 0) ? 2 : 1);
86
87 /* len_pos is the offset of the length field (== header size when there
88 is no separate length field). Ensure the ID bytes and any length
89 bytes are within the buffer before reading them. */
90 if(len < (size_t)len_pos + len_type) {
91 LOG_WARN("Truncated TLV header (need %u bytes, have %u).\n",
92 (unsigned)(len_pos + len_type), (unsigned)len);
93 return 0;
94 }
95
96 tlv->id = buffer[1];
97 /* if len_pos is larger than two it means that there is more ID to read */
98 if(len_pos > 2) {
99 tlv->id = (tlv->id << 8) + buffer[2];
100 }
101
102 if(len_type == 0) {
103 tlv_len = buffer[0] & 7;
104 } else {
105 /* read the length */
106 tlv_len = 0;
107 while(len_type > 0) {
108 tlv_len = tlv_len << 8 | buffer[len_pos++];
109 len_type--;
110 }
111 }
112
113 /* Ensure the value fits within the remaining buffer. len_pos now points
114 to the first value byte and is guaranteed to be <= len. */
115 if(tlv_len > len - len_pos) {
116 LOG_WARN("Truncated TLV value (need %u bytes, have %u).\n",
117 (unsigned)tlv_len, (unsigned)(len - len_pos));
118 return 0;
119 }
120
121 /* and read out the data??? */
122 tlv->length = tlv_len;
123 tlv->value = &buffer[len_pos];
124
125 return len_pos + tlv_len;
126}
127/*---------------------------------------------------------------------------*/
128size_t
129lwm2m_tlv_get_size(const lwm2m_tlv_t *tlv)
130{
131 size_t size;
132 /* first hdr + len size */
133 size = 1 + get_len_type(tlv);
134 /* id size */
135 size += (tlv->id > 255) ? 2 : 1;
136
137 /* and the length */
138 size += tlv->length;
139 return size;
140}
141/*---------------------------------------------------------------------------*/
142/* If the tlv->value is NULL - only the header will be generated - useful for
143 * large strings or opaque streaming (block transfer)
144 */
145size_t
146lwm2m_tlv_write(const lwm2m_tlv_t *tlv, uint8_t *buffer, size_t buffersize)
147{
148 int pos;
149 uint8_t len_type;
150
151 /* len type is the same as number of bytes required for length */
152 len_type = get_len_type(tlv);
153 pos = 1 + len_type;
154 /* ensure that we do not write too much */
155 if(tlv->value != NULL && buffersize < tlv->length + pos) {
156 LOG_WARN("Could not write the TLV - buffer overflow.\n");
157 return 0;
158 }
159
160 if(buffersize < pos + 2) {
161 return 0;
162 }
163
164 /* first type byte in TLV header */
165 buffer[0] = (tlv->type << 6) |
166 (tlv->id > 255 ? (1 << 5) : 0) |
167 (len_type << 3) |
168 (len_type == 0 ? tlv->length : 0);
169
170 pos = 1;
171 /* The ID */
172 if(tlv->id > 255) {
173 buffer[pos++] = (tlv->id >> 8) & 0xff;
174 }
175 buffer[pos++] = tlv->id & 0xff;
176 /* Add length if needed - unrolled loop ? */
177 if(len_type > 2) {
178 buffer[pos++] = (tlv->length >> 16) & 0xff;
179 }
180 if(len_type > 1) {
181 buffer[pos++] = (tlv->length >> 8) & 0xff;
182 }
183 if(len_type > 0) {
184 buffer[pos++] = tlv->length & 0xff;
185 }
186
187 /* finally add the value */
188 if(tlv->value != NULL && tlv->length > 0) {
189 memcpy(&buffer[pos], tlv->value, tlv->length);
190 }
191
192 if(LOG_DBG_ENABLED) {
193 int i;
194 LOG_DBG("TLV: ");
195 for(i = 0; i < pos + ((tlv->value != NULL) ? tlv->length : 0); i++) {
196 LOG_DBG_("%02x", buffer[i]);
197 }
198 LOG_DBG_("\n");
199 }
200
201 return pos + ((tlv->value != NULL) ? tlv->length : 0);
202}
203/*---------------------------------------------------------------------------*/
204int32_t
205lwm2m_tlv_get_int32(const lwm2m_tlv_t *tlv)
206{
207 int i;
208 int32_t value = 0;
209
210 /* A zero-length integer represents the value 0. Returning here also
211 avoids reading tlv->value[0] below when there is no value at all. */
212 if(tlv->length == 0) {
213 return 0;
214 }
215
216 for(i = 0; i < tlv->length; i++) {
217 value = (value << 8) | tlv->value[i];
218 }
219
220 /* Ensure that we set all the bits above what we read in */
221 if(tlv->value[0] & 0x80) {
222 while(i < 4) {
223 value = value | (0xff << (i * 8));
224 i++;
225 }
226 }
227
228 return value;
229}
230/*---------------------------------------------------------------------------*/
231size_t
232lwm2m_tlv_write_int32(uint8_t type, int16_t id, int32_t value, uint8_t *buffer, size_t len)
233{
234 lwm2m_tlv_t tlv;
235 uint8_t buf[4];
236 int i;
237 int v;
238 int last_bit;
239 LOG_DBG("Exporting int32 %d %"PRId32" ", id, value);
240
241 v = value < 0 ? -1 : 0;
242 i = 0;
243 do {
244 buf[3 - i] = value & 0xff;
245 /* check if the last MSB indicates that we need another byte */
246 last_bit = (v == 0 && (value & 0x80) > 0) || (v == -1 && (value & 0x80) == 0);
247 value = value >> 8;
248 i++;
249 } while((value != v || last_bit) && i < 4);
250
251 /* export INT as TLV */
252 LOG_DBG("len: %d\n", i);
253 tlv.type = type;
254 tlv.length = i;
255 tlv.value = &buf[3 - (i - 1)];
256 tlv.id = id;
257 return lwm2m_tlv_write(&tlv, buffer, len);
258}
259/*---------------------------------------------------------------------------*/
260/* convert fixpoint 32-bit to a IEEE Float in the byte array*/
261size_t
262lwm2m_tlv_write_float32(uint8_t type, int16_t id, int32_t value, int bits,
263 uint8_t *buffer, size_t len)
264{
265 int i;
266 int e = 0;
267 int32_t val = 0;
268 int32_t v;
269 uint8_t b[4];
270 lwm2m_tlv_t tlv;
271
272 v = value;
273 if(v < 0) {
274 v = -v;
275 }
276
277 while(v > 1) {
278 val = (val >> 1);
279 if (v & 1) {
280 val = val | (1L << 22);
281 }
282 v = (v >> 1);
283 e++;
284 }
285
286 LOG_DBG("Sign: %d, Fraction: %06"PRIx32" 0b", value < 0, val);
287 for(i = 0; i < 23; i++) {
288 LOG_DBG_("%"PRId32"", ((val >> (22 - i)) & 1));
289 }
290 LOG_DBG_("\nExp:%d\n", e);
291
292 /* convert to the thing we should have */
293 e = e - bits + 127;
294
295 if(value == 0) {
296 e = 0;
297 }
298
299 /* is this the right byte order? */
300 b[0] = (value < 0 ? 0x80 : 0) | (e >> 1);
301 b[1] = ((e & 1) << 7) | ((val >> 16) & 0x7f);
302 b[2] = (val >> 8) & 0xff;
303 b[3] = val & 0xff;
304
305 LOG_DBG("B=%02x%02x%02x%02x\n", b[0], b[1], b[2], b[3]);
306 /* construct the TLV */
307 tlv.type = type;
308 tlv.length = 4;
309 tlv.value = b;
310 tlv.id = id;
311
312 return lwm2m_tlv_write(&tlv, buffer, len);
313}
314/*---------------------------------------------------------------------------*/
315/* convert float to fixpoint */
316size_t
317lwm2m_tlv_float32_to_fix(const lwm2m_tlv_t *tlv, int32_t *value, int bits)
318{
319 /* TLV needs to be 4 bytes */
320 int e, i;
321 int32_t val;
322 int sign;
323
324 /* The IEEE 754 single-precision representation requires exactly 4 bytes.
325 Reject anything shorter to avoid reading past the value. */
326 if(tlv->length < 4) {
327 *value = 0;
328 return 0;
329 }
330
331 sign = (tlv->value[0] & 0x80) != 0;
332 e = ((tlv->value[0] << 1) & 0xff) | (tlv->value[1] >> 7);
333 val = (((long)tlv->value[1] & 0x7f) << 16) | (tlv->value[2] << 8) | tlv->value[3];
334
335 LOG_DBG("Sign: %d, Fraction: %06"PRIx32" 0b", val < 0, val);
336 for(i = 0; i < 23; i++) {
337 LOG_DBG_("%"PRId32"", ((val >> (22 - i)) & 1));
338 }
339 LOG_DBG("\nExp:%d => %d\n", e, e - 127);
340
341 e = e - 127 + bits;
342
343 /* e corresponds to the number of times we need to roll the number */
344
345 LOG_DBG("Actual e=%d\n", e);
346 e = e - 23;
347 LOG_DBG("E after sub %d\n", e);
348 val = val | 1L << 23;
349 if(e > 0) {
350 val = val << e;
351 } else {
352 val = val >> -e;
353 }
354
355 *value = sign ? -val : val;
356 return 4;
357}
358/*---------------------------------------------------------------------------*/
359/** @} */
360
361#if 0
362int main(int argc, char *argv[])
363{
364 lwm2m_tlv_t tlv;
365 uint8_t data[24];
366 /* Make -1 */
367 tlv.length = 2;
368 tlv.value = data;
369 data[0] = 0x00;
370 data[1] = 0x80,
371
372 printf("TLV:%d\n", lwm2m_tlv_get_int32(&tlv));
373
374 printf("Len: %d\n", lwm2m_tlv_write_int32(0, 1, -0x88987f, data, 24));
375}
376#endif
Log support for CoAP.
static int value(int type)
Header file for the Contiki OMA LWM2M TLV.