arctic 1.0.0

A Rust libary for handling bluetooth Polar heart rate monitors
Documentation
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
//! # Control
//!
//! Control contains structures related to sending and receiving messages over PMD control point.
//!

use crate::{find_characteristic, Error, H10MeasurementType, PolarResult};

use btleplug::api::{Characteristic, Peripheral as _, WriteType};
use btleplug::platform::Peripheral;
use uuid::Uuid;

/// Polar Measurement Data Control Point (Read | Write | Indicate)
const PMD_CP_UUID: Uuid = Uuid::from_u128(0xfb005c81_02e7_f387_1cad_8acd2d8df0c8);
/// Polar Measurement Data... Data (Notify)
const PMD_DATA_UUID: Uuid = Uuid::from_u128(0xfb005c82_02e7_f387_1cad_8acd2d8df0c8);

/// Command options to write to the control point
#[derive(Debug, PartialEq, Eq)]
pub enum ControlPointCommand {
    /// Do nothing
    Null = 0,
    /// Get the measurement settings of every data type in `PolarSensor.data_type`
    GetMeasurementSettings,
    /// Start measurement of every data type in `PolarSensor.data_type`
    RequestMeasurementStart,
    /// Stop all measurements in `PolarSensor.data_type`
    StopMeasurement,
}

impl TryFrom<u8> for ControlPointCommand {
    type Error = ();

    fn try_from(val: u8) -> Result<ControlPointCommand, ()> {
        match val {
            0 => Ok(ControlPointCommand::Null),
            1 => Ok(ControlPointCommand::GetMeasurementSettings),
            2 => Ok(ControlPointCommand::RequestMeasurementStart),
            3 => Ok(ControlPointCommand::StopMeasurement),
            _ => {
                println!("Invalid ControlPointCommand {}", val);
                Err(())
            }
        }
    }
}

/// Response code returned after a write to PMD control point
#[derive(Debug, PartialEq, Eq)]
pub enum ControlPointResponseCode {
    /// Command was successful
    Success = 0,
    /// Control point command is not supported by device
    InvalidOpCode,
    /// Device does not know the specified measurement type
    InvalidMeasurementType,
    /// This measurement is not supported by device
    NotSupported,
    /// Given length does not match the received data
    InvalidLength,
    /// Contains parameters that prevent successful handling of request
    InvalidParameter,
    /// Device is already in the requested state
    AlreadyInState,
    /// Requested resolution is not supported by device
    InvalidResolution,
    /// Requested sample rate is not supported by device
    InvalidSampleRate,
    /// Requested range is not supported
    InvalidRange,
    /// Connection MTU does not match device required MTU
    InvalidMTU,
    /// Request contains invalid number of channels
    InvalidNumberOfChannels,
    /// Device is in invalid state
    InvalidState,
    /// Device is in charger and does not support requests
    DeviceInCharger,
}

impl TryFrom<u8> for ControlPointResponseCode {
    type Error = ();

    fn try_from(val: u8) -> Result<ControlPointResponseCode, ()> {
        match val {
            0 => Ok(ControlPointResponseCode::Success),
            1 => Ok(ControlPointResponseCode::InvalidOpCode),
            2 => Ok(ControlPointResponseCode::InvalidMeasurementType),
            3 => Ok(ControlPointResponseCode::NotSupported),
            4 => Ok(ControlPointResponseCode::InvalidLength),
            5 => Ok(ControlPointResponseCode::InvalidParameter),
            6 => Ok(ControlPointResponseCode::AlreadyInState),
            7 => Ok(ControlPointResponseCode::InvalidResolution),
            8 => Ok(ControlPointResponseCode::InvalidSampleRate),
            9 => Ok(ControlPointResponseCode::InvalidRange),
            10 => Ok(ControlPointResponseCode::InvalidMTU),
            11 => Ok(ControlPointResponseCode::InvalidNumberOfChannels),
            12 => Ok(ControlPointResponseCode::InvalidState),
            13 => Ok(ControlPointResponseCode::DeviceInCharger),
            _ => {
                println!("Invalid ControlPointResponseCode {}", val);
                Err(())
            }
        }
    }
}

