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

@@ -2,24 +2,42 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <app_event_manager.h>
#define MODULE protocol_module
#include <caf/events/module_state_event.h>
#include <caf/events/power_event.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/util.h>
#include <pb_decode.h>
#include <pb_encode.h>
#include <proto/device_comm.pb.h>
#include "cdc_proto_tx_event.h"
#include "function_bitmap_update_event.h"
#include "key_function_event.h"
#include "protocol_module.h"
#include "usb_device_state_event.h"
LOG_MODULE_REGISTER(protocol_module, LOG_LEVEL_INF);
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
#define PROTOCOL_VERSION 1U
#define PROTOCOL_VENDOR_ID 0x1915U
#define PROTOCOL_PRODUCT_ID 0x52F0U
#define PROTOCOL_FIRMWARE_MAJOR 0U
#define PROTOCOL_FIRMWARE_MINOR 0U
#define PROTOCOL_CAPABILITY_FLAGS 0U
#define PROTOCOL_CAPABILITY_FLAGS BIT(0)
static bool initialized;
static bool running;
static bool keyboard_core_ready;
static bool usb_active;
static bool hello_done;
static bool type_matches_body(uint8_t type, const CdcPacketBody *body)
{
@@ -28,6 +46,14 @@ static bool type_matches_body(uint8_t type, const CdcPacketBody *body)
return body->which_body == CdcPacketBody_hello_req_tag;
case CDC_PROTO_TYPE_HELLO_RSP:
return body->which_body == CdcPacketBody_hello_rsp_tag;
case CDC_PROTO_TYPE_BITMAP:
return body->which_body == CdcPacketBody_bitmap_tag;
case CDC_PROTO_TYPE_FUNCTION_KEY_EVENT:
return body->which_body == CdcPacketBody_function_key_event_tag;
case CDC_PROTO_TYPE_ACK:
return body->which_body == CdcPacketBody_ack_tag;
case CDC_PROTO_TYPE_ERROR:
return body->which_body == CdcPacketBody_error_tag;
default:
return false;
}
@@ -53,15 +79,29 @@ static int decode_body(const uint8_t *payload, size_t payload_len,
return 0;
}
static int encode_body(const CdcPacketBody *body, uint8_t *payload,
size_t payload_buf_size, size_t *payload_len)
{
pb_ostream_t stream;
if ((body == NULL) || (payload == NULL) || (payload_len == NULL)) {
return -EINVAL;
}
stream = pb_ostream_from_buffer(payload, payload_buf_size);
if (!pb_encode(&stream, CdcPacketBody_fields, body)) {
LOG_WRN("pb_encode failed: %s", PB_GET_ERROR(&stream));
return -EIO;
}
*payload_len = stream.bytes_written;
return 0;
}
static int encode_hello_rsp(uint8_t *rsp_payload, size_t rsp_payload_buf_size,
size_t *rsp_payload_len)
{
CdcPacketBody body = CdcPacketBody_init_zero;
pb_ostream_t stream;
if ((rsp_payload == NULL) || (rsp_payload_len == NULL)) {
return -EINVAL;
}
body.which_body = CdcPacketBody_hello_rsp_tag;
body.body.hello_rsp.protocol_version = PROTOCOL_VERSION;
@@ -71,17 +111,145 @@ static int encode_hello_rsp(uint8_t *rsp_payload, size_t rsp_payload_buf_size,
body.body.hello_rsp.firmware_minor = PROTOCOL_FIRMWARE_MINOR;
body.body.hello_rsp.capability_flags = PROTOCOL_CAPABILITY_FLAGS;
stream = pb_ostream_from_buffer(rsp_payload, rsp_payload_buf_size);
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
}
if (!pb_encode(&stream, CdcPacketBody_fields, &body)) {
LOG_WRN("pb_encode failed: %s", PB_GET_ERROR(&stream));
return -EIO;
static int encode_ack(uint8_t acked_type, uint8_t *rsp_payload,
size_t rsp_payload_buf_size, size_t *rsp_payload_len)
{
CdcPacketBody body = CdcPacketBody_init_zero;
body.which_body = CdcPacketBody_ack_tag;
body.body.ack.acked_type = acked_type;
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
}
static int encode_error(uint8_t error_type, ErrorCode error_code,
uint8_t *rsp_payload, size_t rsp_payload_buf_size,
size_t *rsp_payload_len)
{
CdcPacketBody body = CdcPacketBody_init_zero;
body.which_body = CdcPacketBody_error_tag;
body.body.error.error_type = error_type;
body.body.error.error_code = error_code;
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
}
static int encode_function_key_event(uint16_t usage, uint8_t action,
uint8_t *payload,
size_t payload_buf_size,
size_t *payload_len)
{
CdcPacketBody body = CdcPacketBody_init_zero;
body.which_body = CdcPacketBody_function_key_event_tag;
body.body.function_key_event.usage = usage;
body.body.function_key_event.action = (KeyAction)action;
return encode_body(&body, payload, payload_buf_size, payload_len);
}
static int submit_cdc_proto_tx_event(uint8_t type, const uint8_t *payload,
size_t payload_len)
{
struct cdc_proto_tx_event *event;
if ((payload == NULL) && (payload_len > 0U)) {
return -EINVAL;
}
*rsp_payload_len = stream.bytes_written;
event = new_cdc_proto_tx_event(payload_len);
event->type = type;
if (payload_len > 0U) {
memcpy(event->dyndata.data, payload, payload_len);
}
APP_EVENT_SUBMIT(event);
return 0;
}
static int submit_function_bitmap_update_event(const Bitmap *bitmap)
{
struct function_bitmap_update_event *event;
if ((bitmap == NULL) ||
(bitmap->usage_bitmap.size != KEYBOARD_PROTOCOL_BITMAP_BYTES)) {
return -EINVAL;
}
event = new_function_bitmap_update_event();
memcpy(event->bitmap, bitmap->usage_bitmap.bytes,
KEYBOARD_PROTOCOL_BITMAP_BYTES);
APP_EVENT_SUBMIT(event);
return 0;
}
static int encode_error_response(uint8_t req_type, ErrorCode error_code,
uint8_t *rsp_type, uint8_t *rsp_payload,
size_t rsp_payload_buf_size,
size_t *rsp_payload_len)
{
int err;
err = encode_error(req_type, error_code, rsp_payload,
rsp_payload_buf_size, rsp_payload_len);
if (err) {
return err;
}
*rsp_type = CDC_PROTO_TYPE_ERROR;
return 0;
}
static int encode_ack_response(uint8_t acked_type, uint8_t *rsp_type,
uint8_t *rsp_payload,
size_t rsp_payload_buf_size,
size_t *rsp_payload_len)
{
int err;
err = encode_ack(acked_type, rsp_payload, rsp_payload_buf_size,
rsp_payload_len);
if (err) {
return err;
}
*rsp_type = CDC_PROTO_TYPE_ACK;
return 0;
}
static int module_init(void)
{
keyboard_core_ready = false;
usb_active = false;
hello_done = false;
return 0;
}
static int module_start(void)
{
if (running) {
return 0;
}
running = true;
return 0;
}
static void module_pause(void)
{
if (!running) {
return;
}
hello_done = false;
running = false;
}
int protocol_module_process_cdc_packet(uint8_t req_type,
const uint8_t *req_payload,
size_t req_payload_len,
@@ -97,15 +265,23 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
return -EINVAL;
}
if (!running) {
return -EAGAIN;
}
err = decode_body(req_payload, req_payload_len, &body);
if (err) {
return err;
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_LENGTH,
rsp_type, rsp_payload,
rsp_payload_buf_size, rsp_payload_len);
}
if (!type_matches_body(req_type, &body)) {
LOG_WRN("CDC type/body mismatch type:0x%02x body_case:%d",
req_type, body.which_body);
return -EBADMSG;
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
rsp_type, rsp_payload,
rsp_payload_buf_size, rsp_payload_len);
}
switch (req_type) {
@@ -118,6 +294,7 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
body.body.hello_req.protocol_version);
}
hello_done = true;
err = encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
if (err) {
return err;
@@ -126,8 +303,158 @@ int protocol_module_process_cdc_packet(uint8_t req_type,
*rsp_type = CDC_PROTO_TYPE_HELLO_RSP;
return 0;
case CDC_PROTO_TYPE_BITMAP:
if (!hello_done) {
return encode_error_response(req_type, ErrorCode_ERROR_CODE_NOT_READY,
rsp_type, rsp_payload,
rsp_payload_buf_size,
rsp_payload_len);
}
if (!keyboard_core_ready) {
return encode_error_response(req_type, ErrorCode_ERROR_CODE_NOT_READY,
rsp_type, rsp_payload,
rsp_payload_buf_size,
rsp_payload_len);
}
if (body.body.bitmap.usage_bitmap.size != KEYBOARD_PROTOCOL_BITMAP_BYTES) {
LOG_WRN("Bitmap len:%u expected:%u",
(unsigned int)body.body.bitmap.usage_bitmap.size,
KEYBOARD_PROTOCOL_BITMAP_BYTES);
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_LENGTH,
rsp_type, rsp_payload,
rsp_payload_buf_size,
rsp_payload_len);
}
err = submit_function_bitmap_update_event(&body.body.bitmap);
if (err) {
return encode_error_response(req_type, ErrorCode_ERROR_CODE_INVALID_PARAM,
rsp_type, rsp_payload,
rsp_payload_buf_size,
rsp_payload_len);
}
return encode_ack_response(req_type, rsp_type, rsp_payload,
rsp_payload_buf_size, rsp_payload_len);
default:
LOG_WRN("Unsupported CDC protocol type:0x%02x", req_type);
return -ENOTSUP;
return encode_error_response(req_type, ErrorCode_ERROR_CODE_UNKNOWN_TYPE,
rsp_type, rsp_payload,
rsp_payload_buf_size, rsp_payload_len);
}
}
static bool handle_key_function_event(const struct key_function_event *event)
{
uint8_t payload[64];
size_t payload_len;
int err;
if (!running || !usb_active || !hello_done) {
return false;
}
err = encode_function_key_event(event->usage, event->action, payload,
sizeof(payload), &payload_len);
if (err) {
LOG_WRN("FunctionKeyEvent encode failed (%d)", err);
return false;
}
err = submit_cdc_proto_tx_event(CDC_PROTO_TYPE_FUNCTION_KEY_EVENT,
payload, payload_len);
if (err) {
LOG_WRN("FunctionKeyEvent submit failed (%d)", err);
}
return false;
}
static bool handle_usb_device_state_event(const struct usb_device_state_event *event)
{
usb_active = (event->state == USB_DEVICE_STATE_ACTIVE);
if (!usb_active) {
hello_done = false;
}
return false;
}
static bool app_event_handler(const struct app_event_header *aeh)
{
if (is_key_function_event(aeh)) {
return handle_key_function_event(cast_key_function_event(aeh));
}
if (is_usb_device_state_event(aeh)) {
return handle_usb_device_state_event(cast_usb_device_state_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);
} else {
module_set_state(MODULE_STATE_READY);
}
return false;
}
if (check_state(event, MODULE_ID(keyboard_core_module), MODULE_STATE_READY)) {
keyboard_core_ready = true;
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 {
module_set_state(MODULE_STATE_READY);
}
}
return false;
}
return false;
}
APP_EVENT_LISTENER(MODULE, app_event_handler);
APP_EVENT_SUBSCRIBE(MODULE, key_function_event);
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
APP_EVENT_SUBSCRIBE(MODULE, usb_device_state_event);
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);