添加 MODE_SWITCH_INVALID 枚举值到 mode_switch_mode 中, 用于表示无效的模式状态。同时更新相关的事件处理逻辑, 确保在模式切换时能够正确处理无效模式的情况。 refactor(mode_policy): 简化模块上下文结构并优化模式策略 将模块上下文中的 ble_adv_suspended 和 usb_enabled 标志位 替换为 active_mode 枚举值,简化了数据结构。重新设计了 模式策略应用逻辑,使其更清晰易懂,并改进了BLE和USB设备 的启用/暂停控制流程。 refactor(usb_device): 重构USB设备生命周期管理 移除了 USB_STACK_DISABLED 状态,简化了USB栈的状态管理。 改进了USB设备的启动和停止逻辑,更好地与模块生命周期 集成。现在USB状态事件仅在模块初始化后提交,避免了 不必要的事件发布。 feat(logging): 增加详细的状态转换日志记录 为BLE NUS模块添加了业务状态转换的日志记录功能, 增加了详细的错误和警告日志,包括生命周期状态、 业务状态和连接信息,便于调试和问题排查。 refactor(mode_switch): 优化模式检测和报告机制 修改了模式切换采样的判断逻辑,移除了force_report和 mode_valid标志位,改用last_mode状态来决定是否提交 模式切换事件,使代码更简洁且易于理解。
126 lines
3.1 KiB
C
126 lines
3.1 KiB
C
#include <stdio.h>
|
|
|
|
#include "keyboard_hid_report_event.h"
|
|
|
|
#define KEYBOARD_HID_REPORT_EVENT_LOG_BUF_LEN 192
|
|
|
|
static const char *mode_name(enum mode_switch_mode mode)
|
|
{
|
|
switch (mode) {
|
|
case MODE_SWITCH_INVALID:
|
|
return "INVALID";
|
|
case MODE_SWITCH_USB:
|
|
return "USB";
|
|
case MODE_SWITCH_24G:
|
|
return "2.4G";
|
|
case MODE_SWITCH_BLE:
|
|
return "BLE";
|
|
default:
|
|
return "?";
|
|
}
|
|
}
|
|
|
|
static const char *report_type_name(enum keyboard_report_type report_type)
|
|
{
|
|
switch (report_type) {
|
|
case KEYBOARD_REPORT_TYPE_KEYS:
|
|
return "keys";
|
|
case KEYBOARD_REPORT_TYPE_CONSUMER:
|
|
return "consumer";
|
|
default:
|
|
return "?";
|
|
}
|
|
}
|
|
|
|
static const char *protocol_mode_name(enum keyboard_protocol_mode protocol_mode)
|
|
{
|
|
switch (protocol_mode) {
|
|
case KEYBOARD_PROTOCOL_MODE_BOOT:
|
|
return "boot";
|
|
case KEYBOARD_PROTOCOL_MODE_REPORT:
|
|
return "report";
|
|
default:
|
|
return "?";
|
|
}
|
|
}
|
|
|
|
static const char *queue_policy_name(enum hid_queue_policy queue_policy)
|
|
{
|
|
switch (queue_policy) {
|
|
case HID_QUEUE_POLICY_LATEST:
|
|
return "latest";
|
|
case HID_QUEUE_POLICY_FIFO:
|
|
return "fifo";
|
|
default:
|
|
return "?";
|
|
}
|
|
}
|
|
|
|
static void log_keyboard_hid_report_event(const struct app_event_header *aeh)
|
|
{
|
|
const struct keyboard_hid_report_event *event =
|
|
cast_keyboard_hid_report_event(aeh);
|
|
char log_buf[KEYBOARD_HID_REPORT_EVENT_LOG_BUF_LEN];
|
|
int pos;
|
|
|
|
pos = snprintf(log_buf, sizeof(log_buf),
|
|
"mode:%s type:%s protocol:%s queue:%s len:%zu",
|
|
mode_name(event->mode),
|
|
report_type_name(event->report_type),
|
|
protocol_mode_name(event->protocol_mode),
|
|
queue_policy_name(event->queue_policy),
|
|
event->dyndata.size);
|
|
if ((pos > 0) && (pos < sizeof(log_buf))) {
|
|
for (size_t i = 0; i < event->dyndata.size; i++) {
|
|
int tmp = snprintf(&log_buf[pos], sizeof(log_buf) - pos,
|
|
" %02x", event->dyndata.data[i]);
|
|
|
|
if (tmp < 0) {
|
|
log_buf[sizeof(log_buf) - 2] = '~';
|
|
pos = tmp;
|
|
break;
|
|
}
|
|
|
|
pos += tmp;
|
|
if (pos >= sizeof(log_buf)) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (pos < 0) {
|
|
APP_EVENT_MANAGER_LOG(aeh, "log message preparation failure");
|
|
return;
|
|
}
|
|
|
|
APP_EVENT_MANAGER_LOG(aeh, "%s", log_buf);
|
|
}
|
|
|
|
static void profile_keyboard_hid_report_event(struct log_event_buf *buf,
|
|
const struct app_event_header *aeh)
|
|
{
|
|
const struct keyboard_hid_report_event *event =
|
|
cast_keyboard_hid_report_event(aeh);
|
|
|
|
nrf_profiler_log_encode_uint8(buf, event->mode);
|
|
nrf_profiler_log_encode_uint8(buf, event->report_type);
|
|
nrf_profiler_log_encode_uint8(buf, event->protocol_mode);
|
|
nrf_profiler_log_encode_uint8(buf, event->queue_policy);
|
|
nrf_profiler_log_encode_uint8(buf, (uint8_t)event->dyndata.size);
|
|
}
|
|
|
|
APP_EVENT_INFO_DEFINE(keyboard_hid_report_event,
|
|
ENCODE(NRF_PROFILER_ARG_U8,
|
|
NRF_PROFILER_ARG_U8,
|
|
NRF_PROFILER_ARG_U8,
|
|
NRF_PROFILER_ARG_U8,
|
|
NRF_PROFILER_ARG_U8),
|
|
ENCODE("mode", "report_type", "protocol_mode", "queue_policy", "len"),
|
|
profile_keyboard_hid_report_event);
|
|
|
|
APP_EVENT_TYPE_DEFINE(keyboard_hid_report_event,
|
|
log_keyboard_hid_report_event,
|
|
&keyboard_hid_report_event_info,
|
|
APP_EVENT_FLAGS_CREATE(
|
|
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));
|