#[derive(Debug, PartialEq, Eq)]
enum ResponseCode {
    Success,
    InvalidHandle,
    ReadNotPermitted,
    WriteNotPermitted,
    InvalidPdu,
    InsufficientAuthentication,
    RequestNotSupported,
    InvalidOffset,
    InsufficientAuthorization,
    PrepareQueueFull,
    AttributeNotFound,
    AttributeNotLong,
    InsufficientEncryptionKeySize,
    InsufficientAttributeValueLength,
    UnlikelyError,
    InsufficientEncryption,
    UnsupportedGroupType,
    InsufficientResources,
}

impl TryFrom<u8> for ResponseCode {
    type Error = ();

    fn try_from(val: u8) -> Result<ResponseCode, ()> {
        match val {
            0 => Ok(ResponseCode::Success),
            1 => Ok(ResponseCode::InvalidHandle),
            2 => Ok(ResponseCode::ReadNotPermitted),
            3 => Ok(ResponseCode::WriteNotPermitted),
            4 => Ok(ResponseCode::InvalidPdu),
            5 => Ok(ResponseCode::InsufficientAuthentication),
            6 => Ok(ResponseCode::RequestNotSupported),
            7 => Ok(ResponseCode::InvalidOffset),
            8 => Ok(ResponseCode::InsufficientAuthorization),
            9 => Ok(ResponseCode::PrepareQueueFull),
            10 => Ok(ResponseCode::AttributeNotFound),
            11 => Ok(ResponseCode::AttributeNotLong),
            12 => Ok(ResponseCode::InsufficientEncryptionKeySize),
            13 => Ok(ResponseCode::InsufficientAttributeValueLength),
            14 => Ok(ResponseCode::UnlikelyError),
            15 => Ok(ResponseCode::InsufficientEncryption),
            16 => Ok(ResponseCode::UnsupportedGroupType),
            17 => Ok(ResponseCode::InsufficientResources),
            _ => {
                println!("Invalid ResponseCode {}", val);
                Err(())
            }
        }
    }
}

#[derive(Clone, Copy, Debug)]
enum SettingType {
    SampleRate,
    Resolution,
    Range,
}

impl SettingType {
    fn from(byte: u8) -> SettingType {
        match byte {
            0x00 => SettingType::SampleRate,
            0x01 => SettingType::Resolution,
            _ => SettingType::Range,
        }
    }
}

enum PmdByteType {
    Setting,
    ArrLen,
    Data,
}

/// Struct to store the settings for a specific stream on your device
#[derive(Debug, PartialEq, Eq)]
pub struct StreamSettings {
    ty: H10MeasurementType,
    resolution: u8,
    range: Option<Vec<u8>>,
    sample_rate: Vec<u8>,
}

impl StreamSettings {
    /// Create new stream settings
    pub fn new(resp: &ControlResponse) -> PolarResult<StreamSettings> {
        if *resp.opcode() != ControlPointCommand::GetMeasurementSettings {
            return Err(Error::WrongResponse);
        }

        let mut resolution: u8 = 0;
        let mut ranges: Vec<u8> = vec![];
        let mut sample_rate: Vec<u8> = vec![];

        let mut setting: SettingType = SettingType::from(resp.parameters[0]);
        let mut next_byte: PmdByteType = PmdByteType::ArrLen;
        let mut len_remaining = 0u8;

        let mut data = resp.parameters()[1..].iter();

        while let Some(i) = data.next() {
            match next_byte {
                PmdByteType::Setting => {
                    setting = SettingType::from(*i);
                    next_byte = PmdByteType::ArrLen;
                }
                PmdByteType::ArrLen => {
                    len_remaining = *i;
                    next_byte = PmdByteType::Data;
                }
                PmdByteType::Data => {
                    match setting {
                        SettingType::SampleRate => {
                            sample_rate.push(*i);
                            let _ = data.next().unwrap();
                        }
                        SettingType::Resolution => {
                            resolution = *i;
                            let _ = data.next().unwrap();
                        }
                        SettingType::Range => {
                            ranges.push(*i);
                            let _ = data.next().unwrap();
                        }
                    }

                    len_remaining -= 1;
                    if len_remaining == 0 {
                        next_byte = PmdByteType::Setting;
                    }
                }
            }
        }

        let range = if !ranges.is_empty() {
            Some(ranges)
        } else {
            None
        };

        Ok(StreamSettings {
            ty: *resp.data_type(),
            resolution,
            range,
            sample_rate,
        })
    }

    /// Getter for the resolution (in bits)
    pub fn resolution(&self) -> u8 {
        self.resolution
    }

    /// Getter for range (ACC only) (in G)
    pub fn range(&self) -> &Option<Vec<u8>> {
        &self.range
    }

