Contiki-NG
lwm2m-tlv-writer.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 writer
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.h"
47 
48 /* Log configuration */
49 #include "coap-log.h"
50 #define LOG_MODULE "lwm2m-tlv"
51 #define LOG_LEVEL LOG_LEVEL_NONE
52 
53 /*---------------------------------------------------------------------------*/
54 static size_t
55 init_write(lwm2m_context_t *ctx)
56 {
57  return 0;
58 }
59 /*---------------------------------------------------------------------------*/
60 static size_t
61 end_write(lwm2m_context_t *ctx)
62 {
63  return 0;
64 }
65 /*---------------------------------------------------------------------------*/
66 static size_t
67 write_int_tlv(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
68  int32_t value)
69 {
70  uint8_t type = ctx->writer_flags & WRITER_RESOURCE_INSTANCE ?
71  LWM2M_TLV_TYPE_RESOURCE_INSTANCE : LWM2M_TLV_TYPE_RESOURCE;
72  uint16_t id = ctx->writer_flags & WRITER_RESOURCE_INSTANCE ?
73  ctx->resource_instance_id : ctx->resource_id;
74  return lwm2m_tlv_write_int32(type, id, value, outbuf, outlen);
75 }
76 /*---------------------------------------------------------------------------*/
77 static size_t
78 write_boolean_tlv(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
79  int value)
80 {
81  return write_int_tlv(ctx, outbuf, outlen, value != 0 ? 1 : 0);
82 }
83 /*---------------------------------------------------------------------------*/
84 static size_t
85 write_float32fix_tlv(lwm2m_context_t *ctx, uint8_t *outbuf,
86  size_t outlen, int32_t value, int bits)
87 {
88  uint8_t type = ctx->writer_flags & WRITER_RESOURCE_INSTANCE ?
89  LWM2M_TLV_TYPE_RESOURCE_INSTANCE : LWM2M_TLV_TYPE_RESOURCE;
90  uint16_t id = ctx->writer_flags & WRITER_RESOURCE_INSTANCE ?
91  ctx->resource_instance_id : ctx->resource_id;
92  return lwm2m_tlv_write_float32(type, id, value, bits, outbuf, outlen);
93 }
94 /*---------------------------------------------------------------------------*/
95 static size_t
96 write_string_tlv(lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
97  const char *value, size_t stringlen)
98 {
99  lwm2m_tlv_t tlv;
100  tlv.type = ctx->writer_flags & WRITER_RESOURCE_INSTANCE ?
101  LWM2M_TLV_TYPE_RESOURCE_INSTANCE : LWM2M_TLV_TYPE_RESOURCE;
102  tlv.value = (uint8_t *) value;
103  tlv.length = (uint32_t) stringlen;
104  tlv.id = ctx->resource_id;
105  return lwm2m_tlv_write(&tlv, outbuf, outlen);
106 }
107 /*---------------------------------------------------------------------------*/
108 static size_t
109 write_opaque_header(lwm2m_context_t *ctx, size_t payloadsize)
110 {
111  lwm2m_tlv_t tlv;
112  tlv.type = LWM2M_TLV_TYPE_RESOURCE;
113  tlv.value = (uint8_t *) NULL;
114  tlv.length = (uint32_t) payloadsize;
115  tlv.id = ctx->resource_id;
116  return lwm2m_tlv_write(&tlv, &ctx->outbuf->buffer[ctx->outbuf->len],
117  ctx->outbuf->size - ctx->outbuf->len);
118 }
119 /*---------------------------------------------------------------------------*/
120 static size_t
121 enter_sub(lwm2m_context_t *ctx)
122 {
123  /* set some flags in state */
124  lwm2m_tlv_t tlv;
125  int len = 0;
126  LOG_DBG("Enter sub-resource rsc=%d mark:%d\n", ctx->resource_id, ctx->outbuf->len);
127  ctx->writer_flags |= WRITER_RESOURCE_INSTANCE;
128  tlv.type = LWM2M_TLV_TYPE_MULTI_RESOURCE;
129  tlv.length = 8; /* create an 8-bit TLV */
130  tlv.value = NULL;
131  tlv.id = ctx->resource_id;
132  len = lwm2m_tlv_write(&tlv, &ctx->outbuf->buffer[ctx->outbuf->len],
133  ctx->outbuf->size - ctx->outbuf->len);
134  /* store position for deciding where to re-write the TLV when we
135  know the length - NOTE: either this or memmov of buffer later... */
136  ctx->out_mark_pos_ri = ctx->outbuf->len;
137  return len;
138 }
139 /*---------------------------------------------------------------------------*/
140 static size_t
141 exit_sub(lwm2m_context_t *ctx)
142 {
143  /* clear out state info */
144  int pos = 2; /* this is the lenght pos */
145  int len;
146  ctx->writer_flags &= ~WRITER_RESOURCE_INSTANCE;
147 
148  if(ctx->resource_id > 0xff) {
149  pos++;
150  }
151  len = ctx->outbuf->len - ctx->out_mark_pos_ri;
152 
153  LOG_DBG("Exit sub-resource rsc=%d mark:%d len=%d\n", ctx->resource_id,
154  ctx->out_mark_pos_ri, len);
155 
156  /* update the lenght byte... Assume TLV header is pos + 1 bytes. */
157  ctx->outbuf->buffer[pos + ctx->out_mark_pos_ri] = len - (pos + 1);
158  return 0;
159 }
160 /*---------------------------------------------------------------------------*/
161 const lwm2m_writer_t lwm2m_tlv_writer = {
162  init_write,
163  end_write,
164  enter_sub,
165  exit_sub,
166  write_int_tlv,
167  write_string_tlv,
168  write_float32fix_tlv,
169  write_boolean_tlv,
170  write_opaque_header
171 };
172 /*---------------------------------------------------------------------------*/
173 /** @} */
Log support for CoAP
Header file for the LWM2M object API
Header file for the Contiki OMA LWM2M TLV