Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
 * Copyright (c) 2017 Linaro Limited
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/*
 * Copyright (c) 2015, Yanzi Networks AB.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Original Authors:
 *         Joakim Eriksson <joakime@sics.se>
 *         Niclas Finne <nfi@sics.se>
 */

/*
 * Zephyr Contribution by Michael Scott <michael.scott@linaro.org>
 * - Zephyr code style changes / code cleanup
 * - Move to Zephyr APIs where possible
 * - Convert to Zephyr int/uint types
 * - Remove engine dependency (replace with writer context)
 * - Add write int64 function
 */

/*
 * TODO:
 * - Lots of byte-order API clean up
 * - Var / parameter type cleanup
 * - Replace magic #'s with defines
 */

#define SYS_LOG_DOMAIN "lib/lwm2m_oma_tlv"
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_LWM2M_LEVEL
#include <logging/sys_log.h>
#include <string.h>
#include <stdint.h>
#include <misc/byteorder.h>

#include "lwm2m_rw_oma_tlv.h"
#include "lwm2m_engine.h"

static u8_t get_len_type(const struct oma_tlv *tlv)
{
	if (tlv->length < 8) {
		return 0;
	} else if (tlv->length < 0x100) {
		return 1;
	} else if (tlv->length < 0x10000) {
		return 2;
	}

	return 3;
}

static u8_t tlv_calc_type(u8_t flags)
{
	return flags & WRITER_RESOURCE_INSTANCE ?
			OMA_TLV_TYPE_RESOURCE_INSTANCE : OMA_TLV_TYPE_RESOURCE;
}

static u16_t tlv_calc_id(u8_t flags, struct lwm2m_obj_path *path)
{
	return flags & WRITER_RESOURCE_INSTANCE ?
			path->res_inst_id : path->res_id;
}

static void tlv_setup(struct oma_tlv *tlv, u8_t type, u16_t id,
		     u32_t len, const u8_t *value)
{
	if (tlv) {
		tlv->type = type;
		tlv->id = id;
		tlv->length = len;
		tlv->value = value;
	}
}

static size_t oma_tlv_put(const struct oma_tlv *tlv, u8_t *buffer, size_t len)
{
	int pos;
	u8_t len_type;

	/* len type is the same as number of bytes required for length */
	len_type = get_len_type(tlv);
	pos = 1 + len_type;
	/* ensure that we do not write too much */
	if (len < tlv->length + pos) {
		SYS_LOG_ERR("OMA-TLV: Could not write the TLV - buffer overflow"
			    " (len:%zd < tlv->length:%d + pos:%d)",
			    len, tlv->length, pos);
		return 0;
	}

	/* first type byte in TLV header */
	buffer[0] = (tlv->type << 6) |
		    (tlv->id > 255 ? (1 << 5) : 0) |
		    (len_type << 3) |
		    (len_type == 0 ? tlv->length : 0);
	pos = 1;

	/* The ID */
	if (tlv->id > 255) {
		buffer[pos++] = (tlv->id >> 8) & 0xff;
	}

	buffer[pos++] = tlv->id & 0xff;

	/* Add length if needed - unrolled loop ? */
	if (len_type > 2) {
		buffer[pos++] = (tlv->length >> 16) & 0xff;
	}

	if (len_type > 1) {
		buffer[pos++] = (tlv->length >> 8) & 0xff;
	}

	if (len_type > 0) {
		buffer[pos++] = tlv->length & 0xff;
	}

	/* finally add the value */
	if (tlv->value != NULL && tlv->length > 0) {
		memcpy(&buffer[pos], tlv->value, tlv->length);
	}

	/* TODO: Add debug print of TLV */
	return pos + tlv->length;
}

