1#include "../include/sensor/sensor.h"
11#define SENSOR_PLUGIN_EXPORT __declspec(dllexport)
13#define SENSOR_PLUGIN_EXPORT
16static void fill_timestamp(
char *buf,
size_t len) {
17 time_t now = time(NULL);
18 const struct tm *ptm = localtime(&now);
20 snprintf(buf, len,
"1970-01-01 00:00:00");
23 snprintf(buf, len,
"%04d-%02d-%02d %02d:%02d:%02d", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday,
24 ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
27static int bme680_init(
void) {
29 printf(
"BME680 plugin initialized (real IIO/sysfs on Linux if available; fallback mock).\n");
31 printf(
"BME680 plugin initialized (mock on Windows).\n");
37static int read_float_file(
const char *path,
float *out) {
38 FILE *f = fopen(path,
"r");
42 int ok = fscanf(f,
"%f", &v);
50static int try_read_bme680_iio(
float *temp_c,
float *hum_pct,
float *gas_kohm) {
51 const char *forced = getenv(
"BME680_IIO_PATH");
52 if (forced && forced[0]) {
54 float t = 0.0f, h = 0.0f, g = 0.0f;
55 snprintf(path,
sizeof(path),
"%s/in_temp_input", forced);
56 if (read_float_file(path, &t) != 0)
58 snprintf(path,
sizeof(path),
"%s/in_humidityrelative_input", forced);
59 if (read_float_file(path, &h) != 0)
61 snprintf(path,
sizeof(path),
"%s/in_resistance_input", forced);
62 if (read_float_file(path, &g) != 0)
64 *temp_c = t / 1000.0f;
65 *hum_pct = h / 1000.0f;
66 *gas_kohm = g / 1000.0f;
70 DIR *d = opendir(
"/sys/bus/iio/devices");
73 const struct dirent *ent;
74 while ((ent = readdir(d)) != NULL) {
75 if (strncmp(ent->d_name,
"iio:device", 10) != 0)
77 char base[512], namef[640], name[128];
78 snprintf(base,
sizeof(base),
"/sys/bus/iio/devices/%s", ent->d_name);
79 snprintf(namef,
sizeof(namef),
"%s/name", base);
80 FILE *f = fopen(namef,
"r");
84 fgets(name,
sizeof(name), f);
86 if (strstr(name,
"bme680") == NULL && strstr(name,
"BME680") == NULL)
89 float t = 0.0f, h = 0.0f, g = 0.0f;
91 snprintf(p,
sizeof(p),
"%s/in_temp_input", base);
92 if (read_float_file(p, &t) != 0)
94 snprintf(p,
sizeof(p),
"%s/in_humidityrelative_input", base);
95 if (read_float_file(p, &h) != 0)
97 snprintf(p,
sizeof(p),
"%s/in_resistance_input", base);
98 if (read_float_file(p, &g) != 0)
100 *temp_c = t / 1000.0f;
101 *hum_pct = h / 1000.0f;
102 *gas_kohm = g / 1000.0f;
114 memset(out, 0,
sizeof(*out));
116 snprintf(out->
model,
sizeof(out->
model),
"BME680");
120 out->pm25 = 20.0f + (float)(rand() % 1200) / 100.0f;
121 out->pm10 = 35.0f + (float)(rand() % 5000) / 100.0f;
122 out->co = 0.5f + (float)(rand() % 400) / 100.0f;
124 float rt = 0.0f, rh = 0.0f, rg = 0.0f;
125 if (try_read_bme680_iio(&rt, &rh, &rg) == 0) {
138static int bme680_shutdown(
void) {
139 printf(
"BME680 plugin shutdown.\n");
146 .plugin_version =
"1.0.0",
147 .description =
"Simulated BME680 readings (temperature/humidity/VOC proxy).",
150 .read_sample = bme680_read_sample,
151 .shutdown = bme680_shutdown,
One air quality reading: pollutant values plus sensor/timestamp metadata.
char timestamp[32]
Reading timestamp, formatted "%Y-%m-%d %H:%M:%S".
char model[64]
Sensor model name, e.g.
int sensor_id
Numeric sensor identifier, matches SensorPlugin::sensor_id.
Runtime contract every sensor plugin (.so/.dylib/.dll) must implement.
int api_version
Must equal SENSOR_PLUGIN_API_VERSION at build time.