// // Created by Hydro on 2026/4/21. // #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_init_with_queue (fsm_handle_t *handle_ptr, uint16_t init_state, const fsm_event_queue_init event_queue_init, const fsm_event_queue_put event_queue_put, const fsm_event_queue_get event_queue_get) { fsm_result_e result = FSM_OK; do { if (!(handle_ptr && event_queue_init && event_queue_put && event_queue_get)) { result = FSM_FAIL; 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_put = event_queue_put; handle_ptr->event_queue_init = event_queue_init; handle_ptr->custom_queue = true; result = handle_ptr->event_queue_init(handle_ptr->event_queue_handle, handle_ptr->event_queue, EVENT_QUEUE_LEN * sizeof(fsm_event_t)); } while (0); 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; }