OreBolt-OS/modules/orebolt-proxalarm/bledsp.h

91 lines
3.7 KiB
C
Executable File

/*
* bledsp.h -- BLE + DSP hardware layer for proxalarm
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Bridges the Bluetooth HCI interface with the MIPS DSP ASE for
* real-time RSSI smoothing and signal processing. Provides a
* source-agnostic device table that a future WiFi RSSI source can
* plug into via the same bledsp_device_t interface.
*
* Display: uses H2_LCD_WIDTH / H2_LCD_HEIGHT from h2_ui.h (320x240
* on the H2; change those two defines for 320x320 panels).
*/
#ifndef BLE_DSP_H
#define BLE_DSP_H
#include <stdint.h>
#include <stddef.h>
/* ------------------------------------------------------------------ */
/* Constants */
/* ------------------------------------------------------------------ */
#define BLE_DSP_MAX_DEVICES 32
#define BLE_DSP_RSSI_WINDOW 8 /* rolling-average samples */
#define BLE_DSP_RSSI_FLOOR -100 /* dBm: ignore anything weaker */
#define BLE_DSP_SCAN_INTERVAL_MS 1200 /* ms between scan sweeps */
#define BLE_DSP_HCI_DEV 0 /* hci0 */
/* RSSI thresholds (dBm) -- tunable at runtime via proxalarm UI */
#define BLE_DSP_THRESH_NEAR -50 /* alarm zone */
#define BLE_DSP_THRESH_MID -70 /* tracking zone */
#define BLE_DSP_THRESH_FAR -85 /* detection zone */
/* Signal source identifiers (future WiFi extension) */
typedef enum {
BLE_SRC_BLE = 0,
BLE_SRC_WIFI, /* TODO: future WiFi RSSI integration */
BLE_SRC_COUNT
} bledsp_source_t;
/* ------------------------------------------------------------------ */
/* Device record (fills the source -> proxvec interface) */
/* ------------------------------------------------------------------ */
typedef struct {
uint8_t mac[6];
int8_t rssi_raw; /* latest raw RSSI (dBm) */
int8_t rssi_smoothed; /* DSP-filtered RSSI (dBm) */
int8_t rssi_delta; /* change vs previous window (dB) */
uint32_t last_seen_ms; /* monotonic timestamp */
uint8_t active : 1; /* 1 = seen in last scan window */
uint8_t source : 2; /* bledsp_source_t */
uint8_t scan_count; /* consecutive detections */
} bledsp_device_t;
/* ------------------------------------------------------------------ */
/* Public API */
/* ------------------------------------------------------------------ */
/* Open HCI, configure LE scan, allocate internal state.
* Returns 0 on success, -1 on failure (falls back to sim mode). */
int bledsp_init(void);
/* Run one scan cycle. Non-blocking; returns immediately after
* dispatching the HCI command or (in sim mode) generating data. */
int bledsp_poll(void);
/* Look up a device by MAC. Returns pointer into internal table
* or NULL. Valid until next bledsp_poll() call. */
const bledsp_device_t *bledsp_find(const uint8_t *mac);
/* Copy the full active device table into caller-supplied buffer.
* Returns the number of active devices written (0 .. MAX). */
int bledsp_get_devices(bledsp_device_t *out, int max);
/* Get the smoothed RSSI for a single MAC address.
* Returns the smoothed value or BLE_DSP_RSSI_FLOOR if unknown. */
int16_t bledsp_get_smoothed_rssi(const uint8_t *mac);
/* Adjust the near-alarm RSSI threshold at runtime (dBm). */
void bledsp_set_near_threshold(int8_t dBm);
int8_t bledsp_get_near_threshold(void);
/* Check whether the backend is real BLE or simulation. */
int bledsp_is_simulated(void);
/* Stop scanning, close HCI socket, free resources. */
void bledsp_shutdown(void);
#endif /* BLE_DSP_H */