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
//! Packages sent by the device.

use core::fmt;
use core::fmt::{Debug, Formatter};
use core::task::Poll;

use futures::ready;

use crate::bit_ops::{get_bit, get_bit_range};
use crate::encoding::decode_high_byte;
use crate::Error;

macro_rules! incoming_packages {
    (
        $(
            $(#[$outer:meta])*
            $code:literal => |$bytes:ident: [u8; $length:literal]| $(#[$outer2:meta])* $name:ident {
                $(
                    $(#[$field_meta:meta])*
                    $field_vis:vis $field_name:ident: $field_type:ty = $field_const:expr
                ),*$(,)?
            }
        ),*$(,)?
    ) => {
        /// A Package sent by the device.
        #[derive(Debug)]
        pub enum IncomingPackage {
            $(
                $(#[$outer])*
                $name($name),
            )*
        }

        $(
            $(#[$outer])*
            $(#[$outer2])*
            pub struct $name {
                $(
                    $(#[$field_meta])*
                    $field_vis $field_name: $field_type,
                )*
            }

            impl $name {
                /// Create a new Package from the given byte array
                pub(super) fn from_bytes($bytes: [u8; $length]) -> Self {
                    $name {
                        $($field_name: $field_const,)*
                    }
                }
            }
        )*

        /// State machine which handles incoming packages
        pub enum IncomingStateMachine {
            /// Current status: nothing read
            None,
            $(
                $(#[$outer])*
                $name {
                    /// byte buffer
                    buffer: [u8; ($length+1)],
                    /// byte count
                    received_bytes: usize
                },
            )*
        }

        impl IncomingStateMachine {
            /// Resumes execution of the state machine
            ///
            /// # Arguments
            ///
            /// * `read` - A function which read new bytes into the provided buffer.
            /// It must return `Poll::Pending` if no data is available for reading, or
            /// `Poll::Ready(Ok(num_bytes_read))` otherwise.
            pub fn resume<
                #[cfg(feature = "std")] E: snafu::AsErrorSource,
                #[cfg(not(feature = "std"))] E,
            >(
                &mut self,
                mut read: impl FnMut(&mut [u8]) -> Poll<core::result::Result<usize, E>>
            ) -> Poll<$crate::Result<IncomingPackage, E>> {
                loop {
                    match self {
                        IncomingStateMachine::None => {
                            // Read single byte to identify package
                            let mut code = [0u8];
                            let count = ready!(read(&mut code))?;
                            if count == 0 {
                                return Err(Error::DeviceReadZero).into();
                            }
                            if count > 1 {
                                return Err(
                                    Error::DeviceReadTooMuch { requested: 1, reported: count }
                                ).into();
                            }
                            match code[0] {
                                $(
                                    $code => *self = IncomingStateMachine::$name {
                                        buffer: [0; ($length + 1)],
                                        received_bytes: 0
                                    },
                                )*
                                code => return Err(Error::UnknownTypeCode{ code }).into(),
                            }
                        },
                        $(
                            IncomingStateMachine::$name {
                                ref mut buffer,
                                ref mut received_bytes
                            } => {
                                // Continue reading the required number of bytes for the signaled
                                // package type
                                let slice = &mut buffer[*received_bytes..($length + 1)];
                                let count = ready!(read(slice))?;
                                if count == 0 {
                                    return Err(Error::DeviceReadZero).into();
                                }
                                if count > slice.len() {
                                    return Err(Error::DeviceReadTooMuch {
                                        requested: slice.len(),
                                        reported: count
                                    }).into();
                                }
                                *received_bytes += count;
                                if *received_bytes == ($length + 1) {
                                    // Package received completely, decode
                                    let [high_byte, data @ ..] = *buffer;
                                    let decoded = match decode_high_byte((high_byte, data)){
                                        Ok(decoded) => decoded,
                                        Err(invalid_index) => {
                                            let mut bytes = [0; 8];
                                            bytes[..$length+1].copy_from_slice(buffer);
                                            return Err(Error::InvalidPackageData {
                                                code: $code,
                                                bytes,
                                                length: $length+1,
                                                invalid_index
                                            }).into();
                                        }
                                    };
                                    let data = $name::from_bytes(decoded);

                                    // Reset state machine
                                    *self = IncomingStateMachine::None;

                                    return Poll::Ready(Ok(IncomingPackage::$name(data)))
                                }
                            },
                        )*
                    }
                }
            }
        }
    };
}

incoming_packages! {
    /// Real time data
    0x01 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] RealTimeData {
        /// Signal strength
        pub signal_strength: u8 = get_bit_range(bytes[0], 0..=3),
        /// Searching time too long
        pub searching_time_too_long: bool = get_bit(bytes[0], 4),
        /// Low SpO2
        pub low_spo2: bool = get_bit(bytes[0], 5),
        /// Pulse beep
        pub pulse_beep: bool = get_bit(bytes[0], 6),
        /// Probe errors
        pub probe_errors: bool = get_bit(bytes[0], 7),
        /// Pulse waveform
        pub pulse_waveform: u8 = get_bit_range(bytes[1], 0..=6),
        /// Searching pulse
        pub searching_pulse: bool = get_bit(bytes[1], 7),
        /// Bar graph
        pub bar_graph: u8 = get_bit_range(bytes[2], 0..=3),
        /// PI invalid
        pub pi_invalid: bool = get_bit(bytes[2], 4),
        /// Pulse rate
        pub pulse_rate: u8 = bytes[3],
        /// SpO2
        pub spo2: u8 = bytes[4],
        /// PI
        pub pi: u16 = (bytes[5] as u16) + ((bytes[6] as u16) << 8)
    },
    /// Device identifier
    0x04 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] DeviceIdentifier {
        /// Identifier
        pub identifier: [u8; 7] = bytes,
    },
    /// User Information
    0x05 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] UserInformation {
        /// User Index Number
        pub user_index: u8 = bytes[0],
        /// User Information
        pub user_info: [u8; 6] = [bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6]]
    },
    /// Storage start time(date)
    0x07 => |bytes: [u8; 6]| #[derive(Debug, Copy, Clone)] StorageStartTimeDate {
        /// User Index Number
        pub user_index: u8 = bytes[0],
        /// Storage Segment Number
        pub storage_segment: u8 = bytes[1],
        /// Year
        pub year: u16 = (bytes[2] as u16) + ((bytes[3] as u16) << 8),
        /// Month
        pub month: u8 = bytes[4],
        /// Day
        pub day: u8 = bytes[5],
    },
    /// Storage start time(time)
    0x12 => |bytes: [u8; 6]| #[derive(Debug, Copy, Clone)] StorageStartTimeTime {
        /// User Index Number
        pub user_index: u8 = bytes[0],
        /// Storage Segment Number
        pub storage_segment: u8 = bytes[1],
        /// Hour
        pub hour: u8 = bytes[2],
        /// Minutes
        pub minute: u8 = bytes[3],
        /// Seconds
        pub second: u8 = bytes[4],
    },
    /// Storage Data Length
    0x08 => |bytes: [u8; 6]| #[derive(Debug, Copy, Clone)] StorageDataLength {
        /// User Index Number
        pub user_index: u8 = bytes[0],
        /// Data Segment Number
        pub data_segment: u8 = bytes[1],
        /// Data segment length
        pub length: u32 =
            (bytes[2] as u32) + ((bytes[3] as u32) << 8) + ((bytes[4] as u32) << 16) + ((bytes[5] as u32) << 24),
    },
    /// Storage Data with PI
    0x09 => |bytes: [u8; 4]| #[derive(Debug, Copy, Clone)] StorageDataWithPI {
        /// SpO2
        pub spo2: u8 = bytes[0],
        /// Pulse rate
        pub pulse_rate: u8 = bytes[1],
        /// Perfusion Index
        pub pi: u16 = (bytes[2] as u16) + ((bytes[3] as u16) << 8),
    },
    /// Storage Data Segment Amount
    0x0A => |bytes: [u8; 2]| #[derive(Debug, Copy, Clone)] StorageDataSegmentAmount {
        /// User Index Number
        pub user_index: u8 = bytes[0],
        /// Segment Amount
        pub segment_amount: u8 = bytes[1],
    },
    /// Command Feedback
    0x0B => |bytes: [u8; 2]| CommandFeedback {
        /// Command
        pub command: u8 = bytes[0],
        /// Reason Code
        pub code: u8 = bytes[1],
    },
    /// Device free feedback
    0x0C => |_bytes: [u8; 0]| #[derive(Debug, Copy, Clone)] FreeFeedback {},
    /// Device disconnect notice
    0x0D => |bytes: [u8; 1]| #[derive(Debug, Copy, Clone)] DisconnectNotice {
        /// Disconnect reason
        pub reason: u8 = bytes[0],
    },
    /// PI Identifiers
    0x0E => |bytes: [u8; 1]| #[derive(Debug, Copy, Clone)] PIIdentifiers {
        /// Whether to support PI in real-time data
        pub pi_support: u8 = bytes[0],
    },
    /// Storage Data
    0x0F => |bytes: [u8; 6]| #[derive(Debug, Copy, Clone)] StorageData {
        /// SpO2 entry 1
        pub spo2_1: u8 = bytes[0],
        /// Pulse rate entry 1
        pub pulse_rate_1: u8 = bytes[1],
        /// SpO2 entry 2
        pub spo2_2: u8 = bytes[2],
        /// Pulse rate entry 2
        pub pulse_rate_2: u8 = bytes[3],
        /// SpO2 entry 3
        pub spo2_3: u8 = bytes[4],
        /// Pulse rate entry 3
        pub pulse_rate_3: u8 = bytes[5],
    },
    /// User Amount
    0x10 => |bytes: [u8; 1]| #[derive(Debug, Copy, Clone)] UserAmount {
        /// Total User Number
        pub total_user: u8 = bytes[0],
    },
    /// Device Notice
    0x11 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] DeviceNotice {
        /// Device Notice Type
        pub device_notice: u8 = bytes[0],
        /// Notice Information
        pub device_info: [u8; 6] = [bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6]],
    },
    /// Storage Data Identifiers
    0x15 => |bytes: [u8; 7]| #[derive(Debug, Copy, Clone)] StorageDataIdentifiers {
        /// User Index Number
        pub user_index: u8 = bytes[0],
        /// Data Segment Number
        pub data_segment: u8 = bytes[1],
        /// PI Identifiers
        pub pi_identifiers: u8 = bytes[2],
        /// Retention
        pub retention: [u8; 4] = [bytes[3], bytes[4], bytes[5], bytes[6]],
    },
}

impl CommandFeedback {
    /// Meaning of this device command feedback
    pub fn message(&self) -> &str {
        match self.code {
            0x00 => "Completed operation",
            0x01 => "Shutdown device",
            0x02 => "Exchange users",
            0x03 => "Recording",
            0x04 => "Failure to delete the storage data",
            0x05 => "Not supported",
            _ => "Unknown reason",
        }
    }
}

impl Debug for CommandFeedback {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("CommandFeedback")
            .field("command", &format_args!("{:#04X}", self.command))
            .field("reason_code", &format_args!("{:#04X}", self.code))
            .field("message", &format_args!("'{}'", self.message()))
            .finish()
    }
}