feat: 添加模块生命周期管理框架并重构现有模块
添加了模块生命周期管理头文件 module_lifecycle.h,定义了完整的生命周期状态机, 包括初始化、运行、停止、挂起和错误状态。同时将电池模块、BLE BAS模块、BLE HID 模块和BLE NUS模块重构为使用新的生命周期框架进行状态管理。 提升日志缓冲区大小以支持更详细的调试信息记录。
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include "function_bitmap_state_event.h"
|
||||
#include "function_bitmap_update_event.h"
|
||||
#include "hid_led_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
#include "proto_rx_event.h"
|
||||
#include "proto_transport_state_event.h"
|
||||
#include "proto_tx_event.h"
|
||||
@@ -38,16 +39,41 @@ LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
||||
#define PROTOCOL_CAPABILITY_FLAGS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4))
|
||||
#define PROTOCOL_MAX_MSG_LEN 128U
|
||||
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
|
||||
enum proto_session_state {
|
||||
PROTO_SESSION_DOWN = 0,
|
||||
PROTO_SESSION_WAIT_HELLO,
|
||||
PROTO_SESSION_ACTIVE,
|
||||
};
|
||||
|
||||
static enum proto_session_state session_state[PROTO_TRANSPORT_COUNT];
|
||||
struct protocol_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
enum proto_session_state session_state[PROTO_TRANSPORT_COUNT];
|
||||
};
|
||||
|
||||
static int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
|
||||
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 struct protocol_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
};
|
||||
|
||||
#define session_state ctx.session_state
|
||||
|
||||
static int decode_body(const uint8_t *payload, size_t payload_len,
|
||||
CdcPacketBody *body)
|
||||
@@ -157,7 +183,7 @@ static int encode_function_bitmap_state(const uint8_t *bitmap, uint8_t *payload,
|
||||
return encode_body(&body, payload, payload_buf_size, payload_len);
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(session_state); i++) {
|
||||
session_state[i] = PROTO_SESSION_DOWN;
|
||||
@@ -166,27 +192,26 @@ static int module_init(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < ARRAY_SIZE(session_state); i++) {
|
||||
session_state[i] = PROTO_SESSION_DOWN;
|
||||
}
|
||||
|
||||
running = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int protocol_module_process_message(enum proto_transport transport,
|
||||
@@ -204,7 +229,7 @@ int protocol_module_process_message(enum proto_transport transport,
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return -EAGAIN;
|
||||
}
|
||||
|
||||
@@ -317,7 +342,7 @@ static bool handle_proto_rx_event(const struct proto_rx_event *event)
|
||||
size_t rsp_payload_len = 0U;
|
||||
int err;
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -373,7 +398,7 @@ static bool handle_function_bitmap_state_event(
|
||||
size_t payload_len;
|
||||
int err;
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -406,7 +431,7 @@ static bool handle_hid_led_event(const struct hid_led_event *event)
|
||||
int err;
|
||||
enum proto_transport transport;
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -454,25 +479,9 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
|
||||
if (is_module_state_event(aeh)) {
|
||||
const struct module_state_event *event = cast_module_state_event(aeh);
|
||||
int err;
|
||||
|
||||
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
||||
if (!initialized) {
|
||||
err = module_init();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
err = module_start();
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -481,23 +490,16 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_wake_up_event(aeh)) {
|
||||
if (initialized) {
|
||||
int err = module_start();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
} else {
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user