# ADR-0002: Protocol Logic Is State-Driven

Status: Accepted

## Context

E27 communication requires ordered exchanges:
linking, hello, session bootstrap, authorization, and online operation.
Implicit or ad-hoc sequencing leads to fragile behavior.

## Decision

E27 protocol behavior is modeled as an explicit state machine.

- States represent protocol validity, not transport connectivity.
- State transitions occur only in response to validated protocol events.
- State is authoritative.

Example states include (names illustrative only):
WAIT_PANEL_HELLO, WAIT_API_LINK_RESPONSE,
WAIT_CLIENT_HELLO_RESPONSE, ONLINE_BASELINE, ONLINE_AUTHORIZED.

Protocol state transitions are independent of application-level
request/response correlation and do not rely on JSON sequence numbers.
See ADR-0011.

## Rationale

An explicit state model prevents illegal transitions,
simplifies debugging, and mirrors the Java reference implementation.

## Consequences

- Backend logic must be state-aware.
- API calls are gated by protocol state.
- Error handling becomes deterministic.

## Scope / Non-Goals

- Does not define exact state names.
- Does not mandate a specific state machine implementation style.

## References

- Node-Red E27 reference implementation
- E27 API Comms Protocol
- ADR-0011: Dual Sequence Numbers in E27 Protocol (Envelope vs JSON)
