API Documentation

Provider lib

Public API for the provider library. Details on how to use this can be found here.

Enums

enum provider_status

Values:

enumerator PROVIDER_STATUS_OK
enumerator PROVIDER_STATUS_ERROR
enumerator PROVIDER_STATUS_SAMPLE_FETCH_ERR
enumerator PROVIDER_STATUS_CHAN_GET_ERR
enumerator PROVIDER_STATUS_OOB_ERR

Functions

int provider_reader_register(struct provider_reader *pr, struct provider *p)

Registers a single provider in the provider reader and increments dst_N.

Usually called from helpers like provider_reader_register_all_sensor.

int provider_reader_read_all(struct provider_reader *pr, float *dst)

Reads from all providers in the provider reader using read_fn and collates results into a float buffer.

dst size must be greater or equal than dst_N.

int provider_reader_hdr(struct provider_reader *pr, struct provider_hdr_entry *hdr)

Generates a header containing information on provider id and channel of every element in the float buffer returned by provider_reader_read_all.

hdr size must be greater or equal than dst_N. Header doesn’t change between reads and should be generated only after provider registration.

struct provider
#include <provider.h>

Public Members

enum sensor_channel *channels
uint32_t channels_N
int (*read_fn)(struct provider*, float*)
void *info
struct provider_reader
#include <provider.h>

Public Members

struct provider *ps[CONFIG_MAX_PROVIDER_COUNT]
uint32_t ps_N
uint32_t dst_N
struct provider_hdr_entry
#include <provider.h>

Public Members

uint32_t provider_id
enum sensor_channel chan

Anomaly lib

Anomaly lib documentation. Details on usage are found here.

Defines

DETECTOR_SMOOTHING_NONE

No smoothing used for anomaly detection.

DETECTOR_SMOOTHING_EXP_SMOOTHING

Apply exponential moving average smoothing. Ensure to set the smoothing factor:

struct detector_classifier dc;
detector_classifier_init(&dc, ...);
dc.exp_moving_avg_opts.smoothing_factor = 0.3f;
DETECTOR_DEFAULT_OPTS

Typedefs

typedef void (*detector_cb_t)(void *ctx, float score)

Callback type when classifier threshold is met.

Param ctx:

Opaque context passed by caller.

Param score:

Logit output of the classifier.

typedef float (*detector_smoothing_cb_t)(struct detector_classifier *dc, float score, void *ctx)

Type for the smoothing function.

Enums

enum detector_status

Values:

enumerator DETECTOR_STATUS_OK
enumerator DETECTOR_STATUS_ERROR
enumerator DETECTOR_STATUS_SIZE_ERR
enumerator DETECTOR_STATUS_EINVAL
enumerator DETECTOR_STATUS_ERANGE

Functions

float detector_smoothing_exp_smoothing(struct detector_classifier *dc, float score, void *ctx)
static inline int32_t detector_detect(const float *buffer, float *score)

Calculates anomaly score of provided data

buffer size depends on the detector. score is a single float.

static inline int32_t detector_init()
static inline int32_t detector_get_info(void *info)
int32_t detector_classifier_init(struct detector *d, struct detector_classifier *dc, struct provider_reader *pr, detector_smoothing_cb_t smoothing, int64_t processing_delay, float threshold)

Initialize the detector classifier struct.

Parameters:
struct detector *d

Initialized detector

struct detector_classifier *dc

Uninitialized detector classifier

struct provider_reader *pr

Pointer to the initialized provider_reader object

detector_smoothing_cb_t smoothing

Function used for smoothing scores. Possible values are:

  • DETECTOR_SMOOTHING_NONE - no smoothing.

  • DETECTOR_SMOOTHING_EXP_SMOOTHING - exponential smoothing.

int64_t processing_delay

Cadence for obtaining new values from the detector.

float threshold

Probability threshold in (0, 1) to determine whether a score is anomaly or not.

int32_t detector_classifier_start(struct detector_classifier *dc, int priority, int thread_opts)

Start the callback handler in the background.

Parameters:
struct detector_classifier *dc

Initialized classifier object

int priority

Priority given to thread.

int thread_opts

Pass DETECTOR_DEFAULT_OPTS for the default settings. Otherwise, this contains standard Zephyr thread options.

int32_t detector_classifier_deinit(struct detector_classifier *dc)

Stop the callback handler and cleanup

Parameters:
struct detector_classifier *dc

classifier object

int32_t detector_classifier_register_cb(struct detector_classifier *dc, detector_cb_t cb, void *ctx)

Register a callback that is run when an anomaly is detected.

Parameters:
struct detector_classifier *dc

The detector object.

detector_cb_t cb

Callback function

void *ctx

Opaque context passed to this specific callback.

void detector_classifier_unregister_cb(struct detector_classifier *dc)

Unregister all callbacks

Parameters:
struct detector_classifier *dc

The detector object

Variables

struct detector g_detector
struct detector
#include <detector.h>

Public Members

int32_t (*detect)(struct detector*, const float*, float*)
int32_t (*init)(struct detector*)
int32_t (*get_info)(struct detector*, void**)
struct detector_smoothing_opts_exp_mav
#include <detector.h>

Public Members

float smoothing_factor

Value between 0 and 1.

struct detector_classifier
#include <detector.h>

Callback handler manager for anomaly detection.

Public Members

struct detector *detector

Detector object for this specific classifier

float threshold

Threshold for the anomaly detection

int64_t process_ms

Expected processing time

size_t num_cbs

Number of registered callbacks

void *cb_ctxs[CONFIG_ANOMALY_LIB_DETECTION_CALLBACKS_MAX_CB_HDLRS]
detector_smoothing_cb_t smoothing

Smoothing function

float smoothed_moving_average

Current value of the moving average

bool smoothing_started

Indicator whether smoothing started. Used to prevent initializing smoothing_score to some absurd number such as 0 or NaN.

struct detector_smoothing_opts_exp_mav exp_moving_avg_opts

Options when DETECTOR_SMOOTHING_EXP_SMOOTHING is used

union detector_classifier

Private Members

struct provider_reader *pr

Provider reader object

atomic_t deinit_started

started deinitialization process

struct k_work_q wq

internal work queue

float buffer[128]

internal buffer for the provider

struct provider_hdr_entry hdr[128]

internal header buffer

struct k_thread thread_data

thread data

k_tid_t tid

thread ID

detector_cb_t cb_hdlrs[CONFIG_ANOMALY_LIB_DETECTION_CALLBACKS_MAX_CB_HDLRS]

Struct containing the registered callbacks


Last update: 2026-05-21