1 /*
2  * Copyright (c) 2017 Linaro Limited
3  * Copyright (c) 2018-2019 Foundries.io
4  * Copyright (c) 2022 Laird Connectivity
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 /*
10  * Source material for uCIFI battery object (3411):
11  * https://raw.githubusercontent.com/OpenMobileAlliance/lwm2m-registry/prod/3411.xml
12  */
13 
14 #define LOG_MODULE_NAME net_ucifi_battery
15 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
16 
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
19 
20 #include <stdint.h>
21 #include <zephyr/init.h>
22 
23 #include "lwm2m_object.h"
24 #include "lwm2m_engine.h"
25 #include "lwm2m_resource_ids.h"
26 #include "ucifi_battery.h"
27 
28 #define BATTERY_VERSION_MAJOR 1
29 #define BATTERY_VERSION_MINOR 0
30 
31 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_UCIFI_BATTERY_INSTANCE_COUNT
32 
33 #define BATTERY_MAX_ID 12
34 #define RESOURCE_INSTANCE_COUNT (BATTERY_MAX_ID)
35 
36 /* resource state variables */
37 static uint8_t battery_level[MAX_INSTANCE_COUNT];
38 static uint32_t supply_loss_counter[MAX_INSTANCE_COUNT];
39 
40 static struct lwm2m_engine_obj battery;
41 static struct lwm2m_engine_obj_field fields[] = {
42 	OBJ_FIELD_DATA(UCIFI_BATTERY_LEVEL_RID, R, U8),
43 	OBJ_FIELD_DATA(UCIFI_BATTERY_CAPACITY_RID, R_OPT, FLOAT),
44 	OBJ_FIELD_DATA(UCIFI_BATTERY_VOLTAGE_RID, R_OPT, FLOAT),
45 	OBJ_FIELD_DATA(UCIFI_BATTERY_TYPE_RID, RW_OPT, STRING),
46 	OBJ_FIELD_DATA(UCIFI_BATTERY_LOW_THESHOLD_RID, RW_OPT, U8),
47 	OBJ_FIELD_DATA(UCIFI_BATTERY_LEVEL_TOO_LOW_RID, R_OPT, BOOL),
48 	OBJ_FIELD_DATA(UCIFI_BATTERY_SHUTDOWN_RID, RW_OPT, BOOL),
49 	OBJ_FIELD_DATA(UCIFI_BATTERY_NUM_CYCLES_RID, R_OPT, U32),
50 	OBJ_FIELD_DATA(UCIFI_BATTERY_SUPPLY_LOSS_RID, R_OPT, BOOL),
51 	OBJ_FIELD_DATA(UCIFI_BATTERY_SUPPLY_LOSS_COUNTER_RID, R_OPT, U32),
52 	OBJ_FIELD_EXECUTE_OPT(UCIFI_BATTERY_SUPPLY_LOSS_COUNTER_RESET_RID),
53 	OBJ_FIELD_DATA(UCIFI_BATTERY_SUPPLY_LOSS_REASON_RID, R_OPT, STRING),
54 };
55 
56 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
57 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][BATTERY_MAX_ID];
58 static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
59 
clear_supply_loss_counter(uint16_t obj_inst_id,int index)60 static void clear_supply_loss_counter(uint16_t obj_inst_id, int index)
61 {
62 	supply_loss_counter[index] = 0;
63 	lwm2m_notify_observer(UCIFI_OBJECT_BATTERY_ID, obj_inst_id,
64 			UCIFI_BATTERY_SUPPLY_LOSS_COUNTER_RID);
65 }
66 
supply_loss_counter_reset_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)67 static int supply_loss_counter_reset_cb(uint16_t obj_inst_id, uint8_t *args, uint16_t args_len)
68 {
69 	int i;
70 
71 	LOG_DBG("RESET supply loss counter %d", obj_inst_id);
72 	for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
73 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
74 			clear_supply_loss_counter(obj_inst_id, i);
75 			return 0;
76 		}
77 	}
78 
79 	return -ENOENT;
80 }
81 
battery_create(uint16_t obj_inst_id)82 static struct lwm2m_engine_obj_inst *battery_create(uint16_t obj_inst_id)
83 {
84 	int index, i = 0, j = 0;
85 
86 	/* Check that there is no other instance with this ID */
87 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
88 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
89 			LOG_ERR("Can not create instance - "
90 				"already existing: %u",
91 				obj_inst_id);
92 			return NULL;
93 		}
94 	}
95 
96 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
97 		if (!inst[index].obj) {
98 			break;
99 		}
100 	}
101 
102 	if (index >= MAX_INSTANCE_COUNT) {
103 		LOG_ERR("Can not create instance - no more room: %u", obj_inst_id);
104 		return NULL;
105 	}
106 
107 	/* Set default values */
108 	battery_level[index] = 0;
109 	supply_loss_counter[index] = 0;
110 
111 	(void)memset(res[index], 0, sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
112 	init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
113 
114 	/* initialize instance resource data */
115 	INIT_OBJ_RES_DATA(UCIFI_BATTERY_LEVEL_RID, res[index], i, res_inst[index], j,
116 			  &battery_level[index], sizeof(*battery_level));
117 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_CAPACITY_RID, res[index], i, res_inst[index], j);
118 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_VOLTAGE_RID, res[index], i, res_inst[index], j);
119 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_TYPE_RID, res[index], i, res_inst[index], j);
120 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_LOW_THESHOLD_RID, res[index], i, res_inst[index], j);
121 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_LEVEL_TOO_LOW_RID, res[index], i, res_inst[index], j);
122 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_SHUTDOWN_RID, res[index], i, res_inst[index], j);
123 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_NUM_CYCLES_RID, res[index], i, res_inst[index], j);
124 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_SUPPLY_LOSS_RID, res[index], i, res_inst[index], j);
125 	INIT_OBJ_RES_DATA(UCIFI_BATTERY_SUPPLY_LOSS_COUNTER_RID, res[index], i, res_inst[index], j,
126 			  &supply_loss_counter[index], sizeof(*supply_loss_counter));
127 	INIT_OBJ_RES_EXECUTE(UCIFI_BATTERY_SUPPLY_LOSS_COUNTER_RESET_RID, res[index], i,
128 			     supply_loss_counter_reset_cb);
129 	INIT_OBJ_RES_OPTDATA(UCIFI_BATTERY_SUPPLY_LOSS_REASON_RID, res[index], i, res_inst[index],
130 			     j);
131 
132 	inst[index].resources = res[index];
133 	inst[index].resource_count = i;
134 	LOG_DBG("Create uCIFI Battery instance: %d", obj_inst_id);
135 	return &inst[index];
136 }
137 
ucifi_battery_init(void)138 static int ucifi_battery_init(void)
139 {
140 	battery.obj_id = UCIFI_OBJECT_BATTERY_ID;
141 	battery.version_major = BATTERY_VERSION_MAJOR;
142 	battery.version_minor = BATTERY_VERSION_MINOR;
143 	battery.is_core = false;
144 	battery.fields = fields;
145 	battery.field_count = ARRAY_SIZE(fields);
146 	battery.max_instance_count = MAX_INSTANCE_COUNT;
147 	battery.create_cb = battery_create;
148 	lwm2m_register_obj(&battery);
149 
150 	return 0;
151 }
152 
153 LWM2M_OBJ_INIT(ucifi_battery_init);
154