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:
@@ -13,10 +13,7 @@
|
||||
|
||||
#include <caf/events/power_manager_event.h>
|
||||
|
||||
#include "usb_device_module.h"
|
||||
#include "usb_device_state_event.h"
|
||||
#include "usb_function_ready_event.h"
|
||||
#include "usb_prepare_event.h"
|
||||
#include "usb_state_event.h"
|
||||
|
||||
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
|
||||
@@ -24,7 +21,7 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
#define USB_DEVICE_PID 0x52F0
|
||||
#define USB_DEVICE_MANUFACTURER "Atguigu"
|
||||
#define USB_DEVICE_PRODUCT "WH Mini Keyboard"
|
||||
#define USB_REQUIRED_FUNCTION_MASK (USB_FUNCTION_HID | USB_FUNCTION_CDC_ACM)
|
||||
#define USB_REQUIRED_READY_MASK (USB_STATEF_HID_READY | USB_STATEF_CDC_READY)
|
||||
|
||||
USBD_DEVICE_DEFINE(blinky_usbd, DEVICE_DT_GET(DT_NODELABEL(usbd)),
|
||||
USB_DEVICE_VID, USB_DEVICE_PID);
|
||||
@@ -42,20 +39,69 @@ static const char *const class_blocklist[] = {
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool prepare_broadcasted;
|
||||
static bool usbd_initialized;
|
||||
static bool usb_enabled;
|
||||
static uint8_t ready_function_mask;
|
||||
static enum usb_device_state device_state = USB_DEVICE_STATE_DISCONNECTED;
|
||||
static uint32_t usb_state_flags;
|
||||
|
||||
static void update_usb_device_state(enum usb_device_state state)
|
||||
enum usb_runtime_state {
|
||||
USB_RUNTIME_STATE_DISCONNECTED,
|
||||
USB_RUNTIME_STATE_POWERED,
|
||||
USB_RUNTIME_STATE_ACTIVE,
|
||||
USB_RUNTIME_STATE_SUSPENDED,
|
||||
};
|
||||
|
||||
static enum usb_runtime_state device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
||||
|
||||
static void broadcast_usb_state(void)
|
||||
{
|
||||
submit_usb_state_snapshot(MODULE_ID(MODULE), usb_state_flags);
|
||||
}
|
||||
|
||||
static void set_usb_state_flags(uint32_t mask)
|
||||
{
|
||||
usb_state_flags |= mask;
|
||||
}
|
||||
|
||||
static void clear_usb_state_flags(uint32_t mask)
|
||||
{
|
||||
usb_state_flags &= ~mask;
|
||||
}
|
||||
|
||||
static void update_usb_runtime_state(enum usb_runtime_state state)
|
||||
{
|
||||
if (device_state == state) {
|
||||
return;
|
||||
}
|
||||
|
||||
device_state = state;
|
||||
submit_usb_device_state_event(state);
|
||||
|
||||
switch (state) {
|
||||
case USB_RUNTIME_STATE_DISCONNECTED:
|
||||
clear_usb_state_flags(USB_STATEF_POWERED |
|
||||
USB_STATEF_ACTIVE |
|
||||
USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_POWERED:
|
||||
set_usb_state_flags(USB_STATEF_POWERED);
|
||||
clear_usb_state_flags(USB_STATEF_ACTIVE | USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_ACTIVE:
|
||||
set_usb_state_flags(USB_STATEF_POWERED | USB_STATEF_ACTIVE);
|
||||
clear_usb_state_flags(USB_STATEF_SUSPENDED);
|
||||
break;
|
||||
|
||||
case USB_RUNTIME_STATE_SUSPENDED:
|
||||
set_usb_state_flags(USB_STATEF_POWERED | USB_STATEF_SUSPENDED);
|
||||
clear_usb_state_flags(USB_STATEF_ACTIVE);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
broadcast_usb_state();
|
||||
}
|
||||
|
||||
static void update_power_manager_restriction(bool vbus_present)
|
||||
@@ -114,7 +160,7 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
} else {
|
||||
usb_enabled = true;
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -130,26 +176,26 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
}
|
||||
|
||||
usb_enabled = false;
|
||||
update_usb_device_state(USB_DEVICE_STATE_DISCONNECTED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_DISCONNECTED);
|
||||
break;
|
||||
|
||||
case USBD_MSG_CONFIGURATION:
|
||||
if (msg->status) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
||||
} else if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_SUSPEND:
|
||||
if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_SUSPENDED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_SUSPENDED);
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_RESUME:
|
||||
if (usb_enabled) {
|
||||
update_usb_device_state(USB_DEVICE_STATE_ACTIVE);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_ACTIVE);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -181,6 +227,7 @@ static int usb_stack_init(void)
|
||||
}
|
||||
|
||||
usbd_initialized = true;
|
||||
set_usb_state_flags(USB_STATEF_STACK_READY);
|
||||
|
||||
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
@@ -190,19 +237,33 @@ static int usb_stack_init(void)
|
||||
}
|
||||
|
||||
usb_enabled = true;
|
||||
update_usb_device_state(USB_DEVICE_STATE_POWERED);
|
||||
update_usb_runtime_state(USB_RUNTIME_STATE_POWERED);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int try_start_usb_stack(void)
|
||||
{
|
||||
if (usbd_initialized) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((usb_state_flags & USB_REQUIRED_READY_MASK) != USB_REQUIRED_READY_MASK) {
|
||||
set_usb_state_flags(USB_STATEF_PREPARE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
clear_usb_state_flags(USB_STATEF_PREPARE);
|
||||
return usb_stack_init();
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
device_state = USB_DEVICE_STATE_DISCONNECTED;
|
||||
device_state = USB_RUNTIME_STATE_DISCONNECTED;
|
||||
usb_enabled = false;
|
||||
ready_function_mask = 0U;
|
||||
prepare_broadcasted = false;
|
||||
usbd_initialized = false;
|
||||
usb_state_flags = 0U;
|
||||
update_power_manager_restriction(false);
|
||||
|
||||
return 0;
|
||||
@@ -215,16 +276,12 @@ static int module_start(void)
|
||||
}
|
||||
|
||||
running = true;
|
||||
set_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
||||
|
||||
if (usbd_initialized || prepare_broadcasted) {
|
||||
return 0;
|
||||
}
|
||||
int err = try_start_usb_stack();
|
||||
|
||||
ready_function_mask = 0U;
|
||||
prepare_broadcasted = true;
|
||||
submit_usb_prepare_event();
|
||||
|
||||
return 0;
|
||||
broadcast_usb_state();
|
||||
return err;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
@@ -234,6 +291,8 @@ static void module_pause(void)
|
||||
}
|
||||
|
||||
running = false;
|
||||
clear_usb_state_flags(USB_STATEF_STACK_RUNNING);
|
||||
broadcast_usb_state();
|
||||
}
|
||||
|
||||
static bool handle_module_state_event(const struct module_state_event *event)
|
||||
@@ -266,28 +325,32 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool handle_usb_function_ready_event(const struct usb_function_ready_event *event)
|
||||
static bool handle_usb_state_event(const struct usb_state_event *event)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!running || !prepare_broadcasted || usbd_initialized) {
|
||||
if ((event->op == USB_STATE_EVENT_OP_SNAPSHOT) ||
|
||||
(event->sink_module_id != MODULE_ID(MODULE))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ready_function_mask |= event->function_mask;
|
||||
|
||||
if ((ready_function_mask & USB_REQUIRED_FUNCTION_MASK) !=
|
||||
USB_REQUIRED_FUNCTION_MASK) {
|
||||
if (event->op == USB_STATE_EVENT_OP_SET_BITS) {
|
||||
usb_state_flags |= event->flags;
|
||||
} else if (event->op == USB_STATE_EVENT_OP_CLEAR_BITS) {
|
||||
usb_state_flags &= ~event->flags;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
err = usb_stack_init();
|
||||
int err = running ? try_start_usb_stack() : 0;
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
broadcast_usb_state();
|
||||
|
||||
if (usbd_initialized) {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -297,8 +360,8 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
return handle_module_state_event(cast_module_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_usb_function_ready_event(aeh)) {
|
||||
return handle_usb_function_ready_event(cast_usb_function_ready_event(aeh));
|
||||
if (is_usb_state_event(aeh)) {
|
||||
return handle_usb_state_event(cast_usb_state_event(aeh));
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
@@ -329,6 +392,6 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_function_ready_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, usb_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|
||||
|
||||
Reference in New Issue
Block a user