From 70a1a032ec437864c806ee76719bbc24ebdc8300 Mon Sep 17 00:00:00 2001 From: Jason Reiss Date: Wed, 21 Dec 2022 09:52:50 -0600 Subject: Bugfix: Calculate tmst diff over rollover correctly for cnt2utc and cnt2gps --- libloragw/src/loragw_gps.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libloragw/src/loragw_gps.c b/libloragw/src/loragw_gps.c index b461439..bb37e1b 100644 --- a/libloragw/src/loragw_gps.c +++ b/libloragw/src/loragw_gps.c @@ -719,8 +719,9 @@ int lgw_cnt2utc(struct tref ref, uint32_t count_us, struct timespec *utc) { return LGW_GPS_ERROR; } - /* calculate delta in seconds between reference count_us and target count_us */ - if (count_us > ref.count_us) { + /* calculate delta in seconds between reference count_us and target count_us + calculate rollover diff, uint32 difference is OK */ + if (count_us > ref.count_us || ((ref.count_us & 0xFF000000) == 0xFF000000) && ((count_us & 0xFF000000) == 0)) delta_sec = (double)(count_us - ref.count_us) / (TS_CPS * ref.xtal_err); } else { delta_sec = -(double)(ref.count_us - count_us) / (TS_CPS * ref.xtal_err); @@ -782,8 +783,9 @@ int lgw_cnt2gps(struct tref ref, uint32_t count_us, struct timespec *gps_time) { return LGW_GPS_ERROR; } - /* calculate delta in seconds between reference count_us and target count_us */ - if (count_us > ref.count_us) { + /* calculate delta in seconds between reference count_us and target count_us + calculate rollover diff, uint32 difference is OK */ + if (count_us > ref.count_us || ((ref.count_us & 0xFF000000) == 0xFF000000) && ((count_us & 0xFF000000) == 0)) { delta_sec = (double)(count_us - ref.count_us) / (TS_CPS * ref.xtal_err); } else { delta_sec = -(double)(ref.count_us - count_us) / (TS_CPS * ref.xtal_err); -- cgit v1.2.3