Contiki 3.x
lwm2m-object.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, 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 oma-lwm2m
33  * @{
34  *
35  */
36 
37 /**
38  * \file
39  * Implementation of the Contiki OMA LWM2M object API
40  * \author
41  * Joakim Eriksson <joakime@sics.se>
42  * Niclas Finne <nfi@sics.se>
43  */
44 
45 #include "lwm2m-object.h"
46 #include <string.h>
47 /*---------------------------------------------------------------------------*/
48 int
49 lwm2m_object_is_resource_string(const lwm2m_resource_t *resource)
50 {
51  if(resource == NULL) {
52  return 0;
53  }
54  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VALUE ||
55  resource->type == LWM2M_RESOURCE_TYPE_STR_VARIABLE ||
56  resource->type == LWM2M_RESOURCE_TYPE_STR_VARIABLE_ARRAY) {
57  return 1;
58  }
59  return 0;
60 }
61 /*---------------------------------------------------------------------------*/
62 const uint8_t *
63 lwm2m_object_get_resource_string(const lwm2m_resource_t *resource,
64  const lwm2m_context_t *context)
65 {
66  if(resource == NULL || context == NULL) {
67  return NULL;
68  }
69  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VALUE) {
70  return resource->value.string.value;
71  }
72  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VARIABLE) {
73  return *(resource->value.stringvar.var);
74  }
75  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VARIABLE_ARRAY) {
76  if(context->object_instance_index < resource->value.stringvararr.count) {
77  return resource->value.stringvararr.var +
78  resource->value.stringvararr.size * context->object_instance_index;
79  }
80  return NULL;
81  }
82  /* Not a string */
83  return NULL;
84 }
85 /*---------------------------------------------------------------------------*/
86 uint16_t
87 lwm2m_object_get_resource_strlen(const lwm2m_resource_t *resource,
88  const lwm2m_context_t *context)
89 {
90  if(resource == NULL || context == NULL) {
91  return 0;
92  }
93  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VALUE) {
94  return resource->value.string.len;
95  }
96  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VARIABLE) {
97  return *(resource->value.stringvar.len);
98  }
99  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VARIABLE_ARRAY) {
100  if(context->object_instance_index < resource->value.stringvararr.count) {
101  return resource->value.stringvararr.len[context->object_instance_index];
102  }
103  return 0;
104  }
105  /* Not a string */
106  return 0;
107 }
108 /*---------------------------------------------------------------------------*/
109 int
110 lwm2m_object_set_resource_string(const lwm2m_resource_t *resource,
111  const lwm2m_context_t *context,
112  uint16_t len, const uint8_t *string)
113 {
114  if(resource == NULL || context == NULL) {
115  return 0;
116  }
117  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VARIABLE) {
118  if(len > resource->value.stringvar.size) {
119  /* Too large */
120  return 0;
121  }
122  memcpy(resource->value.stringvar.var, string, len);
123  *(resource->value.stringvar.len) = len;
124  return 1;
125  }
126  if(resource->type == LWM2M_RESOURCE_TYPE_STR_VARIABLE_ARRAY) {
127  if(context->object_instance_index < resource->value.stringvararr.count &&
128  len <= resource->value.stringvararr.size) {
129  memcpy(resource->value.stringvararr.var +
130  resource->value.stringvararr.size * context->object_instance_index,
131  string, len);
132  resource->value.stringvararr.len[context->object_instance_index] = len;
133  return 1;
134  }
135  return 0;
136  }
137  /* Not a string variable */
138  return 0;
139 }
140 /*---------------------------------------------------------------------------*/
141 int
142 lwm2m_object_is_resource_int(const lwm2m_resource_t *resource)
143 {
144  if(resource == NULL) {
145  return 0;
146  }
147  if(resource->type == LWM2M_RESOURCE_TYPE_INT_VALUE ||
148  resource->type == LWM2M_RESOURCE_TYPE_INT_VARIABLE ||
149  resource->type == LWM2M_RESOURCE_TYPE_INT_VARIABLE_ARRAY) {
150  return 1;
151  }
152  return 0;
153 }
154 /*---------------------------------------------------------------------------*/
155 int
156 lwm2m_object_get_resource_int(const lwm2m_resource_t *resource,
157  const lwm2m_context_t *context,
158  int32_t *value)
159 {
160  if(resource == NULL || context == NULL || value == NULL) {
161  return 0;
162  }
163  if(resource->type == LWM2M_RESOURCE_TYPE_INT_VALUE) {
164  *value = resource->value.integer.value;
165  return 1;
166  }
167  if(resource->type == LWM2M_RESOURCE_TYPE_INT_VARIABLE) {
168  *value = *(resource->value.integervar.var);
169  return 1;
170  }
171  if(resource->type == LWM2M_RESOURCE_TYPE_INT_VARIABLE_ARRAY) {
172  if(context->object_instance_index < resource->value.integervararr.count) {
173  *value = resource->value.integervararr.var[context->object_instance_index];
174  return 1;
175  }
176  return 0;
177  }
178  /* Not an integer */
179  return 0;
180 }
181 /*---------------------------------------------------------------------------*/
182 int
183 lwm2m_object_set_resource_int(const lwm2m_resource_t *resource,
184  const lwm2m_context_t *context,
185  int32_t value)
186 {
187  if(resource == NULL || context == NULL) {
188  return 0;
189  }
190  if(resource->type == LWM2M_RESOURCE_TYPE_INT_VARIABLE) {
191  *(resource->value.integervar.var) = value;
192  return 1;
193  }
194  if(resource->type == LWM2M_RESOURCE_TYPE_INT_VARIABLE_ARRAY) {
195  if(context->object_instance_index < resource->value.integervararr.count) {
196  resource->value.integervararr.var[context->object_instance_index] =
197  value;
198  return 1;
199  }
200  return 0;
201  }
202  /* Not an integer variable */
203  return 0;
204 }
205 /*---------------------------------------------------------------------------*/
206 int
207 lwm2m_object_is_resource_floatfix(const lwm2m_resource_t *resource)
208 {
209  if(resource == NULL) {
210  return 0;
211  }
212  if(resource->type == LWM2M_RESOURCE_TYPE_FLOATFIX_VALUE ||
213  resource->type == LWM2M_RESOURCE_TYPE_FLOATFIX_VARIABLE ||
214  resource->type == LWM2M_RESOURCE_TYPE_FLOATFIX_VARIABLE_ARRAY) {
215  return 1;
216  }
217  return 0;
218 }
219 /*---------------------------------------------------------------------------*/
220 int
221 lwm2m_object_get_resource_floatfix(const lwm2m_resource_t *resource,
222  const lwm2m_context_t *context,
223  int32_t *value)
224 {
225  if(resource == NULL || context == NULL || value == NULL) {
226  return 0;
227  }
228  if(resource->type == LWM2M_RESOURCE_TYPE_FLOATFIX_VALUE) {
229  *value = resource->value.floatfix.value;
230  return 1;
231  }
232  if(resource->type == LWM2M_RESOURCE_TYPE_FLOATFIX_VARIABLE) {
233  *value = *(resource->value.floatfixvar.var);
234  return 1;
235  }
236  if(resource->type == LWM2M_RESOURCE_TYPE_FLOATFIX_VARIABLE_ARRAY) {
237  if(context->object_instance_index < resource->value.floatfixvararr.count) {
238  *value = resource->value.floatfixvararr.var[context->object_instance_index];
239  return 1;
240  }
241  return 0;
242  }
243  /* Not an float */
244  return 0;
245 }
246 /*---------------------------------------------------------------------------*/
247 int
248 lwm2m_object_set_resource_floatfix(const lwm2m_resource_t *resource,
249  const lwm2m_context_t *context,
250  int32_t value)
251 {
252  if(resource == NULL || context == NULL) {
253  return 0;
254  }
255  if(resource->type == LWM2M_RESOURCE_TYPE_FLOATFIX_VARIABLE) {
256  *(resource->value.floatfixvar.var) = value;
257  return 1;
258  }
259  if(resource->type == LWM2M_RESOURCE_TYPE_FLOATFIX_VARIABLE_ARRAY) {
260  if(context->object_instance_index < resource->value.floatfixvararr.count) {
261  resource->value.floatfixvararr.var[context->object_instance_index] =
262  value;
263  return 1;
264  }
265  return 0;
266  }
267  /* Not an float variable */
268  return 0;
269 }
270 /*---------------------------------------------------------------------------*/
271 int
272 lwm2m_object_is_resource_boolean(const lwm2m_resource_t *resource)
273 {
274  if(resource == NULL) {
275  return 0;
276  }
277  if(resource->type == LWM2M_RESOURCE_TYPE_BOOLEAN_VALUE ||
278  resource->type == LWM2M_RESOURCE_TYPE_BOOLEAN_VARIABLE ||
279  resource->type == LWM2M_RESOURCE_TYPE_BOOLEAN_VARIABLE_ARRAY) {
280  return 1;
281  }
282  return 0;
283 }
284 /*---------------------------------------------------------------------------*/
285 int
286 lwm2m_object_get_resource_boolean(const lwm2m_resource_t *resource,
287  const lwm2m_context_t *context,
288  int *value)
289 {
290  if(resource == NULL || context == NULL || value == NULL) {
291  return 0;
292  }
293  if(resource->type == LWM2M_RESOURCE_TYPE_BOOLEAN_VALUE) {
294  *value = resource->value.boolean.value;
295  return 1;
296  }
297  if(resource->type == LWM2M_RESOURCE_TYPE_BOOLEAN_VARIABLE) {
298  *value = *(resource->value.booleanvar.var);
299  return 1;
300  }
301  if(resource->type == LWM2M_RESOURCE_TYPE_BOOLEAN_VARIABLE_ARRAY) {
302  if(context->object_instance_index < resource->value.booleanvararr.count) {
303  *value = resource->value.booleanvararr.var[context->object_instance_index];
304  return 1;
305  }
306  return 0;
307  }
308  /* Not a boolean */
309  return 0;
310 }
311 /*---------------------------------------------------------------------------*/
312 int
313 lwm2m_object_set_resource_boolean(const lwm2m_resource_t *resource,
314  const lwm2m_context_t *context,
315  int value)
316 {
317  if(resource == NULL || context == NULL) {
318  return 0;
319  }
320  if(resource->type == LWM2M_RESOURCE_TYPE_BOOLEAN_VARIABLE) {
321  *(resource->value.booleanvar.var) = value;
322  return 1;
323  }
324  if(resource->type == LWM2M_RESOURCE_TYPE_BOOLEAN_VARIABLE_ARRAY) {
325  if(context->object_instance_index < resource->value.booleanvararr.count) {
326  resource->value.booleanvararr.var[context->object_instance_index] =
327  value;
328  return 1;
329  }
330  return 0;
331  }
332  /* Not a boolean variable */
333  return 0;
334 }
335 /*---------------------------------------------------------------------------*/
336 /** @} */
Header file for the Contiki OMA LWM2M object API
static struct sicslowpan_addr_context * context
Addresses contexts for IPHC.
Definition: sicslowpan.c:501
#define NULL
The null pointer.