divecomputer-core 0.1.0

Shared building blocks for dive computer protocols: checksums, framing and the dive data model
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
//! The dive data model ([spec](https://michidk.github.io/divecomputer-rs/spec/common/data-model.html)).
//!
//! A download yields [`RawDive`]s; a family's parser turns one into a [`Dive`]. The model
//! mirrors one DiveJSON *recording*: header values, gas mixes, tanks, samples and events
//! from one device.

use alloc::{string::String, vec::Vec};
use core::time::Duration;

use crate::units::{Depth, GasFraction, Pressure, Temperature};

/// A dive as downloaded, before parsing: the bytes exactly as the device sent them, and
/// the fingerprint that identifies the dive for incremental downloads.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct RawDive {
    fingerprint: Vec<u8>,
    data: Vec<u8>,
}

impl RawDive {
    /// Creates a raw dive.
    #[must_use]
    pub const fn new(fingerprint: Vec<u8>, data: Vec<u8>) -> Self {
        Self { fingerprint, data }
    }

    /// The bytes that identify this dive on its device. Store the newest dive's
    /// fingerprint and pass it to the next download to fetch only newer dives.
    #[must_use]
    pub fn fingerprint(&self) -> &[u8] {
        &self.fingerprint
    }

    /// The dive's raw bytes, in the family's log format.
    #[must_use]
    pub fn data(&self) -> &[u8] {
        &self.data
    }
}

/// A parsed dive: one device's recording of one dive.
///
/// Every field is optional or may be empty: a parser fills what the device recorded and
/// leaves the rest absent, never zero-filled. Values are in the units of
/// [`crate::units`]; times are elapsed from the dive start.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Dive {
    /// The dive start on the device's local wall clock. Combine with
    /// [`utc_offset`](Self::utc_offset) when that is known; never convert through the
    /// host's time zone.
    pub start: Option<jiff::civil::DateTime>,
    /// Offset of [`start`](Self::start) from UTC in seconds, only when the device
    /// recorded its time zone.
    pub utc_offset: Option<i32>,
    /// The device's own dive counter.
    pub number: Option<u32>,
    /// Dive time as stored by the device. May differ from the last sample time.
    #[cfg_attr(feature = "serde", serde(with = "millis_opt"))]
    pub duration: Option<Duration>,
    /// Maximum depth as stored by the device, not recomputed from samples.
    pub max_depth: Option<Depth>,
    /// Average depth as stored by the device.
    pub avg_depth: Option<Depth>,
    /// Lowest water temperature as stored by the device.
    pub min_temperature: Option<Temperature>,
    /// Highest water temperature as stored by the device.
    pub max_temperature: Option<Temperature>,
    /// The mode the device ran in.
    pub mode: Option<DiveMode>,
    /// The water type the device was set to.
    pub salinity: Option<Salinity>,
    /// The water density the device was set to, in kg/m³.
    pub water_density: Option<f64>,
    /// The surface (atmospheric) pressure the device measured or assumed.
    pub surface_pressure: Option<Pressure>,
    /// The decompression model the device ran, with its settings.
    pub deco_model: Option<DecoModel>,
    /// Surface position at the start of the dive.
    pub location: Option<Location>,
    /// The device that recorded the dive.
    pub device: DeviceInfo,
    /// Gas mixes; [`Sample`]s and [`Event`]s refer to them by index.
    pub gas_mixes: Vec<GasMix>,
    /// Tanks; [`TankPressure`]s refer to them by index.
    pub tanks: Vec<Tank>,
    /// Samples in time order.
    pub samples: Vec<Sample>,
    /// Events in time order. An event may fall after the last sample.
    pub events: Vec<Event>,
}

/// Identification of the device that recorded a dive. Strings are as the device
/// reports them.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct DeviceInfo {
    /// The maker, for example `Shearwater`.
    pub brand: Option<String>,
    /// The product name, for example `Perdix 2`.
    pub model: Option<String>,
    /// The device's serial number in its own rendering.
    pub serial: Option<String>,
    /// The firmware version the device was running.
    pub firmware: Option<String>,
}

