feat(events): 添加传输策略事件支持
添加了新的 transport_policy_event 事件类型,用于管理 HID 传输策略和蓝牙配置文件策略。该事件包含源模式、HID 传输策 略和蓝牙配置文件策略三个枚举值,并提供了相应的提交函数 和日志记录功能。 BREAKING CHANGE: 将原有的 mode_switch_event 替换为更具体的 transport_policy_event,相关模块需要更新以使用新的事件类 型。
This commit is contained in:
@@ -20,8 +20,8 @@
|
||||
#include "keyboard_core.h"
|
||||
#include "keyboard_hid_report_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
#include "mode_switch_event.h"
|
||||
#include "set_protocol_event.h"
|
||||
#include "transport_policy_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -92,7 +92,7 @@ struct keyboard_core_module_ctx {
|
||||
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 mode_switch_mode current_mode;
|
||||
enum hid_transport_policy current_transport;
|
||||
bool mode_valid;
|
||||
};
|
||||
|
||||
@@ -128,18 +128,19 @@ static struct keyboard_core_module_ctx ctx = {
|
||||
#define reports_cache ctx.reports_cache
|
||||
#define function_usage_mask ctx.function_usage_mask
|
||||
#define transport_protocol_modes ctx.transport_protocol_modes
|
||||
#define current_mode ctx.current_mode
|
||||
#define current_transport ctx.current_transport
|
||||
#define mode_valid ctx.mode_valid
|
||||
#define running module_lifecycle_is_running(&ctx.lc)
|
||||
|
||||
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;
|
||||
|
||||
@@ -152,13 +153,32 @@ static enum keyboard_protocol_mode active_protocol_mode_get(void)
|
||||
{
|
||||
enum hid_transport transport;
|
||||
|
||||
if (mode_valid && mode_to_transport(current_mode, &transport)) {
|
||||
if (mode_valid && policy_to_transport(current_transport, &transport)) {
|
||||
return 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;
|
||||
@@ -341,15 +361,17 @@ 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 ||
|
||||
!transport_policy_to_mode(current_transport, &mode) ||
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
sys_put_le16(usage_id, report_buf);
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_FIFO, report_buf, KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
|
||||
@@ -386,8 +408,9 @@ 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 (!mode_valid || !transport_policy_to_mode(current_transport, &mode)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -411,7 +434,7 @@ static void emit_keys_report(bool force)
|
||||
*cache_valid = true;
|
||||
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||
mode, KEYBOARD_REPORT_TYPE_KEYS, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, report_buf, report_size);
|
||||
}
|
||||
|
||||
@@ -419,8 +442,10 @@ 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 (!mode_valid || !transport_policy_to_mode(current_transport, &mode) ||
|
||||
(protocol_mode == KEYBOARD_PROTOCOL_MODE_BOOT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -435,7 +460,7 @@ static void emit_consumer_report(bool force)
|
||||
reports_cache.consumer_valid = true;
|
||||
|
||||
(void)submit_keyboard_hid_report_event(
|
||||
current_mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
mode, KEYBOARD_REPORT_TYPE_CONSUMER, protocol_mode,
|
||||
HID_QUEUE_POLICY_LATEST, report_buf,
|
||||
KEYBOARD_CONSUMER_REPORT_SIZE);
|
||||
}
|
||||
@@ -454,15 +479,27 @@ static void emit_function_state_event(void)
|
||||
(void)submit_function_bitmap_state_event(keyboard_state.pressed_usage_bitmap);
|
||||
}
|
||||
|
||||
static void emit_release_reports(enum mode_switch_mode mode)
|
||||
static void emit_release_reports(enum hid_transport_policy transport_policy)
|
||||
{
|
||||
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;
|
||||
|
||||
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);
|
||||
@@ -505,7 +542,7 @@ static int do_stop(void)
|
||||
}
|
||||
|
||||
if (mode_valid) {
|
||||
emit_release_reports(current_mode);
|
||||
emit_release_reports(current_transport);
|
||||
}
|
||||
emit_function_state_event();
|
||||
|
||||
@@ -568,26 +605,30 @@ 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;
|
||||
current_transport = event->hid_transport;
|
||||
return false;
|
||||
}
|
||||
|
||||
mode_changed = mode_valid && (current_mode != event->mode);
|
||||
if (mode_changed) {
|
||||
emit_release_reports(current_mode);
|
||||
transport_changed = mode_valid && (current_transport != event->hid_transport);
|
||||
if (transport_changed) {
|
||||
emit_release_reports(current_transport);
|
||||
emit_function_state_event();
|
||||
keyboard_state_clear();
|
||||
reports_cache_invalidate();
|
||||
}
|
||||
|
||||
current_mode = event->mode;
|
||||
mode_valid = true;
|
||||
emit_all_reports(true);
|
||||
current_transport = event->hid_transport;
|
||||
mode_valid = (current_transport != HID_TRANSPORT_POLICY_NONE);
|
||||
|
||||
if (mode_valid) {
|
||||
emit_all_reports(true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -643,7 +684,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
transport_protocol_modes[event->transport] = event->protocol_mode;
|
||||
|
||||
if (running && mode_valid &&
|
||||
mode_to_transport(current_mode, &active_transport) &&
|
||||
policy_to_transport(current_transport, &active_transport) &&
|
||||
(active_transport == event->transport)) {
|
||||
reports_cache_invalidate();
|
||||
emit_keys_report(true);
|
||||
@@ -657,8 +698,9 @@ 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_module_state_event(aeh)) {
|
||||
@@ -696,7 +738,7 @@ 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, 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);
|
||||
|
||||
Reference in New Issue
Block a user