size_t oma_tlv_get(struct oma_tlv *tlv, const u8_t *buffer, size_t len)
{
	u8_t len_type;
	u8_t len_pos = 1;
	size_t tlv_len;

	tlv->type = (buffer[0] >> 6) & 3;
	len_type = (buffer[0] >> 3) & 3;
	len_pos = 1 + (((buffer[0] & (1 << 5)) != 0) ? 2 : 1);
	tlv->id = buffer[1];

	/* if len_pos > 2 it means that there are more ID to read */
	if (len_pos > 2) {
		tlv->id = (tlv->id << 8) + buffer[2];
	}

	if (len_type == 0) {
		tlv_len = buffer[0] & 7;
	} else {
		/* read the length */
		tlv_len = 0;
		while (len_type > 0) {
			tlv_len = tlv_len << 8 | buffer[len_pos++];
			len_type--;
		}
	}

	/* and read out the data??? */
	tlv->length = tlv_len;
	tlv->value = &buffer[len_pos];
	return len_pos + tlv_len;
}

static size_t put_begin_ri(struct lwm2m_output_context *out,
			   struct lwm2m_obj_path *path)
{
	/* set some flags in state */
	struct oma_tlv tlv;
	size_t len;

	out->writer_flags |= WRITER_RESOURCE_INSTANCE;
	tlv_setup(&tlv, OMA_TLV_TYPE_MULTI_RESOURCE, path->res_id, 8, NULL);

	/* we remove the nonsense payload here (len = 8) */
	len = oma_tlv_put(&tlv, &out->outbuf[out->outlen],
			    out->outsize - out->outlen) - 8;
	/*
	 * store position for deciding where to re-write the TLV when we
	 * know the length - NOTE: either this or memmov of buffer later...
	 */
	out->mark_pos_ri = out->outlen;
	out->outlen += len;
	return len;
}

static size_t put_end_ri(struct lwm2m_output_context *out,
			 struct lwm2m_obj_path *path)
{
	/* clear out state info */
	int pos = 2; /* this is the length pos */
	size_t len;

	out->writer_flags &= ~WRITER_RESOURCE_INSTANCE;
	if (path->res_id > 0xff) {
		pos++;
	}

	len = out->outlen - out->mark_pos_ri;

	/* update the length byte... Assume TLV header is pos + 1 bytes. */
	out->outbuf[pos + out->mark_pos_ri] = len - (pos + 1);
	return 0;
}

static size_t put_s8(struct lwm2m_output_context *out,
		     struct lwm2m_obj_path *path, s8_t value)
{
	size_t len;
	struct oma_tlv tlv;

	tlv_setup(&tlv, tlv_calc_type(out->writer_flags),
		  tlv_calc_id(out->writer_flags, path), sizeof(value),
		  (u8_t *)&value);

	len = oma_tlv_put(&tlv, &out->outbuf[out->outlen],
			  out->outsize - out->outlen);
	out->outlen += len;
	return len;
}

static size_t put_s16(struct lwm2m_output_context *out,
		      struct lwm2m_obj_path *path, s16_t value)
{
	size_t len;
	struct oma_tlv tlv;
	s16_t net_value;

	net_value = sys_cpu_to_be16(value);
	tlv_setup(&tlv, tlv_calc_type(out->writer_flags),
		  tlv_calc_id(out->writer_flags, path), sizeof(net_value),
		  (u8_t *)&net_value);

	len = oma_tlv_put(&tlv, &out->outbuf[out->outlen],
			  out->outsize - out->outlen);
	out->outlen += len;
	return len;
}

static size_t put_s32(struct lwm2m_output_context *out,
			struct lwm2m_obj_path *path, s32_t value)
{
	size_t len;
	struct oma_tlv tlv;
	s32_t net_value;

	net_value = sys_cpu_to_be32(value);
	tlv_setup(&tlv, tlv_calc_type(out->writer_flags),
		  tlv_calc_id(out->writer_flags, path), sizeof(net_value),
		  (u8_t *)&net_value);

	len = oma_tlv_put(&tlv, &out->outbuf[out->outlen],
			  out->outsize - out->outlen);
	out->outlen += len;
	return len;
}

static size_t put_s64(struct lwm2m_output_context *out,
			struct lwm2m_obj_path *path, s64_t value)
{
	size_t len;
	struct oma_tlv tlv;
	s64_t net_value;

	net_value = sys_cpu_to_be64(value);
	tlv_setup(&tlv, tlv_calc_type(out->writer_flags),
		  tlv_calc_id(out->writer_flags, path), sizeof(net_value),
		  (u8_t *)&net_value);

