# transportdebuger/lib_fsm architecture

> Architecture diagram of the GitHub repository transportdebuger/lib_fsm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/transportdebuger/lib_fsm
- Repository: https://github.com/transportdebuger/lib_fsm

## Overview

s21 FSM is a small reusable C library for defining and running finite-state machines. A caller creates an instance, registers states and transitions, selects an initial state, and initializes it; it then dispatches events or updates the current state. The API also supports changing and removing model entries and managing global, state, and transition contexts. The map focuses on runtime behavior; callback ordering and re-entrancy protection are documented capabilities. Source excerpts are partial, so internal relationships not shown there are omitted.

Main entry points: [`src/s21_fsm.c`](https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c) (Event dispatch), [`include/s21_fsm.h`](https://github.com/transportdebuger/lib_fsm/blob/main/include/s21_fsm.h) (C API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Public API"]
  node_public_api["C API<br/>[s21_fsm.h]"]
end

subgraph group_model["FSM Model"]
  node_lifecycle["FSM lifecycle<br/>[s21_fsm.c]"]
  node_states["State registry<br/>[s21_fsm.c]"]
  node_transitions["Transition registry<br/>[s21_fsm.c]"]
  node_fsm_storage[("FSM instance<br/>[s21_fsm.c]")]
end

subgraph group_runtime["Execution"]
  node_dispatch["Event dispatch<br/>[s21_fsm.c]"]
  node_update["State update<br/>[s21_fsm.c]"]
  node_callbacks["User callbacks"]
end

node_caller(("C/C++ or FFI caller"))

node_caller -->|"calls"| node_public_api
node_public_api -->|"implemented by"| node_lifecycle
node_caller -->|"registers states"| node_states
node_caller -->|"registers transitions"| node_transitions
node_caller -->|"initializes"| node_lifecycle
node_lifecycle -->|"creates"| node_fsm_storage
node_states -->|"stores states"| node_fsm_storage
node_transitions -->|"stores transitions"| node_fsm_storage
node_caller -->|"dispatches events"| node_dispatch
node_dispatch -->|"reads current state"| node_fsm_storage
node_dispatch -->|"searches transitions"| node_transitions
node_dispatch -->|"checks guard"| node_callbacks
node_dispatch -->|"runs effects"| node_callbacks
node_caller -->|"updates state"| node_update
node_update -->|"runs do callback"| node_callbacks
node_dispatch -->|"runs entry and exit"| node_callbacks

click node_public_api "https://github.com/transportdebuger/lib_fsm/blob/main/include/s21_fsm.h"
click node_lifecycle "https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c"
click node_states "https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c"
click node_transitions "https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c"
click node_fsm_storage "https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c"
click node_dispatch "https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c"
click node_update "https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_api toneBlue
class node_lifecycle,node_states,node_transitions,node_fsm_storage toneAmber
class node_dispatch,node_update,node_callbacks toneMint
class node_caller toneIndigo
```

## Components

### Public API

- [C API](https://github.com/transportdebuger/lib_fsm/blob/main/include/s21_fsm.h): `include/s21_fsm.h`

### FSM Model

- [FSM lifecycle](https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c): `src/s21_fsm.c`
- [State registry](https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c): `src/s21_fsm.c`
- [Transition registry](https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c): `src/s21_fsm.c`
- [FSM instance](https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c): `src/s21_fsm.c`

### Execution

- [Event dispatch](https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c): `src/s21_fsm.c`
- [State update](https://github.com/transportdebuger/lib_fsm/blob/main/src/s21_fsm.c): `src/s21_fsm.c`
- **User callbacks**

### Other components

- **C/C++ or FFI caller**

## Connections

- C/C++ or FFI caller → C API: calls (evidence: `README.md`)
- C API → FSM lifecycle: implemented by (evidence: `src/s21_fsm.c`)
- C/C++ or FFI caller → State registry: registers states (evidence: `README.md`)
- C/C++ or FFI caller → Transition registry: registers transitions (evidence: `README.md`)
- C/C++ or FFI caller → FSM lifecycle: initializes (evidence: `README.md`)
- FSM lifecycle → FSM instance: creates (evidence: `src/s21_fsm.c`)
- State registry → FSM instance: stores states (evidence: `src/s21_fsm.c`)
- Transition registry → FSM instance: stores transitions (evidence: `src/s21_fsm.c`)
- C/C++ or FFI caller → Event dispatch: dispatches events (evidence: `README.md`)
- Event dispatch → FSM instance: reads current state (evidence: `src/s21_fsm.c`)
- Event dispatch → Transition registry: searches transitions (evidence: `src/s21_fsm.c`)
- Event dispatch → User callbacks: checks guard (evidence: `src/s21_fsm.c`)
- Event dispatch → User callbacks: runs effects (evidence: `README.md`)
- C/C++ or FFI caller → State update: updates state (evidence: `README.md`)
- State update → User callbacks: runs do callback (evidence: `README.md`)
- Event dispatch → User callbacks: runs entry and exit (evidence: `README.md`)
