Skip to content

Commit e68d046

Browse files
committed
fsm: add draft for state handlers list traverse
1 parent fec4e20 commit e68d046

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

src/active-object/active_object_impl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717
void ACTIVE_OBJECT_T##_Dispatch(ACTIVE_OBJECT_T *const self, EVENT_T event) { \
1818
if (QUEUE_##EVENT_T##_IsFull(&self->queue)) return; /* TODO Handle error, e.g., logging */ \
1919
QUEUE_##EVENT_T##_Enqueue(&self->queue, event); \
20-
} \
20+
} \
21+
\
22+
/* TODO add non basic transition, handle start, traverse, exit state functions */ \
2123
\
2224
bool ACTIVE_OBJECT_T##_basicTransitionToNextState(ACTIVE_OBJECT_T *const self, STATE_T nextState) { \
2325
self->state = nextState; \
2426
return true; \
2527
} \
2628
\
27-
void ACTIVE_OBJECT_T##_ProcessQueue(ACTIVE_OBJECT_T *const self, EVENT_T##_HANDLER_F eventHandlerCb, STATE_T##_TRANSITION_F transitionToNextStateCb, ACTIVE_OBJECT_T##HAS_EMPTY_QUEUE_F hasEmptyQueueCb) { \
29+
void ACTIVE_OBJECT_T##_ProcessQueue( \
30+
ACTIVE_OBJECT_T *const self, \
31+
EVENT_T##_HANDLER_F eventHandlerCb, \
32+
STATE_T##_TRANSITION_F transitionToNextStateCb, \
33+
ACTIVE_OBJECT_T##HAS_EMPTY_QUEUE_F hasEmptyQueueCb) { \
2834
bool isEmptyQueue = EMPTY_QUEUE == QUEUE_##EVENT_T##_GetSize(&self->queue); \
2935
if (isEmptyQueue) return hasEmptyQueueCb(self); \
3036
EVENT_T e = QUEUE_##EVENT_T##_Dequeue(&self->queue); \

src/fsm/fsm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ typedef struct { \
5252
\
5353
STATE_T ACTIVE_OBJECT_T##_FSM_ProcessEventToNextState(\
5454
ACTIVE_OBJECT_T *const activeObject, \
55-
EVENT_T event, \
56-
EVENT_T##_HANDLE_F transitionTable[statesMax][eventsMax] \
55+
EVENT_T event, \
56+
EVENT_T##_HANDLE_F transitionTable[statesMax][eventsMax], \
57+
STATE_T##_HANDLE_FUNCTIONS stateHandlersList[statesMax] \
5758
);
5859

5960
/**

src/fsm/fsm_impl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
STATE_T ACTIVE_OBJECT_T##_FSM_ProcessEventToNextState(\
99
ACTIVE_OBJECT_T *const activeObject, \
1010
EVENT_T event, \
11-
EVENT_T##_HANDLE_F transitionTable[statesMax][eventsMax] \
11+
EVENT_T##_HANDLE_F transitionTable[statesMax][eventsMax], \
12+
STATE_T##_HANDLE_FUNCTIONS stateHandlersList[statesMax] \
1213
) { \
1314
STATE_T currState = activeObject->state; \
1415
EVENT_T##_HANDLE_F stateHandler = transitionTable[currState][event.sig]; \
1516
\
1617
assert(NULL != stateHandler); \
1718
\
18-
STATE_T nextState = stateHandler(activeObject, event); \
19-
\
19+
STATE_T nextState = stateHandler(activeObject, event); \
20+
\
2021
return nextState; \
2122
};
2223

test/fsm/fsm.test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ void test_fsm_process_event_to_next_state(void) {
5959
// Process MOCK_EVENT_A_SIG while in MOCK_STATE_IDLE -> should transition to MOCK_STATE_RUNNING
6060
ao.state = MOCK_STATE_IDLE;
6161
MockEvent e = { .sig = MOCK_EVENT_A_SIG };
62-
MockState nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e, transitionTable);
62+
MockState nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e, transitionTable, NULL);
6363
TEST_ASSERT_EQUAL(MOCK_STATE_RUNNING, nextState);
6464

6565
// Process MOCK_EVENT_B_SIG while in MOCK_STATE_RUNNING -> should transition to MOCK_STATE_PAUSED
6666
ao.state = MOCK_STATE_RUNNING;
6767
e.sig = MOCK_EVENT_B_SIG;
68-
nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e, transitionTable);
68+
nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e, transitionTable, NULL);
6969
TEST_ASSERT_EQUAL(MOCK_STATE_PAUSED, nextState);
7070

7171
// Process MOCK_EVENT_C_SIG while in MOCK_STATE_PAUSED -> should transition to MOCK_STATE_IDLE
7272
ao.state = MOCK_STATE_PAUSED;
7373
e.sig = MOCK_EVENT_C_SIG;
74-
nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e, transitionTable);
74+
nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e, transitionTable, NULL);
7575
TEST_ASSERT_EQUAL(MOCK_STATE_IDLE, nextState);
7676
}
7777

test/integration/fsm_active_object.test.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,21 @@ void integration_test_active_object_with_fsm_and_queue(void) {
8989
TEST_ASSERT_EQUAL(1, QUEUE_MockEvent_GetSize(&ao.queue));
9090

9191
// Process Event with FSM
92-
MockState nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e1, stateTransitionTable);
92+
MockState nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e1, stateTransitionTable, NULL);
9393
TEST_ASSERT_EQUAL(MOCK_STATE_RUNNING, nextState);
9494
ao.state = nextState; // Update the actual state of the active object
9595

9696
// Dispatching and processing a second event
9797
MockEvent e2 = { .sig = MOCK_EVENT_B_SIG };
9898
MockActiveObject_Dispatch(&ao, e2);
99-
nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e2, stateTransitionTable);
99+
nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e2, stateTransitionTable, NULL);
100100
TEST_ASSERT_EQUAL(MOCK_STATE_PAUSED, nextState);
101101
ao.state = nextState; // Update the actual state of the active object
102102

103103
// Dispatching and processing a third event
104104
MockEvent e3 = { .sig = MOCK_EVENT_C_SIG };
105105
MockActiveObject_Dispatch(&ao, e3);
106-
nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e3, stateTransitionTable);
106+
nextState = MockActiveObject_FSM_ProcessEventToNextState(&ao, e3, stateTransitionTable, NULL);
107107
TEST_ASSERT_EQUAL(MOCK_STATE_IDLE, nextState);
108108
ao.state = nextState; // Update the actual state of the active object
109109
}

0 commit comments

Comments
 (0)