2026-04-17 19:12:57 +08:00
|
|
|
#ifndef MODULE_LIFECYCLE_H_
|
|
|
|
|
#define MODULE_LIFECYCLE_H_
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
#include <caf/events/module_state_event.h>
|
|
|
|
|
|
|
|
|
|
enum module_lifecycle {
|
|
|
|
|
LC_UNINIT,
|
|
|
|
|
LC_STOPPED,
|
2026-04-21 14:34:08 +08:00
|
|
|
LC_RUNNING,
|
2026-04-17 19:12:57 +08:00
|
|
|
LC_ERROR,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum module_lifecycle_mode {
|
|
|
|
|
ML_MODE_NONE,
|
|
|
|
|
ML_MODE_POWER,
|
|
|
|
|
ML_MODE_SUSPEND,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct module_lifecycle_ops {
|
|
|
|
|
int (*do_init)(void);
|
|
|
|
|
int (*do_start)(void);
|
|
|
|
|
int (*do_stop)(void);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct module_lifecycle_cfg {
|
|
|
|
|
enum module_lifecycle_mode mode;
|
|
|
|
|
enum module_state stopped_state;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct module_lifecycle_ctx {
|
|
|
|
|
enum module_lifecycle state;
|
|
|
|
|
const struct module_lifecycle_cfg *cfg;
|
|
|
|
|
const struct module_lifecycle_ops *ops;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static inline bool module_lifecycle_is_running(
|
|
|
|
|
const struct module_lifecycle_ctx *ctx)
|
|
|
|
|
{
|
|
|
|
|
return ctx->state == LC_RUNNING;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline bool module_lifecycle_is_initialized(
|
|
|
|
|
const struct module_lifecycle_ctx *ctx)
|
|
|
|
|
{
|
|
|
|
|
return (ctx->state != LC_UNINIT) && (ctx->state != LC_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 14:34:08 +08:00
|
|
|
static inline const char *module_lifecycle_name(enum module_lifecycle state)
|
2026-04-18 14:17:23 +08:00
|
|
|
{
|
2026-04-21 14:34:08 +08:00
|
|
|
switch (state) {
|
2026-04-18 14:17:23 +08:00
|
|
|
case LC_UNINIT:
|
2026-04-21 14:34:08 +08:00
|
|
|
return "UNINIT";
|
2026-04-18 14:17:23 +08:00
|
|
|
case LC_STOPPED:
|
2026-04-21 14:34:08 +08:00
|
|
|
return "STOPPED";
|
|
|
|
|
case LC_RUNNING:
|
|
|
|
|
return "RUNNING";
|
2026-04-18 14:17:23 +08:00
|
|
|
case LC_ERROR:
|
2026-04-21 14:34:08 +08:00
|
|
|
return "ERROR";
|
2026-04-18 14:17:23 +08:00
|
|
|
default:
|
2026-04-21 14:34:08 +08:00
|
|
|
return "?";
|
2026-04-18 14:17:23 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int module_lifecycle_validate_ops(
|
|
|
|
|
const struct module_lifecycle_ctx *ctx)
|
|
|
|
|
{
|
|
|
|
|
if ((ctx == NULL) || (ctx->cfg == NULL) || (ctx->ops == NULL) ||
|
|
|
|
|
(ctx->ops->do_init == NULL)) {
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (ctx->cfg->mode) {
|
|
|
|
|
case ML_MODE_NONE:
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
case ML_MODE_POWER:
|
|
|
|
|
case ML_MODE_SUSPEND:
|
|
|
|
|
return (ctx->ops->do_start != NULL) && (ctx->ops->do_stop != NULL) ?
|
|
|
|
|
0 :
|
|
|
|
|
-EINVAL;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2026-04-17 19:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int module_lifecycle_report_state(
|
|
|
|
|
struct module_lifecycle_ctx *ctx,
|
|
|
|
|
enum module_lifecycle state)
|
|
|
|
|
{
|
|
|
|
|
switch (state) {
|
|
|
|
|
case LC_RUNNING:
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
case LC_STOPPED:
|
2026-04-21 14:34:08 +08:00
|
|
|
switch (ctx->cfg->mode) {
|
|
|
|
|
case ML_MODE_POWER:
|
|
|
|
|
module_set_state(ctx->cfg->stopped_state);
|
|
|
|
|
return 0;
|
2026-04-17 19:12:57 +08:00
|
|
|
|
2026-04-21 14:34:08 +08:00
|
|
|
case ML_MODE_SUSPEND:
|
|
|
|
|
module_set_state(MODULE_STATE_SUSPENDED);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
case ML_MODE_NONE:
|
|
|
|
|
return 0;
|
2026-04-17 19:12:57 +08:00
|
|
|
|
2026-04-21 14:34:08 +08:00
|
|
|
default:
|
2026-04-18 14:17:23 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
case LC_ERROR:
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
case LC_UNINIT:
|
|
|
|
|
default:
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int module_lifecycle_fail(struct module_lifecycle_ctx *ctx, int err)
|
|
|
|
|
{
|
|
|
|
|
ctx->state = LC_ERROR;
|
|
|
|
|
(void)module_lifecycle_report_state(ctx, LC_ERROR);
|
|
|
|
|
|
|
|
|
|
return err ? err : -EIO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int module_set_lifecycle(struct module_lifecycle_ctx *ctx,
|
|
|
|
|
enum module_lifecycle target)
|
|
|
|
|
{
|
2026-04-18 14:17:23 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
err = module_lifecycle_validate_ops(ctx);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
2026-04-17 19:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ctx->state == LC_ERROR) {
|
|
|
|
|
return -EFAULT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ctx->state == target) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (target == LC_ERROR) {
|
|
|
|
|
return module_lifecycle_fail(ctx, -EIO);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-21 14:34:08 +08:00
|
|
|
if ((target != LC_STOPPED) && (target != LC_RUNNING)) {
|
2026-04-18 14:17:23 +08:00
|
|
|
return -EPERM;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-17 19:12:57 +08:00
|
|
|
switch (ctx->state) {
|
|
|
|
|
case LC_UNINIT:
|
2026-04-21 14:34:08 +08:00
|
|
|
err = ctx->ops->do_init();
|
|
|
|
|
if (err) {
|
|
|
|
|
return module_lifecycle_fail(ctx, err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx->state = LC_STOPPED;
|
|
|
|
|
err = module_lifecycle_report_state(ctx, LC_STOPPED);
|
2026-04-18 14:17:23 +08:00
|
|
|
if (err) {
|
2026-04-21 14:34:08 +08:00
|
|
|
return module_lifecycle_fail(ctx, err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return module_set_lifecycle(ctx, target);
|
|
|
|
|
|
|
|
|
|
case LC_STOPPED:
|
|
|
|
|
if (target != LC_RUNNING) {
|
|
|
|
|
return -EPERM;
|
2026-04-17 19:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-21 14:34:08 +08:00
|
|
|
err = ctx->ops->do_start ? ctx->ops->do_start() : 0;
|
|
|
|
|
if (err) {
|
|
|
|
|
return module_lifecycle_fail(ctx, err);
|
2026-04-17 19:12:57 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-21 14:34:08 +08:00
|
|
|
ctx->state = LC_RUNNING;
|
|
|
|
|
return module_lifecycle_report_state(ctx, LC_RUNNING);
|
2026-04-17 19:12:57 +08:00
|
|
|
|
|
|
|
|
case LC_RUNNING:
|
2026-04-21 14:34:08 +08:00
|
|
|
if (target != LC_STOPPED) {
|
|
|
|
|
return -EPERM;
|
|
|
|
|
}
|
2026-04-17 19:12:57 +08:00
|
|
|
|
2026-04-21 14:34:08 +08:00
|
|
|
err = ctx->ops->do_stop ? ctx->ops->do_stop() : 0;
|
|
|
|
|
if (err) {
|
|
|
|
|
return module_lifecycle_fail(ctx, err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx->state = LC_STOPPED;
|
|
|
|
|
return module_lifecycle_report_state(ctx, LC_STOPPED);
|
2026-04-17 19:12:57 +08:00
|
|
|
|
|
|
|
|
case LC_ERROR:
|
|
|
|
|
default:
|
2026-04-18 14:17:23 +08:00
|
|
|
return -EPERM;
|
2026-04-17 19:12:57 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* MODULE_LIFECYCLE_H_ */
|