Skip to content

Multiple FSM's in same program with colliding state names have unexpected side effects #41

Description

@taliesin

Most probably I'm missing something trivial here, but I'm unable to understand what is happening in the following case:

  1. I have 2 files 'wifi_sta_manager.cpp' and 'wifi_ap_manager.cpp'
  2. Both hold a 'local' tinyFSM state machine, one is WiFiStationFSM, the other one is WiFiSoftAPFSM
  3. Some of the states of these FSMs have the same names: Idle, Started ...
  4. Both are initialized using FSM_INITIAL_STATE(name of FSM, Idle)

This is the station FSM

// states forward declaration
struct Idle;
struct Started;
struct Connected;
struct BackOff;

// State Machine Base Class
struct WiFiStationFSM : tinyfsm::Fsm<WiFiStationFSM>
{
    virtual void react(const tinyfsm::Event) {}

    virtual void react(const WiFiStarted &) {}
    virtual void react(const WiFiConnected &) {}
    virtual void react(const WiFiDisconnected &) {}
    // default behaviour is to stop wifi on transition to Idle
    virtual void react(const SetttingsChanged &) { transit<Idle>(station_stop); }
    virtual void react(const OneSecond &) {}

    virtual void entry() {}
    virtual void exit() {}
};
// ... state implementation to follow

// Initial state definition
FSM_INITIAL_STATE(WiFiStationFSM, Idle)

// and somewhere
WiFiStationFSM::start();

And again for the AP FSM:

// states forward declaration
struct Idle;
struct Started;

// State Machine Base Class
struct WiFiSoftAPFSM : tinyfsm::Fsm<WiFiSoftAPFSM>
{
    virtual void react(const tinyfsm::Event) {}

    virtual void react(const WiFiStarted &) {}
    // default behaviour is to stop wifi on transition to Idle
    virtual void react(const SetttingsChanged &) { transit<Idle>(softap_stop); }

    virtual void entry() {}
    virtual void exit() {}
};

// Initial state definition
FSM_INITIAL_STATE(WiFiSoftAPFSM, Idle)

// and somewhere
WiFiSoftAPFSM::start();

The trouble is, that the AP FSM start does not enter the Idle state. If I rename the states to APIdle and APStarted it works.
None of the state structs are defined in a header file, but solely in 'their' cpp files.

What am I missing?
BTW: This is an ESP32 project (IDF 5.2.1), riscv32-esp-elf-gcc (crosstool-NG esp-13.2.0_20230928) 13.2.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions