Skip to content

Commit 12fece7

Browse files
committed
modules: implement controller with SMF state machine and ZBUS comm
Add a new controller module that serves as the central coordination unit for the fire detection system. The module implements a hierarchical state machine using Zephyr's SMF framework to manage application logic and coordinate inter-module communication via ZBUS. Key features: - SMF-based state machine (Init/Idle/Active/Error/Recovery states) - Automatic sensor coordination and error recovery - Thread-safe ZBUS communication with configurable parameters - Clean separation of concerns from main.c - Clean-up and alignment of sensor module to controller module This refactoring simplifies main.c to basic initialization while establishing a scalable architecture for future module expansion. Refs: #13 Signed-off-by: Natalia Pluta <pluta.natalia.m@gmail.com>
1 parent 0b30107 commit 12fece7

9 files changed

Lines changed: 611 additions & 110 deletions

File tree

app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ project(app LANGUAGES C)
1212
target_sources(app PRIVATE
1313
src/main.c
1414
src/modules/sensor/sensor_module.c
15+
src/modules/controller/controller_module.c
1516
)
1617

1718
# Add include directories for modules

app/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ menu "Application Configuration"
1515
# Include sensor module configuration
1616
rsource "src/modules/sensor/Kconfig.sensor"
1717

18+
# Include controller module configuration
19+
rsource "src/modules/controller/Kconfig.controller"
20+
1821
endmenu
1922

2023
module = APP

app/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ CONFIG_SMF=y
2222

2323
# Enable sensor module
2424
CONFIG_SENSOR_MODULE=y
25+
26+
# Enable controller module
27+
CONFIG_CONTROLLER_MODULE=y

app/src/main.c

Lines changed: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,19 @@
55

66
#include <zephyr/kernel.h>
77
#include <zephyr/logging/log.h>
8-
#include <zephyr/zbus/zbus.h>
98

109
#include <app/drivers/blink.h>
1110
#include <app_version.h>
1211

13-
#include <modules/sensor/sensor_module.h>
12+
#include <modules/controller/controller_module.h>
1413

1514
LOG_MODULE_REGISTER(main, CONFIG_APP_LOG_LEVEL);
1615

17-
#define BLINK_PERIOD_MS_STEP 100U
18-
#define BLINK_PERIOD_MS_MAX 1000U
19-
20-
#define SENSORS_WARMUP_DELAY_MS 5000U
21-
#define SENSOR_READ_INTERVAL_MS 1000U
22-
23-
/* ZBUS observer for sensor messages */
24-
static void sensor_response_callback(const struct zbus_channel *chan);
25-
ZBUS_LISTENER_DEFINE(sensor_response_listener, sensor_response_callback);
16+
/* Sleep duration for main loop */
17+
#define MAIN_LOOP_SLEEP_SECONDS 100U
2618

2719
/* Function prototypes */
2820
static int init_blink_led(const struct device **blink);
29-
static void log_sensor_data(const struct sensor_msg *msg);
3021

3122
static int init_blink_led(const struct device **blink)
3223
{
@@ -46,69 +37,38 @@ static int init_blink_led(const struct device **blink)
4637
return 0;
4738
}
4839

