feat: 添加模块生命周期管理框架并重构现有模块

添加了模块生命周期管理头文件 module_lifecycle.h,定义了完整的生命周期状态机,
包括初始化、运行、停止、挂起和错误状态。同时将电池模块、BLE BAS模块、BLE HID
模块和BLE NUS模块重构为使用新的生命周期框架进行状态管理。

提升日志缓冲区大小以支持更详细的调试信息记录。
This commit is contained in:
2026-04-17 19:12:57 +08:00
parent 8bfb8b540c
commit ceebaaa600
19 changed files with 1361 additions and 1079 deletions

View File

@@ -16,6 +16,7 @@
#include <zephyr/sys/ring_buffer.h>
#include <zephyr/sys/util.h>
#include "module_lifecycle.h"
#include "proto_rx_event.h"
#include "proto_transport_state_event.h"
#include "proto_tx_event.h"
@@ -38,33 +39,53 @@ enum usb_cdc_business_state {
};
struct usb_cdc_ctx {
enum module_state lifecycle;
struct module_lifecycle_ctx lc;
enum usb_cdc_business_state business;
const struct device *cdc_dev;
uint8_t rx_ring_buffer[USB_CDC_RX_RING_BUF_SIZE];
uint8_t tx_ring_buffer[USB_CDC_TX_RING_BUF_SIZE];
struct ring_buf rx_ringbuf;
struct ring_buf tx_ringbuf;
struct k_work rx_work;
struct k_work_delayable rx_flush_work;
uint8_t proto_rx_buf[USB_CDC_PROTO_RX_BUF_SIZE];
bool usb_active;
size_t proto_rx_len;
};
static int do_init(void);
static int do_start(void);
static int do_stop(void);
static const struct device *const cdc_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart);
static const struct module_lifecycle_cfg lifecycle_cfg = {
.mode = ML_MODE_POWER,
.stopped_state = MODULE_STATE_STANDBY,
};
static const struct module_lifecycle_ops lifecycle_ops = {
.do_init = do_init,
.do_start = do_start,
.do_stop = do_stop,
};
static uint8_t rx_ring_buffer[USB_CDC_RX_RING_BUF_SIZE];
static uint8_t tx_ring_buffer[USB_CDC_TX_RING_BUF_SIZE];
static struct ring_buf rx_ringbuf;
static struct ring_buf tx_ringbuf;
static struct k_work rx_work;
static struct k_work_delayable rx_flush_work;
static uint8_t proto_rx_buf[USB_CDC_PROTO_RX_BUF_SIZE];
static struct usb_cdc_ctx ctx = {
.lifecycle = MODULE_STATE_OFF,
.lc = {
.state = LC_UNINIT,
.cfg = &lifecycle_cfg,
.ops = &lifecycle_ops,
},
.business = USB_CDC_BUS_OFFLINE,
.cdc_dev = DEVICE_DT_GET_ONE(zephyr_cdc_acm_uart),
.usb_active = false,
.proto_rx_len = 0U,
};
#define proto_rx_buf ctx.proto_rx_buf
static void validate_line_coding(void);
static bool lifecycle_is_ready(void)
{
return ctx.lifecycle == MODULE_STATE_READY;
return module_lifecycle_is_running(&ctx.lc);
}
static enum proto_transport_link_state transport_link_state_get(void)
@@ -75,44 +96,36 @@ static enum proto_transport_link_state transport_link_state_get(void)
PROTO_TRANSPORT_LINK_DOWN;
}
static bool control_poll_needed(enum module_state lifecycle,
enum usb_cdc_business_state business)
{
ARG_UNUSED(lifecycle);
ARG_UNUSED(business);
return false;
}
static void reset_ring_buffers(void)
{
unsigned int key = irq_lock();
ring_buf_init(&rx_ringbuf, sizeof(rx_ring_buffer), rx_ring_buffer);
ring_buf_init(&tx_ringbuf, sizeof(tx_ring_buffer), tx_ring_buffer);
ring_buf_init(&ctx.rx_ringbuf, sizeof(ctx.rx_ring_buffer), ctx.rx_ring_buffer);
ring_buf_init(&ctx.tx_ringbuf, sizeof(ctx.tx_ring_buffer), ctx.tx_ring_buffer);
irq_unlock(key);
}
static void disable_uart_io(void)
{
uart_irq_rx_disable(cdc_dev);
uart_irq_tx_disable(cdc_dev);
uart_irq_rx_disable(ctx.cdc_dev);
uart_irq_tx_disable(ctx.cdc_dev);
ctx.proto_rx_len = 0U;
k_work_cancel_delayable(&rx_flush_work);
k_work_cancel_delayable(&ctx.rx_flush_work);
reset_ring_buffers();
}
static void state_reconcile(enum module_state old_lifecycle,
static void state_reconcile(enum module_lifecycle old_lifecycle,
enum usb_cdc_business_state old_business)
{
enum proto_transport_link_state old_link =
((old_lifecycle == MODULE_STATE_READY) &&
((old_lifecycle == LC_RUNNING) &&
(old_business == USB_CDC_SESSION_READY)) ?
PROTO_TRANSPORT_LINK_READY :
PROTO_TRANSPORT_LINK_DOWN;
enum proto_transport_link_state new_link = transport_link_state_get();
if ((old_lifecycle == MODULE_STATE_READY) &&
if ((old_lifecycle == LC_RUNNING) &&
(old_business == USB_CDC_SESSION_READY) &&
(new_link == PROTO_TRANSPORT_LINK_DOWN)) {
disable_uart_io();
@@ -124,17 +137,17 @@ static void state_reconcile(enum module_state old_lifecycle,
validate_line_coding();
err = uart_line_ctrl_set(cdc_dev, UART_LINE_CTRL_DCD, 1);
err = uart_line_ctrl_set(ctx.cdc_dev, UART_LINE_CTRL_DCD, 1);
if (err) {
LOG_WRN("Failed to set DCD (%d)", err);
}
err = uart_line_ctrl_set(cdc_dev, UART_LINE_CTRL_DSR, 1);
err = uart_line_ctrl_set(ctx.cdc_dev, UART_LINE_CTRL_DSR, 1);
if (err) {
LOG_WRN("Failed to set DSR (%d)", err);
}
uart_irq_rx_enable(cdc_dev);
uart_irq_rx_enable(ctx.cdc_dev);
}
if (old_link != new_link) {
@@ -143,23 +156,9 @@ static void state_reconcile(enum module_state old_lifecycle,
}
}
static void lifecycle_set(enum module_state new_state)
{
enum module_state old_lifecycle = ctx.lifecycle;
enum usb_cdc_business_state old_business = ctx.business;
if (ctx.lifecycle == new_state) {
return;
}
ctx.lifecycle = new_state;
state_reconcile(old_lifecycle, old_business);
module_set_state(new_state);
}
static void business_state_set(enum usb_cdc_business_state new_state)
{
enum module_state old_lifecycle = ctx.lifecycle;
enum module_lifecycle old_lifecycle = ctx.lc.state;
enum usb_cdc_business_state old_business = ctx.business;
if (ctx.business == new_state) {
@@ -192,7 +191,7 @@ static void kick_tx(void)
return;
}
uart_irq_tx_enable(cdc_dev);
uart_irq_tx_enable(ctx.cdc_dev);
}
static void validate_line_coding(void)
@@ -200,7 +199,7 @@ static void validate_line_coding(void)
uint32_t baudrate = 0U;
int err;
err = uart_line_ctrl_get(cdc_dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
err = uart_line_ctrl_get(ctx.cdc_dev, UART_LINE_CTRL_BAUD_RATE, &baudrate);
if (err) {
LOG_WRN("Failed to get CDC baudrate (%d)", err);
} else {
@@ -215,7 +214,7 @@ static void validate_line_coding(void)
{
struct uart_config cfg;
err = uart_config_get(cdc_dev, &cfg);
err = uart_config_get(ctx.cdc_dev, &cfg);
if (err) {
LOG_WRN("uart_config_get failed (%d)", err);
} else {
@@ -243,7 +242,7 @@ static void rx_work_handler(struct k_work *work)
uint32_t len;
unsigned int key = irq_lock();
len = ring_buf_get(&rx_ringbuf, buffer, sizeof(buffer));
len = ring_buf_get(&ctx.rx_ringbuf, buffer, sizeof(buffer));
irq_unlock(key);
if (len == 0U) {
@@ -259,7 +258,7 @@ static void rx_work_handler(struct k_work *work)
if (len > 0U) {
memcpy(&proto_rx_buf[ctx.proto_rx_len], buffer, len);
ctx.proto_rx_len += len;
k_work_reschedule(&rx_flush_work, USB_CDC_PROTO_RX_FLUSH_DELAY);
k_work_reschedule(&ctx.rx_flush_work, USB_CDC_PROTO_RX_FLUSH_DELAY);
}
}
}
@@ -296,7 +295,7 @@ static void cdc_interrupt_handler(const struct device *dev, void *user_data)
uint32_t written;
unsigned int key = irq_lock();
written = ring_buf_put(&rx_ringbuf, buffer,
written = ring_buf_put(&ctx.rx_ringbuf, buffer,
(uint32_t)recv_len);
irq_unlock(key);
@@ -305,7 +304,7 @@ static void cdc_interrupt_handler(const struct device *dev, void *user_data)
recv_len - (int)written);
}
k_work_submit(&rx_work);
k_work_submit(&ctx.rx_work);
}
}
@@ -315,7 +314,7 @@ static void cdc_interrupt_handler(const struct device *dev, void *user_data)
int sent_len;
unsigned int key = irq_lock();
len = ring_buf_get(&tx_ringbuf, buffer, sizeof(buffer));
len = ring_buf_get(&ctx.tx_ringbuf, buffer, sizeof(buffer));
irq_unlock(key);
if (len == 0U) {
@@ -335,37 +334,52 @@ static void cdc_interrupt_handler(const struct device *dev, void *user_data)
}
}
static int module_init(void)
static int do_init(void)
{
if (!device_is_ready(cdc_dev)) {
if (!device_is_ready(ctx.cdc_dev)) {
LOG_ERR("CDC ACM device not ready");
return -ENODEV;
}
reset_ring_buffers();
k_work_init(&rx_work, rx_work_handler);
k_work_init_delayable(&rx_flush_work, rx_flush_work_handler);
uart_irq_callback_set(cdc_dev, cdc_interrupt_handler);
ctx = (struct usb_cdc_ctx) {
.lifecycle = MODULE_STATE_OFF,
.business = USB_CDC_BUS_OFFLINE,
.usb_active = false,
.proto_rx_len = 0U,
};
k_work_init(&ctx.rx_work, rx_work_handler);
k_work_init_delayable(&ctx.rx_flush_work, rx_flush_work_handler);
uart_irq_callback_set(ctx.cdc_dev, cdc_interrupt_handler);
ctx.business = USB_CDC_BUS_OFFLINE;
ctx.usb_active = false;
ctx.proto_rx_len = 0U;
return 0;
}
static void module_start(void)
static int do_start(void)
{
lifecycle_set(MODULE_STATE_READY);
business_state_sync_from_usb();
return 0;
}
static void module_pause(void)
static int do_stop(void)
{
business_state_set(USB_CDC_BUS_OFFLINE);
lifecycle_set(MODULE_STATE_STANDBY);
ctx.business = USB_CDC_BUS_OFFLINE;
return 0;
}
static int apply_lifecycle(enum module_lifecycle target)
{
enum module_lifecycle old_lifecycle = ctx.lc.state;
enum usb_cdc_business_state old_business = ctx.business;
int err = module_set_lifecycle(&ctx.lc, target);
if (err) {
return err;
}
state_reconcile(old_lifecycle, old_business);
if (target == LC_RUNNING) {
business_state_sync_from_usb();
}
return 0;
}
static bool handle_usb_state_event(const struct usb_state_event *event)
@@ -384,7 +398,7 @@ static bool handle_usb_state_event(const struct usb_state_event *event)
static bool handle_usb_control_event(const struct usb_control_event *event)
{
if (event->dev != cdc_dev) {
if (event->dev != ctx.cdc_dev) {
return false;
}
@@ -449,7 +463,7 @@ static bool handle_proto_tx_event(const struct proto_tx_event *event)
}
key = irq_lock();
written = ring_buf_put(&tx_ringbuf, event->dyndata.data,
written = ring_buf_put(&ctx.tx_ringbuf, event->dyndata.data,
(uint32_t)event->dyndata.size);
irq_unlock(key);
@@ -483,32 +497,23 @@ static bool app_event_handler(const struct app_event_header *aeh)
cast_module_state_event(aeh);
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
if (ctx.lifecycle == MODULE_STATE_OFF) {
int err = module_init();
if (err) {
lifecycle_set(MODULE_STATE_ERROR);
return false;
}
}
module_start();
(void)apply_lifecycle(LC_RUNNING);
}
return false;
}
if (is_power_down_event(aeh)) {
if (ctx.lifecycle != MODULE_STATE_OFF) {
module_pause();
if (module_lifecycle_is_initialized(&ctx.lc)) {
(void)apply_lifecycle(LC_STOPPED);
}
return false;
}
if (is_wake_up_event(aeh)) {
if (ctx.lifecycle != MODULE_STATE_OFF) {
module_start();
if (module_lifecycle_is_initialized(&ctx.lc)) {
(void)apply_lifecycle(LC_RUNNING);
}
return false;