1 /**
2  * @file lwm2m_obj_binaryappdata.c
3  * @brief
4  *
5  * Copyright (c) 2022 Kickmaker
6  *
7  * SPDX-License-Identifier: Apache-2.0
8  */
9 
10 /*
11  * BinaryAppDataContainer
12  * https://raw.githubusercontent.com/OpenMobileAlliance/lwm2m-registry/prod/19.xml
13  */
14 
15 #define LOG_MODULE_NAME net_lwm2m_binaryappdata
16 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
17 
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
20 
21 #include <string.h>
22 #include <zephyr/init.h>
23 
24 #include "lwm2m_object.h"
25 #include "lwm2m_engine.h"
26 #include "lwm2m_obj_binaryappdata.h"
27 
28 #define BINARYAPPDATA_VERSION_MAJOR 1
29 #define BINARYAPPDATA_VERSION_MINOR 0
30 #define BINARYAPPDATA_MAX_ID 6
31 
32 #define MAX_INSTANCE_COUNT              CONFIG_LWM2M_BINARYAPPDATA_INSTANCE_COUNT
33 #define BINARYAPPDATA_DATA_INSTANCE_MAX CONFIG_LWM2M_BINARYAPPDATA_DATA_INSTANCE_COUNT
34 
35 /*
36  * Calculate resource instances as follows:
37  * start with BINARYAPPDATA_MAX_ID
38  * subtract EXEC resources (0)
39  * add BINARYAPPDATA_DATA_INSTANCE_MAX resource instances
40  */
41 #define RESOURCE_INSTANCE_COUNT (BINARYAPPDATA_MAX_ID - (0) + BINARYAPPDATA_DATA_INSTANCE_MAX)
42 
43 static struct lwm2m_engine_obj lwm2m_binaryappdata;
44 static struct lwm2m_engine_obj_field fields[] = {
45 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_ID, RW, OPAQUE),
46 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_PRIORITY_ID, RW_OPT, U8),
47 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_CREATION_TIME_ID, RW_OPT, TIME),
48 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_DESCRIPTION_ID, RW_OPT, STRING),
49 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_FORMAT_ID, RW_OPT, STRING),
50 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_APP_ID, RW_OPT, U16),
51 };
52 
53 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
54 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][BINARYAPPDATA_MAX_ID];
55 static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
56 
lwm2m_binaryappdata_create(uint16_t obj_inst_id)57 static struct lwm2m_engine_obj_inst *lwm2m_binaryappdata_create(uint16_t obj_inst_id)
58 {
59 	int index, avail = -1, i = 0, j = 0;
60 
61 	/* Check that there is no other instance with this ID */
62 	for (index = 0; index < ARRAY_SIZE(inst); index++) {
63 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
64 			LOG_ERR("Can not create instance - "
65 				"already existing: %u",
66 				obj_inst_id);
67 			return NULL;
68 		}
69 
70 		/* Save first available slot index */
71 		if (avail < 0 && !inst[index].obj) {
72 			avail = index;
73 		}
74 	}
75 
76 	if (avail < 0) {
77 		LOG_ERR("Can not create instance - no more room: %u", obj_inst_id);
78 		return NULL;
79 	}
80 
81 	(void)memset(res[avail], 0, sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
82 
83 	init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
84 
85 	/* initialize instance resource data */
86 	INIT_OBJ_RES_OPT(LWM2M_BINARYAPPDATA_DATA_ID, res[avail], i, res_inst[avail], j,
87 			BINARYAPPDATA_DATA_INSTANCE_MAX, true, true, NULL, NULL, NULL, NULL, NULL);
88 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_DATA_PRIORITY_ID, res[avail], i,
89 			res_inst[avail], j);
90 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_DATA_CREATION_TIME_ID, res[avail], i,
91 			res_inst[avail], j);
92 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_DATA_DESCRIPTION_ID, res[avail], i,
93 			res_inst[avail], j);
94 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_DATA_FORMAT_ID, res[avail], i, res_inst[avail], j);
95 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_APP_ID, res[avail], i, res_inst[avail], j);
96 
97 	inst[avail].resources = res[avail];
98 	inst[avail].resource_count = i;
99 
100 	LOG_DBG("Created LWM2M binary app data container instance: %d", obj_inst_id);
101 	return &inst[avail];
102 }
103 
lwm2m_binaryappdata_init(void)104 static int lwm2m_binaryappdata_init(void)
105 {
106 	int ret = 0;
107 
108 	/* initialize the Event Log field data */
109 	lwm2m_binaryappdata.obj_id = LWM2M_OBJECT_BINARYAPPDATACONTAINER_ID;
110 	lwm2m_binaryappdata.version_major = BINARYAPPDATA_VERSION_MAJOR;
111 	lwm2m_binaryappdata.version_minor = BINARYAPPDATA_VERSION_MINOR;
112 	lwm2m_binaryappdata.is_core = false;
113 	lwm2m_binaryappdata.fields = fields;
114 	lwm2m_binaryappdata.field_count = ARRAY_SIZE(fields);
115 	lwm2m_binaryappdata.max_instance_count = MAX_INSTANCE_COUNT;
116 	lwm2m_binaryappdata.create_cb = lwm2m_binaryappdata_create;
117 	lwm2m_register_obj(&lwm2m_binaryappdata);
118 
119 	return ret;
120 }
121 
122 LWM2M_OBJ_INIT(lwm2m_binaryappdata_init);
123