狀態機模式是一種行為模式,在《設計模式》這本書中對其有詳細的描述,通過多態實現不同狀態的調轉行為的確是一種很好的方法,只可惜在嵌入式環境下,有時只能寫純C代碼,並且還需要考慮代碼的重入和多任務請求跳轉等情形,因此實現起來着實需要一番考慮。
近日在看了一個開源系統時,看到了一個狀態機的實現,也學着寫了一個,與大家分享。
首先,分析一下一個普通的狀態機究竟要實現哪些內容。
狀態機存儲從開始時刻到現在的變化,並根據當前輸入,決定下一個狀態。這意味着,狀態機要存儲狀態、獲得輸入(我們把它叫做跳轉條件)、做出響應。
如上圖所示,{s1, s2, s3}均為狀態,箭頭c1/a1表示在s1狀態、輸入為c1時,跳轉到s2,並進行a1操作。
最下方為一組輸入,狀態機應做出如下反應:
當前狀態 | 輸入 | 下一個狀態 | 動作 |
s1 | c1 | s2 | a1 |
s2 | c2 | s3 | a2 |
s3 | c1 | s2 | a3 |
s2 | c2 | s3 | a2 |
s3 | c1 | s2 | a3 |
s2 | c1 | s_trap | a_trap |
s_trap | c1 | s_trap | a_trap |
當某個狀態遇到不能識別的輸入時,就默認進入陷阱狀態,在陷阱狀態中,不論遇到怎樣的輸入都不能跳出。
為了表達上面這個自動機,我們定義它們的狀態和輸入類型:
typedef int State; typedef int Condition; #define STATES 3 + 1 #define STATE_1 0 #define STATE_2 1 #define STATE_3 2 #define STATE_TRAP 3 #define CONDITIONS 2 #define CONDITION_1 0 #define CONDITION_2 1
在嵌入式環境中,由於存儲空間比較小,因此把它們全部定義成宏。此外,為了降低執行時間的不確定性,我們使用O(1)的跳轉表來模擬狀態的跳轉。
首先定義跳轉類型:
typedef void (*ActionType)(State state, Condition condition); typedef struct { State next; ActionType action; } Trasition, * pTrasition;
然后按照上圖中的跳轉關系,把三個跳轉加一個陷阱跳轉先定義出來:
// (s1, c1, s2, a1) Trasition t1 = { STATE_2, action_1 }; // (s2, c2, s3, a2) Trasition t2 = { STATE_3, action_2 }; // (s3, c1, s2, a3) Trasition t3 = { STATE_2, action_3 }; // (s, c, trap, a1) Trasition tt = { STATE_TRAP, action_trap };
其中的動作,由用戶自己完成,在這里僅定義一條輸出語句。
void action_1(State state, Condition condition) { printf("Action 1 triggered.\n"); }
最后定義跳轉表:
pTrasition transition_table[STATES][CONDITIONS] = { /* c1, c2*/ /* s1 */&t1, &tt, /* s2 */&tt, &t2, /* s3 */&t3, &tt, /* st */&tt, &tt, };
即可表達上文中的跳轉關系。
最后定義狀態機,如果不考慮多任務請求,那么狀態機僅需要存儲當前狀態便行了。例如:
typedef struct { State current; } StateMachine, * pStateMachine; State step(pStateMachine machine, Condition condition) { pTrasition t = transition_table[machine->current][condition]; (*(t->action))(machine->current, condition); machine->current = t->next; return machine->current; }
但是考慮到當一個跳轉正在進行的時候,同時又有其他任務請求跳轉,則會出現數據不一致的問題。
舉個例子:task1(s1, c1/a1 –> s2)和task2(s2, c2/a2 –> s3)先后執行,是可以順利到達s3狀態的,但若操作a1運行的時候,執行權限被task2搶占,則task2此時看到的當前狀態還是s1,s1遇到c2就進入陷阱狀態,而不會到達s3了,也就是說,狀態的跳轉發生了不確定,這是不能容忍的。
因此要重新設計狀態機,增加一個“事務中”條件和一個用於存儲輸入的條件隊列。修改后的代碼如下:
#define E_OK 0 #define E_NO_DATA 1 #define E_OVERFLOW 2 typedef struct { Condition queue[QMAX]; int head; int tail; bool overflow; } ConditionQueue, * pConditionQueue; int push(ConditionQueue * queue, Condition c) { unsigned int flags; Irq_Save(flags); if ((queue->head == queue->tail + 1) || ((queue->head == 0) && (queue->tail == 0))) { queue->overflow = true; Irq_Restore(flags); return E_OVERFLOW; } else { queue->queue[queue->tail] = c; queue->tail = (queue->tail + 1) % QMAX; Irq_Restore(flags); } return E_OK; } int poll(ConditionQueue * queue, Condition * c) { unsigned int flags; Irq_Save(flags); if (queue->head == queue->tail) { Irq_Restore(flags); return E_NO_DATA; } else { *c = queue->queue[queue->head]; queue->overflow = false; queue->head = (queue->head + 1) % QMAX; Irq_Restore(flags); } return E_OK; } typedef struct { State current; bool inTransaction; ConditionQueue queue; } StateMachine, * pStateMachine; static State __step(pStateMachine machine, Condition condition) { State current = machine -> current; pTrasition t = transition_table[current][condition]; (*(t->action))(current, condition); current = t->next; machine->current = current; return current; } State step(pStateMachine machine, Condition condition) { Condition next_condition; int status; State current; if (machine->inTransaction) { push(&(machine->queue), condition); return STATE_INTRANSACTION; } else { machine->inTransaction = true; current = __step(machine, condition); status = poll(&(machine->queue), &next_condition); while(status == E_OK) { __step(machine, next_condition); status = poll(&(machine->queue), &next_condition); } machine->inTransaction = false; return current; } } void initialize(pStateMachine machine, State s) { machine->current = s; machine->inTransaction = false; machine->queue.head = 0; machine->queue.tail = 0; machine->queue.overflow = false; }