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
/*
Copyright 2020 Timo Saarinen

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//! GNSS data structures

pub(crate) mod gga;
pub(crate) mod gll;
pub(crate) mod gns;
pub(crate) mod gsa;
pub(crate) mod gsv;
pub(crate) mod rmc;
pub(crate) mod vtg;
pub(crate) mod alm;
pub(crate) mod dtm;
pub(crate) mod mss;
pub(crate) mod stn;
pub(crate) mod vbw;
pub(crate) mod zda;
pub(crate) mod dpt;
pub(crate) mod dbs;
pub(crate) mod mtw;
pub(crate) mod vhw;
pub(crate) mod hdt;
pub(crate) mod mwv;

use super::*;
pub use gga::{GgaData, GgaQualityIndicator};
pub use gll::GllData;
pub use gns::GnsData;
pub use gsa::{GsaData, GsaFixMode};
pub use gsv::GsvData;
pub use rmc::RmcData;
pub use vtg::VtgData;
pub use alm::AlmData;
pub use dtm::DtmData;
pub use mss::MssData;
pub use stn::StnData;
pub use vbw::VbwData;
pub use zda::ZdaData;
pub use dpt::DptData;
pub use dbs::DbsData;
pub use mtw::MtwData;
pub use vhw::VhwData;
pub use hdt::HdtData;
pub use mwv::MwvData;

// -------------------------------------------------------------------------------------------------

/// Navigation system, identified with NMEA GNSS sentence prefix (e.g. $BDGGA)
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NavigationSystem {
    /// Combination of several satellite systems
    Combination, // GNxxx

    /// American GPS
    Gps, // GPxxx

    /// Russian GLONASS
    Glonass, // GLxxx

    /// European Galileo
    Galileo, // GAxxx

    // Chinese BeiDou
    Beidou, // BDxxx

    // Indian NavIC
    Navic, // GIxxx

    // Japanese Qzss
    Qzss, // QZxxx

    /// Proprietary manufacturer specific message
    Proprietary, // PMMM, P usually followed by a three character manufacturer code

    // Some other
    Other,
}

impl core::fmt::Display for NavigationSystem {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            NavigationSystem::Combination => write!(f, "combination"),
            NavigationSystem::Gps => write!(f, "GPS"),
            NavigationSystem::Glonass => write!(f, "GLONASS"),
            NavigationSystem::Galileo => write!(f, "Galileo"),
            NavigationSystem::Beidou => write!(f, "BeiDou"),
            NavigationSystem::Navic => write!(f, "Navic"),
            NavigationSystem::Qzss => write!(f, "QZSS"),
            NavigationSystem::Proprietary => write!(f, "proprietary"),
            NavigationSystem::Other => write!(f, "other"),
        }
    }
}

impl core::str::FromStr for NavigationSystem {
    type Err = ParseError;

    fn from_str(talker_id: &str) -> Result<Self, Self::Err> {
        if talker_id.len() < 1 {
            return Err(ParseError::InvalidSentence(
                "Invalid talker identifier".to_string(),
            ));
        }
        if &talker_id[0..1] == "P" {
            Ok(Self::Proprietary)
        } else {
            if talker_id.len() < 2 {
                return Err(ParseError::InvalidSentence(
                    "Invalid talker identifier".to_string(),
                ));
            }
            match &talker_id[0..2] {
                "GN" => Ok(Self::Combination),
                "GP" => Ok(Self::Gps),
                "GL" => Ok(Self::Glonass),
                "GA" => Ok(Self::Galileo),
                "BD" => Ok(Self::Beidou),
                "GI" => Ok(Self::Navic),
                "QZ" => Ok(Self::Qzss),
                _ => Ok(Self::Other),
            }
        }
    }
}

// -------------------------------------------------------------------------------------------------
/// VTG/GLL FAA mode (NMEA 2.3 standard has this information)
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum FaaMode {
    /// Autonomous mode (automatic 2D/3D)
    Autonomous,

    /// Differential GPS mode (DGPS).
    Differential,

    /// Estimated (dead-reckoning) data.
    Estimated,

    /// No valid data available.
    NotValid,

    /// Simulated data.
    Simulator,
}

impl FaaMode {
    pub fn new(val: &str) -> Result<FaaMode, String> {
        match val {
            "A" => Ok(FaaMode::Autonomous),
            "D" => Ok(FaaMode::Differential),
            "E" => Ok(FaaMode::Estimated),
            "N" => Ok(FaaMode::NotValid),
            _ => Err(format!("Unrecognized FAA information value: {}", val)),
        }
    }
}

impl core::fmt::Display for FaaMode {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            FaaMode::Autonomous => write!(f, "A"),
            FaaMode::Differential => write!(f, "D"),
            FaaMode::Estimated => write!(f, "E"),
            FaaMode::NotValid => write!(f, "N"),
            _ => write!(f, "?"),
        }
    }
}