	len = oma_tlv_put(&tlv, &out->outbuf[out->outlen],
			  out->outsize - out->outlen);
	out->outlen += len;
	return len;
}

static size_t put_string(struct lwm2m_output_context *out,
			 struct lwm2m_obj_path *path,
			 const char *value, size_t strlen)
{
	size_t len;
	struct oma_tlv tlv;

	tlv_setup(&tlv, tlv_calc_type(out->writer_flags),
		  path->res_id, (u32_t)strlen,
		  (u8_t *)value);
	len = oma_tlv_put(&tlv, &out->outbuf[out->outlen],
			    out->outsize - out->outlen);
	out->outlen += len;
	return len;
}

/* use binary32 format */
static size_t put_float32fix(struct lwm2m_output_context *out,
			     struct lwm2m_obj_path *path,
			     float32_value_t *value)
{
	size_t len;
	struct oma_tlv tlv;
	u8_t *buffer = &out->outbuf[out->outlen];
	int e = 0;
	s32_t val = 0;
	s32_t v;
	u8_t b[4];

	/*
	 * TODO: Currently, there is no standard API for handling the decimal
	 * portion of the float32_value structure.  In the future, we should use
	 * the value->val2 (decimal portion) to set the decimal mask and in the
	 * following binary float calculations.
	 *
	 * HACK BELOW: hard code the decimal mask to 0 (whole number)
	 */
	int bits = 0;

	v = value->val1;
	if (v < 0) {
		v = -v;
	}

	while (v > 1) {
		val = (val >> 1);

		if (v & 1) {
			val = val | (1L << 22);
		}

		v = (v >> 1);
		e++;
	}

	/* convert to the thing we should have */
	e = e - bits + 127;
	if (value->val1 == 0) {
		e = 0;
	}

	/* is this the right byte order? */
	b[0] = (value->val1 < 0 ? 0x80 : 0) | (e >> 1);
	b[1] = ((e & 1) << 7) | ((val >> 16) & 0x7f);
	b[2] = (val >> 8) & 0xff;
	b[3] = val & 0xff;

	tlv_setup(&tlv, tlv_calc_type(out->writer_flags),
		  tlv_calc_id(out->writer_flags, path),
		  4, b);
	len = oma_tlv_put(&tlv, buffer, out->outsize - out->outlen);
	out->outlen += len;

	return len;
}

static size_t put_float64fix(struct lwm2m_output_context *out,
			     struct lwm2m_obj_path *path,
			     float64_value_t *value)
{
	size_t len;
	s64_t binary64 = 0, net_binary64 = 0;
	struct oma_tlv tlv;
	u8_t *buffer = &out->outbuf[out->outlen];

	/* TODO */

	net_binary64 = sys_cpu_to_be64(binary64);
	tlv_setup(&tlv, tlv_calc_type(out->writer_flags),
		  tlv_calc_id(out->writer_flags, path),
		  sizeof(net_binary64), (u8_t *)&net_binary64);
	len = oma_tlv_put(&tlv, buffer, out->outsize - out->outlen);
	out->outlen += len;
	return len;
}

static size_t put_bool(struct lwm2m_output_context *out,
		       struct lwm2m_obj_path *path, bool value)
{
	return put_s8(out, path, value != 0 ? 1 : 0);
}

static size_t get_s32(struct lwm2m_input_context *in, s32_t *value)
{
	struct oma_tlv tlv;
	size_t size = oma_tlv_get(&tlv, in->inbuf, in->insize);

	*value = 0;
	if (size > 0) {
		switch (tlv.length) {
		case 1:
			*value = *(s8_t *)tlv.value;
			break;
		case 2:
			*value = sys_cpu_to_be16(*(s16_t *)tlv.value);
			break;
		case 4:
			*value = sys_cpu_to_be32(*(s32_t *)tlv.value);
			break;
		default:
			SYS_LOG_ERR("invalid length: %u", tlv.length);
			size = 0;
			tlv.length = 0;
		}

		in->last_value_len = tlv.length;
	}

	return size;
}

