From 2a8b44d0582033790e289f113102e7e8a124e157 Mon Sep 17 00:00:00 2001 From: skiinder Date: Mon, 16 Mar 2026 17:45:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(app):=20=E6=B7=BB=E5=8A=A0=E8=93=9D?= =?UTF-8?q?=E7=89=99=E7=94=B5=E9=87=8F=E6=A8=A1=E5=9D=97=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在CMakeLists.txt中添加ble_battery_module.c源文件 - 实现BLE电池服务模块,提供电池电量GATT服务 - 支持电池电量读取和通知功能 - 集成到应用事件管理器,监听电池状态事件 - 当蓝牙连接就绪时设置模块状态为READY --- CMakeLists.txt | 1 + src/modules/ble_battery_module.c | 91 ++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/modules/ble_battery_module.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 36eca63..93e1dc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ target_sources(app PRIVATE src/events/mode_event.c src/modules/battery_module.c src/modules/ble_adv_ctrl_module.c + src/modules/ble_battery_module.c src/modules/ble_bond_module.c src/modules/keyboard_module.c src/modules/led_state_module.c diff --git a/src/modules/ble_battery_module.c b/src/modules/ble_battery_module.c new file mode 100644 index 0000000..521e6ff --- /dev/null +++ b/src/modules/ble_battery_module.c @@ -0,0 +1,91 @@ +#include + +#include +#include + +#include + +#define MODULE ble_battery +#include + +#include "battery_status_event.h" + +#include +LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF); + +static bool notify_enabled; +static uint8_t battery_level = 100U; + +static void bas_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value) +{ + ARG_UNUSED(attr); + notify_enabled = (value == BT_GATT_CCC_NOTIFY); +} + +static ssize_t read_battery_level(struct bt_conn *conn, + const struct bt_gatt_attr *attr, + void *buf, + uint16_t len, + uint16_t offset) +{ + const uint8_t *value = attr->user_data; + + return bt_gatt_attr_read(conn, attr, buf, len, offset, value, sizeof(*value)); +} + +BT_GATT_SERVICE_DEFINE(ble_battery_svc, + BT_GATT_PRIMARY_SERVICE(BT_UUID_BAS), + BT_GATT_CHARACTERISTIC(BT_UUID_BAS_BATTERY_LEVEL, + BT_GATT_CHRC_READ | BT_GATT_CHRC_NOTIFY, + BT_GATT_PERM_READ_ENCRYPT, + read_battery_level, NULL, &battery_level), + BT_GATT_CCC(bas_ccc_cfg_changed, + BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT), +); + +static bool handle_battery_status_event(const struct battery_status_event *event) +{ + battery_level = event->soc; + + if (!notify_enabled) { + return false; + } + + int err = bt_gatt_notify(NULL, &ble_battery_svc.attrs[1], &battery_level, sizeof(battery_level)); + + if (err == -ENOTCONN) { + LOG_WRN("BAS notify skipped: peer disconnecting"); + } else if (err) { + LOG_ERR("BAS notify failed: %d", err); + } + + return false; +} + +static bool handle_module_state_event(const struct module_state_event *event) +{ + if (!check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) { + return false; + } + + module_set_state(MODULE_STATE_READY); + return false; +} + +static bool app_event_handler(const struct app_event_header *aeh) +{ + if (is_battery_status_event(aeh)) { + return handle_battery_status_event(cast_battery_status_event(aeh)); + } + + if (is_module_state_event(aeh)) { + return handle_module_state_event(cast_module_state_event(aeh)); + } + + __ASSERT_NO_MSG(false); + return false; +} + +APP_EVENT_LISTENER(MODULE, app_event_handler); +APP_EVENT_SUBSCRIBE(MODULE, battery_status_event); +APP_EVENT_SUBSCRIBE(MODULE, module_state_event);