rtime-refclock 0.14.0

Reference clock drivers (GPS/NMEA, PPS pulse-per-second) for the rTime NTP/PTP time synchronization daemon
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use crate::RefClockError;
use rtime_core::clock::LeapIndicator;
use rtime_core::source::{SourceId, SourceMeasurement};
use rtime_core::timestamp::{NtpDuration, NtpTimestamp};

/// GPS time fix extracted from NMEA sentences.
#[derive(Debug, Clone)]
pub struct GpsFix {
    /// UTC hours from GPS.
    pub hours: u8,
    /// UTC minutes from GPS.
    pub minutes: u8,
    /// UTC seconds from GPS.
    pub seconds: u8,
    /// Sub-second milliseconds from GPS.
    pub subsec_ms: u16,
    /// Day of month.
    pub day: u8,
    /// Month (1-12).
    pub month: u8,
    /// Full year (e.g. 2026).
    pub year: u16,
    /// Whether the fix is valid.
    pub valid: bool,
}

impl GpsFix {
    /// Convert this GPS fix to an NTP timestamp.
    ///
    /// Returns `None` if the fix is not valid.
    pub fn to_ntp_timestamp(&self) -> Option<NtpTimestamp> {
        if !self.valid {
            return None;
        }

        // Compute days from NTP epoch (1900-01-01) to this date.
        let days = days_from_ntp_epoch(self.year, self.month, self.day)?;

        let seconds_in_day =
            self.hours as u64 * 3600 + self.minutes as u64 * 60 + self.seconds as u64;
        let total_seconds = days * 86400 + seconds_in_day;

        // NTP timestamp: upper 32 bits = seconds since 1900-01-01
        let ntp_seconds = total_seconds as u32; // wraps at 2036, same as NTP era 0
        let fraction = (self.subsec_ms as u64 * (1u64 << 32)) / 1000;

        Some(NtpTimestamp::new(ntp_seconds, fraction as u32))
    }
}

/// Compute number of days from NTP epoch (1900-01-01) to a given date.
fn days_from_ntp_epoch(year: u16, month: u8, day: u8) -> Option<u64> {
    if !(1..=12).contains(&month) || !(1..=31).contains(&day) || year < 1900 {
        return None;
    }

    let mut total_days: u64 = 0;

    // Add days for full years from 1900 to year-1.
    for y in 1900..year {
        total_days += if is_leap_year(y) { 366 } else { 365 };
    }

    // Add days for full months in the current year.
    let days_in_months: [u8; 12] = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    for m in 1..month {
        let mut d = days_in_months[(m - 1) as usize] as u64;
        if m == 2 && is_leap_year(year) {
            d = 29;
        }
        total_days += d;
    }

    total_days += (day - 1) as u64;

    Some(total_days)
}

fn is_leap_year(y: u16) -> bool {
    (y.is_multiple_of(4) && !y.is_multiple_of(100)) || y.is_multiple_of(400)
}

/// Validate NMEA checksum.
///
/// The checksum is the XOR of all bytes between `$` and `*`, compared against the
/// two hex digits after `*`.
fn validate_checksum(sentence: &str) -> bool {
    let sentence = sentence.trim();

    // Find the '$' start and '*' before checksum.
    let start = match sentence.find('$') {
        Some(i) => i + 1,
        None => return false,
    };
    let star = match sentence.find('*') {
        Some(i) => i,
        None => return false,
    };

    if star <= start || star + 3 > sentence.len() {
        return false;
    }

    let payload = &sentence[start..star];
    let expected_hex = &sentence[star + 1..star + 3];

    let computed: u8 = payload.bytes().fold(0u8, |acc, b| acc ^ b);

    let Ok(expected) = u8::from_str_radix(expected_hex, 16) else {
        return false;
    };

    computed == expected
}

/// Parse a $GPRMC sentence (Recommended Minimum Navigation Information).
///
/// Format: `$GPRMC,hhmmss.ss,A,lat,N,lon,W,speed,course,ddmmyy,magvar,E*checksum`
pub fn parse_gprmc(sentence: &str) -> Result<GpsFix, RefClockError> {
    if !validate_checksum(sentence) {
        return Err(RefClockError::ParseError(
            "GPRMC checksum mismatch".to_string(),
        ));
    }

    // Strip checksum portion for field parsing.
    let data = sentence.trim().strip_prefix('$').unwrap_or(sentence.trim());
    let data = data.split('*').next().unwrap_or(data);

    let fields: Vec<&str> = data.split(',').collect();
    if fields.len() < 10 {
        return Err(RefClockError::ParseError(format!(
            "GPRMC: expected at least 10 fields, got {}",
            fields.len()
        )));
    }

    // fields[0] = "GPRMC"
    // fields[1] = time "hhmmss.ss"
    // fields[2] = status "A"=active "V"=void
    // fields[9] = date "ddmmyy"

    let (hours, minutes, seconds, subsec_ms) = parse_nmea_time(fields[1])?;
    let valid = fields[2] == "A";
    let (day, month, year) = parse_rmc_date(fields[9])?;

    Ok(GpsFix {
        hours,
        minutes,
        seconds,
        subsec_ms,
        day,
        month,
        year,
        valid,
    })
}