/* TODO: research to make sure this is correct */
static size_t get_s64(struct lwm2m_input_context *in, s64_t *value)
{
	struct oma_tlv tlv;
	size_t size = oma_tlv_get(&tlv, in->inbuf, in->insize);

	*value = 0;
	if (size > 0) {
		switch (tlv.length) {
		case 1:
			*value = *tlv.value;
			break;
		case 2:
			*value = sys_cpu_to_be16(*(s16_t *)tlv.value);
			break;
		case 4:
			*value = sys_cpu_to_be32(*(s32_t *)tlv.value);
			break;
		case 8:
			*value = sys_cpu_to_be64(*(s64_t *)tlv.value);
			break;
		default:
			SYS_LOG_ERR("invalid length: %u", tlv.length);
			size = 0;
			tlv.length = 0;
		}

		in->last_value_len = tlv.length;
	}

	return size;
}

static size_t get_string(struct lwm2m_input_context *in,
			 u8_t *value, size_t strlen)
{
	struct oma_tlv tlv;
	size_t size = oma_tlv_get(&tlv, in->inbuf, in->insize);

	if (size > 0) {
		if (strlen <= tlv.length) {
			/*
			 * The outbuffer can not contain the
			 * full string including ending zero
			 */
			return 0;
		}

		memcpy(value, tlv.value, tlv.length);
		value[tlv.length] = '\0';
		in->last_value_len = tlv.length;
	}

	return size;
}

/* convert float to fixpoint */
static size_t get_float32fix(struct lwm2m_input_context *in,
			     float32_value_t *value)
{
	struct oma_tlv tlv;
	size_t size = oma_tlv_get(&tlv, in->inbuf, in->insize);
	int e;
	s32_t val;
	int sign = (tlv.value[0] & 0x80) != 0;
	/* */
	int bits = 0;

	if (size > 0) {
		/* TLV needs to be 4 bytes */
		e = ((tlv.value[0] << 1) & 0xff) | (tlv.value[1] >> 7);
		val = (((long)tlv.value[1] & 0x7f) << 16) |
		      (tlv.value[2] << 8) |
		      tlv.value[3];
		e = e - 127 + bits;

		/* e is the number of times we need to roll the number */
		SYS_LOG_DBG("Actual e=%d", e);
		e = e - 23;
		SYS_LOG_DBG("E after sub %d", e);
		val = val | 1L << 23;
		if (e > 0) {
			val = val << e;
		} else {
			val = val >> -e;
		}

		value->val1 = sign ? -val : val;

		/*
		 * TODO: Currently, there is no standard API for handling the
		 * decimal portion of the float32_value structure.  In the
		 * future, once that is settled, we should calculate
		 * value->val2 in the above float calculations.
		 *
		 * HACK BELOW: hard code the decimal value 0
		 */
		value->val2 = 0;
		in->last_value_len = tlv.length;
	}

	return size;
}

static size_t get_float64fix(struct lwm2m_input_context *in,
			     float64_value_t *value)
{
	struct oma_tlv tlv;
	size_t size = oma_tlv_get(&tlv, in->inbuf, in->insize);

	if (size > 0) {
		if (tlv.length != 8) {
			SYS_LOG_ERR("invalid length: %u (not 8)", tlv.length);
			return 0;
		}

		/* TODO */

		in->last_value_len = tlv.length;
	}

	return size;
}

static size_t get_bool(struct lwm2m_input_context *in, bool *value)
{
	struct oma_tlv tlv;
	size_t size = oma_tlv_get(&tlv, in->inbuf, in->insize);
	int i;
	s32_t temp = 0;

	if (size > 0) {
		/* will probably need to handle MSB as a sign bit? */
		for (i = 0; i < tlv.length; i++) {
			temp = (temp << 8) | tlv.value[i];
		}
		*value = (temp != 0);
		in->last_value_len = tlv.length;
	}

	return size;
}

const struct lwm2m_writer oma_tlv_writer = {
	NULL,
	NULL,
	put_begin_ri,
	put_end_ri,
	put_s8,
	put_s16,
	put_s32,
	put_s64,
	put_string,
	put_float32fix,
	put_float64fix,
	put_bool
};

