Contiki 3.x
lwm2m-json.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, Eistec 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  * \file
38  * Implementation of the Contiki OMA LWM2M JSON writer
39  * \author
40  * Joakim NohlgĂ„rd <joakim.nohlgard@eistec.se>
41  */
42 
43 #include "lwm2m-object.h"
44 #include "lwm2m-json.h"
45 #include "lwm2m-plain-text.h"
46 #include <stdio.h>
47 #include <stddef.h>
48 #include <stdint.h>
49 #include <inttypes.h>
50 
51 #define DEBUG 0
52 #if DEBUG
53 #define PRINTF(...) printf(__VA_ARGS__)
54 #else
55 #define PRINTF(...)
56 #endif
57 
58 /*---------------------------------------------------------------------------*/
59 static size_t
60 write_boolean(const lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
61  int value)
62 {
63  int len = snprintf((char *)outbuf, outlen, "{\"e\":[{\"n\":\"%u\",\"bv\":%s}]}\n", ctx->resource_id, value ? "true" : "false");
64  if((len < 0) || (len >= outlen)) {
65  return 0;
66  }
67  return len;
68 }
69 /*---------------------------------------------------------------------------*/
70 static size_t
71 write_int(const lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
72  int32_t value)
73 {
74  int len = snprintf((char *)outbuf, outlen, "{\"e\":[{\"n\":\"%u\",\"v\":%" PRId32 "}]}\n", ctx->resource_id, value);
75  if((len < 0) || (len >= outlen)) {
76  return 0;
77  }
78  return len;
79 }
80 /*---------------------------------------------------------------------------*/
81 static size_t
82 write_float32fix(const lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
83  int32_t value, int bits)
84 {
85  size_t len = 0;
86  int res;
87  res = snprintf((char *)outbuf, outlen, "{\"e\":[{\"n\":\"%u\",\"v\":", ctx->resource_id);
88  if(res <= 0 || res >= outlen) {
89  return 0;
90  }
91  len += res;
92  outlen -= res;
93  res = lwm2m_plain_text_write_float32fix(&outbuf[len], outlen, value, bits);
94  if((res <= 0) || (res >= outlen)) {
95  return 0;
96  }
97  len += res;
98  outlen -= res;
99  res = snprintf((char *)&outbuf[len], outlen, "}]}\n");
100  if((res <= 0) || (res >= outlen)) {
101  return 0;
102  }
103  len += res;
104  return len;
105 }
106 /*---------------------------------------------------------------------------*/
107 static size_t
108 write_string(const lwm2m_context_t *ctx, uint8_t *outbuf, size_t outlen,
109  const char *value, size_t stringlen)
110 {
111  size_t i;
112  size_t len = 0;
113  int res;
114  PRINTF("{\"e\":[{\"n\":\"%u\",\"sv\":\"", ctx->resource_id);
115  res = snprintf((char *)outbuf, outlen, "{\"e\":[{\"n\":\"%u\",\"sv\":\"", ctx->resource_id);
116  if(res < 0 || res >= outlen) {
117  return 0;
118  }
119  len += res;
120  for (i = 0; i < stringlen && len < outlen; ++i) {
121  /* Escape special characters */
122  /* TODO: Handle UTF-8 strings */
123  if(value[i] < '\x20') {
124  PRINTF("\\x%x", value[i]);
125  res = snprintf((char *)&outbuf[len], outlen - len, "\\x%x", value[i]);
126  if((res < 0) || (res >= (outlen - len))) {
127  return 0;
128  }
129  len += res;
130  continue;
131  } else if(value[i] == '"' || value[i] == '\\') {
132  PRINTF("\\");
133  outbuf[len] = '\\';
134  ++len;
135  if(len >= outlen) {
136  return 0;
137  }
138  }
139  PRINTF("%c", value[i]);
140  outbuf[len] = value[i];
141  ++len;
142  if(len >= outlen) {
143  return 0;
144  }
145  }
146  PRINTF("\"}]}\n");
147  res = snprintf((char *)&outbuf[len], outlen - len, "\"}]}\n");
148  if((res < 0) || (res >= (outlen - len))) {
149  return 0;
150  }
151  len += res;
152  return len;
153 }
154 /*---------------------------------------------------------------------------*/
155 const lwm2m_writer_t lwm2m_json_writer = {
156  write_int,
157  write_string,
158  write_float32fix,
159  write_boolean
160 };
161 /*---------------------------------------------------------------------------*/
162 /** @} */
Header file for the Contiki OMA LWM2M object API
Header file for the Contiki OMA LWM2M plain text reader / writer
Header file for the Contiki OMA LWM2M JSON writer