feat(events): 添加功能位图状态事件并移除旧的按键功能事件
- 添加新的 function_bitmap_state_event 事件类型用于跟踪功能键位图状态 - 移除已废弃的 key_function_event 事件及其相关文件 - 更新 CMakeLists.txt 中的源文件列表以包含新事件文件 - 修改协议定义文件 device_comm.options 和 device_comm.proto 以使用位图方式传输功能键状态而不是单独的按键事件 - 更新键盘核心模块中的位图处理逻辑,添加 usage_to_bitmap_pos 辅助函数来正确定位修饰键和普通按键的位置 - 修改报告构建逻辑以正确处理新的位图布局 - 更新协议模块以处理新的功能位图状态事件和 LED 状态事件 - 实现协议模块中的 ACK、错误响应和 LED 状态编码功能
This commit is contained in:
@@ -42,13 +42,13 @@ target_sources(app PRIVATE
|
|||||||
src/events/bat_state_event.c
|
src/events/bat_state_event.c
|
||||||
src/events/datetime_event.c
|
src/events/datetime_event.c
|
||||||
src/events/encoder_event.c
|
src/events/encoder_event.c
|
||||||
|
src/events/function_bitmap_state_event.c
|
||||||
src/events/function_bitmap_update_event.c
|
src/events/function_bitmap_update_event.c
|
||||||
src/events/hid_led_event.c
|
src/events/hid_led_event.c
|
||||||
src/events/hid_report_sent_event.c
|
src/events/hid_report_sent_event.c
|
||||||
src/events/hid_transport_state_event.c
|
src/events/hid_transport_state_event.c
|
||||||
src/events/hid_tx_report_event.c
|
src/events/hid_tx_report_event.c
|
||||||
src/events/led_strip_en_event.c
|
src/events/led_strip_en_event.c
|
||||||
src/events/key_function_event.c
|
|
||||||
src/mode_switch_module.c
|
src/mode_switch_module.c
|
||||||
src/events/keyboard_hid_report_event.c
|
src/events/keyboard_hid_report_event.c
|
||||||
src/events/mode_switch_event.c
|
src/events/mode_switch_event.c
|
||||||
|
|||||||
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,37 +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);
|
|
||||||
|
|
||||||
static inline void submit_key_function_event(uint16_t usage, uint8_t action)
|
|
||||||
{
|
|
||||||
struct key_function_event *event = new_key_function_event();
|
|
||||||
|
|
||||||
event->usage = usage;
|
|
||||||
event->action = action;
|
|
||||||
APP_EVENT_SUBMIT(event);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif /* BLINKY_KEY_FUNCTION_EVENT_H_ */
|
|
||||||
@@ -1 +1,2 @@
|
|||||||
Bitmap.usage_bitmap max_size:29
|
Bitmap.usage_bitmap max_size:29
|
||||||
|
FunctionKeyEvent.usage_bitmap max_size:29
|
||||||
|
|||||||
@@ -17,14 +17,8 @@ message Bitmap {
|
|||||||
bytes usage_bitmap = 1;
|
bytes usage_bitmap = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum KeyAction {
|
|
||||||
KEY_ACTION_RELEASE = 0;
|
|
||||||
KEY_ACTION_PRESS = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
message FunctionKeyEvent {
|
message FunctionKeyEvent {
|
||||||
uint32 usage = 1;
|
bytes usage_bitmap = 1;
|
||||||
KeyAction action = 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
message LedState {
|
message LedState {
|
||||||
|
|||||||
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));
|
||||||
@@ -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));
|
|
||||||
@@ -15,10 +15,10 @@
|
|||||||
#include <zephyr/sys/util.h>
|
#include <zephyr/sys/util.h>
|
||||||
|
|
||||||
#include "encoder_event.h"
|
#include "encoder_event.h"
|
||||||
|
#include "function_bitmap_state_event.h"
|
||||||
#include "function_bitmap_update_event.h"
|
#include "function_bitmap_update_event.h"
|
||||||
#include "keyboard_core.h"
|
#include "keyboard_core.h"
|
||||||
#include "keyboard_hid_report_event.h"
|
#include "keyboard_hid_report_event.h"
|
||||||
#include "key_function_event.h"
|
|
||||||
#include "mode_switch_event.h"
|
#include "mode_switch_event.h"
|
||||||
#include "set_protocol_event.h"
|
#include "set_protocol_event.h"
|
||||||
|
|
||||||
@@ -146,13 +146,40 @@ static const struct keymap_entry *keymap_get(uint16_t key_id)
|
|||||||
return NULL;
|
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 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)
|
static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
||||||
@@ -161,13 +188,12 @@ static bool usage_bitmap_write(uint8_t *bitmap, uint16_t usage_id, bool pressed)
|
|||||||
uint8_t bit_idx;
|
uint8_t bit_idx;
|
||||||
bool was_pressed;
|
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);
|
LOG_WRN("Unsupported keyboard usage 0x%04x", usage_id);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte_idx = usage_id / 8U;
|
|
||||||
bit_idx = usage_id % 8U;
|
|
||||||
was_pressed = (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
was_pressed = (bitmap[byte_idx] & BIT(bit_idx)) != 0U;
|
||||||
|
|
||||||
if (was_pressed == pressed) {
|
if (was_pressed == pressed) {
|
||||||
@@ -227,12 +253,14 @@ static void build_boot_report(uint8_t report[KEYBOARD_BOOT_REPORT_SIZE])
|
|||||||
|
|
||||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||||
memset(report, 0, KEYBOARD_BOOT_REPORT_SIZE);
|
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;
|
report[1] = KEYBOARD_BOOT_RESERVED_BYTE;
|
||||||
|
|
||||||
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
for (uint16_t usage_id = 0; usage_id <= KEYBOARD_NKRO_USAGE_MAX; usage_id++) {
|
||||||
uint8_t byte_idx = usage_id / 8U;
|
uint8_t byte_idx;
|
||||||
uint8_t bit_idx = usage_id % 8U;
|
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) {
|
if ((effective_hid_bitmap[byte_idx] & BIT(bit_idx)) == 0U) {
|
||||||
continue;
|
continue;
|
||||||
@@ -254,8 +282,8 @@ static void build_nkro_report(uint8_t report[KEYBOARD_NKRO_REPORT_SIZE])
|
|||||||
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
uint8_t effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES];
|
||||||
|
|
||||||
build_effective_hid_bitmap(effective_hid_bitmap);
|
build_effective_hid_bitmap(effective_hid_bitmap);
|
||||||
report[0] = effective_hid_bitmap[KEYBOARD_PROTOCOL_BITMAP_BYTES - 1U];
|
report[0] = effective_hid_bitmap[0];
|
||||||
memcpy(&report[1], effective_hid_bitmap, KEYBOARD_NKRO_BITMAP_BYTES);
|
memcpy(&report[1], &effective_hid_bitmap[1], KEYBOARD_NKRO_BITMAP_BYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint16_t active_consumer_usage_get(void)
|
static uint16_t active_consumer_usage_get(void)
|
||||||
@@ -386,15 +414,9 @@ 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++) {
|
(void)submit_function_bitmap_state_event(keyboard_state.pressed_usage_bitmap);
|
||||||
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)
|
static void emit_release_reports(enum mode_switch_mode mode)
|
||||||
@@ -451,7 +473,7 @@ static void module_pause(void)
|
|||||||
if (mode_valid) {
|
if (mode_valid) {
|
||||||
emit_release_reports(current_mode);
|
emit_release_reports(current_mode);
|
||||||
}
|
}
|
||||||
emit_function_release_events();
|
emit_function_state_event();
|
||||||
|
|
||||||
keyboard_state_clear();
|
keyboard_state_clear();
|
||||||
reports_cache_invalidate();
|
reports_cache_invalidate();
|
||||||
@@ -497,10 +519,7 @@ static bool handle_button_event(const struct button_event *event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (routed_to_function) {
|
if (routed_to_function) {
|
||||||
submit_key_function_event(entry->usage_id,
|
emit_function_state_event();
|
||||||
event->pressed ?
|
|
||||||
KEY_FUNCTION_ACTION_PRESS :
|
|
||||||
KEY_FUNCTION_ACTION_RELEASE);
|
|
||||||
} else {
|
} else {
|
||||||
emit_keys_report(false);
|
emit_keys_report(false);
|
||||||
}
|
}
|
||||||
@@ -526,7 +545,7 @@ static bool handle_mode_switch_event(const struct mode_switch_event *event)
|
|||||||
mode_changed = mode_valid && (current_mode != event->mode);
|
mode_changed = mode_valid && (current_mode != event->mode);
|
||||||
if (mode_changed) {
|
if (mode_changed) {
|
||||||
emit_release_reports(current_mode);
|
emit_release_reports(current_mode);
|
||||||
emit_function_release_events();
|
emit_function_state_event();
|
||||||
keyboard_state_clear();
|
keyboard_state_clear();
|
||||||
reports_cache_invalidate();
|
reports_cache_invalidate();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,9 +18,14 @@
|
|||||||
|
|
||||||
#include <proto/device_comm.pb.h>
|
#include <proto/device_comm.pb.h>
|
||||||
|
|
||||||
|
#include "function_bitmap_state_event.h"
|
||||||
|
#include "function_bitmap_update_event.h"
|
||||||
|
#include "hid_led_event.h"
|
||||||
#include "proto_rx_event.h"
|
#include "proto_rx_event.h"
|
||||||
#include "proto_tx_event.h"
|
#include "proto_tx_event.h"
|
||||||
#include "protocol_module.h"
|
#include "protocol_module.h"
|
||||||
|
#include "theme_rgb_update_event.h"
|
||||||
|
#include "time_sync_event.h"
|
||||||
|
|
||||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||||
|
|
||||||
@@ -29,7 +34,7 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
|||||||
#define PROTOCOL_PRODUCT_ID 0x52F0U
|
#define PROTOCOL_PRODUCT_ID 0x52F0U
|
||||||
#define PROTOCOL_FIRMWARE_MAJOR 0U
|
#define PROTOCOL_FIRMWARE_MAJOR 0U
|
||||||
#define PROTOCOL_FIRMWARE_MINOR 0U
|
#define PROTOCOL_FIRMWARE_MINOR 0U
|
||||||
#define PROTOCOL_CAPABILITY_FLAGS 0U
|
#define PROTOCOL_CAPABILITY_FLAGS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4))
|
||||||
#define PROTOCOL_MAX_MSG_LEN 128U
|
#define PROTOCOL_MAX_MSG_LEN 128U
|
||||||
|
|
||||||
static bool initialized;
|
static bool initialized;
|
||||||
@@ -91,6 +96,59 @@ static int encode_hello_rsp(uint8_t *rsp_payload, size_t rsp_payload_buf_size,
|
|||||||
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
return encode_body(&body, rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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_led_state(uint32_t led_mask, uint8_t *payload,
|
||||||
|
size_t payload_buf_size, size_t *payload_len)
|
||||||
|
{
|
||||||
|
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||||
|
|
||||||
|
body.which_body = CdcPacketBody_led_state_tag;
|
||||||
|
body.body.led_state.led_mask = led_mask;
|
||||||
|
|
||||||
|
return encode_body(&body, payload, payload_buf_size, payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int encode_function_bitmap_state(const uint8_t *bitmap, uint8_t *payload,
|
||||||
|
size_t payload_buf_size,
|
||||||
|
size_t *payload_len)
|
||||||
|
{
|
||||||
|
CdcPacketBody body = CdcPacketBody_init_zero;
|
||||||
|
|
||||||
|
if (bitmap == NULL) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.which_body = CdcPacketBody_function_key_event_tag;
|
||||||
|
body.body.function_key_event.usage_bitmap.size = KEYBOARD_PROTOCOL_BITMAP_BYTES;
|
||||||
|
memcpy(body.body.function_key_event.usage_bitmap.bytes, bitmap,
|
||||||
|
KEYBOARD_PROTOCOL_BITMAP_BYTES);
|
||||||
|
|
||||||
|
return encode_body(&body, payload, payload_buf_size, payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
static int module_init(void)
|
static int module_init(void)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
|
for (size_t i = 0; i < ARRAY_SIZE(hello_done); i++) {
|
||||||
@@ -156,11 +214,8 @@ int protocol_module_process_message(enum proto_transport transport,
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (body.which_body != CdcPacketBody_hello_req_tag) {
|
switch (body.which_body) {
|
||||||
LOG_WRN("Unsupported protobuf body case %d", body.which_body);
|
case CdcPacketBody_hello_req_tag:
|
||||||
return -ENOTSUP;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INF("HelloReq transport:%u protocol_version:%u",
|
LOG_INF("HelloReq transport:%u protocol_version:%u",
|
||||||
transport, body.body.hello_req.protocol_version);
|
transport, body.body.hello_req.protocol_version);
|
||||||
|
|
||||||
@@ -171,6 +226,86 @@ int protocol_module_process_message(enum proto_transport transport,
|
|||||||
|
|
||||||
hello_done[transport] = true;
|
hello_done[transport] = true;
|
||||||
return encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
return encode_hello_rsp(rsp_payload, rsp_payload_buf_size, rsp_payload_len);
|
||||||
|
|
||||||
|
case CdcPacketBody_bitmap_tag:
|
||||||
|
if (!hello_done[transport]) {
|
||||||
|
return encode_error(CdcPacketBody_bitmap_tag,
|
||||||
|
ErrorCode_ERROR_CODE_NOT_READY,
|
||||||
|
rsp_payload, rsp_payload_buf_size,
|
||||||
|
rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body.body.bitmap.usage_bitmap.size != KEYBOARD_PROTOCOL_BITMAP_BYTES) {
|
||||||
|
return encode_error(CdcPacketBody_bitmap_tag,
|
||||||
|
ErrorCode_ERROR_CODE_INVALID_LENGTH,
|
||||||
|
rsp_payload, rsp_payload_buf_size,
|
||||||
|
rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
err = submit_function_bitmap_update_event(
|
||||||
|
body.body.bitmap.usage_bitmap.bytes);
|
||||||
|
if (err) {
|
||||||
|
return encode_error(CdcPacketBody_bitmap_tag,
|
||||||
|
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||||
|
rsp_payload, rsp_payload_buf_size,
|
||||||
|
rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
return encode_ack(CdcPacketBody_bitmap_tag, rsp_payload,
|
||||||
|
rsp_payload_buf_size, rsp_payload_len);
|
||||||
|
|
||||||
|
case CdcPacketBody_time_sync_tag:
|
||||||
|
if (!hello_done[transport]) {
|
||||||
|
return encode_error(CdcPacketBody_time_sync_tag,
|
||||||
|
ErrorCode_ERROR_CODE_NOT_READY,
|
||||||
|
rsp_payload, rsp_payload_buf_size,
|
||||||
|
rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (body.body.time_sync.version != 1U) {
|
||||||
|
return encode_error(CdcPacketBody_time_sync_tag,
|
||||||
|
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||||
|
rsp_payload, rsp_payload_buf_size,
|
||||||
|
rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_time_sync_event(body.body.time_sync.version,
|
||||||
|
body.body.time_sync.flags,
|
||||||
|
body.body.time_sync.timezone_min,
|
||||||
|
body.body.time_sync.utc_ms,
|
||||||
|
body.body.time_sync.accuracy_ms);
|
||||||
|
return encode_ack(CdcPacketBody_time_sync_tag, rsp_payload,
|
||||||
|
rsp_payload_buf_size, rsp_payload_len);
|
||||||
|
|
||||||
|
case CdcPacketBody_theme_rgb_tag:
|
||||||
|
if (!hello_done[transport]) {
|
||||||
|
return encode_error(CdcPacketBody_theme_rgb_tag,
|
||||||
|
ErrorCode_ERROR_CODE_NOT_READY,
|
||||||
|
rsp_payload, rsp_payload_buf_size,
|
||||||
|
rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((body.body.theme_rgb.red > 255U) ||
|
||||||
|
(body.body.theme_rgb.green > 255U) ||
|
||||||
|
(body.body.theme_rgb.blue > 255U)) {
|
||||||
|
return encode_error(CdcPacketBody_theme_rgb_tag,
|
||||||
|
ErrorCode_ERROR_CODE_INVALID_PARAM,
|
||||||
|
rsp_payload, rsp_payload_buf_size,
|
||||||
|
rsp_payload_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
submit_theme_rgb_update_event((struct theme_rgb) {
|
||||||
|
.r = (uint8_t)body.body.theme_rgb.red,
|
||||||
|
.g = (uint8_t)body.body.theme_rgb.green,
|
||||||
|
.b = (uint8_t)body.body.theme_rgb.blue,
|
||||||
|
});
|
||||||
|
return encode_ack(CdcPacketBody_theme_rgb_tag, rsp_payload,
|
||||||
|
rsp_payload_buf_size, rsp_payload_len);
|
||||||
|
|
||||||
|
default:
|
||||||
|
LOG_WRN("Unsupported protobuf body case %d", body.which_body);
|
||||||
|
return -ENOTSUP;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool handle_proto_rx_event(const struct proto_rx_event *event)
|
static bool handle_proto_rx_event(const struct proto_rx_event *event)
|
||||||
@@ -205,12 +340,87 @@ static bool handle_proto_rx_event(const struct proto_rx_event *event)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool handle_function_bitmap_state_event(
|
||||||
|
const struct function_bitmap_state_event *event)
|
||||||
|
{
|
||||||
|
uint8_t payload[PROTOCOL_MAX_MSG_LEN];
|
||||||
|
size_t payload_len;
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (enum proto_transport transport = 0; transport < PROTO_TRANSPORT_COUNT;
|
||||||
|
transport++) {
|
||||||
|
if (!hello_done[transport]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = encode_function_bitmap_state(event->bitmap, payload,
|
||||||
|
sizeof(payload), &payload_len);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("FunctionKeyEvent encode failed (%d)", err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = submit_proto_tx_event(transport, payload, payload_len);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("FunctionKeyEvent submit failed (%d)", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool handle_hid_led_event(const struct hid_led_event *event)
|
||||||
|
{
|
||||||
|
uint8_t payload[PROTOCOL_MAX_MSG_LEN];
|
||||||
|
size_t payload_len;
|
||||||
|
int err;
|
||||||
|
enum proto_transport transport;
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
transport = (event->transport == HID_TRANSPORT_USB) ?
|
||||||
|
PROTO_TRANSPORT_USB_CDC :
|
||||||
|
PROTO_TRANSPORT_BLE_NUS;
|
||||||
|
|
||||||
|
if (!hello_done[transport]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = encode_led_state(event->led_bm, payload, sizeof(payload), &payload_len);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("LedState encode failed (%d)", err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
err = submit_proto_tx_event(transport, payload, payload_len);
|
||||||
|
if (err) {
|
||||||
|
LOG_WRN("LedState submit failed (%d)", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static bool app_event_handler(const struct app_event_header *aeh)
|
static bool app_event_handler(const struct app_event_header *aeh)
|
||||||
{
|
{
|
||||||
if (is_proto_rx_event(aeh)) {
|
if (is_proto_rx_event(aeh)) {
|
||||||
return handle_proto_rx_event(cast_proto_rx_event(aeh));
|
return handle_proto_rx_event(cast_proto_rx_event(aeh));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_function_bitmap_state_event(aeh)) {
|
||||||
|
return handle_function_bitmap_state_event(
|
||||||
|
cast_function_bitmap_state_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_hid_led_event(aeh)) {
|
||||||
|
return handle_hid_led_event(cast_hid_led_event(aeh));
|
||||||
|
}
|
||||||
|
|
||||||
if (is_module_state_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);
|
||||||
int err;
|
int err;
|
||||||
@@ -266,6 +476,8 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
|||||||
}
|
}
|
||||||
|
|
||||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, function_bitmap_state_event);
|
||||||
|
APP_EVENT_SUBSCRIBE(MODULE, hid_led_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||||
APP_EVENT_SUBSCRIBE(MODULE, proto_rx_event);
|
APP_EVENT_SUBSCRIBE(MODULE, proto_rx_event);
|
||||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||||
|
|||||||
Reference in New Issue
Block a user