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
// Copyright (C) 2015-2021 Swift Navigation Inc.
// Contact: https://support.swiftnav.com
//
// This source is subject to the license found in the file 'LICENSE' which must
// be be distributed together with this source. All other rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

//****************************************************************************
// Automatically generated from yaml/swiftnav/sbp/ndb.yaml
// with generate.py. Please do not hand edit!
//****************************************************************************/
//! Messages for logging NDB events.
pub use msg_ndb_event::MsgNdbEvent;

pub mod msg_ndb_event {
    #![allow(unused_imports)]

    use super::*;
    use crate::messages::gnss::*;
    use crate::messages::lib::*;

    /// Navigation DataBase Event
    ///
    /// This message is sent out when an object is stored into NDB. If needed
    /// message could also be sent out when fetching an object from NDB.
    ///
    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
    #[derive(Debug, Clone)]
    pub struct MsgNdbEvent {
        /// The message sender_id
        #[cfg_attr(feature = "serde", serde(skip_serializing))]
        pub sender_id: Option<u16>,
        /// HW time in milliseconds.
        #[cfg_attr(feature = "serde", serde(rename(serialize = "recv_time")))]
        pub recv_time: u64,
        /// Event type.
        #[cfg_attr(feature = "serde", serde(rename(serialize = "event")))]
        pub event: u8,
        /// Event object type.
        #[cfg_attr(feature = "serde", serde(rename(serialize = "object_type")))]
        pub object_type: u8,
        /// Event result.
        #[cfg_attr(feature = "serde", serde(rename(serialize = "result")))]
        pub result: u8,
        /// Data source for STORE event, reserved for other events.
        #[cfg_attr(feature = "serde", serde(rename(serialize = "data_source")))]
        pub data_source: u8,
        /// GNSS signal identifier, If object_type is Ephemeris OR Almanac, sid
        /// indicates for which signal the object belongs to. Reserved in other
        /// cases.
        #[cfg_attr(feature = "serde", serde(rename(serialize = "object_sid")))]
        pub object_sid: GnssSignal,
        /// GNSS signal identifier, If object_type is Almanac, Almanac WN, Iono OR
        /// L2C capabilities AND data_source is NDB_DS_RECEIVER sid indicates from
        /// which SV data was decoded. Reserved in other cases.
        #[cfg_attr(feature = "serde", serde(rename(serialize = "src_sid")))]
        pub src_sid: GnssSignal,
        /// A unique identifier of the sending hardware. For v1.0, set to the 2
        /// least significant bytes of the device serial number, valid only if
        /// data_source is NDB_DS_SBP. Reserved in case of other data_source.
        #[cfg_attr(feature = "serde", serde(rename(serialize = "original_sender")))]
        pub original_sender: u16,
    }

    impl MsgNdbEvent {
        /// Gets the [EventType][self::EventType] stored in the `event` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `EventType` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `EventType` were added.
        pub fn event_type(&self) -> Result<EventType, u8> {
            get_bit_range!(self.event, u8, u8, 1, 0).try_into()
        }

        /// Set the bitrange corresponding to the [EventType][EventType] of the `event` bitfield.
        pub fn set_event_type(&mut self, event_type: EventType) {
            set_bit_range!(&mut self.event, event_type, u8, u8, 1, 0);
        }

        /// Gets the [EventObjectType][self::EventObjectType] stored in the `object_type` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `EventObjectType` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `EventObjectType` were added.
        pub fn event_object_type(&self) -> Result<EventObjectType, u8> {
            get_bit_range!(self.object_type, u8, u8, 2, 0).try_into()
        }

        /// Set the bitrange corresponding to the [EventObjectType][EventObjectType] of the `object_type` bitfield.
        pub fn set_event_object_type(&mut self, event_object_type: EventObjectType) {
            set_bit_range!(&mut self.object_type, event_object_type, u8, u8, 2, 0);
        }

        /// Gets the [EventResult][self::EventResult] stored in the `result` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `EventResult` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `EventResult` were added.
        pub fn event_result(&self) -> Result<EventResult, u8> {
            get_bit_range!(self.result, u8, u8, 3, 0).try_into()
        }

        /// Set the bitrange corresponding to the [EventResult][EventResult] of the `result` bitfield.
        pub fn set_event_result(&mut self, event_result: EventResult) {
            set_bit_range!(&mut self.result, event_result, u8, u8, 3, 0);
        }

        /// Gets the [DataSource][self::DataSource] stored in the `data_source` bitfield.
        ///
        /// Returns `Ok` if the bitrange contains a known `DataSource` variant.
        /// Otherwise the value of the bitrange is returned as an `Err(u8)`. This may be because of a malformed message,
        /// or because new variants of `DataSource` were added.
        pub fn data_source(&self) -> Result<DataSource, u8> {
            get_bit_range!(self.data_source, u8, u8, 1, 0).try_into()
        }

