Control, Gateways & Networked Systems · Study deck

States, Events, and Transitions

Picture a cabinet controller receiving an unlock command while its door is already open.

Gateway Gus is your guide for this deck.

statemachine
Gateway Gus, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Define state, event, transition, guard, action, and fault state as distinct model elements
  • Write a transition table naming source state, event, guard, action, next state, and expected output
  • Design a bounded retry path that routes guard failures to an explicit fault or recovery state instead of retrying forever
  • Convert asynchronous interrupts, callbacks, timers, and radio messages into explicit queued events before evaluating them against the current state
iotclass.org

Major section

Overview: Make Device Behavior Explicit

Instead of scattering behavior across flags, callbacks, and nested conditionals, the state machine names where the device is, what happened, and what behavior is allowed next.

  • This chapter focuses on the fundamentals: states, events, transitions, guards, actions, outputs, and fault paths.
  • The goal is not a decorative diagram.
  • A guard is not the same as an action.

Key terms

Good guards
Good guards are fast, deterministic, and side-effect free.

Why it matters

Keeping those parts separate makes reviews sharper because each transition can be checked for allowed input, blocked input, visible output, and safe cleanup.

A state-machine route is reviewable when the current state, event, guard, transition action, next state, and observable output are all named before code is written
A state-machine route is reviewable when the current state, event, guard, transition action, next state, and observable output are all named before code is written
iotclass.org

Major section

Overview: Make Device Behavior Explicit (continued)

The goal is behavior that a developer can implement, a tester can exercise, and an operator can reason about when the device is under stress.

  • For example, a soil-moisture sensor can be described with states such as SLEEP, SAMPLE, TRANSMIT, WAIT_ACK, and FAULT.
  • The state-machine view also separates facts that are easy to confuse in code.
  • The current state is not the same as the event that just arrived.
iotclass.org

Major section

Overview: Make Device Behavior Explicit (continued)

A condition that must be true before the transition can fire.

  • A timeout event is not the same as the retry policy.
  • Each mode should have clear event handling and a clear way out.
  • Good guards are fast, deterministic, and side-effect free.
  • The event names the input, timer, completion, or failure that must be handled.
iotclass.org

Major section

Overview: Make Device Behavior Explicit (continued)

The labels expose the mechanism behind a state-machine route is reviewable when the current state, event, guard, transition action, next state, and observable output are all named before code is written.

  • An explicit safe mode that preserves useful context and prevents ambiguous half-completed behavior.
  • The guard decides whether the transition is safe under the current context.
  • The action and next state define visible output, resource use, and follow-up behavior.
iotclass.org

Major section

Transition Contract

A practical state machine is a contract.

  • It says which events are accepted in each state, which guards must pass, which actions run, and which next state becomes active.
  • The contract should be readable as a transition table before it becomes code.
  • Thus Transition Contract becomes part of the running system argument, not an isolated diagram tour.

Key terms

Transition Contract
Transition Contract is easier to reason about once its roles are separated.

Why it matters

Those labels explain how a transition table makes the design checkable because each row connects source state, event, guard, action, next state, and expected output.

A transition table makes the design checkable because each row connects source state, event, guard, action, next state, and expected output
A transition table makes the design checkable because each row connects source state, event, guard, action, next state, and expected output
iotclass.org

Major section

Transition Contract (continued)

A flag combination that nobody can inspect.

  • A clear side-effect-free condition, such as retry count below its limit.
  • A guard that also sends messages or changes hardware.
  • A battery sensor samples periodically, transmits a packet, and waits for an acknowledgement.
  • Timers should arrive as events.
iotclass.org

Major section

Transition Contract (continued)

Those labels explain how a transition table makes the design checkable because each row connects source state, event, guard, action, next state, and expected output.

  • When the retry guard fails, the machine should enter a fault or recovery state instead of transmitting forever.
  • Tests need that boundary.
  • Retries need explicit guards and a recovery path.
iotclass.org

Major section

Timing, Hierarchy, Fault Paths

In IoT devices, those hard parts are usually asynchronous events, timers, retries, shared resources, and recovery after faults.

  • The internal design should make those pressures visible without turning every flag combination into a separate state.
  • Asynchronous sources should not execute hidden state changes in whichever callback happens to run first.
  • Behavior runs in an unpredictable order.

