1 /**
2 * @file lwm2m_event_log.c
3 * @brief
4 *
5 * Copyright (c) 2022 Kickmaker
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 /*
11 * Event Log
12 * https://raw.githubusercontent.com/OpenMobileAlliance/lwm2m-registry/prod/20.xml
13 */
14
15 #define LOG_MODULE_NAME net_lwm2m_event_log
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_event_log.h"
27
28 #define EVENT_LOG_VERSION_MAJOR 1
29 #define EVENT_LOG_VERSION_MINOR 0
30 #define EVENT_LOG_MAX_ID 6
31
32 /*
33 * Calculate resource instances as follows:
34 * start with EVENT_LOG_MAX_ID
35 * subtract EXEC resources (2)
36 */
37 #define RESOURCE_INSTANCE_COUNT (EVENT_LOG_MAX_ID - 2)
38
39 struct lwm2m_event_log_obj {
40 uint8_t log_class;
41 uint8_t log_status;
42 uint8_t log_data_format;
43 };
44
45 static struct lwm2m_engine_obj lwm2m_event_log;
46 static struct lwm2m_engine_obj_field fields[] = {
47 OBJ_FIELD_DATA(LWM2M_EVENT_LOG_CLASS_ID, RW_OPT, U8),
48 OBJ_FIELD_EXECUTE_OPT(LWM2M_EVENT_LOG_START_ID),
49 OBJ_FIELD_EXECUTE_OPT(LWM2M_EVENT_LOG_STOP_ID),
50 OBJ_FIELD_DATA(LWM2M_EVENT_LOG_STATUS_ID, R_OPT, U8),
51 OBJ_FIELD_DATA(LWM2M_EVENT_LOG_DATA_ID, R, OPAQUE),
52 OBJ_FIELD_DATA(LWM2M_EVENT_LOG_DATAFORMAT_ID, RW_OPT, U8),
53 };
54
55 static struct lwm2m_engine_obj_inst inst;
56 static struct lwm2m_engine_res res[EVENT_LOG_MAX_ID];
57 static struct lwm2m_engine_res_inst res_inst[RESOURCE_INSTANCE_COUNT];
58
lwm2m_event_log_create(uint16_t obj_inst_id)59 static struct lwm2m_engine_obj_inst *lwm2m_event_log_create(uint16_t obj_inst_id)
60 {
61 int i = 0, j = 0;
62
63 init_res_instance(res_inst, ARRAY_SIZE(res_inst));
64
65 /* initialize instance resource data */
66 INIT_OBJ_RES_OPTDATA(LWM2M_EVENT_LOG_CLASS_ID, res, i, res_inst, j);
67 INIT_OBJ_RES_EXECUTE(LWM2M_EVENT_LOG_START_ID, res, i, NULL);
68 INIT_OBJ_RES_EXECUTE(LWM2M_EVENT_LOG_STOP_ID, res, i, NULL);
69 INIT_OBJ_RES_OPTDATA(LWM2M_EVENT_LOG_STATUS_ID, res, i, res_inst, j);
70 INIT_OBJ_RES_OPT(LWM2M_EVENT_LOG_DATA_ID, res, i, res_inst, j, 1,
71 false, true, NULL, NULL, NULL, NULL, NULL);
72 INIT_OBJ_RES_OPTDATA(LWM2M_EVENT_LOG_DATAFORMAT_ID, res, i, res_inst, j);
73
74 inst.resources = res;
75 inst.resource_count = i;
76
77 LOG_DBG("Created LWM2M event log instance: %d", obj_inst_id);
78 return &inst;
79 }
80
lwm2m_event_log_init(void)81 static int lwm2m_event_log_init(void)
82 {
83 struct lwm2m_engine_obj_inst *obj_inst = NULL;
84 int ret = 0;
85
86 /* initialize the Event Log field data */
87 lwm2m_event_log.obj_id = LWM2M_OBJECT_EVENT_LOG_ID;
88 lwm2m_event_log.version_major = EVENT_LOG_VERSION_MAJOR;
89 lwm2m_event_log.version_minor = EVENT_LOG_VERSION_MINOR;
90 lwm2m_event_log.is_core = false;
91 lwm2m_event_log.fields = fields;
92 lwm2m_event_log.field_count = ARRAY_SIZE(fields);
93 lwm2m_event_log.max_instance_count = 1U;
94 lwm2m_event_log.create_cb = lwm2m_event_log_create;
95 lwm2m_register_obj(&lwm2m_event_log);
96
97 /* auto create the first instance */
98 ret = lwm2m_create_obj_inst(LWM2M_OBJECT_EVENT_LOG_ID, 0, &obj_inst);
99 if (ret < 0) {
100 LOG_ERR("Create LWM2M Event Log instance 0 error: %d", ret);
101 }
102
103 return ret;
104 }
105
106 SYS_INIT(lwm2m_event_log_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
107