1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use anyhow::{anyhow, Result};
use esp32_nimble::{
    enums::{PowerLevel, PowerType},
    BLEAdvertisementData, BLEDevice, BLEScan,
};
use esp_idf_hal::task::block_on;
use std::sync::{Arc, Mutex};

use crate::{
    clock::Timer,
    infra::{Poller, State, Switch},
    message::{Notifier, Trigger},
};

/// Initializes the BLE device with the specified power level for advertising and scanning.
///
/// # Arguments
/// * `power_level` - The power level to use for both advertising and scanning.
///
/// # Returns
/// `Ok(())` on success.
///
/// # Errors
/// Returns an error if the BLE device cannot be configured with the specified power levels.
pub fn initialize(power_level: PowerLevel) -> Result<()> {
    let device = BLEDevice::take();
    device.set_power(PowerType::Advertising, power_level)?;
    device.set_power(PowerType::Scan, power_level)?;

    Ok(())
}

/// Function type for deriving advertisement name and payload from state.
type DeriveFn = fn(&State, Option<&[u8]>) -> (String, Option<Vec<u8>>);

/// Represents a BLE advertiser.
pub struct Advertiser {
    state: State,
    payload: Option<Vec<u8>>,
    derive: DeriveFn,
}

impl Advertiser {
    /// Creates a new `Advertiser` instance.
    ///
    /// # Arguments
    /// * `state` - Initial state of the advertiser.
    /// * `derive` - Function to derive advertisement name and payload from state.
    ///
    /// # Returns
    /// A new `Advertiser` with the advertisement already applied.
    ///
    /// # Errors
    /// Returns an error if the advertisement cannot be applied.
    pub fn new(state: State, derive: DeriveFn) -> Result<Self> {
        let ret = Self {
            state,
            payload: None,
            derive,
        };
        ret.apply()?;

        Ok(ret)
    }

    /// Applies the current state to the BLE advertiser.
    ///
    /// # Errors
    /// Returns an error if the BLE device or advertising data cannot be configured.
    fn apply(&self) -> Result<()> {
        let device = BLEDevice::take();
        let advertising = device.get_advertising();
        let (name, payload) = (self.derive)(&self.state, self.payload.as_deref());

        let mut data = BLEAdvertisementData::new();
        data.name(&name);
        if let Some(bytes) = &payload {
            data.manufacturer_data(bytes);
        }

        advertising.lock().set_data(&mut data)?;
        advertising.lock().start()?;

        Ok(())
    }

    /// Updates the BLE advertisement payload and re-applies the advertisement.
    ///
    /// # Arguments
    /// * `payload` - Optional new manufacturer data bytes, or `None` to clear.
    ///
    /// # Returns
    /// `Ok(())` on success.
    ///
    /// # Errors
    /// Returns an error if the advertisement cannot be re-applied.
    pub fn set_payload(&mut self, payload: Option<Vec<u8>>) -> Result<()> {
        self.payload = payload;
        self.apply()
    }
}

impl Switch for Advertiser {
    /// Toggles the state of the advertiser.
    ///
    /// # Returns
    /// `Ok(())` on success.
    ///
    /// # Errors
    /// Returns an error if the advertisement cannot be re-applied.
    fn toggle(&mut self) -> Result<()> {
        self.state.toggle();

        self.apply()
    }
}

/// Configuration for BLE scanning behavior.
///
/// # Type Parameters
/// * `T` - The trigger type implementing the `Trigger` trait.
pub struct ScannerConfig<T: Trigger> {
    triggers: fn(&str) -> Option<&'static T>,
    default_trigger: &'static T,
    payload_trigger: &'static T,
    scan_freq_hz: u64,
}

