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 /* Support 2 instances of binary data in one object */
33 #define BINARYAPPDATA_DATA_INSTANCE_MAX 2
34 
35 /* Support 2 multi instance object */
36 #define MAX_INSTANCE_COUNT	2
37 
38 /*
39  * Calculate resource instances as follows:
40  * start with BINARYAPPDATA_MAX_ID
41  * subtract EXEC resources (0)
42  * add BINARYAPPDATA_DATA_INSTANCE_MAX resource instances
43  */
44 #define RESOURCE_INSTANCE_COUNT (BINARYAPPDATA_MAX_ID - (0) + BINARYAPPDATA_DATA_INSTANCE_MAX)
45 
46 static struct lwm2m_engine_obj lwm2m_binaryappdata;
47 static struct lwm2m_engine_obj_field fields[] = {
48 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_ID, RW, OPAQUE),
49 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_PRIORITY_ID, RW_OPT, U8),
50 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_CREATION_TIME_ID, RW_OPT, TIME),
51 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_DESCRIPTION_ID, RW_OPT, STRING),
52 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_DATA_FORMAT_ID, RW_OPT, STRING),
53 	OBJ_FIELD_DATA(LWM2M_BINARYAPPDATA_APP_ID, RW_OPT, U16),
54 };
55 
56 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
57 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][BINARYAPPDATA_MAX_ID];
58 static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
59 
lwm2m_binaryappdata_create(uint16_t obj_inst_id)60 static struct lwm2m_engine_obj_inst *lwm2m_binaryappdata_create(uint16_t obj_inst_id)
61 {
62 	int index, avail = -1, i = 0, j = 0;
63 
64 	/* Check that there is no other instance with this ID */
65 	for (index = 0; index < ARRAY_SIZE(inst); index++) {
66 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
67 			LOG_ERR("Can not create instance - "
68 				"already existing: %u",
69 				obj_inst_id);
70 			return NULL;
71 		}
72 
73 		/* Save first available slot index */
74 		if (avail < 0 && !inst[index].obj) {
75 			avail = index;
76 		}
77 	}
78 
79 	if (avail < 0) {
80 		LOG_ERR("Can not create instance - no more room: %u", obj_inst_id);
81 		return NULL;
82 	}
83 
84 	(void)memset(res[avail], 0, sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
85 
86 	init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
87 
88 	/* initialize instance resource data */
89 	INIT_OBJ_RES_OPT(LWM2M_BINARYAPPDATA_DATA_ID, res[avail], i, res_inst[avail], j,
90 			BINARYAPPDATA_DATA_INSTANCE_MAX, true, true, NULL, NULL, NULL, NULL, NULL);
91 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_DATA_PRIORITY_ID, res[avail], i,
92 			res_inst[avail], j);
93 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_DATA_CREATION_TIME_ID, res[avail], i,
94 			res_inst[avail], j);
95 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_DATA_DESCRIPTION_ID, res[avail], i,
96 			res_inst[avail], j);
97 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_DATA_FORMAT_ID, res[avail], i, res_inst[avail], j);
98 	INIT_OBJ_RES_OPTDATA(LWM2M_BINARYAPPDATA_APP_ID, res[avail], i, res_inst[avail], j);
99 
100 	inst[avail].resources = res[avail];
101 	inst[avail].resource_count = i;
102 
103 	LOG_DBG("Created LWM2M binary app data container instance: %d", obj_inst_id);
104 	return &inst[avail];
105 }
106 
lwm2m_binaryappdata_init(void)107 static int lwm2m_binaryappdata_init(void)
108 {
109 	int ret = 0;
110 
111 	/* initialize the Event Log field data */
112 	lwm2m_binaryappdata.obj_id = LWM2M_OBJECT_BINARYAPPDATACONTAINER_ID;
113 	lwm2m_binaryappdata.version_major = BINARYAPPDATA_VERSION_MAJOR;
114 	lwm2m_binaryappdata.version_minor = BINARYAPPDATA_VERSION_MINOR;
115 	lwm2m_binaryappdata.is_core = false;
116 	lwm2m_binaryappdata.fields = fields;
117 	lwm2m_binaryappdata.field_count = ARRAY_SIZE(fields);
118 	lwm2m_binaryappdata.max_instance_count = MAX_INSTANCE_COUNT;
119 	lwm2m_binaryappdata.create_cb = lwm2m_binaryappdata_create;
120 	lwm2m_register_obj(&lwm2m_binaryappdata);
121 
122 	return ret;
123 }
124 
125 LWM2M_OBJ_INIT(lwm2m_binaryappdata_init);
126