State Machines

Communication protocols are usually implemented as finite state machines. As in the OSI model, where each protocol layer is specialized to handle certain aspects of the whole communication system, states reflect discrete phases within each protocol. State transitions are triggered by events that cause the state machine to switch from one state to another.

To visualize state machines, state diagrams and state tables are used. State diagrams are graphical representations of states and transitions. State tables are 2-dimensional decision tables, where actions depend on the coordinates of state and event.

This is a simple state machine with 6 events:

and three states:

Figure 2.3 shows the state diagram corresponding to the state table shown in Table 2.4.

State Diagram
Figure 2-3 Simple State Diagram
Events States
CLOSE OPEN CONNECTED
Open OPEN error error
Close error CLOSE error
Connect error CONNECTED error
Disconnect error error OPEN
Read error error CONNECTED
Write error error CONNECTED
Table 2-4 Simple State Table

The initial state is the CLOSE state. In this state only the Open event is valid, causing a transition into the OPEN state. For all other events the state machine returns an error and remains in the CLOSE state.

Once in the OPEN state, the Close event will cause a transition back to the CLOSE state, while a Connect event will cause a transition to the CONNECTED state. For all other events the state machine returns an error and remains in the OPEN state.

Once in the CONNECTED state (also called data transfer state) Read or Write events are permitted but do not cause a change of state. Once the data transfer is completed a Disconnect event will cause a return to the OPEN state.

In the OPEN state the device can be closed (returning to the CLOSE state) or reconnected (returning to the CONNECTED state).

A protocol layer state machine may be further divided into hierarchical sublayers, each handling certain phases of the protocol. An example of such a nested state machine is the packet level of the CCITT Recommendation X.25 (see Appendix A or The Blue Book, Annex B).