feat(events): 添加传输策略事件支持

添加了新的 transport_policy_event 事件类型,用于管理 HID
传输策略和蓝牙配置文件策略。该事件包含源模式、HID 传输策
略和蓝牙配置文件策略三个枚举值,并提供了相应的提交函数
和日志记录功能。

BREAKING CHANGE: 将原有的 mode_switch_event 替换为更具体的
transport_policy_event,相关模块需要更新以使用新的事件类
型。
This commit is contained in:
2026-04-23 09:48:06 +08:00
parent a11f4c0110
commit 6a03df1b39
6 changed files with 289 additions and 54 deletions

View File

@@ -16,7 +16,7 @@
#include "keyboard_core.h"
#include "keyboard_hid_report_event.h"
#include "module_lifecycle.h"
#include "mode_switch_event.h"
#include "transport_policy_event.h"
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
@@ -60,7 +60,7 @@ struct hid_flowctrl_module_ctx {
uint8_t consumer_fifo_tail;
uint8_t consumer_fifo_count;
struct in_flight_report in_flight[HID_SEND_CH_COUNT];
enum mode_switch_mode current_mode;
enum hid_transport_policy current_transport;
uint16_t next_sequence;
};
@@ -107,25 +107,25 @@ static struct hid_flowctrl_module_ctx ctx = {
#define consumer_fifo_tail ctx.consumer_fifo_tail
#define consumer_fifo_count ctx.consumer_fifo_count
#define in_flight ctx.in_flight
#define current_mode ctx.current_mode
#define current_transport ctx.current_transport
#define next_sequence ctx.next_sequence
#define running module_lifecycle_is_running(&ctx.lc)
static bool current_mode_to_channel(enum keyboard_report_type report_type,
enum hid_send_channel *channel)
static bool current_transport_to_channel(enum keyboard_report_type report_type,
enum hid_send_channel *channel)
{
if (channel == NULL) {
return false;
}
switch (current_mode) {
case MODE_SWITCH_USB:
switch (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:
case HID_TRANSPORT_POLICY_BLE:
*channel = HID_SEND_CH_BLE_SHARED;
return true;
@@ -209,7 +209,7 @@ static void try_send_keys(void)
return;
}
if (!current_mode_to_channel(KEYBOARD_REPORT_TYPE_KEYS, &channel)) {
if (!current_transport_to_channel(KEYBOARD_REPORT_TYPE_KEYS, &channel)) {
return;
}
@@ -238,7 +238,7 @@ static void try_send_consumer_fifo(void)
return;
}
if (!current_mode_to_channel(KEYBOARD_REPORT_TYPE_CONSUMER, &channel)) {
if (!current_transport_to_channel(KEYBOARD_REPORT_TYPE_CONSUMER, &channel)) {
return;
}
@@ -273,7 +273,7 @@ static void try_send_consumer_latest(void)
return;
}
if (!current_mode_to_channel(KEYBOARD_REPORT_TYPE_CONSUMER, &channel)) {
if (!current_transport_to_channel(KEYBOARD_REPORT_TYPE_CONSUMER, &channel)) {
return;
}
@@ -383,14 +383,15 @@ 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 = (current_transport != event->hid_transport);
current_mode = event->mode;
current_transport = event->hid_transport;
if (mode_changed || ((current_mode != MODE_SWITCH_USB) &&
(current_mode != MODE_SWITCH_BLE))) {
if (transport_changed ||
(current_transport == HID_TRANSPORT_POLICY_NONE)) {
clear_pending_reports();
}
@@ -401,7 +402,7 @@ static bool handle_mode_switch_event(const struct mode_switch_event *event)
static int do_init(void)
{
clear_pending_reports();
current_mode = MODE_SWITCH_USB;
current_transport = HID_TRANSPORT_POLICY_USB;
memset(channel_state, 0, sizeof(channel_state));
channel_state[HID_SEND_CH_USB_KEYS].protocol_mode =
KEYBOARD_PROTOCOL_MODE_REPORT;
@@ -451,8 +452,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
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)) {
@@ -490,7 +492,7 @@ APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, keyboard_hid_report_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);