feat(protocol): 添加键盘协议功能支持

添加了完整的键盘协议功能,包括:
- 新增多个事件类型:cdc_proto_tx_event、function_bitmap_update_event、key_function_event
- 在CMakeLists.txt中注册新的事件源文件
- 扩展keyboard_core.h定义键盘协议相关宏
- 增强protocol_module.h定义协议消息类型常量
- 更新protobuf定义device_comm.proto添加Bitmap、FunctionKeyEvent、Ack、Error消息
- 实现CDC协议包装器模块处理协议消息传输
- 改进键盘核心模块实现按键功能映射和位图管理
- 添加协议模块处理Hello、Bitmap、FunctionKeyEvent等协议消息
- 实现USB设备状态管理和错误响应机制
This commit is contained in:
2026-04-13 11:55:59 +08:00
parent d86f0d6b78
commit 30bc314698
14 changed files with 690 additions and 46 deletions

View File

@@ -15,8 +15,10 @@
#include <zephyr/sys/util.h>
#include "encoder_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 "set_protocol_event.h"
@@ -39,8 +41,8 @@ struct keymap_entry {
};
struct keyboard_state {
uint8_t modifiers;
uint8_t keys_bitmap[KEYBOARD_NKRO_BITMAP_BYTES];
uint8_t pressed_usage_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
uint8_t function_pressed_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
uint32_t consumer_bits;
};
@@ -85,6 +87,7 @@ static const uint16_t consumer_usage_map[KEYBOARD_CONSUMER_CTRL_COUNT] = {
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] = {
[HID_TRANSPORT_USB] = KEYBOARD_PROTOCOL_MODE_REPORT,
[HID_TRANSPORT_BLE] = KEYBOARD_PROTOCOL_MODE_REPORT,
@@ -143,40 +146,35 @@ static const struct keymap_entry *keymap_get(uint16_t key_id)
return NULL;
}
static bool usage_is_modifier(uint16_t usage_id)
static bool usage_bitmap_test(const uint8_t *bitmap, uint16_t usage_id)
{
return IN_RANGE(usage_id, KEYBOARD_USAGE_FIRST_MODIFIER,
KEYBOARD_USAGE_LAST_MODIFIER);
}
static bool keyboard_key_update(uint16_t usage_id, bool pressed)
{
if (usage_is_modifier(usage_id)) {
uint8_t new_modifiers = keyboard_state.modifiers;
WRITE_BIT(new_modifiers, usage_id - KEYBOARD_USAGE_FIRST_MODIFIER, pressed);
if (new_modifiers == keyboard_state.modifiers) {
return false;
}
keyboard_state.modifiers = new_modifiers;
return true;
if ((bitmap == NULL) || (usage_id > KEYBOARD_PROTOCOL_USAGE_MAX)) {
return false;
}
if (usage_id > KEYBOARD_NKRO_USAGE_MAX) {
return (bitmap[usage_id / 8U] & BIT(usage_id % 8U)) != 0U;
}
static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
{
uint8_t byte_idx;
uint8_t bit_idx;
bool was_pressed;
if ((bitmap == NULL) || (usage_id > KEYBOARD_PROTOCOL_USAGE_MAX)) {
LOG_WRN("Unsupported keyboard usage 0x%04x", usage_id);
return false;
}
uint8_t byte_idx = usage_id / 8U;
uint8_t bit_idx = usage_id % 8U;
bool was_pressed = (keyboard_state.keys_bitmap[byte_idx] & BIT(bit_idx)) != 0U;
byte_idx = usage_id / 8U;
bit_idx = usage_id % 8U;
was_pressed = (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
if (was_pressed == pressed) {
return false;
}
WRITE_BIT(keyboard_state.keys_bitmap[byte_idx], bit_idx, pressed);
WRITE_BIT(bitmap[byte_idx], bit_idx, pressed);
return true;
}
@@ -202,6 +200,11 @@ static void keyboard_state_clear(void)
memset(&keyboard_state, 0, sizeof(keyboard_state));
}
static void function_usage_mask_clear(void)
{
memset(function_usage_mask, 0, sizeof(function_usage_mask));
}
static void reports_cache_invalidate(void)
{
reports_cache.boot_valid = false;
@@ -209,19 +212,29 @@ static void reports_cache_invalidate(void)
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];
}
}
static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
{
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
size_t key_count = 0;
build_effective_hid_bitmap(effective_hid_bitmap);
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
report[0] = keyboard_state.modifiers;
report[0] = effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES - 1U];
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;
if ((keyboard_state.keys_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
if ((effective_hid_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
continue;
}
@@ -238,8 +251,11 @@ static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
static void build_nkro_report(uint8_t report[KEYBOARD_NKRO_REPORT_SIZE])
{
report[0] = keyboard_state.modifiers;
memcpy(&report[1], keyboard_state.keys_bitmap, KEYBOARD_NKRO_BITMAP_BYTES);
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);
}
static uint16_t active_consumer_usage_get(void)
@@ -275,6 +291,15 @@ static void submit_keyboard_report_event(enum keyboard_report_type report_type,
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];
@@ -389,6 +414,17 @@ static void emit_all_reports(bool force)
}
}
static void emit_function_release_events(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;
}
submit_key_function_event(usage_id, KEY_FUNCTION_ACTION_RELEASE);
}
}
static void emit_release_reports(enum mode_switch_mode mode)
{
struct keyboard_hid_report_event *event;
@@ -421,6 +457,7 @@ static int module_init(void)
{
keyboard_state_clear();
reports_cache_invalidate();
function_usage_mask_clear();
mode_valid = false;
transport_protocol_modes[HID_TRANSPORT_USB] =
KEYBOARD_PROTOCOL_MODE_REPORT;
@@ -449,6 +486,7 @@ static void module_pause(void)
if (mode_valid) {
emit_release_reports(current_mode);
}
emit_function_release_events();
keyboard_state_clear();
reports_cache_invalidate();
@@ -472,8 +510,33 @@ static bool handle_button_event(const struct button_event *event)
}
if (entry->usage_type == KEY_USAGE_TYPE_KEYBOARD) {
changed = keyboard_key_update(entry->usage_id, event->pressed);
if (changed) {
bool routed_to_function;
changed = usage_bitmap_write(keyboard_state.pressed_usage_bitmap,
entry->usage_id, event->pressed);
if (!changed) {
return false;
}
if (event->pressed) {
routed_to_function =
usage_bitmap_test(function_usage_mask, entry->usage_id);
(void)usage_bitmap_write(keyboard_state.function_pressed_bitmap,
entry->usage_id, routed_to_function);
} else {
routed_to_function =
usage_bitmap_test(keyboard_state.function_pressed_bitmap,
entry->usage_id);
(void)usage_bitmap_write(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);
} else {
emit_keys_report(false);
}
} else {
@@ -498,6 +561,7 @@ static bool handle_mode_switch_event(const struct mode_switch_event *event)
mode_changed = mode_valid && (current_mode != event->mode);
if (mode_changed) {
emit_release_reports(current_mode);
emit_function_release_events();
keyboard_state_clear();
reports_cache_invalidate();
}
@@ -526,6 +590,13 @@ static bool handle_encoder_event(const struct encoder_event *event)
return false;
}
static bool handle_function_bitmap_update_event(
const struct function_bitmap_update_event *event)
{
memcpy(function_usage_mask, event->bitmap, sizeof(function_usage_mask));
return false;
}
static bool app_event_handler(const struct app_event_header *aeh)
{
if (is_button_event(aeh)) {
@@ -536,6 +607,11 @@ static bool app_event_handler(const struct app_event_header *aeh)
return handle_encoder_event(cast_encoder_event(aeh));
}
if (is_function_bitmap_update_event(aeh)) {
return handle_function_bitmap_update_event(
cast_function_bitmap_update_event(aeh));
}
if (is_set_protocol_event(aeh)) {
const struct set_protocol_event *event = cast_set_protocol_event(aeh);
enum hid_transport active_transport;
@@ -623,6 +699,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
APP_EVENT_LISTENER(MODULE, app_event_handler);
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, module_state_event);