1 /*
2  * Copyright (c) 2017 Linaro Limited
3  * Copyright (c) 2018-2019 Foundries.io
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /*
9  * Copyright (c) 2015, Yanzi Networks AB.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the copyright holder nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35  * OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Original Authors:
40  *         Joakim Eriksson <joakime@sics.se>
41  *         Niclas Finne <nfi@sics.se>
42  */
43 
44 /*
45  * Zephyr Contribution by Michael Scott <michael.scott@linaro.org>
46  * - Zephyr code style changes / code cleanup
47  * - Move to Zephyr APIs where possible
48  * - Convert to Zephyr int/uint types
49  * - Remove engine dependency (replace with writer context)
50  * - Add write int64 function
51  */
52 
53 /*
54  * TODO:
55  * - Type cleanups
56  * - Cleanup integer parsing
57  */
58 
59 #define LOG_MODULE_NAME net_lwm2m_plain_text
60 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
61 
62 #include <zephyr/logging/log.h>
63 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
64 
65 #include <stdarg.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <ctype.h>
70 
71 #include "lwm2m_object.h"
72 #include "lwm2m_rw_plain_text.h"
73 #include "lwm2m_engine.h"
74 #include "lwm2m_util.h"
75 
76 /* some temporary buffer space for format conversions */
77 static char pt_buffer[42];
78 
plain_text_put_format(struct lwm2m_output_context * out,const char * format,...)79 int plain_text_put_format(struct lwm2m_output_context *out, const char *format,
80 			  ...)
81 {
82 	va_list vargs;
83 	int n, ret;
84 
85 	va_start(vargs, format);
86 	n = vsnprintk(pt_buffer, sizeof(pt_buffer), format, vargs);
87 	va_end(vargs);
88 	if (n < 0 || n >= sizeof(pt_buffer)) {
89 		return -EINVAL;
90 	}
91 
92 	ret = buf_append(CPKT_BUF_WRITE(out->out_cpkt), pt_buffer, n);
93 	if (ret < 0) {
94 		return ret;
95 	}
96 
97 	return n;
98 }
99 
put_s32(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,int32_t value)100 static int put_s32(struct lwm2m_output_context *out,
101 		   struct lwm2m_obj_path *path, int32_t value)
102 {
103 	return plain_text_put_format(out, "%d", value);
104 }
105 
put_s8(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,int8_t value)106 static int put_s8(struct lwm2m_output_context *out, struct lwm2m_obj_path *path,
107 		  int8_t value)
108 {
109 	return plain_text_put_format(out, "%d", value);
110 }
111 
put_s16(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,int16_t value)112 static int put_s16(struct lwm2m_output_context *out,
113 		   struct lwm2m_obj_path *path, int16_t value)
114 {
115 	return plain_text_put_format(out, "%d", value);
116 }
117 
put_s64(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,int64_t value)118 static int put_s64(struct lwm2m_output_context *out,
119 		   struct lwm2m_obj_path *path, int64_t value)
120 {
121 	return plain_text_put_format(out, "%lld", value);
122 }
123 
put_time(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,time_t value)124 static int put_time(struct lwm2m_output_context *out,
125 		   struct lwm2m_obj_path *path, time_t value)
126 {
127 	return plain_text_put_format(out, "%lld", (int64_t)value);
128 }
129 
plain_text_put_float(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,double * value)130 int plain_text_put_float(struct lwm2m_output_context *out,
131 			 struct lwm2m_obj_path *path, double *value)
132 {
133 	int len, ret;
134 
135 	len = lwm2m_ftoa(value, pt_buffer, sizeof(pt_buffer), 15);
136 	if (len < 0 || len >= sizeof(pt_buffer)) {
137 		LOG_ERR("Failed to encode float value");
138 		return -EINVAL;
139 	}
140 
141 	ret = buf_append(CPKT_BUF_WRITE(out->out_cpkt), pt_buffer, len);
142 	if (ret < 0) {
143 		return ret;
144 	}
145 
146 	return len;
147 }
148 
put_string(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,char * buf,size_t buflen)149 static int put_string(struct lwm2m_output_context *out,
150 		      struct lwm2m_obj_path *path, char *buf, size_t buflen)
151 {
152 	int ret;
153 
154 	ret = buf_append(CPKT_BUF_WRITE(out->out_cpkt), buf, buflen);
155 	if (ret < 0) {
156 		return ret;
157 	}
158 
159 	return buflen;
160 }
161 
put_bool(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,bool value)162 static int put_bool(struct lwm2m_output_context *out,
163 		    struct lwm2m_obj_path *path, bool value)
164 {
165 	if (value) {
166 		return plain_text_put_format(out, "%u", 1);
167 	} else {
168 		return plain_text_put_format(out, "%u", 0);
169 	}
170 }
171 
put_objlnk(struct lwm2m_output_context * out,struct lwm2m_obj_path * path,struct lwm2m_objlnk * value)172 static int put_objlnk(struct lwm2m_output_context *out,
173 		      struct lwm2m_obj_path *path, struct lwm2m_objlnk *value)
174 {
175 	return plain_text_put_format(out, "%u:%u", value->obj_id,
176 				     value->obj_inst);
177 }
178 
plain_text_read_int(struct lwm2m_input_context * in,int64_t * value,bool accept_sign)179 static int plain_text_read_int(struct lwm2m_input_context *in, int64_t *value,
180 			       bool accept_sign)
181 {
182 	int i = 0;
183 	bool neg = false;
184 	uint64_t temp;
185 	uint8_t c;
186 
187 	if (in->offset >= in->in_cpkt->offset) {
188 		/* No remaining data in the payload. */
189 		return -ENODATA;
190 	}
191 
192 	/* initialize values to 0 */
193 	temp = 0;
194 
195 	while (in->offset < in->in_cpkt->offset) {
196 		if (buf_read_u8(&c, CPKT_BUF_READ(in->in_cpkt),
197 				&in->offset) < 0) {
198 			break;
199 		}
200 
201 		if (c == '-' && accept_sign && i == 0) {
202 			neg = true;
203 		} else if (isdigit(c) != 0) {
204 			temp = temp * 10ULL + (c - '0');
205 			if (temp > ((uint64_t)INT64_MAX + (neg ? 1ULL : 0ULL))) {
206 				return -EINVAL;
207 			}
208 		} else {
209 			/* anything else stop reading */
210 			in->offset--;
211 			break;
212 		}
213 
214 		i++;
215 	}
216 
217 	*value = neg ? -temp : temp;
218 
219 	return i;
220 }
221 
get_s32(struct lwm2m_input_context * in,int32_t * value)222 static int get_s32(struct lwm2m_input_context *in, int32_t *value)
223 {
224 	int64_t tmp = 0;
225 	size_t len = 0;
226 
227 	len = plain_text_read_int(in, &tmp, true);
228 	if (len > 0) {
229 		*value = (int32_t)tmp;
230 	}
231 
232 	return len;
233 }
234 
get_s64(struct lwm2m_input_context * in,int64_t * value)235 static int get_s64(struct lwm2m_input_context *in, int64_t *value)
236 {
237 	return plain_text_read_int(in, value, true);
238 }
239 
get_time(struct lwm2m_input_context * in,time_t * value)240 static int get_time(struct lwm2m_input_context *in, time_t *value)
241 {
242 	int64_t temp64;
243 	int ret;
244 
245 	ret = plain_text_read_int(in, &temp64, true);
246 	*value = (time_t)temp64;
247 
248 	return ret;
249 }
250 
get_string(struct lwm2m_input_context * in,uint8_t * value,size_t buflen)251 static int get_string(struct lwm2m_input_context *in, uint8_t *value,
252 		      size_t buflen)
253 {
254 	uint16_t in_len;
255 
256 	coap_packet_get_payload(in->in_cpkt, &in_len);
257 
258 	if (in_len + 1 > buflen) {
259 		LOG_ERR("Buffer too small to accommodate string");
260 		return -ENOMEM;
261 	}
262 
263 	if (buf_read(value, in_len, CPKT_BUF_READ(in->in_cpkt),
264 		     &in->offset) < 0) {
265 		value[0] = '\0';
266 		return 0;
267 	}
268 
269 	value[in_len] = '\0';
270 	return (size_t)in_len;
271 }
272 
get_float(struct lwm2m_input_context * in,double * value)273 static int get_float(struct lwm2m_input_context *in, double *value)
274 {
275 	size_t i = 0, len = 0;
276 	bool has_dot = false;
277 	uint8_t tmp, buf[24];
278 	int ret;
279 
280 	if (in->offset >= in->in_cpkt->offset) {
281 		/* No remaining data in the payload. */
282 		return -ENODATA;
283 	}
284 
285 	while (in->offset < in->in_cpkt->offset) {
286 		if (buf_read_u8(&tmp, CPKT_BUF_READ(in->in_cpkt),
287 				&in->offset) < 0) {
288 			break;
289 		}
290 
291 		if ((tmp == '-' && i == 0) || (tmp == '.' && !has_dot) ||
292 		    isdigit(tmp) != 0) {
293 			len++;
294 
295 			/* Copy only if it fits into provided buffer - we won't
296 			 * get better precision anyway.
297 			 */
298 			if (i < sizeof(buf) - 1) {
299 				buf[i++] = tmp;
300 			}
301 
302 			if (tmp == '.') {
303 				has_dot = true;
304 			}
305 		} else {
306 			/* anything else stop reading */
307 			in->offset--;
308 			break;
309 		}
310 	}
311 
312 	buf[i] = '\0';
313 
314 	ret = lwm2m_atof(buf, value);
315 	if (ret != 0) {
316 		LOG_ERR("Failed to parse float value");
317 		return -EBADMSG;
318 	}
319 
320 	return len;
321 }
322 
get_bool(struct lwm2m_input_context * in,bool * value)323 static int get_bool(struct lwm2m_input_context *in, bool *value)
324 {
325 	uint8_t tmp;
326 	int ret;
327 
328 	if (in->offset >= in->in_cpkt->offset) {
329 		/* No remaining data in the payload. */
330 		return -ENODATA;
331 	}
332 
333 	ret = buf_read_u8(&tmp, CPKT_BUF_READ(in->in_cpkt), &in->offset);
334 	if (ret < 0) {
335 		return ret;
336 	}
337 
338 	if (tmp != '1' && tmp != '0') {
339 		return -EBADMSG;
340 	}
341 
342 	*value = (tmp == '1') ? true : false;
343 	return sizeof(uint8_t);
344 }
345 
get_objlnk(struct lwm2m_input_context * in,struct lwm2m_objlnk * value)346 static int get_objlnk(struct lwm2m_input_context *in,
347 		      struct lwm2m_objlnk *value)
348 {
349 	int64_t tmp;
350 	int len, total_len;
351 
352 	len = plain_text_read_int(in, &tmp, false);
353 	if (len <= 0) {
354 		return -ENODATA;
355 	}
356 
357 	total_len = len;
358 	value->obj_id = (uint16_t)tmp;
359 
360 	/* Skip ':' delimiter. */
361 	total_len++;
362 	in->offset++;
363 
364 	len = plain_text_read_int(in, &tmp, false);
365 	if (len <= 0) {
366 		return -ENODATA;
367 	}
368 
369 	total_len += len;
370 	value->obj_inst = (uint16_t)tmp;
371 
372 	return total_len;
373 }
374 
375 const struct lwm2m_writer plain_text_writer = {
376 	.put_s8 = put_s8,
377 	.put_s16 = put_s16,
378 	.put_s32 = put_s32,
379 	.put_s64 = put_s64,
380 	.put_string = put_string,
381 	.put_float = plain_text_put_float,
382 	.put_time = put_time,
383 	.put_bool = put_bool,
384 	.put_objlnk = put_objlnk,
385 };
386 
387 const struct lwm2m_reader plain_text_reader = {
388 	.get_s32 = get_s32,
389 	.get_s64 = get_s64,
390 	.get_string = get_string,
391 	.get_time = get_time,
392 	.get_float = get_float,
393 	.get_bool = get_bool,
394 	.get_objlnk = get_objlnk,
395 };
396 
do_read_op_plain_text(struct lwm2m_message * msg,int content_format)397 int do_read_op_plain_text(struct lwm2m_message *msg, int content_format)
398 {
399 	/* Plain text can only return single resource (instance) */
400 	if (msg->path.level < LWM2M_PATH_LEVEL_RESOURCE) {
401 		return -EPERM;
402 	} else if (msg->path.level > LWM2M_PATH_LEVEL_RESOURCE) {
403 		if (!IS_ENABLED(CONFIG_LWM2M_VERSION_1_1)) {
404 			return -ENOENT;
405 		} else if (msg->path.level > LWM2M_PATH_LEVEL_RESOURCE_INST) {
406 			return -ENOENT;
407 		}
408 	}
409 
410 	return lwm2m_perform_read_op(msg, content_format);
411 }
412 
do_write_op_plain_text(struct lwm2m_message * msg)413 int do_write_op_plain_text(struct lwm2m_message *msg)
414 {
415 	struct lwm2m_engine_obj_inst *obj_inst = NULL;
416 	struct lwm2m_engine_obj_field *obj_field;
417 	struct lwm2m_engine_res *res = NULL;
418 	struct lwm2m_engine_res_inst *res_inst = NULL;
419 	int ret;
420 	uint8_t created = 0U;
421 
422 	ret = lwm2m_get_or_create_engine_obj(msg, &obj_inst, &created);
423 	if (ret < 0) {
424 		return ret;
425 	}
426 
427 	ret = lwm2m_engine_validate_write_access(msg, obj_inst, &obj_field);
428 	if (ret < 0) {
429 		return ret;
430 	}
431 
432 	ret = lwm2m_engine_get_create_res_inst(&msg->path, &res, &res_inst);
433 	if (ret < 0) {
434 		return -ENOENT;
435 	}
436 
437 	if (msg->path.level < 3) {
438 		msg->path.level = 3U;
439 	}
440 
441 	return lwm2m_write_handler(obj_inst, res, res_inst, obj_field, msg);
442 }
443