Sensor stub driver

The sensor stub, a simple simulated sensor driver, is an interface between the sensor API and simple callback functions to fetch and forward the data. The sensor stub implements sensor simulation with minimal required resources. It can also be used to provide some real sensor data from the application in the simplest way possible. The application or the sample supports multiple instances of the sensor stub driver.

Configuration

You can define instances of the sensor stub on devicetree like this:

sensor_stub: sensor_stub {
        compatible = "nordic,sensor-stub";
        generator = "sensor_stub_gen";
        status = "okay";
};

The most important setting here is the generator property. The driver requires the following functions to be implemented:

  • int <generator>_init(const struct device *dev) - This function initializes the driver.

  • int <generator>_get(const struct device *dev, enum sensor_channel ch, struct sensor_value *val) - This function gets the measured value.

  • int <generator>_fetch(const struct device *dev, enum sensor_channel ch) - This function fetches (prepares) the measured value.

The functions must have global linkage and connect directly to the sensor stub driver.

API documentation

Header file: include/drivers/sensor_stub.h
Source file: drivers/sensor/sensor_stub/sensor_stub.c
group sensor_stub

Simple simulated sensor device driver.

The simulator redirects the _fetch and _get functions from the implemented sensor directly to the user functions.

Functions

void *sensor_stub_udata_get(const struct device *dev)

Get user data.

The data pointer to free usage by generator functions.

Parameters:
  • dev – Device handler.

Returns:

The data pointer.

void sensor_stub_udata_set(const struct device *dev, void *udata)

Set user data.

Set the user data to be used by generator functions.

Parameters:
  • dev – Device handler.

  • udata – User data pointer.