49-
static void log_sensor_data(const struct sensor_msg *msg)
50-
{
51-
/* Log BME280 data */
52-
LOG_INF("BME280: Temp: %d.%06d C, Press: %d.%06d kPa, Hum: %d.%06d %%",
53-
msg->temperature.val1, msg->temperature.val2, msg->pressure.val1,
54-
msg->pressure.val2, msg->humidity.val1, msg->humidity.val2);
55-
56-
/* Log CCS811 data */
57-
LOG_INF("CCS811: CO2: %d ppm, VOC: %d ppb", msg->co2.val1, msg->voc.val1);
58-
59-
/* Log HM3301 data */
60-
LOG_INF("HM3301: PM1.0: %d ug/m3, PM2.5: %d ug/m3, PM10: %d ug/m3", msg->pm1_0.val1,
61-
msg->pm2_5.val1, msg->pm10.val1);
62-
63-
LOG_INF("Sensor data timestamp: %lld ms", msg->timestamp);
64-
}
65-
66-
static void sensor_response_callback(const struct zbus_channel *chan)
67-
{
68-
const struct sensor_msg *msg;
69-
70-
if (chan == &sensor_chan) {
71-
msg = &MSG_TO_SENSOR_MSG(zbus_chan_const_msg(chan));
72-
if (msg != NULL && msg->type == SENSOR_SAMPLE_RESPONSE) {
73-
LOG_DBG("Received sensor response");
74-
log_sensor_data(msg);
75-
}
76-
}
77-
}
78-
7940
int main(void)
8041
{
8142
int ret;
8243
const struct device *blink;
8344

8445
LOG_INF("Zephyr Fire Detection System %s", APP_VERSION_STRING);
8546

86-
/* Wait for sensors to stabilize */
87-
k_sleep(K_MSEC(SENSORS_WARMUP_DELAY_MS));
88-
8947
/* Initialize blink LED */
9048
ret = init_blink_led(&blink);
9149
if (ret < 0) {
9250
LOG_ERR("Blink LED initialization failed");
9351
return 0;
9452
}
9553

96-
/* Initialize sensor module */
97-
ret = sensor_module_init();
54+
/* Initialize controller module */
55+
ret = controller_module_init();
9856
if (ret < 0) {
99-
LOG_ERR("Sensor module initialization failed");
57+
LOG_ERR("Controller module initialization failed");
10058
return 0;
10159
}
10260

61+
/* Start data sampling */
62+
ret = controller_module_start_sampling();
63+
if (ret < 0) {
64+
LOG_ERR("Failed to start sampling");
65+
return 0;
66+
}
67+
68+
LOG_INF("System initialized successfully");
69+
10370
while (1) {
104-
/* Request sensor data */
105-
LOG_DBG("Requesting sensor data");
106-
ret = sensor_module_request_data();
107-
if (ret < 0) {
108-
LOG_ERR("Failed to request sensor data (%d)", ret);
109-
}
110-
111-
k_sleep(K_MSEC(SENSOR_READ_INTERVAL_MS));
71+
k_sleep(K_SECONDS(MAIN_LOOP_SLEEP_SECONDS));
11272
}
11373

11474
return 0;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
config CONTROLLER_MODULE
4+
bool "Controller Module"
5+
default y
6+
depends on ZBUS && SMF
7+
help
8+
Enable the controller module that coordinates all other modules
9+
and handles application business logic.
10+
11+
if CONTROLLER_MODULE
12+
13+
config CONTROLLER_MODULE_STACK_SIZE
14+
int "Controller thread stack size"
15+
default 2048
16+
help
17+
Stack size for the controller thread.
18+
19+
config CONTROLLER_MODULE_THREAD_PRIORITY
20+
int "Controller thread priority"
21+
default 4
22+
help
23+
Priority for the controller thread.
24+
25+
config CONTROLLER_MODULE_LOG_LEVEL
26+
int "Controller module log level"
27+
default 3
28+
help
29+
Log level for the controller module (0-4, where 0 is none and 4 is debug).
30+
31+
config CONTROLLER_MODULE_SAMPLE_INTERVAL_MS
32+
int "Default sensor sampling interval (ms)"
33+
default 1000
34+
help
35+
Default interval between sensor data requests.
36+
37+
config CONTROLLER_MODULE_THREAD_SLEEP_MS
38+
int "Controller thread sleep interval (ms)"
39+
default 100
40+
help
41+
Sleep interval for the controller thread main loop.
42+
43+
config CONTROLLER_MODULE_ZBUS_SUBSCRIBER_QUEUE_SIZE
44+
int "ZBUS subscriber queue size"
45+
default 4
46+
help
47+
Queue size for the ZBUS controller subscriber.
48+
49+
config CONTROLLER_MODULE_MAX_RETRIES
50+
int "Maximum error retries"
51+
default 3
52+
help
53+
Maximum number of consecutive errors before entering error state.
54+
55+
config CONTROLLER_MODULE_RECOVERY_DELAY_MS
56+
int "Recovery attempt delay (ms)"
57+
default 2000
58+
help
59+
Delay before attempting recovery from error state.
60+
61+
config CONTROLLER_MODULE_MAX_RECOVERY_ATTEMPTS
62+
int "Maximum recovery attempts"
63+
default 5
64+
help
65+
Maximum number of recovery attempts before giving up.
66+
67+
config CONTROLLER_MODULE_RECOVERY_RETRY_DELAY_MS
68+
int "Delay between recovery retry attempts (ms)"
69+
default 1000
70+
help
71+
Delay between recovery retry attempts when recovery fails.
72+
73+
config CONTROLLER_MODULE_AUTO_START_SAMPLING
74+
bool "Auto-start sensor sampling"
75+
default y
76+
help
77+
Automatically start sensor data sampling when controller enters idle state.
78+
79+
endif # CONTROLLER_MODULE

0 commit comments

Comments
 (0)