Skip to content

Commit baa5683

Browse files
niym-otnashif
authored andcommitted
bluetooth: BAS: add battery level status char to bas service
Added the battery level status char to bas service as per bas_1.1 spec. Added BSIM test for BAS service to test the NTF/INDs of BAS characteristics. Signed-off-by: Nithin Ramesh Myliattil <[email protected]>
1 parent b3ae323 commit baa5683

File tree

19 files changed

+1643
-110
lines changed

19 files changed

+1643
-110
lines changed

include/zephyr/bluetooth/services/bas.h

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2024 Demant A/S
23
* Copyright (c) 2018 Nordic Semiconductor ASA
34
* Copyright (c) 2016 Intel Corporation
45
*
@@ -19,11 +20,173 @@
1920
*/
2021

2122
#include <stdint.h>
23+
#include <zephyr/sys/util.h>
2224

2325
#ifdef __cplusplus
2426
extern "C" {
2527
#endif
2628

29+
/**
30+
* @brief Battery Level Status Characteristic flags.
31+
*
32+
* Enumeration for the flags indicating the presence
33+
* of various fields in the Battery Level Status characteristic.
34+
*/
35+
enum bt_bas_bls_flags {
36+
/** Bit indicating that the Battery Level Status identifier is present. */
37+
BT_BAS_BLS_FLAG_IDENTIFIER_PRESENT = BIT(0),
38+
39+
/** Bit indicating that the Battery Level is present. */
40+
BT_BAS_BLS_FLAG_BATTERY_LEVEL_PRESENT = BIT(1),
41+
42+
/** Bit indicating that additional status information is present. */
43+
BT_BAS_BLS_FLAG_ADDITIONAL_STATUS_PRESENT = BIT(2),
44+
};
45+
46+
/** @brief Battery Present Status
47+
*
48+
* Enumeration for the presence of the battery.
49+
*/
50+
enum bt_bas_bls_battery_present {
51+
/** Battery is not present. */
52+
BT_BAS_BLS_BATTERY_NOT_PRESENT = 0,
53+
54+
/** Battery is present. */
55+
BT_BAS_BLS_BATTERY_PRESENT = 1
56+
};
57+
58+
/** @brief Wired External Power Source Status
59+
*
60+
* Enumeration for the status of the wired external power source.
61+
*/
62+
enum bt_bas_bls_wired_power_source {
63+
/** Wired external power source is not connected. */
64+
BT_BAS_BLS_WIRED_POWER_NOT_CONNECTED = 0,
65+
66+
/** Wired external power source is connected. */
67+
BT_BAS_BLS_WIRED_POWER_CONNECTED = 1,
68+
69+
/** Wired external power source status is unknown. */
70+
BT_BAS_BLS_WIRED_POWER_UNKNOWN = 2
71+
};
72+
73+
/** @brief Wireless External Power Source Status
74+
*
75+
* Enumeration for the status of the wireless external power source.
76+
*/
77+
enum bt_bas_bls_wireless_power_source {
78+
/** Wireless external power source is not connected. */
79+
BT_BAS_BLS_WIRELESS_POWER_NOT_CONNECTED = 0,
80+
81+
/** Wireless external power source is connected. */
82+
BT_BAS_BLS_WIRELESS_POWER_CONNECTED = 1,
83+
84+
/** Wireless external power source status is unknown. */
85+
BT_BAS_BLS_WIRELESS_POWER_UNKNOWN = 2
86+
};
87+
88+
/** @brief Battery Charge State
89+
*
90+
* Enumeration for the charge state of the battery.
91+
*/
92+
enum bt_bas_bls_battery_charge_state {
93+
/** Battery charge state is unknown. */
94+
BT_BAS_BLS_CHARGE_STATE_UNKNOWN = 0,
95+
96+
/** Battery is currently charging. */
97+
BT_BAS_BLS_CHARGE_STATE_CHARGING = 1,
98+
99+
/** Battery is discharging actively. */
100+
BT_BAS_BLS_CHARGE_STATE_DISCHARGING_ACTIVE = 2,
101+
102+
/** Battery is discharging but inactive. */
103+
BT_BAS_BLS_CHARGE_STATE_DISCHARGING_INACTIVE = 3
104+
};
105+
106+
/** @brief Battery Charge Level
107+
*
108+
* Enumeration for the level of charge in the battery.
109+
*/
110+
enum bt_bas_bls_battery_charge_level {
111+
/** Battery charge level is unknown. */
112+
BT_BAS_BLS_CHARGE_LEVEL_UNKNOWN = 0,
113+
114+
/** Battery charge level is good. */
115+
BT_BAS_BLS_CHARGE_LEVEL_GOOD = 1,
116+
117+
/** Battery charge level is low. */
118+
BT_BAS_BLS_CHARGE_LEVEL_LOW = 2,
119+
120+
/** Battery charge level is critical. */
121+
BT_BAS_BLS_CHARGE_LEVEL_CRITICAL = 3
122+
};
123+
124+
/** @brief Battery Charge Type
125+
*
126+
* Enumeration for the type of charging applied to the battery.
127+
*/
128+
enum bt_bas_bls_battery_charge_type {
129+
/** Battery charge type is unknown or not charging. */
130+
BT_BAS_BLS_CHARGE_TYPE_UNKNOWN = 0,
131+
132+
/** Battery is charged using constant current. */
133+
BT_BAS_BLS_CHARGE_TYPE_CONSTANT_CURRENT = 1,
134+
135+
/** Battery is charged using constant voltage. */
136+
BT_BAS_BLS_CHARGE_TYPE_CONSTANT_VOLTAGE = 2,
137+
138+
/** Battery is charged using trickle charge. */
139+
BT_BAS_BLS_CHARGE_TYPE_TRICKLE = 3,
140+
141+
/** Battery is charged using float charge. */
142+
BT_BAS_BLS_CHARGE_TYPE_FLOAT = 4
143+
};
144+
145+
/** @brief Charging Fault Reason
146+
*
147+
* Enumeration for the reasons of charging faults.
148+
*/
149+
enum bt_bas_bls_charging_fault_reason {
150+
/** No charging fault. */
151+
BT_BAS_BLS_FAULT_REASON_NONE = 0,
152+
153+
/** Charging fault due to battery issue. */
154+
BT_BAS_BLS_FAULT_REASON_BATTERY = BIT(0),
155+
156+
/** Charging fault due to external power source issue. */
157+
BT_BAS_BLS_FAULT_REASON_EXTERNAL_POWER = BIT(1),
158+
159+
/** Charging fault for other reasons. */
160+
BT_BAS_BLS_FAULT_REASON_OTHER = BIT(2)
161+
};
162+
163+
/** @brief Service Required Status
164+
*
165+
* Enumeration for whether the service is required.
166+
*/
167+
enum bt_bas_bls_service_required {
168+
/** Service is not required. */
169+
BT_BAS_BLS_SERVICE_REQUIRED_FALSE = 0,
170+
171+
/** Service is required. */
172+
BT_BAS_BLS_SERVICE_REQUIRED_TRUE = 1,
173+
174+
/** Service requirement is unknown. */
175+
BT_BAS_BLS_SERVICE_REQUIRED_UNKNOWN = 2
176+
};
177+
178+
/** @brief Battery Fault Status
179+
*
180+
* Enumeration for the fault status of the battery.
181+
*/
182+
enum bt_bas_bls_battery_fault {
183+
/** No battery fault. */
184+
BT_BAS_BLS_BATTERY_FAULT_NO = 0,
185+
186+
/** Battery fault present. */
187+
BT_BAS_BLS_BATTERY_FAULT_YES = 1
188+
};
189+
27190
/** @brief Read battery level value.
28191
*
29192
* Read the characteristic value of the battery level
@@ -43,6 +206,81 @@ uint8_t bt_bas_get_battery_level(void);
43206
*/
44207
int bt_bas_set_battery_level(uint8_t level);
45208

209+
/**
210+
* @brief Set the battery present status.
211+
*
212+
* @param present The battery present status to set.
213+
*/
214+
void bt_bas_bls_set_battery_present(enum bt_bas_bls_battery_present present);
215+
216+
/**
217+
* @brief Set the wired external power source status.
218+
*
219+
* @param source The wired external power source status to set.
220+
*/
221+
void bt_bas_bls_set_wired_external_power_source(enum bt_bas_bls_wired_power_source source);
222+
223+
/**
224+
* @brief Set the wireless external power source status.
225+
*
226+
* @param source The wireless external power source status to set.
227+
*/
228+
void bt_bas_bls_set_wireless_external_power_source(enum bt_bas_bls_wireless_power_source source);
229+
230+
/**
231+
* @brief Set the battery charge state.
232+
*
233+
* @param state The battery charge state to set.
234+
*/
235+
void bt_bas_bls_set_battery_charge_state(enum bt_bas_bls_battery_charge_state state);
236+
237+
/**
238+
* @brief Set the battery charge level.
239+
*
240+
* @param level The battery charge level to set.
241+
*/
242+
void bt_bas_bls_set_battery_charge_level(enum bt_bas_bls_battery_charge_level level);
243+
244+
/**
245+
* @brief Set the battery charge type.
246+
*
247+
* @param type The battery charge type to set.
248+
*/
249+
void bt_bas_bls_set_battery_charge_type(enum bt_bas_bls_battery_charge_type type);
250+
251+
/**
252+
* @brief Set the charging fault reason.
253+
*
254+
* @param reason The charging fault reason to set.
255+
*/
256+
void bt_bas_bls_set_charging_fault_reason(enum bt_bas_bls_charging_fault_reason reason);
257+
258+
/**
259+
* @brief Set the identifier of the battery.
260+
*
261+
* kconfig_dep{CONFIG_BT_BAS_BLS_IDENTIFIER_PRESENT}
262+
*
263+
* @param identifier Identifier to set.
264+
*/
265+
void bt_bas_bls_set_identifier(uint16_t identifier);
266+
267+
/**
268+
* @brief Set the service required status.
269+
*
270+
* kconfig_dep{CONFIG_BT_BAS_BLS_ADDITIONAL_STATUS_PRESENT}
271+
*
272+
* @param value Service required status to set.
273+
*/
274+
void bt_bas_bls_set_service_required(enum bt_bas_bls_service_required value);
275+
276+
/**
277+
* @brief Set the battery fault status.
278+
*
279+
* kconfig_dep{CONFIG_BT_BAS_BLS_ADDITIONAL_STATUS_PRESENT}
280+
*
281+
* @param value Battery fault status to set.
282+
*/
283+
void bt_bas_bls_set_battery_fault(enum bt_bas_bls_battery_fault value);
46284

47285
#ifdef __cplusplus
48286
}

