kernel-services
npx machina-cli add skill beriberikix/zephyr-agent-skills/kernel-services --openclawZephyr Kernel Services
Move beyond basic threading and logging to build modular, event-driven, and robust Zephyr applications.
Core Workflows
1. Event-Driven Communication (Zbus)
Decouple your modules using a lightweight publish-and-subscribe bus.
- Reference: zbus.md
- Key Tools:
ZBUS_CHAN_DEFINE,ZBUS_SUBSCRIBER_DEFINE,zbus_chan_pub.
2. Behavioral Logic (SMF)
Manage complex system states and transitions using the State Machine Framework.
- Reference: smf.md
- Key Tools:
smf_set_state,SMF_CREATE_STATE, Hierarchical states.
3. Background Processing (Work Queues)
Defer long-running or non-critical tasks to prevent blocking interrupts or high-priority threads.
- Reference: settings_workqueue.md
- Key Tools:
k_work_submit,k_work_delayable, Custom work queues.
4. Persistence (Settings)
Save and restore configuration data and state across reboots.
- Reference: settings_workqueue.md
- Key Tools:
settings_load,settings_save_one, NVS backends.
Quick Start (Zbus)
// Define a channel for sensor data
ZBUS_CHAN_DEFINE(sensor_data_chan, struct sensor_msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY, ZBUS_CHAN_DEFAULTS);
// Publish from a thread
zbus_chan_pub(&sensor_data_chan, &msg, K_NO_WAIT);
Professional Patterns (Asset Tracker Style)
- Modularity: Use Zbus as the backbone for inter-module communication.
- Predictability: Use SMF to define clear lifecycle states for each module (e.g., Uninitialized -> Ready -> Active -> Error).
- Responsiveness: Use custom work queues for sensor data ingestion to keep the main thread responsive for cloud communication.
- Sensor Integration: For sensor data ingestion patterns, see the hardware-io skill.
Resources
- References:
zbus.md: Publish/Subscribe patterns and subscriber types.smf.md: Finite and Hierarchical state machine implementation.settings_workqueue.md: Background work and persistent storage.
Source
git clone https://github.com/beriberikix/zephyr-agent-skills/blob/main/skills/kernel-services/SKILL.mdView on GitHub Overview
kernel-services provides advanced Zephyr kernel features for modular, event-driven applications. It covers inter-thread communication with Zbus pub/sub, behavioral control via the State Machine Framework (SMF), background tasks with work queues, and persistent configuration through the Settings subsystem. This combo enables complex state-driven logic and data persistence across reboots.
How This Skill Works
Zbus enables decoupled inter-thread messaging through channels that modules publish to or subscribe from. SMF defines states and transitions to model module behavior, while work queues push long-running tasks off the main path. Settings stores and recovers configuration data, ensuring persistence across restarts.
When to Use It
- You are building a modular Zephyr app with clear module boundaries requiring decoupled communication.
- You need robust behavioral logic with defined states and transitions using SMF.
- You want to offload long-running tasks from interrupts or high-priority threads via work queues.
- You require persistent configuration and state across reboots using Settings.
- You are designing sensor pipelines or assets that must be integrated via event-driven messaging.
Quick Start
- Step 1: Define a channel for sensor data using ZBUS_CHAN_DEFINE(sensor_data_chan, struct sensor_msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY, ZBUS_CHAN_DEFAULTS);
- Step 2: Publish from a thread with zbus_chan_pub(&sensor_data_chan, &msg, K_NO_WAIT);
- Step 3: Implement a subscriber to receive and handle messages, e.g., using ZBUS_SUBSCRIBER_DEFINE and a callback to process incoming sensor_msg data.
Best Practices
- Define explicit Zbus channels and keep publishers and subscribers interoperable across modules.
- Model module lifecycles with SMF using clear states and transitions; reuse hierarchical states for complexity.
- Use k_work and delayable work to balance latency and throughput; avoid heavy work in ISRs.
- Choose appropriate Settings backends (for example, NVS) and keep keys namespaced by module.
- Document interfaces and message schemas to reduce coupling and facilitate testing.
Example Use Cases
- Asset tracker: modular sensor modules communicate via Zbus and manage lifecycle with SMF.
- Sensor data ingestion pipeline using work queues to keep cloud updates responsive.
- Device with persistent settings across reboots using Settings and NVS.
- Stateful motor controller with SMF and event-driven triggers.
- Modular logging and telemetry subsystem integrated through Zbus channels.