const struct lwm2m_reader oma_tlv_reader = {
	get_s32,
	get_s64,
	get_string,
	get_float32fix,
	get_float64fix,
	get_bool
};

static int do_write_op_tlv_item(struct lwm2m_engine_context *context,
				u8_t *data, int len)
{
	struct lwm2m_input_context *in = context->in;
	struct lwm2m_engine_obj_inst *obj_inst = NULL;
	struct lwm2m_engine_res_inst *res = NULL;
	struct lwm2m_engine_obj_field *obj_field = NULL;
	u8_t created = 0;
	int ret, i;

	in->inbuf = data;
	in->inpos = 0;
	in->insize = len;

	ret = lwm2m_get_or_create_engine_obj(context, &obj_inst, &created);
	if (ret < 0) {
		return ret;
	}

	obj_field = lwm2m_get_engine_obj_field(obj_inst->obj,
					       context->path->res_id);
	/* if obj_field is not found, treat as an optional resource */
	if (!obj_field) {
		if (context->operation == LWM2M_OP_CREATE) {
			return -ENOTSUP;
		}

		return -ENOENT;
	}

	if ((obj_field->permissions & LWM2M_PERM_W) != LWM2M_PERM_W) {
		return -EPERM;
	}

	if (!obj_inst->resources || obj_inst->resource_count == 0) {
		return -EINVAL;
	}

	for (i = 0; i < obj_inst->resource_count; i++) {
		if (obj_inst->resources[i].res_id == context->path->res_id) {
			res = &obj_inst->resources[i];
			break;
		}
	}

	if (!res) {
		return -ENOENT;
	}

	return lwm2m_write_handler(obj_inst, res, obj_field, context);
}

int do_write_op_tlv(struct lwm2m_engine_obj *obj,
		    struct lwm2m_engine_context *context)
{
	struct lwm2m_input_context *in = context->in;
	struct lwm2m_obj_path *path = context->path;
	struct lwm2m_engine_obj_inst *obj_inst = NULL;
	size_t len;
	struct oma_tlv tlv;
	int tlvpos = 0, ret;
	u16_t insize = in->insize;
	u8_t *inbuf = in->inbuf;

	while (tlvpos < insize) {
		len = oma_tlv_get(&tlv, &inbuf[tlvpos],
				  insize - tlvpos);

		SYS_LOG_DBG("Got TLV format First is: type:%d id:%d "
			    "len:%d (p:%d len:%d/%d)",
			    tlv.type, tlv.id, (int) tlv.length,
			    (int) tlvpos, (int) len, (int) insize);

		if (tlv.type == OMA_TLV_TYPE_OBJECT_INSTANCE) {
			struct oma_tlv tlv2;
			int len2;
			int pos = 0;

			path->obj_inst_id = tlv.id;
			if (tlv.length == 0) {
				/* Create only - no data */
				ret = lwm2m_create_obj_inst(path->obj_id,
							    path->obj_inst_id,
							    &obj_inst);
				if (ret < 0) {
					return ret;
				}
			}

			while (pos < tlv.length &&
			       (len2 = oma_tlv_get(&tlv2, &tlv.value[pos],
						    tlv.length - pos))) {
				if (tlv2.type == OMA_TLV_TYPE_RESOURCE) {
					path->res_id = tlv2.id;
					path->level = 3;
					/* ignore errors */
					do_write_op_tlv_item(
							context,
							(u8_t *)&tlv.value[pos],
							len2);
				}

				pos += len2;
			}

			zoap_header_set_code(context->out->out_zpkt,
					     ZOAP_RESPONSE_CODE_CREATED);
		} else if (tlv.type == OMA_TLV_TYPE_RESOURCE) {
			path->res_id = tlv.id;
			path->level = 3;
			ret = do_write_op_tlv_item(context,
						   &inbuf[tlvpos], len);
			/*
			 * ignore errors for CREATE op
			 * TODO: support BOOTSTRAP WRITE where optional
			 * resources are ignored
			 */
			if (ret < 0 && (context->operation != LWM2M_OP_CREATE ||
					ret != -ENOTSUP)) {
				return ret;
			}
		}

		tlvpos += len;
	}

	return 0;
}