feat(mode_switch): 添加无效模式枚举值并更新事件处理
添加 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状态来决定是否提交 模式切换事件,使代码更简洁且易于理解。
This commit is contained in:
@@ -43,7 +43,6 @@ static const char *const class_blocklist[] = {
|
||||
|
||||
enum usb_stack_state {
|
||||
USB_STACK_UNINITIALIZED = 0,
|
||||
USB_STACK_DISABLED,
|
||||
USB_STACK_READY,
|
||||
USB_STACK_ENABLED,
|
||||
};
|
||||
@@ -91,8 +90,8 @@ static struct usb_owner_ctx usb_ctx = {
|
||||
|
||||
static inline enum usb_state usb_public_state_get(void)
|
||||
{
|
||||
if ((usb_ctx.stack == USB_STACK_UNINITIALIZED) ||
|
||||
(usb_ctx.stack == USB_STACK_DISABLED)) {
|
||||
if (!module_lifecycle_is_running(&usb_ctx.lc) ||
|
||||
(usb_ctx.stack == USB_STACK_UNINITIALIZED)) {
|
||||
return USB_STATE_DISABLED;
|
||||
}
|
||||
|
||||
@@ -110,11 +109,6 @@ static inline enum usb_state usb_public_state_get(void)
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool usb_stack_was_initialized(void)
|
||||
{
|
||||
return (usb_ctx.stack != USB_STACK_UNINITIALIZED);
|
||||
}
|
||||
|
||||
static inline void usb_bus_set(enum usb_bus_state state)
|
||||
{
|
||||
if (usb_ctx.bus == state) {
|
||||
@@ -122,7 +116,10 @@ static inline void usb_bus_set(enum usb_bus_state state)
|
||||
}
|
||||
|
||||
usb_ctx.bus = state;
|
||||
submit_usb_state(usb_public_state_get());
|
||||
|
||||
if (module_lifecycle_is_initialized(&usb_ctx.lc)) {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
}
|
||||
|
||||
static void update_power_manager_restriction(bool vbus_present)
|
||||
@@ -222,31 +219,30 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
switch (msg->type) {
|
||||
case USBD_MSG_VBUS_READY:
|
||||
update_power_manager_restriction(true);
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
|
||||
if (usb_ctx.stack == USB_STACK_READY) {
|
||||
int err = usbd_enable(&blinky_usbd);
|
||||
if (module_lifecycle_is_running(&usb_ctx.lc) &&
|
||||
(usb_ctx.stack == USB_STACK_READY)) {
|
||||
int err = do_start();
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
} else {
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
LOG_ERR("USB start on VBUS ready failed (%d)", err);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case USBD_MSG_VBUS_REMOVED:
|
||||
update_power_manager_restriction(false);
|
||||
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
int err = usbd_disable(&blinky_usbd);
|
||||
int err = do_stop();
|
||||
|
||||
if (err) {
|
||||
LOG_ERR("usbd_disable failed (%d)", err);
|
||||
LOG_ERR("USB stop on VBUS removed failed (%d)", err);
|
||||
}
|
||||
} else {
|
||||
update_power_manager_restriction(false);
|
||||
}
|
||||
|
||||
if (usb_ctx.stack != USB_STACK_DISABLED) {
|
||||
if (usb_ctx.stack != USB_STACK_UNINITIALIZED) {
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
}
|
||||
|
||||
@@ -278,16 +274,13 @@ static void usbd_msg_cb(struct usbd_context *const usbd_ctx,
|
||||
}
|
||||
}
|
||||
|
||||
static int usb_stack_init_once(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (usb_stack_was_initialized()) {
|
||||
if (usb_ctx.stack == USB_STACK_DISABLED) {
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
usb_ctx.stack = USB_STACK_UNINITIALIZED;
|
||||
usb_ctx.bus = USB_BUS_DISCONNECTED;
|
||||
update_power_manager_restriction(false);
|
||||
|
||||
STRUCT_SECTION_FOREACH(usb_function_hook, hook) {
|
||||
if (hook->pre_stack_init == NULL) {
|
||||
@@ -321,6 +314,17 @@ static int usb_stack_init_once(void)
|
||||
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!usbd_can_detect_vbus(&blinky_usbd)) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
if (err) {
|
||||
@@ -329,89 +333,40 @@ static int usb_stack_init_once(void)
|
||||
}
|
||||
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usb_stack_enable_if_needed(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = usb_stack_init_once();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if ((usb_ctx.stack == USB_STACK_READY) &&
|
||||
(!usbd_can_detect_vbus(&blinky_usbd) ||
|
||||
(usb_ctx.bus != USB_BUS_DISCONNECTED))) {
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
|
||||
if (usb_ctx.bus == USB_BUS_DISCONNECTED) {
|
||||
usb_bus_set(USB_BUS_POWERED);
|
||||
} else {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
usb_ctx.bus = USB_BUS_POWERED;
|
||||
update_power_manager_restriction(true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
submit_usb_state(usb_public_state_get());
|
||||
if (usb_ctx.bus == USB_BUS_DISCONNECTED) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = usbd_enable(&blinky_usbd);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_enable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
usb_ctx.stack = USB_STACK_ENABLED;
|
||||
update_power_manager_restriction(true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int usb_stack_disable_if_needed(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
int err = usbd_disable(&blinky_usbd);
|
||||
int err;
|
||||
|
||||
if (usb_ctx.stack == USB_STACK_ENABLED) {
|
||||
err = usbd_disable(&blinky_usbd);
|
||||
if (err) {
|
||||
LOG_ERR("usbd_disable failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
usb_ctx.stack = USB_STACK_DISABLED;
|
||||
usb_ctx.stack = USB_STACK_READY;
|
||||
update_power_manager_restriction(false);
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_init(void)
|
||||
{
|
||||
usb_ctx.stack = USB_STACK_UNINITIALIZED;
|
||||
usb_ctx.bus = USB_BUS_DISCONNECTED;
|
||||
update_power_manager_restriction(false);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_start(void)
|
||||
{
|
||||
if (module_lifecycle_is_running(&usb_ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
submit_usb_state(usb_public_state_get());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!module_lifecycle_is_running(&usb_ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
submit_usb_state(usb_public_state_get());
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -421,7 +376,9 @@ static bool handle_module_state_event(const struct module_state_event *event)
|
||||
return false;
|
||||
}
|
||||
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_RUNNING);
|
||||
if (module_set_lifecycle(&usb_ctx.lc, LC_RUNNING) == 0) {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -434,14 +391,10 @@ static bool handle_module_suspend_req_event(
|
||||
return false;
|
||||
}
|
||||
|
||||
int err = usb_stack_disable_if_needed();
|
||||
|
||||
if (err) {
|
||||
LOG_WRN("USB suspend request failed (%d)", err);
|
||||
return false;
|
||||
if (module_set_lifecycle(&usb_ctx.lc, LC_SUSPENDED) == 0) {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_SUSPENDED);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -453,15 +406,10 @@ static bool handle_module_resume_req_event(
|
||||
return false;
|
||||
}
|
||||
|
||||
int err = usb_stack_enable_if_needed();
|
||||
|
||||
if (err) {
|
||||
LOG_WRN("USB resume request failed (%d)", err);
|
||||
return false;
|
||||
if (module_set_lifecycle(&usb_ctx.lc, LC_RUNNING) == 0) {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_RUNNING);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -483,7 +431,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (module_lifecycle_is_initialized(&usb_ctx.lc)) {
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_STOPPED);
|
||||
if (module_set_lifecycle(&usb_ctx.lc, LC_STOPPED) == 0) {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -491,7 +441,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (module_lifecycle_is_initialized(&usb_ctx.lc)) {
|
||||
(void)module_set_lifecycle(&usb_ctx.lc, LC_RUNNING);
|
||||
if (module_set_lifecycle(&usb_ctx.lc, LC_RUNNING) == 0) {
|
||||
submit_usb_state(usb_public_state_get());
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user