feat(usb): 引入统一的USB状态事件系统

重构USB事件管理,将原有的多个专用事件(usb_device_state_event、
usb_function_ready_event、usb_prepare_event)合并为统一的
usb_state_event。新的事件系统采用位标志方式管理USB状态,
提供更灵活的状态跟踪机制。

BREAKING CHANGE: 移除了旧的USB相关事件类型,需要更新依赖这些
事件的模块代码。
This commit is contained in:
2026-04-15 09:30:40 +08:00
parent 78a6dc212d
commit c4b205b8a1
13 changed files with 301 additions and 306 deletions

View File

@@ -1,46 +0,0 @@
#include "usb_device_state_event.h"
static const char *usb_device_state_name(enum usb_device_state state)
{
switch (state) {
case USB_DEVICE_STATE_DISCONNECTED:
return "disconnected";
case USB_DEVICE_STATE_POWERED:
return "powered";
case USB_DEVICE_STATE_ACTIVE:
return "active";
case USB_DEVICE_STATE_SUSPENDED:
return "suspended";
default:
return "?";
}
}
static void log_usb_device_state_event(const struct app_event_header *aeh)
{
const struct usb_device_state_event *event =
cast_usb_device_state_event(aeh);
APP_EVENT_MANAGER_LOG(aeh, "state:%s",
usb_device_state_name(event->state));
}
static void profile_usb_device_state_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct usb_device_state_event *event =
cast_usb_device_state_event(aeh);
nrf_profiler_log_encode_uint8(buf, event->state);
}
APP_EVENT_INFO_DEFINE(usb_device_state_event,
ENCODE(NRF_PROFILER_ARG_U8),
ENCODE("state"),
profile_usb_device_state_event);
APP_EVENT_TYPE_DEFINE(usb_device_state_event,
log_usb_device_state_event,
&usb_device_state_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));

View File

@@ -1,42 +0,0 @@
#include "usb_function_ready_event.h"
static const char *usb_function_name(uint8_t function_mask)
{
switch (function_mask) {
case USB_FUNCTION_HID:
return "hid";
case USB_FUNCTION_CDC_ACM:
return "cdc_acm";
default:
return "?";
}
}
static void log_usb_function_ready_event(const struct app_event_header *aeh)
{
const struct usb_function_ready_event *event =
cast_usb_function_ready_event(aeh);
APP_EVENT_MANAGER_LOG(aeh, "function:%s",
usb_function_name(event->function_mask));
}
static void profile_usb_function_ready_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct usb_function_ready_event *event =
cast_usb_function_ready_event(aeh);
nrf_profiler_log_encode_uint8(buf, event->function_mask);
}
APP_EVENT_INFO_DEFINE(usb_function_ready_event,
ENCODE(NRF_PROFILER_ARG_U8),
ENCODE("function_mask"),
profile_usb_function_ready_event);
APP_EVENT_TYPE_DEFINE(usb_function_ready_event,
log_usb_function_ready_event,
&usb_function_ready_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));

View File

@@ -1,24 +0,0 @@
#include "usb_prepare_event.h"
static void log_usb_prepare_event(const struct app_event_header *aeh)
{
APP_EVENT_MANAGER_LOG(aeh, "prepare");
}
static void profile_usb_prepare_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
ARG_UNUSED(buf);
ARG_UNUSED(aeh);
}
APP_EVENT_INFO_DEFINE(usb_prepare_event,
ENCODE(),
ENCODE(),
profile_usb_prepare_event);
APP_EVENT_TYPE_DEFINE(usb_prepare_event,
log_usb_prepare_event,
&usb_prepare_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));

View File

@@ -0,0 +1,54 @@
#include <caf/events/module_state_event.h>
#include "usb_state_event.h"
static const char *usb_state_event_op_name(uint8_t op)
{
switch ((enum usb_state_event_op)op) {
case USB_STATE_EVENT_OP_SET_BITS:
return "set_bits";
case USB_STATE_EVENT_OP_CLEAR_BITS:
return "clear_bits";
case USB_STATE_EVENT_OP_SNAPSHOT:
return "snapshot";
default:
return "?";
}
}
static const char *module_name_or_any(const void *module_id)
{
return (module_id != NULL) ? module_name_get(module_id) : "*";
}
static void log_usb_state_event(const struct app_event_header *aeh)
{
const struct usb_state_event *event = cast_usb_state_event(aeh);
APP_EVENT_MANAGER_LOG(aeh,
"src:%s sink:%s op:%s flags:0x%08x",
module_name_or_any(event->src_module_id),
module_name_or_any(event->sink_module_id),
usb_state_event_op_name(event->op),
event->flags);
}
static void profile_usb_state_event(struct log_event_buf *buf,
const struct app_event_header *aeh)
{
const struct usb_state_event *event = cast_usb_state_event(aeh);
nrf_profiler_log_encode_uint8(buf, event->op);
nrf_profiler_log_encode_uint32(buf, event->flags);
}
APP_EVENT_INFO_DEFINE(usb_state_event,
ENCODE(NRF_PROFILER_ARG_U8, NRF_PROFILER_ARG_U32),
ENCODE("op", "flags"),
profile_usb_state_event);
APP_EVENT_TYPE_DEFINE(usb_state_event,
log_usb_state_event,
&usb_state_event_info,
APP_EVENT_FLAGS_CREATE(
APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE));