Modules

Overview

Gantry applications are composed of modules. Modules process events and communicate with each other via the Stow.

Modules are enabled and configured by Kconfig options. When enabled they run automatically on boot after the Stow has been initialized. This makes it trivial to include or exclude modules without extra boilerplate.

        %%{init: {"theme": "neutral"}}%%
graph LR
    Stow[("Stow")]
    A["Module A"]
    B["Module B"]

    A -->|set| Stow
    B -->|get| Stow
    

Registering a Module

Use GANTRY_MODULE_DEFINE to register a module with its own dedicated thread, or GANTRY_LIBRARY_MODULE_DEFINE for a module with init logic but no thread (e.g. one that only provides an API or configures another subsystem).

static void my_module_init(void);
static void my_module_thread(void* arg1, void* arg2, void* arg3);

GANTRY_MODULE_DEFINE(my_module, my_module_init, my_module_thread, CONFIG_MY_MODULE_STACK_SIZE, CONFIG_MY_MODULE_THREAD_PRIORITY);

// A module with no thread of its own
GANTRY_LIBRARY_MODULE_DEFINE(my_library, my_library_init);

Every registered module’s init runs once at boot, after the Stow has been initialized. If the module has a thread, that thread is created immediately after init returns.

        %%{init: {"theme": "neutral"}}%%
graph TD
    B{"Boot"}
    K["Zephyr Kernel"]
    S["Stow"]
    M["Gantry Modules"]
    A("App's main function")

    B --> K
    K --> S
    S --> M
    M --> A
    

Important

There is no ordering guarantee for module initialization; a module must not depend on another module having already started. Communicating through the Stow largely avoids cross-module dependencies. If dependencies are needed, consider adding SYS_INIT priorities or initializing the modules manually.

Philosophy

Ideally, modules should only block in one place, their thread’s event queue. This keeps a module’s control flow easy to reason about. The samples contain examples of module structures. Note, a timeout can be added to the event queue to service a periodic event or state machine.

Modules communicate with each other through the Stow. Ideally, modules should not call directly into other modules unless they must be tightly coupled. Instead, a module that cares about another module’s state subscribes to the relevant Stow item; a module that changes state sets it. This avoids the alternative where modules accumulate direct dependencies on each other.

Stow Subscriptions

To receive updates from the Stow modules can use STOW_SUBSCRIPTION_DEFINE to define static subscriptions that survive for the lifetime of the app. Alternatively, stow_subscribe() can be used for dynamic subscriptions at runtime.

Subscriptions require a callback to handle received update events. STOW_CALLBACK_DEFINE can be used to define a simple callback that simply passes the event along to the module’s event queue (if room is available). This is the recommended way to handle update events.

API Reference

typedef void (*gantry_module_init_fn)(void)

Module init callback.

Called once at boot, after Stow is initialized and before the module’s thread (if any) is created.

GANTRY_MODULE_DEFINE(_name, _init, _thread_entry, _stack_size, _priority)

Register an application module.

Declares and application module with its own thread. Modules are started automatically at boot: _init is called first, then a thread running _thread_entry is created.

Note

Modules run after the Stow is initialized. The initialization order between application modules in not guaranteed.

Parameters:
  • _name – Module name

  • _init – Init function, called before the thread is created

  • _thread_entry – Thread entry point

  • _stack_size – Thread stack size in bytes

  • _priority – Thread priority

GANTRY_LIBRARY_MODULE_DEFINE(_name, _init)

Register a library module.

Declares a library module, a module without a dedicated thread.

Note

Modules run after the Stow is initialized. The initialization order between application modules in not guaranteed.

Parameters:
  • _name – Module name

  • _init – Init function

STOW_CALLBACK_DEFINE(_name, _queue)

Define a Stow subscription callback.

Creates a Stow subscription callback that funnels item updates into the provided queue.

Parameters:
  • _name – Name of the generated callback

  • _queue – The queue to pass updates to

struct gantry_module
#include <module.h>

An application module registration.

Created by GANTRY_MODULE_DEFINE or GANTRY_LIBRARY_MODULE_DEFINE.