Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Open Source

The embedded Finite State Machine

A finite state machine (fsm) is a method to model a system's behavior. The model is composed of a set of states, of which one is designated as the start or initial state. The initial state is the state that the fsm begins when it is instantiated. The transition from the initial state and between the set of states is determined by a set of events. From one state to the next, an event may be processed to cause a transition or ignored.

The embedded Finite State Machine (eFSM) I present here is written in C and portable to different environments. You can download the eFSM from SourceForge here.

The set of states is defined in a state description table. The state description table from the demo is shown below:

static state_description_t normalized_state_table[] =
  {{idle_s, "Idle State"},  
   {wait_for_init_ack_s, "Wait for Init Ack State"}, 
   {established_s, "Established State"}, 
   {wait_for_term_ack_s, "Wait for Terminate Ack State"}, 

Once the states are determined, the set of normalized events is defined is the event description table as shown below:

static event_description_t normalized_event_table[] =
  {{start_init_e, "Start Session Init"},
   {init_rcvd_e, "Session Init"},
   {init_tmo_e, "Session Init ACK TMO"},
   {init_ack_e, "Session Init ACK"},
   {start_term_e, "Start Session Termination"},
   {term_rcvd_e, "Session Terminate"},
   {term_ack_e, "Session Terminate ACK"},

Now to put the states and events together. The state table provides an entry for each state. The entries map a state to the associated state-event table. In the table below, the established state is mapped to the established state-event table:

static state_tuple_t  demo_state_table[] =
/***   State ID  ******************   Event Table ***/ 
    { {idle_s,                        state_idle_events},
      {wait_for_init_ack_s,           state_wait_for_init_ack_events}, 
      {established_s,                 state_established_events}, 
      {wait_for_term_ack_s,           state_wait_for_term_ack_events},
      {FSM_NULL_STATE_ID, NULL} };  

Here the event handler and the transition caused by an event is defined:

static event_tuple_t  state_established_events[] =
/******  Event ID  *******  Handler ************  Next State ID *****/ 
    {  { start_init_e,    event_ignore,           established_s},
       { init_rcvd_e,     event_ignore,           established_s},
       { init_tmo_e,      event_ignore,           established_s},
       { init_ack_e,      event_ignore,           established_s},
       { start_term_e,    event_start_term,       wait_for_term_ack_s},
       { term_rcvd_e,     event_term_rcvd,        idle_s},
       { term_ack_e,      event_ignore,           established_s} }; 

With the state and event tables, the fsm_engine() can be called to process events and drive state transitions.

Included are calls to display the state machine history of events and resulting state transitions (useful for debugging) and a call to display the state-event tables. The organization of the demo software suggests an organization for your real application.

You can find a comprehensive and detailed description of finite state machines here.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.