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 	uint8_t tmp;
185 
186 	if (in->offset >= in->in_cpkt->offset) {
187 		/* No remaining data in the payload. */
188 		return -ENODATA;
189 	}
190 
191 	/* initialize values to 0 */
192 	*value = 0;
193 
194 	while (in->offset < in->in_cpkt->offset) {
195 		if (buf_read_u8(&tmp, CPKT_BUF_READ(in->in_cpkt),
196 				&in->offset) < 0) {
197 			break;
198 		}
199 
200 		if (tmp == '-' && accept_sign && i == 0) {
201 			neg = true;
202 		} else if (isdigit(tmp) != 0) {
203 			*value = *value * 10 + (tmp - '0');
204 		} else {
205 			/* anything else stop reading */
206 			in->offset--;
207 			break;
208 		}
209 
210 		i++;
211 	}
212 
213 	if (neg) {
214 		*value = -*value;
215 	}
216 
217 	return i;
218 }
219 
get_s32(struct lwm2m_input_context * in,int32_t * value)220 static int get_s32(struct lwm2m_input_context *in, int32_t *value)
221 {
222 	int64_t tmp = 0;
223 	size_t len = 0;
224 
225 	len = plain_text_read_int(in, &tmp, true);
226 	if (len > 0) {
227 		*value = (int32_t)tmp;
228 	}
229 
230 	return len;
231 }
232 
get_s64(struct lwm2m_input_context * in,int64_t * value)233 static int get_s64(struct lwm2m_input_context *in, int64_t *value)
234 {
235 	return plain_text_read_int(in, value, true);
236 }
237 
get_time(struct lwm2m_input_context * in,time_t * value)238 static int get_time(struct lwm2m_input_context *in, time_t *value)
239 {
240 	int64_t temp64;
241 	int ret;
242 
243 	ret = plain_text_read_int(in, &temp64, true);
244 	*value = (time_t)temp64;
245 
246 	return ret;
247 }
248 
get_string(struct lwm2m_input_context * in,uint8_t * value,size_t buflen)249 static int get_string(struct lwm2m_input_context *in, uint8_t *value,
250 		      size_t buflen)
251 {
252 	uint16_t in_len;
253 
254 	coap_packet_get_payload(in->in_cpkt, &in_len);
255 
256 	if (in_len > buflen) {
257 		LOG_ERR("Buffer too small to accommodate string, truncating");
258 		in_len = buflen - 1;
259 	}
260 
261 	if (buf_read(value, in_len, CPKT_BUF_READ(in->in_cpkt),
262 		     &in->offset) < 0) {
263 		value[0] = '\0';
264 		return 0;
265 	}
266 
267 	value[in_len] = '\0';
268 	return (size_t)in_len;
269 }
270 
get_float(struct lwm2m_input_context * in,double * value)271 static int get_float(struct lwm2m_input_context *in, double *value)
272 {
273 	size_t i = 0, len = 0;
274 	bool has_dot = false;
275 	uint8_t tmp, buf[24];
276 	int ret;
277 
278 	if (in->offset >= in->in_cpkt->offset) {
279 		/* No remaining data in the payload. */
280 		return -ENODATA;
281 	}
282 
283 	while (in->offset < in->in_cpkt->offset) {
284 		if (buf_read_u8(&tmp, CPKT_BUF_READ(in->in_cpkt),
285 				&in->offset) < 0) {
286 			break;
287 		}
288 
289 		if ((tmp == '-' && i == 0) || (tmp == '.' && !has_dot) ||
290 		    isdigit(tmp) != 0) {
291 			len++;
292 
293 			/* Copy only if it fits into provided buffer - we won't
294 			 * get better precision anyway.
295 			 */
296 			if (i < sizeof(buf) - 1) {
297 				buf[i++] = tmp;
298 			}
299 
300 			if (tmp == '.') {
301 				has_dot = true;
302 			}
303 		} else {
304 			/* anything else stop reading */
305 			in->offset--;
306 			break;
307 		}
308 	}
309 
310 	buf[i] = '\0';
311 
312 	ret = lwm2m_atof(buf, value);
313 	if (ret != 0) {
314 		LOG_ERR("Failed to parse float value");
315 		return -EBADMSG;
316 	}
317 
318 	return len;
319 }
320 
get_bool(struct lwm2m_input_context * in,bool * value)321 static int get_bool(struct lwm2m_input_context *in, bool *value)
322 {
323 	uint8_t tmp;
324 	int ret;
325 
326 	if (in->offset >= in->in_cpkt->offset) {
327 		/* No remaining data in the payload. */
328 		return -ENODATA;
329 	}
330 
331 	ret = buf_read_u8(&tmp, CPKT_BUF_READ(in->in_cpkt), &in->offset);
332 	if (ret < 0) {
333 		return ret;
334 	}
335 
336 	if (tmp != '1' && tmp != '0') {
337 		return -EBADMSG;
338 	}
339 
340 	*value = (tmp == '1') ? true : false;
341 	return sizeof(uint8_t);
342 }
343 
get_objlnk(struct lwm2m_input_context * in,struct lwm2m_objlnk * value)344 static int get_objlnk(struct lwm2m_input_context *in,
345 		      struct lwm2m_objlnk *value)
346 {
347 	int64_t tmp;
348 	int len, total_len;
349 
350 	len = plain_text_read_int(in, &tmp, false);
351 	if (len <= 0) {
352 		return -ENODATA;
353 	}
354 
355 	total_len = len;
356 	value->obj_id = (uint16_t)tmp;
357 
358 	/* Skip ':' delimiter. */
359 	total_len++;
360 	in->offset++;
361 
362 	len = plain_text_read_int(in, &tmp, false);
363 	if (len <= 0) {
364 		return -ENODATA;
365 	}
366 
367 	total_len += len;
368 	value->obj_inst = (uint16_t)tmp;
369 
370 	return total_len;
371 }
372 
373 const struct lwm2m_writer plain_text_writer = {
374 	.put_s8 = put_s8,
375 	.put_s16 = put_s16,
376 	.put_s32 = put_s32,
377 	.put_s64 = put_s64,
378 	.put_string = put_string,
379 	.put_float = plain_text_put_float,
380 	.put_time = put_time,
381 	.put_bool = put_bool,
382 	.put_objlnk = put_objlnk,
383 };
384 
385 const struct lwm2m_reader plain_text_reader = {
386 	.get_s32 = get_s32,
387 	.get_s64 = get_s64,
388 	.get_string = get_string,
389 	.get_time = get_time,
390 	.get_float = get_float,
391 	.get_bool = get_bool,
392 	.get_objlnk = get_objlnk,
393 };
394 
do_read_op_plain_text(struct lwm2m_message * msg,int content_format)395 int do_read_op_plain_text(struct lwm2m_message *msg, int content_format)
396 {
397 	/* Plain text can only return single resource (instance) */
398 	if (msg->path.level < LWM2M_PATH_LEVEL_RESOURCE) {
399 		return -EPERM;
400 	} else if (msg->path.level > LWM2M_PATH_LEVEL_RESOURCE) {
401 		if (!IS_ENABLED(CONFIG_LWM2M_VERSION_1_1)) {
402 			return -ENOENT;
403 		} else if (msg->path.level > LWM2M_PATH_LEVEL_RESOURCE_INST) {
404 			return -ENOENT;
405 		}
406 	}
407 
408 	return lwm2m_perform_read_op(msg, content_format);
409 }
410 
do_write_op_plain_text(struct lwm2m_message * msg)411 int do_write_op_plain_text(struct lwm2m_message *msg)
412 {
413 	struct lwm2m_engine_obj_inst *obj_inst = NULL;
414 	struct lwm2m_engine_obj_field *obj_field;
415 	struct lwm2m_engine_res *res = NULL;
416 	struct lwm2m_engine_res_inst *res_inst = NULL;
417 	int ret;
418 	uint8_t created = 0U;
419 
420 	ret = lwm2m_get_or_create_engine_obj(msg, &obj_inst, &created);
421 	if (ret < 0) {
422 		return ret;
423 	}
424 
425 	ret = lwm2m_engine_validate_write_access(msg, obj_inst, &obj_field);
426 	if (ret < 0) {
427 		return ret;
428 	}
429 
430 	ret = lwm2m_engine_get_create_res_inst(&msg->path, &res, &res_inst);
431 	if (ret < 0) {
432 		return -ENOENT;
433 	}
434 
435 	if (msg->path.level < 3) {
436 		msg->path.level = 3U;
437 	}
438 
439 	return lwm2m_write_handler(obj_inst, res, res_inst, obj_field, msg);
440 }
441