feat: 新增接口实现,基础功能完成

This commit is contained in:
赵天浩
2026-04-21 07:00:49 +08:00
parent 80e4dd2fc5
commit 1a2aeca518
2 changed files with 152 additions and 6 deletions

147
fsm.c
View File

@@ -2,8 +2,33 @@
// Created by Hydro on 2026/4/21. // Created by Hydro on 2026/4/21.
// //
#include "fsm.h" #include "fsm.h"
fsm_transition_t *
fsm_transition (fsm_handle_t *handle_ptr, const fsm_event_t *event)
{
fsm_transition_t *trans = NULL;
do
{
if (!(handle_ptr && event))
{
break;
}
const fsm_state_t *current_state = fsm_get_state(handle_ptr);
for (size_t i = 0; i < handle_ptr->num_trans; i++)
{
if (handle_ptr->p_trans_list[i].state == current_state->state
&& handle_ptr->p_trans_list[i].event == event->event)
{
trans = &handle_ptr->p_trans_list[i];
break;
}
}
} while (0);
return trans;
}
fsm_result_e fsm_result_e
fsm_init_with_queue (fsm_handle_t *handle_ptr, fsm_init_with_queue (fsm_handle_t *handle_ptr,
uint16_t init_state,
const fsm_event_queue_init event_queue_init, const fsm_event_queue_init event_queue_init,
const fsm_event_queue_put event_queue_put, const fsm_event_queue_put event_queue_put,
const fsm_event_queue_get event_queue_get) const fsm_event_queue_get event_queue_get)
@@ -17,14 +42,134 @@ fsm_init_with_queue (fsm_handle_t *handle_ptr,
result = FSM_FAIL; result = FSM_FAIL;
break; break;
} }
for (size_t i = 0; i < handle_ptr->num_states; i++)
{
if (handle_ptr->p_state_list[i].state == init_state)
{
handle_ptr->current_state = &handle_ptr->p_state_list[i];
}
}
handle_ptr->event_queue_get = event_queue_get; handle_ptr->event_queue_get = event_queue_get;
handle_ptr->event_queue_put = event_queue_put; handle_ptr->event_queue_put = event_queue_put;
handle_ptr->event_queue_init = event_queue_init; handle_ptr->event_queue_init = event_queue_init;
handle_ptr->custom_queue = true;
result = handle_ptr->event_queue_init(handle_ptr->event_queue_handle, result = handle_ptr->event_queue_init(handle_ptr->event_queue_handle,
handle_ptr->event_queue, handle_ptr->event_queue,
EVENT_QUEUE_LEN EVENT_QUEUE_LEN
* sizeof(fsm_event_t)); * sizeof(fsm_event_t));
} while (0); } while (0);
return result; return result;
}
fsm_result_e
fsm_event_put (fsm_handle_t *handle_ptr, fsm_event_t *event)
{
fsm_result_e result = FSM_OK;
do
{
if (!(handle_ptr && event))
{
result = FSM_FAIL;
break;
}
if (handle_ptr->custom_queue)
{
result = handle_ptr->event_queue_put(handle_ptr->event_queue_handle,
event);
}
else
{
// TODO:补充默认队列的环形缓冲区填充逻辑
result = FSM_FAIL;
}
} while (0);
return result;
}
fsm_result_e
fsm_process_queue (fsm_handle_t *handle_ptr)
{
fsm_result_e result = FSM_OK;
do
{
if (!handle_ptr)
{
result = FSM_FAIL;
break;
}
fsm_event_t event = { 0 };
if (handle_ptr->custom_queue)
{
result = handle_ptr->event_queue_get(handle_ptr->event_queue_handle,
&event);
}
if (result != FSM_OK)
{
result = FSM_FAIL;
break;
}
fsm_dispatch(handle_ptr, &event);
} while (0);
return result;
}
fsm_result_e
fsm_dispatch (fsm_handle_t *handle_ptr, const fsm_event_t *event)
{
fsm_result_e result = FSM_OK;
do
{
if (!(handle_ptr && event))
{
result = FSM_FAIL;
break;
}
const fsm_transition_t *trans = fsm_transition(handle_ptr, event);
fsm_state_t *current_state = fsm_get_state(handle_ptr);
if (trans)
{
// 发生状态切换
if (trans->next_state != current_state->state)
{
if (current_state->on_exit)
{
current_state->on_exit(current_state->user_data);
}
// 执行事件动作
if (trans->on_action)
{
trans->on_action(event->user_data);
}
for (size_t i = 0; i < handle_ptr->num_states; i++)
{
if (handle_ptr->p_state_list[i].state == trans->next_state)
{
current_state = &handle_ptr->p_state_list[i];
if (current_state->on_entry)
{
current_state->on_entry(current_state->user_data);
}
break;
}
}
handle_ptr->current_state = current_state;
}
else
{
// 执行事件动作
if (trans->on_action)
{
trans->on_action(event->user_data);
}
}
}
else
{
result = FSM_FAIL;
break;
}
} while (0);
return result;
}
fsm_state_t *
fsm_get_state (const fsm_handle_t *h)
{
return h->current_state;
} }

11
fsm.h
View File

@@ -46,9 +46,9 @@ struct qf_fsm_event
}; };
struct qf_fsm struct qf_fsm
{ {
uint16_t current_state; fsm_state_t *current_state;
uint16_t num_states;
char *name; char *name;
uint16_t num_states;
fsm_state_t *p_state_list; fsm_state_t *p_state_list;
uint32_t num_trans; uint32_t num_trans;
fsm_transition_t *p_trans_list; fsm_transition_t *p_trans_list;
@@ -59,13 +59,14 @@ struct qf_fsm
fsm_event_queue_put event_queue_put; fsm_event_queue_put event_queue_put;
fsm_event_queue_get event_queue_get; fsm_event_queue_get event_queue_get;
}; };
fsm_result_e fsm_init(fsm_handle_t *handle_ptr); fsm_result_e fsm_init(fsm_handle_t *handle_ptr, uint16_t init_state);
fsm_result_e fsm_init_with_queue(fsm_handle_t *handle_ptr, fsm_result_e fsm_init_with_queue(fsm_handle_t *handle_ptr,
uint16_t init_state,
fsm_event_queue_init event_queue_init, fsm_event_queue_init event_queue_init,
fsm_event_queue_put event_queue_put, fsm_event_queue_put event_queue_put,
fsm_event_queue_get event_queue_get); fsm_event_queue_get event_queue_get);
fsm_result_e fsm_event_put(fsm_handle_t *handle_ptr, fsm_event_t *event); fsm_result_e fsm_event_put(fsm_handle_t *handle_ptr, fsm_event_t *event);
fsm_result_e fsm_process_queue(fsm_handle_t *handle_ptr); fsm_result_e fsm_process_queue(fsm_handle_t *handle_ptr);
fsm_result_e fsm_dispatch(fsm_handle_t *handle_ptr, fsm_event_t *event); fsm_result_e fsm_dispatch(fsm_handle_t *handle_ptr, const fsm_event_t *event);
uint16_t fsm_get_state(fsm_handle_t *h); fsm_state_t *fsm_get_state(const fsm_handle_t *h);
#endif // QF_FSM_FSM_H #endif // QF_FSM_FSM_H