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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Device settings read/write.
//!
//! This module provides functionality to read and modify device
//! settings on Aranet sensors.
use tracing::{debug, info};
use crate::device::Device;
use crate::error::{Error, Result};
use crate::uuid::{CALIBRATION, COMMAND, READ_INTERVAL, SENSOR_STATE};
/// Measurement interval options.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum MeasurementInterval {
/// 1 minute interval.
OneMinute = 0x01,
/// 2 minute interval.
TwoMinutes = 0x02,
/// 5 minute interval.
FiveMinutes = 0x05,
/// 10 minute interval.
TenMinutes = 0x0A,
}
impl MeasurementInterval {
/// Get the interval in seconds.
pub fn as_seconds(&self) -> u16 {
match self {
MeasurementInterval::OneMinute => 60,
MeasurementInterval::TwoMinutes => 120,
MeasurementInterval::FiveMinutes => 300,
MeasurementInterval::TenMinutes => 600,
}
}
/// Try to create from seconds value.
pub fn from_seconds(seconds: u16) -> Option<Self> {
match seconds {
60 => Some(MeasurementInterval::OneMinute),
120 => Some(MeasurementInterval::TwoMinutes),
300 => Some(MeasurementInterval::FiveMinutes),
600 => Some(MeasurementInterval::TenMinutes),
_ => None,
}
}
/// Try to create from minutes value.
pub fn from_minutes(minutes: u8) -> Option<Self> {
match minutes {
1 => Some(MeasurementInterval::OneMinute),
2 => Some(MeasurementInterval::TwoMinutes),
5 => Some(MeasurementInterval::FiveMinutes),
10 => Some(MeasurementInterval::TenMinutes),
_ => None,
}
}
}
/// Bluetooth range options.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum BluetoothRange {
/// Standard range.
#[default]
Standard = 0x00,
/// Extended range.
Extended = 0x01,
}
/// Temperature display unit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TemperatureUnit {
/// Celsius (default for most devices).
#[default]
Celsius,
/// Fahrenheit.
Fahrenheit,
}
/// Radon display unit.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RadonUnit {
/// Becquerels per cubic meter (default).
#[default]
BqM3,
/// PicoCuries per liter.
PciL,
}
/// Device settings read from the SENSOR_STATE characteristic.
#[derive(Debug, Clone, Default)]
pub struct DeviceSettings {
/// Smart Home integration enabled.
pub smart_home_enabled: bool,
/// Bluetooth range setting.
pub bluetooth_range: BluetoothRange,
/// Temperature display unit.
pub temperature_unit: TemperatureUnit,
/// Radon display unit (only relevant for Aranet Radon).
pub radon_unit: RadonUnit,
/// Whether buzzer is enabled.
pub buzzer_enabled: bool,
/// Whether automatic calibration is enabled (Aranet4 only).
pub auto_calibration_enabled: bool,
}
/// Calibration data from the device.
#[derive(Debug, Clone, Default)]
pub struct CalibrationData {
/// Raw calibration bytes.
pub raw: Vec<u8>,
/// CO2 calibration offset (if available).
pub co2_offset: Option<i16>,
}
impl Device {
/// Get the current measurement interval.
pub async fn get_interval(&self) -> Result<MeasurementInterval> {
let data = self.read_characteristic(READ_INTERVAL).await?;
if data.len() < 2 {
return Err(Error::InvalidData("Invalid interval data".to_string()));
}
let seconds = u16::from_le_bytes([data[0], data[1]]);
MeasurementInterval::from_seconds(seconds)
.ok_or_else(|| Error::InvalidData(format!("Unknown interval: {} seconds", seconds)))
}
/// Set the measurement interval.
///
/// The device will start using the new interval after the current
/// measurement cycle completes.
///
/// Note: This method does not verify the write succeeded. For verified
/// writes, use [`Self::set_interval_verified`].
pub async fn set_interval(&self, interval: MeasurementInterval) -> Result<()> {
info!("Setting measurement interval to {:?}", interval);
// Command format: 0x90 XX (XX = interval in minutes)
let minutes = match interval {
MeasurementInterval::OneMinute => 0x01,
MeasurementInterval::TwoMinutes => 0x02,
MeasurementInterval::FiveMinutes => 0x05,
MeasurementInterval::TenMinutes => 0x0A,
};
let cmd = [0x90, minutes];
self.write_characteristic(COMMAND, &cmd).await?;
Ok(())
}
/// Set the measurement interval with verification.
///
/// This method writes the new interval and then reads it back to verify
/// the change was applied successfully. Use this for critical settings
/// changes where confirmation is needed.
///
/// # Errors
///
/// Returns `Error::WriteFailed` if the read-back value doesn't match
/// the requested interval.
pub async fn set_interval_verified(&self, interval: MeasurementInterval) -> Result<()> {
self.set_interval(interval).await?;
// Small delay to allow the device to process the command
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let actual = self.get_interval().await?;
if actual != interval {
return Err(Error::WriteFailed {
uuid: COMMAND.to_string(),
reason: format!(
"Interval verification failed: expected {:?}, got {:?}",
interval, actual
),
});
}
info!("Measurement interval verified: {:?}", interval);
Ok(())
}
/// Enable or disable Smart Home integration.
///
/// When enabled, the device advertises sensor data that can be read
/// without connecting (passive scanning).
///
/// Note: This method does not verify the write succeeded. For verified
/// writes, use [`Self::set_smart_home_verified`].
pub async fn set_smart_home(&self, enabled: bool) -> Result<()> {
info!("Setting Smart Home integration to {}", enabled);
// Command format: 0x91 XX (XX = 00 disabled, 01 enabled)
let cmd = [0x91, if enabled { 0x01 } else { 0x00 }];
self.write_characteristic(COMMAND, &cmd).await?;
Ok(())
}
/// Enable or disable Smart Home integration with verification.
///
/// This method writes the setting and then reads it back to verify
/// the change was applied successfully.
///
/// # Errors
///
/// Returns `Error::WriteFailed` if the read-back value doesn't match
/// the requested setting.
pub async fn set_smart_home_verified(&self, enabled: bool) -> Result<()> {
self.set_smart_home(enabled).await?;
// Small delay to allow the device to process the command
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let settings = self.get_settings().await?;
if settings.smart_home_enabled != enabled {
return Err(Error::WriteFailed {
uuid: COMMAND.to_string(),
reason: format!(
"Smart Home verification failed: expected {}, got {}",
enabled, settings.smart_home_enabled
),
});
}
info!("Smart Home integration verified: {}", enabled);
Ok(())
}
/// Set the Bluetooth range.
///
/// Note: This method does not verify the write succeeded. For verified
/// writes, use [`Self::set_bluetooth_range_verified`].
pub async fn set_bluetooth_range(&self, range: BluetoothRange) -> Result<()> {
info!("Setting Bluetooth range to {:?}", range);
// Command format: 0x92 XX (XX = 00 standard, 01 extended)
let cmd = [0x92, range as u8];
self.write_characteristic(COMMAND, &cmd).await?;
Ok(())
}
/// Set the Bluetooth range with verification.
///
/// This method writes the setting and then reads it back to verify
/// the change was applied successfully.
///
/// # Errors
///
/// Returns `Error::WriteFailed` if the read-back value doesn't match
/// the requested setting.
pub async fn set_bluetooth_range_verified(&self, range: BluetoothRange) -> Result<()> {
self.set_bluetooth_range(range).await?;
// Small delay to allow the device to process the command
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let settings = self.get_settings().await?;
if settings.bluetooth_range != range {
return Err(Error::WriteFailed {
uuid: COMMAND.to_string(),
reason: format!(
"Bluetooth range verification failed: expected {:?}, got {:?}",
range, settings.bluetooth_range
),
});
}
info!("Bluetooth range verified: {:?}", range);
Ok(())
}
/// Read calibration data from the device.
pub async fn get_calibration(&self) -> Result<CalibrationData> {
let raw = self.read_characteristic(CALIBRATION).await?;
// Parse CO2 offset if available (typically at offset 2-3)
let co2_offset = if raw.len() >= 4 {
Some(i16::from_le_bytes([raw[2], raw[3]]))
} else {
None
};
Ok(CalibrationData { raw, co2_offset })
}
/// Read device settings from the SENSOR_STATE characteristic.
///
/// This reads the device configuration including:
/// - Smart Home integration status
/// - Bluetooth range setting
/// - Temperature display unit
/// - Radon display unit (for Aranet Radon devices)
/// - Buzzer settings
/// - Calibration settings
pub async fn get_settings(&self) -> Result<DeviceSettings> {
let data = self.read_characteristic(SENSOR_STATE).await?;
if data.len() < 3 {
return Err(Error::InvalidData(
"Sensor state data too short".to_string(),
));
}
debug!("Sensor state raw: {:02x?} (len={})", data, data.len());
// Parse the sensor state bytes according to the Aranet protocol:
// byte[0] = device type (0xF1=Aranet4, 0xF2=Aranet2, 0xF3=Radon, 0xF4=Radiation)
// byte[1] = configuration flags 'c'
// byte[2] = options flags 'o'
let device_type_byte = data[0];
let config_flags = data[1];
let option_flags = data[2];
let is_aranet4 = device_type_byte == 0xF1;
let is_aranet_radon = device_type_byte == 0xF3;
let is_aranet_radiation = device_type_byte == 0xF4;
// Parse configuration flags (byte 1):
// bit 0: buzzer enabled
// bit 5: temperature unit (0=Fahrenheit, 1=Celsius)
// bit 7: varies by device (Aranet4=auto calibration, Radon=Bq/pCi)
let buzzer_enabled = (config_flags & 0x01) != 0;
let temp_bit = (config_flags >> 5) & 0x01;
let bit7 = (config_flags >> 7) & 0x01;
// Temperature unit: bit 5 = 1 means Celsius, 0 means Fahrenheit
// Note: Aranet Radiation doesn't have temperature, defaults to Celsius
let temperature_unit = if is_aranet_radiation || temp_bit == 1 {
TemperatureUnit::Celsius
} else {
TemperatureUnit::Fahrenheit
};
// Radon unit: for Aranet Radon, bit 7 = 1 means Bq/m³, 0 means pCi/L
let radon_unit = if is_aranet_radon {
if bit7 == 1 {
RadonUnit::BqM3
} else {
RadonUnit::PciL
}
} else {
RadonUnit::BqM3 // Default for non-radon devices
};
// Auto calibration enabled (Aranet4 only)
let auto_calibration_enabled = is_aranet4 && bit7 == 1;
// Parse option flags (byte 2):
// bit 1: bluetooth range (0=normal/standard, 1=extended)
// bit 7: smart home integration enabled
let bluetooth_range = if (option_flags >> 1) & 0x01 == 1 {
BluetoothRange::Extended
} else {
BluetoothRange::Standard
};
let smart_home_enabled = (option_flags >> 7) & 0x01 == 1;
debug!(
"Parsed settings: smart_home={}, bt_range={:?}, temp_unit={:?}, radon_unit={:?}",
smart_home_enabled, bluetooth_range, temperature_unit, radon_unit
);
Ok(DeviceSettings {
smart_home_enabled,
bluetooth_range,
temperature_unit,
radon_unit,
buzzer_enabled,
auto_calibration_enabled,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_interval_from_seconds() {
assert_eq!(
MeasurementInterval::from_seconds(60),
Some(MeasurementInterval::OneMinute)
);
assert_eq!(
MeasurementInterval::from_seconds(120),
Some(MeasurementInterval::TwoMinutes)
);
assert_eq!(
MeasurementInterval::from_seconds(300),
Some(MeasurementInterval::FiveMinutes)
);
assert_eq!(
MeasurementInterval::from_seconds(600),
Some(MeasurementInterval::TenMinutes)
);
assert_eq!(MeasurementInterval::from_seconds(100), None);
}
#[test]
fn test_interval_from_minutes() {
assert_eq!(
MeasurementInterval::from_minutes(1),
Some(MeasurementInterval::OneMinute)
);
assert_eq!(
MeasurementInterval::from_minutes(2),
Some(MeasurementInterval::TwoMinutes)
);
assert_eq!(
MeasurementInterval::from_minutes(5),
Some(MeasurementInterval::FiveMinutes)
);
assert_eq!(
MeasurementInterval::from_minutes(10),
Some(MeasurementInterval::TenMinutes)
);
assert_eq!(MeasurementInterval::from_minutes(3), None);
}
#[test]
fn test_interval_as_seconds() {
assert_eq!(MeasurementInterval::OneMinute.as_seconds(), 60);
assert_eq!(MeasurementInterval::TwoMinutes.as_seconds(), 120);
assert_eq!(MeasurementInterval::FiveMinutes.as_seconds(), 300);
assert_eq!(MeasurementInterval::TenMinutes.as_seconds(), 600);
}
}