impl<T: Trigger> ScannerConfig<T> {
    /// Creates a new scan configuration.
    ///
    /// # Arguments
    /// * `triggers` - Function to look up a trigger by BLE device name.
    /// * `default_trigger` - Trigger to emit when no matching device is found.
    /// * `payload_trigger` - Store payload when this trigger matches.
    /// * `scan_freq_hz` - Scan frequency in Hz.
    ///
    /// # Returns
    /// A new `ScannerConfig` instance.
    #[must_use]
    pub fn new(
        triggers: fn(&str) -> Option<&'static T>,
        default_trigger: &'static T,
        payload_trigger: &'static T,
        scan_freq_hz: u64,
    ) -> Self {
        Self {
            triggers,
            default_trigger,
            payload_trigger,
            scan_freq_hz,
        }
    }
}

/// Represents a BLE scanner.
///
/// # Type Parameters
/// * `'a` - Lifetime of the scanner.
/// * `T` - The trigger type implementing the `Trigger` trait.
pub struct Scanner<'a, T: Trigger> {
    notifier: Notifier<T>,
    timer: Timer<'a, T>,
    state: Arc<Mutex<State>>,
    payload: Arc<Mutex<Option<Vec<u8>>>>,
    device: &'a BLEDevice,
    scan: BLEScan,
    config: ScannerConfig<T>,
}

impl<'a, T: Trigger> Scanner<'a, T> {
    /// BLE scan window duration in milliseconds.
    const WINDOW: i32 = 1000;

    /// Creates a new `Scanner` instance.
    ///
    /// # Arguments
    /// * `notifier` - A notifier to send scan results.
    /// * `timer` - A timer for scan intervals.
    /// * `state` - Shared state of the scanner.
    /// * `payload` - Shared storage for BLE payload data.
    /// * `config` - Scan configuration (triggers, frequency, etc.).
    ///
    /// # Returns
    /// A new `Scanner` ready to poll.
    ///
    /// # Errors
    /// Returns an error if the scanner cannot be initialized.
    pub fn new(
        notifier: Notifier<T>,
        timer: Timer<'a, T>,
        state: Arc<Mutex<State>>,
        payload: Arc<Mutex<Option<Vec<u8>>>>,
        config: ScannerConfig<T>,
    ) -> Result<Self> {
        let device = BLEDevice::take();
        let scan = BLEScan::new();

        Ok(Self {
            notifier,
            timer,
            state,
            payload,
            device,
            scan,
            config,
        })
    }

    /// Performs a BLE scan.
    ///
    /// # Errors
    /// Returns an error if the scan fails.
    async fn do_scan(&mut self) -> Result<Option<&'static T>> {
        let triggers = self.config.triggers;
        let payload = Arc::clone(&self.payload);
        let payload_trigger = self.config.payload_trigger;
        Ok(self
            .scan
            .start(self.device, Self::WINDOW, move |_, data| {
                data.name().and_then(|name| {
                    let name = String::from_utf8_lossy(name);
                    if let Some(trigger) = triggers(&name) {
                        if trigger == payload_trigger {
                            if let Some(mfg) = data.manufacture_data() {
                                if let Ok(mut stored) = payload.lock() {
                                    // manufacture_data() splits the raw bytes into a
                                    // 2-byte company_identifier and the remaining payload.
                                    // We reconstruct the original bytes here.
                                    let mut full = mfg
                                        .company_identifier
                                        .to_le_bytes()
                                        .to_vec();
                                    full.extend_from_slice(mfg.payload);
                                    *stored = Some(full);
                                }
                            }
                        }
                        Some(trigger)
                    } else {
                        None
                    }
                })
            })
            .await?)
    }
}

impl<T: Trigger> Poller for Scanner<'_, T> {
    /// Polls the BLE scanner for devices.
    ///
    /// This function continuously scans for BLE devices and notifies the results.
    ///
    /// # Errors
    /// Returns an error if the scan or notification fails.
    fn poll(&mut self) -> Result<!> {
        block_on(async {
            loop {
                self.timer.delay(self.config.scan_freq_hz).await?;

                if self
                    .state
                    .lock()
                    .map_err(|e| anyhow!("Mutex lock error: {:?}", e))?
                    .is_off()
                {
                    continue;
                }

                let trigger = if let Some(trigger) = self.do_scan().await? {
                    trigger
                } else {
                    self.config.default_trigger
                };

                self.notifier.notify(trigger)?;
            }
        })
    }
}