        /// Set the bitrange corresponding to the [DataSource][DataSource] of the `data_source` bitfield.
        pub fn set_data_source(&mut self, data_source: DataSource) {
            set_bit_range!(&mut self.data_source, data_source, u8, u8, 1, 0);
        }
    }

    impl ConcreteMessage for MsgNdbEvent {
        const MESSAGE_TYPE: u16 = 1024;
        const MESSAGE_NAME: &'static str = "MSG_NDB_EVENT";
    }

    impl SbpMessage for MsgNdbEvent {
        fn message_name(&self) -> &'static str {
            <Self as ConcreteMessage>::MESSAGE_NAME
        }
        fn message_type(&self) -> u16 {
            <Self as ConcreteMessage>::MESSAGE_TYPE
        }
        fn sender_id(&self) -> Option<u16> {
            self.sender_id
        }
        fn set_sender_id(&mut self, new_id: u16) {
            self.sender_id = Some(new_id);
        }
        fn encoded_len(&self) -> usize {
            WireFormat::len(self) + crate::HEADER_LEN + crate::CRC_LEN
        }
    }

    impl TryFrom<Sbp> for MsgNdbEvent {
        type Error = TryFromSbpError;
        fn try_from(msg: Sbp) -> Result<Self, Self::Error> {
            match msg {
                Sbp::MsgNdbEvent(m) => Ok(m),
                _ => Err(TryFromSbpError),
            }
        }
    }

    impl WireFormat for MsgNdbEvent {
        const MIN_LEN: usize = <u64 as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN
            + <u8 as WireFormat>::MIN_LEN
            + <GnssSignal as WireFormat>::MIN_LEN
            + <GnssSignal as WireFormat>::MIN_LEN
            + <u16 as WireFormat>::MIN_LEN;
        fn len(&self) -> usize {
            WireFormat::len(&self.recv_time)
                + WireFormat::len(&self.event)
                + WireFormat::len(&self.object_type)
                + WireFormat::len(&self.result)
                + WireFormat::len(&self.data_source)
                + WireFormat::len(&self.object_sid)
                + WireFormat::len(&self.src_sid)
                + WireFormat::len(&self.original_sender)
        }
        fn write<B: BufMut>(&self, buf: &mut B) {
            WireFormat::write(&self.recv_time, buf);
            WireFormat::write(&self.event, buf);
            WireFormat::write(&self.object_type, buf);
            WireFormat::write(&self.result, buf);
            WireFormat::write(&self.data_source, buf);
            WireFormat::write(&self.object_sid, buf);
            WireFormat::write(&self.src_sid, buf);
            WireFormat::write(&self.original_sender, buf);
        }
        fn parse_unchecked<B: Buf>(buf: &mut B) -> Self {
            MsgNdbEvent {
                sender_id: None,
                recv_time: WireFormat::parse_unchecked(buf),
                event: WireFormat::parse_unchecked(buf),
                object_type: WireFormat::parse_unchecked(buf),
                result: WireFormat::parse_unchecked(buf),
                data_source: WireFormat::parse_unchecked(buf),
                object_sid: WireFormat::parse_unchecked(buf),
                src_sid: WireFormat::parse_unchecked(buf),
                original_sender: WireFormat::parse_unchecked(buf),
            }
        }
    }

    /// Event type.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum EventType {
        /// UNKNOWN
        UNKNOWN = 0,

        /// STORE
        STORE = 1,

        /// FETCH
        FETCH = 2,

        /// ERASE
        ERASE = 3,
    }

    impl std::fmt::Display for EventType {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                EventType::UNKNOWN => f.write_str("UNKNOWN"),
                EventType::STORE => f.write_str("STORE"),
                EventType::FETCH => f.write_str("FETCH"),
                EventType::ERASE => f.write_str("ERASE"),
            }
        }
    }

    impl TryFrom<u8> for EventType {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(EventType::UNKNOWN),
                1 => Ok(EventType::STORE),
                2 => Ok(EventType::FETCH),
                3 => Ok(EventType::ERASE),
                i => Err(i),
            }
        }
    }

    /// Event object type.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum EventObjectType {
        /// UNKNOWN
        UNKNOWN = 0,

        /// EPHEMERIS
        EPHEMERIS = 1,

        /// ALMANAC
        ALMANAC = 2,

        /// ALMANAC_WN
        AlmanacWn = 3,

        /// IONO
        IoNO = 4,

        /// L2C_CAP
        L2CCap = 5,

        /// LGF
        LGF = 6,
    }

    impl std::fmt::Display for EventObjectType {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                EventObjectType::UNKNOWN => f.write_str("UNKNOWN"),
                EventObjectType::EPHEMERIS => f.write_str("EPHEMERIS"),
                EventObjectType::ALMANAC => f.write_str("ALMANAC"),
                EventObjectType::AlmanacWn => f.write_str("ALMANAC_WN"),
                EventObjectType::IoNO => f.write_str("IONO"),
                EventObjectType::L2CCap => f.write_str("L2C_CAP"),
                EventObjectType::LGF => f.write_str("LGF"),
            }
        }
    }

    impl TryFrom<u8> for EventObjectType {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(EventObjectType::UNKNOWN),
                1 => Ok(EventObjectType::EPHEMERIS),
                2 => Ok(EventObjectType::ALMANAC),
                3 => Ok(EventObjectType::AlmanacWn),
                4 => Ok(EventObjectType::IoNO),
                5 => Ok(EventObjectType::L2CCap),
                6 => Ok(EventObjectType::LGF),
                i => Err(i),
            }
        }
    }

    /// Event result.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum EventResult {
        /// NDB_ERR_NONE
        NdbErrNone = 0,

        /// NDB_ERR_MISSING_IE
        NdbErrMissingIe = 1,

        /// NDB_ERR_UNSUPPORTED
        NdbErrUnsupported = 2,

        /// NDB_ERR_FILE_IO
        NdbErrFileIo = 3,

        /// NDB_ERR_INIT_DONE
        NdbErrInitDone = 4,

        /// NDB_ERR_BAD_PARAM
        NdbErrBadParam = 5,

        /// NDB_ERR_UNRELIABLE_DATA
        NdbErrUnreliableData = 6,

        /// NDB_ERR_ALGORITHM_ERROR
        NdbErrAlgorithmError = 7,

        /// NDB_ERR_NO_DATA
        NdbErrNoData = 8,

        /// NDB_ERR_NO_CHANGE
        NdbErrNoChange = 9,

        /// NDB_ERR_OLDER_DATA
        NdbErrOlderData = 10,
    }

    impl std::fmt::Display for EventResult {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                EventResult::NdbErrNone => f.write_str("NDB_ERR_NONE"),
                EventResult::NdbErrMissingIe => f.write_str("NDB_ERR_MISSING_IE"),
                EventResult::NdbErrUnsupported => f.write_str("NDB_ERR_UNSUPPORTED"),
                EventResult::NdbErrFileIo => f.write_str("NDB_ERR_FILE_IO"),
                EventResult::NdbErrInitDone => f.write_str("NDB_ERR_INIT_DONE"),
                EventResult::NdbErrBadParam => f.write_str("NDB_ERR_BAD_PARAM"),
                EventResult::NdbErrUnreliableData => f.write_str("NDB_ERR_UNRELIABLE_DATA"),
                EventResult::NdbErrAlgorithmError => f.write_str("NDB_ERR_ALGORITHM_ERROR"),
                EventResult::NdbErrNoData => f.write_str("NDB_ERR_NO_DATA"),
                EventResult::NdbErrNoChange => f.write_str("NDB_ERR_NO_CHANGE"),
                EventResult::NdbErrOlderData => f.write_str("NDB_ERR_OLDER_DATA"),
            }
        }
    }

    impl TryFrom<u8> for EventResult {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(EventResult::NdbErrNone),
                1 => Ok(EventResult::NdbErrMissingIe),
                2 => Ok(EventResult::NdbErrUnsupported),
                3 => Ok(EventResult::NdbErrFileIo),
                4 => Ok(EventResult::NdbErrInitDone),
                5 => Ok(EventResult::NdbErrBadParam),
                6 => Ok(EventResult::NdbErrUnreliableData),
                7 => Ok(EventResult::NdbErrAlgorithmError),
                8 => Ok(EventResult::NdbErrNoData),
                9 => Ok(EventResult::NdbErrNoChange),
                10 => Ok(EventResult::NdbErrOlderData),
                i => Err(i),
            }
        }
    }

    /// Data source.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum DataSource {
        /// NDB_DS_UNDEFINED
        NdbDsUndefined = 0,

        /// NDB_DS_INIT
        NdbDsInit = 1,

        /// NDB_DS_RECEIVER
        NdbDsReceiver = 2,

        /// NDB_DS_SBP
        NdbDsSbp = 3,
    }

    impl std::fmt::Display for DataSource {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            match self {
                DataSource::NdbDsUndefined => f.write_str("NDB_DS_UNDEFINED"),
                DataSource::NdbDsInit => f.write_str("NDB_DS_INIT"),
                DataSource::NdbDsReceiver => f.write_str("NDB_DS_RECEIVER"),
                DataSource::NdbDsSbp => f.write_str("NDB_DS_SBP"),
            }
        }
    }

    impl TryFrom<u8> for DataSource {
        type Error = u8;
        fn try_from(i: u8) -> Result<Self, Self::Error> {
            match i {
                0 => Ok(DataSource::NdbDsUndefined),
                1 => Ok(DataSource::NdbDsInit),
                2 => Ok(DataSource::NdbDsReceiver),
                3 => Ok(DataSource::NdbDsSbp),
                i => Err(i),
            }
        }
    }
}