-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
feat(battery): Split battery reporting over BLE GATT #1243
Closed
Closed
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
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
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,90 @@ | ||
/* | ||
* Copyright (c) 2020 The ZMK Contributors | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#include <zephyr/device.h> | ||
#include <zephyr/init.h> | ||
#include <sys/types.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/drivers/sensor.h> | ||
#include <zephyr/bluetooth/gatt.h> | ||
|
||
#include <zephyr/logging/log.h> | ||
|
||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); | ||
|
||
#include <zmk/event_manager.h> | ||
#include <zmk/battery.h> | ||
#include <zmk/events/battery_state_changed.h> | ||
|
||
// Initialize the charge level to a special value indicating no sampling has been made yet. | ||
static uint8_t last_state_of_peripheral_charge[CONFIG_ZMK_SPLIT_BLE_CENTRAL_PERIPHERALS] = {0}; | ||
|
||
static void blvl_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value) { | ||
ARG_UNUSED(attr); | ||
|
||
bool notif_enabled = (value == BT_GATT_CCC_NOTIFY); | ||
|
||
LOG_INF("BAS Notifications %s", notif_enabled ? "enabled" : "disabled"); | ||
} | ||
|
||
static ssize_t read_blvl(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, | ||
uint16_t len, uint16_t offset) { | ||
const char *lvl8 = attr->user_data; | ||
return bt_gatt_attr_read(conn, attr, buf, len, offset, lvl8, sizeof(uint8_t)); | ||
} | ||
|
||
static const struct bt_gatt_cpf aux_level_cpf = { | ||
.format = 0x04, // uint8 | ||
.exponent = 0x0, | ||
.unit = 0x27AD, // Percentage | ||
.name_space = 0x01, // Bluetooth SIG | ||
.description = 0x0108, // "auxiliary" | ||
}; | ||
|
||
#define PERIPH_CUD_(x) "Peripheral " #x | ||
#define PERIPH_CUD(x) PERIPH_CUD_(x) | ||
|
||
#define PERIPH_BATT_LEVEL_ATTRS(i, _) \ | ||
BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL, BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, \ | ||
BT_GATT_PERM_READ, read_blvl, NULL, \ | ||
&last_state_of_peripheral_charge[i]), \ | ||
BT_GATT_CCC(blvl_ccc_cfg_changed, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE), \ | ||
BT_GATT_CPF(&aux_level_cpf), BT_GATT_CUD(PERIPH_CUD(i), BT_GATT_PERM_READ), | ||
|
||
BT_GATT_SERVICE_DEFINE(bas_aux, BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS), | ||
LISTIFY(CONFIG_ZMK_SPLIT_BLE_CENTRAL_PERIPHERALS, PERIPH_BATT_LEVEL_ATTRS, | ||
())); | ||
|
||
int peripheral_batt_lvl_listener(const zmk_event_t *eh) { | ||
const struct zmk_peripheral_battery_state_changed *ev = | ||
as_zmk_peripheral_battery_state_changed(eh); | ||
if (ev == NULL) { | ||
return ZMK_EV_EVENT_BUBBLE; | ||
}; | ||
|
||
if (ev->source >= CONFIG_ZMK_SPLIT_BLE_CENTRAL_PERIPHERALS) { | ||
LOG_WRN("Got battery level event for an out of range peripheral index"); | ||
return ZMK_EV_EVENT_BUBBLE; | ||
} | ||
|
||
LOG_DBG("Peripheral battery level event: %u", ev->state_of_charge); | ||
last_state_of_peripheral_charge[ev->source] = ev->state_of_charge; | ||
|
||
// 1 offsets for the service attribute, then offset 5 for each battery because that's how | ||
// many attributes are added per battery | ||
uint8_t index = 1 + (5 * ev->source); | ||
|
||
int rc = bt_gatt_notify(NULL, &bas_aux.attrs[index], &last_state_of_peripheral_charge, | ||
sizeof(last_state_of_peripheral_charge)); | ||
if (rc < 0 && rc != -ENOTCONN) { | ||
LOG_WRN("Failed to notify hosts of peripheral battery level: %d", rc); | ||
} | ||
|
||
return ZMK_EV_EVENT_BUBBLE; | ||
}; | ||
|
||
ZMK_LISTENER(peripheral_batt_lvl_listener, peripheral_batt_lvl_listener); | ||
ZMK_SUBSCRIPTION(peripheral_batt_lvl_listener, zmk_peripheral_battery_state_changed); |
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
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.
Hmm.... We already have the concept of position events having a "source" (see
app/include/zmk/events/position_state_changed.h
). If we're now going to have yet another event that is either from local or peripheral device, we might want to pull that out in a more generalized place, and simply supplement the existin battery state changed event w/ asource
field as well. Thoughts?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.
I agree, I need some time to understand to how that's implemented/used.