OBSERVER Embedded C library v1.0.0
Functions
observer.c File Reference

Deterministic Observer pattern implementation for safety-critical systems. More...

#include "observer.h"
#include "observer_public_types.h"
#include <stddef.h>
#include <stdint.h>
Include dependency graph for observer.c:

Functions

subscr_status_e subscribe (observer_cb_t *subscription_table, observer_cb_t cb_2_register, uint8_t subscription_table_size)
 Register a callback in the subscription table (void callbacks). More...
 
subscr_status_e unsubscribe (observer_cb_t *subscription_table, observer_cb_t cb_2_remove, uint8_t subscription_table_size)
 Unregister a callback from the subscription table (void callbacks). More...
 
subscr_status_e notify (observer_cb_t *subscription_table, uint8_t subscription_table_size)
 Notify all registered callbacks (no argument). More...
 
subscr_status_e subscribe_state_change (observer_cb_state_t *subscription_table, observer_cb_state_t cb_2_register, uint8_t subscription_table_size)
 Register a callback with event_state_e argument. More...
 
subscr_status_e unsubscribe_state_change (observer_cb_state_t *subscription_table, observer_cb_state_t cb_2_remove, uint8_t subscription_table_size)
 Unregister a callback with event_state_e argument. More...
 
subscr_status_e notify_state_change (observer_cb_state_t *subscription_table, uint8_t subscription_table_size, event_state_e state)
 Notify all registered callbacks with event_state_e argument. More...
 
subscr_status_e subscribe_u8 (observer_cb_u8_arg_t *subscription_table, observer_cb_u8_arg_t cb_2_register, uint8_t subscription_table_size)
 Register a callback with uint8_t argument. More...
 
subscr_status_e unsubscribe_u8 (observer_cb_u8_arg_t *subscription_table, observer_cb_u8_arg_t cb_2_remove, uint8_t subscription_table_size)
 Unregister a callback with uint8_t argument. More...
 
subscr_status_e notify_u8 (observer_cb_u8_arg_t *subscription_table, uint8_t subscription_table_size, uint8_t data)
 Notify all registered callbacks with uint8_t argument. More...
 

Detailed Description

Deterministic Observer pattern implementation for safety-critical systems.

Implements a static, deterministic variant of the Observer pattern suitable for safety-critical and resource-constrained embedded environments.

All API functions:

@thread_safety None of the API functions are reentrant or thread-safe. Caller must ensure synchronization if used concurrently.

Version
1.0.0
Date
2025-10-25

@autho niwciu (niwci.nosp@m.u@gm.nosp@m.ail.c.nosp@m.om)

@safety Designed to meet MISRA C 2012 and ISO 26262 Part 6 software safety goals.

Function Documentation

◆ notify()

subscr_status_e notify ( observer_cb_t subscription_table,
uint8_t  subscription_table_size 
)

Notify all registered callbacks (no argument).

Invokes all non-NULL callbacks in ascending index order.

Parameters
[in]subscription_tablePointer to an array of observer_cb_t.
[in]subscription_table_sizeTable length.
Returns
OBSERVER_OK At least one callback invoked.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
OBSERVER_TABLE_EMPTY_ERROR No callbacks registered.
Precondition
Valid table pointer of given size.
Postcondition
All active callbacks are executed once. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-003

◆ notify_state_change()

subscr_status_e notify_state_change ( observer_cb_state_t subscription_table,
uint8_t  subscription_table_size,
event_state_e  state 
)

Notify all registered callbacks with event_state_e argument.

Calls all valid (non-NULL) callbacks, passing the given event state.

Parameters
[in]subscription_tablePointer to array of observer_cb_state_t.
[in]subscription_table_sizeTable length.
[in]stateEvent state passed to callbacks.
Returns
OBSERVER_OK At least one callback invoked.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
OBSERVER_TABLE_EMPTY_ERROR No callbacks registered.
Precondition
Valid table pointer of given size.
Postcondition
All registered callbacks called with state. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-006

◆ notify_u8()

subscr_status_e notify_u8 ( observer_cb_u8_arg_t subscription_table,
uint8_t  subscription_table_size,
uint8_t  data 
)

Notify all registered callbacks with uint8_t argument.

Invokes all registered callbacks, passing a uint8_t data value.

Parameters
[in]subscription_tablePointer to array of observer_cb_u8_arg_t.
[in]subscription_table_sizeTable length.
[in]dataData argument passed to each callback.
Returns
OBSERVER_OK At least one callback invoked.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
OBSERVER_TABLE_EMPTY_ERROR No callbacks registered.
Precondition
Valid table pointer of given size.
Postcondition
All registered callbacks called with data. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-009

