1 /** @file
2  *  @brief GATT Battery Service
3  */
4 
5 /*
6  * Copyright (c) 2024 Demant A/S
7  * Copyright (c) 2018 Nordic Semiconductor ASA
8  * Copyright (c) 2016 Intel Corporation
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #include <errno.h>
14 #include <zephyr/init.h>
15 #include <zephyr/sys/__assert.h>
16 #include <stdbool.h>
17 #include <zephyr/types.h>
18 #include <zephyr/sys/util.h>
19 #include <zephyr/sys/util_macro.h>
20 
21 #include <zephyr/bluetooth/bluetooth.h>
22 #include <zephyr/bluetooth/conn.h>
23 #include <zephyr/bluetooth/gatt.h>
24 #include <zephyr/bluetooth/uuid.h>
25 #include <zephyr/bluetooth/services/bas.h>
26 #include "bas_internal.h"
27 
28 #define LOG_LEVEL CONFIG_BT_BAS_LOG_LEVEL
29 #include <zephyr/logging/log.h>
30 LOG_MODULE_REGISTER(bas, CONFIG_BT_BAS_LOG_LEVEL);
31 
32 static uint8_t battery_level = 100U;
33 
blvl_ccc_cfg_changed(const struct bt_gatt_attr * attr,uint16_t value)34 static void blvl_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
35 {
36 	ARG_UNUSED(attr);
37 
38 	bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
39 
40 	LOG_INF("BAS Notifications %s", notif_enabled ? "enabled" : "disabled");
41 }
42 
43 #if defined(CONFIG_BT_BAS_BLS)
blvl_status_ccc_cfg_changed(const struct bt_gatt_attr * attr,uint16_t value)44 static void blvl_status_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
45 {
46 	ARG_UNUSED(attr);
47 
48 	bool notif_enabled = (value == BT_GATT_CCC_NOTIFY);
49 	bool ind_enabled = (value == BT_GATT_CCC_INDICATE);
50 
51 	LOG_INF("BAS Notifications %s", notif_enabled ? "enabled" : "disabled");
52 	LOG_INF("BAS Indications %s", ind_enabled ? "enabled" : "disabled");
53 }
54 #endif
55 
read_blvl(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)56 static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf,
57 			 uint16_t len, uint16_t offset)
58 {
59 	uint8_t lvl8 = battery_level;
60 
61 	return bt_gatt_attr_read(conn, attr, buf, len, offset, &lvl8, sizeof(lvl8));
62 }
63 
64 /* Constant values from the Assigned Numbers specification:
65  * https://www.bluetooth.com/wp-content/uploads/Files/Specification/Assigned_Numbers.pdf?id=89
66  */
67 static const struct bt_gatt_cpf level_cpf = {
68 	.format = 0x04, /* uint8 */
69 	.exponent = 0x0,
70 	.unit = 0x27AD,        /* Percentage */
71 	.name_space = 0x01,    /* Bluetooth SIG */
72 	.description = 0x0106, /* "main" */
73 };
74 
75 BT_GATT_SERVICE_DEFINE(
76 	bas, BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS),
77 	BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY,
78 			       BT_GATT_PERM_READ, read_blvl, NULL, NULL),
79 	BT_GATT_CCC(blvl_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
80 	BT_GATT_CPF(&level_cpf),
81 #if defined(CONFIG_BT_BAS_BLS)
82 	BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL_STATUS,
83 			       BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY | BT_GATT_CHRC_INDICATE,
84 			       BT_GATT_PERM_READ, bt_bas_bls_read_blvl_status, NULL, NULL),
85 	BT_GATT_CCC(blvl_status_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
86 #endif
87 #if defined(CONFIG_BT_BAS_BCS)
88 	BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_CRIT_STATUS,
89 			       BT_GATT_CHRC_READ | BT_GATT_CHRC_INDICATE, BT_GATT_PERM_READ,
90 			       bt_bas_bcs_read_critical_status, NULL, NULL),
91 	BT_GATT_CCC(bt_bas_bcs_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
92 #endif /* CONFIG_BT_BAS_BCS */
93 );
94 
bas_init(void)95 static int bas_init(void)
96 {
97 	if (IS_ENABLED(CONFIG_BT_BAS_BLS)) {
98 		/* Initialize the Battery Level Status Module */
99 		bt_bas_bls_init();
100 		if (IS_ENABLED(CONFIG_BT_BAS_BLS_IDENTIFIER_PRESENT)) {
101 			/* Set the identifier only if BT_BAS_BLS_IDENTIFIER_PRESENT is defined */
102 			bt_bas_bls_set_identifier(level_cpf.description);
103 		}
104 	}
105 	return 0;
106 }
107 
bt_bas_get_battery_level(void)108 uint8_t bt_bas_get_battery_level(void)
109 {
110 	return battery_level;
111 }
112 
bt_bas_set_battery_level(uint8_t level)113 int bt_bas_set_battery_level(uint8_t level)
114 {
115 	int rc;
116 
117 	if (level > 100U) {
118 		return -EINVAL;
119 	}
120 
121 	battery_level = level;
122 
123 	rc = bt_gatt_notify(NULL, &bas.attrs[1], &level, sizeof(level));
124 
125 	if (IS_ENABLED(CONFIG_BT_BAS_BLS_BATTERY_LEVEL_PRESENT)) {
126 		bt_bas_bls_set_battery_level(level);
127 	}
128 
129 	return rc == -ENOTCONN ? 0 : rc;
130 }
131 
bt_bas_get_bas_attr(uint16_t index)132 const struct bt_gatt_attr *bt_bas_get_bas_attr(uint16_t index)
133 {
134 	if (index < bas.attr_count) {
135 		return &bas.attrs[index];
136 	}
137 	return NULL;
138 }
139 
140 SYS_INIT(bas_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
141