CUTEHSM – An Efficient Hierarchical State Machine in C

Why Do We Need an Elegant Hierarchical State Machine?

Traditional State Machine Implementation:

// Traditional implementation: 4 function pointers + complex state transition logic
typedef struct {
    void (*entry)();
    void (*exit)();
    void (*work)();
    void (*event)();
    // ... more function pointers
} TraditionalState;

This design not only leads to code redundancy but also has a rigid event handling mechanism that cannot elegantly manage hierarchical state relationships.

Improvements of CUTEHSM

1. Unified Event Handling Model

typedef void (*CuteHandler)(CUTEHSM *hsm, uint32_t event, uint32_t param);

typedef struct CuteState_t {
    const char    *name;
    CuteState       *parent;
    CuteState       *child;
    CuteHandler     handler;  // Unique handler function
} CuteState;
  • Reduces function call overhead
  • Unified event handling process

2. Intelligent State Transition Algorithm

The core of CUTEHSM is the optimized Lowest Common Ancestor (LCA) search algorithm:

static CuteState* Cute_find_lca(CuteState *a, CuteState *b) {
    // Calculate depth difference
    while (depth_a > depth_b) { a = a->parent; depth_a--; }
    while (depth_b > depth_a) { b = b->parent; depth_b--; }

    // Synchronized upward search
    while (a && b && a != b) { a = a->parent; b = b->parent; }
    return a;
}
  • Time complexity: O(h), where h is the height of the state tree
  • No recursive calls, suitable for embedded environments

3. Extreme Memory Efficiency

The memory efficiency of CUTEHSM:

Metric

CUTEHSM

Improvement over traditional implementation

State structure size

24 bytes

40%

Number of function pointers

1

75%

Code size

~80 lines

46%

4. Event Bubbling Handling

5. Transition Between Composite and Simple States

Practical Demonstration: Solar MPPT Charging Management

CUTEHSM - An Efficient Hierarchical State Machine in C

CUTEHSM - An Efficient Hierarchical State Machine in C

// State instance definitions
CuteState state_protect = {    .name = "Protect State",    .parent = NULL,    .child = NULL,    .handler = protect_state_handler,};
CuteState state_test = {    .name = "Test State",    .parent = NULL,    .child = NULL,    .handler = test_state_handler,};
CuteState state_config = {    .name = "Config State",    .parent = NULL,    .child = NULL,    .handler = config_state_handler,};
CuteState state_adjust = {    .name = "Adjust State",    .parent = NULL,    .child = NULL,    .handler = adjust_state_handler,};
CuteState state_float = {    .name = "Float State",    .parent = NULL,    .child = NULL,    .handler = float_state_handler,};
CuteState state_charge = {    .name = "Charge State",    .parent = NULL,    .child = &state_config,    .handler = charge_state_handler,};
CuteState state_idle = {    .name = "Idle State",    .parent = NULL,    .child = NULL,    .handler = idle_state_handler,};
CuteState state_normal = {    .name = "Normal State",    .parent = NULL,    .child = &state_idle,    .handler = normal_state_handler,};

// Set parent-child relationships
void setup_state_hierarchy(){
    state_charge.parent = &state_normal;
    state_idle.parent = &state_normal;
    state_config.parent = &state_charge;
    state_adjust.parent = &state_charge;
    state_float.parent = &state_charge;
    state_test.parent = NULL;    // Top-level state
    state_protect.parent = NULL; // Top-level state
}

Project Address:https://github.com/daxia-hu/cutehsm

Leave a Comment