feat: 添加蓝牙多槽位绑定支持模块
- 新增 ble_bond_multi_module.c 实现多槽位蓝牙绑定管理功能 - 添加 ble_bond_multi_event 事件系统支持槽位状态广播 - 在 CMakeLists.txt 中注册新模块和事件源文件 - 更新 Kconfig 配置添加 BLINKY_BLE_BOND_MULTI 选项 - 修改 prj.conf 配置支持 4 个配对设备和 5 个身份标识 - 关闭默认 CAF ble_bond 模块使用自定义实现 - 更新 ui_settings_controller.h 接口支持槽位元数据设置 - 在 display_module.c 中添加事件订阅刷新UI显示 - 编写详细的设计文档 ble_multi_slot_design.md
This commit is contained in:
527
src/ble_bond_multi_module.c
Normal file
527
src/ble_bond_multi_module.c
Normal file
@@ -0,0 +1,527 @@
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE ble_bond
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
#include <caf/events/ble_common_event.h>
|
||||
#include <zephyr/bluetooth/bluetooth.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/addr.h>
|
||||
#include <zephyr/settings/settings.h>
|
||||
#include <zephyr/sys/printk.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "ble_bond_multi_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define BLE_SLOT_MIN 1U
|
||||
#define BLE_SLOT_MAX BLE_BOND_MULTI_BLE_SLOT_COUNT
|
||||
#define IDENTITY_DONGLE BLE_BOND_MULTI_DONGLE_SLOT_ID
|
||||
#define SETTINGS_KEY_CURRENT_SLOT "current_slot"
|
||||
#define SETTINGS_KEY_META_PREFIX "meta/"
|
||||
#define DEFAULT_DISPLAY_NAME_BONDED "Bonded Device"
|
||||
#define DEFAULT_DISPLAY_NAME_EMPTY "Empty"
|
||||
|
||||
struct ble_bond_multi_slot_meta_storage {
|
||||
uint8_t occupied;
|
||||
bt_addr_le_t last_peer_addr;
|
||||
char display_name[BLE_BOND_MULTI_DISPLAY_NAME_MAX_LEN];
|
||||
};
|
||||
|
||||
struct ble_bond_multi_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
uint8_t current_slot;
|
||||
uint8_t pending_slot;
|
||||
bool current_slot_valid;
|
||||
bool identities_ready;
|
||||
struct ble_bond_multi_slot_meta slot_meta[CONFIG_BT_ID_MAX];
|
||||
struct bt_conn *active_conn;
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
static int identity_ensure_exists(uint8_t identity);
|
||||
static void slot_bond_cnt_cb(const struct bt_bond_info *info, void *user_data);
|
||||
int ble_bond_multi_select_slot(uint8_t slot);
|
||||
int ble_bond_multi_erase_current_slot(void);
|
||||
const struct ble_bond_multi_slot_meta *ble_bond_multi_get_slot_meta(uint8_t slot);
|
||||
uint8_t ble_bond_multi_get_current_slot(void);
|
||||
|
||||
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
||||
.mode = ML_MODE_NONE,
|
||||
.stopped_state = MODULE_STATE_OFF,
|
||||
};
|
||||
|
||||
static const struct module_lifecycle_ops lifecycle_ops = {
|
||||
.do_init = do_init,
|
||||
.do_start = do_start,
|
||||
.do_stop = do_stop,
|
||||
};
|
||||
|
||||
static struct ble_bond_multi_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.current_slot = BLE_SLOT_MIN,
|
||||
.pending_slot = BLE_SLOT_MIN,
|
||||
};
|
||||
|
||||
BUILD_ASSERT(CONFIG_BT_ID_MAX > IDENTITY_DONGLE,
|
||||
"BT_ID_MAX must include BLE slots and dongle slot");
|
||||
BUILD_ASSERT(CONFIG_BT_MAX_PAIRED >= 4,
|
||||
"BT_MAX_PAIRED must allow three BLE slots and dongle slot");
|
||||
|
||||
static bool is_ble_slot(uint8_t slot)
|
||||
{
|
||||
return (slot >= BLE_SLOT_MIN) && (slot <= BLE_SLOT_MAX);
|
||||
}
|
||||
|
||||
static void display_name_set_default(uint8_t slot)
|
||||
{
|
||||
struct ble_bond_multi_slot_meta *meta = &ctx.slot_meta[slot];
|
||||
const char *name = meta->occupied ? DEFAULT_DISPLAY_NAME_BONDED :
|
||||
DEFAULT_DISPLAY_NAME_EMPTY;
|
||||
|
||||
strncpy(meta->display_name, name, sizeof(meta->display_name));
|
||||
meta->display_name[sizeof(meta->display_name) - 1U] = '\0';
|
||||
}
|
||||
|
||||
static void slot_meta_clear(uint8_t slot)
|
||||
{
|
||||
struct ble_bond_multi_slot_meta *meta = &ctx.slot_meta[slot];
|
||||
|
||||
memset(meta, 0, sizeof(*meta));
|
||||
bt_addr_le_copy(&meta->last_peer_addr, BT_ADDR_LE_ANY);
|
||||
display_name_set_default(slot);
|
||||
}
|
||||
|
||||
static void slot_meta_ensure_name(uint8_t slot)
|
||||
{
|
||||
if (ctx.slot_meta[slot].display_name[0] == '\0') {
|
||||
display_name_set_default(slot);
|
||||
}
|
||||
}
|
||||
|
||||
static void slot_meta_from_storage(uint8_t slot,
|
||||
const struct ble_bond_multi_slot_meta_storage *storage)
|
||||
{
|
||||
struct ble_bond_multi_slot_meta *meta = &ctx.slot_meta[slot];
|
||||
|
||||
meta->occupied = (storage->occupied != 0U);
|
||||
bt_addr_le_copy(&meta->last_peer_addr, &storage->last_peer_addr);
|
||||
memcpy(meta->display_name, storage->display_name, sizeof(meta->display_name));
|
||||
meta->display_name[sizeof(meta->display_name) - 1U] = '\0';
|
||||
slot_meta_ensure_name(slot);
|
||||
}
|
||||
|
||||
static void slot_meta_to_storage(uint8_t slot,
|
||||
struct ble_bond_multi_slot_meta_storage *storage)
|
||||
{
|
||||
const struct ble_bond_multi_slot_meta *meta = &ctx.slot_meta[slot];
|
||||
|
||||
memset(storage, 0, sizeof(*storage));
|
||||
storage->occupied = meta->occupied ? 1U : 0U;
|
||||
bt_addr_le_copy(&storage->last_peer_addr, &meta->last_peer_addr);
|
||||
memcpy(storage->display_name, meta->display_name,
|
||||
sizeof(storage->display_name));
|
||||
}
|
||||
|
||||
static int settings_set(const char *key, size_t len_rd,
|
||||
settings_read_cb read_cb, void *cb_arg)
|
||||
{
|
||||
ssize_t rc;
|
||||
|
||||
if (!strcmp(key, SETTINGS_KEY_CURRENT_SLOT)) {
|
||||
uint8_t stored_slot;
|
||||
|
||||
if (len_rd != sizeof(stored_slot)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
rc = read_cb(cb_arg, &stored_slot, sizeof(stored_slot));
|
||||
if (rc == sizeof(stored_slot) && is_ble_slot(stored_slot)) {
|
||||
ctx.current_slot = stored_slot;
|
||||
ctx.pending_slot = stored_slot;
|
||||
ctx.current_slot_valid = true;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!strncmp(key, SETTINGS_KEY_META_PREFIX,
|
||||
sizeof(SETTINGS_KEY_META_PREFIX) - 1)) {
|
||||
const char *slot_str = key + (sizeof(SETTINGS_KEY_META_PREFIX) - 1);
|
||||
long slot = strtol(slot_str, NULL, 10);
|
||||
struct ble_bond_multi_slot_meta_storage storage;
|
||||
|
||||
if ((slot < BLE_SLOT_MIN) || (slot > IDENTITY_DONGLE) ||
|
||||
(len_rd != sizeof(storage))) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
rc = read_cb(cb_arg, &storage, sizeof(storage));
|
||||
if (rc == sizeof(storage)) {
|
||||
slot_meta_from_storage((uint8_t)slot, &storage);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SETTINGS_STATIC_HANDLER_DEFINE(ble_bond_multi, "ble_multi", NULL, settings_set,
|
||||
NULL, NULL);
|
||||
|
||||
static int settings_save_current_slot(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = settings_save_one("ble_multi/" SETTINGS_KEY_CURRENT_SLOT,
|
||||
&ctx.current_slot,
|
||||
sizeof(ctx.current_slot));
|
||||
if (err) {
|
||||
LOG_ERR("Save current slot failed (%d)", err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int settings_save_slot_meta(uint8_t slot)
|
||||
{
|
||||
int err;
|
||||
char key[24];
|
||||
struct ble_bond_multi_slot_meta_storage storage;
|
||||
|
||||
slot_meta_to_storage(slot, &storage);
|
||||
snprintk(key, sizeof(key), "ble_multi/" SETTINGS_KEY_META_PREFIX "%u", slot);
|
||||
err = settings_save_one(key, &storage, sizeof(storage));
|
||||
if (err) {
|
||||
LOG_ERR("Save slot %u meta failed (%d)", slot, err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static void active_conn_clear(void)
|
||||
{
|
||||
ctx.active_conn = NULL;
|
||||
}
|
||||
|
||||
static void bond_addr_get_cb(const struct bt_bond_info *info, void *user_data)
|
||||
{
|
||||
bt_addr_le_t *addr = user_data;
|
||||
|
||||
if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) {
|
||||
bt_addr_le_copy(addr, &info->addr);
|
||||
}
|
||||
}
|
||||
|
||||
static void publish_state(enum ble_bond_multi_op op)
|
||||
{
|
||||
struct ble_bond_multi_event *event = new_ble_bond_multi_event();
|
||||
uint8_t occ = 0U;
|
||||
|
||||
event->current_slot = ctx.current_slot;
|
||||
event->active_identity_id = ctx.current_slot;
|
||||
event->op = op;
|
||||
|
||||
for (uint8_t slot = BLE_SLOT_MIN; slot <= BLE_SLOT_MAX; slot++) {
|
||||
if (ctx.slot_meta[slot].occupied) {
|
||||
occ |= BIT(slot - BLE_SLOT_MIN);
|
||||
}
|
||||
|
||||
event->slots[slot - BLE_SLOT_MIN] = ctx.slot_meta[slot];
|
||||
}
|
||||
|
||||
event->slot_occupied_bitmap = occ;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static int identity_ensure_exists(uint8_t identity)
|
||||
{
|
||||
size_t count = CONFIG_BT_ID_MAX;
|
||||
bt_addr_le_t addrs[CONFIG_BT_ID_MAX];
|
||||
|
||||
(void)bt_id_get(addrs, &count);
|
||||
|
||||
while (count <= identity) {
|
||||
int id = bt_id_create(NULL, NULL);
|
||||
|
||||
if (id < 0) {
|
||||
LOG_ERR("bt_id_create failed (%d)", id);
|
||||
return id;
|
||||
}
|
||||
|
||||
count++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool slot_has_bond(uint8_t slot)
|
||||
{
|
||||
size_t cnt = 0;
|
||||
|
||||
bt_foreach_bond(slot, slot_bond_cnt_cb, &cnt);
|
||||
|
||||
return cnt > 0U;
|
||||
}
|
||||
|
||||
static void slot_bond_cnt_cb(const struct bt_bond_info *info, void *user_data)
|
||||
{
|
||||
size_t *count = user_data;
|
||||
|
||||
ARG_UNUSED(info);
|
||||
(*count)++;
|
||||
}
|
||||
|
||||
static void slot_update_from_bonds(uint8_t slot)
|
||||
{
|
||||
struct ble_bond_multi_slot_meta *meta = &ctx.slot_meta[slot];
|
||||
|
||||
meta->occupied = slot_has_bond(slot);
|
||||
if (!meta->occupied) {
|
||||
bt_addr_le_copy(&meta->last_peer_addr, BT_ADDR_LE_ANY);
|
||||
} else if (!bt_addr_le_cmp(&meta->last_peer_addr, BT_ADDR_LE_ANY)) {
|
||||
bt_foreach_bond(slot, bond_addr_get_cb, &meta->last_peer_addr);
|
||||
}
|
||||
slot_meta_ensure_name(slot);
|
||||
}
|
||||
|
||||
static void all_slots_refresh_from_bonds(void)
|
||||
{
|
||||
for (uint8_t slot = BLE_SLOT_MIN; slot <= IDENTITY_DONGLE; slot++) {
|
||||
slot_update_from_bonds(slot);
|
||||
}
|
||||
}
|
||||
|
||||
static void submit_selected_event(uint8_t identity)
|
||||
{
|
||||
struct ble_peer_operation_event *event = new_ble_peer_operation_event();
|
||||
|
||||
event->op = PEER_OPERATION_SELECTED;
|
||||
event->bt_app_id = identity;
|
||||
event->bt_stack_id = identity;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_erased_event(uint8_t identity)
|
||||
{
|
||||
struct ble_peer_operation_event *event = new_ble_peer_operation_event();
|
||||
|
||||
event->op = PEER_OPERATION_ERASED;
|
||||
event->bt_app_id = identity;
|
||||
event->bt_stack_id = identity;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static int switch_slot(uint8_t slot)
|
||||
{
|
||||
if (!is_ble_slot(slot) || (slot == ctx.current_slot)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ctx.pending_slot = slot;
|
||||
ctx.current_slot = slot;
|
||||
submit_selected_event(slot);
|
||||
return settings_save_current_slot();
|
||||
}
|
||||
|
||||
static int erase_slot(uint8_t slot)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = bt_unpair(slot, BT_ADDR_LE_ANY);
|
||||
if (err) {
|
||||
LOG_ERR("bt_unpair slot %u failed (%d)", slot, err);
|
||||
return err;
|
||||
}
|
||||
|
||||
slot_meta_clear(slot);
|
||||
err = settings_save_slot_meta(slot);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
submit_erased_event(slot);
|
||||
submit_selected_event(slot);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_init(void)
|
||||
{
|
||||
for (uint8_t slot = BLE_SLOT_MIN; slot <= IDENTITY_DONGLE; slot++) {
|
||||
slot_meta_clear(slot);
|
||||
}
|
||||
|
||||
if (!ctx.current_slot_valid) {
|
||||
ctx.current_slot = BLE_SLOT_MIN;
|
||||
ctx.pending_slot = BLE_SLOT_MIN;
|
||||
}
|
||||
|
||||
active_conn_clear();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
for (uint8_t identity = BLE_SLOT_MIN; identity <= IDENTITY_DONGLE;
|
||||
identity++) {
|
||||
err = identity_ensure_exists(identity);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
ctx.identities_ready = true;
|
||||
|
||||
all_slots_refresh_from_bonds();
|
||||
for (uint8_t slot = BLE_SLOT_MIN; slot <= IDENTITY_DONGLE; slot++) {
|
||||
(void)settings_save_slot_meta(slot);
|
||||
}
|
||||
|
||||
submit_selected_event(ctx.current_slot);
|
||||
publish_state(BLE_BOND_MULTI_OP_REFRESH);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_stop(void)
|
||||
{
|
||||
active_conn_clear();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
{
|
||||
switch (event->state) {
|
||||
case PEER_STATE_CONNECTED:
|
||||
ctx.active_conn = event->id;
|
||||
return false;
|
||||
|
||||
case PEER_STATE_SECURED:
|
||||
if (ctx.active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx.slot_meta[ctx.current_slot].occupied = true;
|
||||
bt_addr_le_copy(&ctx.slot_meta[ctx.current_slot].last_peer_addr,
|
||||
bt_conn_get_dst(ctx.active_conn));
|
||||
if ((ctx.slot_meta[ctx.current_slot].display_name[0] == '\0') ||
|
||||
!strcmp(ctx.slot_meta[ctx.current_slot].display_name,
|
||||
DEFAULT_DISPLAY_NAME_EMPTY)) {
|
||||
display_name_set_default(ctx.current_slot);
|
||||
}
|
||||
|
||||
(void)settings_save_slot_meta(ctx.current_slot);
|
||||
publish_state(BLE_BOND_MULTI_OP_REFRESH);
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
case PEER_STATE_CONN_FAILED:
|
||||
if (ctx.active_conn == event->id) {
|
||||
active_conn_clear();
|
||||
}
|
||||
return false;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(settings_loader), MODULE_STATE_READY)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_ble_peer_event(aeh)) {
|
||||
return handle_ble_peer_event(cast_ble_peer_event(aeh));
|
||||
}
|
||||
|
||||
if (is_ble_bond_multi_event(aeh)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int ble_bond_multi_select_slot(uint8_t slot)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
if (!ctx.identities_ready) {
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
err = switch_slot(slot);
|
||||
if (!err) {
|
||||
publish_state(BLE_BOND_MULTI_OP_SWITCH);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int ble_bond_multi_erase_current_slot(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
if (!ctx.identities_ready) {
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
err = erase_slot(ctx.current_slot);
|
||||
if (!err) {
|
||||
publish_state(BLE_BOND_MULTI_OP_ERASE);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
const struct ble_bond_multi_slot_meta *ble_bond_multi_get_slot_meta(uint8_t slot)
|
||||
{
|
||||
if (!is_ble_slot(slot)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &ctx.slot_meta[slot];
|
||||
}
|
||||
|
||||
uint8_t ble_bond_multi_get_current_slot(void)
|
||||
{
|
||||
return ctx.current_slot;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event);
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "bat_state_event.h"
|
||||
#include "ble_bond_multi_event.h"
|
||||
#include "datetime_event.h"
|
||||
#include "hid_led_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
@@ -213,6 +214,11 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_ble_bond_multi_event(aeh)) {
|
||||
refresh_ui();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_hid_led_event(aeh)) {
|
||||
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||
|
||||
@@ -316,6 +322,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, bat_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, ble_bond_multi_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, datetime_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
|
||||
38
src/events/ble_bond_multi_event.c
Normal file
38
src/events/ble_bond_multi_event.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "ble_bond_multi_event.h"
|
||||
|
||||
static void log_ble_bond_multi_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_bond_multi_event *event =
|
||||
cast_ble_bond_multi_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "slot:%u identity:%u op:%u occ:0x%02x",
|
||||
event->current_slot, event->active_identity_id,
|
||||
event->op, event->slot_occupied_bitmap);
|
||||
}
|
||||
|
||||
static void profile_ble_bond_multi_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_bond_multi_event *event =
|
||||
cast_ble_bond_multi_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->current_slot);
|
||||
nrf_profiler_log_encode_uint8(buf, event->active_identity_id);
|
||||
nrf_profiler_log_encode_uint8(buf, event->op);
|
||||
nrf_profiler_log_encode_uint8(buf, event->slot_occupied_bitmap);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(ble_bond_multi_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("current_slot", "active_identity_id", "op",
|
||||
"slot_occupied_bitmap"),
|
||||
profile_ble_bond_multi_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(ble_bond_multi_event,
|
||||
log_ble_bond_multi_event,
|
||||
&ble_bond_multi_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -94,7 +94,6 @@ struct keyboard_core_module_ctx {
|
||||
uint8_t function_usage_mask[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
enum keyboard_protocol_mode transport_protocol_modes[HID_TRANSPORT_COUNT];
|
||||
enum hid_transport_policy current_transport;
|
||||
bool mode_valid;
|
||||
bool settings_active;
|
||||
};
|
||||
|
||||
@@ -146,8 +145,7 @@ static enum keyboard_protocol_mode active_protocol_mode_get(void)
|
||||
{
|
||||
enum hid_transport transport;
|
||||
|
||||
if (ctx.mode_valid &&
|
||||
policy_to_transport(ctx.current_transport, &transport)) {
|
||||
if (policy_to_transport(ctx.current_transport, &transport)) {
|
||||
return ctx.transport_protocol_modes[transport];
|
||||
}
|
||||
|
||||
@@ -358,7 +356,7 @@ static void submit_consumer_fifo_frame(uint16_t usage_id)
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
enum mode_switch_mode mode;
|
||||
|
||||
if (!module_lifecycle_is_running(&ctx.lc) || !ctx.mode_valid ||
|
||||
if (!module_lifecycle_is_running(&ctx.lc) ||
|
||||
ctx.settings_active ||
|
||||
!transport_policy_to_mode(ctx.current_transport, &mode) ||
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||
@@ -406,8 +404,7 @@ static void emit_keys_report(bool force)
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
enum mode_switch_mode mode;
|
||||
|
||||
if (!ctx.mode_valid ||
|
||||
!transport_policy_to_mode(ctx.current_transport, &mode)) {
|
||||
if (!transport_policy_to_mode(ctx.current_transport, &mode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -445,8 +442,7 @@ static void emit_consumer_report(bool force)
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
enum mode_switch_mode mode;
|
||||
|
||||
if (!ctx.mode_valid ||
|
||||
!transport_policy_to_mode(ctx.current_transport, &mode) ||
|
||||
if (!transport_policy_to_mode(ctx.current_transport, &mode) ||
|
||||
ctx.settings_active ||
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||
return;
|
||||
@@ -522,8 +518,8 @@ static int do_init(void)
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
function_usage_mask_clear();
|
||||
ctx.mode_valid = false;
|
||||
ctx.settings_active = false;
|
||||
ctx.current_transport = HID_TRANSPORT_POLICY_USB;
|
||||
ctx.transport_protocol_modes[HID_TRANSPORT_USB] =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
ctx.transport_protocol_modes[HID_TRANSPORT_BLE] =
|
||||
@@ -547,14 +543,13 @@ static int do_stop(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ctx.mode_valid) {
|
||||
if (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) {
|
||||
emit_release_reports(ctx.current_transport);
|
||||
}
|
||||
emit_function_state_event();
|
||||
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
ctx.mode_valid = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -625,8 +620,8 @@ static bool handle_transport_policy_event(
|
||||
return false;
|
||||
}
|
||||
|
||||
transport_changed =
|
||||
ctx.mode_valid && (ctx.current_transport != event->hid_transport);
|
||||
transport_changed = (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) &&
|
||||
(ctx.current_transport != event->hid_transport);
|
||||
if (transport_changed) {
|
||||
emit_release_reports(ctx.current_transport);
|
||||
emit_function_state_event();
|
||||
@@ -635,9 +630,8 @@ static bool handle_transport_policy_event(
|
||||
}
|
||||
|
||||
ctx.current_transport = event->hid_transport;
|
||||
ctx.mode_valid = (ctx.current_transport != HID_TRANSPORT_POLICY_NONE);
|
||||
|
||||
if (ctx.mode_valid) {
|
||||
if (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) {
|
||||
emit_all_reports(true);
|
||||
}
|
||||
|
||||
@@ -646,7 +640,8 @@ static bool handle_transport_policy_event(
|
||||
|
||||
static bool handle_encoder_event(const struct encoder_event *event)
|
||||
{
|
||||
if (!module_lifecycle_is_running(&ctx.lc) || !ctx.mode_valid) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc) ||
|
||||
(ctx.current_transport == HID_TRANSPORT_POLICY_NONE)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -701,7 +696,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
ctx.transport_protocol_modes[event->transport] =
|
||||
event->protocol_mode;
|
||||
|
||||
if (module_lifecycle_is_running(&ctx.lc) && ctx.mode_valid &&
|
||||
if (module_lifecycle_is_running(&ctx.lc) &&
|
||||
policy_to_transport(ctx.current_transport, &active_transport) &&
|
||||
(active_transport == event->transport)) {
|
||||
reports_cache_invalidate();
|
||||
@@ -731,7 +726,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
ctx.settings_active = event->active;
|
||||
if (ctx.settings_active) {
|
||||
if (ctx.mode_valid) {
|
||||
if (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) {
|
||||
emit_release_reports(ctx.current_transport);
|
||||
}
|
||||
emit_function_state_event();
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "ble_bond_multi_event.h"
|
||||
#include "encoder_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
#include "settings_mode_event.h"
|
||||
@@ -157,6 +159,22 @@ static bool handle_theme_rgb_update_event(
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_ble_bond_multi_event(
|
||||
const struct ble_bond_multi_event *event)
|
||||
{
|
||||
ui_settings_ble_set_current_slot(event->current_slot);
|
||||
|
||||
for (uint8_t i = 0U; i < ARRAY_SIZE(event->slots); i++) {
|
||||
ui_settings_ble_set_slot_meta(i + 1U, &event->slots[i]);
|
||||
}
|
||||
|
||||
if (ctx.active) {
|
||||
ui_settings_controller_refresh(false);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_click_event(aeh)) {
|
||||
@@ -172,6 +190,11 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
cast_theme_rgb_update_event(aeh));
|
||||
}
|
||||
|
||||
if (is_ble_bond_multi_event(aeh)) {
|
||||
return handle_ble_bond_multi_event(
|
||||
cast_ble_bond_multi_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
@@ -206,5 +229,6 @@ APP_EVENT_SUBSCRIBE(MODULE, click_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, encoder_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, theme_rgb_update_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, ble_bond_multi_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
@@ -5,25 +5,29 @@
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "ble_bond_multi_event.h"
|
||||
#include "ui/ui_settings_controller.h"
|
||||
#include "settings_view_event.h"
|
||||
#include "ui_settings_pages.h"
|
||||
|
||||
#define BLE_SLOT_COUNT 3U
|
||||
|
||||
int ble_bond_multi_select_slot(uint8_t slot);
|
||||
int ble_bond_multi_erase_current_slot(void);
|
||||
|
||||
struct controller_ctx {
|
||||
struct ui_settings_page *current;
|
||||
bool active;
|
||||
uint8_t active_ble_slot;
|
||||
char ble_labels[BLE_SLOT_COUNT][16];
|
||||
char ble_labels[BLE_SLOT_COUNT][BLE_BOND_MULTI_DISPLAY_NAME_MAX_LEN];
|
||||
struct theme_rgb theme;
|
||||
};
|
||||
|
||||
static struct controller_ctx ctx = {
|
||||
.active_ble_slot = 0U,
|
||||
.ble_labels = {
|
||||
"Host A",
|
||||
"Host B",
|
||||
"Empty",
|
||||
"Empty",
|
||||
"Empty",
|
||||
},
|
||||
.theme = {
|
||||
@@ -164,16 +168,13 @@ const char *ui_settings_ble_current_label(void)
|
||||
void ui_settings_ble_select_slot(uint8_t slot)
|
||||
{
|
||||
if (slot < BLE_SLOT_COUNT) {
|
||||
ctx.active_ble_slot = slot;
|
||||
(void)ble_bond_multi_select_slot(slot + 1U);
|
||||
}
|
||||
}
|
||||
|
||||
void ui_settings_ble_erase_current(void)
|
||||
{
|
||||
strncpy(ctx.ble_labels[ctx.active_ble_slot], "Empty",
|
||||
sizeof(ctx.ble_labels[ctx.active_ble_slot]));
|
||||
ctx.ble_labels[ctx.active_ble_slot]
|
||||
[sizeof(ctx.ble_labels[ctx.active_ble_slot]) - 1U] = '\0';
|
||||
(void)ble_bond_multi_erase_current_slot();
|
||||
}
|
||||
|
||||
const char *ui_settings_ble_slot_label(uint8_t slot)
|
||||
@@ -185,6 +186,26 @@ const char *ui_settings_ble_slot_label(uint8_t slot)
|
||||
return ctx.ble_labels[slot];
|
||||
}
|
||||
|
||||
void ui_settings_ble_set_current_slot(uint8_t slot)
|
||||
{
|
||||
if ((slot >= 1U) && (slot <= BLE_SLOT_COUNT)) {
|
||||
ctx.active_ble_slot = slot - 1U;
|
||||
}
|
||||
}
|
||||
|
||||
void ui_settings_ble_set_slot_meta(uint8_t slot,
|
||||
const struct ble_bond_multi_slot_meta *meta)
|
||||
{
|
||||
if ((meta == NULL) || (slot < 1U) || (slot > BLE_SLOT_COUNT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
strncpy(ctx.ble_labels[slot - 1U],
|
||||
meta->display_name[0] ? meta->display_name : "Empty",
|
||||
sizeof(ctx.ble_labels[slot - 1U]));
|
||||
ctx.ble_labels[slot - 1U][sizeof(ctx.ble_labels[slot - 1U]) - 1U] = '\0';
|
||||
}
|
||||
|
||||
void ui_settings_theme_set_current(struct theme_rgb theme)
|
||||
{
|
||||
ctx.theme = theme;
|
||||
|
||||
Reference in New Issue
Block a user