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
//! Conversions from messages to types in `swiftnav`.

use std::{
    convert::{TryFrom, TryInto},
    fmt, num,
};

use crate::messages;

#[derive(Debug, Clone)]
pub enum GpsTimeError {
    InvalidGpsTime(swiftnav::time::InvalidGpsTime),
    TryFromIntError(num::TryFromIntError),
}

impl fmt::Display for GpsTimeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            GpsTimeError::InvalidGpsTime(e) => e.fmt(f),
            GpsTimeError::TryFromIntError(e) => e.fmt(f),
        }
    }
}

impl std::error::Error for GpsTimeError {}

impl From<swiftnav::time::InvalidGpsTime> for GpsTimeError {
    fn from(e: swiftnav::time::InvalidGpsTime) -> Self {
        GpsTimeError::InvalidGpsTime(e)
    }
}

impl From<num::TryFromIntError> for GpsTimeError {
    fn from(e: num::TryFromIntError) -> Self {
        GpsTimeError::TryFromIntError(e)
    }
}

impl From<std::convert::Infallible> for GpsTimeError {
    fn from(_: std::convert::Infallible) -> Self {
        unreachable!()
    }
}

impl TryFrom<messages::gnss::GpsTime> for swiftnav::time::GpsTime {
    type Error = swiftnav::time::InvalidGpsTime;

    fn try_from(
        msg: messages::gnss::GpsTime,
    ) -> Result<swiftnav::time::GpsTime, swiftnav::time::InvalidGpsTime> {
        let tow = (msg.tow as f64) * 1e-3 + (msg.ns_residual as f64) * 1e-9;
        swiftnav::time::GpsTime::new(msg.wn as i16, tow)
    }
}

impl TryFrom<messages::gnss::GpsTimeSec> for swiftnav::time::GpsTime {
    type Error = swiftnav::time::InvalidGpsTime;

    fn try_from(
        msg: messages::gnss::GpsTimeSec,
    ) -> Result<swiftnav::time::GpsTime, swiftnav::time::InvalidGpsTime> {
        swiftnav::time::GpsTime::new(msg.wn as i16, msg.tow as f64)
    }
}

impl TryFrom<messages::gnss::GnssSignal> for swiftnav::signal::GnssSignal {
    type Error = swiftnav::signal::InvalidGnssSignal;

    fn try_from(
        value: messages::gnss::GnssSignal,
    ) -> Result<swiftnav::signal::GnssSignal, swiftnav::signal::InvalidGnssSignal> {
        swiftnav::signal::GnssSignal::new(value.sat as u16, value.code.try_into()?)
    }
}

impl TryFrom<messages::observation::MsgEphemerisGps> for swiftnav::ephemeris::Ephemeris {
    type Error = EphemerisDecodeError;

    fn try_from(
        eph: messages::observation::MsgEphemerisGps,
    ) -> Result<swiftnav::ephemeris::Ephemeris, EphemerisDecodeError> {
        Ok(swiftnav::ephemeris::Ephemeris::new(
            eph.common.sid.try_into()?,
            eph.common.toe.try_into()?,
            eph.common.ura,
            eph.common.fit_interval,
            eph.common.valid,
            eph.common.health_bits,
            0,
            swiftnav::ephemeris::EphemerisTerms::new_kepler(
                swiftnav::signal::Constellation::Gps,
                [eph.tgd, 0.],
                eph.c_rc as f64,
                eph.c_rs as f64,
                eph.c_uc as f64,
                eph.c_us as f64,
                eph.c_ic as f64,
                eph.c_is as f64,
                eph.dn,
                eph.m0,
                eph.ecc,
                eph.sqrta,
                eph.omega0,
                eph.omegadot,
                eph.w,
                eph.inc,
                eph.inc_dot,
                eph.af0 as f64,
                eph.af1 as f64,
                eph.af2 as f64,
                eph.toc.try_into()?,
                eph.iodc,
                eph.iode as u16,
            ),
        ))
    }
}

impl TryFrom<messages::observation::MsgEphemerisGal> for swiftnav::ephemeris::Ephemeris {
    type Error = EphemerisDecodeError;

    fn try_from(
        eph: messages::observation::MsgEphemerisGal,
    ) -> Result<swiftnav::ephemeris::Ephemeris, EphemerisDecodeError> {
        Ok(swiftnav::ephemeris::Ephemeris::new(
            eph.common.sid.try_into()?,
            eph.common.toe.try_into()?,
            eph.common.ura,
            eph.common.fit_interval,
            eph.common.valid,
            eph.common.health_bits,
            eph.source,
            swiftnav::ephemeris::EphemerisTerms::new_kepler(
                swiftnav::signal::Constellation::Gal,
                [eph.bgd_e1e5a, eph.bgd_e1e5b],
                eph.c_rc as f64,
                eph.c_rs as f64,
                eph.c_uc as f64,
                eph.c_us as f64,
                eph.c_ic as f64,
                eph.c_is as f64,
                eph.dn,
                eph.m0,
                eph.ecc,
                eph.sqrta,
                eph.omega0,
                eph.omegadot,
                eph.w,
                eph.inc,
                eph.inc_dot,
                eph.af0,
                eph.af1,
                eph.af2 as f64,
                eph.toc.try_into()?,
                eph.iodc,
                eph.iode,
            ),
        ))
    }
}

#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
pub enum EphemerisDecodeError {
    InvalidTime(swiftnav::time::InvalidGpsTime),
    InvalidSignal(swiftnav::signal::InvalidGnssSignal),
}

impl fmt::Display for EphemerisDecodeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EphemerisDecodeError::InvalidTime(e) => e.fmt(f),
            EphemerisDecodeError::InvalidSignal(e) => e.fmt(f),
        }
    }
}

impl std::error::Error for EphemerisDecodeError {}

impl From<swiftnav::time::InvalidGpsTime> for EphemerisDecodeError {
    fn from(e: swiftnav::time::InvalidGpsTime) -> Self {
        EphemerisDecodeError::InvalidTime(e)
    }
}

impl From<swiftnav::signal::InvalidGnssSignal> for EphemerisDecodeError {
    fn from(e: swiftnav::signal::InvalidGnssSignal) -> Self {
        EphemerisDecodeError::InvalidSignal(e)
    }
}

impl TryFrom<messages::observation::PackedObsContent> for swiftnav::navmeas::NavigationMeasurement {
    type Error = swiftnav::signal::InvalidGnssSignal;

    fn try_from(
        observation: messages::observation::PackedObsContent,
    ) -> Result<swiftnav::navmeas::NavigationMeasurement, swiftnav::signal::InvalidGnssSignal> {
        let mut measurement = swiftnav::navmeas::NavigationMeasurement::new();

        measurement.set_lock_time(swiftnav::navmeas::decode_lock_time(observation.lock));
        measurement.set_sid(observation.sid.try_into()?);
        // A CN0 of 0 is considered invalid
        if observation.cn0 != 0 {
            measurement.set_cn0(observation.cn0 as f64 / 4.);
        }
        if observation.flags & 0x01 != 0 {
            measurement.set_pseudorange(observation.p as f64 / 5e1);
        }
        if observation.flags & 0x08 != 0 {
            measurement
                .set_measured_doppler(observation.d.i as f64 + (observation.d.f as f64) / 256.);
        }
        if observation.flags & 0x80 != 0 {
            measurement
                .set_flags(measurement.flags() | swiftnav::navmeas::NAV_MEAS_FLAG_RAIM_EXCLUSION);
        }

        Ok(measurement)
    }
}