1 /* 2 * Copyright (c) 2024 Demant A/S 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef BT_BAS_INTERNAL_H_ 8 #define BT_BAS_INTERNAL_H_ 9 10 #include <sys/types.h> 11 #include <zephyr/sys/atomic.h> 12 #include <zephyr/types.h> 13 14 /** 15 * @brief Battery level status structure definition. 16 */ 17 struct bt_bas_bls { 18 19 /** @brief Flags Field 20 * 21 * The values of this field are defined below. 22 * 23 * - bit 0: Identifier Present 24 * - Indicates whether the identifier field is present. 25 * - bit 1: Battery Level Present 26 * - Indicates whether the battery level field is present. 27 * - bit 2: Additional Status Present 28 * - Indicates whether the additional status field is present. 29 * - bit 3–7: RFU (Reserved for Future Use) 30 * - Reserved bits for future use; should be set to zero. 31 */ 32 uint8_t flags; 33 34 /** @brief Power State 35 * 36 * The values of this field are defined below. 37 * 38 * - bit 0: Battery Present 39 * - 0 = No 40 * - 1 = Yes 41 * - bit 1–2: Wired External Power Source Connected 42 * - 0 = No 43 * - 1 = Yes 44 * - 2 = Unknown 45 * - 3 = RFU 46 * - bit 3–4: Wireless External Power Source Connected 47 * - 0 = No 48 * - 1 = Yes 49 * - 2 = Unknown 50 * - 3 = RFU 51 * - bit 5–6: Battery Charge State 52 * - 0 = Unknown 53 * - 1 = Charging 54 * - 2 = Discharging: Active 55 * - 3 = Discharging: Inactive 56 * - bit 7–8: Battery Charge Level 57 * - 0 = Unknown 58 * - 1 = Good 59 * - 2 = Low 60 * - 3 = Critical 61 * - bit 9–11: Charging Type 62 * - 0 = Unknown or Not Charging 63 * - 1 = Constant Current 64 * - 2 = Constant Voltage 65 * - 3 = Trickle 66 * - 4 = Float 67 * - 5–7 = RFU 68 * - bit 12–14: Charging Fault Reason 69 * - Bit 12: Battery 70 * - Bit 13: External Power source 71 * - Bit 14: Other 72 * - bit 15: RFU 73 */ 74 uint16_t power_state; 75 76 #if defined(CONFIG_BT_BAS_BLS_IDENTIFIER_PRESENT) 77 /** Identifier for the battery, range 0x0000 to 0xFFFF.*/ 78 uint16_t identifier; 79 #endif /* CONFIG_BT_BAS_BLS_IDENTIFIER_PRESENT */ 80 81 #if defined(CONFIG_BT_BAS_BLS_BATTERY_LEVEL_PRESENT) 82 /** Current battery level */ 83 uint8_t battery_level; 84 #endif /* CONFIG_BT_BAS_BLS_BATTERY_LEVEL_PRESENT */ 85 86 #if defined(CONFIG_BT_BAS_BLS_ADDITIONAL_STATUS_PRESENT) 87 88 /** @brief Additional Status 89 * 90 * The values of this field are defined below. 91 * 92 * - bit 0–1: Service Required 93 * - 0 = False 94 * - 1 = True 95 * - 2 = Unknown 96 * - 3 = RFU 97 * - bit 2: Battery Fault 98 * - 0 = False or Unknown 99 * - 1 = Yes 100 * - bit 3–7: RFU 101 */ 102 uint8_t additional_status; 103 104 #endif /* CONFIG_BT_BAS_BLS_ADDITIONAL_STATUS_PRESENT */ 105 106 } __packed; 107 108 /** 109 * @brief Initialize the Battery Level Status Module. 110 * 111 */ 112 void bt_bas_bls_init(void); 113 114 /** 115 * @brief Read the Battery Critical Status characteristic. 116 * 117 * @param conn Pointer to the Bluetooth connection object representing the client requesting 118 * the characteristic. 119 * @param attr Pointer to the GATT attribute of Battery Critical Status characteristic. 120 * @param buf Buffer to store the read value. 121 * @param len Length of the buffer. 122 * @param offset Offset within the characteristic value to start reading. 123 * 124 * @return The number of bytes read and sent to the client, or a negative error code on failure. 125 */ 126 ssize_t bt_bas_bcs_read_critical_status(struct bt_conn *conn, const struct bt_gatt_attr *attr, 127 void *buf, uint16_t len, uint16_t offset); 128 129 /** 130 * @brief Callback function for BCS Client Characteristic Configuration changes. 131 * 132 * 133 * @param attr Pointer to the GATT attribute of battery critical status char. 134 * @param value The new value of the CCC. This value indicates whether 135 * notifications or indications are enabled or disabled. 136 */ 137 void bt_bas_bcs_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value); 138 139 /** 140 * @brief Set the battery level characteristic value. 141 * 142 * @param battery_level The new battery level value in percent (0-100). 143 */ 144 void bt_bas_bls_set_battery_level(uint8_t battery_level); 145 146 /** 147 * @brief Set the battery critical state flag. 148 * 149 * @param critical_state The battery critical state to set (true for critical, false otherwise). 150 */ 151 void bt_bas_bcs_set_battery_critical_state(bool critical_state); 152 153 /** 154 * @brief Set the immediate service required flag. 155 * 156 * @param service_required The immediate service required status to set. 157 */ 158 void bt_bas_bcs_set_immediate_service_required(bool service_required); 159 160 /** 161 * @brief Read the Battery Level Status characteristic. 162 * 163 * @param conn Pointer to the Bluetooth connection object representing the client requesting 164 * the characteristic. 165 * @param attr Pointer to the GATT attribute representing the Battery Level Status characteristic. 166 * @param buf Buffer to store the read value. 167 * @param len Length of the buffer. 168 * @param offset Offset within the characteristic value to start reading. 169 * 170 * @return The number of bytes read and sent to the client, or a negative error code on failure. 171 */ 172 ssize_t bt_bas_bls_read_blvl_status(struct bt_conn *conn, const struct bt_gatt_attr *attr, 173 void *buf, uint16_t len, uint16_t offset); 174 175 /** 176 * @brief Retrieve the Bluetooth GATT attribute for the BAS service by index. 177 * 178 * @param index The index of the attribute within the BAS service. 179 * 180 * @return Pointer to the Bluetooth GATT attribute if the index is valid, 181 * otherwise NULL if the index is out of bounds. 182 */ 183 const struct bt_gatt_attr *bt_bas_get_bas_attr(uint16_t index); 184 185 #endif /* BT_BAS_INTERNAL_H_ */ 186