Key terms

Once events
Once events are serialized, fault handling still needs an explicit route back to known behaviour.

Why it matters

This matters before defining retries because a retry is safe only when the failed work, retained context, and actuator condition are known.

Interrupts, callbacks, timers, sensor completions, radio messages, and faults should become explicit events before they are evaluated by the state machine
Interrupts, callbacks, timers, sensor completions, radio messages, and faults should become explicit events before they are evaluated by the state machine
iotclass.org

Major section

Timing, Hierarchy, Fault Paths (continued)

Timeouts become hidden loops or blocking waits.

  • The: Event queue makes those inputs ordered and visible, and the: State machine handles one event at a time.
  • That serialization is the connection from asynchronous hardware pressure to deterministic guards, actions, and replayable tests.
  • Model timeout as an event and move long work into states.
iotclass.org

Major section

Timing, Hierarchy, Fault Paths (continued)

It also gives a failure trace an unambiguous event order instead of leaving reviewers to reconstruct callback timing.

  • Radio, sensor, storage, or actuator ownership becomes ambiguous.
  • Once events are serialized, fault handling still needs an explicit route back to known behaviour.
  • A flat state machine is easiest to audit, but it can grow quickly.
iotclass.org

Major section

Timing, Hierarchy, Fault Paths (continued)

That path makes recovery evidence part of the transition contract and closes the timing discussion with a testable failure outcome.

  • Tests can now assert both the FAULT entry outputs and the conditions that permit IDLE to resume.
  • Events that arrive in the wrong state should be handled deliberately, not left to default behavior.
  • Recovery tests should prove safe outputs, retry limits, preserved context, and return to a known state.
iotclass.org

Deck summary

Key takeaways

Instead of scattering behavior across flags, callbacks, and nested conditionals, the state machine names where the device is, what happened, and what behavior is allowed next.

  • The goal is behavior that a developer can implement, a tester can exercise, and an operator can reason about when the device is under stress.
  • A condition that must be true before the transition can fire.
  • The labels expose the mechanism behind a state-machine route is reviewable when the current state, event, guard, transition action, next state, and observable output are all named before code is written.
  • A practical state machine is a contract.
iotclass.org

Retrieval practice

Recall check 1 of 3

Gateway Gus says: answer from memory, then check your reasoning.

Q1A sensor alternates between SLEEP, SAMPLE, TRANSMIT, and FAULT. Why is this a good fit for a finite state machine?

AModel each mode, event, guard, output, and fault path.
BUse one global flag so callbacks can switch behavior without a table.
CTreat every sample, timeout, and radio result as the same state.
DHide recovery in drivers so application code sees only success.
Show answer

Answer: A Finite state machines are strongest when device modes, events, guards, outputs, and recovery paths need to be named and tested.

iotclass.org

Retrieval practice

Recall check 2 of 3

Gateway Gus says: answer from memory, then check your reasoning.

Q2A TRANSMIT state retries forever after acknowledgement timeouts. Which transition-table change most directly fixes the design?

AAdd a retry guard; route limit failures to recovery.
BSend directly from SAMPLE and remove the timeout state.
CLower radio power and leave retries unbounded in TRANSMIT.
DIgnore each timeout until the next scheduled sample event.
Show answer

Answer: A A transition table should expose both the retry path and the recovery path.

iotclass.org

Retrieval practice

Recall check 3 of 3

Gateway Gus says: answer from memory, then check your reasoning.

Q3A firmware design handles radio callbacks, sensor completions, button presses, and timer expirations. What is the safest state-machine pattern?

AQueue callbacks as events and handle them from the current state.
BLet callbacks change modes directly so the event queue stays small.
CBlock in each state handler until expected callbacks arrive.
DDrop out-of-order events because rare timing cases can wait.
Show answer

Answer: A Under the hood, robust IoT state machines turn asynchronous signals into explicit events and then evaluate those events against the current state.

iotclass.org

Print reference

Answers

Answer key.

  1. A · Finite state machines are strongest when device modes, events, guards, outputs, and recovery paths need to be named and tested.
  2. A · A transition table should expose both the retry path and the recovery path.
  3. A · Under the hood, robust IoT state machines turn asynchronous signals into explicit events and then evaluate those events against the current state.
iotclass.org