/// The mode a dive computer ran in.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(rename_all = "snake_case")
)]
#[non_exhaustive]
pub enum DiveMode {
    /// Open circuit.
    OpenCircuit,
    /// Closed-circuit rebreather.
    ClosedCircuit,
    /// Semi-closed rebreather.
    SemiClosed,
    /// Depth and time only, no decompression calculation.
    Gauge,
    /// Breath-hold diving.
    Freedive,
}

/// The water type a device converts pressure to depth with.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(rename_all = "snake_case")
)]
#[non_exhaustive]
pub enum Salinity {
    /// Fresh water.
    Fresh,
    /// The EN 13319 convention (1020 kg/m³).
    En13319,
    /// Salt water.
    Salt,
}

/// A decompression model and the settings it ran with.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(tag = "algorithm", rename_all = "snake_case")
)]
#[non_exhaustive]
pub enum DecoModel {
    /// Bühlmann with gradient factors.
    Buhlmann {
        /// Gradient factor low, percent.
        gf_low: u8,
        /// Gradient factor high, percent.
        gf_high: u8,
    },
    /// VPM-B, optionally combined with a Bühlmann gradient factor (VPM-B/GFS).
    Vpm {
        /// Conservatism on the device's own scale.
        conservatism: u8,
        /// The GFS gradient factor in percent, for VPM-B/GFS.
        gfs_gradient_factor: Option<u8>,
    },
    /// DCIEM.
    Dciem,
}

/// A position in decimal degrees (WGS 84).
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Location {
    /// Latitude, −90 to 90.
    pub latitude: f64,
    /// Longitude, −180 to 180.
    pub longitude: f64,
}

impl Location {
    /// A position in decimal degrees.
    #[must_use]
    pub const fn new(latitude: f64, longitude: f64) -> Self {
        Self {
            latitude,
            longitude,
        }
    }
}

/// A breathing gas.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct GasMix {
    /// Oxygen share.
    pub oxygen: GasFraction,
    /// Helium share; 0 for nitrox and air.
    pub helium: GasFraction,
    /// What the mix is used for, when the device says.
    pub usage: Option<GasUsage>,
}

impl GasMix {
    /// A mix of oxygen and helium, the rest nitrogen.
    #[must_use]
    pub const fn new(oxygen: GasFraction, helium: GasFraction, usage: Option<GasUsage>) -> Self {
        Self {
            oxygen,
            helium,
            usage,
        }
    }
}

/// What a gas or tank is used for.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(rename_all = "snake_case")
)]
#[non_exhaustive]
pub enum GasUsage {
    /// Breathed open circuit.
    OpenCircuit,
    /// Rebreather diluent.
    Diluent,
    /// Rebreather oxygen.
    Oxygen,
}

/// A tank, usually one monitored by a wireless pressure transmitter.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Tank {
    /// The device's name for the tank or transmitter.
    pub name: Option<String>,
    /// The transmitter's serial number in the device's rendering.
    pub transmitter: Option<String>,
    /// What the tank is used for.
    pub usage: Option<GasUsage>,
    /// Index into [`Dive::gas_mixes`].
    pub gas_mix: Option<usize>,
    /// Pressure at the start of the dive.
    pub begin_pressure: Option<Pressure>,
    /// Pressure at the end of the dive.
    pub end_pressure: Option<Pressure>,
}

