-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller_module.c
More file actions
355 lines (286 loc) · 11 KB
/
controller_module.c
File metadata and controls
355 lines (286 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
* Copyright (c) 2025 Natalia Pluta
* SPDX-License-Identifier: Apache-2.0
*/
#include "controller_module.h"
#include <zephyr/logging/log.h>
#include <zephyr/smf.h>
#include <modules/sensor/sensor_module.h>
LOG_MODULE_REGISTER(controller_module, CONFIG_CONTROLLER_MODULE_LOG_LEVEL);
/* ZBUS subscriber for sensor responses */
ZBUS_SUBSCRIBER_DEFINE(controller_sensor_subscriber,
CONFIG_CONTROLLER_MODULE_ZBUS_SUBSCRIBER_QUEUE_SIZE);
/* Controller state machine object initialization macro */
#define CONTROLLER_STATE_OBJECT_INIT() \
(struct controller_state_object) \
{ \
.sample_interval_ms = CONFIG_CONTROLLER_MODULE_SAMPLE_INTERVAL_MS, \
.max_retries = CONFIG_CONTROLLER_MODULE_MAX_RETRIES, .sampling_active = false, \
.sensor_module_ready = false, .ctx = {0}, .error_count = 0, .last_sample_time = 0, \
.current_state = CONTROLLER_MODULE_STATE_INIT, \
}
/* Controller state machine object */
struct controller_state_object {
struct smf_ctx ctx;
/* Current state tracking */
enum controller_module_state current_state;
/* Sampling control */
bool sampling_active;
int64_t last_sample_time;
int64_t sample_interval_ms;
/* Error handling */
int error_count;
int max_retries;
/* Module status tracking */
bool sensor_module_ready;
};
/* Forward declarations for state functions */
static void controller_state_init_run(void *obj);
static void controller_state_idle_run(void *obj);
static void controller_state_active_run(void *obj);
static void controller_state_error_run(void *obj);
static void controller_state_recovery_run(void *obj);
/* State machine table */
static const struct smf_state controller_states[] = {
[CONTROLLER_MODULE_STATE_INIT] =
SMF_CREATE_STATE(NULL, controller_state_init_run, NULL, NULL, NULL),
[CONTROLLER_MODULE_STATE_IDLE] =
SMF_CREATE_STATE(NULL, controller_state_idle_run, NULL, NULL, NULL),
[CONTROLLER_MODULE_STATE_ACTIVE] =
SMF_CREATE_STATE(NULL, controller_state_active_run, NULL, NULL, NULL),
[CONTROLLER_MODULE_STATE_ERROR] =
SMF_CREATE_STATE(NULL, controller_state_error_run, NULL, NULL, NULL),
[CONTROLLER_MODULE_STATE_RECOVERY] =
SMF_CREATE_STATE(NULL, controller_state_recovery_run, NULL, NULL, NULL)};
/* Global controller state machine object */
static struct controller_state_object controller_state_obj;
/* Thread synchronization */
static K_MUTEX_DEFINE(controller_sm_mutex);
/* Thread stack and data */
static K_THREAD_STACK_DEFINE(controller_thread_stack, CONFIG_CONTROLLER_MODULE_STACK_SIZE);
static struct k_thread controller_thread_data;
/* Forward declarations */
static void controller_thread(void *p1, void *p2, void *p3);
static void sensor_response_callback(const struct zbus_channel *chan);
static void log_sensor_data(const struct sensor_msg *msg);
static void controller_set_state(struct controller_state_object *ctx,
enum controller_module_state new_state);
/* ZBUS listener for sensor responses */
ZBUS_LISTENER_DEFINE(controller_sensor_listener, sensor_response_callback);
int controller_module_init(void)
{
int ret;
LOG_INF("Initializing controller module");
/* Initialize state machine object */
controller_state_obj = CONTROLLER_STATE_OBJECT_INIT();
/* Initialize state machine */
smf_set_initial(SMF_CTX(&controller_state_obj),
&controller_states[CONTROLLER_MODULE_STATE_INIT]);
controller_state_obj.current_state = CONTROLLER_MODULE_STATE_INIT;
LOG_INF("Controller state machine initialized");
/* Run initial state machine setup */
k_mutex_lock(&controller_sm_mutex, K_FOREVER);
ret = smf_run_state(SMF_CTX(&controller_state_obj));
k_mutex_unlock(&controller_sm_mutex);
if (ret < 0) {
LOG_ERR("Failed to run controller state machine (%d)", ret);
return ret;
}
/* Create controller thread */
k_thread_create(&controller_thread_data, controller_thread_stack,
K_THREAD_STACK_SIZEOF(controller_thread_stack), controller_thread, NULL,
NULL, NULL, CONFIG_CONTROLLER_MODULE_THREAD_PRIORITY, 0, K_NO_WAIT);
k_thread_name_set(&controller_thread_data, "controller_module");
LOG_INF("Controller module initialized successfully");
return 0;
}
int controller_module_start_sampling(void)
{
k_mutex_lock(&controller_sm_mutex, K_FOREVER);
if (controller_state_obj.current_state == CONTROLLER_MODULE_STATE_IDLE) {
controller_state_obj.sampling_active = true;
controller_state_obj.last_sample_time = k_uptime_get();
controller_set_state(&controller_state_obj, CONTROLLER_MODULE_STATE_ACTIVE);
LOG_INF("Controller: Starting data sampling");
} else {
LOG_WRN("Controller: Cannot start sampling from current state");
}
k_mutex_unlock(&controller_sm_mutex);
return 0;
}
int controller_module_stop_sampling(void)
{
k_mutex_lock(&controller_sm_mutex, K_FOREVER);
controller_state_obj.sampling_active = false;
if (controller_state_obj.current_state == CONTROLLER_MODULE_STATE_ACTIVE) {
controller_set_state(&controller_state_obj, CONTROLLER_MODULE_STATE_IDLE);
LOG_INF("Controller: Stopping data sampling");
}
k_mutex_unlock(&controller_sm_mutex);
return 0;
}
enum controller_module_state controller_module_get_state(void)
{
k_mutex_lock(&controller_sm_mutex, K_FOREVER);
enum controller_module_state current_state = controller_state_obj.current_state;
k_mutex_unlock(&controller_sm_mutex);
return current_state;
}
static void controller_thread(void *p1, void *p2, void *p3)
{
ARG_UNUSED(p1);
ARG_UNUSED(p2);
ARG_UNUSED(p3);
const struct zbus_channel *chan;
LOG_INF("Controller thread started");
while (1) {
/* Check for ZBUS messages with timeout */
int ret = zbus_sub_wait(&controller_sensor_subscriber, &chan,
K_MSEC(CONFIG_CONTROLLER_MODULE_THREAD_SLEEP_MS));
if (ret == 0) {
/* Process received message if it's from sensor channel */
if (chan == &sensor_chan) {
LOG_DBG("Controller: Received sensor data via ZBUS");
/* Message processing is handled by the listener callback */
}
}
/* Run state machine periodically */
k_mutex_lock(&controller_sm_mutex, K_FOREVER);
smf_run_state(SMF_CTX(&controller_state_obj));
k_mutex_unlock(&controller_sm_mutex);
/* Small sleep to prevent tight loop */
k_sleep(K_MSEC(CONFIG_CONTROLLER_MODULE_THREAD_SLEEP_MS));
}
}
/* State machine implementation */
static void controller_state_init_run(void *obj)
{
int ret;
struct controller_state_object *ctx = (struct controller_state_object *)obj;
LOG_INF("Controller SM: Initializing");
/* Initialize sensor module */
ret = sensor_module_init();
if (ret < 0) {
LOG_ERR("Controller SM: Sensor module initialization failed (%d)", ret);
ctx->error_count++;
controller_set_state(ctx, CONTROLLER_MODULE_STATE_ERROR);
return;
}
ctx->sensor_module_ready = true;
ctx->error_count = 0;
LOG_INF("Controller SM: Initialization complete");
controller_set_state(ctx, CONTROLLER_MODULE_STATE_IDLE);
}
static void controller_state_idle_run(void *obj)
{
struct controller_state_object *ctx = (struct controller_state_object *)obj;
LOG_DBG("Controller SM: Idle state");
/* Check if sampling should be started automatically */
if (CONFIG_CONTROLLER_MODULE_AUTO_START_SAMPLING && !ctx->sampling_active) {
LOG_INF("Controller SM: Auto-starting sampling");
ctx->sampling_active = true;
ctx->last_sample_time = k_uptime_get();
controller_set_state(ctx, CONTROLLER_MODULE_STATE_ACTIVE);
}
}
static void controller_state_active_run(void *obj)
{
int ret;
struct controller_state_object *ctx = (struct controller_state_object *)obj;
LOG_DBG("Controller SM: Active state");
/* Check if sampling should be stopped */
if (!ctx->sampling_active) {
controller_set_state(ctx, CONTROLLER_MODULE_STATE_IDLE);
return;
}
/* Check if it's time for next sample */
int64_t now = k_uptime_get();
if (now - ctx->last_sample_time >= ctx->sample_interval_ms) {
LOG_DBG("Controller SM: Requesting sensor data");
ret = sensor_module_request_data();
if (ret < 0) {
LOG_ERR("Controller SM: Failed to request sensor data (%d)", ret);
ctx->error_count++;
if (ctx->error_count > ctx->max_retries) {
controller_set_state(ctx, CONTROLLER_MODULE_STATE_ERROR);
return;
}
} else {
ctx->last_sample_time = now;
ctx->error_count = 0; /* Reset error count on successful request */
}
}
}
static void controller_state_error_run(void *obj)
{
struct controller_state_object *ctx = (struct controller_state_object *)obj;
LOG_ERR("Controller SM: Error state");
/* Wait before attempting recovery */
k_sleep(K_MSEC(CONFIG_CONTROLLER_MODULE_RECOVERY_DELAY_MS));
controller_set_state(ctx, CONTROLLER_MODULE_STATE_RECOVERY);
}
static void controller_state_recovery_run(void *obj)
{
struct controller_state_object *ctx = (struct controller_state_object *)obj;
LOG_INF("Controller SM: Attempting recovery");
/* Reset error count and try to recover */
ctx->error_count = 0;
ctx->sampling_active = false;
ctx->sensor_module_ready = false;
/* Re-initialize sensor module */
int ret = sensor_module_init();
if (ret < 0) {
LOG_ERR("Controller SM: Recovery failed (%d)", ret);
ctx->error_count++;
if (ctx->error_count > CONFIG_CONTROLLER_MODULE_MAX_RECOVERY_ATTEMPTS) {
LOG_ERR("Controller SM: Recovery attempts exhausted");
controller_set_state(ctx, CONTROLLER_MODULE_STATE_ERROR);
} else {
/* Try recovery again */
k_sleep(K_MSEC(CONFIG_CONTROLLER_MODULE_RECOVERY_RETRY_DELAY_MS));
}
} else {
LOG_INF("Controller SM: Recovery successful");
ctx->sensor_module_ready = true;
controller_set_state(ctx, CONTROLLER_MODULE_STATE_IDLE);
}
}
static void sensor_response_callback(const struct zbus_channel *chan)
{
const struct sensor_msg *msg;
const void *chan_msg;
if (chan == &sensor_chan) {
chan_msg = zbus_chan_const_msg(chan);
if (chan_msg != NULL) {
msg = &MSG_TO_SENSOR_MSG(chan_msg);
if (msg->type == SENSOR_SAMPLE_RESPONSE) {
LOG_DBG("Controller: Processing sensor response");
log_sensor_data(msg);
/* Reset error count on successful data reception */
k_mutex_lock(&controller_sm_mutex, K_FOREVER);
controller_state_obj.error_count = 0;
k_mutex_unlock(&controller_sm_mutex);
}
}
}
}
static void log_sensor_data(const struct sensor_msg *msg)
{
/* Log BME280 data */
LOG_INF("BME280: Temp: %d.%06d C, Press: %d.%06d kPa, Hum: %d.%06d %%",
msg->temperature.val1, msg->temperature.val2, msg->pressure.val1,
msg->pressure.val2, msg->humidity.val1, msg->humidity.val2);
/* Log CCS811 data */
LOG_INF("CCS811: CO2: %d ppm, VOC: %d ppb", msg->co2.val1, msg->voc.val1);
/* Log HM3301 data */
LOG_INF("HM3301: PM1.0: %d ug/m3, PM2.5: %d ug/m3, PM10: %d ug/m3", msg->pm1_0.val1,
msg->pm2_5.val1, msg->pm10.val1);
LOG_INF("Sensor data timestamp: %lld ms", msg->timestamp);
}
/* Helper function to set state and keep current_state field in sync */
static void controller_set_state(struct controller_state_object *ctx,
enum controller_module_state new_state)
{
ctx->current_state = new_state;
smf_set_state(SMF_CTX(ctx), &controller_states[new_state]);
}