嵌入式设计模式:有限状态自动机的C语言实现
转自:http://www.cnblogs.com/autosar/archive/2012/06/22/2558604.html
状态机模式是一种行为模式,在《设计模式》这本书中对其有详细的描述,通过多态实现不同状态的调转行为的确是一种很好的方法,只可惜在嵌入式环境下,有时只能写纯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 + ) || ((queue->head == ) && (queue->tail == )))
{
queue->overflow = true;
Irq_Restore(flags);
return E_OVERFLOW;
}
else
{
queue->queue[queue->tail] = c;
queue->tail = (queue->tail + ) % 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 + ) % 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 = ;
machine->queue.tail = ;
machine->queue.overflow = false;
}
嵌入式设计模式:有限状态自动机的C语言实现的更多相关文章
- [状态机]嵌入式设计模式:有限状态自动机的C语言实现
转自:http://www.cnblogs.com/autosar/archive/2012/06/22/2558604.html 状态机模式是一种行为模式,在<设计模式>这本书中对其有详 ...
- 用C语言实现有限状态自动机FSM
摘要:状态机模式是一种行为模式,在<设计模式>这本书中对其有详细的描述,通过多态实现不同状态的调转行为的确是一种很好的方法,只可惜在嵌入式环境下,有时只能写纯C代码,并且还需要考虑代码的重 ...
- 非确定有限状态自动机的构建(一)——NFA的定义和实现
保留版权,转载需注明出处(http://blog.csdn.net/panjunbiao). 非确定有限状态自动机(Nondeterministic Finite Automata,NFA)由以下元素 ...
- 简聊DFA(确定性有限状态自动机)
状态机理论最初的发展在数字电路设计领域.而在软件设计领域,状态机设计的理论俨然已经自成一体. 状态机是软件编程中的一个重要概念,比这个概念更重要的是对它的灵活应用.在一个思路清晰而且高效的程序中,必然 ...
- 非确定有限状态自动机的构建(二)——将CharVal转换为NFA
保留版权,转载注明出处:潘军彪的个人博客(http://blog.csdn.net/panjunbiao/article/details/9378933) 将上下文无关文法读入内存之后,可以将它转换成 ...
- DFA确定有限状态自动机
DFA 在计算理论中,确定有限状态自动机或确定有限自动机(英语:deterministic finite automaton, DFA)是一个能实现状态转移的自动机.对于一个给定的属于该自动机的状态和 ...
- K:有限状态自动机
有限状态自动机是一种特殊的状态机.它表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.有限状态自动机分为两种,一种是 确定有限状态自动机(DFA) ,一种是 非确定有限状态自动机(NF ...
- 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化
神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...
- <轻量算法>根据核密度估计检测波峰算法 ---基于有限状态自动机和递归实现
原创博客,转载请联系博主! 希望我思考问题的思路,也可以给大家一些启发或者反思! 问题背景: 现在我们的手上有一组没有明确规律,但是分布有明显聚簇现象的样本点,如下图所示: 图中数据集是显然是个3维的 ...
随机推荐
- 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 用jquery ,当改变窗口或屏幕大小时调用function,用哪个事件?
$(window).resize(function(){ //process here}); window的onresize事件. $(window).resize(function () { ...
- ES6 — 新增关键字let、const
ECMAScript 是什么? 首先,我们都知道JavaScript由三部分组成:ECMAScript,DOM,BOM: 其中的ECMAScript是Javascript的语法规范. ECMAScri ...
- Spring+Websocket实现消息的推送
http://my.oschina.net/ldl123292/blog/304360
- sgu548 Dragons and Princesses 贪心+优先队列
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=548 题目意思: 有一个骑士,要经过n个房间,开始在第一个房间,每个房间里面有龙或者 ...
- Codeforces Round #120 (Div. 2)
A. Vasya and the Bus 根据\(n,m\)是否为0分类讨论下. B. Surrounded 判断两圆是否有交点,否则构造的圆与两圆相切. C. STL 看代码比较清楚. void t ...
- windows 10安装framework 3.5失败的解决方案
装了两次win 10,全都因为没法安装framework 3.5,用不了老版本的开发环境,又换回了win7. 网上有两种解决方案: a,通过iso安装. 可是拜托,我的系统都是用ghost版本安装的, ...
- nginx配置-http和https
#user nobody;worker_processes 1;error_log logs/error.log;#error_log logs/error.log notice;#error_log ...
- python tornado框架使用
处理方法 t_handler.py from tornado.web import RequestHandler class IndexHandler(RequestHandler): def get ...
- C++ string::size_type 类型【转】
int main() { string str("Hello World!\n"); cout << "The size of " << ...