-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bluetooth: BAS: add battery critical status char to bas service #79966
Merged
aescolar
merged 1 commit into
zephyrproject-rtos:main
from
niym-ot:Add_battery_critical_status_char_to_battery_service
Oct 22, 2024
+334
−37
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
zephyr_sources_ifdef(CONFIG_BT_BAS bas.c) | ||
zephyr_sources_ifdef(CONFIG_BT_BAS_BLS bas_bls.c) | ||
zephyr_sources_ifdef(CONFIG_BT_BAS_BCS bas_bcs.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) 2024 Demant A/S | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/bluetooth/services/bas.h> | ||
#include <zephyr/bluetooth/gatt.h> | ||
#include "bas_internal.h" | ||
|
||
#include <zephyr/logging/log.h> | ||
LOG_MODULE_DECLARE(bas, CONFIG_BT_BAS_LOG_LEVEL); | ||
|
||
#define BATTERY_CRITICAL_STATUS_CHAR_IDX 9 | ||
|
||
static uint8_t battery_critical_status; | ||
|
||
void bt_bas_bcs_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value) | ||
{ | ||
ARG_UNUSED(attr); | ||
|
||
bool ind_enabled = (value == BT_GATT_CCC_INDICATE); | ||
|
||
LOG_DBG("BAS Critical status Indication %s", ind_enabled ? "enabled" : "disabled"); | ||
} | ||
|
||
ssize_t bt_bas_bcs_read_critical_status(struct bt_conn *conn, const struct bt_gatt_attr *attr, | ||
void *buf, uint16_t len, uint16_t offset) | ||
{ | ||
|
||
return bt_gatt_attr_read(conn, attr, buf, len, offset, &battery_critical_status, | ||
sizeof(uint8_t)); | ||
} | ||
|
||
static void bcs_indicate_cb(struct bt_conn *conn, struct bt_gatt_indicate_params *params, | ||
uint8_t err) | ||
{ | ||
if (err != 0) { | ||
LOG_DBG("BCS Indication failed with error %u\n", err); | ||
} else { | ||
LOG_DBG("BCS Indication sent successfully\n"); | ||
} | ||
} | ||
|
||
static void bt_bas_bcs_update_battery_critical_status(void) | ||
{ | ||
/* Indicate all connections */ | ||
const struct bt_gatt_attr *attr = bt_bas_get_bas_attr(BATTERY_CRITICAL_STATUS_CHAR_IDX); | ||
|
||
if (attr) { | ||
uint8_t err; | ||
/* Indicate battery critical status to all connections */ | ||
static struct bt_gatt_indicate_params bcs_ind_params; | ||
|
||
bcs_ind_params.attr = attr; | ||
bcs_ind_params.data = &battery_critical_status; | ||
bcs_ind_params.len = sizeof(battery_critical_status); | ||
bcs_ind_params.func = bcs_indicate_cb; | ||
err = bt_gatt_indicate(NULL, &bcs_ind_params); | ||
if (err && err != -ENOTCONN) { | ||
LOG_DBG("Failed to send critical status ind to all conns (err %d)\n", err); | ||
} | ||
} | ||
} | ||
|
||
void bt_bas_bcs_set_battery_critical_state(bool critical_state) | ||
{ | ||
bool current_state = (battery_critical_status & BT_BAS_BCS_BATTERY_CRITICAL_STATE) != 0; | ||
|
||
if (current_state == critical_state) { | ||
LOG_DBG("Already battery_critical_state is %d\n", critical_state); | ||
return; | ||
} | ||
|
||
if (critical_state) { | ||
battery_critical_status |= BT_BAS_BCS_BATTERY_CRITICAL_STATE; | ||
} else { | ||
battery_critical_status &= ~BT_BAS_BCS_BATTERY_CRITICAL_STATE; | ||
} | ||
bt_bas_bcs_update_battery_critical_status(); | ||
} | ||
|
||
void bt_bas_bcs_set_immediate_service_required(bool service_required) | ||
{ | ||
bool current_state = (battery_critical_status & BT_BAS_BCS_IMMEDIATE_SERVICE_REQUIRED) != 0; | ||
|
||
if (current_state == service_required) { | ||
LOG_DBG("Already immediate_service_required is %d\n", service_required); | ||
return; | ||
} | ||
|
||
if (service_required) { | ||
battery_critical_status |= BT_BAS_BCS_IMMEDIATE_SERVICE_REQUIRED; | ||
} else { | ||
battery_critical_status &= ~BT_BAS_BCS_IMMEDIATE_SERVICE_REQUIRED; | ||
} | ||
bt_bas_bcs_update_battery_critical_status(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now this is fine, but if you start adding the other optional characteristics, then we can no longer rely on a static index.
An alternative is to use the UUID when calling
bt_gatt_indicate
which will be futureproof