samples/bluetooth/hap_ha/prj.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ CONFIG_BT_TBS_CLIENT_GTBS=y
5656
CONFIG_BT_TBS_CLIENT_CCID=y
5757
CONFIG_BT_TBS_CLIENT_STATUS_FLAGS=y
5858

59+
CONFIG_BT_BAS_BLS=y
60+
CONFIG_BT_BAS_BLS_IDENTIFIER_PRESENT=y
61+
CONFIG_BT_BAS_BLS_BATTERY_LEVEL_PRESENT=y
62+
CONFIG_BT_BAS_BLS_ADDITIONAL_STATUS_PRESENT=y
63+
5964
CONFIG_LOG=y

subsys/bluetooth/services/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
zephyr_sources_ifdef(CONFIG_BT_DIS dis.c)
55

6-
zephyr_sources_ifdef(CONFIG_BT_BAS bas.c)
7-
86
zephyr_sources_ifdef(CONFIG_BT_HRS hrs.c)
97

108
zephyr_sources_ifdef(CONFIG_BT_TPS tps.c)
119

10+
if(CONFIG_BT_BAS)
11+
add_subdirectory(bas)
12+
endif()
13+
1214
if(CONFIG_BT_OTS OR CONFIG_BT_OTS_CLIENT)
1315
add_subdirectory(ots)
1416
endif()

subsys/bluetooth/services/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ menu "GATT Services"
88

99
rsource "Kconfig.dis"
1010

11-
rsource "Kconfig.bas"
12-
1311
rsource "Kconfig.hrs"
1412

1513
rsource "Kconfig.tps"
@@ -20,4 +18,6 @@ rsource "ias/Kconfig.ias"
2018

2119
rsource "ots/Kconfig"
2220

21+
rsource "bas/Kconfig.bas"
22+
2323
endmenu

subsys/bluetooth/services/Kconfig.bas

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)