/// Parse a $GPZDA sentence (Time & Date).
///
/// Format: `$GPZDA,hhmmss.ss,dd,mm,yyyy,tzh,tzm*checksum`
pub fn parse_gpzda(sentence: &str) -> Result<GpsFix, RefClockError> {
    if !validate_checksum(sentence) {
        return Err(RefClockError::ParseError(
            "GPZDA checksum mismatch".to_string(),
        ));
    }

    let data = sentence.trim().strip_prefix('$').unwrap_or(sentence.trim());
    let data = data.split('*').next().unwrap_or(data);

    let fields: Vec<&str> = data.split(',').collect();
    if fields.len() < 5 {
        return Err(RefClockError::ParseError(format!(
            "GPZDA: expected at least 5 fields, got {}",
            fields.len()
        )));
    }

    // fields[0] = "GPZDA"
    // fields[1] = time "hhmmss.ss"
    // fields[2] = day "dd"
    // fields[3] = month "mm"
    // fields[4] = year "yyyy"

    let (hours, minutes, seconds, subsec_ms) = parse_nmea_time(fields[1])?;

    let day: u8 = fields[2]
        .parse()
        .map_err(|_| RefClockError::ParseError(format!("GPZDA: invalid day '{}'", fields[2])))?;
    let month: u8 = fields[3]
        .parse()
        .map_err(|_| RefClockError::ParseError(format!("GPZDA: invalid month '{}'", fields[3])))?;
    let year: u16 = fields[4]
        .parse()
        .map_err(|_| RefClockError::ParseError(format!("GPZDA: invalid year '{}'", fields[4])))?;

    Ok(GpsFix {
        hours,
        minutes,
        seconds,
        subsec_ms,
        day,
        month,
        year,
        valid: true, // ZDA has no status field; presence implies valid time.
    })
}

/// Parse NMEA time field "hhmmss.ss" into (hours, minutes, seconds, subsec_ms).
fn parse_nmea_time(field: &str) -> Result<(u8, u8, u8, u16), RefClockError> {
    if field.len() < 6 {
        return Err(RefClockError::ParseError(format!(
            "NMEA time too short: '{field}'"
        )));
    }

    let hours: u8 = field[0..2]
        .parse()
        .map_err(|_| RefClockError::ParseError(format!("invalid hours in '{field}'")))?;
    let minutes: u8 = field[2..4]
        .parse()
        .map_err(|_| RefClockError::ParseError(format!("invalid minutes in '{field}'")))?;

    // Seconds may have a decimal portion.
    let sec_str = &field[4..];
    let (sec_whole, subsec_ms) = if let Some(dot_pos) = sec_str.find('.') {
        let whole: u8 = sec_str[..dot_pos]
            .parse()
            .map_err(|_| RefClockError::ParseError(format!("invalid seconds in '{field}'")))?;
        let frac_str = &sec_str[dot_pos + 1..];
        // Normalize to milliseconds (pad or truncate to 3 digits).
        let ms: u16 = if frac_str.is_empty() {
            0
        } else {
            let padded = format!("{frac_str:0<3}");
            padded[..3]
                .parse()
                .map_err(|_| RefClockError::ParseError(format!("invalid subsec in '{field}'")))?
        };
        (whole, ms)
    } else {
        let whole: u8 = sec_str
            .parse()
            .map_err(|_| RefClockError::ParseError(format!("invalid seconds in '{field}'")))?;
        (whole, 0u16)
    };

    Ok((hours, minutes, sec_whole, subsec_ms))
}

/// Parse RMC date field "ddmmyy" into (day, month, year).
fn parse_rmc_date(field: &str) -> Result<(u8, u8, u16), RefClockError> {
    if field.len() < 6 {
        return Err(RefClockError::ParseError(format!(
            "RMC date too short: '{field}'"
        )));
    }

    let day: u8 = field[0..2]
        .parse()
        .map_err(|_| RefClockError::ParseError(format!("invalid day in date '{field}'")))?;
    let month: u8 = field[2..4]
        .parse()
        .map_err(|_| RefClockError::ParseError(format!("invalid month in date '{field}'")))?;
    let yy: u16 = field[4..6]
        .parse()
        .map_err(|_| RefClockError::ParseError(format!("invalid year in date '{field}'")))?;

    // Two-digit year: 80-99 -> 1980-1999, 00-79 -> 2000-2079
    let year = if yy >= 80 { 1900 + yy } else { 2000 + yy };

    Ok((day, month, year))
}