/// The values a device recorded at one instant. Absent values were not recorded.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Sample {
    /// Elapsed time since the dive start.
    #[cfg_attr(feature = "serde", serde(with = "millis"))]
    pub time: Duration,
    /// Depth.
    pub depth: Option<Depth>,
    /// Water temperature.
    pub temperature: Option<Temperature>,
    /// Tank pressures, one entry per tank that reported.
    pub pressures: Vec<TankPressure>,
    /// The ppO₂ the device computed or voted from its sensors.
    pub ppo2: Option<Pressure>,
    /// ppO₂ of each oxygen sensor, by sensor number (0 = first); `None` for a sensor
    /// without a usable reading.
    pub ppo2_sensors: Vec<Option<Pressure>>,
    /// Rebreather setpoint.
    pub setpoint: Option<Pressure>,
    /// CNS oxygen toxicity, in percent.
    pub cns: Option<f64>,
    /// No-decompression limit or the next decompression stop.
    pub deco: Option<Deco>,
    /// Time to surface.
    #[cfg_attr(feature = "serde", serde(with = "millis_opt"))]
    pub time_to_surface: Option<Duration>,
    /// Remaining gas time from air integration.
    #[cfg_attr(feature = "serde", serde(with = "millis_opt"))]
    pub gas_time_remaining: Option<Duration>,
    /// Gradient factor of the leading tissue (GF99), in percent.
    pub gradient_factor: Option<u8>,
}

/// A tank pressure reading.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct TankPressure {
    /// Index into [`Dive::tanks`].
    pub tank: usize,
    /// The pressure.
    pub pressure: Pressure,
}

impl TankPressure {
    /// A reading for tank `tank`.
    #[must_use]
    pub const fn new(tank: usize, pressure: Pressure) -> Self {
        Self { tank, pressure }
    }
}

/// Decompression state at a sample.
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(tag = "kind", rename_all = "snake_case")
)]
#[non_exhaustive]
pub enum Deco {
    /// No decompression obligation; `time` is the remaining no-decompression time.
    Ndl {
        /// Remaining no-decompression time.
        #[cfg_attr(feature = "serde", serde(with = "millis"))]
        time: Duration,
    },
    /// A decompression stop is required.
    Stop {
        /// Depth of the next stop.
        depth: Depth,
        /// Time at that stop.
        #[cfg_attr(feature = "serde", serde(with = "millis"))]
        time: Duration,
    },
}

/// Something that happened at one instant of the dive.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub struct Event {
    /// Elapsed time since the dive start.
    #[cfg_attr(feature = "serde", serde(with = "millis"))]
    pub time: Duration,
    /// What happened.
    pub kind: EventKind,
}

impl Event {
    /// An event at `time`.
    #[must_use]
    pub const fn new(time: Duration, kind: EventKind) -> Self {
        Self { time, kind }
    }
}

/// The kinds of [`Event`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize),
    serde(tag = "type", rename_all = "snake_case")
)]
#[non_exhaustive]
pub enum EventKind {
    /// The breathing gas changed.
    GasSwitch {
        /// Index into [`Dive::gas_mixes`].
        gas_mix: usize,
    },
    /// The diver marked this moment.
    Bookmark {
        /// Compass heading in degrees, when recorded.
        heading: Option<u32>,
    },
    /// A device event with no common meaning.
    Vendor {
        /// The device's event code.
        code: u32,
        /// The event's first parameter.
        value: u32,
    },
}

/// Serializes a [`Duration`] as whole milliseconds.
#[cfg(feature = "serde")]
mod millis {
    use core::time::Duration;

    pub(super) fn serialize<S: serde::Serializer>(
        duration: &Duration,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.serialize_u64(u64::try_from(duration.as_millis()).unwrap_or(u64::MAX))
    }
}

/// Serializes an optional [`Duration`] as whole milliseconds.
#[cfg(feature = "serde")]
mod millis_opt {
    use core::time::Duration;

    #[allow(clippy::ref_option, reason = "signature required by serde(with)")]
    pub(super) fn serialize<S: serde::Serializer>(
        duration: &Option<Duration>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        match duration {
            Some(duration) => super::millis::serialize(duration, serializer),
            None => serializer.serialize_none(),
        }
    }
}