◆ subscribe()

subscr_status_e subscribe ( observer_cb_t subscription_table,
observer_cb_t  cb_2_register,
uint8_t  subscription_table_size 
)

Register a callback in the subscription table (void callbacks).

Registers a callback function in the provided subscription table. If the callback is already present, it is not added again.

Parameters
[in,out]subscription_tablePointer to an array of observer_cb_t.
[in]cb_2_registerCallback function to register.
[in]subscription_table_sizeNumber of entries in the table (must be > 0).
Returns
OBSERVER_OK Callback registered or already present.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
OBSERVER_TABLE_FULL_ERROR No free slot available.
Precondition
subscription_table points to a valid array of given size.
Postcondition
On success, cb_2_register is stored in the table. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-001

◆ subscribe_state_change()

subscr_status_e subscribe_state_change ( observer_cb_state_t subscription_table,
observer_cb_state_t  cb_2_register,
uint8_t  subscription_table_size 
)

Register a callback with event_state_e argument.

Registers a state-change callback in the given subscription table. Prevents duplicate registrations.

Parameters
[in,out]subscription_tablePointer to array of observer_cb_state_t.
[in]cb_2_registerCallback function to register.
[in]subscription_table_sizeTable length (must be > 0).
Returns
OBSERVER_OK Callback registered or already present.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
OBSERVER_TABLE_FULL_ERROR No free slot available.
Precondition
Valid table pointer of given size.
Postcondition
Callback stored on success. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-004

◆ subscribe_u8()

subscr_status_e subscribe_u8 ( observer_cb_u8_arg_t subscription_table,
observer_cb_u8_arg_t  cb_2_register,
uint8_t  subscription_table_size 
)

Register a callback with uint8_t argument.

Registers a callback in the given table if not already present.

Parameters
[in,out]subscription_tablePointer to array of observer_cb_u8_arg_t.
[in]cb_2_registerCallback to register.
[in]subscription_table_sizeTable length (must be > 0).
Returns
OBSERVER_OK Callback registered or already present.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
OBSERVER_TABLE_FULL_ERROR No free slot.
Precondition
Valid table pointer of given size.
Postcondition
Callback stored on success. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-007

◆ unsubscribe()

subscr_status_e unsubscribe ( observer_cb_t subscription_table,
observer_cb_t  cb_2_remove,
uint8_t  subscription_table_size 
)

Unregister a callback from the subscription table (void callbacks).

Removes a registered callback and compacts the table by shifting subsequent entries left.

Parameters
[in,out]subscription_tablePointer to an array of observer_cb_t.
[in]cb_2_removeCallback function to remove.
[in]subscription_table_sizeTable length.
Returns
OBSERVER_OK Callback successfully removed.
OBSERVER_TABLE_EMPTY_ERROR Callback not found.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
Precondition
Valid table pointer of given size.
Postcondition
Table compacted after removal. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-002

◆ unsubscribe_state_change()

subscr_status_e unsubscribe_state_change ( observer_cb_state_t subscription_table,
observer_cb_state_t  cb_2_remove,
uint8_t  subscription_table_size 
)

Unregister a callback with event_state_e argument.

Removes a previously registered callback from the table and compacts it.

Parameters
[in,out]subscription_tablePointer to array of observer_cb_state_t.
[in]cb_2_removeCallback function to remove.
[in]subscription_table_sizeTable length.
Returns
OBSERVER_OK Callback removed.
OBSERVER_TABLE_EMPTY_ERROR Callback not found.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
Precondition
Valid table pointer of given size.
Postcondition
Table compacted after removal. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-005

◆ unsubscribe_u8()

subscr_status_e unsubscribe_u8 ( observer_cb_u8_arg_t subscription_table,
observer_cb_u8_arg_t  cb_2_remove,
uint8_t  subscription_table_size 
)

Unregister a callback with uint8_t argument.

Removes a callback from the table and shifts subsequent entries left.

Parameters
[in,out]subscription_tablePointer to array of observer_cb_u8_arg_t.
[in]cb_2_removeCallback to remove.
[in]subscription_table_sizeTable length.
Returns
OBSERVER_OK Callback removed.
OBSERVER_TABLE_EMPTY_ERROR Callback not found.
OBSERVER_INVALID_ARGUMENT_ERROR Null pointer or zero size.
Precondition
Valid table pointer of given size.
Postcondition
Table compacted after removal. @safety Not reentrant; caller must ensure synchronization. @traceability OBSR-FUNC-008