/// GPS reference clock driver.
///
/// Reads NMEA sentences from a serial device (e.g. `/dev/ttyUSB0`) and produces
/// [`SourceMeasurement`] values when a valid time fix is parsed.
pub struct GpsDriver {
    device: String,
    source_id: SourceId,
}

impl GpsDriver {
    /// Create a new GPS driver for the given serial device path.
    pub fn new(device: &str) -> Self {
        Self {
            device: device.to_string(),
            source_id: SourceId::RefClock {
                driver: "GPS".to_string(),
                unit: 0,
            },
        }
    }

    /// Return the device path.
    pub fn device(&self) -> &str {
        &self.device
    }

    /// Parse a line of NMEA data and return a measurement if valid.
    ///
    /// `recv_time` is the local clock timestamp when the serial data was received,
    /// used to compute the offset between GPS time and local time.
    pub fn process_line(&self, line: &str, recv_time: NtpTimestamp) -> Option<SourceMeasurement> {
        let trimmed = line.trim();

        let fix = if trimmed.starts_with("$GPRMC") || trimmed.starts_with("$GNRMC") {
            parse_gprmc(trimmed).ok()?
        } else if trimmed.starts_with("$GPZDA") || trimmed.starts_with("$GNZDA") {
            parse_gpzda(trimmed).ok()?
        } else {
            return None;
        };

        let gps_time = fix.to_ntp_timestamp()?;

        // Offset = GPS time - local receive time.
        let offset = NtpDuration::between(recv_time, gps_time);

        // GPS via serial has relatively high and variable latency.
        // Typical serial delay is ~50-100ms, but the second boundary from NMEA is
        // only accurate to roughly +/- a few milliseconds without PPS.
        let delay = NtpDuration::from_millis(50);
        let dispersion = NtpDuration::from_millis(10);

        Some(SourceMeasurement {
            id: self.source_id.clone(),
            offset,
            delay,
            dispersion,
            jitter: 0.005, // 5ms initial jitter estimate
            stratum: 1,    // GPS is a stratum-0 source, we report as stratum-1
            leap_indicator: LeapIndicator::NoWarning,
            root_delay: NtpDuration::ZERO,
            root_dispersion: NtpDuration::ZERO,
            time: recv_time,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Helper to build a correctly checksummed NMEA sentence from a payload.
    fn make_nmea(payload: &str) -> String {
        let cksum: u8 = payload.bytes().fold(0u8, |acc, b| acc ^ b);
        format!("${payload}*{cksum:02X}")
    }

    #[test]
    fn test_validate_checksum_valid() {
        let sentence =
            make_nmea("GPRMC,123519.00,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W");
        assert!(validate_checksum(&sentence));
    }

    #[test]
    fn test_validate_checksum_invalid() {
        // Take a valid sentence and corrupt the checksum.
        let sentence =
            make_nmea("GPRMC,123519.00,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W");
        let corrupted = format!("{}FF", &sentence[..sentence.len() - 2]);
        assert!(!validate_checksum(&corrupted));
    }

    #[test]
    fn test_validate_checksum_no_star() {
        assert!(!validate_checksum("$GPRMC,123519.00,A"));
    }

    #[test]
    fn test_parse_gprmc() {
        let sentence =
            make_nmea("GPRMC,123519.00,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W");
        let fix = parse_gprmc(&sentence).unwrap();
        assert_eq!(fix.hours, 12);
        assert_eq!(fix.minutes, 35);
        assert_eq!(fix.seconds, 19);
        assert_eq!(fix.subsec_ms, 0);
        assert_eq!(fix.day, 23);
        assert_eq!(fix.month, 3);
        assert_eq!(fix.year, 1994);
        assert!(fix.valid);
    }

    #[test]
    fn test_parse_gprmc_void() {
        // Compute correct checksum for this void sentence.
        let payload = "GPRMC,123519.00,V,,,,,,,230394,,";
        let cksum: u8 = payload.bytes().fold(0u8, |acc, b| acc ^ b);
        let sentence = format!("${payload}*{cksum:02X}");
        let fix = parse_gprmc(&sentence).unwrap();
        assert!(!fix.valid);
    }

    #[test]
    fn test_parse_gpzda() {
        // Build a GPZDA with correct checksum.
        let payload = "GPZDA,082710.00,16,09,2026,00,00";
        let cksum: u8 = payload.bytes().fold(0u8, |acc, b| acc ^ b);
        let sentence = format!("${payload}*{cksum:02X}");
        let fix = parse_gpzda(&sentence).unwrap();
        assert_eq!(fix.hours, 8);
        assert_eq!(fix.minutes, 27);
        assert_eq!(fix.seconds, 10);
        assert_eq!(fix.subsec_ms, 0);
        assert_eq!(fix.day, 16);
        assert_eq!(fix.month, 9);
        assert_eq!(fix.year, 2026);
        assert!(fix.valid);
    }

    #[test]
    fn test_gps_fix_to_ntp_timestamp() {
        let fix = GpsFix {
            hours: 0,
            minutes: 0,
            seconds: 0,
            subsec_ms: 0,
            day: 1,
            month: 1,
            year: 1970,
            valid: true,
        };
        let ts = fix.to_ntp_timestamp().unwrap();
        // NTP epoch diff is 70 years from 1900 to 1970 = 2208988800 seconds.
        assert_eq!(ts.seconds(), 2_208_988_800u32);
        assert_eq!(ts.fraction(), 0);
    }

    #[test]
    fn test_gps_fix_to_ntp_timestamp_invalid() {
        let fix = GpsFix {
            hours: 0,
            minutes: 0,
            seconds: 0,
            subsec_ms: 0,
            day: 1,
            month: 1,
            year: 2024,
            valid: false,
        };
        assert!(fix.to_ntp_timestamp().is_none());
    }

    #[test]
    fn test_gps_fix_subsec() {
        let fix = GpsFix {
            hours: 12,
            minutes: 0,
            seconds: 0,
            subsec_ms: 500, // 0.5 seconds
            day: 1,
            month: 1,
            year: 2000,
            valid: true,
        };
        let ts = fix.to_ntp_timestamp().unwrap();
        // Fraction for 500ms = 0.5 * 2^32 = 2147483648
        assert_eq!(ts.fraction(), 2_147_483_648);
    }

    #[test]
    fn test_is_leap_year() {
        assert!(is_leap_year(2000)); // divisible by 400
        assert!(!is_leap_year(1900)); // divisible by 100 but not 400
        assert!(is_leap_year(2024)); // divisible by 4
        assert!(!is_leap_year(2023)); // not divisible by 4
    }

    #[test]
    fn test_days_from_ntp_epoch_unix_epoch() {
        // 1970-01-01 should be 25567 days from 1900-01-01.
        // 70 years: 17 leap years (1904..1968 inclusive) + 53 normal = 17*366 + 53*365
        // = 6222 + 19345 = 25567
        let days = days_from_ntp_epoch(1970, 1, 1).unwrap();
        assert_eq!(days, 25567);
    }

    #[test]
    fn test_parse_nmea_time_with_frac() {
        let (h, m, s, ms) = parse_nmea_time("123519.50").unwrap();
        assert_eq!(h, 12);
        assert_eq!(m, 35);
        assert_eq!(s, 19);
        assert_eq!(ms, 500);
    }

    #[test]
    fn test_parse_nmea_time_no_frac() {
        let (h, m, s, ms) = parse_nmea_time("000000").unwrap();
        assert_eq!(h, 0);
        assert_eq!(m, 0);
        assert_eq!(s, 0);
        assert_eq!(ms, 0);
    }

    #[test]
    fn test_parse_rmc_date() {
        let (d, m, y) = parse_rmc_date("230394").unwrap();
        assert_eq!(d, 23);
        assert_eq!(m, 3);
        assert_eq!(y, 1994);
    }

    #[test]
    fn test_parse_rmc_date_2000s() {
        let (d, m, y) = parse_rmc_date("010126").unwrap();
        assert_eq!(d, 1);
        assert_eq!(m, 1);
        assert_eq!(y, 2026);
    }

    #[test]
    fn test_gps_driver_process_line_rmc() {
        let driver = GpsDriver::new("/dev/ttyUSB0");
        let sentence =
            make_nmea("GPRMC,123519.00,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W");
        let recv_time = NtpTimestamp::now();
        let measurement = driver.process_line(&sentence, recv_time);
        assert!(measurement.is_some());
        let m = measurement.unwrap();
        assert_eq!(m.stratum, 1);
        assert_eq!(m.leap_indicator, LeapIndicator::NoWarning);
    }

    #[test]
    fn test_gps_driver_process_line_unknown() {
        let driver = GpsDriver::new("/dev/ttyUSB0");
        let measurement = driver.process_line("$GPGGA,123519,...", NtpTimestamp::now());
        assert!(measurement.is_none());
    }

    #[test]
    fn test_gps_driver_process_line_invalid_checksum() {
        let driver = GpsDriver::new("/dev/ttyUSB0");
        let good = make_nmea("GPRMC,123519.00,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W");
        let sentence = format!("{}FF", &good[..good.len() - 2]);
        let measurement = driver.process_line(&sentence, NtpTimestamp::now());
        assert!(measurement.is_none());
    }
}