1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <zephyr/init.h>
9 
10 #include "lwm2m_object.h"
11 #include "lwm2m_engine.h"
12 
13 #define TEST_OBJ_ID 32768
14 
15 bool b;
16 uint8_t opaque[32];
17 char string[32];
18 uint8_t u8;
19 int8_t s8;
20 uint16_t u16;
21 int16_t s16;
22 uint32_t u32;
23 int32_t s32;
24 uint64_t u64;
25 int64_t s64;
26 time_t t;
27 double d;
28 struct lwm2m_objlnk objl;
29 
30 static struct lwm2m_engine_obj test_obj;
31 /* Use LWM2M_RES_TYPE_* also as a resource ID, so that
32  * resource ID of U8 type is LWM2M_RES_TYPE_U8
33  */
34 static struct lwm2m_engine_obj_field fields[] = {
35 	OBJ_FIELD(LWM2M_RES_TYPE_OPAQUE, RW, OPAQUE),
36 	OBJ_FIELD(LWM2M_RES_TYPE_STRING, RW, STRING),
37 	OBJ_FIELD(LWM2M_RES_TYPE_U32, RW, U32),
38 	OBJ_FIELD(LWM2M_RES_TYPE_U16, RW, U16),
39 	OBJ_FIELD(LWM2M_RES_TYPE_U8, RW, U8),
40 	OBJ_FIELD(LWM2M_RES_TYPE_S64, RW, S64),
41 	OBJ_FIELD(LWM2M_RES_TYPE_S32, RW, S32),
42 	OBJ_FIELD(LWM2M_RES_TYPE_S16, RW, S16),
43 	OBJ_FIELD(LWM2M_RES_TYPE_S8, RW, S8),
44 	OBJ_FIELD(LWM2M_RES_TYPE_BOOL, RW, BOOL),
45 	OBJ_FIELD(LWM2M_RES_TYPE_TIME, RW, TIME),
46 	OBJ_FIELD(LWM2M_RES_TYPE_FLOAT, RW, FLOAT),
47 	OBJ_FIELD(LWM2M_RES_TYPE_OBJLNK, RW, OBJLNK)
48 };
49 #define RESOURCE_COUNT LWM2M_RES_TYPE_OBJLNK
50 
51 static struct lwm2m_engine_obj_inst inst;
52 static struct lwm2m_engine_res res[RESOURCE_COUNT];
53 static struct lwm2m_engine_res_inst res_inst[RESOURCE_COUNT];
54 
obj_create(uint16_t obj_inst_id)55 static struct lwm2m_engine_obj_inst *obj_create(uint16_t obj_inst_id)
56 {
57 	int i = 0, j = 0;
58 	static bool created;
59 
60 	if (created || obj_inst_id != 0) {
61 		return NULL;
62 	}
63 	created = true;
64 
65 	(void)memset(res, 0, sizeof(res));
66 	init_res_instance(res_inst, ARRAY_SIZE(res_inst));
67 
68 	/* initialize instance resource data */
69 	INIT_OBJ_RES_DATA_LEN(LWM2M_RES_TYPE_OPAQUE, res, i, res_inst, j, opaque, sizeof(opaque),
70 			      0);
71 	INIT_OBJ_RES_DATA_LEN(LWM2M_RES_TYPE_STRING, res, i, res_inst, j, string, sizeof(string),
72 			      0);
73 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_U32, res, i, res_inst, j, &u32, sizeof(u32));
74 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_U16, res, i, res_inst, j, &u16, sizeof(u16));
75 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_U8, res, i, res_inst, j, &u8, sizeof(u8));
76 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_S64, res, i, res_inst, j, &s64, sizeof(s64));
77 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_S32, res, i, res_inst, j, &s32, sizeof(s32));
78 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_S16, res, i, res_inst, j, &s16, sizeof(s16));
79 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_S8, res, i, res_inst, j, &s8, sizeof(s8));
80 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_TIME, res, i, res_inst, j, &t, sizeof(t));
81 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_BOOL, res, i, res_inst, j, &b, sizeof(b));
82 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_FLOAT, res, i, res_inst, j, &d, sizeof(d));
83 	INIT_OBJ_RES_DATA(LWM2M_RES_TYPE_OBJLNK, res, i, res_inst, j, &objl, sizeof(objl));
84 
85 	inst.resources = res;
86 	inst.resource_count = i;
87 
88 	return &inst;
89 }
90 
obj_init(void)91 static int obj_init(void)
92 {
93 	struct lwm2m_engine_obj_inst *obj_inst = NULL;
94 
95 	test_obj.obj_id = TEST_OBJ_ID;
96 	test_obj.version_major = 1;
97 	test_obj.version_minor = 0;
98 	test_obj.is_core = false;
99 	test_obj.fields = fields;
100 	test_obj.field_count = ARRAY_SIZE(fields);
101 	test_obj.max_instance_count = 1;
102 	test_obj.create_cb = obj_create;
103 	lwm2m_register_obj(&test_obj);
104 
105 	/* auto create the first instance */
106 	return lwm2m_create_obj_inst(TEST_OBJ_ID, 0, &obj_inst);
107 }
108 
109 SYS_INIT(obj_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
110