Compare commits
5 Commits
3971d7c4b2
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 88ee4da089 | |||
| adaad6cc34 | |||
| d27df7f3bf | |||
| 07fe70becd | |||
| 54c5f76c84 |
@@ -22,6 +22,7 @@ zephyr_nanopb_sources(app
|
|||||||
target_sources(app PRIVATE
|
target_sources(app PRIVATE
|
||||||
src/main.c
|
src/main.c
|
||||||
src/battery_module.c
|
src/battery_module.c
|
||||||
|
src/ble_bond_multi_module.c
|
||||||
src/ble_adv_uuid16.c
|
src/ble_adv_uuid16.c
|
||||||
src/ble_bas_module.c
|
src/ble_bas_module.c
|
||||||
src/ble_hid_module.c
|
src/ble_hid_module.c
|
||||||
@@ -36,6 +37,7 @@ target_sources(app PRIVATE
|
|||||||
src/mode_policy_module.c
|
src/mode_policy_module.c
|
||||||
src/time_sync_module.c
|
src/time_sync_module.c
|
||||||
src/settings_module.c
|
src/settings_module.c
|
||||||
|
src/swift_pair_module.c
|
||||||
src/ui/ui_main.c
|
src/ui/ui_main.c
|
||||||
src/ui/ui_settings.c
|
src/ui/ui_settings.c
|
||||||
src/ui/ui_settings_controller.c
|
src/ui/ui_settings_controller.c
|
||||||
@@ -48,6 +50,7 @@ target_sources(app PRIVATE
|
|||||||
src/usb_hid_consumer_module.c
|
src/usb_hid_consumer_module.c
|
||||||
src/usb_hid_keyboard_module.c
|
src/usb_hid_keyboard_module.c
|
||||||
src/events/bat_state_event.c
|
src/events/bat_state_event.c
|
||||||
|
src/events/ble_bond_multi_event.c
|
||||||
src/events/datetime_event.c
|
src/events/datetime_event.c
|
||||||
src/events/encoder_event.c
|
src/events/encoder_event.c
|
||||||
src/events/function_bitmap_state_event.c
|
src/events/function_bitmap_state_event.c
|
||||||
|
|||||||
13
Kconfig
13
Kconfig
@@ -2,6 +2,19 @@ mainmenu "blinky"
|
|||||||
|
|
||||||
source "Kconfig.zephyr"
|
source "Kconfig.zephyr"
|
||||||
|
|
||||||
|
menu "Application"
|
||||||
|
|
||||||
|
config BLINKY_BLE_BOND_MULTI_INTERNAL
|
||||||
|
bool
|
||||||
|
depends on BT_BONDABLE
|
||||||
|
depends on BT_SETTINGS
|
||||||
|
depends on CAF_SETTINGS_LOADER
|
||||||
|
depends on CAF_BLE_COMMON_EVENTS
|
||||||
|
select CAF_BLE_BOND_SUPPORTED
|
||||||
|
default y
|
||||||
|
|
||||||
|
endmenu
|
||||||
|
|
||||||
menu "Application Drivers"
|
menu "Application Drivers"
|
||||||
rsource "drivers/Kconfig"
|
rsource "drivers/Kconfig"
|
||||||
endmenu
|
endmenu
|
||||||
|
|||||||
582
docs/ble_multi_slot_design.md
Normal file
582
docs/ble_multi_slot_design.md
Normal file
@@ -0,0 +1,582 @@
|
|||||||
|
# Blinky 多槽蓝牙设计方案
|
||||||
|
|
||||||
|
## 1. 目标与约束
|
||||||
|
|
||||||
|
为 `C:\projects\blinky` 增加多槽蓝牙能力,当前阶段只实现 BLE,多 dongle 方案先预留结构,不实现完整业务流程。
|
||||||
|
|
||||||
|
本方案已经按当前讨论结论收敛为以下硬约束:
|
||||||
|
|
||||||
|
- 支持 3 个 BLE 槽位 `Slot 1~3`
|
||||||
|
- 每个槽位固定对应一个 Bluetooth local identity
|
||||||
|
- 槽位切换本质上是切换 `bt_identity_id`
|
||||||
|
- 不引入 `app_slot_id`
|
||||||
|
- 不使用临时 identity
|
||||||
|
- 不做 erase advertising
|
||||||
|
- 不做擦除确认
|
||||||
|
- 不做擦除回滚
|
||||||
|
- 擦除当前槽位时直接删除该 identity 上的 bond
|
||||||
|
- settings 只持久化 `current_slot` 和各槽位 `meta`
|
||||||
|
- `slot_meta` 中必须保留 `display_name`,用于 UI 显示
|
||||||
|
- `24G / dongle` 采用方案 A,但本阶段只预留专用槽位
|
||||||
|
|
||||||
|
## 2. 本地参考基线
|
||||||
|
|
||||||
|
本方案主要参考本地 NCS 3.2.3 中以下实现和文档:
|
||||||
|
|
||||||
|
- `c:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\ble_bond.rst`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\applications\nrf_desktop\src\modules\ble_bond.c`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\applications\nrf_desktop\src\modules\Kconfig.ble_bond`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\include\caf\events\ble_common_event.h`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\subsys\caf\modules\ble_adv.c`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\subsys\caf\modules\Kconfig.ble_state`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\doc\nrf\libraries\caf\ble_state.rst`
|
||||||
|
|
||||||
|
参考方式如下:
|
||||||
|
|
||||||
|
- 继承 `nrf_desktop` 的总体方向:应用自定义 bond 管理模块,而不是 CAF 默认 `ble_bond`
|
||||||
|
- 继续通过 `ble_peer_operation_event` 驱动 CAF `ble_adv`
|
||||||
|
- 保留 dongle 专用 identity 的规划方式
|
||||||
|
|
||||||
|
明确不采用 `nrf_desktop` 的部分:
|
||||||
|
|
||||||
|
- 不使用 `app_slot_id <-> bt_identity_id` 的二级映射
|
||||||
|
- 不使用临时 identity
|
||||||
|
- 不采用 `ERASE_ADV`
|
||||||
|
- 不采用“新配对成功后再替换旧槽位”的回滚保护机制
|
||||||
|
|
||||||
|
## 3. 对现有项目的判断
|
||||||
|
|
||||||
|
`blinky` 当前已经具备多槽蓝牙需要的大部分基础能力:
|
||||||
|
|
||||||
|
- 已启用 `CONFIG_CAF_BLE_STATE`
|
||||||
|
- 已启用 `CONFIG_CAF_BLE_ADV`
|
||||||
|
- 已启用 `CONFIG_BT_SETTINGS`
|
||||||
|
- HID 和 NUS 都已基于 `ble_peer_event` 跟踪当前连接
|
||||||
|
- UI 已经预留 3 个蓝牙槽位和擦除当前槽位入口
|
||||||
|
- 模式策略层已经区分:
|
||||||
|
- `MODE_SWITCH_BLE -> BLE_PROFILE_POLICY_GENERAL`
|
||||||
|
- `MODE_SWITCH_24G -> BLE_PROFILE_POLICY_DONGLE`
|
||||||
|
|
||||||
|
相关文件:
|
||||||
|
|
||||||
|
- `C:\projects\blinky\src\ble_hid_module.c`
|
||||||
|
- `C:\projects\blinky\src\ble_nus_module.c`
|
||||||
|
- `C:\projects\blinky\src\ui\ui_settings_ble.c`
|
||||||
|
- `C:\projects\blinky\src\ui\ui_settings_controller.c`
|
||||||
|
- `C:\projects\blinky\src\mode_policy_module.c`
|
||||||
|
- `C:\projects\blinky\prj.conf`
|
||||||
|
|
||||||
|
当前限制也很明确:
|
||||||
|
|
||||||
|
- `CONFIG_BT_MAX_PAIRED=1`,全设备只能保存 1 个 bond
|
||||||
|
- 当前是单槽逻辑
|
||||||
|
- 还在使用 CAF 默认 `CONFIG_CAF_BLE_BOND=y`
|
||||||
|
- UI 的槽位状态仍是内存假数据
|
||||||
|
|
||||||
|
## 4. 配置语义结论
|
||||||
|
|
||||||
|
基于本地 Kconfig 和 CAF 文档,关键配置的含义如下:
|
||||||
|
|
||||||
|
- `CONFIG_BT_MAX_PAIRED`
|
||||||
|
说明:整个设备允许保存的 bond 总数
|
||||||
|
- `CONFIG_CAF_BLE_STATE_MAX_LOCAL_ID_BONDS`
|
||||||
|
说明:每个 Bluetooth local identity 允许的 bond 数上限
|
||||||
|
- `CONFIG_BT_ID_MAX`
|
||||||
|
说明:设备允许使用的 local identity 总数
|
||||||
|
|
||||||
|
如果目标是:
|
||||||
|
|
||||||
|
- 3 个 BLE 槽位
|
||||||
|
- 每槽 1 个 bond
|
||||||
|
- 预留 1 个 dongle 槽位
|
||||||
|
|
||||||
|
那么推荐配置应为:
|
||||||
|
|
||||||
|
```conf
|
||||||
|
CONFIG_BT_MAX_CONN=1
|
||||||
|
CONFIG_BT_MAX_PAIRED=4
|
||||||
|
CONFIG_CAF_BLE_STATE_MAX_LOCAL_ID_BONDS=1
|
||||||
|
CONFIG_BT_ID_MAX=5
|
||||||
|
```
|
||||||
|
|
||||||
|
语义如下:
|
||||||
|
|
||||||
|
- `BT_MAX_CONN=1`
|
||||||
|
说明:同时只保持 1 条 BLE 连接
|
||||||
|
- `BT_MAX_PAIRED=4`
|
||||||
|
说明:全设备最多保存 4 个 bond
|
||||||
|
- `CAF_BLE_STATE_MAX_LOCAL_ID_BONDS=1`
|
||||||
|
说明:每个 identity 只允许绑定 1 个主机
|
||||||
|
- `BT_ID_MAX=5`
|
||||||
|
说明:可用 identity 为 `0..4`
|
||||||
|
|
||||||
|
配合本方案的 identity 规划:
|
||||||
|
|
||||||
|
- `0`:默认 identity,不参与槽位
|
||||||
|
- `1`:Slot 1
|
||||||
|
- `2`:Slot 2
|
||||||
|
- `3`:Slot 3
|
||||||
|
- `4`:Dongle Slot 预留
|
||||||
|
|
||||||
|
## 5. 总体架构
|
||||||
|
|
||||||
|
## 5.1 设计结论
|
||||||
|
|
||||||
|
建议采用:
|
||||||
|
|
||||||
|
- 保留 `CAF_BLE_STATE`
|
||||||
|
- 保留 `CAF_BLE_ADV`
|
||||||
|
- 关闭 CAF 默认 `BLE_BOND`
|
||||||
|
- 新增应用自定义模块 `ble_bond_multi_module`
|
||||||
|
|
||||||
|
即:
|
||||||
|
|
||||||
|
- `CONFIG_CAF_BLE_BOND=n`
|
||||||
|
- 新增 `CONFIG_BLINKY_BLE_BOND_MULTI`
|
||||||
|
- 自定义模块 `select CAF_BLE_BOND_SUPPORTED`
|
||||||
|
- 由自定义模块向 CAF 提交 `ble_peer_operation_event`
|
||||||
|
|
||||||
|
原因:
|
||||||
|
|
||||||
|
- CAF 默认 `ble_bond` 不适合固定多 identity 槽位管理
|
||||||
|
- 你的需求已经明确不需要 `nrf_desktop` 那种复杂映射和回滚流程
|
||||||
|
- 自定义模块更适合和现有 UI、模式策略、settings 同步
|
||||||
|
|
||||||
|
## 5.2 核心模型
|
||||||
|
|
||||||
|
本方案直接把“槽位”和“identity”绑定:
|
||||||
|
|
||||||
|
- `Slot 1` = `bt_identity_id 1`
|
||||||
|
- `Slot 2` = `bt_identity_id 2`
|
||||||
|
- `Slot 3` = `bt_identity_id 3`
|
||||||
|
- `Dongle Slot` = `bt_identity_id 4`
|
||||||
|
|
||||||
|
所以:
|
||||||
|
|
||||||
|
- `current_slot` 实际上就是当前使用的 `bt_identity_id`
|
||||||
|
- UI 显示“Slot 1/2/3”时,后端直接映射到固定 identity
|
||||||
|
- 不再存在可变的 `id_lut`
|
||||||
|
|
||||||
|
这个设计的优点:
|
||||||
|
|
||||||
|
- 逻辑简单
|
||||||
|
- settings 简单
|
||||||
|
- 调试简单
|
||||||
|
- 后续接入 dongle 不需要迁移已有 BLE 槽位
|
||||||
|
|
||||||
|
## 6. identity 规划
|
||||||
|
|
||||||
|
建议固定如下:
|
||||||
|
|
||||||
|
| identity | 角色 | 当前阶段 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `0` | 默认 identity | 不使用 |
|
||||||
|
| `1` | BLE Slot 1 | 使用 |
|
||||||
|
| `2` | BLE Slot 2 | 使用 |
|
||||||
|
| `3` | BLE Slot 3 | 使用 |
|
||||||
|
| `4` | Dongle Slot | 预留 |
|
||||||
|
|
||||||
|
设计原则:
|
||||||
|
|
||||||
|
- 不使用 `BT_ID_DEFAULT`
|
||||||
|
原因:默认 identity 的 reset/unpair 行为不适合作为固定槽位基线
|
||||||
|
- 3 个 BLE 槽位 identity 固定不变
|
||||||
|
- `identity 4` 只给 `BLE_PROFILE_POLICY_DONGLE`
|
||||||
|
|
||||||
|
## 7. 模块设计
|
||||||
|
|
||||||
|
## 7.1 新增模块和文件
|
||||||
|
|
||||||
|
建议新增:
|
||||||
|
|
||||||
|
- `src/ble_bond_multi_module.c`
|
||||||
|
- `src/events/ble_bond_multi_event.c`
|
||||||
|
- `inc/events/ble_bond_multi_event.h`
|
||||||
|
|
||||||
|
不建议新增:
|
||||||
|
|
||||||
|
- `inc/ble_bond_multi.h`
|
||||||
|
|
||||||
|
原因:
|
||||||
|
|
||||||
|
- 当前设计是事件驱动模块
|
||||||
|
- 没有必要向外暴露一组直接调用 API
|
||||||
|
- 共享内容更适合放在事件头文件里
|
||||||
|
|
||||||
|
只有在后续出现明确的跨模块直接调用需求时,才需要再补 `inc/ble_bond_multi.h`
|
||||||
|
|
||||||
|
## 7.2 模块职责边界
|
||||||
|
|
||||||
|
### `ble_bond_multi_module`
|
||||||
|
|
||||||
|
负责:
|
||||||
|
|
||||||
|
- 管理当前活动 `bt_identity_id`
|
||||||
|
- 启动时恢复 `current_slot`
|
||||||
|
- 维护各 identity 槽位的 `slot_meta`
|
||||||
|
- 处理“切换当前槽位”
|
||||||
|
- 处理“直接擦除当前槽位”
|
||||||
|
- 监听连接、加密、断链事件,更新 `slot_meta`
|
||||||
|
- 向 CAF 提交 `ble_peer_operation_event`
|
||||||
|
- 向 UI / 显示层广播槽位状态
|
||||||
|
|
||||||
|
不负责:
|
||||||
|
|
||||||
|
- 发 HID/NUS 数据
|
||||||
|
- 改 GATT 服务
|
||||||
|
- 绘制 UI
|
||||||
|
|
||||||
|
### `ble_hid_module` / `ble_nus_module`
|
||||||
|
|
||||||
|
保持现有思路,不理解“多槽逻辑”,只跟随当前活动连接:
|
||||||
|
|
||||||
|
- 连接上谁,就服务谁
|
||||||
|
- 切槽或擦除造成断链时,内部复位连接状态
|
||||||
|
|
||||||
|
### `mode_policy_module`
|
||||||
|
|
||||||
|
负责决定当前是:
|
||||||
|
|
||||||
|
- `BLE_PROFILE_POLICY_GENERAL`
|
||||||
|
- `BLE_PROFILE_POLICY_DONGLE`
|
||||||
|
|
||||||
|
但不负责槽位 bond 细节。
|
||||||
|
|
||||||
|
## 8. 状态机设计
|
||||||
|
|
||||||
|
第一阶段建议极简化,只保留:
|
||||||
|
|
||||||
|
- `DISABLED`
|
||||||
|
- `IDLE`
|
||||||
|
- `SWITCHING`
|
||||||
|
- `ERASING`
|
||||||
|
|
||||||
|
明确不引入:
|
||||||
|
|
||||||
|
- `SELECT_SLOT`
|
||||||
|
- `ERASE_PENDING`
|
||||||
|
- `ERASE_ADV`
|
||||||
|
- `TEMP_IDENTITY`
|
||||||
|
- `ROLLBACK`
|
||||||
|
|
||||||
|
## 8.1 切槽流程
|
||||||
|
|
||||||
|
1. 用户在 UI 选择 `Slot N`
|
||||||
|
2. 模块判断:
|
||||||
|
- 如果 `N == current_slot`,直接返回
|
||||||
|
- 否则进入 `SWITCHING`
|
||||||
|
3. 提交 `PEER_OPERATION_SELECTED`
|
||||||
|
4. CAF `ble_adv` 切换当前 advertising identity
|
||||||
|
5. 当前连接断开
|
||||||
|
6. 设备以新 identity 重新广播
|
||||||
|
7. 目标主机重新连接
|
||||||
|
8. 保存新的 `current_slot`
|
||||||
|
9. 返回 `IDLE`
|
||||||
|
|
||||||
|
这里采用“直接切换”,不做预选确认。
|
||||||
|
|
||||||
|
## 8.2 擦除当前槽位流程
|
||||||
|
|
||||||
|
1. 用户选择“Erase Bond”
|
||||||
|
2. 模块进入 `ERASING`
|
||||||
|
3. 对当前 `bt_identity_id` 执行:
|
||||||
|
- `bt_unpair(identity, BT_ADDR_LE_ANY)`
|
||||||
|
4. 清除该槽位 `slot_meta`
|
||||||
|
5. 重新以当前 identity 进入可配对广播
|
||||||
|
6. 返回 `IDLE`
|
||||||
|
|
||||||
|
这里采用产品级简化语义:
|
||||||
|
|
||||||
|
- 不确认
|
||||||
|
- 不保护旧配对
|
||||||
|
- 不回滚
|
||||||
|
- 擦除后旧主机立即失效
|
||||||
|
|
||||||
|
## 9. Settings 设计
|
||||||
|
|
||||||
|
## 9.1 持久化范围
|
||||||
|
|
||||||
|
只持久化:
|
||||||
|
|
||||||
|
- `current_slot`
|
||||||
|
- 各槽位 `slot_meta`
|
||||||
|
|
||||||
|
不持久化:
|
||||||
|
|
||||||
|
- `id_lut`
|
||||||
|
- 临时 identity 状态
|
||||||
|
- 擦除中间态
|
||||||
|
|
||||||
|
## 9.2 settings namespace
|
||||||
|
|
||||||
|
建议采用:
|
||||||
|
|
||||||
|
- `ble_multi/current_slot`
|
||||||
|
- `ble_multi/meta/1`
|
||||||
|
- `ble_multi/meta/2`
|
||||||
|
- `ble_multi/meta/3`
|
||||||
|
- `ble_multi/meta/4`
|
||||||
|
|
||||||
|
其中:
|
||||||
|
|
||||||
|
- `1/2/3` 是 BLE 槽位
|
||||||
|
- `4` 是 dongle 预留槽位
|
||||||
|
|
||||||
|
## 9.3 slot_meta 结构
|
||||||
|
|
||||||
|
`slot_meta` 建议固定包含以下字段:
|
||||||
|
|
||||||
|
- `occupied`
|
||||||
|
- `last_peer_addr`
|
||||||
|
- `display_name`
|
||||||
|
|
||||||
|
字段说明:
|
||||||
|
|
||||||
|
- `occupied`
|
||||||
|
说明:该槽位当前是否有有效 bond
|
||||||
|
- `last_peer_addr`
|
||||||
|
说明:最近一次绑定主机的 BLE 地址,用于识别和调试
|
||||||
|
- `display_name`
|
||||||
|
说明:UI 显示名,不做简化,作为正式字段保留
|
||||||
|
|
||||||
|
这里 `display_name` 是明确保留项,不是可选优化项。
|
||||||
|
设计目的就是让 UI 能直接显示:
|
||||||
|
|
||||||
|
- `MacBook Pro`
|
||||||
|
- `iPad Mini`
|
||||||
|
- `Windows-PC`
|
||||||
|
|
||||||
|
而不是只能显示 `Bonded` / `Empty`。
|
||||||
|
|
||||||
|
## 9.4 display_name 来源
|
||||||
|
|
||||||
|
需要注意一点:
|
||||||
|
|
||||||
|
- Zephyr bond 本身不会自动提供“友好主机名”
|
||||||
|
|
||||||
|
因此 `display_name` 的来源需要在实现阶段明确,一般有两个方向:
|
||||||
|
|
||||||
|
1. 配对后由应用层通过协议或自定义方式写入
|
||||||
|
2. 若当前阶段无法拿到真实主机名,则先允许 UI/上位机为该槽位写入名称
|
||||||
|
|
||||||
|
也就是说:
|
||||||
|
|
||||||
|
- `display_name` 必须作为正式持久化字段存在
|
||||||
|
- 但其填充机制可以分阶段实现
|
||||||
|
|
||||||
|
第一阶段如果没有自动名称来源,建议默认值如下:
|
||||||
|
|
||||||
|
- 有 bond 但无名称:`"Bonded Device"`
|
||||||
|
- 无 bond:空串或 `"Empty"`
|
||||||
|
|
||||||
|
## 10. UI 设计
|
||||||
|
|
||||||
|
现有 UI 结构已经适合复用,不建议重做。
|
||||||
|
|
||||||
|
现有文件:
|
||||||
|
|
||||||
|
- `C:\projects\blinky\src\ui\ui_settings_ble.c`
|
||||||
|
- `C:\projects\blinky\src\ui\ui_settings_controller.c`
|
||||||
|
|
||||||
|
建议交互规则如下:
|
||||||
|
|
||||||
|
- 进入 BLE 设置页后显示 `Slot 1~3`
|
||||||
|
- 每个槽位 value 直接显示 `display_name`
|
||||||
|
- 若该槽位无 bond,则显示 `Empty`
|
||||||
|
- 当前槽位显示为 `Slot N`
|
||||||
|
- 擦除项显示为 `Erase Slot N`
|
||||||
|
|
||||||
|
操作方式:
|
||||||
|
|
||||||
|
- 选中某个槽位后直接切换
|
||||||
|
- 选中擦除项后直接擦除
|
||||||
|
- 不加确认页
|
||||||
|
|
||||||
|
UI 需要的数据来源:
|
||||||
|
|
||||||
|
- `current_slot`
|
||||||
|
- `slot_meta[1]`
|
||||||
|
- `slot_meta[2]`
|
||||||
|
- `slot_meta[3]`
|
||||||
|
|
||||||
|
所以建议 UI 不再维护本地假数据,而改为订阅 `ble_bond_multi_event` 或读取统一状态缓存。
|
||||||
|
|
||||||
|
## 11. 与模式切换的兼容设计
|
||||||
|
|
||||||
|
## 11.1 BLE 档
|
||||||
|
|
||||||
|
`MODE_SWITCH_BLE` 下:
|
||||||
|
|
||||||
|
- 只允许使用 `identity 1/2/3`
|
||||||
|
- UI 允许切槽和擦除
|
||||||
|
- `current_slot` 必须属于 `1..3`
|
||||||
|
|
||||||
|
## 11.2 24G / dongle 档
|
||||||
|
|
||||||
|
采用方案 A:
|
||||||
|
|
||||||
|
- `MODE_SWITCH_24G` 绑定专用 `identity 4`
|
||||||
|
- `identity 4` 不与普通 BLE 槽位混用
|
||||||
|
- 第一阶段只预留,不实现完整 dongle bond 生命周期
|
||||||
|
|
||||||
|
这样做的好处:
|
||||||
|
|
||||||
|
- 保持 `mode_policy_module.c` 现有设计语义
|
||||||
|
- 普通 BLE 槽位和 dongle 配对状态不会互相污染
|
||||||
|
- 后续接入 dongle 时无需改动 BLE 三槽数据模型
|
||||||
|
|
||||||
|
## 12. 事件设计
|
||||||
|
|
||||||
|
建议新增应用事件:
|
||||||
|
|
||||||
|
- `ble_bond_multi_event`
|
||||||
|
|
||||||
|
放在:
|
||||||
|
|
||||||
|
- `inc/events/ble_bond_multi_event.h`
|
||||||
|
- `src/events/ble_bond_multi_event.c`
|
||||||
|
|
||||||
|
字段建议:
|
||||||
|
|
||||||
|
- `current_slot`
|
||||||
|
- `active_identity_id`
|
||||||
|
- `slot_occupied_bitmap`
|
||||||
|
- `slot_meta_summary`
|
||||||
|
- `op`
|
||||||
|
|
||||||
|
至少要能支持:
|
||||||
|
|
||||||
|
- UI 刷新槽位名称
|
||||||
|
- 显示层提示当前槽位
|
||||||
|
- 设置层同步当前 active slot
|
||||||
|
|
||||||
|
继续复用 CAF 事件:
|
||||||
|
|
||||||
|
- `ble_peer_event`
|
||||||
|
- `ble_peer_operation_event`
|
||||||
|
|
||||||
|
第一阶段不需要依赖:
|
||||||
|
|
||||||
|
- `ble_peer_search_event`
|
||||||
|
|
||||||
|
## 13. 对现有代码的影响评估
|
||||||
|
|
||||||
|
## 13.1 必改
|
||||||
|
|
||||||
|
- `prj.conf`
|
||||||
|
- `CMakeLists.txt`
|
||||||
|
- 新增 `src/ble_bond_multi_module.c`
|
||||||
|
- 新增 `inc/events/ble_bond_multi_event.h`
|
||||||
|
- 新增 `src/events/ble_bond_multi_event.c`
|
||||||
|
- `ui_settings_controller.c`
|
||||||
|
|
||||||
|
## 13.2 小改
|
||||||
|
|
||||||
|
- `mode_policy_module.c`
|
||||||
|
说明:把 `BLE_PROFILE_POLICY_GENERAL` 约束到 `identity 1/2/3`,把 `BLE_PROFILE_POLICY_DONGLE` 预留到 `identity 4`
|
||||||
|
- `display_module.c`
|
||||||
|
说明:如果要显示当前槽位或名称,需要订阅新事件
|
||||||
|
|
||||||
|
## 13.3 原则上不应大改
|
||||||
|
|
||||||
|
- `ble_hid_module.c`
|
||||||
|
- `ble_nus_module.c`
|
||||||
|
- `keyboard_core_module.c`
|
||||||
|
|
||||||
|
这些模块只应该感知当前连接状态,不承担槽位管理职责。
|
||||||
|
|
||||||
|
## 14. 实施分阶段建议
|
||||||
|
|
||||||
|
## 阶段 1:固定 identity 多槽骨架
|
||||||
|
|
||||||
|
- 替换 CAF 默认 `ble_bond`
|
||||||
|
- 新增 `ble_bond_multi_module`
|
||||||
|
- 固定 `identity 1/2/3` 为 BLE 槽位
|
||||||
|
- 预留 `identity 4` 为 dongle 槽位
|
||||||
|
- 恢复和保存 `current_slot`
|
||||||
|
- 打通直接切槽
|
||||||
|
|
||||||
|
交付标准:
|
||||||
|
|
||||||
|
- `Slot 1~3` 可以切换
|
||||||
|
- 每个槽位 bond 独立
|
||||||
|
- 重启后当前槽位可恢复
|
||||||
|
|
||||||
|
## 阶段 2:slot_meta 与 UI 打通
|
||||||
|
|
||||||
|
- 维护 `occupied`
|
||||||
|
- 维护 `last_peer_addr`
|
||||||
|
- 正式持久化 `display_name`
|
||||||
|
- UI 改为显示真实槽位数据
|
||||||
|
|
||||||
|
交付标准:
|
||||||
|
|
||||||
|
- 屏幕上显示真实槽位名
|
||||||
|
- `display_name` 可稳定持久化
|
||||||
|
|
||||||
|
## 阶段 3:直接擦除与 dongle 预留对齐
|
||||||
|
|
||||||
|
- 实现当前槽位直接 `unpair`
|
||||||
|
- 擦除后刷新 `slot_meta`
|
||||||
|
- 擦除后重新进入可配对状态
|
||||||
|
- 明确 `identity 4` 的模式入口
|
||||||
|
|
||||||
|
交付标准:
|
||||||
|
|
||||||
|
- 擦除后旧主机不能再连接
|
||||||
|
- 当前槽位可重新配对
|
||||||
|
- `24G` 路径已有干净预留点
|
||||||
|
|
||||||
|
## 15. 风险与注意事项
|
||||||
|
|
||||||
|
## 15.1 直接擦除的用户体验
|
||||||
|
|
||||||
|
本方案放弃回滚能力。
|
||||||
|
一旦擦除:
|
||||||
|
|
||||||
|
- 旧 bond 立即失效
|
||||||
|
- 用户必须重新配对
|
||||||
|
|
||||||
|
这是当前明确接受的产品行为。
|
||||||
|
|
||||||
|
## 15.2 display_name 获取机制
|
||||||
|
|
||||||
|
`display_name` 虽然是正式字段,但自动来源未必现成。
|
||||||
|
实现阶段必须明确:
|
||||||
|
|
||||||
|
- 名称是自动采集
|
||||||
|
- 还是通过 UI / 上位机写入
|
||||||
|
|
||||||
|
这不是字段是否保留的问题,而是字段填充路径的问题。
|
||||||
|
|
||||||
|
## 15.3 `BT_ID_MAX=5` 可用性
|
||||||
|
|
||||||
|
必须在目标板上验证:
|
||||||
|
|
||||||
|
- identity 创建正常
|
||||||
|
- 切换 advertising identity 正常
|
||||||
|
- settings 恢复正常
|
||||||
|
|
||||||
|
## 15.4 默认 identity 不参与槽位
|
||||||
|
|
||||||
|
本方案建议 `BT_ID_DEFAULT` 不参与槽位管理,避免后续行为不一致。
|
||||||
|
|
||||||
|
## 16. 当前版本结论
|
||||||
|
|
||||||
|
按目前所有已确认信息,最终设计结论如下:
|
||||||
|
|
||||||
|
1. 只做 BLE 三槽,dongle 只预留专用槽位
|
||||||
|
2. 三个 BLE 槽位直接固定到 `bt_identity_id 1/2/3`
|
||||||
|
3. `bt_identity_id 4` 预留给 dongle
|
||||||
|
4. 不使用 `app_slot_id`
|
||||||
|
5. 不使用临时 identity
|
||||||
|
6. 不做 erase advertising
|
||||||
|
7. 不做擦除确认和回滚
|
||||||
|
8. `slot_meta` 固定包含:
|
||||||
|
- `occupied`
|
||||||
|
- `last_peer_addr`
|
||||||
|
- `display_name`
|
||||||
|
9. `display_name` 作为正式持久化字段保留,用于 UI 显示
|
||||||
|
10. 不新增 `inc/ble_bond_multi.h`,共享接口放到事件头文件
|
||||||
|
|
||||||
|
如果你认可这个版本,下一步就可以按这份方案进入实现阶段。
|
||||||
476
docs/dongle-peripheral-design.md
Normal file
476
docs/dongle-peripheral-design.md
Normal file
@@ -0,0 +1,476 @@
|
|||||||
|
# Blinky 键盘 2.4G 端详细设计稿
|
||||||
|
|
||||||
|
## 1. 背景
|
||||||
|
|
||||||
|
当前 `C:\projects\blinky` 已经完成了外设侧多槽 BLE 基础能力:
|
||||||
|
|
||||||
|
- `Slot 1~3` 对应普通 BLE 主机
|
||||||
|
- `identity 4` 预留为 `Dongle Slot`
|
||||||
|
- `mode_policy_module` 已经区分:
|
||||||
|
- `MODE_SWITCH_BLE -> BLE_PROFILE_POLICY_GENERAL`
|
||||||
|
- `MODE_SWITCH_24G -> BLE_PROFILE_POLICY_DONGLE`
|
||||||
|
|
||||||
|
但目前 `identity 4` 还只是“预留位”,没有真正落成完整的 dongle peer 行为。
|
||||||
|
本设计稿的目标,是定义如何在**键盘外设固件内**实现“2.4G 端”,参考 `nrf_desktop` 的外围设备侧 dongle peer 设计。
|
||||||
|
|
||||||
|
这里的“2.4G 端”在第一阶段的实现语义不是 ESB 或私有无线协议,而是:
|
||||||
|
|
||||||
|
- 键盘继续作为 BLE Peripheral
|
||||||
|
- 通过专用 local identity 与自家 dongle 建立专用连接
|
||||||
|
- 在产品层把该模式呈现为“2.4G”
|
||||||
|
|
||||||
|
这和 `nrf_desktop` 的 `dongle peer` 设计方向一致。
|
||||||
|
|
||||||
|
## 2. 设计目标
|
||||||
|
|
||||||
|
本方案的目标如下:
|
||||||
|
|
||||||
|
- 在当前键盘固件内实现专用 `dongle peer`
|
||||||
|
- `MODE_SWITCH_24G` 时固定使用专用 `identity 4`
|
||||||
|
- `MODE_SWITCH_BLE` 时继续使用普通 BLE 槽位 `identity 1/2/3`
|
||||||
|
- `dongle peer` 与普通 BLE 槽位完全隔离
|
||||||
|
- `dongle peer` 有独立 bond 和独立状态显示
|
||||||
|
- Swift Pair 对普通 BLE 开启,对 `dongle peer` 默认关闭
|
||||||
|
- 后续为自家 dongle 识别准备一个最小 `dev_descr` 风格自定义 GATT 服务
|
||||||
|
|
||||||
|
本设计稿不包含“dongle central 固件”的实现,只定义键盘外设侧。
|
||||||
|
|
||||||
|
## 3. 参考基线
|
||||||
|
|
||||||
|
本设计主要参考本地 `nrf_desktop` 的外围设备侧设计:
|
||||||
|
|
||||||
|
- `c:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\ble_bond.rst`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\dev_descr.rst`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\applications\nrf_desktop\doc\qos.rst`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\applications\nrf_desktop\src\modules\dev_descr.c`
|
||||||
|
- `c:\ncs\v3.2.3\nrf\applications\nrf_desktop\src\modules\qos.c`
|
||||||
|
|
||||||
|
参考结论:
|
||||||
|
|
||||||
|
- `dongle peer` 的核心不是新的无线协议,而是**专用 Bluetooth local identity**
|
||||||
|
- 外设与 dongle 的专用配对要和普通 BLE peer 隔离
|
||||||
|
- `dev_descr` 是未来 dongle 识别外设的关键入口
|
||||||
|
- `QoS` 是增强项,不是第一阶段前置条件
|
||||||
|
|
||||||
|
## 4. 当前工程现状
|
||||||
|
|
||||||
|
当前工程已经具备实现 `dongle peer` 的关键基础:
|
||||||
|
|
||||||
|
- `ble_bond_multi_module` 已支持固定 identity 多槽
|
||||||
|
- `identity 1/2/3` 已用于普通 BLE 槽位
|
||||||
|
- `identity 4` 已预留为 `Dongle Slot`
|
||||||
|
- `mode_policy_module` 已输出 `BLE_PROFILE_POLICY_GENERAL / DONGLE`
|
||||||
|
- Swift Pair 已默认开启,并且在 `identity 4` 上默认关闭
|
||||||
|
|
||||||
|
相关文件:
|
||||||
|
|
||||||
|
- `C:\projects\blinky\src\ble_bond_multi_module.c`
|
||||||
|
- `C:\projects\blinky\src\mode_policy_module.c`
|
||||||
|
- `C:\projects\blinky\inc\events\transport_policy_event.h`
|
||||||
|
- `C:\projects\blinky\src\swift_pair_module.c`
|
||||||
|
|
||||||
|
当前缺失点:
|
||||||
|
|
||||||
|
- `MODE_SWITCH_24G` 没有真正强制切到 `identity 4`
|
||||||
|
- `dongle peer` 没有独立 UI 状态
|
||||||
|
- 没有 `dev_descr` 风格自定义服务
|
||||||
|
- 没有独立的 dongle 管理接口
|
||||||
|
|
||||||
|
## 5. 总体设计
|
||||||
|
|
||||||
|
## 5.1 核心设计结论
|
||||||
|
|
||||||
|
采用 `nrf_desktop` 风格的“专用 dongle peer”设计:
|
||||||
|
|
||||||
|
- 保持当前键盘为 BLE Peripheral
|
||||||
|
- 使用 `identity 4` 作为专用 dongle identity
|
||||||
|
- 以 `MODE_SWITCH_24G` 作为选择 `dongle peer` 的产品模式
|
||||||
|
- 普通 BLE 槽位 `1/2/3` 与 `dongle peer` 独立管理
|
||||||
|
|
||||||
|
也就是说:
|
||||||
|
|
||||||
|
- `2.4G` 是产品语义
|
||||||
|
- 第一阶段实现仍然是 BLE Peripheral + 专用 identity
|
||||||
|
|
||||||
|
## 5.2 identity 规划
|
||||||
|
|
||||||
|
identity 规划继续固定如下:
|
||||||
|
|
||||||
|
| Identity | 角色 | 用途 |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| `0` | 默认 identity | 不使用 |
|
||||||
|
| `1` | BLE Slot 1 | 普通 BLE |
|
||||||
|
| `2` | BLE Slot 2 | 普通 BLE |
|
||||||
|
| `3` | BLE Slot 3 | 普通 BLE |
|
||||||
|
| `4` | Dongle Slot | 专用 2.4G / dongle peer |
|
||||||
|
|
||||||
|
设计原则:
|
||||||
|
|
||||||
|
- `identity 4` 不参与普通槽位轮换
|
||||||
|
- `identity 4` 不参与普通 BLE 设置页
|
||||||
|
- `identity 4` 有独立 bond 和独立状态
|
||||||
|
|
||||||
|
## 6. 模块职责分工
|
||||||
|
|
||||||
|
## 6.1 `mode_policy_module`
|
||||||
|
|
||||||
|
职责保持不变:
|
||||||
|
|
||||||
|
- 根据拨杆位置输出 transport policy
|
||||||
|
|
||||||
|
当前定义:
|
||||||
|
|
||||||
|
- `MODE_SWITCH_BLE -> BLE_PROFILE_POLICY_GENERAL`
|
||||||
|
- `MODE_SWITCH_24G -> BLE_PROFILE_POLICY_DONGLE`
|
||||||
|
|
||||||
|
它不直接管理 bond,只输出“当前应该走哪种 BLE profile”。
|
||||||
|
|
||||||
|
## 6.2 `ble_bond_multi_module`
|
||||||
|
|
||||||
|
这是本方案第一阶段的主修改模块。
|
||||||
|
|
||||||
|
需要扩展职责:
|
||||||
|
|
||||||
|
- 支持根据 `BLE_PROFILE_POLICY_DONGLE` 固定选择 `identity 4`
|
||||||
|
- 普通 BLE 与 `dongle peer` 分开管理
|
||||||
|
- 提供 `dongle peer` 独立状态输出
|
||||||
|
- 提供 `dongle peer` 独立擦除操作
|
||||||
|
|
||||||
|
仍然保留已有职责:
|
||||||
|
|
||||||
|
- 普通 BLE 三槽切换
|
||||||
|
- 普通 BLE 三槽擦除
|
||||||
|
- slot meta 持久化
|
||||||
|
|
||||||
|
## 6.3 `swift_pair_module`
|
||||||
|
|
||||||
|
职责保持:
|
||||||
|
|
||||||
|
- 普通 BLE 槽位启用 Swift Pair
|
||||||
|
- `dongle peer` 默认关闭 Swift Pair payload
|
||||||
|
|
||||||
|
这和 `nrf_desktop` 的思路一致。
|
||||||
|
|
||||||
|
## 6.4 新增 `dev_descr_module`
|
||||||
|
|
||||||
|
建议新增一个最小版 `dev_descr` 风格服务。
|
||||||
|
|
||||||
|
目标:
|
||||||
|
|
||||||
|
- 给未来的 dongle 提供外设识别信息
|
||||||
|
- 不依赖设备名做唯一识别
|
||||||
|
|
||||||
|
第一阶段最小字段:
|
||||||
|
|
||||||
|
- `caps`
|
||||||
|
- `hwid`
|
||||||
|
|
||||||
|
后续可扩展:
|
||||||
|
|
||||||
|
- `role`
|
||||||
|
- `product type`
|
||||||
|
- `firmware revision`
|
||||||
|
|
||||||
|
## 6.5 `qos_module`
|
||||||
|
|
||||||
|
第一阶段不做。
|
||||||
|
|
||||||
|
理由:
|
||||||
|
|
||||||
|
- `nrf_desktop` 已明确说明自家 dongle 不依赖外围设备 QoS
|
||||||
|
- 当前先做 `dongle peer` identity 和识别就足够
|
||||||
|
|
||||||
|
## 7. 行为模型
|
||||||
|
|
||||||
|
## 7.1 普通 BLE 模式
|
||||||
|
|
||||||
|
条件:
|
||||||
|
|
||||||
|
- `MODE_SWITCH_BLE`
|
||||||
|
- `BLE_PROFILE_POLICY_GENERAL`
|
||||||
|
|
||||||
|
行为:
|
||||||
|
|
||||||
|
- 当前 active slot 只能为 `1/2/3`
|
||||||
|
- UI 显示普通 BLE 三槽
|
||||||
|
- 允许普通 BLE 切槽和擦除
|
||||||
|
- Swift Pair 默认开启
|
||||||
|
|
||||||
|
## 7.2 2.4G / Dongle 模式
|
||||||
|
|
||||||
|
条件:
|
||||||
|
|
||||||
|
- `MODE_SWITCH_24G`
|
||||||
|
- `BLE_PROFILE_POLICY_DONGLE`
|
||||||
|
|
||||||
|
行为:
|
||||||
|
|
||||||
|
- 当前 active identity 固定为 `4`
|
||||||
|
- 当前 bond 只属于 `identity 4`
|
||||||
|
- 不允许普通 BLE 三槽逻辑影响 `identity 4`
|
||||||
|
- Swift Pair 默认关闭
|
||||||
|
- UI 应显示:
|
||||||
|
- `Dongle Searching`
|
||||||
|
- `Dongle Paired`
|
||||||
|
- `Dongle Connected`
|
||||||
|
- `Dongle Empty`
|
||||||
|
|
||||||
|
## 7.3 模式切换语义
|
||||||
|
|
||||||
|
### BLE -> 24G
|
||||||
|
|
||||||
|
1. `mode_policy_module` 发布 `BLE_PROFILE_POLICY_DONGLE`
|
||||||
|
2. `ble_bond_multi_module` 切 active identity 到 `4`
|
||||||
|
3. 提交 `PEER_OPERATION_SELECTED`
|
||||||
|
4. CAF `ble_adv` 切到 `identity 4`
|
||||||
|
5. 当前连接断开
|
||||||
|
6. 使用 `identity 4` 广播
|
||||||
|
7. dongle 重连
|
||||||
|
|
||||||
|
### 24G -> BLE
|
||||||
|
|
||||||
|
1. `mode_policy_module` 发布 `BLE_PROFILE_POLICY_GENERAL`
|
||||||
|
2. `ble_bond_multi_module` 恢复普通 BLE 当前槽位 `1/2/3`
|
||||||
|
3. 提交 `PEER_OPERATION_SELECTED`
|
||||||
|
4. CAF `ble_adv` 切回对应 identity
|
||||||
|
5. 当前连接断开
|
||||||
|
6. 使用普通 BLE 槽位广播
|
||||||
|
|
||||||
|
## 8. 状态与事件设计
|
||||||
|
|
||||||
|
## 8.1 当前已有事件
|
||||||
|
|
||||||
|
可继续复用:
|
||||||
|
|
||||||
|
- `transport_policy_event`
|
||||||
|
- `ble_peer_event`
|
||||||
|
- `ble_peer_operation_event`
|
||||||
|
- `ble_bond_multi_event`
|
||||||
|
|
||||||
|
## 8.2 建议扩展 `ble_bond_multi_event`
|
||||||
|
|
||||||
|
建议增加字段或语义:
|
||||||
|
|
||||||
|
- `active_profile`
|
||||||
|
- `GENERAL`
|
||||||
|
- `DONGLE`
|
||||||
|
- `dongle_slot_meta`
|
||||||
|
- `occupied`
|
||||||
|
- `last_peer_addr`
|
||||||
|
- `display_name`
|
||||||
|
- `current_identity`
|
||||||
|
|
||||||
|
如果不想改现有事件结构,也可以新增一个独立事件:
|
||||||
|
|
||||||
|
- `ble_dongle_state_event`
|
||||||
|
|
||||||
|
推荐字段:
|
||||||
|
|
||||||
|
- `state`
|
||||||
|
- `EMPTY`
|
||||||
|
- `SEARCHING`
|
||||||
|
- `PAIRED`
|
||||||
|
- `CONNECTED`
|
||||||
|
- `identity_id`
|
||||||
|
- `display_name`
|
||||||
|
|
||||||
|
## 8.3 UI 状态来源
|
||||||
|
|
||||||
|
主界面与设置页可以通过以下信息判断:
|
||||||
|
|
||||||
|
- 当前 `mode`
|
||||||
|
- 当前 `active_identity_id`
|
||||||
|
- `identity 4` 是否有 bond
|
||||||
|
- 当前是否有连接且该连接属于 `identity 4`
|
||||||
|
|
||||||
|
## 9. `dev_descr` 设计
|
||||||
|
|
||||||
|
## 9.1 目标
|
||||||
|
|
||||||
|
最小化模仿 `nrf_desktop dev_descr`:
|
||||||
|
|
||||||
|
- 为 dongle 提供一个稳定可读的识别服务
|
||||||
|
- 避免仅靠设备名判断
|
||||||
|
|
||||||
|
## 9.2 服务内容
|
||||||
|
|
||||||
|
建议服务包含两个 characteristic:
|
||||||
|
|
||||||
|
### Characteristic 1: `caps`
|
||||||
|
|
||||||
|
建议字节布局:
|
||||||
|
|
||||||
|
- bit0: `supports_llpm`
|
||||||
|
- bit1: `supports_dongle_peer`
|
||||||
|
- bit2: `is_keyboard`
|
||||||
|
|
||||||
|
第一阶段只要能读出:
|
||||||
|
|
||||||
|
- 这个设备支持 dongle peer
|
||||||
|
- 这是 keyboard 类型
|
||||||
|
|
||||||
|
### Characteristic 2: `hwid`
|
||||||
|
|
||||||
|
固定长度硬件 ID:
|
||||||
|
|
||||||
|
- 用于以后让 dongle 唯一识别这把键盘
|
||||||
|
- 也可用于配置通道映射
|
||||||
|
|
||||||
|
## 9.3 权限建议
|
||||||
|
|
||||||
|
建议与 `nrf_desktop` 保持一致:
|
||||||
|
|
||||||
|
- `BT_GATT_PERM_READ_ENCRYPT`
|
||||||
|
|
||||||
|
原因:
|
||||||
|
|
||||||
|
- 避免未加密链路上暴露识别信息
|
||||||
|
|
||||||
|
## 9.4 依赖
|
||||||
|
|
||||||
|
需要:
|
||||||
|
|
||||||
|
- `CONFIG_HWINFO`
|
||||||
|
- 固件内已有或新增 `hwid_get()` 辅助逻辑
|
||||||
|
|
||||||
|
## 10. Settings 设计
|
||||||
|
|
||||||
|
继续沿用现有 namespace:
|
||||||
|
|
||||||
|
- `ble_multi/current_slot`
|
||||||
|
- `ble_multi/meta/1`
|
||||||
|
- `ble_multi/meta/2`
|
||||||
|
- `ble_multi/meta/3`
|
||||||
|
- `ble_multi/meta/4`
|
||||||
|
|
||||||
|
其中 `meta/4` 现在正式用于 dongle peer。
|
||||||
|
|
||||||
|
建议 `meta/4` 字段仍保持:
|
||||||
|
|
||||||
|
- `occupied`
|
||||||
|
- `last_peer_addr`
|
||||||
|
- `display_name`
|
||||||
|
|
||||||
|
显示策略:
|
||||||
|
|
||||||
|
- 有真实名字则显示名字
|
||||||
|
- 否则显示 MAC 地址
|
||||||
|
- 无 bond 显示 `Empty`
|
||||||
|
|
||||||
|
## 11. UI 设计
|
||||||
|
|
||||||
|
## 11.1 主界面
|
||||||
|
|
||||||
|
当前主界面可新增或扩展显示:
|
||||||
|
|
||||||
|
- 当前模式 `USB / BLE / 24G`
|
||||||
|
- 如果 `24G` 模式:
|
||||||
|
- 右上角额外状态位显示:
|
||||||
|
- searching
|
||||||
|
- connected
|
||||||
|
|
||||||
|
现有 BLE LINK 状态位可以复用,但语义建议细化:
|
||||||
|
|
||||||
|
- BLE 模式下:表示普通 BLE 当前槽状态
|
||||||
|
- 24G 模式下:表示 dongle peer 状态
|
||||||
|
|
||||||
|
## 11.2 设置页
|
||||||
|
|
||||||
|
建议新增二级入口:
|
||||||
|
|
||||||
|
- `Bluetooth`
|
||||||
|
- Slot 1
|
||||||
|
- Slot 2
|
||||||
|
- Slot 3
|
||||||
|
- Erase Current
|
||||||
|
- `Dongle`
|
||||||
|
- Status
|
||||||
|
- Erase Dongle Bond
|
||||||
|
|
||||||
|
第一阶段如果不想扩菜单,至少要做到:
|
||||||
|
|
||||||
|
- 主界面显示 dongle peer 状态
|
||||||
|
- 设置页不把 `identity 4` 混入 Slot 1~3
|
||||||
|
|
||||||
|
## 12. 分阶段实施建议
|
||||||
|
|
||||||
|
## 阶段 1:Dongle Slot 真正接线
|
||||||
|
|
||||||
|
内容:
|
||||||
|
|
||||||
|
- `ble_bond_multi_module` 支持 `BLE_PROFILE_POLICY_DONGLE`
|
||||||
|
- `MODE_SWITCH_24G` 时固定切到 `identity 4`
|
||||||
|
- `identity 4` 独立 bond 和独立状态
|
||||||
|
- Swift Pair 在 `identity 4` 默认关闭
|
||||||
|
|
||||||
|
交付标准:
|
||||||
|
|
||||||
|
- 24G 模式和 BLE 模式切换时 identity 正确切换
|
||||||
|
- `identity 4` 与 `1/2/3` 完全隔离
|
||||||
|
|
||||||
|
## 阶段 2:UI / 状态管理
|
||||||
|
|
||||||
|
内容:
|
||||||
|
|
||||||
|
- 增加 dongle 状态显示
|
||||||
|
- 增加独立擦除 dongle bond 入口
|
||||||
|
- 不把 dongle 混入普通 BLE 三槽 UI
|
||||||
|
|
||||||
|
交付标准:
|
||||||
|
|
||||||
|
- 用户可以明确区分:
|
||||||
|
- 普通 BLE 槽位
|
||||||
|
- 2.4G / dongle peer
|
||||||
|
|
||||||
|
## 阶段 3:最小 `dev_descr`
|
||||||
|
|
||||||
|
内容:
|
||||||
|
|
||||||
|
- 实现最小自定义 GATT service
|
||||||
|
- 提供 `caps + hwid`
|
||||||
|
|
||||||
|
交付标准:
|
||||||
|
|
||||||
|
- 未来 dongle 可稳定识别这把键盘
|
||||||
|
|
||||||
|
## 阶段 4:后续增强(非当前范围)
|
||||||
|
|
||||||
|
可选项:
|
||||||
|
|
||||||
|
- QoS
|
||||||
|
- 更强的 dongle 配对约束
|
||||||
|
- 生产预绑定
|
||||||
|
- 更明确的 dongle selector/恢复流程
|
||||||
|
|
||||||
|
## 13. 风险与边界
|
||||||
|
|
||||||
|
## 13.1 “2.4G” 实际仍然是 BLE
|
||||||
|
|
||||||
|
第一阶段产品语义是 2.4G,链路实现仍是 BLE。
|
||||||
|
这没有问题,但必须在代码和文档中明确,避免后续和真正私有 2.4G 链路混淆。
|
||||||
|
|
||||||
|
## 13.2 `dev_descr` 与未来 dongle 要对齐
|
||||||
|
|
||||||
|
如果未来 dongle 端也参考 `nrf_desktop central`,那这里的 UUID 和字段应该尽量稳定,不要后面再频繁改。
|
||||||
|
|
||||||
|
## 13.3 `identity 4` 与普通 UI 必须隔离
|
||||||
|
|
||||||
|
一旦 `identity 4` 被误混入普通三槽,就会造成用户理解混乱:
|
||||||
|
|
||||||
|
- 不知道哪个是普通蓝牙
|
||||||
|
- 哪个是 2.4G
|
||||||
|
|
||||||
|
这是 UI 设计上最需要避免的问题。
|
||||||
|
|
||||||
|
## 14. 结论
|
||||||
|
|
||||||
|
本方案建议把当前键盘的“2.4G 端”实现为:
|
||||||
|
|
||||||
|
- `BLE Peripheral + 专用 Dongle identity`
|
||||||
|
- `MODE_SWITCH_24G` 时固定走 `identity 4`
|
||||||
|
- 与普通 BLE 三槽完全隔离
|
||||||
|
- 第一阶段先打通 identity / bond / UI 状态
|
||||||
|
- 第二阶段补最小 `dev_descr`
|
||||||
|
- `QoS` 暂缓
|
||||||
|
|
||||||
|
这条路径与 `nrf_desktop` 外设侧的 dongle peer 设计一致,也与当前工程已有的 `transport_policy_event` 语义完全匹配。
|
||||||
46
inc/events/ble_bond_multi_event.h
Normal file
46
inc/events/ble_bond_multi_event.h
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#ifndef BLINKY_BLE_BOND_MULTI_EVENT_H_
|
||||||
|
#define BLINKY_BLE_BOND_MULTI_EVENT_H_
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <app_event_manager.h>
|
||||||
|
#include <app_event_manager_profiler_tracer.h>
|
||||||
|
#include <zephyr/bluetooth/addr.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define BLE_BOND_MULTI_DISPLAY_NAME_MAX_LEN 32U
|
||||||
|
#define BLE_BOND_MULTI_BLE_SLOT_COUNT 3U
|
||||||
|
#define BLE_BOND_MULTI_DONGLE_SLOT_ID 4U
|
||||||
|
|
||||||
|
enum ble_bond_multi_op {
|
||||||
|
BLE_BOND_MULTI_OP_NONE = 0,
|
||||||
|
BLE_BOND_MULTI_OP_REFRESH,
|
||||||
|
BLE_BOND_MULTI_OP_SWITCH,
|
||||||
|
BLE_BOND_MULTI_OP_ERASE,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ble_bond_multi_slot_meta {
|
||||||
|
bool occupied;
|
||||||
|
bt_addr_le_t last_peer_addr;
|
||||||
|
char display_name[BLE_BOND_MULTI_DISPLAY_NAME_MAX_LEN];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ble_bond_multi_event {
|
||||||
|
struct app_event_header header;
|
||||||
|
uint8_t current_slot;
|
||||||
|
uint8_t active_identity_id;
|
||||||
|
enum ble_bond_multi_op op;
|
||||||
|
uint8_t slot_occupied_bitmap;
|
||||||
|
struct ble_bond_multi_slot_meta slots[BLE_BOND_MULTI_BLE_SLOT_COUNT];
|
||||||
|
};
|
||||||
|
|
||||||
|
APP_EVENT_TYPE_DECLARE(ble_bond_multi_event);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BLINKY_BLE_BOND_MULTI_EVENT_H_ */
|
||||||
@@ -12,6 +12,8 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
struct ble_bond_multi_slot_meta;
|
||||||
|
|
||||||
void ui_settings_controller_open(void);
|
void ui_settings_controller_open(void);
|
||||||
void ui_settings_controller_close(void);
|
void ui_settings_controller_close(void);
|
||||||
bool ui_settings_controller_back(void);
|
bool ui_settings_controller_back(void);
|
||||||
@@ -27,6 +29,9 @@ const char *ui_settings_ble_current_label(void);
|
|||||||
const char *ui_settings_ble_slot_label(uint8_t slot);
|
const char *ui_settings_ble_slot_label(uint8_t slot);
|
||||||
void ui_settings_ble_select_slot(uint8_t slot);
|
void ui_settings_ble_select_slot(uint8_t slot);
|
||||||
void ui_settings_ble_erase_current(void);
|
void ui_settings_ble_erase_current(void);
|
||||||
|
void ui_settings_ble_set_current_slot(uint8_t slot);
|
||||||
|
void ui_settings_ble_set_slot_meta(uint8_t slot,
|
||||||
|
const struct ble_bond_multi_slot_meta *meta);
|
||||||
void ui_settings_theme_set_current(struct theme_rgb theme);
|
void ui_settings_theme_set_current(struct theme_rgb theme);
|
||||||
const char *ui_settings_theme_current_name(void);
|
const char *ui_settings_theme_current_name(void);
|
||||||
uint8_t ui_settings_theme_current_index(void);
|
uint8_t ui_settings_theme_current_index(void);
|
||||||
|
|||||||
6
prj.conf
6
prj.conf
@@ -60,7 +60,8 @@ CONFIG_BT_SMP=y
|
|||||||
CONFIG_BT_BONDABLE=y
|
CONFIG_BT_BONDABLE=y
|
||||||
CONFIG_BT_SETTINGS=y
|
CONFIG_BT_SETTINGS=y
|
||||||
CONFIG_BT_MAX_CONN=1
|
CONFIG_BT_MAX_CONN=1
|
||||||
CONFIG_BT_MAX_PAIRED=1
|
CONFIG_BT_MAX_PAIRED=4
|
||||||
|
CONFIG_BT_ID_MAX=5
|
||||||
CONFIG_BT_ATT_TX_COUNT=5
|
CONFIG_BT_ATT_TX_COUNT=5
|
||||||
CONFIG_BT_L2CAP_TX_MTU=65
|
CONFIG_BT_L2CAP_TX_MTU=65
|
||||||
CONFIG_BT_BUF_ACL_RX_SIZE=69
|
CONFIG_BT_BUF_ACL_RX_SIZE=69
|
||||||
@@ -110,12 +111,13 @@ CONFIG_CAF_BLE_ADV_SUSPEND_ON_READY=y
|
|||||||
CONFIG_CAF_BLE_ADV_FAST_ADV=y
|
CONFIG_CAF_BLE_ADV_FAST_ADV=y
|
||||||
CONFIG_CAF_BLE_ADV_FILTER_ACCEPT_LIST=y
|
CONFIG_CAF_BLE_ADV_FILTER_ACCEPT_LIST=y
|
||||||
CONFIG_CAF_BLE_ADV_MODULE_SUSPEND_EVENTS=y
|
CONFIG_CAF_BLE_ADV_MODULE_SUSPEND_EVENTS=y
|
||||||
CONFIG_CAF_BLE_BOND=y
|
CONFIG_CAF_BLE_BOND=n
|
||||||
CONFIG_CAF_MODULE_SUSPEND_EVENTS=y
|
CONFIG_CAF_MODULE_SUSPEND_EVENTS=y
|
||||||
CONFIG_BT_ADV_PROV_FLAGS=y
|
CONFIG_BT_ADV_PROV_FLAGS=y
|
||||||
CONFIG_BT_ADV_PROV_GAP_APPEARANCE=y
|
CONFIG_BT_ADV_PROV_GAP_APPEARANCE=y
|
||||||
CONFIG_BT_ADV_PROV_DEVICE_NAME=y
|
CONFIG_BT_ADV_PROV_DEVICE_NAME=y
|
||||||
CONFIG_BT_ADV_PROV_DEVICE_NAME_SD=y
|
CONFIG_BT_ADV_PROV_DEVICE_NAME_SD=y
|
||||||
|
CONFIG_BT_ADV_PROV_SWIFT_PAIR=y
|
||||||
|
|
||||||
# LVGL
|
# LVGL
|
||||||
CONFIG_LVGL=y
|
CONFIG_LVGL=y
|
||||||
|
|||||||
555
src/ble_bond_multi_module.c
Normal file
555
src/ble_bond_multi_module.c
Normal file
@@ -0,0 +1,555 @@
|
|||||||
|
#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_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_addr(uint8_t slot, const bt_addr_le_t *addr)
|
||||||
|
{
|
||||||
|
struct ble_bond_multi_slot_meta *meta = &ctx.slot_meta[slot];
|
||||||
|
|
||||||
|
if ((addr == NULL) || !bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) {
|
||||||
|
strncpy(meta->display_name, DEFAULT_DISPLAY_NAME_EMPTY,
|
||||||
|
sizeof(meta->display_name));
|
||||||
|
meta->display_name[sizeof(meta->display_name) - 1U] = '\0';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bt_addr_le_to_str(addr, meta->display_name, sizeof(meta->display_name));
|
||||||
|
meta->display_name[sizeof(meta->display_name) - 1U] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static void display_name_set_default(uint8_t slot)
|
||||||
|
{
|
||||||
|
struct ble_bond_multi_slot_meta *meta = &ctx.slot_meta[slot];
|
||||||
|
const char *name = DEFAULT_DISPLAY_NAME_EMPTY;
|
||||||
|
|
||||||
|
if (meta->occupied &&
|
||||||
|
bt_addr_le_cmp(&meta->last_peer_addr, BT_ADDR_LE_ANY)) {
|
||||||
|
display_name_set_addr(slot, &meta->last_peer_addr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (meta->occupied &&
|
||||||
|
((meta->display_name[0] == '\0') ||
|
||||||
|
!strcmp(meta->display_name, DEFAULT_DISPLAY_NAME_EMPTY))) {
|
||||||
|
display_name_set_addr(slot, &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_addr(ctx.current_slot,
|
||||||
|
&ctx.slot_meta[ctx.current_slot]
|
||||||
|
.last_peer_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
(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);
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#define MODULE display_module
|
#define MODULE display_module
|
||||||
#include <caf/events/module_state_event.h>
|
#include <caf/events/module_state_event.h>
|
||||||
|
#include <caf/events/ble_common_event.h>
|
||||||
#include <caf/events/power_event.h>
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
#include <lvgl_zephyr.h>
|
#include <lvgl_zephyr.h>
|
||||||
@@ -14,6 +15,7 @@
|
|||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
#include "bat_state_event.h"
|
#include "bat_state_event.h"
|
||||||
|
#include "ble_bond_multi_event.h"
|
||||||
#include "datetime_event.h"
|
#include "datetime_event.h"
|
||||||
#include "hid_led_event.h"
|
#include "hid_led_event.h"
|
||||||
#include "module_lifecycle.h"
|
#include "module_lifecycle.h"
|
||||||
@@ -75,6 +77,7 @@ static struct display_module_ctx ctx = {
|
|||||||
BLINKY_THEME_DEFAULT_B),
|
BLINKY_THEME_DEFAULT_B),
|
||||||
.inactive_border_color = LV_COLOR_MAKE(0x3A, 0x44, 0x52),
|
.inactive_border_color = LV_COLOR_MAKE(0x3A, 0x44, 0x52),
|
||||||
.mode = MODE_SWITCH_BLE,
|
.mode = MODE_SWITCH_BLE,
|
||||||
|
.ble_link_state = UI_BLE_LINK_HIDDEN,
|
||||||
},
|
},
|
||||||
.date_text = "1970/01/01",
|
.date_text = "1970/01/01",
|
||||||
.time_text = "00:00:00",
|
.time_text = "00:00:00",
|
||||||
@@ -209,6 +212,53 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
||||||
|
|
||||||
ctx.ui_model.mode = event->mode;
|
ctx.ui_model.mode = event->mode;
|
||||||
|
if (ctx.ui_model.mode != MODE_SWITCH_BLE) {
|
||||||
|
ctx.ui_model.ble_link_state = UI_BLE_LINK_HIDDEN;
|
||||||
|
}
|
||||||
|
refresh_ui();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_ble_bond_multi_event(aeh)) {
|
||||||
|
refresh_ui();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_ble_peer_search_event(aeh)) {
|
||||||
|
const struct ble_peer_search_event *event =
|
||||||
|
cast_ble_peer_search_event(aeh);
|
||||||
|
|
||||||
|
if (ctx.ui_model.mode == MODE_SWITCH_BLE) {
|
||||||
|
ctx.ui_model.ble_link_state = event->active ?
|
||||||
|
UI_BLE_LINK_SEARCHING : UI_BLE_LINK_HIDDEN;
|
||||||
|
refresh_ui();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_ble_peer_event(aeh)) {
|
||||||
|
const struct ble_peer_event *event = cast_ble_peer_event(aeh);
|
||||||
|
|
||||||
|
if (ctx.ui_model.mode != MODE_SWITCH_BLE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (event->state) {
|
||||||
|
case PEER_STATE_CONNECTED:
|
||||||
|
case PEER_STATE_SECURED:
|
||||||
|
ctx.ui_model.ble_link_state = UI_BLE_LINK_CONNECTED;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PEER_STATE_DISCONNECTED:
|
||||||
|
case PEER_STATE_CONN_FAILED:
|
||||||
|
ctx.ui_model.ble_link_state = UI_BLE_LINK_SEARCHING;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
refresh_ui();
|
refresh_ui();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -316,6 +366,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
|
|
||||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, bat_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, bat_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, ble_bond_multi_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, ble_peer_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, ble_peer_search_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, datetime_event);
|
APP_EVENT_SUBSCRIBE(MODULE, datetime_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_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];
|
uint8_t function_usage_mask[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||||
enum keyboard_protocol_mode transport_protocol_modes[HID_TRANSPORT_COUNT];
|
enum keyboard_protocol_mode transport_protocol_modes[HID_TRANSPORT_COUNT];
|
||||||
enum hid_transport_policy current_transport;
|
enum hid_transport_policy current_transport;
|
||||||
bool mode_valid;
|
|
||||||
bool settings_active;
|
bool settings_active;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -146,8 +145,7 @@ static enum keyboard_protocol_mode active_protocol_mode_get(void)
|
|||||||
{
|
{
|
||||||
enum hid_transport transport;
|
enum hid_transport transport;
|
||||||
|
|
||||||
if (ctx.mode_valid &&
|
if (policy_to_transport(ctx.current_transport, &transport)) {
|
||||||
policy_to_transport(ctx.current_transport, &transport)) {
|
|
||||||
return ctx.transport_protocol_modes[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 keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||||
enum mode_switch_mode mode;
|
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 ||
|
ctx.settings_active ||
|
||||||
!transport_policy_to_mode(ctx.current_transport, &mode) ||
|
!transport_policy_to_mode(ctx.current_transport, &mode) ||
|
||||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
(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 keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||||
enum mode_switch_mode mode;
|
enum mode_switch_mode mode;
|
||||||
|
|
||||||
if (!ctx.mode_valid ||
|
if (!transport_policy_to_mode(ctx.current_transport, &mode)) {
|
||||||
!transport_policy_to_mode(ctx.current_transport, &mode)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,8 +442,7 @@ static void emit_consumer_report(bool force)
|
|||||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||||
enum mode_switch_mode mode;
|
enum mode_switch_mode mode;
|
||||||
|
|
||||||
if (!ctx.mode_valid ||
|
if (!transport_policy_to_mode(ctx.current_transport, &mode) ||
|
||||||
!transport_policy_to_mode(ctx.current_transport, &mode) ||
|
|
||||||
ctx.settings_active ||
|
ctx.settings_active ||
|
||||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||||
return;
|
return;
|
||||||
@@ -522,8 +518,8 @@ static int do_init(void)
|
|||||||
keyboard_state_clear();
|
keyboard_state_clear();
|
||||||
reports_cache_invalidate();
|
reports_cache_invalidate();
|
||||||
function_usage_mask_clear();
|
function_usage_mask_clear();
|
||||||
ctx.mode_valid = false;
|
|
||||||
ctx.settings_active = false;
|
ctx.settings_active = false;
|
||||||
|
ctx.current_transport = HID_TRANSPORT_POLICY_USB;
|
||||||
ctx.transport_protocol_modes[HID_TRANSPORT_USB] =
|
ctx.transport_protocol_modes[HID_TRANSPORT_USB] =
|
||||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||||
ctx.transport_protocol_modes[HID_TRANSPORT_BLE] =
|
ctx.transport_protocol_modes[HID_TRANSPORT_BLE] =
|
||||||
@@ -547,14 +543,13 @@ static int do_stop(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx.mode_valid) {
|
if (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) {
|
||||||
emit_release_reports(ctx.current_transport);
|
emit_release_reports(ctx.current_transport);
|
||||||
}
|
}
|
||||||
emit_function_state_event();
|
emit_function_state_event();
|
||||||
|
|
||||||
keyboard_state_clear();
|
keyboard_state_clear();
|
||||||
reports_cache_invalidate();
|
reports_cache_invalidate();
|
||||||
ctx.mode_valid = false;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -625,8 +620,8 @@ static bool handle_transport_policy_event(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
transport_changed =
|
transport_changed = (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) &&
|
||||||
ctx.mode_valid && (ctx.current_transport != event->hid_transport);
|
(ctx.current_transport != event->hid_transport);
|
||||||
if (transport_changed) {
|
if (transport_changed) {
|
||||||
emit_release_reports(ctx.current_transport);
|
emit_release_reports(ctx.current_transport);
|
||||||
emit_function_state_event();
|
emit_function_state_event();
|
||||||
@@ -635,9 +630,8 @@ static bool handle_transport_policy_event(
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.current_transport = event->hid_transport;
|
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);
|
emit_all_reports(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -646,7 +640,8 @@ static bool handle_transport_policy_event(
|
|||||||
|
|
||||||
static bool handle_encoder_event(const struct encoder_event *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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -701,7 +696,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
ctx.transport_protocol_modes[event->transport] =
|
ctx.transport_protocol_modes[event->transport] =
|
||||||
event->protocol_mode;
|
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) &&
|
policy_to_transport(ctx.current_transport, &active_transport) &&
|
||||||
(active_transport == event->transport)) {
|
(active_transport == event->transport)) {
|
||||||
reports_cache_invalidate();
|
reports_cache_invalidate();
|
||||||
@@ -731,7 +726,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
|
|
||||||
ctx.settings_active = event->active;
|
ctx.settings_active = event->active;
|
||||||
if (ctx.settings_active) {
|
if (ctx.settings_active) {
|
||||||
if (ctx.mode_valid) {
|
if (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) {
|
||||||
emit_release_reports(ctx.current_transport);
|
emit_release_reports(ctx.current_transport);
|
||||||
}
|
}
|
||||||
emit_function_state_event();
|
emit_function_state_event();
|
||||||
|
|||||||
@@ -10,7 +10,9 @@
|
|||||||
#include <caf/events/power_event.h>
|
#include <caf/events/power_event.h>
|
||||||
|
|
||||||
#include <zephyr/logging/log.h>
|
#include <zephyr/logging/log.h>
|
||||||
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
|
#include "ble_bond_multi_event.h"
|
||||||
#include "encoder_event.h"
|
#include "encoder_event.h"
|
||||||
#include "module_lifecycle.h"
|
#include "module_lifecycle.h"
|
||||||
#include "settings_mode_event.h"
|
#include "settings_mode_event.h"
|
||||||
@@ -157,6 +159,22 @@ static bool handle_theme_rgb_update_event(
|
|||||||
return false;
|
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)
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
{
|
{
|
||||||
if (is_click_event(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));
|
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)) {
|
if (is_module_state_event(aeh)) {
|
||||||
const struct module_state_event *event = cast_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, encoder_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, theme_rgb_update_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_EARLY(MODULE, power_down_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||||
|
|||||||
58
src/swift_pair_module.c
Normal file
58
src/swift_pair_module.c
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#include <bluetooth/adv_prov/swift_pair.h>
|
||||||
|
|
||||||
|
#define MODULE swift_pair_module
|
||||||
|
#include <caf/events/ble_common_event.h>
|
||||||
|
|
||||||
|
#include <zephyr/logging/log.h>
|
||||||
|
|
||||||
|
#include "ble_bond_multi_event.h"
|
||||||
|
|
||||||
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
|
static void update_swift_pair_payload(uint8_t selected_identity)
|
||||||
|
{
|
||||||
|
bool enable = (selected_identity != BLE_BOND_MULTI_DONGLE_SLOT_ID);
|
||||||
|
|
||||||
|
bt_le_adv_prov_swift_pair_enable(enable);
|
||||||
|
LOG_INF("Swift Pair payload %s for identity %u",
|
||||||
|
enable ? "enabled" : "disabled", selected_identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_ble_bond_multi_event(const struct ble_bond_multi_event *event)
|
||||||
|
{
|
||||||
|
update_swift_pair_payload(event->active_identity_id);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_ble_peer_operation_event(const struct ble_peer_operation_event *event)
|
||||||
|
{
|
||||||
|
switch (event->op) {
|
||||||
|
case PEER_OPERATION_SELECTED:
|
||||||
|
case PEER_OPERATION_ERASED:
|
||||||
|
update_swift_pair_payload(event->bt_stack_id);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
|
{
|
||||||
|
if (is_ble_bond_multi_event(aeh)) {
|
||||||
|
return handle_ble_bond_multi_event(cast_ble_bond_multi_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_ble_peer_operation_event(aeh)) {
|
||||||
|
return handle_ble_peer_operation_event(
|
||||||
|
cast_ble_peer_operation_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, ble_bond_multi_event);
|
||||||
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_operation_event);
|
||||||
@@ -25,6 +25,9 @@ struct ui_main_ctx {
|
|||||||
lv_obj_t *battery_icon;
|
lv_obj_t *battery_icon;
|
||||||
lv_obj_t *battery_label;
|
lv_obj_t *battery_label;
|
||||||
lv_obj_t *battery_state_label;
|
lv_obj_t *battery_state_label;
|
||||||
|
lv_obj_t *ble_link_wrap;
|
||||||
|
lv_obj_t *ble_link_spinner;
|
||||||
|
lv_obj_t *ble_link_icon;
|
||||||
lv_obj_t *date_label;
|
lv_obj_t *date_label;
|
||||||
lv_obj_t *time_label;
|
lv_obj_t *time_label;
|
||||||
};
|
};
|
||||||
@@ -42,6 +45,8 @@ static const char *const status_texts[UI_STATUS_COUNT] = {
|
|||||||
"A",
|
"A",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define UI_BLE_LINK_BLUE lv_color_hex(0x0082FC)
|
||||||
|
|
||||||
static lv_color_t ui_main_get_battery_color(uint8_t battery_level)
|
static lv_color_t ui_main_get_battery_color(uint8_t battery_level)
|
||||||
{
|
{
|
||||||
if (battery_level > 70U) {
|
if (battery_level > 70U) {
|
||||||
@@ -176,6 +181,31 @@ void ui_main_refresh_battery(const struct ui_main_model *model)
|
|||||||
lv_label_set_text(g_ui.battery_label, battery_text);
|
lv_label_set_text(g_ui.battery_label, battery_text);
|
||||||
lv_label_set_text(g_ui.battery_state_label, state_symbol);
|
lv_label_set_text(g_ui.battery_state_label, state_symbol);
|
||||||
lv_obj_set_style_text_color(g_ui.battery_state_label, state_color, 0);
|
lv_obj_set_style_text_color(g_ui.battery_state_label, state_color, 0);
|
||||||
|
|
||||||
|
if ((g_ui.ble_link_wrap == NULL) || (g_ui.ble_link_spinner == NULL) ||
|
||||||
|
(g_ui.ble_link_icon == NULL)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (model->ble_link_state) {
|
||||||
|
case UI_BLE_LINK_SEARCHING:
|
||||||
|
lv_obj_clear_flag(g_ui.ble_link_wrap, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
lv_obj_clear_flag(g_ui.ble_link_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
lv_obj_add_flag(g_ui.ble_link_icon, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case UI_BLE_LINK_CONNECTED:
|
||||||
|
lv_obj_clear_flag(g_ui.ble_link_wrap, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
lv_obj_add_flag(g_ui.ble_link_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
lv_obj_clear_flag(g_ui.ble_link_icon, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
lv_obj_set_style_text_color(g_ui.ble_link_icon, UI_BLE_LINK_BLUE, 0);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case UI_BLE_LINK_HIDDEN:
|
||||||
|
default:
|
||||||
|
lv_obj_add_flag(g_ui.ble_link_wrap, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ui_main_refresh_datetime(const char *date_text, const char *time_text)
|
void ui_main_refresh_datetime(const char *date_text, const char *time_text)
|
||||||
@@ -270,6 +300,31 @@ void ui_main_init(const struct ui_main_model *model,
|
|||||||
g_ui.battery_state_label = lv_label_create(battery_wrap);
|
g_ui.battery_state_label = lv_label_create(battery_wrap);
|
||||||
lv_obj_set_style_text_font(g_ui.battery_state_label, &lv_font_montserrat_14, 0);
|
lv_obj_set_style_text_font(g_ui.battery_state_label, &lv_font_montserrat_14, 0);
|
||||||
|
|
||||||
|
g_ui.ble_link_wrap = lv_obj_create(battery_wrap);
|
||||||
|
lv_obj_remove_style_all(g_ui.ble_link_wrap);
|
||||||
|
lv_obj_set_size(g_ui.ble_link_wrap, 18, 18);
|
||||||
|
lv_obj_add_flag(g_ui.ble_link_wrap, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
|
||||||
|
g_ui.ble_link_spinner = lv_spinner_create(g_ui.ble_link_wrap);
|
||||||
|
lv_obj_center(g_ui.ble_link_spinner);
|
||||||
|
lv_obj_set_size(g_ui.ble_link_spinner, 16, 16);
|
||||||
|
lv_spinner_set_anim_params(g_ui.ble_link_spinner, 2000, 200);
|
||||||
|
lv_obj_set_style_arc_width(g_ui.ble_link_spinner, 2, LV_PART_MAIN);
|
||||||
|
lv_obj_set_style_arc_width(g_ui.ble_link_spinner, 2, LV_PART_INDICATOR);
|
||||||
|
lv_obj_set_style_arc_color(g_ui.ble_link_spinner, lv_color_hex(0x2A3442),
|
||||||
|
LV_PART_MAIN);
|
||||||
|
lv_obj_set_style_arc_color(g_ui.ble_link_spinner, model->theme_color,
|
||||||
|
LV_PART_INDICATOR);
|
||||||
|
lv_obj_remove_style(g_ui.ble_link_spinner, NULL, LV_PART_KNOB);
|
||||||
|
lv_obj_clear_flag(g_ui.ble_link_spinner, LV_OBJ_FLAG_CLICKABLE);
|
||||||
|
|
||||||
|
g_ui.ble_link_icon = lv_label_create(g_ui.ble_link_wrap);
|
||||||
|
lv_label_set_text(g_ui.ble_link_icon, LV_SYMBOL_BLUETOOTH);
|
||||||
|
lv_obj_center(g_ui.ble_link_icon);
|
||||||
|
lv_obj_set_style_text_font(g_ui.ble_link_icon, &lv_font_montserrat_14, 0);
|
||||||
|
lv_obj_set_style_text_color(g_ui.ble_link_icon, UI_BLE_LINK_BLUE, 0);
|
||||||
|
lv_obj_add_flag(g_ui.ble_link_icon, LV_OBJ_FLAG_HIDDEN);
|
||||||
|
|
||||||
middle_row = lv_obj_create(content);
|
middle_row = lv_obj_create(content);
|
||||||
lv_obj_remove_style_all(middle_row);
|
lv_obj_remove_style_all(middle_row);
|
||||||
lv_obj_set_width(middle_row, LV_PCT(100));
|
lv_obj_set_width(middle_row, LV_PCT(100));
|
||||||
|
|||||||
@@ -13,11 +13,18 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
enum ui_ble_link_state {
|
||||||
|
UI_BLE_LINK_HIDDEN = 0,
|
||||||
|
UI_BLE_LINK_SEARCHING,
|
||||||
|
UI_BLE_LINK_CONNECTED,
|
||||||
|
};
|
||||||
|
|
||||||
struct ui_main_model {
|
struct ui_main_model {
|
||||||
lv_color_t theme_color;
|
lv_color_t theme_color;
|
||||||
lv_color_t inactive_border_color;
|
lv_color_t inactive_border_color;
|
||||||
uint8_t battery_level;
|
uint8_t battery_level;
|
||||||
enum mode_switch_mode mode;
|
enum mode_switch_mode mode;
|
||||||
|
enum ui_ble_link_state ble_link_state;
|
||||||
uint8_t led_mask;
|
uint8_t led_mask;
|
||||||
bool charging;
|
bool charging;
|
||||||
bool full;
|
bool full;
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ static void create_row(struct ui_settings_row *row)
|
|||||||
lv_obj_set_flex_grow(row->value, 1);
|
lv_obj_set_flex_grow(row->value, 1);
|
||||||
lv_obj_set_style_text_font(row->value, &lv_font_montserrat_14, 0);
|
lv_obj_set_style_text_font(row->value, &lv_font_montserrat_14, 0);
|
||||||
lv_obj_set_style_text_color(row->value, lv_color_hex(0x97A3B5), 0);
|
lv_obj_set_style_text_color(row->value, lv_color_hex(0x97A3B5), 0);
|
||||||
lv_obj_set_style_text_align(row->value, LV_TEXT_ALIGN_RIGHT, 0);
|
lv_obj_set_style_text_align(row->value, LV_TEXT_ALIGN_LEFT, 0);
|
||||||
lv_label_set_long_mode(row->value, LV_LABEL_LONG_CLIP);
|
lv_label_set_long_mode(row->value, LV_LABEL_LONG_SCROLL);
|
||||||
|
|
||||||
row->custom = lv_obj_create(row->row);
|
row->custom = lv_obj_create(row->row);
|
||||||
lv_obj_remove_style_all(row->custom);
|
lv_obj_remove_style_all(row->custom);
|
||||||
|
|||||||
@@ -5,25 +5,29 @@
|
|||||||
|
|
||||||
#include <zephyr/sys/util.h>
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
|
#include "ble_bond_multi_event.h"
|
||||||
#include "ui/ui_settings_controller.h"
|
#include "ui/ui_settings_controller.h"
|
||||||
#include "settings_view_event.h"
|
#include "settings_view_event.h"
|
||||||
#include "ui_settings_pages.h"
|
#include "ui_settings_pages.h"
|
||||||
|
|
||||||
#define BLE_SLOT_COUNT 3U
|
#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 controller_ctx {
|
||||||
struct ui_settings_page *current;
|
struct ui_settings_page *current;
|
||||||
bool active;
|
bool active;
|
||||||
uint8_t active_ble_slot;
|
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;
|
struct theme_rgb theme;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct controller_ctx ctx = {
|
static struct controller_ctx ctx = {
|
||||||
.active_ble_slot = 0U,
|
.active_ble_slot = 0U,
|
||||||
.ble_labels = {
|
.ble_labels = {
|
||||||
"Host A",
|
"Empty",
|
||||||
"Host B",
|
"Empty",
|
||||||
"Empty",
|
"Empty",
|
||||||
},
|
},
|
||||||
.theme = {
|
.theme = {
|
||||||
@@ -164,16 +168,13 @@ const char *ui_settings_ble_current_label(void)
|
|||||||
void ui_settings_ble_select_slot(uint8_t slot)
|
void ui_settings_ble_select_slot(uint8_t slot)
|
||||||
{
|
{
|
||||||
if (slot < BLE_SLOT_COUNT) {
|
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)
|
void ui_settings_ble_erase_current(void)
|
||||||
{
|
{
|
||||||
strncpy(ctx.ble_labels[ctx.active_ble_slot], "Empty",
|
(void)ble_bond_multi_erase_current_slot();
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *ui_settings_ble_slot_label(uint8_t 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];
|
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)
|
void ui_settings_theme_set_current(struct theme_rgb theme)
|
||||||
{
|
{
|
||||||
ctx.theme = theme;
|
ctx.theme = theme;
|
||||||
|
|||||||
Reference in New Issue
Block a user