    /// Getter for sample rates (in Hz)
    pub fn sample_rate(&self) -> &Vec<u8> {
        &self.sample_rate
    }
}

/// Store data returned from the device after a write to the control point
#[derive(Debug)]
pub struct ControlResponse {
    opcode: ControlPointCommand,
    measurement_type: H10MeasurementType,
    status: ControlPointResponseCode,
    parameters: Vec<u8>,
}

impl ControlResponse {
    /// Create new `ControlResponse`
    pub async fn new(data: Vec<u8>) -> PolarResult<ControlResponse> {
        // We need at least 4 bytes for a complete packet
        if data.len() < 4 {
            return Err(Error::InvalidData);
        }
        // check that our response is a control point response
        if data[0] != 0xf0 {
            return Err(Error::InvalidData);
        }
        let opcode = ControlPointCommand::try_from(data[1]).map_err(|_| Error::InvalidData)?;
        let measurement_type =
            H10MeasurementType::try_from(data[2]).map_err(|_| Error::InvalidData)?;
        let status = ControlPointResponseCode::try_from(data[3]).map_err(|_| Error::InvalidData)?;
        let mut parameters = Vec::new();

        if data.len() > 5 {
            parameters = data[5..].to_vec();
        }

        Ok(ControlResponse {
            opcode,
            measurement_type,
            status,
            parameters,
        })
    }

    /// Return extra parameters of this response
    pub fn parameters(&self) -> &Vec<u8> {
        &self.parameters
    }

    /// Return opcode of this response
    pub fn opcode(&self) -> &ControlPointCommand {
        &self.opcode
    }

    /// Get measurement type
    pub fn data_type(&self) -> &H10MeasurementType {
        &self.measurement_type
    }

    /// Get response status
    pub fn status(&self) -> &ControlPointResponseCode {
        &self.status
    }
}

/// Struct that has access to the PMD control point point and PMD data
#[derive(Debug, PartialEq, Eq)]
pub struct ControlPoint {
    control_point: Characteristic,
    measurement_data: Characteristic,
}

impl ControlPoint {
    /// Create new `ControlPoint`
    pub async fn new(device: &Peripheral) -> PolarResult<ControlPoint> {
        let control_point = find_characteristic(device, PMD_CP_UUID).await?;
        let measurement_data = find_characteristic(device, PMD_DATA_UUID).await?;

        Ok(ControlPoint {
            control_point,
            measurement_data,
        })
    }

    /// Send command to Control Point
    pub async fn send_command(&self, device: &Peripheral, data: Vec<u8>) -> PolarResult<()> {
        self.write(device, data).await?;

        Ok(())
    }

    async fn write(&self, device: &Peripheral, data: Vec<u8>) -> PolarResult<()> {
        device
            .write(&self.control_point, &data, WriteType::WithResponse)
            .await
            .map_err(Error::BleError)
    }

    /// Read data from control point (for reading the features of a device)
    pub async fn read(&self, device: &Peripheral) -> PolarResult<Vec<u8>> {
        device
            .read(&self.control_point)
            .await
            .map_err(Error::BleError)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    // for async testing
    macro_rules! aw {
        ($e:expr) => {
            tokio_test::block_on($e)
        };
    }

    #[test]
    fn settings_ecg() {
        let norm = StreamSettings {
            ty: H10MeasurementType::Ecg,
            resolution: 14,
            range: None,
            sample_rate: vec![130],
        };

        let data = aw!(ControlResponse::new(vec![
            0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x82, 0x00, 0x01, 0x01, 0x0e, 0x00
        ]))
        .unwrap();

        assert_eq!(norm, StreamSettings::new(&data).unwrap());
    }

    #[test]
    fn settings_acc() {
        let norm = StreamSettings {
            ty: H10MeasurementType::Acc,
            resolution: 16,
            range: Some(vec![2, 4, 8]),
            sample_rate: vec![25, 50, 100, 200],
        };

        let data = aw!(ControlResponse::new(vec![
            0xf0, 0x01, 0x02, 0x00, 0x00, 0x00, 0x04, 0x19, 0x00, 0x32, 0x00, 0x64, 0x00, 0xC8,
            0x00, 0x01, 0x01, 0x10, 0x00, 0x02, 0x03, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00
        ]))
        .unwrap();

        assert_eq!(norm, StreamSettings::new(&data).unwrap());
    }
}