Compare commits
27 Commits
968fb626bb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 88ee4da089 | |||
| adaad6cc34 | |||
| d27df7f3bf | |||
| 07fe70becd | |||
| 54c5f76c84 | |||
| 3971d7c4b2 | |||
| 48968e7880 | |||
| fbdc5426be | |||
| 6a03df1b39 | |||
| a11f4c0110 | |||
| 0a1357c62d | |||
| e0817a7b44 | |||
| caf8d5acc6 | |||
| ceebaaa600 | |||
| 8bfb8b540c | |||
| 0cbb16052d | |||
| 2ca02325c1 | |||
| bd57b00080 | |||
| 6125f04102 | |||
| 0a905d280d | |||
| 79af0eb025 | |||
| bc42a4dd63 | |||
| c4b205b8a1 | |||
| 78a6dc212d | |||
| c342a8d3f0 | |||
| 23e23f63a7 | |||
| ff8f0d81d7 |
@@ -6,10 +6,12 @@ project(blinky)
|
||||
|
||||
list(APPEND CMAKE_MODULE_PATH ${ZEPHYR_BASE}/modules/nanopb)
|
||||
include(nanopb)
|
||||
zephyr_linker_sources(SECTIONS src/usb_function_hook.ld)
|
||||
|
||||
zephyr_include_directories(
|
||||
inc
|
||||
inc/events
|
||||
src
|
||||
)
|
||||
add_subdirectory(drivers)
|
||||
|
||||
@@ -20,39 +22,56 @@ zephyr_nanopb_sources(app
|
||||
target_sources(app PRIVATE
|
||||
src/main.c
|
||||
src/battery_module.c
|
||||
src/ble_adv_ctrl_module.c
|
||||
src/ble_bond_multi_module.c
|
||||
src/ble_adv_uuid16.c
|
||||
src/ble_bas_module.c
|
||||
src/ble_hid_module.c
|
||||
src/ble_serial_module.c
|
||||
src/ble_nus_module.c
|
||||
src/display_module.c
|
||||
src/encoder_module.c
|
||||
src/hid_flowctrl_module.c
|
||||
src/keyboard_core_module.c
|
||||
src/led_effect/led_effect_registry.c
|
||||
src/led_effect/effects/led_effect_key_fade.c
|
||||
src/led_strip_module.c
|
||||
src/mode_policy_module.c
|
||||
src/time_sync_module.c
|
||||
src/settings_module.c
|
||||
src/swift_pair_module.c
|
||||
src/ui/ui_main.c
|
||||
src/cdc_wrapper_module.c
|
||||
src/ui/ui_settings.c
|
||||
src/ui/ui_settings_controller.c
|
||||
src/ui/ui_settings_root.c
|
||||
src/ui/ui_settings_ble.c
|
||||
src/ui/ui_settings_theme.c
|
||||
src/protocol_module.c
|
||||
src/usb_cdc_module.c
|
||||
src/usb_device_module.c
|
||||
src/usb_hid_module.c
|
||||
src/usb_hid_consumer_module.c
|
||||
src/usb_hid_keyboard_module.c
|
||||
src/events/bat_state_event.c
|
||||
src/events/ble_serial_rx_event.c
|
||||
src/events/ble_serial_tx_event.c
|
||||
src/events/cdc_proto_tx_event.c
|
||||
src/events/ble_bond_multi_event.c
|
||||
src/events/datetime_event.c
|
||||
src/events/encoder_event.c
|
||||
src/events/function_bitmap_state_event.c
|
||||
src/events/function_bitmap_update_event.c
|
||||
src/events/hid_channel_state_event.c
|
||||
src/events/hid_led_event.c
|
||||
src/events/hid_report_sent_event.c
|
||||
src/events/hid_transport_state_event.c
|
||||
src/events/hid_tx_report_event.c
|
||||
src/events/key_function_event.c
|
||||
src/events/led_strip_en_event.c
|
||||
src/mode_switch_module.c
|
||||
src/events/keyboard_hid_report_event.c
|
||||
src/events/mode_switch_event.c
|
||||
src/events/proto_rx_event.c
|
||||
src/events/proto_transport_state_event.c
|
||||
src/events/proto_tx_event.c
|
||||
src/events/set_protocol_event.c
|
||||
src/events/usb_cdc_rx_event.c
|
||||
src/events/usb_cdc_tx_event.c
|
||||
src/events/usb_device_state_event.c
|
||||
src/events/usb_function_ready_event.c
|
||||
src/events/usb_prepare_event.c
|
||||
src/events/settings_mode_event.c
|
||||
src/events/settings_view_event.c
|
||||
src/events/theme_rgb_update_event.c
|
||||
src/events/time_sync_event.c
|
||||
src/events/transport_policy_event.c
|
||||
src/events/usb_control_event.c
|
||||
src/events/usb_state_event.c
|
||||
)
|
||||
|
||||
13
Kconfig
13
Kconfig
@@ -2,6 +2,19 @@ mainmenu "blinky"
|
||||
|
||||
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"
|
||||
rsource "drivers/Kconfig"
|
||||
endmenu
|
||||
|
||||
@@ -46,6 +46,21 @@
|
||||
};
|
||||
};
|
||||
|
||||
spi1_ws2812_default: spi1_ws2812_default {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(SPIM_SCK, 1, 4)>,
|
||||
<NRF_PSEL(SPIM_MOSI, 0, 20)>;
|
||||
};
|
||||
};
|
||||
|
||||
spi1_ws2812_sleep: spi1_ws2812_sleep {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(SPIM_SCK, 1, 4)>,
|
||||
<NRF_PSEL(SPIM_MOSI, 0, 20)>;
|
||||
low-power-enable;
|
||||
};
|
||||
};
|
||||
|
||||
pwm0_default: pwm0_default {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(PWM_OUT0, 1, 11)>;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <nordic/nrf52840_qiaa.dtsi>
|
||||
#include "mini_keyboard-pinctrl.dtsi"
|
||||
#include <zephyr/dt-bindings/adc/adc.h>
|
||||
#include <zephyr/dt-bindings/led/led.h>
|
||||
#include <zephyr/dt-bindings/mipi_dbi/mipi_dbi.h>
|
||||
#include <zephyr/dt-bindings/pwm/pwm.h>
|
||||
|
||||
@@ -14,20 +15,29 @@
|
||||
zephyr,flash = &flash0;
|
||||
zephyr,code-partition = &slot0_partition;
|
||||
zephyr,display = &screen_lcd;
|
||||
zephyr,led-strip = &led_strip;
|
||||
zephyr,console = &cdc_acm_uart1;
|
||||
zephyr,log-uart = &log_uarts;
|
||||
};
|
||||
|
||||
aliases {
|
||||
led0 = &myled0;
|
||||
led-strip = &led_strip;
|
||||
qdec0 = &qdec;
|
||||
backlight = &backlight;
|
||||
};
|
||||
|
||||
log_uarts: log_uarts {
|
||||
compatible = "zephyr,log-uart";
|
||||
uarts = <&cdc_acm_uart1>;
|
||||
};
|
||||
|
||||
hid_kbd: hid_kbd {
|
||||
compatible = "zephyr,hid-device";
|
||||
label = "HID_KBD";
|
||||
protocol-code = "keyboard";
|
||||
in-report-size = <29>;
|
||||
out-report-size = <1>;
|
||||
out-report-size = <29>;
|
||||
in-polling-period-us = <1000>;
|
||||
out-polling-period-us = <1000>;
|
||||
};
|
||||
@@ -206,6 +216,28 @@
|
||||
cs-gpios = <&gpio0 2 GPIO_ACTIVE_LOW>;
|
||||
};
|
||||
|
||||
&spi1 {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&spi1_ws2812_default>;
|
||||
pinctrl-1 = <&spi1_ws2812_sleep>;
|
||||
pinctrl-names = "default", "sleep";
|
||||
|
||||
led_strip: ws2812@0 {
|
||||
compatible = "worldsemi,ws2812-spi";
|
||||
reg = <0>;
|
||||
spi-max-frequency = <6400000>;
|
||||
chain-length = <17>;
|
||||
color-mapping = <LED_COLOR_ID_GREEN
|
||||
LED_COLOR_ID_RED
|
||||
LED_COLOR_ID_BLUE>;
|
||||
spi-one-frame = <0x70>;
|
||||
spi-zero-frame = <0x40>;
|
||||
bits-per-symbol = <8>;
|
||||
reset-delay = <8>;
|
||||
supply-gpios = <&gpio0 13 GPIO_ACTIVE_HIGH>;
|
||||
};
|
||||
};
|
||||
|
||||
&pwm0 {
|
||||
status = "okay";
|
||||
pinctrl-0 = <&pwm0_default>;
|
||||
@@ -235,4 +267,9 @@
|
||||
compatible = "zephyr,cdc-acm-uart";
|
||||
label = "CDC_ACM_0";
|
||||
};
|
||||
|
||||
cdc_acm_uart1: cdc_acm_uart1 {
|
||||
compatible = "zephyr,cdc-acm-uart";
|
||||
label = "CDC_ACM_1";
|
||||
};
|
||||
};
|
||||
|
||||
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`,共享接口放到事件头文件
|
||||
|
||||
如果你认可这个版本,下一步就可以按这份方案进入实现阶段。
|
||||
302
docs/device_communication_protocol_v1.md
Normal file
302
docs/device_communication_protocol_v1.md
Normal file
@@ -0,0 +1,302 @@
|
||||
# 下位机通信协议 v1 修订版
|
||||
|
||||
本版基于原 `device_communication_protocol_v1.md` 修订,变更点如下:
|
||||
|
||||
- `CDC` 去掉外层协议,直接传业务消息
|
||||
- `BLE GATT` 改为使用 `NUS (Nordic UART Service)`
|
||||
- `Bitmap` 统一为 29 字节键盘位图格式
|
||||
- `FunctionKeyEvent` 不再上报单键按下/释放,改为上报全键盘位图,格式与 `Bitmap` 一致
|
||||
- 顶层消息增加 `msg_id` / `reply_to`
|
||||
- `Ack` 与 `Error` 合并为统一 `Response`
|
||||
- `CDC` 与 `NUS` 均增加统一外层帧:`magic(2) + len(1) + protobuf`
|
||||
|
||||
## 1. 总体原则
|
||||
|
||||
- 标准键盘输入继续走 `HID / BLE HID`,不变。
|
||||
- 私有通信继续保留两条通道:
|
||||
- `USB`:走 `CDC`
|
||||
- `BLE`:走 `NUS`
|
||||
- 两条通道统一承载同一套 `protobuf` 业务消息。
|
||||
- `CDC` 与 `NUS` 均使用完全相同的外层帧格式,帧内承载 `protobuf` 业务消息。
|
||||
- 当前版本不做应用层分片。
|
||||
|
||||
说明:
|
||||
|
||||
- 顶层 `protobuf` 消息名改为 `DeviceMessage`,表示独立于具体传输通道的统一业务信封。
|
||||
- 当前版本使用统一外层帧,`CDC` 与 `NUS` 都按完整帧进行发送与解析。
|
||||
|
||||
## 2. 共享业务消息
|
||||
|
||||
统一的 `protobuf` 业务消息为 `DeviceMessage`,内部 `oneof body` 可取以下类型:
|
||||
|
||||
| 消息 | 方向 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `HelloReq` | Host -> Device | 握手请求 |
|
||||
| `HelloRsp` | Device -> Host | 握手响应 |
|
||||
| `Bitmap` | Host -> Device | 下发功能键位图配置 |
|
||||
| `FunctionKeyEvent` | Device -> Host | 上报当前全键盘状态位图 |
|
||||
| `LedState` | Device -> Host | 上报键盘 LED 状态 |
|
||||
| `TimeSync` | Host -> Device | 下发时间同步 |
|
||||
| `ThemeRgb` | Host -> Device | 下发主题颜色 |
|
||||
| `Response` | Device -> Host | 控制命令统一响应 |
|
||||
|
||||
顶层公共字段:
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `msg_id` | `uint32` | 当前消息 ID;`0` 表示未使用 |
|
||||
| `reply_to` | `uint32` | 当前消息响应的请求 ID;`0` 表示不是响应 |
|
||||
|
||||
规则:
|
||||
|
||||
- Host 发起的请求消息应填写唯一 `msg_id`。
|
||||
- `HelloRsp` 与 `Response` 应填写 `reply_to = 原请求.msg_id`。
|
||||
- `FunctionKeyEvent`、`LedState` 等异步上报消息应设置 `reply_to = 0`。
|
||||
- 当前版本建议每条链路同一时刻仅保留 `1` 条 in-flight 控制请求。
|
||||
|
||||
## 3. CDC 协议
|
||||
|
||||
CDC 在线路上的真实字节格式如下:
|
||||
|
||||
```text
|
||||
magic(2B) + len(1B) + protobuf(DeviceMessage)
|
||||
```
|
||||
|
||||
约束:
|
||||
|
||||
- `magic` 固定为 `0xAA55`,按小端发送,即线路字节序为 `0x55 0xAA`。
|
||||
- `len` 为后续 `protobuf(DeviceMessage)` 的字节长度,范围 `0..64`。
|
||||
- Host -> Device 与 Device -> Host 都发送完整帧。
|
||||
- 当前版本要求单条业务消息一次完整发送,不做应用层分片与重组。
|
||||
|
||||
## 4. NUS 协议
|
||||
|
||||
BLE 私有通信改为使用 `NUS (Nordic UART Service)`。
|
||||
|
||||
NUS 线路上的真实字节格式如下:
|
||||
|
||||
```text
|
||||
magic(2B) + len(1B) + protobuf(DeviceMessage)
|
||||
```
|
||||
|
||||
方向定义:
|
||||
|
||||
- Host -> Device:向 `RX` 特征写入完整帧
|
||||
- Device -> Host:通过 `TX Notify` 上报完整帧
|
||||
|
||||
## 5. NUS 服务定义
|
||||
|
||||
Service UUID:
|
||||
|
||||
```text
|
||||
6E400001-B5A3-F393-E0A9-E50E24DCCA9E
|
||||
```
|
||||
|
||||
Characteristic 定义:
|
||||
|
||||
| 名称 | UUID | 属性 | 用途 |
|
||||
| --- | --- | --- | --- |
|
||||
| `RX` | `6E400002-B5A3-F393-E0A9-E50E24DCCA9E` | `Write`, `Write Without Response` | Host -> Device |
|
||||
| `TX` | `6E400003-B5A3-F393-E0A9-E50E24DCCA9E` | `Notify` | Device -> Host |
|
||||
|
||||
约束:
|
||||
|
||||
- 连接后主机必须先订阅 `TX`。
|
||||
- 当前版本不做应用层分片。
|
||||
- 因为增加了 `magic + len` 外层帧,建议协商 `ATT_MTU >= 64`,保证完整帧可一次发送。
|
||||
|
||||
## 6. 外层帧定义
|
||||
|
||||
外层帧字段如下:
|
||||
|
||||
| 字段 | 大小 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `magic` | `2` 字节 | 固定 `0xAA55`,线路字节序 `55 AA` |
|
||||
| `len` | `1` 字节 | `protobuf(DeviceMessage)` 长度,最大 `64` |
|
||||
| `payload` | `len` 字节 | `protobuf(DeviceMessage)` |
|
||||
|
||||
## 7. protobuf 字段定义
|
||||
|
||||
以下是业务字段语义。
|
||||
|
||||
### HelloReq
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `protocol_version` | `uint32` | 当前固定为 `1` |
|
||||
|
||||
### HelloRsp
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `protocol_version` | `uint32` | 当前固定为 `1` |
|
||||
| `vendor_id` | `uint32` | 当前建议 `0x1209` |
|
||||
| `product_id` | `uint32` | 当前建议 `0x0001` |
|
||||
| `firmware_major` | `uint32` | 固件主版本 |
|
||||
| `firmware_minor` | `uint32` | 固件次版本 |
|
||||
| `capability_flags` | `uint32` | 能力位 |
|
||||
|
||||
### Bitmap
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `usage_bitmap` | `bytes` | 固定 29 字节,表示功能键配置位图 |
|
||||
|
||||
语义:
|
||||
|
||||
- `Bitmap.usage_bitmap` 长度固定为 `29` 字节。
|
||||
- 第 `0` 字节表示 `0xE0` 到 `0xE7` 这 8 个控制键。
|
||||
- 第 `1` 到第 `28` 字节表示 `0x00` 到 `0xDF`。
|
||||
- 位值为 `1` 表示该 usage 被配置为功能键。
|
||||
- 位值为 `0` 表示该 usage 不是功能键,继续按普通键处理。
|
||||
|
||||
位映射规则:
|
||||
|
||||
- 第 `0` 字节:
|
||||
- `bit0 -> 0xE0`
|
||||
- `bit1 -> 0xE1`
|
||||
- ...
|
||||
- `bit7 -> 0xE7`
|
||||
- 第 `1` 字节:
|
||||
- `bit0 -> 0x00`
|
||||
- `bit1 -> 0x01`
|
||||
- ...
|
||||
- `bit7 -> 0x07`
|
||||
- 第 `2` 字节:
|
||||
- `bit0 -> 0x08`
|
||||
- ...
|
||||
- `bit7 -> 0x0F`
|
||||
- 依此类推。
|
||||
- 第 `28` 字节:
|
||||
- `bit0 -> 0xD8`
|
||||
- ...
|
||||
- `bit7 -> 0xDF`
|
||||
|
||||
### FunctionKeyEvent
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `usage_bitmap` | `bytes` | 固定 29 字节,表示当前全键盘状态位图 |
|
||||
|
||||
语义:
|
||||
|
||||
- `FunctionKeyEvent` 不再使用 `usage + action` 的单键事件格式。
|
||||
- `FunctionKeyEvent.usage_bitmap` 长度固定为 `29` 字节。
|
||||
- 位图编码方式与 `Bitmap.usage_bitmap` 完全一致。
|
||||
- 位值为 `1` 表示该 usage 当前处于按下状态。
|
||||
- 位值为 `0` 表示该 usage 当前处于释放状态。
|
||||
- 该消息表示一次完整键盘状态快照,而不是单个按键变化。
|
||||
|
||||
### LedState
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `led_mask` | `uint32` | NumLock/CapsLock 等位掩码 |
|
||||
|
||||
### TimeSync
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `version` | `uint32` | 当前固定 `1` |
|
||||
| `flags` | `uint32` | 标志位 |
|
||||
| `timezone_min` | `sint32` | 时区偏移,单位分钟 |
|
||||
| `utc_ms` | `fixed64` | UTC 毫秒时间戳 |
|
||||
| `accuracy_ms` | `fixed32` | 精度,毫秒 |
|
||||
|
||||
### ThemeRgb
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `red` | `uint32` | `0..255` |
|
||||
| `green` | `uint32` | `0..255` |
|
||||
| `blue` | `uint32` | `0..255` |
|
||||
|
||||
### Response
|
||||
|
||||
| 字段 | 类型 | 说明 |
|
||||
| --- | --- | --- |
|
||||
| `error_code` | `enum ResponseCode` | 响应结果;`OK` 表示成功,其余表示失败原因 |
|
||||
|
||||
## 8. 枚举定义
|
||||
|
||||
### ResponseCode
|
||||
|
||||
| 值 | 含义 |
|
||||
| ---: | --- |
|
||||
| `0` | `OK` |
|
||||
| `1` | `UNKNOWN_TYPE` |
|
||||
| `2` | `INVALID_LENGTH` |
|
||||
| `3` | `INVALID_PARAM` |
|
||||
| `4` | `NOT_READY` |
|
||||
|
||||
说明:
|
||||
|
||||
- 原 `KeyAction` 枚举已不再使用,因为 `FunctionKeyEvent` 改为完整位图上报。
|
||||
|
||||
## 9. CapabilityFlags 定义
|
||||
|
||||
`HelloRsp.capability_flags` 先按以下位定义:
|
||||
|
||||
| Bit | 含义 |
|
||||
| ---: | --- |
|
||||
| `0` | 支持功能位图配置 |
|
||||
| `1` | 支持时间同步 |
|
||||
| `2` | 支持主题切换 |
|
||||
| `3` | 支持 LED 状态上报 |
|
||||
| `4` | 支持全键盘位图事件上报 |
|
||||
|
||||
当前建议最小返回值:
|
||||
|
||||
- 若仅先做握手:可先返回 `0`
|
||||
- 若 `Bitmap` 可用:打开 `bit0`
|
||||
- 若 `FunctionKeyEvent` 全键盘位图上报可用:打开 `bit4`
|
||||
|
||||
## 10. 握手流程
|
||||
|
||||
### CDC 握手
|
||||
|
||||
1. Host 打开串口
|
||||
2. Host 发送 `magic + len + protobuf(DeviceMessage{hello_req})`
|
||||
3. Device 解析外层帧与 `protobuf(DeviceMessage)`
|
||||
4. Device 取出 `hello_req`
|
||||
5. Device 返回 `magic + len + protobuf(DeviceMessage{hello_rsp})`
|
||||
|
||||
### NUS 握手
|
||||
|
||||
1. Host 连接 BLE
|
||||
2. Host 发现 `NUS Service`
|
||||
3. Host 订阅 `TX Notify`
|
||||
4. Host 向 `RX` 写入完整帧
|
||||
5. Device 解析外层帧与 `protobuf(DeviceMessage)`
|
||||
6. Device 通过 `TX Notify` 发回完整帧
|
||||
|
||||
说明:
|
||||
|
||||
- 文中 `protobuf(HelloReq)` / `protobuf(HelloRsp)` 指业务上发送对应的 `DeviceMessage` 消息,其 `oneof body` 分别为 `hello_req` / `hello_rsp`。
|
||||
|
||||
## 11. 设备行为规则
|
||||
|
||||
- 上电默认所有按键都是普通键。
|
||||
- 普通键的标准输入行为继续走 `HID / BLE HID`,不变。
|
||||
- 收到 `Bitmap` 后,位图中标记为功能键的按键按功能键逻辑处理。
|
||||
- `FunctionKeyEvent` 上报的是当前全键盘状态快照,编码格式与 `Bitmap` 一致。
|
||||
- `LedState` 在 LED 状态变化时上报。
|
||||
- `TimeSync` 收到后更新时间管理模块。
|
||||
- `ThemeRgb` 收到后更新主题模块。
|
||||
- 控制类请求处理完成后统一返回 `Response`。
|
||||
- 不支持的消息类型也应返回 `Response { error_code = UNKNOWN_TYPE }`,不得静默丢弃。
|
||||
|
||||
## 12. Response 规则
|
||||
|
||||
- `HelloReq` 不返回 `Response`,而是直接返回 `HelloRsp`,且 `reply_to = HelloReq.msg_id`。
|
||||
- `Bitmap`、`TimeSync`、`ThemeRgb` 处理完成后应返回 `Response`。
|
||||
- `Response.error_code = OK` 表示成功。
|
||||
- 不支持的消息类型返回 `Response { error_code = UNKNOWN_TYPE }`。
|
||||
- 长度错误、参数错误、模块未就绪时分别返回 `INVALID_LENGTH`、`INVALID_PARAM`、`NOT_READY`。
|
||||
|
||||
示例:
|
||||
|
||||
- 收到 `Bitmap` 成功处理
|
||||
返回 `Response { error_code = OK }`
|
||||
- 收到 `ThemeRgb` 参数非法
|
||||
返回 `Response { error_code = INVALID_PARAM }`
|
||||
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` 语义完全匹配。
|
||||
@@ -1,12 +0,0 @@
|
||||
#ifndef BLINKY_CDC_WRAPPER_MODULE_H_
|
||||
#define BLINKY_CDC_WRAPPER_MODULE_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_CDC_WRAPPER_MODULE_H_ */
|
||||
@@ -20,6 +20,16 @@ struct bat_state_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(bat_state_event);
|
||||
|
||||
static inline void submit_bat_state_event(uint8_t soc, bool charging, bool full)
|
||||
{
|
||||
struct bat_state_event *event = new_bat_state_event();
|
||||
|
||||
event->soc = soc;
|
||||
event->charging = charging;
|
||||
event->full = full;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
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_ */
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_BLE_SERIAL_RX_EVENT_H_
|
||||
#define BLINKY_BLE_SERIAL_RX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ble_serial_rx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_rx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_BLE_SERIAL_RX_EVENT_H_ */
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_BLE_SERIAL_TX_EVENT_H_
|
||||
#define BLINKY_BLE_SERIAL_TX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ble_serial_tx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(ble_serial_tx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_BLE_SERIAL_TX_EVENT_H_ */
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef BLINKY_CDC_PROTO_TX_EVENT_H_
|
||||
#define BLINKY_CDC_PROTO_TX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct cdc_proto_tx_event {
|
||||
struct app_event_header header;
|
||||
uint8_t type;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(cdc_proto_tx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_CDC_PROTO_TX_EVENT_H_ */
|
||||
45
inc/events/datetime_event.h
Normal file
45
inc/events/datetime_event.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef BLINKY_DATETIME_EVENT_H_
|
||||
#define BLINKY_DATETIME_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DATETIME_EVENT_DATE_TEXT_LEN 16
|
||||
#define DATETIME_EVENT_TIME_TEXT_LEN 16
|
||||
|
||||
struct datetime_event {
|
||||
struct app_event_header header;
|
||||
char date_text[DATETIME_EVENT_DATE_TEXT_LEN];
|
||||
char time_text[DATETIME_EVENT_TIME_TEXT_LEN];
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(datetime_event);
|
||||
|
||||
static inline int submit_datetime_event(const char *date_text, const char *time_text)
|
||||
{
|
||||
struct datetime_event *event = new_datetime_event();
|
||||
|
||||
if ((date_text == NULL) || (time_text == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
strncpy(event->date_text, date_text, sizeof(event->date_text));
|
||||
event->date_text[sizeof(event->date_text) - 1] = '\0';
|
||||
strncpy(event->time_text, time_text, sizeof(event->time_text));
|
||||
event->time_text[sizeof(event->time_text) - 1] = '\0';
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_DATETIME_EVENT_H_ */
|
||||
@@ -15,6 +15,14 @@ struct encoder_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(encoder_event);
|
||||
|
||||
static inline void submit_encoder_event(int8_t detents)
|
||||
{
|
||||
struct encoder_event *event = new_encoder_event();
|
||||
|
||||
event->detents = detents;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
41
inc/events/function_bitmap_state_event.h
Normal file
41
inc/events/function_bitmap_state_event.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_
|
||||
#define BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct function_bitmap_state_event {
|
||||
struct app_event_header header;
|
||||
uint8_t bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(function_bitmap_state_event);
|
||||
|
||||
static inline int submit_function_bitmap_state_event(const uint8_t *bitmap)
|
||||
{
|
||||
struct function_bitmap_state_event *event;
|
||||
|
||||
if (bitmap == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_function_bitmap_state_event();
|
||||
memcpy(event->bitmap, bitmap, sizeof(event->bitmap));
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_FUNCTION_BITMAP_STATE_EVENT_H_ */
|
||||
@@ -1,6 +1,9 @@
|
||||
#ifndef BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_
|
||||
#define BLINKY_FUNCTION_BITMAP_UPDATE_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
@@ -17,6 +20,20 @@ struct function_bitmap_update_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(function_bitmap_update_event);
|
||||
|
||||
static inline int submit_function_bitmap_update_event(const uint8_t *bitmap)
|
||||
{
|
||||
struct function_bitmap_update_event *event;
|
||||
|
||||
if (bitmap == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_function_bitmap_update_event();
|
||||
memcpy(event->bitmap, bitmap, sizeof(event->bitmap));
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
38
inc/events/hid_channel_state_event.h
Normal file
38
inc/events/hid_channel_state_event.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef BLINKY_HID_CHANNEL_STATE_EVENT_H_
|
||||
#define BLINKY_HID_CHANNEL_STATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct hid_channel_state_event {
|
||||
struct app_event_header header;
|
||||
enum hid_send_channel channel;
|
||||
uint8_t report_ready_bm;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_channel_state_event);
|
||||
|
||||
static inline void submit_hid_channel_state_event(
|
||||
enum hid_send_channel channel, uint8_t report_ready_bm,
|
||||
enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
struct hid_channel_state_event *event = new_hid_channel_state_event();
|
||||
|
||||
event->channel = channel;
|
||||
event->report_ready_bm = report_ready_bm;
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_HID_CHANNEL_STATE_EVENT_H_ */
|
||||
@@ -18,6 +18,15 @@ struct hid_led_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_led_event);
|
||||
|
||||
static inline void submit_hid_led_event(enum hid_transport transport, uint8_t led_bm)
|
||||
{
|
||||
struct hid_led_event *event = new_hid_led_event();
|
||||
|
||||
event->transport = transport;
|
||||
event->led_bm = led_bm;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -12,7 +12,7 @@ extern "C" {
|
||||
|
||||
struct hid_report_sent_event {
|
||||
struct app_event_header header;
|
||||
enum hid_transport transport;
|
||||
enum hid_send_channel channel;
|
||||
enum keyboard_report_type report_type;
|
||||
uint16_t sequence;
|
||||
bool error;
|
||||
@@ -20,6 +20,19 @@ struct hid_report_sent_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_report_sent_event);
|
||||
|
||||
static inline void submit_hid_report_sent_event(enum hid_send_channel channel,
|
||||
enum keyboard_report_type report_type,
|
||||
uint16_t sequence, bool error)
|
||||
{
|
||||
struct hid_report_sent_event *event = new_hid_report_sent_event();
|
||||
|
||||
event->channel = channel;
|
||||
event->report_type = report_type;
|
||||
event->sequence = sequence;
|
||||
event->error = error;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef BLINKY_HID_TRANSPORT_STATE_EVENT_H_
|
||||
#define BLINKY_HID_TRANSPORT_STATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "keyboard_core.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct hid_transport_state_event {
|
||||
struct app_event_header header;
|
||||
enum hid_transport transport;
|
||||
bool ready;
|
||||
bool keys_ready;
|
||||
bool consumer_ready;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(hid_transport_state_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_HID_TRANSPORT_STATE_EVENT_H_ */
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef BLINKY_HID_TX_REPORT_EVENT_H_
|
||||
#define BLINKY_HID_TX_REPORT_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
@@ -12,7 +16,7 @@ extern "C" {
|
||||
|
||||
struct hid_tx_report_event {
|
||||
struct app_event_header header;
|
||||
enum hid_transport transport;
|
||||
enum hid_send_channel channel;
|
||||
enum keyboard_report_type report_type;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
uint16_t sequence;
|
||||
@@ -21,6 +25,31 @@ struct hid_tx_report_event {
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(hid_tx_report_event);
|
||||
|
||||
static inline int submit_hid_tx_report_event(enum hid_send_channel channel,
|
||||
enum keyboard_report_type report_type,
|
||||
enum keyboard_protocol_mode protocol_mode,
|
||||
uint16_t sequence,
|
||||
const uint8_t *data, size_t size)
|
||||
{
|
||||
struct hid_tx_report_event *event;
|
||||
|
||||
if ((data == NULL) && (size > 0U)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_hid_tx_report_event(size);
|
||||
event->channel = channel;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->sequence = sequence;
|
||||
if (size > 0U) {
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
#ifndef BLINKY_KEY_FUNCTION_EVENT_H_
|
||||
#define BLINKY_KEY_FUNCTION_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum key_function_action {
|
||||
KEY_FUNCTION_ACTION_RELEASE = 0U,
|
||||
KEY_FUNCTION_ACTION_PRESS = 1U,
|
||||
};
|
||||
|
||||
struct key_function_event {
|
||||
struct app_event_header header;
|
||||
uint16_t usage;
|
||||
uint8_t action;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(key_function_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_KEY_FUNCTION_EVENT_H_ */
|
||||
@@ -1,6 +1,10 @@
|
||||
#ifndef BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
||||
#define BLINKY_KEYBOARD_HID_REPORT_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
@@ -22,6 +26,30 @@ struct keyboard_hid_report_event {
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(keyboard_hid_report_event);
|
||||
|
||||
static inline int submit_keyboard_hid_report_event(
|
||||
enum mode_switch_mode mode, enum keyboard_report_type report_type,
|
||||
enum keyboard_protocol_mode protocol_mode,
|
||||
enum hid_queue_policy queue_policy, const uint8_t *data, size_t size)
|
||||
{
|
||||
struct keyboard_hid_report_event *event;
|
||||
|
||||
if ((data == NULL) && (size > 0U)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_keyboard_hid_report_event(size);
|
||||
event->mode = mode;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->queue_policy = queue_policy;
|
||||
if (size > 0U) {
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
32
inc/events/led_strip_en_event.h
Normal file
32
inc/events/led_strip_en_event.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef BLINKY_LED_STRIP_EN_EVENT_H_
|
||||
#define BLINKY_LED_STRIP_EN_EVENT_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct led_strip_en_event {
|
||||
struct app_event_header header;
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(led_strip_en_event);
|
||||
|
||||
static inline void submit_led_strip_en_event(bool enabled)
|
||||
{
|
||||
struct led_strip_en_event *event = new_led_strip_en_event();
|
||||
|
||||
event->enabled = enabled;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_LED_STRIP_EN_EVENT_H_ */
|
||||
@@ -9,6 +9,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
enum mode_switch_mode {
|
||||
MODE_SWITCH_INVALID,
|
||||
MODE_SWITCH_USB,
|
||||
MODE_SWITCH_24G,
|
||||
MODE_SWITCH_BLE,
|
||||
@@ -22,6 +23,16 @@ struct mode_switch_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(mode_switch_event);
|
||||
|
||||
static inline void submit_mode_switch_event(enum mode_switch_mode mode,
|
||||
uint16_t voltage_mv)
|
||||
{
|
||||
struct mode_switch_event *event = new_mode_switch_event();
|
||||
|
||||
event->mode = mode;
|
||||
event->voltage_mv = voltage_mv;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
28
inc/events/proto_common.h
Normal file
28
inc/events/proto_common.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef BLINKY_PROTO_COMMON_H_
|
||||
#define BLINKY_PROTO_COMMON_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum proto_transport {
|
||||
PROTO_TRANSPORT_USB_CDC = 0,
|
||||
PROTO_TRANSPORT_BLE_NUS,
|
||||
PROTO_TRANSPORT_COUNT,
|
||||
};
|
||||
|
||||
enum proto_transport_link_state {
|
||||
PROTO_TRANSPORT_LINK_DOWN = 0,
|
||||
PROTO_TRANSPORT_LINK_READY,
|
||||
};
|
||||
|
||||
#define PROTO_FRAME_MAGIC 0xAA55U
|
||||
#define PROTO_FRAME_HEADER_SIZE 3U
|
||||
#define PROTO_MAX_PAYLOAD_LEN 64U
|
||||
#define PROTO_MAX_FRAME_LEN (PROTO_FRAME_HEADER_SIZE + PROTO_MAX_PAYLOAD_LEN)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_COMMON_H_ */
|
||||
51
inc/events/proto_rx_event.h
Normal file
51
inc/events/proto_rx_event.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef BLINKY_PROTO_RX_EVENT_H_
|
||||
#define BLINKY_PROTO_RX_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct proto_rx_event {
|
||||
struct app_event_header header;
|
||||
enum proto_transport transport;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(proto_rx_event);
|
||||
|
||||
static inline int submit_proto_rx_event(enum proto_transport transport,
|
||||
const uint8_t *data, size_t len)
|
||||
{
|
||||
struct proto_rx_event *event;
|
||||
|
||||
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
||||
((data == NULL) && (len > 0U)) ||
|
||||
(len > PROTO_MAX_FRAME_LEN)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_proto_rx_event(len);
|
||||
event->transport = transport;
|
||||
|
||||
if (len > 0U) {
|
||||
memcpy(event->dyndata.data, data, len);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_RX_EVENT_H_ */
|
||||
36
inc/events/proto_transport_state_event.h
Normal file
36
inc/events/proto_transport_state_event.h
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef BLINKY_PROTO_TRANSPORT_STATE_EVENT_H_
|
||||
#define BLINKY_PROTO_TRANSPORT_STATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct proto_transport_state_event {
|
||||
struct app_event_header header;
|
||||
enum proto_transport transport;
|
||||
enum proto_transport_link_state state;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(proto_transport_state_event);
|
||||
|
||||
static inline void submit_proto_transport_state_event(
|
||||
enum proto_transport transport, enum proto_transport_link_state state)
|
||||
{
|
||||
struct proto_transport_state_event *event =
|
||||
new_proto_transport_state_event();
|
||||
|
||||
event->transport = transport;
|
||||
event->state = state;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_TRANSPORT_STATE_EVENT_H_ */
|
||||
51
inc/events/proto_tx_event.h
Normal file
51
inc/events/proto_tx_event.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef BLINKY_PROTO_TX_EVENT_H_
|
||||
#define BLINKY_PROTO_TX_EVENT_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct proto_tx_event {
|
||||
struct app_event_header header;
|
||||
enum proto_transport transport;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(proto_tx_event);
|
||||
|
||||
static inline int submit_proto_tx_event(enum proto_transport transport,
|
||||
const uint8_t *data, size_t len)
|
||||
{
|
||||
struct proto_tx_event *event;
|
||||
|
||||
if ((transport >= PROTO_TRANSPORT_COUNT) ||
|
||||
((data == NULL) && (len > 0U)) ||
|
||||
(len > PROTO_MAX_FRAME_LEN)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
event = new_proto_tx_event(len);
|
||||
event->transport = transport;
|
||||
|
||||
if (len > 0U) {
|
||||
memcpy(event->dyndata.data, data, len);
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_PROTO_TX_EVENT_H_ */
|
||||
@@ -18,6 +18,16 @@ struct set_protocol_event {
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(set_protocol_event);
|
||||
|
||||
static inline void submit_set_protocol_event(enum hid_transport transport,
|
||||
enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
struct set_protocol_event *event = new_set_protocol_event();
|
||||
|
||||
event->transport = transport;
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
30
inc/events/settings_mode_event.h
Normal file
30
inc/events/settings_mode_event.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef BLINKY_SETTINGS_MODE_EVENT_H_
|
||||
#define BLINKY_SETTINGS_MODE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct settings_mode_event {
|
||||
struct app_event_header header;
|
||||
bool active;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(settings_mode_event);
|
||||
|
||||
static inline void submit_settings_mode_event(bool active)
|
||||
{
|
||||
struct settings_mode_event *event = new_settings_mode_event();
|
||||
|
||||
event->active = active;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_SETTINGS_MODE_EVENT_H_ */
|
||||
35
inc/events/settings_view_event.h
Normal file
35
inc/events/settings_view_event.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef BLINKY_SETTINGS_VIEW_EVENT_H_
|
||||
#define BLINKY_SETTINGS_VIEW_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "ui/ui_settings_page.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct settings_view_event {
|
||||
struct app_event_header header;
|
||||
struct ui_settings_page *page;
|
||||
bool animate;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(settings_view_event);
|
||||
|
||||
static inline void submit_settings_view_event(struct ui_settings_page *page,
|
||||
bool animate)
|
||||
{
|
||||
struct settings_view_event *event = new_settings_view_event();
|
||||
|
||||
event->page = page;
|
||||
event->animate = animate;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_SETTINGS_VIEW_EVENT_H_ */
|
||||
32
inc/events/theme_rgb_update_event.h
Normal file
32
inc/events/theme_rgb_update_event.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef BLINKY_THEME_RGB_UPDATE_EVENT_H_
|
||||
#define BLINKY_THEME_RGB_UPDATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "theme_color.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct theme_rgb_update_event {
|
||||
struct app_event_header header;
|
||||
struct theme_rgb theme;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(theme_rgb_update_event);
|
||||
|
||||
static inline void submit_theme_rgb_update_event(struct theme_rgb theme)
|
||||
{
|
||||
struct theme_rgb_update_event *event = new_theme_rgb_update_event();
|
||||
|
||||
event->theme = theme;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_THEME_RGB_UPDATE_EVENT_H_ */
|
||||
40
inc/events/time_sync_event.h
Normal file
40
inc/events/time_sync_event.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef BLINKY_TIME_SYNC_EVENT_H_
|
||||
#define BLINKY_TIME_SYNC_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct time_sync_event {
|
||||
struct app_event_header header;
|
||||
uint32_t version;
|
||||
uint32_t flags;
|
||||
int32_t timezone_min;
|
||||
uint64_t utc_ms;
|
||||
uint32_t accuracy_ms;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(time_sync_event);
|
||||
|
||||
static inline void submit_time_sync_event(uint32_t version, uint32_t flags,
|
||||
int32_t timezone_min, uint64_t utc_ms,
|
||||
uint32_t accuracy_ms)
|
||||
{
|
||||
struct time_sync_event *event = new_time_sync_event();
|
||||
|
||||
event->version = version;
|
||||
event->flags = flags;
|
||||
event->timezone_min = timezone_min;
|
||||
event->utc_ms = utc_ms;
|
||||
event->accuracy_ms = accuracy_ms;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_TIME_SYNC_EVENT_H_ */
|
||||
42
inc/events/transport_policy_event.h
Normal file
42
inc/events/transport_policy_event.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef BLINKY_TRANSPORT_POLICY_EVENT_H_
|
||||
#define BLINKY_TRANSPORT_POLICY_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#include "mode_switch_event.h"
|
||||
|
||||
enum hid_transport_policy {
|
||||
HID_TRANSPORT_POLICY_NONE = 0,
|
||||
HID_TRANSPORT_POLICY_USB,
|
||||
HID_TRANSPORT_POLICY_BLE,
|
||||
};
|
||||
|
||||
enum ble_profile_policy {
|
||||
BLE_PROFILE_POLICY_NONE = 0,
|
||||
BLE_PROFILE_POLICY_GENERAL,
|
||||
BLE_PROFILE_POLICY_DONGLE,
|
||||
};
|
||||
|
||||
struct transport_policy_event {
|
||||
struct app_event_header header;
|
||||
enum mode_switch_mode source_mode;
|
||||
enum hid_transport_policy hid_transport;
|
||||
enum ble_profile_policy ble_profile;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(transport_policy_event);
|
||||
|
||||
static inline void submit_transport_policy_event(
|
||||
enum mode_switch_mode source_mode,
|
||||
enum hid_transport_policy hid_transport,
|
||||
enum ble_profile_policy ble_profile)
|
||||
{
|
||||
struct transport_policy_event *event = new_transport_policy_event();
|
||||
|
||||
event->source_mode = source_mode;
|
||||
event->hid_transport = hid_transport;
|
||||
event->ble_profile = ble_profile;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#endif /* BLINKY_TRANSPORT_POLICY_EVENT_H_ */
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_USB_CDC_RX_EVENT_H_
|
||||
#define BLINKY_USB_CDC_RX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_cdc_rx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_rx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_CDC_RX_EVENT_H_ */
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef BLINKY_USB_CDC_TX_EVENT_H_
|
||||
#define BLINKY_USB_CDC_TX_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_cdc_tx_event {
|
||||
struct app_event_header header;
|
||||
struct event_dyndata dyndata;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DYNDATA_DECLARE(usb_cdc_tx_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_CDC_TX_EVENT_H_ */
|
||||
71
inc/events/usb_control_event.h
Normal file
71
inc/events/usb_control_event.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#ifndef BLINKY_USB_CONTROL_EVENT_H_
|
||||
#define BLINKY_USB_CONTROL_EVENT_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
#include <zephyr/device.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum usb_control_event_type {
|
||||
USB_CONTROL_EVENT_CDC_LINE_STATE = 0,
|
||||
USB_CONTROL_EVENT_CDC_LINE_CODING,
|
||||
};
|
||||
|
||||
struct usb_control_event {
|
||||
struct app_event_header header;
|
||||
enum usb_control_event_type type;
|
||||
const struct device *dev;
|
||||
union {
|
||||
struct {
|
||||
bool dtr;
|
||||
} cdc_line_state;
|
||||
struct {
|
||||
uint32_t baudrate;
|
||||
uint8_t data_bits;
|
||||
uint8_t stop_bits;
|
||||
uint8_t parity;
|
||||
uint8_t flow_ctrl;
|
||||
} cdc_line_coding;
|
||||
} data;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_control_event);
|
||||
|
||||
static inline void submit_usb_control_cdc_line_state_event(
|
||||
const struct device *dev, bool dtr)
|
||||
{
|
||||
struct usb_control_event *event = new_usb_control_event();
|
||||
|
||||
event->type = USB_CONTROL_EVENT_CDC_LINE_STATE;
|
||||
event->dev = dev;
|
||||
event->data.cdc_line_state.dtr = dtr;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static inline void submit_usb_control_cdc_line_coding_event(
|
||||
const struct device *dev, uint32_t baudrate, uint8_t data_bits,
|
||||
uint8_t stop_bits, uint8_t parity, uint8_t flow_ctrl)
|
||||
{
|
||||
struct usb_control_event *event = new_usb_control_event();
|
||||
|
||||
event->type = USB_CONTROL_EVENT_CDC_LINE_CODING;
|
||||
event->dev = dev;
|
||||
event->data.cdc_line_coding.baudrate = baudrate;
|
||||
event->data.cdc_line_coding.data_bits = data_bits;
|
||||
event->data.cdc_line_coding.stop_bits = stop_bits;
|
||||
event->data.cdc_line_coding.parity = parity;
|
||||
event->data.cdc_line_coding.flow_ctrl = flow_ctrl;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_CONTROL_EVENT_H_ */
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef BLINKY_USB_DEVICE_STATE_EVENT_H_
|
||||
#define BLINKY_USB_DEVICE_STATE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_device_state_event {
|
||||
struct app_event_header header;
|
||||
enum usb_device_state state;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_device_state_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_DEVICE_STATE_EVENT_H_ */
|
||||
@@ -1,24 +0,0 @@
|
||||
#ifndef BLINKY_USB_FUNCTION_READY_EVENT_H_
|
||||
#define BLINKY_USB_FUNCTION_READY_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_function_ready_event {
|
||||
struct app_event_header header;
|
||||
uint8_t function_mask;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_function_ready_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_FUNCTION_READY_EVENT_H_ */
|
||||
@@ -1,21 +0,0 @@
|
||||
#ifndef BLINKY_USB_PREPARE_EVENT_H_
|
||||
#define BLINKY_USB_PREPARE_EVENT_H_
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_prepare_event {
|
||||
struct app_event_header header;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_prepare_event);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_PREPARE_EVENT_H_ */
|
||||
41
inc/events/usb_state_event.h
Normal file
41
inc/events/usb_state_event.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef BLINKY_USB_STATE_EVENT_H_
|
||||
#define BLINKY_USB_STATE_EVENT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
#include <app_event_manager_profiler_tracer.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum usb_state {
|
||||
USB_STATE_DISABLED = 0,
|
||||
USB_STATE_DISCONNECTED,
|
||||
USB_STATE_POWERED,
|
||||
USB_STATE_ACTIVE,
|
||||
USB_STATE_SUSPENDED,
|
||||
};
|
||||
|
||||
struct usb_state_event {
|
||||
struct app_event_header header;
|
||||
enum usb_state state;
|
||||
};
|
||||
|
||||
APP_EVENT_TYPE_DECLARE(usb_state_event);
|
||||
|
||||
static inline void submit_usb_state(enum usb_state state)
|
||||
{
|
||||
struct usb_state_event *event = new_usb_state_event();
|
||||
|
||||
event->state = state;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_STATE_EVENT_H_ */
|
||||
@@ -36,6 +36,13 @@ enum hid_transport {
|
||||
HID_TRANSPORT_COUNT,
|
||||
};
|
||||
|
||||
enum hid_send_channel {
|
||||
HID_SEND_CH_USB_KEYS,
|
||||
HID_SEND_CH_USB_CONSUMER,
|
||||
HID_SEND_CH_BLE_SHARED,
|
||||
HID_SEND_CH_COUNT,
|
||||
};
|
||||
|
||||
enum keyboard_consumer_control {
|
||||
KEYBOARD_CONSUMER_CTRL_MUTE,
|
||||
KEYBOARD_CONSUMER_CTRL_VOLUME_UP,
|
||||
|
||||
207
inc/module_lifecycle.h
Normal file
207
inc/module_lifecycle.h
Normal file
@@ -0,0 +1,207 @@
|
||||
#ifndef MODULE_LIFECYCLE_H_
|
||||
#define MODULE_LIFECYCLE_H_
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
enum module_lifecycle {
|
||||
LC_UNINIT,
|
||||
LC_STOPPED,
|
||||
LC_RUNNING,
|
||||
LC_ERROR,
|
||||
};
|
||||
|
||||
enum module_lifecycle_mode {
|
||||
ML_MODE_NONE,
|
||||
ML_MODE_POWER,
|
||||
ML_MODE_SUSPEND,
|
||||
};
|
||||
|
||||
struct module_lifecycle_ops {
|
||||
int (*do_init)(void);
|
||||
int (*do_start)(void);
|
||||
int (*do_stop)(void);
|
||||
};
|
||||
|
||||
struct module_lifecycle_cfg {
|
||||
enum module_lifecycle_mode mode;
|
||||
enum module_state stopped_state;
|
||||
};
|
||||
|
||||
struct module_lifecycle_ctx {
|
||||
enum module_lifecycle state;
|
||||
const struct module_lifecycle_cfg *cfg;
|
||||
const struct module_lifecycle_ops *ops;
|
||||
};
|
||||
|
||||
static inline bool module_lifecycle_is_running(
|
||||
const struct module_lifecycle_ctx *ctx)
|
||||
{
|
||||
return ctx->state == LC_RUNNING;
|
||||
}
|
||||
|
||||
static inline bool module_lifecycle_is_initialized(
|
||||
const struct module_lifecycle_ctx *ctx)
|
||||
{
|
||||
return (ctx->state != LC_UNINIT) && (ctx->state != LC_ERROR);
|
||||
}
|
||||
|
||||
static inline const char *module_lifecycle_name(enum module_lifecycle state)
|
||||
{
|
||||
switch (state) {
|
||||
case LC_UNINIT:
|
||||
return "UNINIT";
|
||||
case LC_STOPPED:
|
||||
return "STOPPED";
|
||||
case LC_RUNNING:
|
||||
return "RUNNING";
|
||||
case LC_ERROR:
|
||||
return "ERROR";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static inline int module_lifecycle_validate_ops(
|
||||
const struct module_lifecycle_ctx *ctx)
|
||||
{
|
||||
if ((ctx == NULL) || (ctx->cfg == NULL) || (ctx->ops == NULL) ||
|
||||
(ctx->ops->do_init == NULL)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
switch (ctx->cfg->mode) {
|
||||
case ML_MODE_NONE:
|
||||
return 0;
|
||||
|
||||
case ML_MODE_POWER:
|
||||
case ML_MODE_SUSPEND:
|
||||
return (ctx->ops->do_start != NULL) && (ctx->ops->do_stop != NULL) ?
|
||||
0 :
|
||||
-EINVAL;
|
||||
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int module_lifecycle_report_state(
|
||||
struct module_lifecycle_ctx *ctx,
|
||||
enum module_lifecycle state)
|
||||
{
|
||||
switch (state) {
|
||||
case LC_RUNNING:
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
return 0;
|
||||
|
||||
case LC_STOPPED:
|
||||
switch (ctx->cfg->mode) {
|
||||
case ML_MODE_POWER:
|
||||
module_set_state(ctx->cfg->stopped_state);
|
||||
return 0;
|
||||
|
||||
case ML_MODE_SUSPEND:
|
||||
module_set_state(MODULE_STATE_SUSPENDED);
|
||||
return 0;
|
||||
|
||||
case ML_MODE_NONE:
|
||||
return 0;
|
||||
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
case LC_ERROR:
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return 0;
|
||||
|
||||
case LC_UNINIT:
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int module_lifecycle_fail(struct module_lifecycle_ctx *ctx, int err)
|
||||
{
|
||||
ctx->state = LC_ERROR;
|
||||
(void)module_lifecycle_report_state(ctx, LC_ERROR);
|
||||
|
||||
return err ? err : -EIO;
|
||||
}
|
||||
|
||||
static inline int module_set_lifecycle(struct module_lifecycle_ctx *ctx,
|
||||
enum module_lifecycle target)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = module_lifecycle_validate_ops(ctx);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (ctx->state == LC_ERROR) {
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
if (ctx->state == target) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (target == LC_ERROR) {
|
||||
return module_lifecycle_fail(ctx, -EIO);
|
||||
}
|
||||
|
||||
if ((target != LC_STOPPED) && (target != LC_RUNNING)) {
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
switch (ctx->state) {
|
||||
case LC_UNINIT:
|
||||
err = ctx->ops->do_init();
|
||||
if (err) {
|
||||
return module_lifecycle_fail(ctx, err);
|
||||
}
|
||||
|
||||
ctx->state = LC_STOPPED;
|
||||
err = module_lifecycle_report_state(ctx, LC_STOPPED);
|
||||
if (err) {
|
||||
return module_lifecycle_fail(ctx, err);
|
||||
}
|
||||
|
||||
return module_set_lifecycle(ctx, target);
|
||||
|
||||
case LC_STOPPED:
|
||||
if (target != LC_RUNNING) {
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
err = ctx->ops->do_start ? ctx->ops->do_start() : 0;
|
||||
if (err) {
|
||||
return module_lifecycle_fail(ctx, err);
|
||||
}
|
||||
|
||||
ctx->state = LC_RUNNING;
|
||||
return module_lifecycle_report_state(ctx, LC_RUNNING);
|
||||
|
||||
case LC_RUNNING:
|
||||
if (target != LC_STOPPED) {
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
err = ctx->ops->do_stop ? ctx->ops->do_stop() : 0;
|
||||
if (err) {
|
||||
return module_lifecycle_fail(ctx, err);
|
||||
}
|
||||
|
||||
ctx->state = LC_STOPPED;
|
||||
return module_lifecycle_report_state(ctx, LC_STOPPED);
|
||||
|
||||
case LC_ERROR:
|
||||
default:
|
||||
return -EPERM;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* MODULE_LIFECYCLE_H_ */
|
||||
@@ -1,24 +1,19 @@
|
||||
#ifndef BLINKY_PROTOCOL_MODULE_H_
|
||||
#define BLINKY_PROTOCOL_MODULE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "proto_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define CDC_PROTO_TYPE_HELLO_REQ 0x01U
|
||||
#define CDC_PROTO_TYPE_HELLO_RSP 0x02U
|
||||
#define CDC_PROTO_TYPE_BITMAP 0x10U
|
||||
#define CDC_PROTO_TYPE_FUNCTION_KEY_EVENT 0x20U
|
||||
#define CDC_PROTO_TYPE_ACK 0x7EU
|
||||
#define CDC_PROTO_TYPE_ERROR 0x7FU
|
||||
|
||||
int protocol_module_process_cdc_packet(uint8_t req_type,
|
||||
int protocol_module_process_message(enum proto_transport transport,
|
||||
const uint8_t *req_payload,
|
||||
size_t req_payload_len,
|
||||
uint8_t *rsp_type,
|
||||
uint8_t *rsp_payload,
|
||||
size_t rsp_payload_buf_size,
|
||||
size_t *rsp_payload_len);
|
||||
|
||||
24
inc/theme_color.h
Normal file
24
inc/theme_color.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef BLINKY_THEME_COLOR_H_
|
||||
#define BLINKY_THEME_COLOR_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define BLINKY_THEME_DEFAULT_R 0x4CU
|
||||
#define BLINKY_THEME_DEFAULT_G 0x9EU
|
||||
#define BLINKY_THEME_DEFAULT_B 0xF5U
|
||||
|
||||
struct theme_rgb {
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_THEME_COLOR_H_ */
|
||||
66
inc/ui/ui_page.h
Normal file
66
inc/ui/ui_page.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifndef BLINKY_UI_PAGE_H_
|
||||
#define BLINKY_UI_PAGE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ui_page;
|
||||
|
||||
struct ui_page_ops {
|
||||
void (*init)(struct ui_page *page);
|
||||
void (*deinit)(struct ui_page *page);
|
||||
void (*refresh)(struct ui_page *page);
|
||||
};
|
||||
|
||||
struct ui_page {
|
||||
const struct ui_page_ops *ops;
|
||||
struct ui_page *parent;
|
||||
bool initialized;
|
||||
};
|
||||
|
||||
static inline void ui_page_init(struct ui_page *page)
|
||||
{
|
||||
if ((page == NULL) || (page->ops == NULL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (page->initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (page->ops->init != NULL) {
|
||||
page->ops->init(page);
|
||||
}
|
||||
page->initialized = true;
|
||||
}
|
||||
|
||||
static inline void ui_page_deinit(struct ui_page *page)
|
||||
{
|
||||
if ((page == NULL) || !page->initialized || (page->ops == NULL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (page->ops->deinit != NULL) {
|
||||
page->ops->deinit(page);
|
||||
}
|
||||
page->initialized = false;
|
||||
}
|
||||
|
||||
static inline void ui_page_refresh(struct ui_page *page)
|
||||
{
|
||||
if ((page == NULL) || !page->initialized ||
|
||||
(page->ops == NULL) || (page->ops->refresh == NULL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
page->ops->refresh(page);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_UI_PAGE_H_ */
|
||||
43
inc/ui/ui_settings_controller.h
Normal file
43
inc/ui/ui_settings_controller.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef BLINKY_UI_SETTINGS_CONTROLLER_H_
|
||||
#define BLINKY_UI_SETTINGS_CONTROLLER_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "theme_color.h"
|
||||
#include "ui_page.h"
|
||||
#include "ui_settings_page.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ble_bond_multi_slot_meta;
|
||||
|
||||
void ui_settings_controller_open(void);
|
||||
void ui_settings_controller_close(void);
|
||||
bool ui_settings_controller_back(void);
|
||||
void ui_settings_controller_select(void);
|
||||
void ui_settings_controller_move(int8_t delta);
|
||||
void ui_settings_controller_refresh(bool animate);
|
||||
bool ui_settings_controller_is_active(void);
|
||||
|
||||
void ui_settings_controller_switch_to(struct ui_settings_page *page,
|
||||
struct ui_page *parent);
|
||||
|
||||
const char *ui_settings_ble_current_label(void);
|
||||
const char *ui_settings_ble_slot_label(uint8_t slot);
|
||||
void ui_settings_ble_select_slot(uint8_t slot);
|
||||
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);
|
||||
const char *ui_settings_theme_current_name(void);
|
||||
uint8_t ui_settings_theme_current_index(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_UI_SETTINGS_CONTROLLER_H_ */
|
||||
56
inc/ui/ui_settings_page.h
Normal file
56
inc/ui/ui_settings_page.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#ifndef BLINKY_UI_SETTINGS_PAGE_H_
|
||||
#define BLINKY_UI_SETTINGS_PAGE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
#include "ui_page.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ui_settings_page;
|
||||
struct ui_settings_item;
|
||||
|
||||
typedef void (*ui_settings_item_draw_fn)(const struct ui_settings_item *item,
|
||||
lv_obj_t *container);
|
||||
|
||||
struct ui_settings_item {
|
||||
const char *icon;
|
||||
const char *title;
|
||||
const char *value;
|
||||
ui_settings_item_draw_fn draw;
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
struct ui_settings_page_ops {
|
||||
struct ui_page_ops base;
|
||||
|
||||
uint8_t (*get_count)(struct ui_settings_page *page);
|
||||
void (*get_item)(struct ui_settings_page *page, uint8_t index,
|
||||
struct ui_settings_item *item);
|
||||
void (*on_enter)(struct ui_settings_page *page);
|
||||
void (*on_select)(struct ui_settings_page *page, uint8_t index);
|
||||
void (*on_back)(struct ui_settings_page *page);
|
||||
};
|
||||
|
||||
struct ui_settings_page {
|
||||
struct ui_page base;
|
||||
const struct ui_settings_page_ops *ops;
|
||||
const char *title;
|
||||
const char *hint;
|
||||
uint8_t selected;
|
||||
};
|
||||
|
||||
static inline struct ui_settings_page *ui_page_to_settings(struct ui_page *page)
|
||||
{
|
||||
return (struct ui_settings_page *)page;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_UI_SETTINGS_PAGE_H_ */
|
||||
@@ -1,26 +0,0 @@
|
||||
#ifndef BLINKY_USB_DEVICE_MODULE_H_
|
||||
#define BLINKY_USB_DEVICE_MODULE_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum usb_function {
|
||||
USB_FUNCTION_HID = 0x01,
|
||||
USB_FUNCTION_CDC_ACM = 0x02,
|
||||
};
|
||||
|
||||
enum usb_device_state {
|
||||
USB_DEVICE_STATE_DISCONNECTED,
|
||||
USB_DEVICE_STATE_POWERED,
|
||||
USB_DEVICE_STATE_ACTIVE,
|
||||
USB_DEVICE_STATE_SUSPENDED,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_DEVICE_MODULE_H_ */
|
||||
25
inc/usb_function_hook.h
Normal file
25
inc/usb_function_hook.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef BLINKY_USB_FUNCTION_HOOK_H_
|
||||
#define BLINKY_USB_FUNCTION_HOOK_H_
|
||||
|
||||
#include <zephyr/sys/iterable_sections.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct usb_function_hook {
|
||||
const char *name;
|
||||
int (*pre_stack_init)(void);
|
||||
};
|
||||
|
||||
#define USB_FUNCTION_HOOK_DEFINE(_name, _pre_stack_init) \
|
||||
const STRUCT_SECTION_ITERABLE(usb_function_hook, _name) = { \
|
||||
.name = STRINGIFY(_name), \
|
||||
.pre_stack_init = (_pre_stack_init), \
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_USB_FUNCTION_HOOK_H_ */
|
||||
29
prj.conf
29
prj.conf
@@ -3,6 +3,7 @@ CONFIG_CAF_BUTTONS=y
|
||||
CONFIG_CAF_BUTTONS_DEF_PATH="buttons_def.h"
|
||||
CONFIG_CAF_CLICK_DETECTOR=y
|
||||
CONFIG_CAF_CLICK_DETECTOR_DEF_PATH="click_detector_def.h"
|
||||
CONFIG_CAF_CLICK_DETECTOR_LONG_CLICK_MSEC=1500
|
||||
CONFIG_GPIO=y
|
||||
CONFIG_I2C=y
|
||||
CONFIG_LED=y
|
||||
@@ -26,8 +27,19 @@ CONFIG_FLASH_MAP=y
|
||||
CONFIG_NVS=y
|
||||
CONFIG_HEAP_MEM_POOL_SIZE=4096
|
||||
CONFIG_LOG=y
|
||||
CONFIG_LOG_BUFFER_SIZE=16384
|
||||
CONFIG_LOG_BACKEND_UART=y
|
||||
CONFIG_LOG_BACKEND_SHOW_COLOR=n
|
||||
CONFIG_LOG_BACKEND_RTT_OUTPUT_BUFFER_SIZE=16384
|
||||
CONFIG_ASSERT=y
|
||||
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=16384
|
||||
CONFIG_APP_EVENT_MANAGER_MAX_EVENT_CNT=64
|
||||
CONFIG_LED_STRIP=y
|
||||
CONFIG_WS2812_STRIP_SPI=y
|
||||
CONFIG_CONSOLE=y
|
||||
CONFIG_UART_CONSOLE=y
|
||||
CONFIG_STDOUT_CONSOLE=y
|
||||
CONFIG_PRINTK=y
|
||||
|
||||
# USB HID next stack
|
||||
CONFIG_USB_DEVICE_STACK_NEXT=y
|
||||
@@ -43,15 +55,18 @@ CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n
|
||||
# BLE
|
||||
CONFIG_BT=y
|
||||
CONFIG_BT_PERIPHERAL=y
|
||||
CONFIG_BT_ZEPHYR_NUS=y
|
||||
CONFIG_BT_SMP=y
|
||||
CONFIG_BT_BONDABLE=y
|
||||
CONFIG_BT_SETTINGS=y
|
||||
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_L2CAP_TX_MTU=65
|
||||
CONFIG_BT_BUF_ACL_RX_SIZE=69
|
||||
CONFIG_BT_BUF_ACL_TX_SIZE=69
|
||||
CONFIG_BT_PERIPHERAL_PREF_TIMEOUT=400
|
||||
CONFIG_BT_CONN_CTX=y
|
||||
CONFIG_BT_DEVICE_NAME="WH Mini Keyboard"
|
||||
CONFIG_BT_DEVICE_APPEARANCE=961
|
||||
@@ -96,16 +111,13 @@ CONFIG_CAF_BLE_ADV_SUSPEND_ON_READY=y
|
||||
CONFIG_CAF_BLE_ADV_FAST_ADV=y
|
||||
CONFIG_CAF_BLE_ADV_FILTER_ACCEPT_LIST=y
|
||||
CONFIG_CAF_BLE_ADV_MODULE_SUSPEND_EVENTS=y
|
||||
CONFIG_CAF_BLE_BOND=y
|
||||
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK=y
|
||||
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_KEY_ID=0x180
|
||||
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_LONG=y
|
||||
CONFIG_CAF_BLE_BOND_PEER_ERASE_CLICK_TIMEOUT=-1
|
||||
CONFIG_CAF_BLE_BOND=n
|
||||
CONFIG_CAF_MODULE_SUSPEND_EVENTS=y
|
||||
CONFIG_BT_ADV_PROV_FLAGS=y
|
||||
CONFIG_BT_ADV_PROV_GAP_APPEARANCE=y
|
||||
CONFIG_BT_ADV_PROV_DEVICE_NAME=y
|
||||
CONFIG_BT_ADV_PROV_DEVICE_NAME_SD=y
|
||||
CONFIG_BT_ADV_PROV_SWIFT_PAIR=y
|
||||
|
||||
# LVGL
|
||||
CONFIG_LVGL=y
|
||||
@@ -118,8 +130,11 @@ CONFIG_LV_COLOR_16_SWAP=y
|
||||
CONFIG_LV_Z_BITS_PER_PIXEL=16
|
||||
CONFIG_LV_Z_VDB_SIZE=25
|
||||
CONFIG_LV_Z_DOUBLE_VDB=y
|
||||
CONFIG_LV_Z_MEM_POOL_SIZE=16384
|
||||
CONFIG_LV_Z_MEM_POOL_SIZE=32768
|
||||
CONFIG_LV_USE_LABEL=y
|
||||
CONFIG_LV_FONT_MONTSERRAT_14=y
|
||||
CONFIG_LV_FONT_MONTSERRAT_32=y
|
||||
CONFIG_MAIN_STACK_SIZE=4096
|
||||
|
||||
CONFIG_USE_SEGGER_RTT=y
|
||||
CONFIG_SPEED_OPTIMIZATIONS=y
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
Bitmap.usage_bitmap max_size:29
|
||||
FunctionKeyEvent.usage_bitmap max_size:29
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
syntax = "proto3";
|
||||
|
||||
enum ResponseCode {
|
||||
RESPONSE_CODE_OK = 0;
|
||||
RESPONSE_CODE_UNKNOWN_TYPE = 1;
|
||||
RESPONSE_CODE_INVALID_LENGTH = 2;
|
||||
RESPONSE_CODE_INVALID_PARAM = 3;
|
||||
RESPONSE_CODE_NOT_READY = 4;
|
||||
}
|
||||
|
||||
message HelloReq {
|
||||
uint32 protocol_version = 1;
|
||||
}
|
||||
@@ -17,40 +25,44 @@ message Bitmap {
|
||||
bytes usage_bitmap = 1;
|
||||
}
|
||||
|
||||
enum KeyAction {
|
||||
KEY_ACTION_RELEASE = 0;
|
||||
KEY_ACTION_PRESS = 1;
|
||||
}
|
||||
|
||||
message FunctionKeyEvent {
|
||||
uint32 usage = 1;
|
||||
KeyAction action = 2;
|
||||
bytes usage_bitmap = 1;
|
||||
}
|
||||
|
||||
message Ack {
|
||||
uint32 acked_type = 1;
|
||||
message LedState {
|
||||
uint32 led_mask = 1;
|
||||
}
|
||||
|
||||
enum ErrorCode {
|
||||
ERROR_CODE_NONE = 0;
|
||||
ERROR_CODE_UNKNOWN_TYPE = 1;
|
||||
ERROR_CODE_INVALID_LENGTH = 2;
|
||||
ERROR_CODE_INVALID_PARAM = 3;
|
||||
ERROR_CODE_NOT_READY = 4;
|
||||
message TimeSync {
|
||||
uint32 version = 1;
|
||||
uint32 flags = 2;
|
||||
sint32 timezone_min = 3;
|
||||
fixed64 utc_ms = 4;
|
||||
fixed32 accuracy_ms = 5;
|
||||
}
|
||||
|
||||
message Error {
|
||||
uint32 error_type = 1;
|
||||
ErrorCode error_code = 2;
|
||||
message ThemeRgb {
|
||||
uint32 red = 1;
|
||||
uint32 green = 2;
|
||||
uint32 blue = 3;
|
||||
}
|
||||
|
||||
message CdcPacketBody {
|
||||
message Response {
|
||||
ResponseCode error_code = 1;
|
||||
}
|
||||
|
||||
message DeviceMessage {
|
||||
uint32 msg_id = 10;
|
||||
uint32 reply_to = 11;
|
||||
|
||||
oneof body {
|
||||
HelloReq hello_req = 1;
|
||||
HelloRsp hello_rsp = 2;
|
||||
Bitmap bitmap = 3;
|
||||
FunctionKeyEvent function_key_event = 4;
|
||||
Ack ack = 5;
|
||||
Error error = 6;
|
||||
LedState led_state = 5;
|
||||
TimeSync time_sync = 6;
|
||||
ThemeRgb theme_rgb = 7;
|
||||
Response response = 8;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <zephyr/pm/device.h>
|
||||
|
||||
#include "bat_state_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -32,17 +33,43 @@ BUILD_ASSERT(DT_NODE_HAS_STATUS(VBATT_NODE, okay),
|
||||
BUILD_ASSERT(DT_NODE_HAS_STATUS(IP5306_NODE, okay),
|
||||
"Missing ip5306 node in devicetree");
|
||||
|
||||
static const struct device *const vbatt_dev = DEVICE_DT_GET(VBATT_NODE);
|
||||
static const struct device *const ip5306_dev = DEVICE_DT_GET(IP5306_NODE);
|
||||
static struct k_work_delayable battery_sample_work;
|
||||
static struct {
|
||||
struct battery_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
const struct device *vbatt_dev;
|
||||
const struct device *ip5306_dev;
|
||||
struct k_work_delayable battery_sample_work;
|
||||
struct {
|
||||
bool valid;
|
||||
uint8_t soc;
|
||||
bool charging;
|
||||
bool full;
|
||||
} last_bat_state;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
|
||||
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
||||
.mode = ML_MODE_POWER,
|
||||
.stopped_state = MODULE_STATE_STANDBY,
|
||||
};
|
||||
|
||||
static const struct module_lifecycle_ops lifecycle_ops = {
|
||||
.do_init = do_init,
|
||||
.do_start = do_start,
|
||||
.do_stop = do_stop,
|
||||
};
|
||||
|
||||
static struct battery_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.vbatt_dev = DEVICE_DT_GET(VBATT_NODE),
|
||||
.ip5306_dev = DEVICE_DT_GET(IP5306_NODE),
|
||||
};
|
||||
|
||||
static int sensor_value_to_mv(const struct sensor_value *value)
|
||||
{
|
||||
@@ -53,7 +80,7 @@ static int measurement_enable(bool enable)
|
||||
{
|
||||
enum pm_device_action action = enable ? PM_DEVICE_ACTION_RESUME
|
||||
: PM_DEVICE_ACTION_SUSPEND;
|
||||
int err = pm_device_action_run(vbatt_dev, action);
|
||||
int err = pm_device_action_run(ctx.vbatt_dev, action);
|
||||
|
||||
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
||||
LOG_ERR("Cannot %s vbatt sensor (%d)", enable ? "resume" : "suspend", err);
|
||||
@@ -81,30 +108,6 @@ static uint8_t battery_soc_from_mv(int voltage_mv)
|
||||
return (uint8_t)(bucket * 10);
|
||||
}
|
||||
|
||||
static void submit_bat_state_event(uint8_t soc, bool charging, bool full)
|
||||
{
|
||||
struct bat_state_event *event;
|
||||
|
||||
if (last_bat_state.valid &&
|
||||
(last_bat_state.soc == soc) &&
|
||||
(last_bat_state.charging == charging) &&
|
||||
(last_bat_state.full == full)) {
|
||||
return;
|
||||
}
|
||||
|
||||
last_bat_state.valid = true;
|
||||
last_bat_state.soc = soc;
|
||||
last_bat_state.charging = charging;
|
||||
last_bat_state.full = full;
|
||||
|
||||
event = new_bat_state_event();
|
||||
event->soc = soc;
|
||||
event->charging = charging;
|
||||
event->full = full;
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void battery_sample_fn(struct k_work *work)
|
||||
{
|
||||
struct ip5306_status pmic_status;
|
||||
@@ -114,69 +117,78 @@ static void battery_sample_fn(struct k_work *work)
|
||||
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = sensor_sample_fetch(vbatt_dev);
|
||||
err = sensor_sample_fetch(ctx.vbatt_dev);
|
||||
if (err) {
|
||||
LOG_WRN("Battery sample fetch failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
err = sensor_channel_get(vbatt_dev, SENSOR_CHAN_VOLTAGE, &voltage);
|
||||
err = sensor_channel_get(ctx.vbatt_dev, SENSOR_CHAN_VOLTAGE, &voltage);
|
||||
if (err) {
|
||||
LOG_WRN("Battery channel get failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
err = ip5306_get_status(ip5306_dev, &pmic_status);
|
||||
err = ip5306_get_status(ctx.ip5306_dev, &pmic_status);
|
||||
if (err) {
|
||||
LOG_WRN("IP5306 status read failed (%d)", err);
|
||||
goto reschedule;
|
||||
}
|
||||
|
||||
voltage_mv = sensor_value_to_mv(&voltage);
|
||||
submit_bat_state_event(battery_soc_from_mv(voltage_mv),
|
||||
pmic_status.charging,
|
||||
pmic_status.full);
|
||||
uint8_t soc = battery_soc_from_mv(voltage_mv);
|
||||
|
||||
if (!ctx.last_bat_state.valid ||
|
||||
(ctx.last_bat_state.soc != soc) ||
|
||||
(ctx.last_bat_state.charging != pmic_status.charging) ||
|
||||
(ctx.last_bat_state.full != pmic_status.full)) {
|
||||
ctx.last_bat_state.valid = true;
|
||||
ctx.last_bat_state.soc = soc;
|
||||
ctx.last_bat_state.charging = pmic_status.charging;
|
||||
ctx.last_bat_state.full = pmic_status.full;
|
||||
submit_bat_state_event(soc, pmic_status.charging, pmic_status.full);
|
||||
}
|
||||
|
||||
reschedule:
|
||||
if (running) {
|
||||
k_work_reschedule(&battery_sample_work, BATTERY_SAMPLE_INTERVAL);
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
k_work_reschedule(&ctx.battery_sample_work, BATTERY_SAMPLE_INTERVAL);
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
if (!device_is_ready(vbatt_dev)) {
|
||||
if (!device_is_ready(ctx.vbatt_dev)) {
|
||||
LOG_ERR("vbatt device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!device_is_ready(ip5306_dev)) {
|
||||
if (!device_is_ready(ctx.ip5306_dev)) {
|
||||
LOG_ERR("ip5306 device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
int err = ip5306_init(ip5306_dev);
|
||||
int err = ip5306_init(ctx.ip5306_dev);
|
||||
if (err) {
|
||||
LOG_ERR("ip5306 init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
k_work_init_delayable(&battery_sample_work, battery_sample_fn);
|
||||
memset(&last_bat_state, 0, sizeof(last_bat_state));
|
||||
k_work_init_delayable(&ctx.battery_sample_work, battery_sample_fn);
|
||||
memset(&ctx.last_bat_state, 0, sizeof(ctx.last_bat_state));
|
||||
power_manager_restrict(MODULE_IDX(MODULE), POWER_MANAGER_LEVEL_SUSPENDED);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -185,21 +197,21 @@ static int module_start(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
running = true;
|
||||
k_work_reschedule(&battery_sample_work, K_NO_WAIT);
|
||||
k_work_reschedule(&ctx.battery_sample_work, K_NO_WAIT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
(void)k_work_cancel_delayable(&battery_sample_work);
|
||||
(void)k_work_cancel_delayable(&ctx.battery_sample_work);
|
||||
(void)measurement_enable(false);
|
||||
running = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
@@ -208,47 +220,23 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE ble_adv_ctrl_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/module_suspend_event.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "mode_switch_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_adv_suspended = true;
|
||||
|
||||
static void broadcast_ble_adv_req(bool suspend)
|
||||
{
|
||||
if (suspend) {
|
||||
struct module_suspend_req_event *event = new_module_suspend_req_event();
|
||||
|
||||
event->sink_module_id = MODULE_ID(ble_adv);
|
||||
event->src_module_id = MODULE_ID(MODULE);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
} else {
|
||||
struct module_resume_req_event *event = new_module_resume_req_event();
|
||||
|
||||
event->sink_module_id = MODULE_ID(ble_adv);
|
||||
event->src_module_id = MODULE_ID(MODULE);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
ble_adv_suspended = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
|
||||
static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
{
|
||||
bool should_suspend;
|
||||
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
should_suspend = (event->mode != MODE_SWITCH_BLE);
|
||||
|
||||
if (should_suspend != ble_adv_suspended) {
|
||||
ble_adv_suspended = should_suspend;
|
||||
broadcast_ble_adv_req(should_suspend);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_mode_switch_event(aeh)) {
|
||||
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
@@ -11,34 +11,54 @@
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "bat_state_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
static uint8_t current_soc = 100U;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_ready;
|
||||
struct ble_bas_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
uint8_t current_soc;
|
||||
bool ble_ready;
|
||||
};
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(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_bas_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.current_soc = 100U,
|
||||
};
|
||||
|
||||
static int do_init(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int bas_update_level(void)
|
||||
{
|
||||
int err;
|
||||
int err = bt_bas_set_battery_level(ctx.current_soc);
|
||||
|
||||
if (running) {
|
||||
if ((err == -EAGAIN) || (err == -ENOTCONN)) {
|
||||
LOG_INF("BAS notify deferred (%d)", err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
|
||||
if (!ble_ready) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = bt_bas_set_battery_level(current_soc);
|
||||
if (err) {
|
||||
LOG_WRN("bt_bas_set_battery_level failed (%d)", err);
|
||||
return err;
|
||||
@@ -47,21 +67,30 @@ static int module_start(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
running = false;
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!ctx.ble_ready) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return bas_update_level();
|
||||
}
|
||||
|
||||
static int do_stop(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool handle_bat_state_event(const struct bat_state_event *event)
|
||||
{
|
||||
current_soc = event->soc;
|
||||
ctx.current_soc = event->soc;
|
||||
|
||||
if (running && ble_ready) {
|
||||
int err = bt_bas_set_battery_level(current_soc);
|
||||
|
||||
if (err) {
|
||||
LOG_WRN("bt_bas_set_battery_level failed (%d)", err);
|
||||
}
|
||||
if (module_lifecycle_is_running(&ctx.lc) && ctx.ble_ready) {
|
||||
(void)bas_update_level();
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -75,38 +104,18 @@ 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);
|
||||
int err;
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) {
|
||||
ble_ready = true;
|
||||
ctx.ble_ready = true;
|
||||
|
||||
if (running) {
|
||||
err = bt_bas_set_battery_level(current_soc);
|
||||
if (err) {
|
||||
LOG_WRN("bt_bas_set_battery_level failed (%d)", err);
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
(void)bas_update_level();
|
||||
}
|
||||
}
|
||||
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
@@ -7,17 +7,17 @@
|
||||
|
||||
#define MODULE ble_hid_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <caf/events/ble_common_event.h>
|
||||
#include <bluetooth/services/hids.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "hid_led_event.h"
|
||||
#include "hid_channel_state_event.h"
|
||||
#include "hid_report_sent_event.h"
|
||||
#include "hid_transport_state_event.h"
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "module_lifecycle.h"
|
||||
#include "set_protocol_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
@@ -40,15 +40,40 @@ BT_HIDS_DEF(hids_obj,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE,
|
||||
BLE_HID_KEYS_LED_REPORT_SIZE);
|
||||
|
||||
static struct bt_conn *active_conn;
|
||||
static struct in_flight_report in_flight;
|
||||
static enum keyboard_protocol_mode protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool secured;
|
||||
static bool keyboard_report_notify_enabled;
|
||||
static bool consumer_report_notify_enabled;
|
||||
static bool boot_keyboard_notify_enabled;
|
||||
struct ble_hid_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
struct bt_conn *active_conn;
|
||||
struct in_flight_report in_flight;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
bool secured;
|
||||
bool keyboard_report_notify_enabled;
|
||||
bool consumer_report_notify_enabled;
|
||||
bool boot_keyboard_notify_enabled;
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(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_hid_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
};
|
||||
|
||||
static const uint8_t hid_report_desc[] = {
|
||||
0x05, 0x01, /* Usage Page (Generic Desktop) */
|
||||
@@ -98,54 +123,27 @@ static const uint8_t hid_report_desc[] = {
|
||||
0xC0 /* End Collection */
|
||||
};
|
||||
|
||||
static void submit_set_protocol_event(void)
|
||||
static void submit_ble_transport_state_event(void)
|
||||
{
|
||||
struct set_protocol_event *event = new_set_protocol_event();
|
||||
bool ready = module_lifecycle_is_running(&ctx.lc) &&
|
||||
ctx.secured && (ctx.active_conn != NULL);
|
||||
uint8_t report_ready_bm = 0U;
|
||||
|
||||
event->transport = HID_TRANSPORT_BLE;
|
||||
event->protocol_mode = protocol_mode;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
if (ready && ((ctx.protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
ctx.boot_keyboard_notify_enabled :
|
||||
ctx.keyboard_report_notify_enabled)) {
|
||||
report_ready_bm |= BIT(KEYBOARD_REPORT_TYPE_KEYS);
|
||||
}
|
||||
|
||||
static void submit_hid_led_event(uint8_t led_bm)
|
||||
{
|
||||
struct hid_led_event *event = new_hid_led_event();
|
||||
|
||||
event->transport = HID_TRANSPORT_BLE;
|
||||
event->led_bm = led_bm;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
if (ready && (ctx.protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
|
||||
ctx.consumer_report_notify_enabled) {
|
||||
report_ready_bm |= BIT(KEYBOARD_REPORT_TYPE_CONSUMER);
|
||||
}
|
||||
|
||||
static void submit_transport_state_event(void)
|
||||
{
|
||||
struct hid_transport_state_event *event = new_hid_transport_state_event();
|
||||
bool ready = running && secured && (active_conn != NULL);
|
||||
|
||||
event->transport = HID_TRANSPORT_BLE;
|
||||
event->ready = ready;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->keys_ready = ready &&
|
||||
((protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
boot_keyboard_notify_enabled :
|
||||
keyboard_report_notify_enabled);
|
||||
event->consumer_ready = ready &&
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT) &&
|
||||
consumer_report_notify_enabled;
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_hid_report_sent_event(enum keyboard_report_type report_type,
|
||||
uint16_t sequence, bool error)
|
||||
{
|
||||
struct hid_report_sent_event *event = new_hid_report_sent_event();
|
||||
|
||||
event->transport = HID_TRANSPORT_BLE;
|
||||
event->report_type = report_type;
|
||||
event->sequence = sequence;
|
||||
event->error = error;
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
submit_hid_channel_state_event(
|
||||
HID_SEND_CH_BLE_SHARED,
|
||||
report_ready_bm,
|
||||
ctx.protocol_mode);
|
||||
}
|
||||
|
||||
static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_evt evt)
|
||||
@@ -153,18 +151,18 @@ static void input_report_notify_handler(uint8_t report_id, enum bt_hids_notify_e
|
||||
bool enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
|
||||
|
||||
if (report_id == BLE_HID_KEYS_REPORT_ID) {
|
||||
keyboard_report_notify_enabled = enabled;
|
||||
ctx.keyboard_report_notify_enabled = enabled;
|
||||
} else if (report_id == BLE_HID_CONSUMER_REPORT_ID) {
|
||||
consumer_report_notify_enabled = enabled;
|
||||
ctx.consumer_report_notify_enabled = enabled;
|
||||
}
|
||||
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static void boot_keyboard_notify_handler(enum bt_hids_notify_evt evt)
|
||||
{
|
||||
boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
|
||||
submit_transport_state_event();
|
||||
ctx.boot_keyboard_notify_enabled = (evt == BT_HIDS_CCCD_EVT_NOTIFY_ENABLED);
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
||||
@@ -172,12 +170,14 @@ static void hid_report_complete_cb(struct bt_conn *conn, void *user_data)
|
||||
ARG_UNUSED(conn);
|
||||
ARG_UNUSED(user_data);
|
||||
|
||||
if (!in_flight.active) {
|
||||
if (!ctx.in_flight.active) {
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hid_report_sent_event(in_flight.report_type, in_flight.sequence, false);
|
||||
in_flight.active = false;
|
||||
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
|
||||
ctx.in_flight.report_type,
|
||||
ctx.in_flight.sequence, false);
|
||||
ctx.in_flight.active = false;
|
||||
}
|
||||
|
||||
static void keyboard_led_report_common(struct bt_hids_rep *rep, bool write)
|
||||
@@ -186,7 +186,7 @@ static void keyboard_led_report_common(struct bt_hids_rep *rep, bool write)
|
||||
return;
|
||||
}
|
||||
|
||||
submit_hid_led_event(rep->data[0]);
|
||||
submit_hid_led_event(HID_TRANSPORT_BLE, rep->data[0]);
|
||||
}
|
||||
|
||||
static void keyboard_led_report_handler(struct bt_hids_rep *rep,
|
||||
@@ -213,22 +213,22 @@ static void pm_evt_handler(enum bt_hids_pm_evt evt, struct bt_conn *conn)
|
||||
|
||||
switch (evt) {
|
||||
case BT_HIDS_PM_EVT_BOOT_MODE_ENTERED:
|
||||
protocol_mode = KEYBOARD_PROTOCOL_MODE_BOOT;
|
||||
ctx.protocol_mode = KEYBOARD_PROTOCOL_MODE_BOOT;
|
||||
break;
|
||||
|
||||
case BT_HIDS_PM_EVT_REPORT_MODE_ENTERED:
|
||||
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
ctx.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
submit_set_protocol_event();
|
||||
submit_transport_state_event();
|
||||
submit_set_protocol_event(HID_TRANSPORT_BLE, ctx.protocol_mode);
|
||||
submit_ble_transport_state_event();
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
struct bt_hids_init_param hids_init_param = { 0 };
|
||||
struct bt_hids_inp_rep *input_report;
|
||||
@@ -265,38 +265,38 @@ static int module_init(void)
|
||||
return bt_hids_init(&hids_obj, &hids_init_param);
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
in_flight.active = false;
|
||||
running = false;
|
||||
submit_transport_state_event();
|
||||
ctx.in_flight.active = false;
|
||||
submit_ble_transport_state_event();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void reset_connection_state(void)
|
||||
{
|
||||
active_conn = NULL;
|
||||
secured = false;
|
||||
keyboard_report_notify_enabled = false;
|
||||
consumer_report_notify_enabled = false;
|
||||
boot_keyboard_notify_enabled = false;
|
||||
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
in_flight.active = false;
|
||||
ctx.active_conn = NULL;
|
||||
ctx.secured = false;
|
||||
ctx.keyboard_report_notify_enabled = false;
|
||||
ctx.consumer_report_notify_enabled = false;
|
||||
ctx.boot_keyboard_notify_enabled = false;
|
||||
ctx.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
ctx.in_flight.active = false;
|
||||
}
|
||||
|
||||
static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
@@ -305,31 +305,31 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
|
||||
switch (event->state) {
|
||||
case PEER_STATE_CONNECTED:
|
||||
if (active_conn != NULL) {
|
||||
if (ctx.active_conn != NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
active_conn = event->id;
|
||||
protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
submit_set_protocol_event();
|
||||
ctx.active_conn = event->id;
|
||||
ctx.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
submit_set_protocol_event(HID_TRANSPORT_BLE, ctx.protocol_mode);
|
||||
err = bt_hids_connected(&hids_obj, event->id);
|
||||
if (err) {
|
||||
LOG_ERR("bt_hids_connected failed (%d)", err);
|
||||
}
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
case PEER_STATE_SECURED:
|
||||
if (active_conn != event->id) {
|
||||
if (ctx.active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
secured = true;
|
||||
submit_transport_state_event();
|
||||
ctx.secured = true;
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
if (active_conn != event->id) {
|
||||
if (ctx.active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -339,7 +339,7 @@ static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
submit_transport_state_event();
|
||||
submit_ble_transport_state_event();
|
||||
return false;
|
||||
|
||||
default:
|
||||
@@ -351,33 +351,35 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || (event->transport != HID_TRANSPORT_BLE) || in_flight.active) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc) ||
|
||||
(event->channel != HID_SEND_CH_BLE_SHARED) ||
|
||||
ctx.in_flight.active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((active_conn == NULL) || !secured) {
|
||||
if ((ctx.active_conn == NULL) || !ctx.secured) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||
if (event->protocol_mode != protocol_mode) {
|
||||
if (event->protocol_mode != ctx.protocol_mode) {
|
||||
LOG_WRN("Drop BLE keys report due to protocol mismatch");
|
||||
return false;
|
||||
}
|
||||
|
||||
in_flight.active = true;
|
||||
in_flight.report_type = event->report_type;
|
||||
in_flight.sequence = event->sequence;
|
||||
ctx.in_flight.active = true;
|
||||
ctx.in_flight.report_type = event->report_type;
|
||||
ctx.in_flight.sequence = event->sequence;
|
||||
|
||||
if (protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||
if (ctx.protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||
err = bt_hids_boot_kb_inp_rep_send(&hids_obj,
|
||||
active_conn,
|
||||
ctx.active_conn,
|
||||
event->dyndata.data,
|
||||
(uint8_t)event->dyndata.size,
|
||||
hid_report_complete_cb);
|
||||
} else {
|
||||
err = bt_hids_inp_rep_send(&hids_obj,
|
||||
active_conn,
|
||||
ctx.active_conn,
|
||||
BLE_HID_KEYS_REPORT_IDX,
|
||||
event->dyndata.data,
|
||||
(uint8_t)event->dyndata.size,
|
||||
@@ -385,9 +387,10 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
}
|
||||
|
||||
if (err) {
|
||||
in_flight.active = false;
|
||||
ctx.in_flight.active = false;
|
||||
LOG_WRN("BLE keyboard report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
|
||||
KEYBOARD_REPORT_TYPE_KEYS,
|
||||
event->sequence, true);
|
||||
}
|
||||
|
||||
@@ -395,25 +398,26 @@ static bool handle_hid_tx_report_event(const struct hid_tx_report_event *event)
|
||||
}
|
||||
|
||||
if (event->report_type == KEYBOARD_REPORT_TYPE_CONSUMER) {
|
||||
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_REPORT) {
|
||||
if (ctx.protocol_mode != KEYBOARD_PROTOCOL_MODE_REPORT) {
|
||||
LOG_WRN("Drop BLE consumer report in boot mode");
|
||||
return false;
|
||||
}
|
||||
|
||||
in_flight.active = true;
|
||||
in_flight.report_type = event->report_type;
|
||||
in_flight.sequence = event->sequence;
|
||||
ctx.in_flight.active = true;
|
||||
ctx.in_flight.report_type = event->report_type;
|
||||
ctx.in_flight.sequence = event->sequence;
|
||||
|
||||
err = bt_hids_inp_rep_send(&hids_obj,
|
||||
active_conn,
|
||||
ctx.active_conn,
|
||||
BLE_HID_CONSUMER_REPORT_IDX,
|
||||
event->dyndata.data,
|
||||
(uint8_t)event->dyndata.size,
|
||||
hid_report_complete_cb);
|
||||
if (err) {
|
||||
in_flight.active = false;
|
||||
ctx.in_flight.active = false;
|
||||
LOG_WRN("BLE consumer report submit failed (%d)", err);
|
||||
submit_hid_report_sent_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
submit_hid_report_sent_event(HID_SEND_CH_BLE_SHARED,
|
||||
KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
event->sequence, true);
|
||||
}
|
||||
}
|
||||
@@ -433,48 +437,9 @@ 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);
|
||||
int err;
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -487,5 +452,3 @@ APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_tx_report_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
324
src/ble_nus_module.c
Normal file
324
src/ble_nus_module.c
Normal file
@@ -0,0 +1,324 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE ble_nus_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
#include <caf/events/ble_common_event.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/services/nus.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "module_lifecycle.h"
|
||||
#include "proto_rx_event.h"
|
||||
#include "proto_transport_state_event.h"
|
||||
#include "proto_tx_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
enum ble_nus_business_state {
|
||||
BLE_NUS_STACK_OFFLINE = 0,
|
||||
BLE_NUS_IDLE,
|
||||
BLE_NUS_WAIT_NOTIFY,
|
||||
BLE_NUS_SESSION_READY,
|
||||
};
|
||||
|
||||
struct ble_nus_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
enum ble_nus_business_state business;
|
||||
struct bt_conn *active_conn;
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(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_nus_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.business = BLE_NUS_STACK_OFFLINE,
|
||||
.active_conn = NULL,
|
||||
};
|
||||
|
||||
static const char *business_state_name(enum ble_nus_business_state state)
|
||||
{
|
||||
switch (state) {
|
||||
case BLE_NUS_STACK_OFFLINE:
|
||||
return "STACK_OFFLINE";
|
||||
case BLE_NUS_IDLE:
|
||||
return "IDLE";
|
||||
case BLE_NUS_WAIT_NOTIFY:
|
||||
return "WAIT_NOTIFY";
|
||||
case BLE_NUS_SESSION_READY:
|
||||
return "SESSION_READY";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *link_state_name(enum proto_transport_link_state state)
|
||||
{
|
||||
switch (state) {
|
||||
case PROTO_TRANSPORT_LINK_DOWN:
|
||||
return "DOWN";
|
||||
case PROTO_TRANSPORT_LINK_READY:
|
||||
return "READY";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static bool lifecycle_is_ready(void)
|
||||
{
|
||||
return module_lifecycle_is_running(&ctx.lc);
|
||||
}
|
||||
|
||||
static enum proto_transport_link_state transport_link_state_get(void)
|
||||
{
|
||||
return (lifecycle_is_ready() &&
|
||||
(ctx.business == BLE_NUS_SESSION_READY)) ?
|
||||
PROTO_TRANSPORT_LINK_READY :
|
||||
PROTO_TRANSPORT_LINK_DOWN;
|
||||
}
|
||||
|
||||
static void state_reconcile(enum module_lifecycle old_lifecycle,
|
||||
enum ble_nus_business_state old_business)
|
||||
{
|
||||
enum proto_transport_link_state old_link =
|
||||
((old_lifecycle == LC_RUNNING) &&
|
||||
(old_business == BLE_NUS_SESSION_READY)) ?
|
||||
PROTO_TRANSPORT_LINK_READY :
|
||||
PROTO_TRANSPORT_LINK_DOWN;
|
||||
enum proto_transport_link_state new_link = transport_link_state_get();
|
||||
|
||||
if (old_link != new_link) {
|
||||
submit_proto_transport_state_event(PROTO_TRANSPORT_BLE_NUS,
|
||||
new_link);
|
||||
}
|
||||
}
|
||||
|
||||
static void business_state_set(enum ble_nus_business_state new_state)
|
||||
{
|
||||
enum module_lifecycle old_lifecycle = ctx.lc.state;
|
||||
enum ble_nus_business_state old_business = ctx.business;
|
||||
|
||||
if (ctx.business == new_state) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INF("BLE NUS business %s -> %s",
|
||||
business_state_name(ctx.business),
|
||||
business_state_name(new_state));
|
||||
ctx.business = new_state;
|
||||
state_reconcile(old_lifecycle, old_business);
|
||||
}
|
||||
|
||||
static void business_state_set_stack_ready(void)
|
||||
{
|
||||
if (ctx.business == BLE_NUS_STACK_OFFLINE) {
|
||||
business_state_set(BLE_NUS_IDLE);
|
||||
}
|
||||
}
|
||||
|
||||
static void business_state_set_from_notify(bool enabled)
|
||||
{
|
||||
if (ctx.business == BLE_NUS_STACK_OFFLINE) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx.active_conn == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
business_state_set(enabled ? BLE_NUS_SESSION_READY :
|
||||
BLE_NUS_WAIT_NOTIFY);
|
||||
}
|
||||
|
||||
static void notif_enabled(bool enabled, void *ctx_ptr)
|
||||
{
|
||||
ARG_UNUSED(ctx_ptr);
|
||||
|
||||
LOG_INF("BLE NUS TX notify %s", enabled ? "enabled" : "disabled");
|
||||
business_state_set_from_notify(enabled);
|
||||
}
|
||||
|
||||
static void received(struct bt_conn *conn, const void *data, uint16_t len,
|
||||
void *ctx_ptr)
|
||||
{
|
||||
ARG_UNUSED(ctx_ptr);
|
||||
|
||||
if (!lifecycle_is_ready() || (ctx.business == BLE_NUS_STACK_OFFLINE) ||
|
||||
(conn != ctx.active_conn)) {
|
||||
LOG_WRN("BLE NUS drop RX len:%u lc:%s business:%s active_conn:%p conn:%p link:%s",
|
||||
len, module_lifecycle_name(ctx.lc.state),
|
||||
business_state_name(ctx.business),
|
||||
(void *)ctx.active_conn, (void *)conn,
|
||||
link_state_name(transport_link_state_get()));
|
||||
return;
|
||||
}
|
||||
|
||||
if ((len < PROTO_FRAME_HEADER_SIZE) || (len > PROTO_MAX_FRAME_LEN)) {
|
||||
LOG_WRN("BLE NUS drop invalid framed RX len:%u", len);
|
||||
return;
|
||||
}
|
||||
|
||||
(void)submit_proto_rx_event(PROTO_TRANSPORT_BLE_NUS, data, len);
|
||||
}
|
||||
|
||||
static struct bt_nus_cb nus_listener = {
|
||||
.notif_enabled = notif_enabled,
|
||||
.received = received,
|
||||
};
|
||||
|
||||
static void reset_connection_state(void)
|
||||
{
|
||||
ctx.active_conn = NULL;
|
||||
|
||||
if (ctx.business != BLE_NUS_STACK_OFFLINE) {
|
||||
business_state_set(BLE_NUS_IDLE);
|
||||
}
|
||||
}
|
||||
|
||||
static int do_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = bt_nus_cb_register(&nus_listener, NULL);
|
||||
if (err) {
|
||||
LOG_ERR("bt_nus_cb_register failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
ctx.business = BLE_NUS_STACK_OFFLINE;
|
||||
ctx.active_conn = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_start(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_stop(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int apply_lifecycle(enum module_lifecycle target)
|
||||
{
|
||||
enum module_lifecycle old_lifecycle = ctx.lc.state;
|
||||
enum ble_nus_business_state old_business = ctx.business;
|
||||
int err = module_set_lifecycle(&ctx.lc, target);
|
||||
|
||||
if (!err) {
|
||||
state_reconcile(old_lifecycle, old_business);
|
||||
} else {
|
||||
LOG_WRN("BLE NUS lifecycle change failed target:%s err:%d",
|
||||
module_lifecycle_name(target), err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
{
|
||||
switch (event->state) {
|
||||
case PEER_STATE_CONNECTED:
|
||||
if (ctx.active_conn != NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx.active_conn = event->id;
|
||||
if (ctx.business != BLE_NUS_STACK_OFFLINE) {
|
||||
business_state_set(BLE_NUS_WAIT_NOTIFY);
|
||||
}
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
if (ctx.active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
return false;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_proto_tx_event(const struct proto_tx_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (event->transport != PROTO_TRANSPORT_BLE_NUS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((transport_link_state_get() != PROTO_TRANSPORT_LINK_READY) ||
|
||||
(ctx.active_conn == NULL)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = bt_nus_send(ctx.active_conn, event->dyndata.data,
|
||||
(uint16_t)event->dyndata.size);
|
||||
if (err) {
|
||||
LOG_WRN("bt_nus_send failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_proto_tx_event(aeh)) {
|
||||
return handle_proto_tx_event(cast_proto_tx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_ble_peer_event(aeh)) {
|
||||
return handle_ble_peer_event(cast_ble_peer_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event =
|
||||
cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
(void)apply_lifecycle(LC_RUNNING);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) {
|
||||
business_state_set_stack_ready();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, proto_tx_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event);
|
||||
@@ -1,308 +0,0 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE ble_serial_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <caf/events/ble_common_event.h>
|
||||
#include <zephyr/bluetooth/att.h>
|
||||
#include <zephyr/bluetooth/conn.h>
|
||||
#include <zephyr/bluetooth/gatt.h>
|
||||
#include <zephyr/bluetooth/uuid.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "ble_serial_rx_event.h"
|
||||
#include "ble_serial_tx_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define BLE_SERIAL_RX_MAX_LEN 64U
|
||||
#define BLE_SERIAL_MIN_PAYLOAD_LEN 32U
|
||||
#define BLE_SERIAL_NOTIFY_OVERHEAD 3U
|
||||
#define BLE_SERIAL_SERVICE_UUID_VAL \
|
||||
BT_UUID_128_ENCODE(0x0b7f6000, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
|
||||
#define BLE_SERIAL_RX_UUID_VAL \
|
||||
BT_UUID_128_ENCODE(0x0b7f6001, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
|
||||
#define BLE_SERIAL_TX_UUID_VAL \
|
||||
BT_UUID_128_ENCODE(0x0b7f6002, 0x38d2, 0x4f62, 0x8f6f, 0x36c4fd73a110)
|
||||
|
||||
static struct bt_conn *active_conn;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool ble_ready;
|
||||
static bool secured;
|
||||
static bool tx_notify_enabled;
|
||||
|
||||
static const struct bt_uuid_128 ble_serial_service_uuid =
|
||||
BT_UUID_INIT_128(BLE_SERIAL_SERVICE_UUID_VAL);
|
||||
static const struct bt_uuid_128 ble_serial_rx_uuid =
|
||||
BT_UUID_INIT_128(BLE_SERIAL_RX_UUID_VAL);
|
||||
static const struct bt_uuid_128 ble_serial_tx_uuid =
|
||||
BT_UUID_INIT_128(BLE_SERIAL_TX_UUID_VAL);
|
||||
|
||||
static void submit_ble_serial_rx_event(const uint8_t *data, size_t len)
|
||||
{
|
||||
struct ble_serial_rx_event *event = new_ble_serial_rx_event(len);
|
||||
|
||||
memcpy(event->dyndata.data, data, len);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void tx_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value)
|
||||
{
|
||||
ARG_UNUSED(attr);
|
||||
|
||||
tx_notify_enabled = (value == BT_GATT_CCC_NOTIFY);
|
||||
LOG_INF("BLE serial TX notify %s", tx_notify_enabled ? "enabled" : "disabled");
|
||||
}
|
||||
|
||||
static ssize_t rx_write_handler(struct bt_conn *conn,
|
||||
const struct bt_gatt_attr *attr,
|
||||
const void *buf,
|
||||
uint16_t len,
|
||||
uint16_t offset,
|
||||
uint8_t flags)
|
||||
{
|
||||
ARG_UNUSED(attr);
|
||||
ARG_UNUSED(flags);
|
||||
|
||||
if (!running || !ble_ready || !secured || (conn != active_conn)) {
|
||||
return BT_GATT_ERR(BT_ATT_ERR_WRITE_REQ_REJECTED);
|
||||
}
|
||||
|
||||
if (offset != 0U) {
|
||||
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
|
||||
}
|
||||
|
||||
if ((buf == NULL) || (len == 0U) || (len > BLE_SERIAL_RX_MAX_LEN)) {
|
||||
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
|
||||
}
|
||||
|
||||
submit_ble_serial_rx_event(buf, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
BT_GATT_SERVICE_DEFINE(ble_serial_svc,
|
||||
BT_GATT_PRIMARY_SERVICE(&ble_serial_service_uuid.uuid),
|
||||
BT_GATT_CHARACTERISTIC(&ble_serial_rx_uuid.uuid,
|
||||
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
|
||||
BT_GATT_PERM_WRITE_ENCRYPT,
|
||||
NULL, rx_write_handler, NULL),
|
||||
BT_GATT_CHARACTERISTIC(&ble_serial_tx_uuid.uuid,
|
||||
BT_GATT_CHRC_NOTIFY,
|
||||
BT_GATT_PERM_NONE,
|
||||
NULL, NULL, NULL),
|
||||
BT_GATT_CCC(tx_ccc_cfg_changed,
|
||||
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_ENCRYPT)
|
||||
);
|
||||
|
||||
static void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
|
||||
{
|
||||
if (conn != active_conn) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INF("BLE serial MTU updated tx:%u rx:%u", tx, rx);
|
||||
}
|
||||
|
||||
static struct bt_gatt_cb gatt_callbacks = {
|
||||
.att_mtu_updated = mtu_updated,
|
||||
};
|
||||
|
||||
static size_t notify_payload_max(void)
|
||||
{
|
||||
uint16_t mtu;
|
||||
|
||||
if (active_conn == NULL) {
|
||||
return 0U;
|
||||
}
|
||||
|
||||
mtu = bt_gatt_get_mtu(active_conn);
|
||||
if (mtu <= BLE_SERIAL_NOTIFY_OVERHEAD) {
|
||||
return 0U;
|
||||
}
|
||||
|
||||
return (size_t)(mtu - BLE_SERIAL_NOTIFY_OVERHEAD);
|
||||
}
|
||||
|
||||
static void reset_connection_state(void)
|
||||
{
|
||||
active_conn = NULL;
|
||||
secured = false;
|
||||
tx_notify_enabled = false;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
bt_gatt_cb_register(&gatt_callbacks);
|
||||
reset_connection_state();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
running = false;
|
||||
tx_notify_enabled = false;
|
||||
}
|
||||
|
||||
static bool handle_ble_peer_event(const struct ble_peer_event *event)
|
||||
{
|
||||
switch (event->state) {
|
||||
case PEER_STATE_CONNECTED:
|
||||
if (active_conn != NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
active_conn = event->id;
|
||||
secured = false;
|
||||
tx_notify_enabled = false;
|
||||
return false;
|
||||
|
||||
case PEER_STATE_SECURED:
|
||||
if (active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
secured = true;
|
||||
return false;
|
||||
|
||||
case PEER_STATE_DISCONNECTED:
|
||||
if (active_conn != event->id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
reset_connection_state();
|
||||
return false;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_ble_serial_tx_event(const struct ble_serial_tx_event *event)
|
||||
{
|
||||
size_t payload_max;
|
||||
int err;
|
||||
|
||||
if (!running || !ble_ready || (active_conn == NULL) || !secured || !tx_notify_enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
payload_max = notify_payload_max();
|
||||
if (payload_max < BLE_SERIAL_MIN_PAYLOAD_LEN) {
|
||||
LOG_WRN("BLE serial MTU payload too small:%u", (uint32_t)payload_max);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->dyndata.size > payload_max) {
|
||||
LOG_WRN("BLE serial TX too large len:%u max:%u",
|
||||
(uint32_t)event->dyndata.size, (uint32_t)payload_max);
|
||||
return false;
|
||||
}
|
||||
|
||||
err = bt_gatt_notify(active_conn, &ble_serial_svc.attrs[4],
|
||||
event->dyndata.data,
|
||||
(uint16_t)event->dyndata.size);
|
||||
if (err) {
|
||||
LOG_WRN("bt_gatt_notify failed (%d)", err);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_ble_serial_tx_event(aeh)) {
|
||||
return handle_ble_serial_tx_event(cast_ble_serial_tx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_ble_peer_event(aeh)) {
|
||||
return handle_ble_peer_event(cast_ble_peer_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
int err;
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (check_state(event, MODULE_ID(ble_state), MODULE_STATE_READY)) {
|
||||
ble_ready = true;
|
||||
if (running) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else if (ble_ready) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, ble_serial_tx_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, ble_peer_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -1,299 +0,0 @@
|
||||
#include <errno.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE cdc_wrapper_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "cdc_proto_tx_event.h"
|
||||
#include "protocol_module.h"
|
||||
#include "usb_cdc_rx_event.h"
|
||||
#include "usb_cdc_tx_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
#define CDC_WRAPPER_HEAD1 0xAAU
|
||||
#define CDC_WRAPPER_HEAD2 0x55U
|
||||
#define CDC_WRAPPER_MAX_PAYLOAD_LEN 64U
|
||||
#define CDC_WRAPPER_MAX_FRAME_LEN (2U + 1U + 1U + CDC_WRAPPER_MAX_PAYLOAD_LEN + 1U)
|
||||
|
||||
enum frame_parse_state {
|
||||
FRAME_PARSE_HEAD1,
|
||||
FRAME_PARSE_HEAD2,
|
||||
FRAME_PARSE_LEN,
|
||||
FRAME_PARSE_TYPE,
|
||||
FRAME_PARSE_PAYLOAD,
|
||||
FRAME_PARSE_CHECKSUM,
|
||||
};
|
||||
|
||||
struct cdc_frame_parser {
|
||||
enum frame_parse_state state;
|
||||
uint8_t len;
|
||||
uint8_t type;
|
||||
uint8_t checksum;
|
||||
uint8_t payload[CDC_WRAPPER_MAX_PAYLOAD_LEN];
|
||||
size_t payload_pos;
|
||||
};
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static struct cdc_frame_parser parser;
|
||||
|
||||
static void parser_reset(void)
|
||||
{
|
||||
parser.state = FRAME_PARSE_HEAD1;
|
||||
parser.len = 0U;
|
||||
parser.type = 0U;
|
||||
parser.checksum = 0U;
|
||||
parser.payload_pos = 0U;
|
||||
}
|
||||
|
||||
static uint8_t frame_checksum(uint8_t len, uint8_t type,
|
||||
const uint8_t *payload, size_t payload_len)
|
||||
{
|
||||
uint8_t checksum = CDC_WRAPPER_HEAD1 ^ CDC_WRAPPER_HEAD2 ^ len ^ type;
|
||||
|
||||
for (size_t i = 0; i < payload_len; i++) {
|
||||
checksum ^= payload[i];
|
||||
}
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
static void submit_tx_frame(uint8_t type, const uint8_t *payload, size_t payload_len)
|
||||
{
|
||||
struct usb_cdc_tx_event *event;
|
||||
size_t frame_len = 2U + 1U + 1U + payload_len + 1U;
|
||||
|
||||
event = new_usb_cdc_tx_event(frame_len);
|
||||
event->dyndata.data[0] = CDC_WRAPPER_HEAD1;
|
||||
event->dyndata.data[1] = CDC_WRAPPER_HEAD2;
|
||||
event->dyndata.data[2] = (uint8_t)payload_len;
|
||||
event->dyndata.data[3] = type;
|
||||
memcpy(&event->dyndata.data[4], payload, payload_len);
|
||||
event->dyndata.data[4U + payload_len] =
|
||||
frame_checksum((uint8_t)payload_len, type, payload, payload_len);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void process_complete_frame(void)
|
||||
{
|
||||
uint8_t rsp_type;
|
||||
uint8_t rsp_payload[CDC_WRAPPER_MAX_PAYLOAD_LEN];
|
||||
size_t rsp_payload_len = 0U;
|
||||
int err;
|
||||
|
||||
err = protocol_module_process_cdc_packet(parser.type,
|
||||
parser.payload,
|
||||
parser.payload_pos,
|
||||
&rsp_type,
|
||||
rsp_payload,
|
||||
sizeof(rsp_payload),
|
||||
&rsp_payload_len);
|
||||
if (err == -ENOTSUP) {
|
||||
LOG_WRN("Ignore unsupported CDC packet type:0x%02x", parser.type);
|
||||
return;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
LOG_WRN("Protocol processing failed (%d)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INF("CDC response type:0x%02x len:%u",
|
||||
rsp_type, (uint32_t)rsp_payload_len);
|
||||
submit_tx_frame(rsp_type, rsp_payload, rsp_payload_len);
|
||||
}
|
||||
|
||||
static void consume_byte(uint8_t byte)
|
||||
{
|
||||
switch (parser.state) {
|
||||
case FRAME_PARSE_HEAD1:
|
||||
if (byte == CDC_WRAPPER_HEAD1) {
|
||||
parser.state = FRAME_PARSE_HEAD2;
|
||||
}
|
||||
break;
|
||||
|
||||
case FRAME_PARSE_HEAD2:
|
||||
if (byte == CDC_WRAPPER_HEAD2) {
|
||||
parser.state = FRAME_PARSE_LEN;
|
||||
} else if (byte != CDC_WRAPPER_HEAD1) {
|
||||
parser.state = FRAME_PARSE_HEAD1;
|
||||
}
|
||||
break;
|
||||
|
||||
case FRAME_PARSE_LEN:
|
||||
if (byte > CDC_WRAPPER_MAX_PAYLOAD_LEN) {
|
||||
LOG_WRN("Drop CDC frame with invalid len:%u", byte);
|
||||
parser_reset();
|
||||
break;
|
||||
}
|
||||
|
||||
parser.len = byte;
|
||||
parser.payload_pos = 0U;
|
||||
parser.state = FRAME_PARSE_TYPE;
|
||||
break;
|
||||
|
||||
case FRAME_PARSE_TYPE:
|
||||
parser.type = byte;
|
||||
parser.state = (parser.len == 0U) ? FRAME_PARSE_CHECKSUM :
|
||||
FRAME_PARSE_PAYLOAD;
|
||||
break;
|
||||
|
||||
case FRAME_PARSE_PAYLOAD:
|
||||
parser.payload[parser.payload_pos++] = byte;
|
||||
if (parser.payload_pos >= parser.len) {
|
||||
parser.state = FRAME_PARSE_CHECKSUM;
|
||||
}
|
||||
break;
|
||||
|
||||
case FRAME_PARSE_CHECKSUM:
|
||||
if (byte != frame_checksum(parser.len, parser.type,
|
||||
parser.payload, parser.payload_pos)) {
|
||||
LOG_WRN("Drop CDC frame with invalid checksum");
|
||||
parser_reset();
|
||||
break;
|
||||
}
|
||||
|
||||
process_complete_frame();
|
||||
parser_reset();
|
||||
break;
|
||||
|
||||
default:
|
||||
parser_reset();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_usb_cdc_rx_event(const struct usb_cdc_rx_event *event)
|
||||
{
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
consume_byte(event->dyndata.data[i]);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_cdc_proto_tx_event(const struct cdc_proto_tx_event *event)
|
||||
{
|
||||
if (!running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->dyndata.size > CDC_WRAPPER_MAX_PAYLOAD_LEN) {
|
||||
LOG_WRN("Drop CDC proto TX len:%u max:%u",
|
||||
(uint32_t)event->dyndata.size,
|
||||
(uint32_t)CDC_WRAPPER_MAX_PAYLOAD_LEN);
|
||||
return false;
|
||||
}
|
||||
|
||||
submit_tx_frame(event->type, event->dyndata.data, event->dyndata.size);
|
||||
return false;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
parser_reset();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
{
|
||||
if (running) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
|
||||
running = false;
|
||||
parser_reset();
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_usb_cdc_rx_event(aeh)) {
|
||||
return handle_usb_cdc_rx_event(cast_usb_cdc_rx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_cdc_proto_tx_event(aeh)) {
|
||||
return handle_cdc_proto_tx_event(cast_cdc_proto_tx_event(aeh));
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, cdc_proto_tx_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_cdc_rx_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#define MODULE display_module
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/ble_common_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
|
||||
#include <lvgl_zephyr.h>
|
||||
@@ -14,9 +15,18 @@
|
||||
#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"
|
||||
#include "mode_switch_event.h"
|
||||
#include "settings_mode_event.h"
|
||||
#include "settings_view_event.h"
|
||||
#include "theme_rgb_update_event.h"
|
||||
#include "theme_color.h"
|
||||
#include "ui/ui_page.h"
|
||||
#include "ui/ui_main.h"
|
||||
#include "ui/ui_settings.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -24,42 +34,82 @@ BUILD_ASSERT(DT_HAS_CHOSEN(zephyr_display), "Missing zephyr,display chosen node"
|
||||
BUILD_ASSERT(DT_NODE_HAS_STATUS(DT_ALIAS(backlight), okay),
|
||||
"Missing backlight alias");
|
||||
|
||||
static const struct device *const display_dev =
|
||||
DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
|
||||
static const struct device *const backlight_dev =
|
||||
DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight)));
|
||||
static const uint32_t backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight));
|
||||
static struct ui_main_model ui_model = {
|
||||
.theme_color = LV_COLOR_MAKE(0x4C, 0x9E, 0xF5),
|
||||
struct display_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
const struct device *display_dev;
|
||||
const struct device *backlight_dev;
|
||||
uint32_t backlight_idx;
|
||||
struct ui_main_model ui_model;
|
||||
struct ui_settings_page *settings_page;
|
||||
bool settings_active;
|
||||
bool lvgl_initialized;
|
||||
char date_text[DATETIME_EVENT_DATE_TEXT_LEN];
|
||||
char time_text[DATETIME_EVENT_TIME_TEXT_LEN];
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
|
||||
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
||||
.mode = ML_MODE_POWER,
|
||||
.stopped_state = MODULE_STATE_STANDBY,
|
||||
};
|
||||
|
||||
static const struct module_lifecycle_ops lifecycle_ops = {
|
||||
.do_init = do_init,
|
||||
.do_start = do_start,
|
||||
.do_stop = do_stop,
|
||||
};
|
||||
|
||||
static struct display_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display)),
|
||||
.backlight_dev = DEVICE_DT_GET(DT_PARENT(DT_ALIAS(backlight))),
|
||||
.backlight_idx = DT_NODE_CHILD_IDX(DT_ALIAS(backlight)),
|
||||
.ui_model = {
|
||||
.theme_color = LV_COLOR_MAKE(BLINKY_THEME_DEFAULT_R,
|
||||
BLINKY_THEME_DEFAULT_G,
|
||||
BLINKY_THEME_DEFAULT_B),
|
||||
.inactive_border_color = LV_COLOR_MAKE(0x3A, 0x44, 0x52),
|
||||
.mode = MODE_SWITCH_BLE,
|
||||
.ble_link_state = UI_BLE_LINK_HIDDEN,
|
||||
},
|
||||
.date_text = "1970/01/01",
|
||||
.time_text = "00:00:00",
|
||||
};
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool lvgl_initialized;
|
||||
|
||||
static struct ui_page *main_page(void)
|
||||
{
|
||||
return ui_main_page_get(&ctx.ui_model, ctx.date_text, ctx.time_text);
|
||||
}
|
||||
|
||||
static int backlight_set(bool on)
|
||||
{
|
||||
if (on) {
|
||||
return led_on(backlight_dev, backlight_idx);
|
||||
return led_on(ctx.backlight_dev, ctx.backlight_idx);
|
||||
}
|
||||
|
||||
return led_off(backlight_dev, backlight_idx);
|
||||
return led_off(ctx.backlight_dev, ctx.backlight_idx);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
LOG_INF("Display init on %s", display_dev->name);
|
||||
LOG_INF("Display init on %s", ctx.display_dev->name);
|
||||
|
||||
if (!device_is_ready(display_dev)) {
|
||||
LOG_ERR("Display device %s not ready", display_dev->name);
|
||||
if (!device_is_ready(ctx.display_dev)) {
|
||||
LOG_ERR("Display device %s not ready", ctx.display_dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!device_is_ready(backlight_dev)) {
|
||||
LOG_ERR("Backlight device %s not ready", backlight_dev->name);
|
||||
if (!device_is_ready(ctx.backlight_dev)) {
|
||||
LOG_ERR("Backlight device %s not ready", ctx.backlight_dev->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
@@ -72,67 +122,77 @@ static int module_init(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!lvgl_initialized) {
|
||||
if (!ctx.lvgl_initialized) {
|
||||
err = lvgl_init();
|
||||
if (err) {
|
||||
LOG_ERR("lvgl_init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
lvgl_initialized = true;
|
||||
ctx.lvgl_initialized = true;
|
||||
|
||||
lvgl_lock();
|
||||
ui_main_init(&ui_model, "WH Mini", "Hello World");
|
||||
ui_page_init(main_page());
|
||||
lvgl_unlock();
|
||||
}
|
||||
|
||||
lvgl_lock();
|
||||
if (!ctx.settings_active) {
|
||||
ui_page_init(main_page());
|
||||
}
|
||||
lvgl_unlock();
|
||||
|
||||
err = backlight_set(true);
|
||||
if (err) {
|
||||
LOG_ERR("Backlight enable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = display_blanking_off(display_dev);
|
||||
err = display_blanking_off(ctx.display_dev);
|
||||
if (err) {
|
||||
LOG_ERR("display_blanking_off failed (%d)", err);
|
||||
(void)backlight_set(false);
|
||||
return err;
|
||||
}
|
||||
|
||||
running = true;
|
||||
LOG_INF("LVGL display started");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
(void)display_blanking_on(display_dev);
|
||||
(void)display_blanking_on(ctx.display_dev);
|
||||
(void)backlight_set(false);
|
||||
running = false;
|
||||
LOG_INF("LVGL display paused");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void refresh_ui(void)
|
||||
{
|
||||
if (!lvgl_initialized) {
|
||||
if (!ctx.lvgl_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
lvgl_lock();
|
||||
ui_main_refresh_all(&ui_model, "WH Mini", "Hello World");
|
||||
if (ctx.settings_active && (ctx.settings_page != NULL)) {
|
||||
ui_settings_show(ctx.settings_page, true);
|
||||
} else {
|
||||
ui_page_refresh(main_page());
|
||||
}
|
||||
lvgl_unlock();
|
||||
}
|
||||
|
||||
@@ -141,9 +201,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
if (is_bat_state_event(aeh)) {
|
||||
const struct bat_state_event *event = cast_bat_state_event(aeh);
|
||||
|
||||
ui_model.battery_level = event->soc;
|
||||
ui_model.charging = event->charging;
|
||||
ui_model.full = event->full;
|
||||
ctx.ui_model.battery_level = event->soc;
|
||||
ctx.ui_model.charging = event->charging;
|
||||
ctx.ui_model.full = event->full;
|
||||
refresh_ui();
|
||||
return false;
|
||||
}
|
||||
@@ -151,7 +211,54 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
if (is_mode_switch_event(aeh)) {
|
||||
const struct mode_switch_event *event = cast_mode_switch_event(aeh);
|
||||
|
||||
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();
|
||||
return false;
|
||||
}
|
||||
@@ -159,55 +266,96 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
if (is_hid_led_event(aeh)) {
|
||||
const struct hid_led_event *event = cast_hid_led_event(aeh);
|
||||
|
||||
ui_model.led_mask = event->led_bm;
|
||||
ctx.ui_model.led_mask = event->led_bm;
|
||||
refresh_ui();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_theme_rgb_update_event(aeh)) {
|
||||
const struct theme_rgb_update_event *event =
|
||||
cast_theme_rgb_update_event(aeh);
|
||||
|
||||
ctx.ui_model.theme_color = (lv_color_t)LV_COLOR_MAKE(event->theme.r,
|
||||
event->theme.g,
|
||||
event->theme.b);
|
||||
refresh_ui();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_settings_mode_event(aeh)) {
|
||||
const struct settings_mode_event *event =
|
||||
cast_settings_mode_event(aeh);
|
||||
|
||||
ctx.settings_active = event->active;
|
||||
if (!ctx.settings_active) {
|
||||
ctx.settings_page = NULL;
|
||||
}
|
||||
if (!ctx.lvgl_initialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
lvgl_lock();
|
||||
if (ctx.settings_active) {
|
||||
ui_page_deinit(main_page());
|
||||
ui_settings_init();
|
||||
if (ctx.settings_page != NULL) {
|
||||
ui_settings_show(ctx.settings_page, false);
|
||||
}
|
||||
} else {
|
||||
ui_settings_deinit();
|
||||
ui_page_init(main_page());
|
||||
ui_page_refresh(main_page());
|
||||
}
|
||||
lvgl_unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_settings_view_event(aeh)) {
|
||||
const struct settings_view_event *event =
|
||||
cast_settings_view_event(aeh);
|
||||
|
||||
ctx.settings_page = event->page;
|
||||
if (ctx.settings_active && ctx.lvgl_initialized) {
|
||||
lvgl_lock();
|
||||
ui_settings_show(ctx.settings_page, event->animate);
|
||||
lvgl_unlock();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_datetime_event(aeh)) {
|
||||
const struct datetime_event *event = cast_datetime_event(aeh);
|
||||
|
||||
strncpy(ctx.date_text, event->date_text, sizeof(ctx.date_text));
|
||||
ctx.date_text[sizeof(ctx.date_text) - 1] = '\0';
|
||||
strncpy(ctx.time_text, event->time_text, sizeof(ctx.time_text));
|
||||
ctx.time_text[sizeof(ctx.time_text) - 1] = '\0';
|
||||
refresh_ui();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
int err;
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -218,8 +366,15 @@ 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, ble_peer_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, ble_peer_search_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, datetime_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, settings_mode_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, settings_view_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, theme_rgb_update_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <zephyr/pm/device.h>
|
||||
|
||||
#include "encoder_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -22,40 +23,64 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
BUILD_ASSERT(DT_NODE_EXISTS(ENCODER_QDEC_NODE), "Missing qdec0 alias");
|
||||
|
||||
static const struct device *const qdec_dev = DEVICE_DT_GET(ENCODER_QDEC_NODE);
|
||||
|
||||
static struct k_work encoder_report_work;
|
||||
static struct sensor_trigger encoder_trigger = {
|
||||
.type = SENSOR_TRIG_DATA_READY,
|
||||
.chan = SENSOR_CHAN_ROTATION,
|
||||
struct encoder_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
const struct device *qdec_dev;
|
||||
struct k_work encoder_report_work;
|
||||
struct sensor_trigger encoder_trigger;
|
||||
int64_t angle_remainder_udeg;
|
||||
};
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static int64_t angle_remainder_udeg;
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
|
||||
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
||||
.mode = ML_MODE_POWER,
|
||||
.stopped_state = MODULE_STATE_STANDBY,
|
||||
};
|
||||
|
||||
static const struct module_lifecycle_ops lifecycle_ops = {
|
||||
.do_init = do_init,
|
||||
.do_start = do_start,
|
||||
.do_stop = do_stop,
|
||||
};
|
||||
|
||||
static struct encoder_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.qdec_dev = DEVICE_DT_GET(ENCODER_QDEC_NODE),
|
||||
.encoder_trigger = {
|
||||
.type = SENSOR_TRIG_DATA_READY,
|
||||
.chan = SENSOR_CHAN_ROTATION,
|
||||
},
|
||||
};
|
||||
|
||||
static int64_t sensor_value_to_udeg(const struct sensor_value *value)
|
||||
{
|
||||
return ((int64_t)value->val1 * 1000000LL) + value->val2;
|
||||
}
|
||||
|
||||
static void submit_detents(int32_t detents)
|
||||
static void submit_detents_batched(int32_t detents)
|
||||
{
|
||||
while (detents != 0) {
|
||||
struct encoder_event *event = new_encoder_event();
|
||||
int8_t event_detents;
|
||||
|
||||
if (detents > INT8_MAX) {
|
||||
event->detents = INT8_MAX;
|
||||
event_detents = INT8_MAX;
|
||||
detents -= INT8_MAX;
|
||||
} else if (detents < INT8_MIN) {
|
||||
event->detents = INT8_MIN;
|
||||
event_detents = INT8_MIN;
|
||||
detents -= INT8_MIN;
|
||||
} else {
|
||||
event->detents = (int8_t)detents;
|
||||
event_detents = (int8_t)detents;
|
||||
detents = 0;
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
submit_encoder_event(event_detents);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,28 +93,29 @@ static void encoder_report_work_handler(struct k_work *work)
|
||||
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
err = sensor_sample_fetch(qdec_dev);
|
||||
err = sensor_sample_fetch(ctx.qdec_dev);
|
||||
if (err) {
|
||||
LOG_WRN("QDEC sample fetch failed (%d)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
err = sensor_channel_get(qdec_dev, SENSOR_CHAN_ROTATION, &rotation);
|
||||
err = sensor_channel_get(ctx.qdec_dev, SENSOR_CHAN_ROTATION, &rotation);
|
||||
if (err) {
|
||||
LOG_WRN("QDEC channel get failed (%d)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
total_udeg = angle_remainder_udeg + sensor_value_to_udeg(&rotation);
|
||||
total_udeg = ctx.angle_remainder_udeg + sensor_value_to_udeg(&rotation);
|
||||
detents = (int32_t)(total_udeg / ENCODER_DETENT_UDEG);
|
||||
angle_remainder_udeg = total_udeg - ((int64_t)detents * ENCODER_DETENT_UDEG);
|
||||
ctx.angle_remainder_udeg =
|
||||
total_udeg - ((int64_t)detents * ENCODER_DETENT_UDEG);
|
||||
|
||||
if (detents != 0) {
|
||||
submit_detents(detents);
|
||||
submit_detents_batched(detents);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,26 +125,27 @@ static void encoder_trigger_handler(const struct device *dev,
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(trigger);
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
k_work_submit(&encoder_report_work);
|
||||
k_work_submit(&ctx.encoder_report_work);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!device_is_ready(qdec_dev)) {
|
||||
if (!device_is_ready(ctx.qdec_dev)) {
|
||||
LOG_ERR("QDEC device not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
k_work_init(&encoder_report_work, encoder_report_work_handler);
|
||||
angle_remainder_udeg = 0;
|
||||
k_work_init(&ctx.encoder_report_work, encoder_report_work_handler);
|
||||
ctx.angle_remainder_udeg = 0;
|
||||
|
||||
err = sensor_trigger_set(qdec_dev, &encoder_trigger, encoder_trigger_handler);
|
||||
err = sensor_trigger_set(ctx.qdec_dev, &ctx.encoder_trigger,
|
||||
encoder_trigger_handler);
|
||||
if (err) {
|
||||
LOG_ERR("Cannot set QDEC trigger (%d)", err);
|
||||
return err;
|
||||
@@ -127,41 +154,41 @@ static int module_init(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = pm_device_action_run(qdec_dev, PM_DEVICE_ACTION_RESUME);
|
||||
err = pm_device_action_run(ctx.qdec_dev, PM_DEVICE_ACTION_RESUME);
|
||||
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
||||
LOG_ERR("Cannot resume QDEC device (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
angle_remainder_udeg = 0;
|
||||
running = true;
|
||||
ctx.angle_remainder_udeg = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = pm_device_action_run(qdec_dev, PM_DEVICE_ACTION_SUSPEND);
|
||||
err = pm_device_action_run(ctx.qdec_dev, PM_DEVICE_ACTION_SUSPEND);
|
||||
if (err && (err != -EALREADY) && (err != -ENOTSUP)) {
|
||||
LOG_WRN("Cannot suspend QDEC device (%d)", err);
|
||||
}
|
||||
|
||||
angle_remainder_udeg = 0;
|
||||
running = false;
|
||||
ctx.angle_remainder_udeg = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
@@ -170,47 +197,23 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
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));
|
||||
@@ -1,73 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ble_serial_rx_event.h"
|
||||
|
||||
#define BLE_SERIAL_RX_EVENT_LOG_BUF_LEN 384
|
||||
|
||||
static void log_ble_serial_rx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_serial_rx_event *event = cast_ble_serial_rx_event(aeh);
|
||||
char log_buf[BLE_SERIAL_RX_EVENT_LOG_BUF_LEN];
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"",
|
||||
event->dyndata.size);
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c",
|
||||
isprint(event->dyndata.data[i]) ?
|
||||
event->dyndata.data[i] : '.');
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\" hex:");
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, " %02x",
|
||||
event->dyndata.data[i]);
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
break;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
||||
}
|
||||
|
||||
static void profile_ble_serial_rx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_serial_rx_event *event = cast_ble_serial_rx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(ble_serial_rx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_ble_serial_rx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(ble_serial_rx_event,
|
||||
log_ble_serial_rx_event,
|
||||
&ble_serial_rx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,62 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "ble_serial_tx_event.h"
|
||||
|
||||
#define BLE_SERIAL_TX_EVENT_LOG_BUF_LEN 256
|
||||
|
||||
static void log_ble_serial_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_serial_tx_event *event = cast_ble_serial_tx_event(aeh);
|
||||
char log_buf[BLE_SERIAL_TX_EVENT_LOG_BUF_LEN];
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"",
|
||||
event->dyndata.size);
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c",
|
||||
isprint(event->dyndata.data[i]) ?
|
||||
event->dyndata.data[i] : '.');
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\"");
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
||||
}
|
||||
|
||||
static void profile_ble_serial_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct ble_serial_tx_event *event = cast_ble_serial_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(ble_serial_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_ble_serial_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(ble_serial_tx_event,
|
||||
log_ble_serial_tx_event,
|
||||
&ble_serial_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,29 +0,0 @@
|
||||
#include "cdc_proto_tx_event.h"
|
||||
|
||||
static void log_cdc_proto_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct cdc_proto_tx_event *event = cast_cdc_proto_tx_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "type:0x%02x len:%zu",
|
||||
event->type, event->dyndata.size);
|
||||
}
|
||||
|
||||
static void profile_cdc_proto_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct cdc_proto_tx_event *event = cast_cdc_proto_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->type);
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(cdc_proto_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U8),
|
||||
ENCODE("type", "len"),
|
||||
profile_cdc_proto_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(cdc_proto_tx_event,
|
||||
log_cdc_proto_tx_event,
|
||||
&cdc_proto_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
26
src/events/datetime_event.c
Normal file
26
src/events/datetime_event.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include "datetime_event.h"
|
||||
|
||||
static void log_datetime_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct datetime_event *event = cast_datetime_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "date:%s time:%s",
|
||||
event->date_text, event->time_text);
|
||||
}
|
||||
|
||||
static void profile_datetime_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(datetime_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_datetime_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(datetime_event,
|
||||
log_datetime_event,
|
||||
&datetime_event_info,
|
||||
APP_EVENT_FLAGS_CREATE());
|
||||
27
src/events/function_bitmap_state_event.c
Normal file
27
src/events/function_bitmap_state_event.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "function_bitmap_state_event.h"
|
||||
|
||||
static void log_function_bitmap_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct function_bitmap_state_event *event =
|
||||
cast_function_bitmap_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "bitmap_len:%zu", sizeof(event->bitmap));
|
||||
}
|
||||
|
||||
static void profile_function_bitmap_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(function_bitmap_state_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_function_bitmap_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(function_bitmap_state_event,
|
||||
log_function_bitmap_state_event,
|
||||
&function_bitmap_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
51
src/events/hid_channel_state_event.c
Normal file
51
src/events/hid_channel_state_event.c
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "hid_channel_state_event.h"
|
||||
|
||||
static const char *channel_name(enum hid_send_channel channel)
|
||||
{
|
||||
switch (channel) {
|
||||
case HID_SEND_CH_USB_KEYS:
|
||||
return "usb_keys";
|
||||
case HID_SEND_CH_USB_CONSUMER:
|
||||
return "usb_consumer";
|
||||
case HID_SEND_CH_BLE_SHARED:
|
||||
return "ble_shared";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_hid_channel_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_channel_state_event *event =
|
||||
cast_hid_channel_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh,
|
||||
"channel:%s ready_bm:0x%02x protocol:%d",
|
||||
channel_name(event->channel),
|
||||
event->report_ready_bm,
|
||||
event->protocol_mode);
|
||||
}
|
||||
|
||||
static void profile_hid_channel_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_channel_state_event *event =
|
||||
cast_hid_channel_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->channel);
|
||||
nrf_profiler_log_encode_uint8(buf, event->report_ready_bm);
|
||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(hid_channel_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("channel", "ready_bm", "protocol_mode"),
|
||||
profile_hid_channel_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_channel_state_event,
|
||||
log_hid_channel_state_event,
|
||||
&hid_channel_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,12 +1,14 @@
|
||||
#include "hid_report_sent_event.h"
|
||||
|
||||
static const char *transport_name(enum hid_transport transport)
|
||||
static const char *channel_name(enum hid_send_channel channel)
|
||||
{
|
||||
switch (transport) {
|
||||
case HID_TRANSPORT_USB:
|
||||
return "USB";
|
||||
case HID_TRANSPORT_BLE:
|
||||
return "BLE";
|
||||
switch (channel) {
|
||||
case HID_SEND_CH_USB_KEYS:
|
||||
return "usb_keys";
|
||||
case HID_SEND_CH_USB_CONSUMER:
|
||||
return "usb_consumer";
|
||||
case HID_SEND_CH_BLE_SHARED:
|
||||
return "ble_shared";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
@@ -28,8 +30,8 @@ static void log_hid_report_sent_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_report_sent_event *event = cast_hid_report_sent_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s type:%s seq:%u error:%u",
|
||||
transport_name(event->transport),
|
||||
APP_EVENT_MANAGER_LOG(aeh, "channel:%s type:%s seq:%u error:%u",
|
||||
channel_name(event->channel),
|
||||
report_type_name(event->report_type),
|
||||
event->sequence,
|
||||
event->error);
|
||||
@@ -40,7 +42,7 @@ static void profile_hid_report_sent_event(struct log_event_buf *buf,
|
||||
{
|
||||
const struct hid_report_sent_event *event = cast_hid_report_sent_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint8(buf, event->channel);
|
||||
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
||||
nrf_profiler_log_encode_uint16(buf, event->sequence);
|
||||
nrf_profiler_log_encode_uint8(buf, event->error);
|
||||
@@ -51,7 +53,7 @@ APP_EVENT_INFO_DEFINE(hid_report_sent_event,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U16,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("transport", "report_type", "sequence", "error"),
|
||||
ENCODE("channel", "report_type", "sequence", "error"),
|
||||
profile_hid_report_sent_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_report_sent_event,
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
#include "hid_transport_state_event.h"
|
||||
|
||||
static const char *transport_name(enum hid_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case HID_TRANSPORT_USB:
|
||||
return "USB";
|
||||
case HID_TRANSPORT_BLE:
|
||||
return "BLE";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
switch (protocol_mode) {
|
||||
case KEYBOARD_PROTOCOL_MODE_BOOT:
|
||||
return "boot";
|
||||
case KEYBOARD_PROTOCOL_MODE_REPORT:
|
||||
return "report";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_hid_transport_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_transport_state_event *event =
|
||||
cast_hid_transport_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh,
|
||||
"transport:%s ready:%u keys_ready:%u consumer_ready:%u protocol:%s",
|
||||
transport_name(event->transport),
|
||||
event->ready,
|
||||
event->keys_ready,
|
||||
event->consumer_ready,
|
||||
protocol_mode_name(event->protocol_mode));
|
||||
}
|
||||
|
||||
static void profile_hid_transport_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct hid_transport_state_event *event =
|
||||
cast_hid_transport_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint8(buf, event->ready);
|
||||
nrf_profiler_log_encode_uint8(buf, event->keys_ready);
|
||||
nrf_profiler_log_encode_uint8(buf, event->consumer_ready);
|
||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(hid_transport_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("transport", "ready", "keys_ready", "consumer_ready",
|
||||
"protocol_mode"),
|
||||
profile_hid_transport_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_transport_state_event,
|
||||
log_hid_transport_state_event,
|
||||
&hid_transport_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -4,13 +4,15 @@
|
||||
|
||||
#define HID_TX_REPORT_EVENT_LOG_BUF_LEN 192
|
||||
|
||||
static const char *transport_name(enum hid_transport transport)
|
||||
static const char *channel_name(enum hid_send_channel channel)
|
||||
{
|
||||
switch (transport) {
|
||||
case HID_TRANSPORT_USB:
|
||||
return "USB";
|
||||
case HID_TRANSPORT_BLE:
|
||||
return "BLE";
|
||||
switch (channel) {
|
||||
case HID_SEND_CH_USB_KEYS:
|
||||
return "usb_keys";
|
||||
case HID_SEND_CH_USB_CONSUMER:
|
||||
return "usb_consumer";
|
||||
case HID_SEND_CH_BLE_SHARED:
|
||||
return "ble_shared";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
@@ -47,8 +49,8 @@ static void log_hid_tx_report_event(const struct app_event_header *aeh)
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf),
|
||||
"transport:%s type:%s protocol:%s seq:%u len:%zu",
|
||||
transport_name(event->transport),
|
||||
"channel:%s type:%s protocol:%s seq:%u len:%zu",
|
||||
channel_name(event->channel),
|
||||
report_type_name(event->report_type),
|
||||
protocol_mode_name(event->protocol_mode),
|
||||
event->sequence,
|
||||
@@ -84,7 +86,7 @@ static void profile_hid_tx_report_event(struct log_event_buf *buf,
|
||||
{
|
||||
const struct hid_tx_report_event *event = cast_hid_tx_report_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint8(buf, event->channel);
|
||||
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
||||
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
||||
nrf_profiler_log_encode_uint16(buf, event->sequence);
|
||||
@@ -97,7 +99,7 @@ APP_EVENT_INFO_DEFINE(hid_tx_report_event,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U16,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("transport", "report_type", "protocol_mode", "sequence", "len"),
|
||||
ENCODE("channel", "report_type", "protocol_mode", "sequence", "len"),
|
||||
profile_hid_tx_report_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(hid_tx_report_event,
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#include "key_function_event.h"
|
||||
|
||||
static const char *action_name(uint8_t action)
|
||||
{
|
||||
switch (action) {
|
||||
case KEY_FUNCTION_ACTION_RELEASE:
|
||||
return "release";
|
||||
|
||||
case KEY_FUNCTION_ACTION_PRESS:
|
||||
return "press";
|
||||
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_key_function_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct key_function_event *event = cast_key_function_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "usage:0x%04x action:%s",
|
||||
event->usage, action_name(event->action));
|
||||
}
|
||||
|
||||
static void profile_key_function_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct key_function_event *event = cast_key_function_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint16(buf, event->usage);
|
||||
nrf_profiler_log_encode_uint8(buf, event->action);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(key_function_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U16, NRF_PROFILER_ARG_U8),
|
||||
ENCODE("usage", "action"),
|
||||
profile_key_function_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(key_function_event,
|
||||
log_key_function_event,
|
||||
&key_function_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -7,6 +7,8 @@
|
||||
static const char *mode_name(enum mode_switch_mode mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case MODE_SWITCH_INVALID:
|
||||
return "INVALID";
|
||||
case MODE_SWITCH_USB:
|
||||
return "USB";
|
||||
case MODE_SWITCH_24G:
|
||||
|
||||
27
src/events/led_strip_en_event.c
Normal file
27
src/events/led_strip_en_event.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "led_strip_en_event.h"
|
||||
|
||||
static void log_led_strip_en_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct led_strip_en_event *event = cast_led_strip_en_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "enabled:%u", event->enabled);
|
||||
}
|
||||
|
||||
static void profile_led_strip_en_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct led_strip_en_event *event = cast_led_strip_en_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->enabled);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(led_strip_en_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("enabled"),
|
||||
profile_led_strip_en_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(led_strip_en_event,
|
||||
log_led_strip_en_event,
|
||||
&led_strip_en_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -5,6 +5,8 @@
|
||||
static const char *mode_name(enum mode_switch_mode mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case MODE_SWITCH_INVALID:
|
||||
return "INVALID";
|
||||
case MODE_SWITCH_USB:
|
||||
return "USB";
|
||||
case MODE_SWITCH_24G:
|
||||
|
||||
42
src/events/proto_rx_event.c
Normal file
42
src/events/proto_rx_event.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "proto_rx_event.h"
|
||||
|
||||
static const char *transport_name(enum proto_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case PROTO_TRANSPORT_USB_CDC:
|
||||
return "usb_cdc";
|
||||
case PROTO_TRANSPORT_BLE_NUS:
|
||||
return "ble_nus";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_proto_rx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_rx_event *event = cast_proto_rx_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s len:%zu",
|
||||
transport_name(event->transport),
|
||||
event->dyndata.size);
|
||||
}
|
||||
|
||||
static void profile_proto_rx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_rx_event *event = cast_proto_rx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint16(buf, (uint16_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(proto_rx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
|
||||
ENCODE("transport", "len"),
|
||||
profile_proto_rx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(proto_rx_event,
|
||||
log_proto_rx_event,
|
||||
&proto_rx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
56
src/events/proto_transport_state_event.c
Normal file
56
src/events/proto_transport_state_event.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "proto_transport_state_event.h"
|
||||
|
||||
static const char *transport_name(enum proto_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case PROTO_TRANSPORT_USB_CDC:
|
||||
return "usb_cdc";
|
||||
case PROTO_TRANSPORT_BLE_NUS:
|
||||
return "ble_nus";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *state_name(enum proto_transport_link_state state)
|
||||
{
|
||||
switch (state) {
|
||||
case PROTO_TRANSPORT_LINK_DOWN:
|
||||
return "down";
|
||||
case PROTO_TRANSPORT_LINK_READY:
|
||||
return "ready";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_proto_transport_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_transport_state_event *event =
|
||||
cast_proto_transport_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s state:%s",
|
||||
transport_name(event->transport),
|
||||
state_name(event->state));
|
||||
}
|
||||
|
||||
static void profile_proto_transport_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_transport_state_event *event =
|
||||
cast_proto_transport_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint8(buf, event->state);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(proto_transport_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U8),
|
||||
ENCODE("transport", "state"),
|
||||
profile_proto_transport_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(proto_transport_state_event,
|
||||
log_proto_transport_state_event,
|
||||
&proto_transport_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
42
src/events/proto_tx_event.c
Normal file
42
src/events/proto_tx_event.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "proto_tx_event.h"
|
||||
|
||||
static const char *transport_name(enum proto_transport transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case PROTO_TRANSPORT_USB_CDC:
|
||||
return "usb_cdc";
|
||||
case PROTO_TRANSPORT_BLE_NUS:
|
||||
return "ble_nus";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_proto_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_tx_event *event = cast_proto_tx_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "transport:%s len:%zu",
|
||||
transport_name(event->transport),
|
||||
event->dyndata.size);
|
||||
}
|
||||
|
||||
static void profile_proto_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct proto_tx_event *event = cast_proto_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->transport);
|
||||
nrf_profiler_log_encode_uint16(buf, (uint16_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(proto_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U16),
|
||||
ENCODE("transport", "len"),
|
||||
profile_proto_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(proto_tx_event,
|
||||
log_proto_tx_event,
|
||||
&proto_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
29
src/events/settings_mode_event.c
Normal file
29
src/events/settings_mode_event.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#include "settings_mode_event.h"
|
||||
|
||||
static void log_settings_mode_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct settings_mode_event *event =
|
||||
cast_settings_mode_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "active:%u", event->active);
|
||||
}
|
||||
|
||||
static void profile_settings_mode_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct settings_mode_event *event =
|
||||
cast_settings_mode_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->active ? 1U : 0U);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(settings_mode_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("active"),
|
||||
profile_settings_mode_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(settings_mode_event,
|
||||
log_settings_mode_event,
|
||||
&settings_mode_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
33
src/events/settings_view_event.c
Normal file
33
src/events/settings_view_event.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "settings_view_event.h"
|
||||
|
||||
static void log_settings_view_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct settings_view_event *event =
|
||||
cast_settings_view_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "page:%p animate:%u",
|
||||
event->page, event->animate);
|
||||
}
|
||||
|
||||
static void profile_settings_view_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct settings_view_event *event =
|
||||
cast_settings_view_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint32(buf, (uint32_t)(uintptr_t)event->page);
|
||||
nrf_profiler_log_encode_uint8(buf, event->animate ? 1U : 0U);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(settings_view_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U32, NRF_PROFILER_ARG_U8),
|
||||
ENCODE("page", "animate"),
|
||||
profile_settings_view_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(settings_view_event,
|
||||
log_settings_view_event,
|
||||
&settings_view_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
34
src/events/theme_rgb_update_event.c
Normal file
34
src/events/theme_rgb_update_event.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "theme_rgb_update_event.h"
|
||||
|
||||
static void log_theme_rgb_update_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct theme_rgb_update_event *event =
|
||||
cast_theme_rgb_update_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "r:%u g:%u b:%u",
|
||||
event->theme.r, event->theme.g, event->theme.b);
|
||||
}
|
||||
|
||||
static void profile_theme_rgb_update_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct theme_rgb_update_event *event =
|
||||
cast_theme_rgb_update_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->theme.r);
|
||||
nrf_profiler_log_encode_uint8(buf, event->theme.g);
|
||||
nrf_profiler_log_encode_uint8(buf, event->theme.b);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(theme_rgb_update_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("r", "g", "b"),
|
||||
profile_theme_rgb_update_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(theme_rgb_update_event,
|
||||
log_theme_rgb_update_event,
|
||||
&theme_rgb_update_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
41
src/events/time_sync_event.c
Normal file
41
src/events/time_sync_event.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "time_sync_event.h"
|
||||
|
||||
static void log_time_sync_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct time_sync_event *event = cast_time_sync_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh,
|
||||
"ver:%u flags:0x%08x tz:%d utc_ms:%llu acc:%u",
|
||||
event->version,
|
||||
event->flags,
|
||||
event->timezone_min,
|
||||
event->utc_ms,
|
||||
event->accuracy_ms);
|
||||
}
|
||||
|
||||
static void profile_time_sync_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct time_sync_event *event = cast_time_sync_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint32(buf, event->version);
|
||||
nrf_profiler_log_encode_uint32(buf, event->flags);
|
||||
nrf_profiler_log_encode_int32(buf, event->timezone_min);
|
||||
nrf_profiler_log_encode_uint32(buf, (uint32_t)(event->utc_ms & 0xFFFFFFFFULL));
|
||||
nrf_profiler_log_encode_uint32(buf, event->accuracy_ms);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(time_sync_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U32,
|
||||
NRF_PROFILER_ARG_U32,
|
||||
NRF_PROFILER_ARG_S32,
|
||||
NRF_PROFILER_ARG_U32,
|
||||
NRF_PROFILER_ARG_U32),
|
||||
ENCODE("version", "flags", "timezone_min", "utc_ms_lo", "accuracy_ms"),
|
||||
profile_time_sync_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(time_sync_event,
|
||||
log_time_sync_event,
|
||||
&time_sync_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
78
src/events/transport_policy_event.c
Normal file
78
src/events/transport_policy_event.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "transport_policy_event.h"
|
||||
|
||||
static const char *source_mode_name(enum mode_switch_mode mode)
|
||||
{
|
||||
switch (mode) {
|
||||
case MODE_SWITCH_USB:
|
||||
return "USB";
|
||||
case MODE_SWITCH_BLE:
|
||||
return "BLE";
|
||||
case MODE_SWITCH_24G:
|
||||
return "24G";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *hid_transport_policy_name(enum hid_transport_policy transport)
|
||||
{
|
||||
switch (transport) {
|
||||
case HID_TRANSPORT_POLICY_NONE:
|
||||
return "none";
|
||||
case HID_TRANSPORT_POLICY_USB:
|
||||
return "usb";
|
||||
case HID_TRANSPORT_POLICY_BLE:
|
||||
return "ble";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static const char *ble_profile_policy_name(enum ble_profile_policy profile)
|
||||
{
|
||||
switch (profile) {
|
||||
case BLE_PROFILE_POLICY_NONE:
|
||||
return "none";
|
||||
case BLE_PROFILE_POLICY_GENERAL:
|
||||
return "general";
|
||||
case BLE_PROFILE_POLICY_DONGLE:
|
||||
return "dongle";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_transport_policy_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct transport_policy_event *event =
|
||||
cast_transport_policy_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "source:%s hid:%s ble:%s",
|
||||
source_mode_name(event->source_mode),
|
||||
hid_transport_policy_name(event->hid_transport),
|
||||
ble_profile_policy_name(event->ble_profile));
|
||||
}
|
||||
|
||||
static void profile_transport_policy_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct transport_policy_event *event =
|
||||
cast_transport_policy_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->source_mode);
|
||||
nrf_profiler_log_encode_uint8(buf, event->hid_transport);
|
||||
nrf_profiler_log_encode_uint8(buf, event->ble_profile);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(transport_policy_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("source_mode", "hid_transport", "ble_profile"),
|
||||
profile_transport_policy_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(transport_policy_event,
|
||||
log_transport_policy_event,
|
||||
&transport_policy_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
IF_ENABLED(CONFIG_BRIDGE_LOG_TRANSPORT_POLICY_EVENT,
|
||||
(APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE))));
|
||||
@@ -1,73 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "usb_cdc_rx_event.h"
|
||||
|
||||
#define USB_CDC_RX_EVENT_LOG_BUF_LEN 384
|
||||
|
||||
static void log_usb_cdc_rx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_rx_event *event = cast_usb_cdc_rx_event(aeh);
|
||||
char log_buf[USB_CDC_RX_EVENT_LOG_BUF_LEN];
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"",
|
||||
event->dyndata.size);
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c",
|
||||
isprint(event->dyndata.data[i]) ?
|
||||
event->dyndata.data[i] : '.');
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\" hex:");
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, " %02x",
|
||||
event->dyndata.data[i]);
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
break;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
||||
}
|
||||
|
||||
static void profile_usb_cdc_rx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_rx_event *event = cast_usb_cdc_rx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_cdc_rx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_usb_cdc_rx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_cdc_rx_event,
|
||||
log_usb_cdc_rx_event,
|
||||
&usb_cdc_rx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,62 +0,0 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "usb_cdc_tx_event.h"
|
||||
|
||||
#define USB_CDC_TX_EVENT_LOG_BUF_LEN 256
|
||||
|
||||
static void log_usb_cdc_tx_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_tx_event *event = cast_usb_cdc_tx_event(aeh);
|
||||
char log_buf[USB_CDC_TX_EVENT_LOG_BUF_LEN];
|
||||
int pos;
|
||||
|
||||
pos = snprintf(log_buf, sizeof(log_buf), "len:%zu ascii:\"",
|
||||
event->dyndata.size);
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < event->dyndata.size; i++) {
|
||||
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos, "%c",
|
||||
isprint(event->dyndata.data[i]) ?
|
||||
event->dyndata.data[i] : '.');
|
||||
|
||||
if ((tmp < 0) || ((pos + tmp) >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
pos += tmp;
|
||||
}
|
||||
|
||||
pos += snprintf(&log_buf[pos], sizeof(log_buf) - pos, "\"");
|
||||
if ((pos < 0) || (pos >= sizeof(log_buf))) {
|
||||
APP_EVENT_MANAGER_LOG(aeh, "len:%zu ascii:\"...\"",
|
||||
event->dyndata.size);
|
||||
return;
|
||||
}
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
||||
}
|
||||
|
||||
static void profile_usb_cdc_tx_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_cdc_tx_event *event = cast_usb_cdc_tx_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_cdc_tx_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("len"),
|
||||
profile_usb_cdc_tx_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_cdc_tx_event,
|
||||
log_usb_cdc_tx_event,
|
||||
&usb_cdc_tx_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
82
src/events/usb_control_event.c
Normal file
82
src/events/usb_control_event.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#include "usb_control_event.h"
|
||||
|
||||
static const char *control_event_name(enum usb_control_event_type type)
|
||||
{
|
||||
switch (type) {
|
||||
case USB_CONTROL_EVENT_CDC_LINE_STATE:
|
||||
return "cdc_line_state";
|
||||
case USB_CONTROL_EVENT_CDC_LINE_CODING:
|
||||
return "cdc_line_coding";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_control_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_control_event *event = cast_usb_control_event(aeh);
|
||||
|
||||
switch (event->type) {
|
||||
case USB_CONTROL_EVENT_CDC_LINE_STATE:
|
||||
APP_EVENT_MANAGER_LOG(aeh, "type:%s dtr:%u",
|
||||
control_event_name(event->type),
|
||||
event->data.cdc_line_state.dtr);
|
||||
break;
|
||||
|
||||
case USB_CONTROL_EVENT_CDC_LINE_CODING:
|
||||
APP_EVENT_MANAGER_LOG(aeh,
|
||||
"type:%s baud:%u data:%u stop:%u parity:%u flow:%u",
|
||||
control_event_name(event->type),
|
||||
event->data.cdc_line_coding.baudrate,
|
||||
event->data.cdc_line_coding.data_bits,
|
||||
event->data.cdc_line_coding.stop_bits,
|
||||
event->data.cdc_line_coding.parity,
|
||||
event->data.cdc_line_coding.flow_ctrl);
|
||||
break;
|
||||
|
||||
default:
|
||||
APP_EVENT_MANAGER_LOG(aeh, "type:%s",
|
||||
control_event_name(event->type));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void profile_usb_control_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_control_event *event = cast_usb_control_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->type);
|
||||
switch (event->type) {
|
||||
case USB_CONTROL_EVENT_CDC_LINE_STATE:
|
||||
nrf_profiler_log_encode_uint8(buf, event->data.cdc_line_state.dtr);
|
||||
break;
|
||||
|
||||
case USB_CONTROL_EVENT_CDC_LINE_CODING:
|
||||
nrf_profiler_log_encode_uint32(buf, event->data.cdc_line_coding.baudrate);
|
||||
nrf_profiler_log_encode_uint8(buf, event->data.cdc_line_coding.data_bits);
|
||||
nrf_profiler_log_encode_uint8(buf, event->data.cdc_line_coding.stop_bits);
|
||||
nrf_profiler_log_encode_uint8(buf, event->data.cdc_line_coding.parity);
|
||||
nrf_profiler_log_encode_uint8(buf, event->data.cdc_line_coding.flow_ctrl);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_control_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U32,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8,
|
||||
NRF_PROFILER_ARG_U8),
|
||||
ENCODE("type", "baud_or_zero", "arg1", "arg2", "arg3", "arg4"),
|
||||
profile_usb_control_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_control_event,
|
||||
log_usb_control_event,
|
||||
&usb_control_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,46 +0,0 @@
|
||||
#include "usb_device_state_event.h"
|
||||
|
||||
static const char *usb_device_state_name(enum usb_device_state state)
|
||||
{
|
||||
switch (state) {
|
||||
case USB_DEVICE_STATE_DISCONNECTED:
|
||||
return "disconnected";
|
||||
case USB_DEVICE_STATE_POWERED:
|
||||
return "powered";
|
||||
case USB_DEVICE_STATE_ACTIVE:
|
||||
return "active";
|
||||
case USB_DEVICE_STATE_SUSPENDED:
|
||||
return "suspended";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_device_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_device_state_event *event =
|
||||
cast_usb_device_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "state:%s",
|
||||
usb_device_state_name(event->state));
|
||||
}
|
||||
|
||||
static void profile_usb_device_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_device_state_event *event =
|
||||
cast_usb_device_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->state);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_device_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("state"),
|
||||
profile_usb_device_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_device_state_event,
|
||||
log_usb_device_state_event,
|
||||
&usb_device_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,42 +0,0 @@
|
||||
#include "usb_function_ready_event.h"
|
||||
|
||||
static const char *usb_function_name(uint8_t function_mask)
|
||||
{
|
||||
switch (function_mask) {
|
||||
case USB_FUNCTION_HID:
|
||||
return "hid";
|
||||
case USB_FUNCTION_CDC_ACM:
|
||||
return "cdc_acm";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_function_ready_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_function_ready_event *event =
|
||||
cast_usb_function_ready_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "function:%s",
|
||||
usb_function_name(event->function_mask));
|
||||
}
|
||||
|
||||
static void profile_usb_function_ready_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_function_ready_event *event =
|
||||
cast_usb_function_ready_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->function_mask);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_function_ready_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("function_mask"),
|
||||
profile_usb_function_ready_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_function_ready_event,
|
||||
log_usb_function_ready_event,
|
||||
&usb_function_ready_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -1,24 +0,0 @@
|
||||
#include "usb_prepare_event.h"
|
||||
|
||||
static void log_usb_prepare_event(const struct app_event_header *aeh)
|
||||
{
|
||||
APP_EVENT_MANAGER_LOG(aeh, "prepare");
|
||||
}
|
||||
|
||||
static void profile_usb_prepare_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
ARG_UNUSED(buf);
|
||||
ARG_UNUSED(aeh);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_prepare_event,
|
||||
ENCODE(),
|
||||
ENCODE(),
|
||||
profile_usb_prepare_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_prepare_event,
|
||||
log_usb_prepare_event,
|
||||
&usb_prepare_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
47
src/events/usb_state_event.c
Normal file
47
src/events/usb_state_event.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
#include "usb_state_event.h"
|
||||
|
||||
static const char *usb_state_name(enum usb_state state)
|
||||
{
|
||||
switch (state) {
|
||||
case USB_STATE_DISABLED:
|
||||
return "disabled";
|
||||
case USB_STATE_DISCONNECTED:
|
||||
return "disconnected";
|
||||
case USB_STATE_POWERED:
|
||||
return "powered";
|
||||
case USB_STATE_ACTIVE:
|
||||
return "active";
|
||||
case USB_STATE_SUSPENDED:
|
||||
return "suspended";
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
static void log_usb_state_event(const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_state_event *event = cast_usb_state_event(aeh);
|
||||
|
||||
APP_EVENT_MANAGER_LOG(aeh, "state:%s", usb_state_name(event->state));
|
||||
}
|
||||
|
||||
static void profile_usb_state_event(struct log_event_buf *buf,
|
||||
const struct app_event_header *aeh)
|
||||
{
|
||||
const struct usb_state_event *event = cast_usb_state_event(aeh);
|
||||
|
||||
nrf_profiler_log_encode_uint8(buf, event->state);
|
||||
}
|
||||
|
||||
APP_EVENT_INFO_DEFINE(usb_state_event,
|
||||
ENCODE(NRF_PROFILER_ARG_U8),
|
||||
ENCODE("state"),
|
||||
profile_usb_state_event);
|
||||
|
||||
APP_EVENT_TYPE_DEFINE(usb_state_event,
|
||||
log_usb_state_event,
|
||||
&usb_state_event_info,
|
||||
APP_EVENT_FLAGS_CREATE(
|
||||
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|
||||
@@ -10,12 +10,13 @@
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "hid_channel_state_event.h"
|
||||
#include "hid_report_sent_event.h"
|
||||
#include "hid_transport_state_event.h"
|
||||
#include "hid_tx_report_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
#include "mode_switch_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
#include "transport_policy_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -37,49 +38,82 @@ struct queued_report {
|
||||
uint8_t data[HID_FLOWCTRL_REPORT_DATA_MAX];
|
||||
};
|
||||
|
||||
struct hid_transport_state_data {
|
||||
bool ready;
|
||||
bool keys_ready;
|
||||
bool consumer_ready;
|
||||
struct hid_channel_state_data {
|
||||
uint8_t report_ready_bm;
|
||||
enum keyboard_protocol_mode protocol_mode;
|
||||
};
|
||||
|
||||
struct in_flight_report {
|
||||
bool active;
|
||||
enum hid_transport transport;
|
||||
enum hid_send_channel channel;
|
||||
enum keyboard_report_type report_type;
|
||||
uint16_t sequence;
|
||||
};
|
||||
|
||||
static struct hid_transport_state_data transport_state[HID_TRANSPORT_COUNT] = {
|
||||
[HID_TRANSPORT_USB] = {
|
||||
struct hid_flowctrl_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
struct hid_channel_state_data channel_state[HID_SEND_CH_COUNT];
|
||||
struct pending_report pending_keys;
|
||||
struct pending_report pending_consumer_latest;
|
||||
struct queued_report consumer_fifo[HID_FLOWCTRL_FIFO_DEPTH];
|
||||
uint8_t consumer_fifo_head;
|
||||
uint8_t consumer_fifo_tail;
|
||||
uint8_t consumer_fifo_count;
|
||||
struct in_flight_report in_flight[HID_SEND_CH_COUNT];
|
||||
enum hid_transport_policy current_transport;
|
||||
uint16_t next_sequence;
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
|
||||
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
||||
.mode = ML_MODE_POWER,
|
||||
.stopped_state = MODULE_STATE_STANDBY,
|
||||
};
|
||||
|
||||
static const struct module_lifecycle_ops lifecycle_ops = {
|
||||
.do_init = do_init,
|
||||
.do_start = do_start,
|
||||
.do_stop = do_stop,
|
||||
};
|
||||
|
||||
static struct hid_flowctrl_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.channel_state = {
|
||||
[HID_SEND_CH_USB_KEYS] = {
|
||||
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
},
|
||||
[HID_TRANSPORT_BLE] = {
|
||||
[HID_SEND_CH_USB_CONSUMER] = {
|
||||
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
},
|
||||
[HID_SEND_CH_BLE_SHARED] = {
|
||||
.protocol_mode = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
},
|
||||
},
|
||||
};
|
||||
static struct pending_report pending_keys;
|
||||
static struct pending_report pending_consumer_latest;
|
||||
static struct queued_report consumer_fifo[HID_FLOWCTRL_FIFO_DEPTH];
|
||||
static uint8_t consumer_fifo_head;
|
||||
static uint8_t consumer_fifo_tail;
|
||||
static uint8_t consumer_fifo_count;
|
||||
static struct in_flight_report in_flight;
|
||||
static enum mode_switch_mode current_mode;
|
||||
static uint16_t next_sequence;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
|
||||
static bool mode_to_transport(enum mode_switch_mode mode, enum hid_transport *transport)
|
||||
static bool current_transport_to_channel(enum keyboard_report_type report_type,
|
||||
enum hid_send_channel *channel)
|
||||
{
|
||||
switch (mode) {
|
||||
case MODE_SWITCH_USB:
|
||||
*transport = HID_TRANSPORT_USB;
|
||||
if (channel == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (ctx.current_transport) {
|
||||
case HID_TRANSPORT_POLICY_USB:
|
||||
*channel = (report_type == KEYBOARD_REPORT_TYPE_KEYS) ?
|
||||
HID_SEND_CH_USB_KEYS :
|
||||
HID_SEND_CH_USB_CONSUMER;
|
||||
return true;
|
||||
|
||||
case MODE_SWITCH_BLE:
|
||||
*transport = HID_TRANSPORT_BLE;
|
||||
case HID_TRANSPORT_POLICY_BLE:
|
||||
*channel = HID_SEND_CH_BLE_SHARED;
|
||||
return true;
|
||||
|
||||
default:
|
||||
@@ -89,156 +123,185 @@ static bool mode_to_transport(enum mode_switch_mode mode, enum hid_transport *tr
|
||||
|
||||
static void clear_pending_reports(void)
|
||||
{
|
||||
memset(&pending_keys, 0, sizeof(pending_keys));
|
||||
memset(&pending_consumer_latest, 0, sizeof(pending_consumer_latest));
|
||||
consumer_fifo_head = 0U;
|
||||
consumer_fifo_tail = 0U;
|
||||
consumer_fifo_count = 0U;
|
||||
memset(&in_flight, 0, sizeof(in_flight));
|
||||
memset(&ctx.pending_keys, 0, sizeof(ctx.pending_keys));
|
||||
memset(&ctx.pending_consumer_latest, 0, sizeof(ctx.pending_consumer_latest));
|
||||
ctx.consumer_fifo_head = 0U;
|
||||
ctx.consumer_fifo_tail = 0U;
|
||||
ctx.consumer_fifo_count = 0U;
|
||||
memset(&ctx.in_flight, 0, sizeof(ctx.in_flight));
|
||||
}
|
||||
|
||||
static void consumer_fifo_push(enum keyboard_report_type report_type,
|
||||
enum keyboard_protocol_mode protocol_mode,
|
||||
const uint8_t *data, size_t size)
|
||||
{
|
||||
if (consumer_fifo_count == HID_FLOWCTRL_FIFO_DEPTH) {
|
||||
if (ctx.consumer_fifo_count == HID_FLOWCTRL_FIFO_DEPTH) {
|
||||
LOG_WRN("Consumer FIFO full, dropping oldest pulse");
|
||||
consumer_fifo_head = (consumer_fifo_head + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||
consumer_fifo_count--;
|
||||
ctx.consumer_fifo_head =
|
||||
(ctx.consumer_fifo_head + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||
ctx.consumer_fifo_count--;
|
||||
}
|
||||
|
||||
struct queued_report *entry = &consumer_fifo[consumer_fifo_tail];
|
||||
struct queued_report *entry = &ctx.consumer_fifo[ctx.consumer_fifo_tail];
|
||||
|
||||
entry->report_type = report_type;
|
||||
entry->protocol_mode = protocol_mode;
|
||||
entry->size = size;
|
||||
memcpy(entry->data, data, size);
|
||||
|
||||
consumer_fifo_tail = (consumer_fifo_tail + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||
consumer_fifo_count++;
|
||||
ctx.consumer_fifo_tail =
|
||||
(ctx.consumer_fifo_tail + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||
ctx.consumer_fifo_count++;
|
||||
}
|
||||
|
||||
static bool consumer_fifo_pop(struct queued_report *entry)
|
||||
{
|
||||
if (consumer_fifo_count == 0U) {
|
||||
if (ctx.consumer_fifo_count == 0U) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*entry = consumer_fifo[consumer_fifo_head];
|
||||
consumer_fifo_head = (consumer_fifo_head + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||
consumer_fifo_count--;
|
||||
*entry = ctx.consumer_fifo[ctx.consumer_fifo_head];
|
||||
ctx.consumer_fifo_head =
|
||||
(ctx.consumer_fifo_head + 1U) % HID_FLOWCTRL_FIFO_DEPTH;
|
||||
ctx.consumer_fifo_count--;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool transport_can_send_report(enum keyboard_report_type report_type)
|
||||
static bool channel_can_send_report(enum hid_send_channel channel,
|
||||
enum keyboard_report_type report_type,
|
||||
enum keyboard_protocol_mode protocol_mode)
|
||||
{
|
||||
enum hid_transport transport;
|
||||
struct hid_transport_state_data *state;
|
||||
const struct hid_channel_state_data *state = &ctx.channel_state[channel];
|
||||
|
||||
if (!mode_to_transport(current_mode, &transport) || in_flight.active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
state = &transport_state[transport];
|
||||
|
||||
if (!state->ready) {
|
||||
if (ctx.in_flight[channel].active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||
return state->keys_ready;
|
||||
return (state->report_ready_bm & BIT(KEYBOARD_REPORT_TYPE_KEYS)) &&
|
||||
(state->protocol_mode == protocol_mode);
|
||||
}
|
||||
|
||||
return state->consumer_ready;
|
||||
if (channel == HID_SEND_CH_BLE_SHARED) {
|
||||
return (state->report_ready_bm & BIT(KEYBOARD_REPORT_TYPE_CONSUMER)) &&
|
||||
(state->protocol_mode == KEYBOARD_PROTOCOL_MODE_REPORT);
|
||||
}
|
||||
|
||||
static void submit_hid_tx_report_event(enum hid_transport transport,
|
||||
enum keyboard_report_type report_type,
|
||||
enum keyboard_protocol_mode protocol_mode,
|
||||
const uint8_t *data, size_t size)
|
||||
return (state->report_ready_bm & BIT(KEYBOARD_REPORT_TYPE_CONSUMER)) != 0U;
|
||||
}
|
||||
|
||||
static void try_send_keys(void)
|
||||
{
|
||||
struct hid_tx_report_event *event = new_hid_tx_report_event(size);
|
||||
enum hid_send_channel channel;
|
||||
|
||||
event->transport = transport;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->sequence = next_sequence++;
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
if (!ctx.pending_keys.valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
in_flight.active = true;
|
||||
in_flight.transport = transport;
|
||||
in_flight.report_type = report_type;
|
||||
in_flight.sequence = event->sequence;
|
||||
if (!current_transport_to_channel(KEYBOARD_REPORT_TYPE_KEYS, &channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
if (!channel_can_send_report(channel, ctx.pending_keys.report_type,
|
||||
ctx.pending_keys.protocol_mode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.in_flight[channel].active = true;
|
||||
ctx.in_flight[channel].channel = channel;
|
||||
ctx.in_flight[channel].report_type = ctx.pending_keys.report_type;
|
||||
ctx.in_flight[channel].sequence = ctx.next_sequence++;
|
||||
(void)submit_hid_tx_report_event(channel, ctx.pending_keys.report_type,
|
||||
ctx.pending_keys.protocol_mode,
|
||||
ctx.in_flight[channel].sequence,
|
||||
ctx.pending_keys.data, ctx.pending_keys.size);
|
||||
ctx.pending_keys.valid = false;
|
||||
}
|
||||
|
||||
static void try_send_consumer_fifo(void)
|
||||
{
|
||||
struct queued_report queued;
|
||||
enum hid_send_channel channel;
|
||||
|
||||
if (ctx.consumer_fifo_count == 0U) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!current_transport_to_channel(KEYBOARD_REPORT_TYPE_CONSUMER, &channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!consumer_fifo_pop(&queued)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!channel_can_send_report(channel, queued.report_type,
|
||||
queued.protocol_mode)) {
|
||||
if (queued.protocol_mode == ctx.channel_state[channel].protocol_mode) {
|
||||
consumer_fifo_push(queued.report_type, queued.protocol_mode,
|
||||
queued.data, queued.size);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.in_flight[channel].active = true;
|
||||
ctx.in_flight[channel].channel = channel;
|
||||
ctx.in_flight[channel].report_type = queued.report_type;
|
||||
ctx.in_flight[channel].sequence = ctx.next_sequence++;
|
||||
(void)submit_hid_tx_report_event(channel, queued.report_type,
|
||||
queued.protocol_mode,
|
||||
ctx.in_flight[channel].sequence,
|
||||
queued.data, queued.size);
|
||||
}
|
||||
|
||||
static void try_send_consumer_latest(void)
|
||||
{
|
||||
enum hid_send_channel channel;
|
||||
|
||||
if (!ctx.pending_consumer_latest.valid) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!current_transport_to_channel(KEYBOARD_REPORT_TYPE_CONSUMER, &channel)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!channel_can_send_report(channel,
|
||||
ctx.pending_consumer_latest.report_type,
|
||||
ctx.pending_consumer_latest.protocol_mode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.in_flight[channel].active = true;
|
||||
ctx.in_flight[channel].channel = channel;
|
||||
ctx.in_flight[channel].report_type =
|
||||
ctx.pending_consumer_latest.report_type;
|
||||
ctx.in_flight[channel].sequence = ctx.next_sequence++;
|
||||
(void)submit_hid_tx_report_event(channel,
|
||||
ctx.pending_consumer_latest.report_type,
|
||||
ctx.pending_consumer_latest.protocol_mode,
|
||||
ctx.in_flight[channel].sequence,
|
||||
ctx.pending_consumer_latest.data,
|
||||
ctx.pending_consumer_latest.size);
|
||||
ctx.pending_consumer_latest.valid = false;
|
||||
}
|
||||
|
||||
static void try_send_next(void)
|
||||
{
|
||||
struct queued_report queued;
|
||||
enum hid_transport transport;
|
||||
struct hid_transport_state_data *state;
|
||||
|
||||
if (!running || in_flight.active || !mode_to_transport(current_mode, &transport)) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return;
|
||||
}
|
||||
|
||||
state = &transport_state[transport];
|
||||
|
||||
if (!state->ready) {
|
||||
return;
|
||||
try_send_keys();
|
||||
try_send_consumer_fifo();
|
||||
try_send_consumer_latest();
|
||||
}
|
||||
|
||||
if (pending_keys.valid && transport_can_send_report(KEYBOARD_REPORT_TYPE_KEYS)) {
|
||||
if (pending_keys.protocol_mode != state->protocol_mode) {
|
||||
LOG_WRN("Drop stale keys report after protocol change");
|
||||
pending_keys.valid = false;
|
||||
} else {
|
||||
submit_hid_tx_report_event(transport,
|
||||
pending_keys.report_type,
|
||||
pending_keys.protocol_mode,
|
||||
pending_keys.data,
|
||||
pending_keys.size);
|
||||
pending_keys.valid = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if ((consumer_fifo_count > 0U) &&
|
||||
transport_can_send_report(KEYBOARD_REPORT_TYPE_CONSUMER) &&
|
||||
consumer_fifo_pop(&queued)) {
|
||||
if (queued.protocol_mode != state->protocol_mode) {
|
||||
LOG_WRN("Drop stale consumer report after protocol change");
|
||||
} else {
|
||||
submit_hid_tx_report_event(transport,
|
||||
queued.report_type,
|
||||
queued.protocol_mode,
|
||||
queued.data,
|
||||
queued.size);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (pending_consumer_latest.valid &&
|
||||
transport_can_send_report(KEYBOARD_REPORT_TYPE_CONSUMER)) {
|
||||
if (pending_consumer_latest.protocol_mode != state->protocol_mode) {
|
||||
LOG_WRN("Drop stale latest consumer report after protocol change");
|
||||
pending_consumer_latest.valid = false;
|
||||
} else {
|
||||
submit_hid_tx_report_event(transport,
|
||||
pending_consumer_latest.report_type,
|
||||
pending_consumer_latest.protocol_mode,
|
||||
pending_consumer_latest.data,
|
||||
pending_consumer_latest.size);
|
||||
pending_consumer_latest.valid = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_event *event)
|
||||
static bool handle_keyboard_hid_report_event(
|
||||
const struct keyboard_hid_report_event *event)
|
||||
{
|
||||
if (!running ||
|
||||
if (!module_lifecycle_is_running(&ctx.lc) ||
|
||||
((event->mode != MODE_SWITCH_USB) && (event->mode != MODE_SWITCH_BLE))) {
|
||||
return false;
|
||||
}
|
||||
@@ -249,17 +312,18 @@ static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_ev
|
||||
event->dyndata.data,
|
||||
event->dyndata.size);
|
||||
} else if (event->report_type == KEYBOARD_REPORT_TYPE_KEYS) {
|
||||
pending_keys.valid = true;
|
||||
pending_keys.report_type = event->report_type;
|
||||
pending_keys.protocol_mode = event->protocol_mode;
|
||||
pending_keys.size = event->dyndata.size;
|
||||
memcpy(pending_keys.data, event->dyndata.data, event->dyndata.size);
|
||||
ctx.pending_keys.valid = true;
|
||||
ctx.pending_keys.report_type = event->report_type;
|
||||
ctx.pending_keys.protocol_mode = event->protocol_mode;
|
||||
ctx.pending_keys.size = event->dyndata.size;
|
||||
memcpy(ctx.pending_keys.data, event->dyndata.data,
|
||||
event->dyndata.size);
|
||||
} else {
|
||||
pending_consumer_latest.valid = true;
|
||||
pending_consumer_latest.report_type = event->report_type;
|
||||
pending_consumer_latest.protocol_mode = event->protocol_mode;
|
||||
pending_consumer_latest.size = event->dyndata.size;
|
||||
memcpy(pending_consumer_latest.data, event->dyndata.data,
|
||||
ctx.pending_consumer_latest.valid = true;
|
||||
ctx.pending_consumer_latest.report_type = event->report_type;
|
||||
ctx.pending_consumer_latest.protocol_mode = event->protocol_mode;
|
||||
ctx.pending_consumer_latest.size = event->dyndata.size;
|
||||
memcpy(ctx.pending_consumer_latest.data, event->dyndata.data,
|
||||
event->dyndata.size);
|
||||
}
|
||||
|
||||
@@ -267,41 +331,19 @@ static bool handle_keyboard_hid_report_event(const struct keyboard_hid_report_ev
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_hid_transport_state_event(const struct hid_transport_state_event *event)
|
||||
static bool handle_hid_channel_state_event(
|
||||
const struct hid_channel_state_event *event)
|
||||
{
|
||||
enum hid_transport active_transport;
|
||||
struct hid_transport_state_data *state;
|
||||
|
||||
if (event->transport >= HID_TRANSPORT_COUNT) {
|
||||
if (event->channel >= HID_SEND_CH_COUNT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
state = &transport_state[event->transport];
|
||||
ctx.channel_state[event->channel].report_ready_bm =
|
||||
event->report_ready_bm;
|
||||
ctx.channel_state[event->channel].protocol_mode = event->protocol_mode;
|
||||
|
||||
state->ready = event->ready;
|
||||
state->keys_ready = event->keys_ready;
|
||||
state->consumer_ready = event->consumer_ready;
|
||||
|
||||
if (state->protocol_mode != event->protocol_mode) {
|
||||
state->protocol_mode = event->protocol_mode;
|
||||
|
||||
if (mode_to_transport(current_mode, &active_transport) &&
|
||||
(active_transport == event->transport)) {
|
||||
pending_keys.valid = false;
|
||||
pending_consumer_latest.valid = false;
|
||||
consumer_fifo_head = 0U;
|
||||
consumer_fifo_tail = 0U;
|
||||
consumer_fifo_count = 0U;
|
||||
}
|
||||
}
|
||||
|
||||
if (!state->ready &&
|
||||
mode_to_transport(current_mode, &active_transport) &&
|
||||
(active_transport == event->transport)) {
|
||||
consumer_fifo_head = 0U;
|
||||
consumer_fifo_tail = 0U;
|
||||
consumer_fifo_count = 0U;
|
||||
in_flight.active = false;
|
||||
if (event->report_ready_bm == 0U) {
|
||||
ctx.in_flight[event->channel].active = false;
|
||||
}
|
||||
|
||||
try_send_next();
|
||||
@@ -310,17 +352,21 @@ static bool handle_hid_transport_state_event(const struct hid_transport_state_ev
|
||||
|
||||
static bool handle_hid_report_sent_event(const struct hid_report_sent_event *event)
|
||||
{
|
||||
if (!in_flight.active || (event->transport != in_flight.transport)) {
|
||||
if (event->channel >= HID_SEND_CH_COUNT) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->sequence != in_flight.sequence) {
|
||||
if (!ctx.in_flight[event->channel].active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event->sequence != ctx.in_flight[event->channel].sequence) {
|
||||
LOG_WRN("Unexpected HID sent sequence %u (expected %u)",
|
||||
event->sequence, in_flight.sequence);
|
||||
event->sequence, ctx.in_flight[event->channel].sequence);
|
||||
return false;
|
||||
}
|
||||
|
||||
in_flight.active = false;
|
||||
ctx.in_flight[event->channel].active = false;
|
||||
|
||||
if (event->error) {
|
||||
LOG_WRN("HID report send failed for seq %u", event->sequence);
|
||||
@@ -330,14 +376,16 @@ static bool handle_hid_report_sent_event(const struct hid_report_sent_event *eve
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
static bool handle_transport_policy_event(
|
||||
const struct transport_policy_event *event)
|
||||
{
|
||||
bool mode_changed = (current_mode != event->mode);
|
||||
bool transport_changed =
|
||||
(ctx.current_transport != event->hid_transport);
|
||||
|
||||
current_mode = event->mode;
|
||||
ctx.current_transport = event->hid_transport;
|
||||
|
||||
if (mode_changed || ((current_mode != MODE_SWITCH_USB) &&
|
||||
(current_mode != MODE_SWITCH_BLE))) {
|
||||
if (transport_changed ||
|
||||
(ctx.current_transport == HID_TRANSPORT_POLICY_NONE)) {
|
||||
clear_pending_reports();
|
||||
}
|
||||
|
||||
@@ -345,110 +393,86 @@ static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
clear_pending_reports();
|
||||
current_mode = MODE_SWITCH_USB;
|
||||
transport_state[HID_TRANSPORT_USB].ready = false;
|
||||
transport_state[HID_TRANSPORT_USB].keys_ready = false;
|
||||
transport_state[HID_TRANSPORT_USB].consumer_ready = false;
|
||||
transport_state[HID_TRANSPORT_USB].protocol_mode =
|
||||
ctx.current_transport = HID_TRANSPORT_POLICY_USB;
|
||||
memset(ctx.channel_state, 0, sizeof(ctx.channel_state));
|
||||
ctx.channel_state[HID_SEND_CH_USB_KEYS].protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
transport_state[HID_TRANSPORT_BLE].ready = false;
|
||||
transport_state[HID_TRANSPORT_BLE].keys_ready = false;
|
||||
transport_state[HID_TRANSPORT_BLE].consumer_ready = false;
|
||||
transport_state[HID_TRANSPORT_BLE].protocol_mode =
|
||||
ctx.channel_state[HID_SEND_CH_USB_CONSUMER].protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
next_sequence = 1U;
|
||||
|
||||
ctx.channel_state[HID_SEND_CH_BLE_SHARED].protocol_mode =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
ctx.next_sequence = 1U;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
try_send_next();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
clear_pending_reports();
|
||||
running = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_keyboard_hid_report_event(aeh)) {
|
||||
return handle_keyboard_hid_report_event(cast_keyboard_hid_report_event(aeh));
|
||||
return handle_keyboard_hid_report_event(
|
||||
cast_keyboard_hid_report_event(aeh));
|
||||
}
|
||||
|
||||
if (is_hid_transport_state_event(aeh)) {
|
||||
return handle_hid_transport_state_event(cast_hid_transport_state_event(aeh));
|
||||
if (is_hid_channel_state_event(aeh)) {
|
||||
return handle_hid_channel_state_event(
|
||||
cast_hid_channel_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_hid_report_sent_event(aeh)) {
|
||||
return handle_hid_report_sent_event(cast_hid_report_sent_event(aeh));
|
||||
return handle_hid_report_sent_event(
|
||||
cast_hid_report_sent_event(aeh));
|
||||
}
|
||||
|
||||
if (is_mode_switch_event(aeh)) {
|
||||
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
||||
if (is_transport_policy_event(aeh)) {
|
||||
return handle_transport_policy_event(
|
||||
cast_transport_policy_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);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -460,9 +484,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, keyboard_hid_report_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_transport_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_channel_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, hid_report_sent_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, transport_policy_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
@@ -15,12 +15,14 @@
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "encoder_event.h"
|
||||
#include "function_bitmap_state_event.h"
|
||||
#include "function_bitmap_update_event.h"
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
#include "key_function_event.h"
|
||||
#include "mode_switch_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
#include "settings_mode_event.h"
|
||||
#include "set_protocol_event.h"
|
||||
#include "transport_policy_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -85,26 +87,52 @@ static const uint16_t consumer_usage_map[KEYBOARD_CONSUMER_CTRL_COUNT] = {
|
||||
[KEYBOARD_CONSUMER_CTRL_PREV_TRACK] = 0x00B6,
|
||||
};
|
||||
|
||||
static struct keyboard_state keyboard_state;
|
||||
static struct keyboard_reports_cache reports_cache;
|
||||
static uint8_t function_usage_mask[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
static enum keyboard_protocol_mode transport_protocol_modes[HID_TRANSPORT_COUNT] = {
|
||||
struct keyboard_core_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
struct keyboard_state keyboard_state;
|
||||
struct keyboard_reports_cache reports_cache;
|
||||
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 settings_active;
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
|
||||
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
||||
.mode = ML_MODE_POWER,
|
||||
.stopped_state = MODULE_STATE_STANDBY,
|
||||
};
|
||||
|
||||
static const struct module_lifecycle_ops lifecycle_ops = {
|
||||
.do_init = do_init,
|
||||
.do_start = do_start,
|
||||
.do_stop = do_stop,
|
||||
};
|
||||
|
||||
static struct keyboard_core_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.transport_protocol_modes = {
|
||||
[HID_TRANSPORT_USB] = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
[HID_TRANSPORT_BLE] = KEYBOARD_PROTOCOL_MODE_REPORT,
|
||||
},
|
||||
};
|
||||
static enum mode_switch_mode current_mode;
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool mode_valid;
|
||||
|
||||
static bool mode_to_transport(enum mode_switch_mode mode, enum hid_transport *transport)
|
||||
static bool policy_to_transport(enum hid_transport_policy policy,
|
||||
enum hid_transport *transport)
|
||||
{
|
||||
switch (mode) {
|
||||
case MODE_SWITCH_USB:
|
||||
switch (policy) {
|
||||
case HID_TRANSPORT_POLICY_USB:
|
||||
*transport = HID_TRANSPORT_USB;
|
||||
return true;
|
||||
|
||||
case MODE_SWITCH_BLE:
|
||||
case HID_TRANSPORT_POLICY_BLE:
|
||||
*transport = HID_TRANSPORT_BLE;
|
||||
return true;
|
||||
|
||||
@@ -117,13 +145,32 @@ static enum keyboard_protocol_mode active_protocol_mode_get(void)
|
||||
{
|
||||
enum hid_transport transport;
|
||||
|
||||
if (mode_valid && mode_to_transport(current_mode, &transport)) {
|
||||
return transport_protocol_modes[transport];
|
||||
if (policy_to_transport(ctx.current_transport, &transport)) {
|
||||
return ctx.transport_protocol_modes[transport];
|
||||
}
|
||||
|
||||
return KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
}
|
||||
|
||||
static bool transport_policy_to_mode(enum hid_transport_policy policy,
|
||||
enum mode_switch_mode *mode)
|
||||
{
|
||||
if (mode == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (policy) {
|
||||
case HID_TRANSPORT_POLICY_USB:
|
||||
*mode = MODE_SWITCH_USB;
|
||||
return true;
|
||||
case HID_TRANSPORT_POLICY_BLE:
|
||||
*mode = MODE_SWITCH_BLE;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct keymap_entry *keymap_get(uint16_t key_id)
|
||||
{
|
||||
size_t left = 0;
|
||||
@@ -146,13 +193,40 @@ static const struct keymap_entry *keymap_get(uint16_t key_id)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_test(const uint8_t *bitmap, uint16_t usage_id)
|
||||
static bool usage_to_bitmap_pos(uint16_t usage_id, uint8_t *byte_idx,
|
||||
uint8_t *bit_idx)
|
||||
{
|
||||
if ((bitmap == NULL) || (usage_id > KEYBOARD_PROTOCOL_USAGE_MAX)) {
|
||||
if ((byte_idx == NULL) || (bit_idx == NULL)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bitmap[usage_id / 8U] & BIT(usage_id % 8U)) != 0U;
|
||||
if ((usage_id >= KEYBOARD_USAGE_FIRST_MODIFIER) &&
|
||||
(usage_id <= KEYBOARD_USAGE_LAST_MODIFIER)) {
|
||||
*byte_idx = 0U;
|
||||
*bit_idx = (uint8_t)(usage_id - KEYBOARD_USAGE_FIRST_MODIFIER);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (usage_id <= KEYBOARD_NKRO_USAGE_MAX) {
|
||||
*byte_idx = (uint8_t)(1U + (usage_id / 8U));
|
||||
*bit_idx = (uint8_t)(usage_id % 8U);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_test(const uint8_t *bitmap, uint16_t usage_id)
|
||||
{
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
|
||||
if ((bitmap == NULL) ||
|
||||
!usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
}
|
||||
|
||||
static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
||||
@@ -161,13 +235,12 @@ static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
||||
uint8_t bit_idx;
|
||||
bool was_pressed;
|
||||
|
||||
if ((bitmap == NULL) || (usage_id > KEYBOARD_PROTOCOL_USAGE_MAX)) {
|
||||
if ((bitmap == NULL) ||
|
||||
!usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx)) {
|
||||
LOG_WRN("Unsupported keyboard usage 0x%04x", usage_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
byte_idx = usage_id / 8U;
|
||||
bit_idx = usage_id % 8U;
|
||||
was_pressed = (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||
|
||||
if (was_pressed == pressed) {
|
||||
@@ -185,38 +258,39 @@ static bool consumer_key_update(uint16_t consumer_id, bool pressed)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool was_pressed = (keyboard_state.consumer_bits & BIT(consumer_id)) != 0U;
|
||||
bool was_pressed =
|
||||
(ctx.keyboard_state.consumer_bits & BIT(consumer_id)) != 0U;
|
||||
|
||||
if (was_pressed == pressed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
WRITE_BIT(keyboard_state.consumer_bits, consumer_id, pressed);
|
||||
WRITE_BIT(ctx.keyboard_state.consumer_bits, consumer_id, pressed);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void keyboard_state_clear(void)
|
||||
{
|
||||
memset(&keyboard_state, 0, sizeof(keyboard_state));
|
||||
memset(&ctx.keyboard_state, 0, sizeof(ctx.keyboard_state));
|
||||
}
|
||||
|
||||
static void function_usage_mask_clear(void)
|
||||
{
|
||||
memset(function_usage_mask, 0, sizeof(function_usage_mask));
|
||||
memset(ctx.function_usage_mask, 0, sizeof(ctx.function_usage_mask));
|
||||
}
|
||||
|
||||
static void reports_cache_invalidate(void)
|
||||
{
|
||||
reports_cache.boot_valid = false;
|
||||
reports_cache.nkro_valid = false;
|
||||
reports_cache.consumer_valid = false;
|
||||
ctx.reports_cache.boot_valid = false;
|
||||
ctx.reports_cache.nkro_valid = false;
|
||||
ctx.reports_cache.consumer_valid = false;
|
||||
}
|
||||
|
||||
static void build_effective_hid_bitmap(uint8_t bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES])
|
||||
{
|
||||
for (size_t i = 0; i < KEYBOARD_PROTOCOL_BITMAP_BYTES; i++) {
|
||||
bitmap[i] = keyboard_state.pressed_usage_bitmap[i] &
|
||||
(uint8_t)~keyboard_state.function_pressed_bitmap[i];
|
||||
bitmap[i] = ctx.keyboard_state.pressed_usage_bitmap[i] &
|
||||
(uint8_t)~ctx.keyboard_state.function_pressed_bitmap[i];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,12 +301,14 @@ static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
|
||||
|
||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
|
||||
report[0] = effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES - 1U];
|
||||
report[0] = effective_hid_bitmap[0];
|
||||
report[1] = KEYBOARD_BOOT_RESERVED_BYTE;
|
||||
|
||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
||||
uint8_t byte_idx = usage_id / 8U;
|
||||
uint8_t bit_idx = usage_id % 8U;
|
||||
uint8_t byte_idx;
|
||||
uint8_t bit_idx;
|
||||
|
||||
(void)usage_to_bitmap_pos(usage_id, &byte_idx, &bit_idx);
|
||||
|
||||
if ((effective_hid_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
|
||||
continue;
|
||||
@@ -254,14 +330,14 @@ static void build_nkro_report(uint8_t report[KEYBOARD_NKRO_REPORT_SIZE])
|
||||
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||
|
||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||
report[0] = effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES - 1U];
|
||||
memcpy(&report[1], effective_hid_bitmap, KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
report[0] = effective_hid_bitmap[0];
|
||||
memcpy(&report[1], &effective_hid_bitmap[1], KEYBOARD_NKRO_BITMAP_BYTES);
|
||||
}
|
||||
|
||||
static uint16_t active_consumer_usage_get(void)
|
||||
{
|
||||
for (uint8_t consumer_id = 0; consumer_id < KEYBOARD_CONSUMER_CTRL_COUNT; consumer_id++) {
|
||||
if ((keyboard_state.consumer_bits & BIT(consumer_id)) != 0U) {
|
||||
if ((ctx.keyboard_state.consumer_bits & BIT(consumer_id)) != 0U) {
|
||||
return consumer_usage_map[consumer_id];
|
||||
}
|
||||
}
|
||||
@@ -274,47 +350,23 @@ static void build_consumer_report(uint8_t report[KEYBOARD_CONSUMER_REPORT_SIZE])
|
||||
sys_put_le16(active_consumer_usage_get(), report);
|
||||
}
|
||||
|
||||
static void submit_keyboard_report_event(enum keyboard_report_type report_type,
|
||||
enum hid_queue_policy queue_policy,
|
||||
const uint8_t *data, size_t size)
|
||||
{
|
||||
struct keyboard_hid_report_event *event =
|
||||
new_keyboard_hid_report_event(size);
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
|
||||
event->mode = current_mode;
|
||||
event->report_type = report_type;
|
||||
event->protocol_mode = protocol_mode;
|
||||
event->queue_policy = queue_policy;
|
||||
memcpy(event->dyndata.data, data, size);
|
||||
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_key_function_event(uint16_t usage_id, uint8_t action)
|
||||
{
|
||||
struct key_function_event *event = new_key_function_event();
|
||||
|
||||
event->usage = usage_id;
|
||||
event->action = action;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
|
||||
static void submit_consumer_fifo_frame(uint16_t usage_id)
|
||||
{
|
||||
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
enum mode_switch_mode mode;
|
||||
|
||||
if (!running || !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)) {
|
||||
return;
|
||||
}
|
||||
|
||||
sys_put_le16(usage_id, report_buf);
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
HID_QUEUE_POLICY_FIFO,
|
||||
report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_FIFO, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
|
||||
static void submit_consumer_pulse_frames(enum keyboard_consumer_control control_id,
|
||||
@@ -350,21 +402,26 @@ static void emit_keys_report(bool force)
|
||||
uint8_t *cache_buf;
|
||||
bool *cache_valid;
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
enum mode_switch_mode mode;
|
||||
|
||||
if (!mode_valid) {
|
||||
if (!transport_policy_to_mode(ctx.current_transport, &mode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx.settings_active) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||
build_boot_report(report_buf);
|
||||
report_size = KEYBOARD_BOOT_REPORT_SIZE;
|
||||
cache_buf = reports_cache.boot_report;
|
||||
cache_valid = &reports_cache.boot_valid;
|
||||
cache_buf = ctx.reports_cache.boot_report;
|
||||
cache_valid = &ctx.reports_cache.boot_valid;
|
||||
} else {
|
||||
build_nkro_report(report_buf);
|
||||
report_size = KEYBOARD_NKRO_REPORT_SIZE;
|
||||
cache_buf = reports_cache.nkro_report;
|
||||
cache_valid = &reports_cache.nkro_valid;
|
||||
cache_buf = ctx.reports_cache.nkro_report;
|
||||
cache_valid = &ctx.reports_cache.nkro_valid;
|
||||
}
|
||||
|
||||
if (!force && *cache_valid && (memcmp(cache_buf, report_buf, report_size) == 0)) {
|
||||
@@ -374,34 +431,37 @@ static void emit_keys_report(bool force)
|
||||
memcpy(cache_buf, report_buf, report_size);
|
||||
*cache_valid = true;
|
||||
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_KEYS,
|
||||
HID_QUEUE_POLICY_LATEST,
|
||||
report_buf,
|
||||
report_size);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, report_buf, report_size);
|
||||
}
|
||||
|
||||
static void emit_consumer_report(bool force)
|
||||
{
|
||||
uint8_t report_buf[KEYBOARD_CONSUMER_REPORT_SIZE];
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
enum mode_switch_mode mode;
|
||||
|
||||
if (!mode_valid || (protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||
if (!transport_policy_to_mode(ctx.current_transport, &mode) ||
|
||||
ctx.settings_active ||
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
build_consumer_report(report_buf);
|
||||
if (!force && reports_cache.consumer_valid &&
|
||||
(memcmp(reports_cache.consumer_report, report_buf,
|
||||
if (!force && ctx.reports_cache.consumer_valid &&
|
||||
(memcmp(ctx.reports_cache.consumer_report, report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE) == 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(reports_cache.consumer_report, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
reports_cache.consumer_valid = true;
|
||||
memcpy(ctx.reports_cache.consumer_report, report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
ctx.reports_cache.consumer_valid = true;
|
||||
|
||||
submit_keyboard_report_event(KEYBOARD_REPORT_TYPE_CONSUMER,
|
||||
HID_QUEUE_POLICY_LATEST,
|
||||
report_buf,
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
|
||||
@@ -414,84 +474,84 @@ static void emit_all_reports(bool force)
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_function_release_events(void)
|
||||
static void emit_function_state_event(void)
|
||||
{
|
||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_PROTOCOL_USAGE_MAX; usage_id++) {
|
||||
if (!usage_bitmap_test(keyboard_state.function_pressed_bitmap, usage_id)) {
|
||||
continue;
|
||||
(void)submit_function_bitmap_state_event(
|
||||
ctx.keyboard_state.pressed_usage_bitmap);
|
||||
}
|
||||
|
||||
submit_key_function_event(usage_id, KEY_FUNCTION_ACTION_RELEASE);
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_release_reports(enum mode_switch_mode mode)
|
||||
static void emit_release_reports(enum hid_transport_policy transport_policy)
|
||||
{
|
||||
struct keyboard_hid_report_event *event;
|
||||
uint8_t keys_report[KEYBOARD_NKRO_REPORT_SIZE] = { 0 };
|
||||
uint8_t consumer_report[KEYBOARD_CONSUMER_REPORT_SIZE] = { 0 };
|
||||
enum keyboard_protocol_mode protocol_mode = active_protocol_mode_get();
|
||||
enum mode_switch_mode mode;
|
||||
size_t keys_report_size =
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT) ?
|
||||
KEYBOARD_BOOT_REPORT_SIZE : KEYBOARD_NKRO_REPORT_SIZE;
|
||||
|
||||
event = new_keyboard_hid_report_event(keys_report_size);
|
||||
event->mode = mode;
|
||||
event->report_type = KEYBOARD_REPORT_TYPE_KEYS;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, keys_report, keys_report_size);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
switch (transport_policy) {
|
||||
case HID_TRANSPORT_POLICY_USB:
|
||||
mode = MODE_SWITCH_USB;
|
||||
break;
|
||||
case HID_TRANSPORT_POLICY_BLE:
|
||||
mode = MODE_SWITCH_BLE;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, keys_report, keys_report_size);
|
||||
|
||||
if (protocol_mode != KEYBOARD_PROTOCOL_MODE_BOOT) {
|
||||
event = new_keyboard_hid_report_event(KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
event->mode = mode;
|
||||
event->report_type = KEYBOARD_REPORT_TYPE_CONSUMER;
|
||||
event->protocol_mode = protocol_mode;
|
||||
memcpy(event->dyndata.data, consumer_report,
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, consumer_report,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
APP_EVENT_SUBMIT(event);
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
function_usage_mask_clear();
|
||||
mode_valid = false;
|
||||
transport_protocol_modes[HID_TRANSPORT_USB] =
|
||||
ctx.settings_active = false;
|
||||
ctx.current_transport = HID_TRANSPORT_POLICY_USB;
|
||||
ctx.transport_protocol_modes[HID_TRANSPORT_USB] =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
transport_protocol_modes[HID_TRANSPORT_BLE] =
|
||||
ctx.transport_protocol_modes[HID_TRANSPORT_BLE] =
|
||||
KEYBOARD_PROTOCOL_MODE_REPORT;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (mode_valid) {
|
||||
emit_release_reports(current_mode);
|
||||
if (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) {
|
||||
emit_release_reports(ctx.current_transport);
|
||||
}
|
||||
emit_function_release_events();
|
||||
emit_function_state_event();
|
||||
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
mode_valid = false;
|
||||
running = false;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool handle_button_event(const struct button_event *event)
|
||||
@@ -499,7 +559,11 @@ static bool handle_button_event(const struct button_event *event)
|
||||
const struct keymap_entry *entry;
|
||||
bool changed;
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ctx.settings_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -512,7 +576,7 @@ static bool handle_button_event(const struct button_event *event)
|
||||
if (entry->usage_type == KEY_USAGE_TYPE_KEYBOARD) {
|
||||
bool routed_to_function;
|
||||
|
||||
changed = usage_bitmap_write(keyboard_state.pressed_usage_bitmap,
|
||||
changed = usage_bitmap_write(ctx.keyboard_state.pressed_usage_bitmap,
|
||||
entry->usage_id, event->pressed);
|
||||
if (!changed) {
|
||||
return false;
|
||||
@@ -520,22 +584,19 @@ static bool handle_button_event(const struct button_event *event)
|
||||
|
||||
if (event->pressed) {
|
||||
routed_to_function =
|
||||
usage_bitmap_test(function_usage_mask, entry->usage_id);
|
||||
(void)usage_bitmap_write(keyboard_state.function_pressed_bitmap,
|
||||
usage_bitmap_test(ctx.function_usage_mask, entry->usage_id);
|
||||
(void)usage_bitmap_write(ctx.keyboard_state.function_pressed_bitmap,
|
||||
entry->usage_id, routed_to_function);
|
||||
} else {
|
||||
routed_to_function =
|
||||
usage_bitmap_test(keyboard_state.function_pressed_bitmap,
|
||||
usage_bitmap_test(ctx.keyboard_state.function_pressed_bitmap,
|
||||
entry->usage_id);
|
||||
(void)usage_bitmap_write(keyboard_state.function_pressed_bitmap,
|
||||
(void)usage_bitmap_write(ctx.keyboard_state.function_pressed_bitmap,
|
||||
entry->usage_id, false);
|
||||
}
|
||||
|
||||
if (routed_to_function) {
|
||||
submit_key_function_event(entry->usage_id,
|
||||
event->pressed ?
|
||||
KEY_FUNCTION_ACTION_PRESS :
|
||||
KEY_FUNCTION_ACTION_RELEASE);
|
||||
emit_function_state_event();
|
||||
} else {
|
||||
emit_keys_report(false);
|
||||
}
|
||||
@@ -549,33 +610,42 @@ static bool handle_button_event(const struct button_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
||||
static bool handle_transport_policy_event(
|
||||
const struct transport_policy_event *event)
|
||||
{
|
||||
bool mode_changed;
|
||||
bool transport_changed;
|
||||
|
||||
if (!running) {
|
||||
current_mode = event->mode;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
ctx.current_transport = event->hid_transport;
|
||||
return false;
|
||||
}
|
||||
|
||||
mode_changed = mode_valid && (current_mode != event->mode);
|
||||
if (mode_changed) {
|
||||
emit_release_reports(current_mode);
|
||||
emit_function_release_events();
|
||||
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();
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
}
|
||||
|
||||
current_mode = event->mode;
|
||||
mode_valid = true;
|
||||
ctx.current_transport = event->hid_transport;
|
||||
|
||||
if (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) {
|
||||
emit_all_reports(true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_encoder_event(const struct encoder_event *event)
|
||||
{
|
||||
if (!running || !mode_valid) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc) ||
|
||||
(ctx.current_transport == HID_TRANSPORT_POLICY_NONE)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ctx.settings_active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -593,7 +663,8 @@ static bool handle_encoder_event(const struct encoder_event *event)
|
||||
static bool handle_function_bitmap_update_event(
|
||||
const struct function_bitmap_update_event *event)
|
||||
{
|
||||
memcpy(function_usage_mask, event->bitmap, sizeof(function_usage_mask));
|
||||
memcpy(ctx.function_usage_mask, event->bitmap,
|
||||
sizeof(ctx.function_usage_mask));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -620,11 +691,13 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (transport_protocol_modes[event->transport] != event->protocol_mode) {
|
||||
transport_protocol_modes[event->transport] = event->protocol_mode;
|
||||
if (ctx.transport_protocol_modes[event->transport] !=
|
||||
event->protocol_mode) {
|
||||
ctx.transport_protocol_modes[event->transport] =
|
||||
event->protocol_mode;
|
||||
|
||||
if (running && mode_valid &&
|
||||
mode_to_transport(current_mode, &active_transport) &&
|
||||
if (module_lifecycle_is_running(&ctx.lc) &&
|
||||
policy_to_transport(ctx.current_transport, &active_transport) &&
|
||||
(active_transport == event->transport)) {
|
||||
reports_cache_invalidate();
|
||||
emit_keys_report(true);
|
||||
@@ -638,55 +711,53 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_mode_switch_event(aeh)) {
|
||||
return handle_mode_switch_event(cast_mode_switch_event(aeh));
|
||||
if (is_transport_policy_event(aeh)) {
|
||||
return handle_transport_policy_event(
|
||||
cast_transport_policy_event(aeh));
|
||||
}
|
||||
|
||||
if (is_settings_mode_event(aeh)) {
|
||||
const struct settings_mode_event *event =
|
||||
cast_settings_mode_event(aeh);
|
||||
|
||||
if (ctx.settings_active == event->active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx.settings_active = event->active;
|
||||
if (ctx.settings_active) {
|
||||
if (ctx.current_transport != HID_TRANSPORT_POLICY_NONE) {
|
||||
emit_release_reports(ctx.current_transport);
|
||||
}
|
||||
emit_function_state_event();
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
int err;
|
||||
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -701,7 +772,8 @@ APP_EVENT_SUBSCRIBE(MODULE, button_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, encoder_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, function_bitmap_update_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, set_protocol_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, mode_switch_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, settings_mode_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, transport_policy_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
155
src/led_effect/effects/led_effect_key_fade.c
Normal file
155
src/led_effect/effects/led_effect_key_fade.c
Normal file
@@ -0,0 +1,155 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <zephyr/sys/util.h>
|
||||
|
||||
#include "led_effect/led_effect.h"
|
||||
|
||||
#define KEY_FADE_LEVEL_MAX 255U
|
||||
#define KEY_FADE_STEP_PER_TICK 12U
|
||||
|
||||
struct led_effect_key_fade_state {
|
||||
const struct led_key_map *key_map;
|
||||
size_t key_map_len;
|
||||
size_t pixel_count;
|
||||
uint8_t default_brightness;
|
||||
uint8_t level[17];
|
||||
struct theme_rgb theme;
|
||||
};
|
||||
|
||||
static struct led_effect_key_fade_state key_fade_state;
|
||||
|
||||
static const struct led_key_map *find_key(const struct led_effect *effect,
|
||||
uint16_t key_id)
|
||||
{
|
||||
const struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
for (size_t i = 0; i < state->key_map_len; i++) {
|
||||
if (state->key_map[i].key_id == key_id) {
|
||||
return &state->key_map[i];
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int led_effect_key_fade_init(struct led_effect *effect,
|
||||
const struct led_effect_config *cfg)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
if ((effect == NULL) || (cfg == NULL) || (cfg->key_map == NULL) ||
|
||||
(cfg->pixel_count > ARRAY_SIZE(state->level))) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
memset(state, 0, sizeof(*state));
|
||||
state->key_map = cfg->key_map;
|
||||
state->key_map_len = cfg->key_map_len;
|
||||
state->pixel_count = cfg->pixel_count;
|
||||
state->default_brightness = cfg->default_brightness;
|
||||
state->theme.r = BLINKY_THEME_DEFAULT_R;
|
||||
state->theme.g = BLINKY_THEME_DEFAULT_G;
|
||||
state->theme.b = BLINKY_THEME_DEFAULT_B;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void led_effect_key_fade_reset(struct led_effect *effect)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
memset(state->level, 0, sizeof(state->level));
|
||||
}
|
||||
|
||||
static void led_effect_key_fade_set_theme(struct led_effect *effect,
|
||||
const struct theme_rgb *theme)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
if (theme == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
state->theme = *theme;
|
||||
}
|
||||
|
||||
static void led_effect_key_fade_on_key_press(struct led_effect *effect,
|
||||
uint16_t key_id)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
const struct led_key_map *map = find_key(effect, key_id);
|
||||
|
||||
if ((map == NULL) || (map->led_idx >= state->pixel_count)) {
|
||||
return;
|
||||
}
|
||||
|
||||
state->level[map->led_idx] =
|
||||
(state->default_brightness == 0U) ? KEY_FADE_LEVEL_MAX :
|
||||
state->default_brightness;
|
||||
}
|
||||
|
||||
static bool led_effect_key_fade_tick(struct led_effect *effect, uint32_t dt_ms,
|
||||
struct led_rgb *pixels, size_t pixel_count)
|
||||
{
|
||||
struct led_effect_key_fade_state *state = effect->state;
|
||||
bool active = false;
|
||||
bool visible = false;
|
||||
uint8_t decay;
|
||||
|
||||
ARG_UNUSED(dt_ms);
|
||||
|
||||
if ((pixels == NULL) || (pixel_count < state->pixel_count)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memset(pixels, 0, sizeof(*pixels) * pixel_count);
|
||||
decay = KEY_FADE_STEP_PER_TICK;
|
||||
|
||||
for (size_t i = 0; i < state->pixel_count; i++) {
|
||||
uint8_t level = state->level[i];
|
||||
|
||||
if (level == 0U) {
|
||||
continue;
|
||||
}
|
||||
|
||||
visible = true;
|
||||
pixels[i].r = (uint8_t)(((uint16_t)state->theme.r * level) / 255U);
|
||||
pixels[i].g = (uint8_t)(((uint16_t)state->theme.g * level) / 255U);
|
||||
pixels[i].b = (uint8_t)(((uint16_t)state->theme.b * level) / 255U);
|
||||
|
||||
state->level[i] = (level > decay) ? (uint8_t)(level - decay) : 0U;
|
||||
active = active || (state->level[i] > 0U);
|
||||
}
|
||||
|
||||
return visible || active;
|
||||
}
|
||||
|
||||
static bool led_effect_key_fade_is_active(const struct led_effect *effect)
|
||||
{
|
||||
const struct led_effect_key_fade_state *state = effect->state;
|
||||
|
||||
for (size_t i = 0; i < state->pixel_count; i++) {
|
||||
if (state->level[i] > 0U) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static const struct led_effect_ops key_fade_ops = {
|
||||
.init = led_effect_key_fade_init,
|
||||
.reset = led_effect_key_fade_reset,
|
||||
.set_theme = led_effect_key_fade_set_theme,
|
||||
.on_key_press = led_effect_key_fade_on_key_press,
|
||||
.tick = led_effect_key_fade_tick,
|
||||
.is_active = led_effect_key_fade_is_active,
|
||||
};
|
||||
|
||||
struct led_effect led_effect_key_fade = {
|
||||
.ops = &key_fade_ops,
|
||||
.state = &key_fade_state,
|
||||
};
|
||||
50
src/led_effect/led_effect.h
Normal file
50
src/led_effect/led_effect.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef BLINKY_LED_EFFECT_H_
|
||||
#define BLINKY_LED_EFFECT_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <zephyr/drivers/led_strip.h>
|
||||
|
||||
#include "led_effect_types.h"
|
||||
#include "theme_color.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct led_effect;
|
||||
|
||||
struct led_effect_config {
|
||||
const struct led_key_map *key_map;
|
||||
size_t key_map_len;
|
||||
size_t pixel_count;
|
||||
uint8_t default_brightness;
|
||||
};
|
||||
|
||||
struct led_effect_ops {
|
||||
int (*init)(struct led_effect *effect,
|
||||
const struct led_effect_config *cfg);
|
||||
void (*reset)(struct led_effect *effect);
|
||||
void (*set_theme)(struct led_effect *effect,
|
||||
const struct theme_rgb *theme);
|
||||
void (*on_key_press)(struct led_effect *effect, uint16_t key_id);
|
||||
bool (*tick)(struct led_effect *effect, uint32_t dt_ms,
|
||||
struct led_rgb *pixels, size_t pixel_count);
|
||||
bool (*is_active)(const struct led_effect *effect);
|
||||
};
|
||||
|
||||
struct led_effect {
|
||||
const struct led_effect_ops *ops;
|
||||
void *state;
|
||||
};
|
||||
|
||||
const struct led_effect *led_effect_get(enum led_effect_id id);
|
||||
struct led_effect *led_effect_get_mutable(enum led_effect_id id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_LED_EFFECT_H_ */
|
||||
27
src/led_effect/led_effect_registry.c
Normal file
27
src/led_effect/led_effect_registry.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <stddef.h>
|
||||
|
||||
#include "led_effect.h"
|
||||
|
||||
extern struct led_effect led_effect_key_fade;
|
||||
|
||||
static struct led_effect *const effect_registry[LED_EFFECT_ID_COUNT] = {
|
||||
[LED_EFFECT_ID_KEY_FADE] = &led_effect_key_fade,
|
||||
};
|
||||
|
||||
const struct led_effect *led_effect_get(enum led_effect_id id)
|
||||
{
|
||||
if (id >= LED_EFFECT_ID_COUNT) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return effect_registry[id];
|
||||
}
|
||||
|
||||
struct led_effect *led_effect_get_mutable(enum led_effect_id id)
|
||||
{
|
||||
if (id >= LED_EFFECT_ID_COUNT) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return effect_registry[id];
|
||||
}
|
||||
24
src/led_effect/led_effect_types.h
Normal file
24
src/led_effect/led_effect_types.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef BLINKY_LED_EFFECT_TYPES_H_
|
||||
#define BLINKY_LED_EFFECT_TYPES_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct led_key_map {
|
||||
uint16_t key_id;
|
||||
uint8_t led_idx;
|
||||
};
|
||||
|
||||
enum led_effect_id {
|
||||
LED_EFFECT_ID_KEY_FADE = 0,
|
||||
LED_EFFECT_ID_COUNT,
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* BLINKY_LED_EFFECT_TYPES_H_ */
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user