Safety#

Functions

int safety_init(void)#

Initializes the safety module.

This function sets up the necessary configurations and resources required for the safety module to operate. It should be called during the system initialization phase.

Returns:

int Returns 0 on success, or a negative error code on failure.

void safety_deinit(void)#

Deinitializes the safety module.

This function releases all resources allocated by the safety module and performs necessary cleanup. It should be called during system shutdown or when the safety module is no longer needed.

int safety_add(struct safety_entry *entry)#

Add an entry to the safety module.

Registers a new entry to be included in future safety checks.

Parameters:

entry[in] Entry to be added.

Returns:

0 on success, or a negative error code on failure. -EINVAL: null pointer.

-EALREADY: entry already added.

-ENOMEM / -ENOSPC: capacity exceeded.

int safety_add_and_enable(struct safety_entry *entry)#

Add and enable an entry in one operation.

Registers an entry to the safety module and immediately enables it.

Parameters:

entry[in] Entry to add and enable.

Returns:

0 on success, or negative error code on failure. -EALREADY: already added. -ENOMEM / -ENOSPC: out of memory. -EIO: enable() failed after add, entry will be removed again.

int safety_remove(struct safety_entry *entry)#

Remove an entry from the safety module.

Deregisters the given entry, if present.

Parameters:

entry[in] Entry to remove.

Returns:

0 on success, or a negative error code on failure. -ENOENT: entry not found.

int safety_check(safety_error_callback_t cb, void *cb_ctx)#

Perform safety checks on all registered entries.

Iterates through all registered entries and invokes their check() method. For each entry that returns a non-OK status, the callback is invoked.

Parameters:
  • cb[in] Callback for reporting failed entries (nullable).

  • cb_ctx[in] User-defined context pointer for callback.

Returns:

0 if all entries returned OK status (i.e. system is safe).

Otherwise, returns the number of failed entries (> 0).

Returns a negative error code on failure.

int safety_iterate(safety_iterator_t cb, void *cb_ctx)#

Iterate over all registered entries.

Invokes the given callback once per registered entry.

Parameters:
  • cb[in] Callback to invoke for each entry.

  • cb_ctx[in] User-defined context pointer for callback.

Returns:

0 on success, or a negative error code on failure.

int safety_register_event_cb(safety_error_callback_t cb, void *cb_ctx)#

Registers a callback for safety events.

This function allows the user to register a callback that will be invoked when a safety event occurs. The callback can be used to handle errors or perform specific actions based on the event.

Parameters:
  • cb[in] The callback function to register.

  • cb_ctx[in] A user-defined context to pass to the callback.

Returns:

int Returns 0 on success, or a negative error code on failure.

bool safety_is_error(const char *name)#

Checks if a safety error exists for the given name.

This function determines whether a safety error is associated with the specified name. It is useful for querying the status of specific safety conditions.

Parameters:

name[in] The name of the safety condition to check.

Returns:

true if an error exists for the given name, false otherwise.

Typedefs

typedef void (*safety_error_callback_t)(struct safety_entry *entry, safety_entry_status_t status, void *ctx)#

Error callback type for safety checks.

Called when an entry returns a non-OK status during safety_check().

Param entry:

[in] The entry that failed the check.

Param status:

[in] The status returned by the entry.

Param ctx:

[in] User-defined context pointer.

typedef void (*safety_iterator_t)(struct safety_entry *entry, void *ctx)#

Iterator callback type for iterating all entries.

Called once per registered entry in safety_iterate().

Param entry:

[in] The current entry.

Param ctx:

[in] User-defined context pointer.