MX 2 лет назад
Родитель
Сommit
c814a81923

+ 45 - 14
README.md

@@ -1,9 +1,21 @@
 # GPS for Flipper Zero
 
-A simple Flipper Zero application for NMEA 0183 serial GPS modules, such as the
-- Adafruit Ultimate GPS Breakout.
+[![FAP Build](https://github.com/ezod/flipperzero-gps/actions/workflows/build.yml/badge.svg)](https://github.com/ezod/flipperzero-gps/actions/workflows/build.yml)
 
-Heavy lifting (NMEA parsing) provided by minmea.
+A simple Flipper Zero application for NMEA 0183 serial GPS modules.
+
+![ui](ui.png)
+
+Heavy lifting (NMEA parsing) provided by [minmea].
+
+## Installation
+
+1. Navigate to the [FAP Build](https://github.com/ezod/flipperzero-gps/actions/workflows/build.yml)
+   GitHub action workflow, and select the most recent run.
+2. The FAP is built for both the `dev` and `release` channels of the official
+   firmware. Download the artifact corresponding to your firmware version.
+3. Extract `gps_nmea.fap` from the ZIP file to `apps/GPIO` on your Flipper
+   Zero SD card.
 
 ## Usage
 
@@ -11,7 +23,7 @@ This is a single-screen app, and a few interactions are provided via the
 hardware buttons:
 
 - Long press the up button to change the **baud rate**. The default baud rate
-  is 9600, but 19200, 38400, 57600, and 115200 baud are also supported.
+  is 9600, but 4800, 19200, 38400, 57600, and 115200 baud are also supported.
 - Long press the right button to change **speed units** from knots to
   kilometers per hour.
 - Press the OK button to set the **backlight** to always on mode. Press it
@@ -23,24 +35,43 @@ hardware buttons:
 Connect the GPS module to power and the USART using GPIO pins 9 (3.3V), 11
 (GND), 13 (TX), and 14 (RX), as appropriate.
 
+![wiring](wiring.png)
 
-See the tutorial video - https://www.youtube.com/watch?v=5vSGFzEBp-k from
-Lab401 by RocketGod - https://github.com/RocketGod-git for a visual guide to
+See the [tutorial video](https://www.youtube.com/watch?v=5vSGFzEBp-k) from
+Lab401 by [RocketGod](https://github.com/RocketGod-git) for a visual guide to
 the hardware setup.
 
-## Confirmed Compatible Modules
+### Confirmed Compatible Modules
 
-* Adafruit Ultimate GPS Breakout
+* [Adafruit Ultimate GPS Breakout]
+* ATGM336H
 * Beitian BN-180
+* Beitian BN-220
+* Beitian BN-280
+* Beitian BN-880
+* Beitian BK-250
+* Beitian BK-357
+* Beitian BK-880Q
+* Beitian BE-280
+* Beitian BN-280ZF
+* Beitian BN-357ZF
 * Royaltek RBT-2100LP
-* u-blox NEO-6M
+* [u-blox NEO-6M]
+* [u-blox NEO-7M]
+* [Uputronics u-blox MAX-M8C Pico]
 
 If you have verified this application working with a module not listed here,
 please submit a PR adding it to the list.
 
-## Links
+## Building
+
+This application can be compiled using [uFBT]. Run `ufbt` in the root directory
+of the repository.
 
-Original repo link - https://github.com/ezod/flipperzero-gps
-Adafruit Ultimate GPS Breakout: https://www.adafruit.com/product/746
-minmea: https://github.com/kosma/minmea
-u-blox NEO-6M: https://www.u-blox.com/en/product/neo-6-series
+[Adafruit Ultimate GPS Breakout]: https://www.adafruit.com/product/746
+[minmea]: https://github.com/kosma/minmea
+[qFlipper]: https://flipperzero.one/update
+[u-blox NEO-6M]: https://www.u-blox.com/en/product/neo-6-series
+[u-blox NEO-7M]: https://www.u-blox.com/en/product/neo-7-series
+[uFBT]: https://github.com/flipperdevices/flipperzero-ufbt
+[Uputronics u-blox MAX-M8C Pico]: https://store.uputronics.com/index.php?route=product/product&product_id=72

+ 7 - 0
application.fam

@@ -8,6 +8,13 @@ App(
     order=35,
     fap_icon="gps_10px.png",
     fap_category="GPIO",
+    fap_private_libs=[
+        Lib(
+            name="minmea",
+            sources=["minmea.c"],
+            cdefines=["timegm=mktime"],
+        ),
+    ],
     fap_author="@ezod & @xMasterX",
     fap_version="1.2",
     fap_description="Works with GPS modules via UART, using NMEA protocol.",

+ 95 - 32
gps.c

@@ -2,7 +2,6 @@
 #include "constants.h"
 
 #include <furi.h>
-#include <furi_hal_power.h>
 #include <gui/gui.h>
 #include <string.h>
 
@@ -21,7 +20,53 @@ static void render_callback(Canvas* const canvas, void* context) {
     GpsUart* gps_uart = context;
     furi_mutex_acquire(gps_uart->mutex, FuriWaitForever);
 
-    if(!gps_uart->changing_baudrate) {
+    char buffer[64];
+
+    switch(gps_uart->view_state) {
+    case CHANGE_BAUDRATE:
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "Baudrate set to:");
+        snprintf(buffer, 64, "%ld baud", gps_uart->baudrate);
+        canvas_draw_str_aligned(canvas, 64, 47, AlignCenter, AlignBottom, buffer);
+        break;
+    case CHANGE_BACKLIGHT:
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str_aligned(
+            canvas,
+            64,
+            32,
+            AlignCenter,
+            AlignBottom,
+            gps_uart->backlight_enabled ? "Backlight enabled" : "Backlight disabled");
+        break;
+    case CHANGE_DEEPSLEEP:
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str_aligned(
+            canvas,
+            64,
+            32,
+            AlignCenter,
+            AlignBottom,
+            gps_uart->deep_sleep_enabled ? "Deep sleep enabled" : "Deep sleep disabled");
+        break;
+    case CHANGE_SPEEDUNIT:
+        canvas_set_font(canvas, FontPrimary);
+        canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "Speed unit set to:");
+        switch(gps_uart->speed_units) {
+        case KPH:
+            canvas_draw_str_aligned(canvas, 64, 47, AlignCenter, AlignBottom, "km/h");
+            break;
+        case MPH:
+            canvas_draw_str_aligned(canvas, 64, 47, AlignCenter, AlignBottom, "mi/h");
+            break;
+        case KNOTS:
+        default:
+            canvas_draw_str_aligned(canvas, 64, 47, AlignCenter, AlignBottom, "kn");
+            break;
+        }
+        break;
+    case NORMAL:
+    default:
         canvas_set_font(canvas, FontPrimary);
         canvas_draw_str_aligned(canvas, 32, 8, AlignCenter, AlignBottom, "Latitude");
         canvas_draw_str_aligned(canvas, 96, 8, AlignCenter, AlignBottom, "Longitude");
@@ -32,7 +77,6 @@ static void render_callback(Canvas* const canvas, void* context) {
         canvas_draw_str_aligned(canvas, 96, 52, AlignCenter, AlignBottom, "Last Fix");
 
         canvas_set_font(canvas, FontSecondary);
-        char buffer[64];
         snprintf(buffer, 64, "%f", (double)gps_uart->status.latitude);
         canvas_draw_str_aligned(canvas, 32, 18, AlignCenter, AlignBottom, buffer);
         snprintf(buffer, 64, "%f", (double)gps_uart->status.longitude);
@@ -42,10 +86,10 @@ static void render_callback(Canvas* const canvas, void* context) {
 
         switch(gps_uart->speed_units) {
         case KPH:
-            snprintf(buffer, 64, "%.2f km", (double)(gps_uart->status.speed * KNOTS_TO_KPH));
+            snprintf(buffer, 64, "%.2f km/h", (double)(gps_uart->status.speed * KNOTS_TO_KPH));
             break;
         case MPH:
-            snprintf(buffer, 64, "%.2f mi", (double)(gps_uart->status.speed * KNOTS_TO_MPH));
+            snprintf(buffer, 64, "%.2f mi/h", (double)(gps_uart->status.speed * KNOTS_TO_MPH));
             break;
         case KNOTS:
         default:
@@ -71,13 +115,7 @@ static void render_callback(Canvas* const canvas, void* context) {
             gps_uart->status.time_minutes,
             gps_uart->status.time_seconds);
         canvas_draw_str_aligned(canvas, 96, 62, AlignCenter, AlignBottom, buffer);
-    } else {
-        char buffer[64];
-        canvas_set_font(canvas, FontPrimary);
-        canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "Baudrate set to:");
-
-        snprintf(buffer, 64, "%ld baud", gps_uart->baudrate);
-        canvas_draw_str_aligned(canvas, 64, 47, AlignCenter, AlignBottom, buffer);
+        break;
     }
 
     furi_mutex_release(gps_uart->mutex);
@@ -95,14 +133,6 @@ int32_t gps_app(void* p) {
 
     FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
 
-    uint8_t attempts = 0;
-    bool otg_was_enabled = furi_hal_power_is_otg_enabled();
-    while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
-        furi_hal_power_enable_otg();
-        furi_delay_ms(10);
-    }
-    furi_delay_ms(200);
-
     GpsUart* gps_uart = gps_uart_enable();
 
     gps_uart->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
@@ -112,6 +142,14 @@ int32_t gps_app(void* p) {
         return 255;
     }
 
+    uint8_t attempts = 0;
+    bool otg_was_enabled = furi_hal_power_is_otg_enabled();
+    while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
+        furi_hal_power_enable_otg();
+        furi_delay_ms(10);
+    }
+    furi_delay_ms(200);
+
     // set system callbacks
     ViewPort* view_port = view_port_alloc();
     view_port_draw_callback_set(view_port, render_callback, gps_uart);
@@ -132,24 +170,27 @@ int32_t gps_app(void* p) {
             if(event.type == EventTypeKey) {
                 if(event.input.type == InputTypeShort) {
                     switch(event.input.key) {
-                    case InputKeyUp:
-                    case InputKeyDown:
-                    case InputKeyRight:
-                    case InputKeyLeft:
                     case InputKeyBack:
+                        processing = false;
                         break;
                     case InputKeyOk:
-                        if(!gps_uart->backlight_on) {
+                        if(!gps_uart->backlight_enabled) {
                             notification_message_block(
                                 gps_uart->notifications, &sequence_display_backlight_enforce_on);
-                            gps_uart->backlight_on = true;
+                            gps_uart->backlight_enabled = true;
                         } else {
                             notification_message_block(
                                 gps_uart->notifications, &sequence_display_backlight_enforce_auto);
                             notification_message(
                                 gps_uart->notifications, &sequence_display_backlight_off);
-                            gps_uart->backlight_on = false;
+                            gps_uart->backlight_enabled = false;
                         }
+
+                        gps_uart->view_state = CHANGE_BACKLIGHT;
+                        furi_mutex_release(gps_uart->mutex);
+                        view_port_update(view_port);
+                        furi_delay_ms(1000);
+                        gps_uart->view_state = NORMAL;
                         break;
                     default:
                         break;
@@ -167,15 +208,39 @@ int32_t gps_app(void* p) {
                         gps_uart->baudrate = gps_baudrates[current_gps_baudrate];
 
                         gps_uart_init_thread(gps_uart);
-                        gps_uart->changing_baudrate = true;
+                        gps_uart->view_state = CHANGE_BAUDRATE;
+
                         furi_mutex_release(gps_uart->mutex);
                         view_port_update(view_port);
+                        furi_delay_ms(1000);
+                        gps_uart->view_state = NORMAL;
                         break;
                     case InputKeyRight:
                         gps_uart->speed_units++;
                         if(gps_uart->speed_units == INVALID) {
                             gps_uart->speed_units = KNOTS;
                         }
+
+                        gps_uart->view_state = CHANGE_SPEEDUNIT;
+                        furi_mutex_release(gps_uart->mutex);
+                        view_port_update(view_port);
+                        furi_delay_ms(1000);
+                        gps_uart->view_state = NORMAL;
+                        break;
+                    case InputKeyDown:
+                        gps_uart->view_state = CHANGE_DEEPSLEEP;
+                        gps_uart->deep_sleep_enabled = !gps_uart->deep_sleep_enabled;
+
+                        // tested on Telit SE868-A and SL871L-S
+                        furi_hal_serial_tx(
+                            gps_uart->serial_handle,
+                            (uint8_t*)"$PMTK161,0*28\r\n",
+                            strlen("$PMTK161,0*28\r\n"));
+
+                        furi_mutex_release(gps_uart->mutex);
+                        view_port_update(view_port);
+                        furi_delay_ms(1000);
+                        gps_uart->view_state = NORMAL;
                         break;
                     case InputKeyBack:
                         processing = false;
@@ -186,12 +251,10 @@ int32_t gps_app(void* p) {
                 }
             }
         }
-        if(!gps_uart->changing_baudrate) {
+
+        if(gps_uart->view_state == NORMAL) {
             furi_mutex_release(gps_uart->mutex);
             view_port_update(view_port);
-        } else {
-            furi_delay_ms(1000);
-            gps_uart->changing_baudrate = false;
         }
     }
 

+ 17 - 10
gps_uart.c

@@ -1,6 +1,6 @@
 #include <string.h>
 
-#include "minmea.h"
+#include <minmea.h>
 #include "gps_uart.h"
 
 typedef enum {
@@ -10,25 +10,31 @@ typedef enum {
 
 #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
 
-static void gps_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
+static void
+    gps_uart_on_irq_cb(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) {
     GpsUart* gps_uart = (GpsUart*)context;
 
-    if(ev == UartIrqEventRXNE) {
+    if(event == FuriHalSerialRxEventData) {
+        uint8_t data = furi_hal_serial_async_rx(handle);
         furi_stream_buffer_send(gps_uart->rx_stream, &data, 1, 0);
         furi_thread_flags_set(furi_thread_get_id(gps_uart->thread), WorkerEvtRxDone);
     }
 }
 
 static void gps_uart_serial_init(GpsUart* gps_uart) {
-    furi_hal_console_disable();
-    furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, gps_uart_on_irq_cb, gps_uart);
-    furi_hal_uart_set_br(FuriHalUartIdUSART1, gps_uart->baudrate);
+    gps_uart->serial_handle = furi_hal_serial_control_acquire(UART_CH);
+    furi_check(gps_uart->serial_handle);
+    furi_hal_serial_init(gps_uart->serial_handle, gps_uart->baudrate);
+    furi_hal_serial_async_rx_start(gps_uart->serial_handle, gps_uart_on_irq_cb, gps_uart, false);
+
+    furi_hal_serial_tx(
+        gps_uart->serial_handle, (uint8_t*)"wakey wakey\r\n", strlen("wakey wakey\r\n"));
 }
 
 static void gps_uart_serial_deinit(GpsUart* gps_uart) {
     UNUSED(gps_uart);
-    furi_hal_uart_set_irq_cb(FuriHalUartIdUSART1, NULL, NULL);
-    furi_hal_console_enable();
+    furi_hal_serial_deinit(gps_uart->serial_handle);
+    furi_hal_serial_control_release(gps_uart->serial_handle);
 }
 
 static void gps_uart_parse_nmea(GpsUart* gps_uart, char* line) {
@@ -202,9 +208,10 @@ GpsUart* gps_uart_enable() {
     gps_uart->notifications = furi_record_open(RECORD_NOTIFICATION);
 
     gps_uart->baudrate = gps_baudrates[current_gps_baudrate];
-    gps_uart->changing_baudrate = false;
-    gps_uart->backlight_on = false;
     gps_uart->speed_units = KNOTS;
+    gps_uart->backlight_enabled = false;
+    gps_uart->deep_sleep_enabled = false;
+    gps_uart->view_state = NORMAL;
 
     gps_uart_init_thread(gps_uart);
 

+ 14 - 2
gps_uart.h

@@ -3,6 +3,8 @@
 #include <furi_hal.h>
 #include <notification/notification_messages.h>
 
+#define UART_CH (FuriHalSerialIdUsart)
+
 #define RX_BUF_SIZE 1024
 
 static const int gps_baudrates[6] = {4800, 9600, 19200, 38400, 57600, 115200};
@@ -25,17 +27,27 @@ typedef struct {
 
 typedef enum { KNOTS, KPH, MPH, INVALID } SpeedUnit;
 
+typedef enum {
+    CHANGE_BAUDRATE,
+    CHANGE_BACKLIGHT,
+    CHANGE_DEEPSLEEP,
+    CHANGE_SPEEDUNIT,
+    NORMAL
+} ViewState;
+
 typedef struct {
     FuriMutex* mutex;
     FuriThread* thread;
     FuriStreamBuffer* rx_stream;
     uint8_t rx_buf[RX_BUF_SIZE];
+    FuriHalSerialHandle* serial_handle;
 
     NotificationApp* notifications;
     uint32_t baudrate;
-    bool changing_baudrate;
-    bool backlight_on;
+    bool backlight_enabled;
+    bool deep_sleep_enabled;
     SpeedUnit speed_units;
+    ViewState view_state;
 
     GpsStatus status;
 } GpsUart;

+ 14 - 0
lib/minmea/.github/workflows/c-cpp.yml

@@ -0,0 +1,14 @@
+name: C/C++ CI
+
+on: [push, pull_request]
+
+jobs:
+  build:
+    runs-on: ubuntu-latest
+    steps:
+    - uses: actions/checkout@v3
+    - name: Install dependencies
+      run: sudo apt-get install -y clang-tools check cmake
+    - name: cmake
+      run: "( rm -rf build && mkdir build && cd build && cmake .. && make && CTEST_OUTPUT_ON_FAILURE=1 make 
+test && echo OK )"

+ 10 - 0
lib/minmea/.gitignore

@@ -0,0 +1,10 @@
+build/
+cmake-build-*/
+*.o
+*~
+*.diff
+.*.swp
+minmea
+tests
+example
+*.exe

+ 1 - 0
lib/minmea/.gitsubtree

@@ -0,0 +1 @@
+https://github.com/kosma/minmea master /

+ 39 - 0
lib/minmea/CMakeLists.txt

@@ -0,0 +1,39 @@
+cmake_minimum_required(VERSION 3.3)
+
+enable_testing()
+
+project(minmea)
+
+find_package(Threads REQUIRED) # Workaround for https://github.com/libcheck/check/issues/48#issuecomment-322965461
+find_package(PkgConfig)
+pkg_check_modules(CHECK REQUIRED check)
+link_directories(${CHECK_LIBRARY_DIRS})
+
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -Wextra -Werror -std=c99")
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_POSIX_C_SOURCE=199309L -D_BSD_SOURCE -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE")
+
+set(minmea_SRCS minmea.c minmea.h)
+add_library(minmea ${minmea_SRCS})
+
+add_executable(example example.c)
+target_link_libraries(example minmea)
+
+add_executable(tests tests.c)
+target_link_libraries(tests minmea ${CHECK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
+target_include_directories(tests PUBLIC ${CHECK_INCLUDE_DIRS})
+target_compile_options(tests PUBLIC ${CHECK_CFLAGS_OTHER})
+
+add_test(NAME tests COMMAND $<TARGET_FILE:tests>)
+list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")
+
+find_program(SCAN_FOUND scan-build)
+
+if (SCAN_FOUND)
+    add_test(
+        NAME clang_static_analysis
+        COMMAND scan-build make
+        WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
+    )
+else()
+    MESSAGE(STATUS "scan-build not found, not scanning code")
+endif()

+ 13 - 0
lib/minmea/COPYING

@@ -0,0 +1,13 @@
+        DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+                    Version 2, December 2004
+
+ Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+ Everyone is permitted to copy and distribute verbatim or modified
+ copies of this license document, and changing it is allowed as long
+ as the name is changed.
+
+            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. You just DO WHAT THE FUCK YOU WANT TO.

+ 165 - 0
lib/minmea/LICENSE.LGPL-3.0

@@ -0,0 +1,165 @@
+                   GNU LESSER GENERAL PUBLIC LICENSE
+                       Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+  This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+  0. Additional Definitions.
+
+  As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+  "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+  An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+  A "Combined Work" is a work produced by combining or linking an
+Application with the Library.  The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+  The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+  The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+  1. Exception to Section 3 of the GNU GPL.
+
+  You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+  2. Conveying Modified Versions.
+
+  If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+   a) under this License, provided that you make a good faith effort to
+   ensure that, in the event an Application does not supply the
+   function or data, the facility still operates, and performs
+   whatever part of its purpose remains meaningful, or
+
+   b) under the GNU GPL, with none of the additional permissions of
+   this License applicable to that copy.
+
+  3. Object Code Incorporating Material from Library Header Files.
+
+  The object code form of an Application may incorporate material from
+a header file that is part of the Library.  You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+   a) Give prominent notice with each copy of the object code that the
+   Library is used in it and that the Library and its use are
+   covered by this License.
+
+   b) Accompany the object code with a copy of the GNU GPL and this license
+   document.
+
+  4. Combined Works.
+
+  You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+   a) Give prominent notice with each copy of the Combined Work that
+   the Library is used in it and that the Library and its use are
+   covered by this License.
+
+   b) Accompany the Combined Work with a copy of the GNU GPL and this license
+   document.
+
+   c) For a Combined Work that displays copyright notices during
+   execution, include the copyright notice for the Library among
+   these notices, as well as a reference directing the user to the
+   copies of the GNU GPL and this license document.
+
+   d) Do one of the following:
+
+       0) Convey the Minimal Corresponding Source under the terms of this
+       License, and the Corresponding Application Code in a form
+       suitable for, and under terms that permit, the user to
+       recombine or relink the Application with a modified version of
+       the Linked Version to produce a modified Combined Work, in the
+       manner specified by section 6 of the GNU GPL for conveying
+       Corresponding Source.
+
+       1) Use a suitable shared library mechanism for linking with the
+       Library.  A suitable mechanism is one that (a) uses at run time
+       a copy of the Library already present on the user's computer
+       system, and (b) will operate properly with a modified version
+       of the Library that is interface-compatible with the Linked
+       Version.
+
+   e) Provide Installation Information, but only if you would otherwise
+   be required to provide such information under section 6 of the
+   GNU GPL, and only to the extent that such information is
+   necessary to install and execute a modified version of the
+   Combined Work produced by recombining or relinking the
+   Application with a modified version of the Linked Version. (If
+   you use option 4d0, the Installation Information must accompany
+   the Minimal Corresponding Source and Corresponding Application
+   Code. If you use option 4d1, you must provide the Installation
+   Information in the manner specified by section 6 of the GNU GPL
+   for conveying Corresponding Source.)
+
+  5. Combined Libraries.
+
+  You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+   a) Accompany the combined library with a copy of the same work based
+   on the Library, uncombined with any other library facilities,
+   conveyed under the terms of this License.
+
+   b) Give prominent notice with the combined library that part of it
+   is a work based on the Library, and explaining where to find the
+   accompanying uncombined form of the same work.
+
+  6. Revised Versions of the GNU Lesser General Public License.
+
+  The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+  Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+  If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.

+ 9 - 0
lib/minmea/LICENSE.MIT

@@ -0,0 +1,9 @@
+MIT License
+
+Copyright (c) 2023 Kosma Moczek
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 7 - 0
lib/minmea/LICENSE.grants

@@ -0,0 +1,7 @@
+At your option, if WTFPL is unacceptable for your legal department, I hereby
+explicitly grant you the right to use any the following licenses for minmea:
+
+1. MIT License
+2. LGPL-3.0 License or later
+
+See accompanying license files for full license text.

+ 175 - 0
lib/minmea/README.md

@@ -0,0 +1,175 @@
+# minmea, a lightweight GPS NMEA 0183 parser library
+
+[![C/C++ 
+CI](https://github.com/kosma/minmea/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/kosma/minmea/actions/workflows/c-cpp.yml)
+
+Minmea is a minimalistic GPS parser library written in pure C intended for
+resource-constrained platforms, especially microcontrollers and other embedded
+systems.
+
+## Features
+
+* Written in ISO C99.
+* No dynamic memory allocation.
+* No floating point usage in the core library.
+* Supports both fixed and floating point values.
+* One source file and one header - can't get any simpler.
+* Tested under Linux, OS X, Windows and embedded ARM GCC.
+* Easily extendable to support new sentences.
+* Complete with a test suite and static analysis.
+
+## Supported sentences
+
+* ``GBS`` (Satellite Fault Detection)
+* ``GGA`` (Fix Data)
+* ``GLL`` (Geographic Position: Latitude/Longitude)
+* ``GSA`` (DOP and active satellites)
+* ``GST`` (Pseudorange Noise Statistics)
+* ``GSV`` (Satellites in view)
+* ``RMC`` (Recommended Minimum: position, velocity, time)
+* ``VTG`` (Track made good and Ground speed)
+* ``ZDA`` (Time & Date - UTC, day, month, year and local time zone)
+
+Adding support for more sentences is trivial; see ``minmea.c`` source. Good documentation
+on NMEA is at https://gpsd.gitlab.io/gpsd/NMEA.html
+
+## Compatibility
+
+Minmea runs out-of-the-box under most Unix-compatible systems. Support for non-Unix systems
+(including native Windows builds under MSVC) is provided via compatibility headers:
+
+1. Define `MINMEA_INCLUDE_COMPAT` in the build environment.
+2. Add appropriate compatibility header from under `compat/` directory as `minmea_compat.h`.
+
+If your GPS receiver outputs very long sentences, consider increasing `MINMEA_MAX_SENTENCE_LENGTH`
+in your build environment.
+
+## Fractional number format
+
+Internally, minmea stores fractional numbers as pairs of two integers: ``{value, scale}``.
+For example, a value of ``"-123.456"`` would be parsed as ``{-123456, 1000}``. As this
+format is quite unwieldy, minmea provides the following convenience functions for converting
+to either fixed-point or floating-point format:
+
+* ``minmea_rescale({-123456, 1000}, 10) => -1235``
+* ``minmea_float({-123456, 1000}) => -123.456``
+
+The compound type ``struct minmea_float`` uses ``int_least32_t`` internally. Therefore,
+the coordinate precision is guaranteed to be at least ``[+-]DDDMM.MMMMM`` (five decimal digits)
+or ±2cm LSB at the equator. Note that GPS modules commonly only provide four decimal digits
+(``[+-]DDDMM.MMMM``), which equates to ±20cm (0.0001 minute is 0.0001/60 degrees and one degree
+is about 111km) at the equator.
+
+## Coordinate format
+
+NMEA uses the clunky ``DDMM.MMMM`` format which, honestly, is not good in the internet era.
+Internally, minmea stores it as a fractional number (see above); for practical uses,
+the value should be probably converted to the DD.DDDDD floating point format using the
+following function:
+
+* ``minmea_tocoord({-375165, 100}) => -37.860832``
+
+The library doesn't perform this conversion automatically for the following reasons:
+
+* The conversion is not reversible.
+* It requires floating point support.
+* The user might want to perform this conversion later on or retain the original values.
+
+## Example
+
+```c
+char line[MINMEA_MAX_SENTENCE_LENGTH];
+while (fgets(line, sizeof(line), stdin) != NULL) {
+    switch (minmea_sentence_id(line, false)) {
+        case MINMEA_SENTENCE_RMC: {
+            struct minmea_sentence_rmc frame;
+            if (minmea_parse_rmc(&frame, line)) {
+                printf("$RMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d\n",
+                        frame.latitude.value, frame.latitude.scale,
+                        frame.longitude.value, frame.longitude.scale,
+                        frame.speed.value, frame.speed.scale);
+                printf("$RMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d\n",
+                        minmea_rescale(&frame.latitude, 1000),
+                        minmea_rescale(&frame.longitude, 1000),
+                        minmea_rescale(&frame.speed, 1000));
+                printf("$RMC floating point degree coordinates and speed: (%f,%f) %f\n",
+                        minmea_tocoord(&frame.latitude),
+                        minmea_tocoord(&frame.longitude),
+                        minmea_tofloat(&frame.speed));
+            }
+        } break;
+
+        case MINMEA_SENTENCE_GGA: {
+            struct minmea_sentence_gga frame;
+            if (minmea_parse_gga(&frame, line)) {
+                printf("$GGA: fix quality: %d\n", frame.fix_quality);
+            }
+        } break;
+
+        case MINMEA_SENTENCE_GSV: {
+            struct minmea_sentence_gsv frame;
+            if (minmea_parse_gsv(&frame, line)) {
+                printf("$GSV: message %d of %d\n", frame.msg_nr, frame.total_msgs);
+                printf("$GSV: satellites in view: %d\n", frame.total_sats);
+                for (int i = 0; i < 4; i++)
+                    printf("$GSV: sat nr %d, elevation: %d, azimuth: %d, snr: %d dbm\n",
+                        frame.sats[i].nr,
+                        frame.sats[i].elevation,
+                        frame.sats[i].azimuth,
+                        frame.sats[i].snr);
+            }
+        } break;
+    }
+}
+```
+
+## Integration with your project
+
+Simply add ``minmea.[ch]`` to your project, ``#include "minmea.h"`` and you're
+good to go.
+
+## Running unit tests
+
+Building and running the tests requires the following:
+
+* CMake
+* Check Framework (https://libcheck.github.io/check/).
+* Clang Static Analyzer (https://clang-analyzer.llvm.org/).
+
+If you have both in your ``$PATH``, running the tests should be as simple as:
+
+```
+mkdir build
+cd build
+cmake ../
+make
+make test
+```
+
+## Limitations
+
+* Only a handful of frames is supported right now.
+* There's no support for omitting parts of the library from building. As
+  a workaround, use the ``-ffunction-sections -Wl,--gc-sections`` linker flags
+  (or equivalent) to remove the unused functions (parsers) from the final image.
+* Some systems lack ``timegm``. On these systems, the recommended course of
+  action is to build with ``-Dtimegm=mktime`` which will work correctly as long
+  the system runs in the default ``UTC`` timezone.
+
+## Contributing
+
+1. Use the GitHub pull request system.
+2. Make sure to follow to existing style (naming, indentation, etc.)
+3. Write unit tests for any new functionality you add.
+4. Be aware you're submitting your work under the repository's license.
+
+## Licensing
+
+Minmea is open source software; see ``COPYING`` for amusement. Email me if the
+license bothers you and I'll happily re-license under anything else under the sun.
+
+## Author
+
+Minmea was written by Kosma Moczek &lt;kosma@kosma.pl&gt; and Patryk Szymczak
+&lt;patryk.szymczak@gmail.com&gt; at Cloud Your Car, with bugs fixed by countless
+good people.

+ 27 - 0
lib/minmea/compat/minmea_compat_ti-rtos.h

@@ -0,0 +1,27 @@
+/*
+ * Copyright © 2017 Kosma Moczek <kosma@cloudyourcar.com>
+ * This program is free software. It comes without any warranty, to the extent
+ * permitted by applicable law. You can redistribute it and/or modify it under
+ * the terms of the Do What The Fuck You Want To Public License, Version 2, as
+ * published by Sam Hocevar. See the COPYING file for more details.
+ */
+
+#ifndef MINMEA_COMPAT_H_
+#define MINMEA_COMPAT_H_
+
+#if defined(__TI_ARM__)
+    #include <ti/posix/ccs/sys/types.h>
+    #include <ti/posix/ccs/time.h>
+#elif defined(__IAR_SYSTEMS_ICC__)
+    #include <ti/posix/iar/sys/types.h>
+    #include <ti/posix/iar/time.h>
+#elif defined(gcc)
+    #include <ti/posix/gcc/sys/types.h>
+    #include <ti/posix/gcc/time.h>
+#endif /* __TI_ARM__ */
+
+#define timegm  mktime
+
+#endif /* MINMEA_COMPAT_H */
+
+/* vim: set ts=4 sw=4 et: */

+ 23 - 0
lib/minmea/compat/minmea_compat_windows.h

@@ -0,0 +1,23 @@
+/*
+ * Copyright © 2017 Kosma Moczek <kosma@cloudyourcar.com>
+ * This program is free software. It comes without any warranty, to the extent
+ * permitted by applicable law. You can redistribute it and/or modify it under
+ * the terms of the Do What The Fuck You Want To Public License, Version 2, as
+ * published by Sam Hocevar. See the COPYING file for more details.
+ */
+
+#if defined(_MSC_VER)
+
+#if !defined(HAVE_STRUCT_TIMESPEC)
+struct timespec {
+    time_t tv_sec;
+    long tv_nsec;
+};
+#endif
+
+#define inline __inline
+#define timegm _mkgmtime
+
+#endif
+
+/* vim: set ts=4 sw=4 et: */

+ 140 - 0
lib/minmea/example.c

@@ -0,0 +1,140 @@
+/*
+ * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
+ * This program is free software. It comes without any warranty, to the extent
+ * permitted by applicable law. You can redistribute it and/or modify it under
+ * the terms of the Do What The Fuck You Want To Public License, Version 2, as
+ * published by Sam Hocevar. See the COPYING file for more details.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "minmea.h"
+
+#define INDENT_SPACES "  "
+
+int main(void)
+{
+    char line[MINMEA_MAX_SENTENCE_LENGTH];
+    while (fgets(line, sizeof(line), stdin) != NULL) {
+        printf("%s", line);
+        switch (minmea_sentence_id(line, false)) {
+            case MINMEA_SENTENCE_RMC: {
+                struct minmea_sentence_rmc frame;
+                if (minmea_parse_rmc(&frame, line)) {
+                    printf(INDENT_SPACES "$xxRMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d\n",
+                            frame.latitude.value, frame.latitude.scale,
+                            frame.longitude.value, frame.longitude.scale,
+                            frame.speed.value, frame.speed.scale);
+                    printf(INDENT_SPACES "$xxRMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d\n",
+                            minmea_rescale(&frame.latitude, 1000),
+                            minmea_rescale(&frame.longitude, 1000),
+                            minmea_rescale(&frame.speed, 1000));
+                    printf(INDENT_SPACES "$xxRMC floating point degree coordinates and speed: (%f,%f) %f\n",
+                            minmea_tocoord(&frame.latitude),
+                            minmea_tocoord(&frame.longitude),
+                            minmea_tofloat(&frame.speed));
+                }
+                else {
+                    printf(INDENT_SPACES "$xxRMC sentence is not parsed\n");
+                }
+            } break;
+
+            case MINMEA_SENTENCE_GGA: {
+                struct minmea_sentence_gga frame;
+                if (minmea_parse_gga(&frame, line)) {
+                    printf(INDENT_SPACES "$xxGGA: fix quality: %d\n", frame.fix_quality);
+                }
+                else {
+                    printf(INDENT_SPACES "$xxGGA sentence is not parsed\n");
+                }
+            } break;
+
+            case MINMEA_SENTENCE_GST: {
+                struct minmea_sentence_gst frame;
+                if (minmea_parse_gst(&frame, line)) {
+                    printf(INDENT_SPACES "$xxGST: raw latitude,longitude and altitude error deviation: (%d/%d,%d/%d,%d/%d)\n",
+                            frame.latitude_error_deviation.value, frame.latitude_error_deviation.scale,
+                            frame.longitude_error_deviation.value, frame.longitude_error_deviation.scale,
+                            frame.altitude_error_deviation.value, frame.altitude_error_deviation.scale);
+                    printf(INDENT_SPACES "$xxGST fixed point latitude,longitude and altitude error deviation"
+                           " scaled to one decimal place: (%d,%d,%d)\n",
+                            minmea_rescale(&frame.latitude_error_deviation, 10),
+                            minmea_rescale(&frame.longitude_error_deviation, 10),
+                            minmea_rescale(&frame.altitude_error_deviation, 10));
+                    printf(INDENT_SPACES "$xxGST floating point degree latitude, longitude and altitude error deviation: (%f,%f,%f)",
+                            minmea_tofloat(&frame.latitude_error_deviation),
+                            minmea_tofloat(&frame.longitude_error_deviation),
+                            minmea_tofloat(&frame.altitude_error_deviation));
+                }
+                else {
+                    printf(INDENT_SPACES "$xxGST sentence is not parsed\n");
+                }
+            } break;
+
+            case MINMEA_SENTENCE_GSV: {
+                struct minmea_sentence_gsv frame;
+                if (minmea_parse_gsv(&frame, line)) {
+                    printf(INDENT_SPACES "$xxGSV: message %d of %d\n", frame.msg_nr, frame.total_msgs);
+                    printf(INDENT_SPACES "$xxGSV: satellites in view: %d\n", frame.total_sats);
+                    for (int i = 0; i < 4; i++)
+                        printf(INDENT_SPACES "$xxGSV: sat nr %d, elevation: %d, azimuth: %d, snr: %d dbm\n",
+                            frame.sats[i].nr,
+                            frame.sats[i].elevation,
+                            frame.sats[i].azimuth,
+                            frame.sats[i].snr);
+                }
+                else {
+                    printf(INDENT_SPACES "$xxGSV sentence is not parsed\n");
+                }
+            } break;
+
+            case MINMEA_SENTENCE_VTG: {
+               struct minmea_sentence_vtg frame;
+               if (minmea_parse_vtg(&frame, line)) {
+                    printf(INDENT_SPACES "$xxVTG: true track degrees = %f\n",
+                           minmea_tofloat(&frame.true_track_degrees));
+                    printf(INDENT_SPACES "        magnetic track degrees = %f\n",
+                           minmea_tofloat(&frame.magnetic_track_degrees));
+                    printf(INDENT_SPACES "        speed knots = %f\n",
+                            minmea_tofloat(&frame.speed_knots));
+                    printf(INDENT_SPACES "        speed kph = %f\n",
+                            minmea_tofloat(&frame.speed_kph));
+               }
+               else {
+                    printf(INDENT_SPACES "$xxVTG sentence is not parsed\n");
+               }
+            } break;
+
+            case MINMEA_SENTENCE_ZDA: {
+                struct minmea_sentence_zda frame;
+                if (minmea_parse_zda(&frame, line)) {
+                    printf(INDENT_SPACES "$xxZDA: %d:%d:%d %02d.%02d.%d UTC%+03d:%02d\n",
+                           frame.time.hours,
+                           frame.time.minutes,
+                           frame.time.seconds,
+                           frame.date.day,
+                           frame.date.month,
+                           frame.date.year,
+                           frame.hour_offset,
+                           frame.minute_offset);
+                }
+                else {
+                    printf(INDENT_SPACES "$xxZDA sentence is not parsed\n");
+                }
+            } break;
+
+            case MINMEA_INVALID: {
+                printf(INDENT_SPACES "$xxxxx sentence is not valid\n");
+            } break;
+
+            default: {
+                printf(INDENT_SPACES "$xxxxx sentence is not parsed\n");
+            } break;
+        }
+    }
+
+    return 0;
+}
+
+/* vim: set ts=4 sw=4 et: */

+ 679 - 0
lib/minmea/minmea.c

@@ -0,0 +1,679 @@
+/*
+ * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
+ * This program is free software. It comes without any warranty, to the extent
+ * permitted by applicable law. You can redistribute it and/or modify it under
+ * the terms of the Do What The Fuck You Want To Public License, Version 2, as
+ * published by Sam Hocevar. See the COPYING file for more details.
+ */
+
+#include "minmea.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdarg.h>
+
+#define boolstr(s) ((s) ? "true" : "false")
+
+static int hex2int(char c)
+{
+    if (c >= '0' && c <= '9')
+        return c - '0';
+    if (c >= 'A' && c <= 'F')
+        return c - 'A' + 10;
+    if (c >= 'a' && c <= 'f')
+        return c - 'a' + 10;
+    return -1;
+}
+
+uint8_t minmea_checksum(const char *sentence)
+{
+    // Support senteces with or without the starting dollar sign.
+    if (*sentence == '$')
+        sentence++;
+
+    uint8_t checksum = 0x00;
+
+    // The optional checksum is an XOR of all bytes between "$" and "*".
+    while (*sentence && *sentence != '*')
+        checksum ^= *sentence++;
+
+    return checksum;
+}
+
+bool minmea_check(const char *sentence, bool strict)
+{
+    uint8_t checksum = 0x00;
+
+    // A valid sentence starts with "$".
+    if (*sentence++ != '$')
+        return false;
+
+    // The optional checksum is an XOR of all bytes between "$" and "*".
+    while (*sentence && *sentence != '*' && isprint((unsigned char) *sentence))
+        checksum ^= *sentence++;
+
+    // If checksum is present...
+    if (*sentence == '*') {
+        // Extract checksum.
+        sentence++;
+        int upper = hex2int(*sentence++);
+        if (upper == -1)
+            return false;
+        int lower = hex2int(*sentence++);
+        if (lower == -1)
+            return false;
+        int expected = upper << 4 | lower;
+
+        // Check for checksum mismatch.
+        if (checksum != expected)
+            return false;
+    } else if (strict) {
+        // Discard non-checksummed frames in strict mode.
+        return false;
+    }
+
+    // The only stuff allowed at this point is a newline.
+    while (*sentence == '\r' || *sentence == '\n') {
+        sentence++;
+    }
+    
+    if (*sentence) {
+        return false;
+    }
+
+    return true;
+}
+
+bool minmea_scan(const char *sentence, const char *format, ...)
+{
+    bool result = false;
+    bool optional = false;
+
+    if (sentence == NULL)
+        return false;
+
+    va_list ap;
+    va_start(ap, format);
+
+    const char *field = sentence;
+#define next_field() \
+    do { \
+        /* Progress to the next field. */ \
+        while (minmea_isfield(*sentence)) \
+            sentence++; \
+        /* Make sure there is a field there. */ \
+        if (*sentence == ',') { \
+            sentence++; \
+            field = sentence; \
+        } else { \
+            field = NULL; \
+        } \
+    } while (0)
+
+    while (*format) {
+        char type = *format++;
+
+        if (type == ';') {
+            // All further fields are optional.
+            optional = true;
+            continue;
+        }
+
+        if (!field && !optional) {
+            // Field requested but we ran out if input. Bail out.
+            goto parse_error;
+        }
+
+        switch (type) {
+            case 'c': { // Single character field (char).
+                char value = '\0';
+
+                if (field && minmea_isfield(*field))
+                    value = *field;
+
+                *va_arg(ap, char *) = value;
+            } break;
+
+            case 'd': { // Single character direction field (int).
+                int value = 0;
+
+                if (field && minmea_isfield(*field)) {
+                    switch (*field) {
+                        case 'N':
+                        case 'E':
+                            value = 1;
+                            break;
+                        case 'S':
+                        case 'W':
+                            value = -1;
+                            break;
+                        default:
+                            goto parse_error;
+                    }
+                }
+
+                *va_arg(ap, int *) = value;
+            } break;
+
+            case 'f': { // Fractional value with scale (struct minmea_float).
+                int sign = 0;
+                int_least32_t value = -1;
+                int_least32_t scale = 0;
+
+                if (field) {
+                    while (minmea_isfield(*field)) {
+                        if (*field == '+' && !sign && value == -1) {
+                            sign = 1;
+                        } else if (*field == '-' && !sign && value == -1) {
+                            sign = -1;
+                        } else if (isdigit((unsigned char) *field)) {
+                            int digit = *field - '0';
+                            if (value == -1)
+                                value = 0;
+                            if (value > (INT_LEAST32_MAX-digit) / 10) {
+                                /* we ran out of bits, what do we do? */
+                                if (scale) {
+                                    /* truncate extra precision */
+                                    break;
+                                } else {
+                                    /* integer overflow. bail out. */
+                                    goto parse_error;
+                                }
+                            }
+                            value = (10 * value) + digit;
+                            if (scale)
+                                scale *= 10;
+                        } else if (*field == '.' && scale == 0) {
+                            scale = 1;
+                        } else if (*field == ' ') {
+                            /* Allow spaces at the start of the field. Not NMEA
+                             * conformant, but some modules do this. */
+                            if (sign != 0 || value != -1 || scale != 0)
+                                goto parse_error;
+                        } else {
+                            goto parse_error;
+                        }
+                        field++;
+                    }
+                }
+
+                if ((sign || scale) && value == -1)
+                    goto parse_error;
+
+                if (value == -1) {
+                    /* No digits were scanned. */
+                    value = 0;
+                    scale = 0;
+                } else if (scale == 0) {
+                    /* No decimal point. */
+                    scale = 1;
+                }
+                if (sign)
+                    value *= sign;
+
+                *va_arg(ap, struct minmea_float *) = (struct minmea_float) {value, scale};
+            } break;
+
+            case 'i': { // Integer value, default 0 (int).
+                int value = 0;
+
+                if (field) {
+                    char *endptr;
+                    value = strtol(field, &endptr, 10);
+                    if (minmea_isfield(*endptr))
+                        goto parse_error;
+                }
+
+                *va_arg(ap, int *) = value;
+            } break;
+
+            case 's': { // String value (char *).
+                char *buf = va_arg(ap, char *);
+
+                if (field) {
+                    while (minmea_isfield(*field))
+                        *buf++ = *field++;
+                }
+
+                *buf = '\0';
+            } break;
+
+            case 't': { // NMEA talker+sentence identifier (char *).
+                // This field is always mandatory.
+                if (!field)
+                    goto parse_error;
+
+                if (field[0] != '$')
+                    goto parse_error;
+                for (int f=0; f<5; f++)
+                    if (!minmea_isfield(field[1+f]))
+                        goto parse_error;
+
+                char *buf = va_arg(ap, char *);
+                memcpy(buf, field+1, 5);
+                buf[5] = '\0';
+            } break;
+
+            case 'D': { // Date (int, int, int), -1 if empty.
+                struct minmea_date *date = va_arg(ap, struct minmea_date *);
+
+                int d = -1, m = -1, y = -1;
+
+                if (field && minmea_isfield(*field)) {
+                    // Always six digits.
+                    for (int f=0; f<6; f++)
+                        if (!isdigit((unsigned char) field[f]))
+                            goto parse_error;
+
+                    char dArr[] = {field[0], field[1], '\0'};
+                    char mArr[] = {field[2], field[3], '\0'};
+                    char yArr[] = {field[4], field[5], '\0'};
+                    d = strtol(dArr, NULL, 10);
+                    m = strtol(mArr, NULL, 10);
+                    y = strtol(yArr, NULL, 10);
+                }
+
+                date->day = d;
+                date->month = m;
+                date->year = y;
+            } break;
+
+            case 'T': { // Time (int, int, int, int), -1 if empty.
+                struct minmea_time *time_ = va_arg(ap, struct minmea_time *);
+
+                int h = -1, i = -1, s = -1, u = -1;
+
+                if (field && minmea_isfield(*field)) {
+                    // Minimum required: integer time.
+                    for (int f=0; f<6; f++)
+                        if (!isdigit((unsigned char) field[f]))
+                            goto parse_error;
+
+                    char hArr[] = {field[0], field[1], '\0'};
+                    char iArr[] = {field[2], field[3], '\0'};
+                    char sArr[] = {field[4], field[5], '\0'};
+                    h = strtol(hArr, NULL, 10);
+                    i = strtol(iArr, NULL, 10);
+                    s = strtol(sArr, NULL, 10);
+                    field += 6;
+
+                    // Extra: fractional time. Saved as microseconds.
+                    if (*field++ == '.') {
+                        uint32_t value = 0;
+                        uint32_t scale = 1000000LU;
+                        while (isdigit((unsigned char) *field) && scale > 1) {
+                            value = (value * 10) + (*field++ - '0');
+                            scale /= 10;
+                        }
+                        u = value * scale;
+                    } else {
+                        u = 0;
+                    }
+                }
+
+                time_->hours = h;
+                time_->minutes = i;
+                time_->seconds = s;
+                time_->microseconds = u;
+            } break;
+
+            case '_': { // Ignore the field.
+            } break;
+
+            default: { // Unknown.
+                goto parse_error;
+            }
+        }
+
+        next_field();
+    }
+
+    result = true;
+
+parse_error:
+    va_end(ap);
+    return result;
+}
+
+bool minmea_talker_id(char talker[3], const char *sentence)
+{
+    char type[6];
+    if (!minmea_scan(sentence, "t", type))
+        return false;
+
+    talker[0] = type[0];
+    talker[1] = type[1];
+    talker[2] = '\0';
+
+    return true;
+}
+
+enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict)
+{
+    if (!minmea_check(sentence, strict))
+        return MINMEA_INVALID;
+
+    char type[6];
+    if (!minmea_scan(sentence, "t", type))
+        return MINMEA_INVALID;
+
+    if (!strcmp(type+2, "GBS"))
+        return MINMEA_SENTENCE_GBS;
+    if (!strcmp(type+2, "GGA"))
+        return MINMEA_SENTENCE_GGA;
+    if (!strcmp(type+2, "GLL"))
+        return MINMEA_SENTENCE_GLL;
+    if (!strcmp(type+2, "GSA"))
+        return MINMEA_SENTENCE_GSA;
+    if (!strcmp(type+2, "GST"))
+        return MINMEA_SENTENCE_GST;
+    if (!strcmp(type+2, "GSV"))
+        return MINMEA_SENTENCE_GSV;
+    if (!strcmp(type+2, "RMC"))
+        return MINMEA_SENTENCE_RMC;
+    if (!strcmp(type+2, "VTG"))
+        return MINMEA_SENTENCE_VTG;
+    if (!strcmp(type+2, "ZDA"))
+        return MINMEA_SENTENCE_ZDA;
+
+    return MINMEA_UNKNOWN;
+}
+
+bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence)
+{
+    // $GNGBS,170556.00,3.0,2.9,8.3,,,,*5C
+    char type[6];
+    if (!minmea_scan(sentence, "tTfffifff",
+            type,
+            &frame->time,
+            &frame->err_latitude,
+            &frame->err_longitude,
+            &frame->err_altitude,
+            &frame->svid,
+            &frame->prob,
+            &frame->bias,
+            &frame->stddev
+            ))
+        return false;
+    if (strcmp(type+2, "GBS"))
+        return false;
+
+    return true;
+}
+
+bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence)
+{
+    // $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
+    char type[6];
+    char validity;
+    int latitude_direction;
+    int longitude_direction;
+    int variation_direction;
+    if (!minmea_scan(sentence, "tTcfdfdffDfd",
+            type,
+            &frame->time,
+            &validity,
+            &frame->latitude, &latitude_direction,
+            &frame->longitude, &longitude_direction,
+            &frame->speed,
+            &frame->course,
+            &frame->date,
+            &frame->variation, &variation_direction))
+        return false;
+    if (strcmp(type+2, "RMC"))
+        return false;
+
+    frame->valid = (validity == 'A');
+    frame->latitude.value *= latitude_direction;
+    frame->longitude.value *= longitude_direction;
+    frame->variation.value *= variation_direction;
+
+    return true;
+}
+
+bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence)
+{
+    // $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
+    char type[6];
+    int latitude_direction;
+    int longitude_direction;
+
+    if (!minmea_scan(sentence, "tTfdfdiiffcfcf_",
+            type,
+            &frame->time,
+            &frame->latitude, &latitude_direction,
+            &frame->longitude, &longitude_direction,
+            &frame->fix_quality,
+            &frame->satellites_tracked,
+            &frame->hdop,
+            &frame->altitude, &frame->altitude_units,
+            &frame->height, &frame->height_units,
+            &frame->dgps_age))
+        return false;
+    if (strcmp(type+2, "GGA"))
+        return false;
+
+    frame->latitude.value *= latitude_direction;
+    frame->longitude.value *= longitude_direction;
+
+    return true;
+}
+
+bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence)
+{
+    // $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
+    char type[6];
+
+    if (!minmea_scan(sentence, "tciiiiiiiiiiiiifff",
+            type,
+            &frame->mode,
+            &frame->fix_type,
+            &frame->sats[0],
+            &frame->sats[1],
+            &frame->sats[2],
+            &frame->sats[3],
+            &frame->sats[4],
+            &frame->sats[5],
+            &frame->sats[6],
+            &frame->sats[7],
+            &frame->sats[8],
+            &frame->sats[9],
+            &frame->sats[10],
+            &frame->sats[11],
+            &frame->pdop,
+            &frame->hdop,
+            &frame->vdop))
+        return false;
+    if (strcmp(type+2, "GSA"))
+        return false;
+
+    return true;
+}
+
+bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence)
+{
+    // $GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41$;
+    char type[6];
+    int latitude_direction;
+    int longitude_direction;
+
+    if (!minmea_scan(sentence, "tfdfdTc;c",
+            type,
+            &frame->latitude, &latitude_direction,
+            &frame->longitude, &longitude_direction,
+            &frame->time,
+            &frame->status,
+            &frame->mode))
+        return false;
+    if (strcmp(type+2, "GLL"))
+        return false;
+
+    frame->latitude.value *= latitude_direction;
+    frame->longitude.value *= longitude_direction;
+
+    return true;
+}
+
+bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence)
+{
+    // $GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58
+    char type[6];
+
+    if (!minmea_scan(sentence, "tTfffffff",
+            type,
+            &frame->time,
+            &frame->rms_deviation,
+            &frame->semi_major_deviation,
+            &frame->semi_minor_deviation,
+            &frame->semi_major_orientation,
+            &frame->latitude_error_deviation,
+            &frame->longitude_error_deviation,
+            &frame->altitude_error_deviation))
+        return false;
+    if (strcmp(type+2, "GST"))
+        return false;
+
+    return true;
+}
+
+bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence)
+{
+    // $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74
+    // $GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D
+    // $GPGSV,4,2,11,08,51,203,30,09,45,215,28*75
+    // $GPGSV,4,4,13,39,31,170,27*40
+    // $GPGSV,4,4,13*7B
+    char type[6];
+
+    if (!minmea_scan(sentence, "tiii;iiiiiiiiiiiiiiii",
+            type,
+            &frame->total_msgs,
+            &frame->msg_nr,
+            &frame->total_sats,
+            &frame->sats[0].nr,
+            &frame->sats[0].elevation,
+            &frame->sats[0].azimuth,
+            &frame->sats[0].snr,
+            &frame->sats[1].nr,
+            &frame->sats[1].elevation,
+            &frame->sats[1].azimuth,
+            &frame->sats[1].snr,
+            &frame->sats[2].nr,
+            &frame->sats[2].elevation,
+            &frame->sats[2].azimuth,
+            &frame->sats[2].snr,
+            &frame->sats[3].nr,
+            &frame->sats[3].elevation,
+            &frame->sats[3].azimuth,
+            &frame->sats[3].snr
+            )) {
+        return false;
+    }
+    if (strcmp(type+2, "GSV"))
+        return false;
+
+    return true;
+}
+
+bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence)
+{
+    // $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48
+    // $GPVTG,156.1,T,140.9,M,0.0,N,0.0,K*41
+    // $GPVTG,096.5,T,083.5,M,0.0,N,0.0,K,D*22
+    // $GPVTG,188.36,T,,M,0.820,N,1.519,K,A*3F
+    char type[6];
+    char c_true, c_magnetic, c_knots, c_kph, c_faa_mode;
+
+    if (!minmea_scan(sentence, "t;fcfcfcfcc",
+            type,
+            &frame->true_track_degrees,
+            &c_true,
+            &frame->magnetic_track_degrees,
+            &c_magnetic,
+            &frame->speed_knots,
+            &c_knots,
+            &frame->speed_kph,
+            &c_kph,
+            &c_faa_mode))
+        return false;
+    if (strcmp(type+2, "VTG"))
+        return false;
+    // values are only valid with the accompanying characters
+    if (c_true != 'T')
+        frame->true_track_degrees.scale = 0;
+    if (c_magnetic != 'M')
+        frame->magnetic_track_degrees.scale = 0;
+    if (c_knots != 'N')
+        frame->speed_knots.scale = 0;
+    if (c_kph != 'K')
+        frame->speed_kph.scale = 0;
+    frame->faa_mode = (enum minmea_faa_mode)c_faa_mode;
+
+    return true;
+}
+
+bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence)
+{
+  // $GPZDA,201530.00,04,07,2002,00,00*60
+  char type[6];
+
+  if(!minmea_scan(sentence, "tTiiiii",
+          type,
+          &frame->time,
+          &frame->date.day,
+          &frame->date.month,
+          &frame->date.year,
+          &frame->hour_offset,
+          &frame->minute_offset))
+      return false;
+  if (strcmp(type+2, "ZDA"))
+      return false;
+
+  // check offsets
+  if (abs(frame->hour_offset) > 13 ||
+      frame->minute_offset > 59 ||
+      frame->minute_offset < 0)
+      return false;
+
+  return true;
+}
+
+int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_)
+{
+    if (date->year == -1 || time_->hours == -1)
+        return -1;
+
+    memset(tm, 0, sizeof(*tm));
+    if (date->year < 80) {
+        tm->tm_year = 2000 + date->year - 1900; // 2000-2079
+    } else if (date->year >= 1900) {
+        tm->tm_year = date->year - 1900;        // 4 digit year, use directly
+    } else {
+        tm->tm_year = date->year;               // 1980-1999
+    }
+    tm->tm_mon = date->month - 1;
+    tm->tm_mday = date->day;
+    tm->tm_hour = time_->hours;
+    tm->tm_min = time_->minutes;
+    tm->tm_sec = time_->seconds;
+
+    return 0;
+}
+
+int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_)
+{
+    struct tm tm;
+    if (minmea_getdatetime(&tm, date, time_))
+        return -1;
+
+    time_t timestamp = timegm(&tm); /* See README.md if your system lacks timegm(). */
+    if (timestamp != (time_t)-1) {
+        ts->tv_sec = timestamp;
+        ts->tv_nsec = time_->microseconds * 1000;
+        return 0;
+    } else {
+        return -1;
+    }
+}
+
+/* vim: set ts=4 sw=4 et: */

+ 42 - 42
minmea.h → lib/minmea/minmea.h

@@ -87,10 +87,8 @@ struct minmea_sentence_gga {
     int fix_quality;
     int satellites_tracked;
     struct minmea_float hdop;
-    struct minmea_float altitude;
-    char altitude_units;
-    struct minmea_float height;
-    char height_units;
+    struct minmea_float altitude; char altitude_units;
+    struct minmea_float height; char height_units;
     struct minmea_float dgps_age;
 };
 
@@ -181,22 +179,22 @@ struct minmea_sentence_zda {
 /**
  * Calculate raw sentence checksum. Does not check sentence integrity.
  */
-uint8_t minmea_checksum(const char* sentence);
+uint8_t minmea_checksum(const char *sentence);
 
 /**
  * Check sentence validity and checksum. Returns true for valid sentences.
  */
-bool minmea_check(const char* sentence, bool strict);
+bool minmea_check(const char *sentence, bool strict);
 
 /**
  * Determine talker identifier.
  */
-bool minmea_talker_id(char talker[3], const char* sentence);
+bool minmea_talker_id(char talker[3], const char *sentence);
 
 /**
  * Determine sentence identifier.
  */
-enum minmea_sentence_id minmea_sentence_id(const char* sentence, bool strict);
+enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict);
 
 /**
  * Scanf-like processor for NMEA sentences. Supports the following formats:
@@ -212,70 +210,72 @@ enum minmea_sentence_id minmea_sentence_id(const char* sentence, bool strict);
  * ; - following fields are optional
  * Returns true on success. See library source code for details.
  */
-bool minmea_scan(const char* sentence, const char* format, ...);
+bool minmea_scan(const char *sentence, const char *format, ...);
 
 /*
  * Parse a specific type of sentence. Return true on success.
  */
-bool minmea_parse_gbs(struct minmea_sentence_gbs* frame, const char* sentence);
-bool minmea_parse_rmc(struct minmea_sentence_rmc* frame, const char* sentence);
-bool minmea_parse_gga(struct minmea_sentence_gga* frame, const char* sentence);
-bool minmea_parse_gsa(struct minmea_sentence_gsa* frame, const char* sentence);
-bool minmea_parse_gll(struct minmea_sentence_gll* frame, const char* sentence);
-bool minmea_parse_gst(struct minmea_sentence_gst* frame, const char* sentence);
-bool minmea_parse_gsv(struct minmea_sentence_gsv* frame, const char* sentence);
-bool minmea_parse_vtg(struct minmea_sentence_vtg* frame, const char* sentence);
-bool minmea_parse_zda(struct minmea_sentence_zda* frame, const char* sentence);
+bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence);
+bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
+bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
+bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
+bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence);
+bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence);
+bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence);
+bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence);
+bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence);
 
 /**
  * Convert GPS UTC date/time representation to a UNIX calendar time.
  */
-int minmea_getdatetime(
-    struct tm* tm,
-    const struct minmea_date* date,
-    const struct minmea_time* time_);
+int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_);
 
 /**
  * Convert GPS UTC date/time representation to a UNIX timestamp.
  */
-int minmea_gettime(
-    struct timespec* ts,
-    const struct minmea_date* date,
-    const struct minmea_time* time_);
+int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_);
 
 /**
  * Rescale a fixed-point value to a different scale. Rounds towards zero.
  */
-static inline int_least32_t minmea_rescale(const struct minmea_float* f, int_least32_t new_scale) {
-    if(f->scale == 0) return 0;
-    if(f->scale == new_scale) return f->value;
-    if(f->scale > new_scale)
-        return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale / new_scale / 2) /
-               (f->scale / new_scale);
+static inline int_least32_t minmea_rescale(const struct minmea_float *f, int_least32_t new_scale)
+{
+    if (f->scale == 0)
+        return 0;
+    if (f->scale == new_scale)
+        return f->value;
+    if (f->scale > new_scale)
+        return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale/new_scale/2) / (f->scale/new_scale);
     else
-        return f->value * (new_scale / f->scale);
+        return f->value * (new_scale/f->scale);
 }
 
 /**
  * Convert a fixed-point value to a floating-point value.
  * Returns NaN for "unknown" values.
  */
-static inline float minmea_tofloat(const struct minmea_float* f) {
-    if(f->scale == 0) return NAN;
-    return (float)f->value / (float)f->scale;
+static inline float minmea_tofloat(const struct minmea_float *f)
+{
+    if (f->scale == 0)
+        return NAN;
+    return (float) f->value / (float) f->scale;
 }
 
 /**
  * Convert a raw coordinate to a floating point DD.DDD... value.
  * Returns NaN for "unknown" values.
  */
-static inline float minmea_tocoord(const struct minmea_float* f) {
-    if(f->scale == 0) return NAN;
-    if(f->scale > (INT_LEAST32_MAX / 100)) return NAN;
-    if(f->scale < (INT_LEAST32_MIN / 100)) return NAN;
+static inline float minmea_tocoord(const struct minmea_float *f)
+{
+    if (f->scale == 0)
+        return NAN;
+    if (f->scale  > (INT_LEAST32_MAX / 100))
+        return NAN;
+    if (f->scale < (INT_LEAST32_MIN / 100))
+        return NAN;
     int_least32_t degrees = f->value / (f->scale * 100);
     int_least32_t minutes = f->value % (f->scale * 100);
-    return (float)degrees + (float)minutes / (60 * f->scale);
+    return (float) degrees + (float) minutes / (60 * f->scale);
 }
 
 /**
@@ -283,7 +283,7 @@ static inline float minmea_tocoord(const struct minmea_float* f) {
  * sentence data field.
  */
 static inline bool minmea_isfield(char c) {
-    return isprint((unsigned char)c) && c != ',' && c != '*';
+    return isprint((unsigned char) c) && c != ',' && c != '*';
 }
 
 #ifdef __cplusplus

+ 1207 - 0
lib/minmea/tests.c

@@ -0,0 +1,1207 @@
+/*
+ * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
+ * This program is free software. It comes without any warranty, to the extent
+ * permitted by applicable law. You can redistribute it and/or modify it under
+ * the terms of the Do What The Fuck You Want To Public License, Version 2, as
+ * published by Sam Hocevar. See the COPYING file for more details.
+ */
+
+#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
+#pragma GCC diagnostic ignored "-Wtype-limits"
+
+#include <stdlib.h>
+#include <string.h>
+#include <check.h>
+
+#include "minmea.h"
+
+static const char *valid_sentences_nochecksum[] = {
+    "$GPTXT,xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
+    "$GPTXT,xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+        "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
+    "$GPTXT,hello\n",
+    "$GPTXT,hello\r",
+    "$GPTXT,hello\r\n",
+    "$GPTXT,hello\r\n\r\n",
+    "$GPTXT,hello\n\r\r\n",
+    NULL,
+};
+
+static const char *valid_sentences_checksum[] = {
+    "$GPTXT,01,01,02,ANTSTATUS=INIT*25",
+    "$GPRMC,,V,,,,,,,,,,N*53",
+    "$GPVTG,,,,,,,,,N*30",
+    "$GPGGA,,,,,,0,00,99.99,,,,,,*48",
+    "$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30",
+    "$GPGLL,,,,,,V,N*64",
+    "$GPXTE,A,A,0.67,L,N*6F",
+    "$GPXTE,A,A,0.67,L,N*6f",
+    "$GPGGA,123204.00,5106.94086,N,01701.51680,E,1,06,3.86,127.9,M,40.5,M,,*51",
+    "$GPGSA,A,3,02,08,09,05,04,26,,,,,,,4.92,3.86,3.05*00",
+    "$GPGSV,4,1,13,02,28,259,33,04,12,212,27,05,34,305,30,07,79,138,*7F",
+    "$GPGSV,4,2,13,08,51,203,30,09,45,215,28,10,69,197,19,13,47,081,*76",
+    "$GPGSV,4,3,13,16,20,040,17,26,08,271,30,28,01,168,18,33,24,219,27*74",
+    "$GPGSV,4,4,13,39,31,170,27*40",
+    "$GPGLL,5106.94086,N,01701.51680,E,123204.00,A,A*63",
+    "$GPRMC,123205.00,A,5106.94085,N,01701.51689,E,0.016,,280214,,,A*7B",
+    "$GPVTG,,T,,M,0.016,N,0.030,K,A*27",
+    "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58",
+    "$GPZDA,160012.71,11,03,2004,-1,00*7D",
+    "$GNGBS,170556.00,3.0,2.9,8.3,,,,*5C",
+    NULL,
+};
+
+static const char *invalid_sentences[] = {
+    "$GPTXT,01,01,02,ANTSTATUS=INIT*26",
+    "$GPRMC,,V,,,,,,,,,,N*532",
+    "$GPVTG,,,,\xff,,,,,N*30",
+    "$$GPGGA,,,,,,0,00,99.99,,,,,,*48",
+    "GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30",
+    "gps: $GPGLL,,,,,,V,N",
+    "$GPXTE,A,A,0.67,L,N*6e",
+    "$GPXTE,A,A,0.67,L,N*6g",
+    "$GPTXT,hello\n ",
+    "$GPTXT,hello\r*24",
+    "$GPTXT,hello\r\n$",
+    NULL,
+};
+
+START_TEST(test_minmea_checksum)
+{
+    ck_assert_int_eq(minmea_checksum(""), 0x00);
+    ck_assert_int_eq(minmea_checksum("$"), 0x00);
+    ck_assert_int_eq(minmea_checksum("*"), 0x00);
+    ck_assert_int_eq(minmea_checksum("$*"), 0x00);
+    ck_assert_int_eq(minmea_checksum("$GPTXT,01,01,02,ANTSTATUS=INIT*25"), 0x25);
+    ck_assert_int_eq(minmea_checksum("$GPTXT,01,01,02,ANTSTATUS=INIT"), 0x25);
+    ck_assert_int_eq(minmea_checksum("GPTXT,01,01,02,ANTSTATUS=INIT*25"), 0x25);
+    ck_assert_int_eq(minmea_checksum("GPTXT,01,01,02,ANTSTATUS=INIT"), 0x25);
+    ck_assert_int_eq(minmea_checksum("$GPXTE,A,A,0.67,L,N*6F"), 0x6f);
+    ck_assert_int_eq(minmea_checksum("GPXTE,A,A,0.67,L,N*6f"), 0x6f);
+}
+END_TEST
+
+START_TEST(test_minmea_check)
+{
+    for (const char **sentence=valid_sentences_nochecksum; *sentence; sentence++) {
+        ck_assert_msg(minmea_check(*sentence, false) == true, "%s", *sentence);
+        ck_assert_msg(minmea_check(*sentence, true) == false, "%s", *sentence);
+    }
+
+    for (const char **sentence=valid_sentences_checksum; *sentence; sentence++) {
+        ck_assert_msg(minmea_check(*sentence, false) == true, "%s", *sentence);
+        ck_assert_msg(minmea_check(*sentence, true) == true, "%s", *sentence);
+    }
+
+    for (const char **sentence=invalid_sentences; *sentence; sentence++) {
+        ck_assert_msg(minmea_check(*sentence, false) == false, "%s", *sentence);
+        ck_assert_msg(minmea_check(*sentence, true) == false, "%s", *sentence);
+    }
+}
+END_TEST
+
+START_TEST(test_minmea_scan_c)
+{
+    char ch, extra;
+
+    ck_assert(minmea_scan("A,123.45", "c", &ch) == true);
+    ck_assert_int_eq(ch, 'A');
+
+    ck_assert(minmea_scan("WUT,123.45", "c", &ch) == true);
+    ck_assert_int_eq(ch, 'W');
+
+    ck_assert(minmea_scan(",123.45", "c", &ch) == true);
+    ck_assert_int_eq(ch, '\0');
+
+    ck_assert(minmea_scan("A,B", "cc", &ch, &extra) == true);
+    ck_assert_int_eq(ch, 'A');
+    ck_assert_int_eq(extra, 'B');
+
+    ck_assert(minmea_scan("C", "cc", &ch, &extra) == false);
+
+    ck_assert(minmea_scan("D", "c;c", &ch, &extra) == true);
+    ck_assert_int_eq(ch, 'D');
+    ck_assert_int_eq(extra, '\0');
+    ck_assert(minmea_scan("E,F", "c;c", &ch, &extra) == true);
+    ck_assert_int_eq(ch, 'E');
+    ck_assert_int_eq(extra, 'F');
+}
+END_TEST
+
+START_TEST(test_minmea_scan_d)
+{
+    int direction;
+
+    ck_assert(minmea_scan("K", "d", &direction) == false);
+
+    ck_assert(minmea_scan("", "d", &direction) == true);
+    ck_assert(minmea_scan(",foo", "d", &direction) == true);
+    ck_assert_int_eq(direction, 0);
+    ck_assert(minmea_scan("N", "d", &direction) == true);
+    ck_assert_int_eq(direction, 1);
+    ck_assert(minmea_scan("S,foo", "d", &direction) == true);
+    ck_assert_int_eq(direction, -1);
+    ck_assert(minmea_scan("W", "d", &direction) == true);
+    ck_assert_int_eq(direction, -1);
+    ck_assert(minmea_scan("E,foo", "d", &direction) == true);
+    ck_assert_int_eq(direction, 1);
+}
+END_TEST
+
+START_TEST(test_minmea_scan_f)
+{
+    struct minmea_float f;
+
+    ck_assert(minmea_scan("-", "f", &f) == false);
+    ck_assert(minmea_scan("10-", "f", &f) == false);
+    ck_assert(minmea_scan("+-10", "f", &f) == false);
+    ck_assert(minmea_scan("12..45", "f", &f) == false);
+    ck_assert(minmea_scan("blah", "f", &f) == false);
+    ck_assert(minmea_scan("12.3.4", "f", &f) == false);
+
+    ck_assert(minmea_scan(",", "f", &f) == true);
+    ck_assert_int_eq(f.scale, 0);
+    ck_assert(minmea_scan("", "f", &f) == true);
+    ck_assert_int_eq(f.scale, 0);
+
+    ck_assert(minmea_scan("42", "f", &f) == true);
+    ck_assert_int_eq(f.value, 42);
+    ck_assert_int_eq(f.scale, 1);
+
+    ck_assert(minmea_scan("15.345", "f", &f) == true);
+    ck_assert_int_eq(f.value, 15345);
+    ck_assert_int_eq(f.scale, 1000);
+
+    ck_assert(minmea_scan("-1.23,V", "f", &f) == true);
+    ck_assert_int_eq(f.value, -123);
+    ck_assert_int_eq(f.scale, 100);
+
+    /* the guaranteed range is 32 bits which translates to +-180 degrees
+     * with 5 decimal digits. make sure we support that. */
+    ck_assert(minmea_scan("18000.00000", "f", &f) == true);
+    ck_assert_int_eq(f.value, 1800000000);
+    ck_assert_int_eq(f.scale, 100000);
+    ck_assert(minmea_scan("-18000.00000", "f", &f) == true);
+    ck_assert_int_eq(f.value, -1800000000);
+    ck_assert_int_eq(f.scale, 100000);
+
+    if (sizeof(int_least32_t) == 4) {
+        /* fits in 32 bits */
+        ck_assert(minmea_scan("2147483647", "f", &f) == true);
+        ck_assert_int_eq(f.value, 2147483647);
+        ck_assert_int_eq(f.scale, 1);
+        /* doesn't fit, truncate precision */
+        ck_assert(minmea_scan("2147483.648", "f", &f) == true);
+        ck_assert_int_eq(f.value, 214748364);
+        ck_assert_int_eq(f.scale, 100);
+        /* doesn't fit, bail out */
+        ck_assert(minmea_scan("2147483648", "f", &f) == false);
+    } else if (sizeof(int_least32_t) == 8) {
+        /* Casting to int64_t is ugly, but otherwise we get this on machines with 32-bit int_least32_t:
+         * error: comparison is always false due to limited range of data type [-Werror=type-limits] */
+        /* fits in 64 bits */
+        ck_assert(minmea_scan("9223372036854775807", "f", &f) == true);
+        ck_assert_int_eq((int64_t) f.value, 9223372036854775807LL);
+        ck_assert_int_eq(f.scale, 1);
+        /* doesn't fit, truncate precision */
+        ck_assert(minmea_scan("9223372036854775.808", "f", &f) == true);
+        ck_assert_int_eq((int64_t) f.value, 922337203685477580LL);
+        ck_assert_int_eq(f.scale, 100);
+        /* doesn't fit, bail out */
+        ck_assert(minmea_scan("9223372036854775808", "f", &f) == false);
+    } else {
+        ck_abort_msg("your platform is esoteric. please fix this unit test.");
+    }
+
+    /* optional f.values */
+    ck_assert(minmea_scan("foo", "_;f", &f) == true);
+    ck_assert_int_eq(f.scale, 0);
+    ck_assert(minmea_scan("foo,", "_;f", &f) == true);
+    ck_assert_int_eq(f.scale, 0);
+    ck_assert(minmea_scan("foo,12.3", "_;f", &f) == true);
+    ck_assert_int_eq(f.value, 123);
+    ck_assert_int_eq(f.scale, 10);
+
+    /* accept spaces at the start of the field. some modules do this, unfortunately. */
+    ck_assert(minmea_scan(" -1.23,V", "f", &f) == true);
+    ck_assert_int_eq(f.value, -123);
+    ck_assert_int_eq(f.scale, 100);
+    ck_assert(minmea_scan("     -4.56,V", "f", &f) == true);
+    ck_assert_int_eq(f.value, -456);
+    ck_assert_int_eq(f.scale, 100);
+    ck_assert(minmea_scan("-3.33 ,V", "f", &f) == false);
+    ck_assert(minmea_scan(" -3.33 ,V", "f", &f) == false);
+    ck_assert(minmea_scan("-3. 33,V", "f", &f) == false);
+    ck_assert(minmea_scan("0 .0,V", "f", &f) == false);
+}
+END_TEST
+
+START_TEST(test_minmea_scan_i)
+{
+    int value, extra;
+
+    // valid parses
+    ck_assert(minmea_scan("14", "i", &value) == true);
+    ck_assert_int_eq(value, 14);
+    ck_assert(minmea_scan("-1234", "i", &value) == true);
+    ck_assert_int_eq(value, -1234);
+
+    // empty field
+    ck_assert(minmea_scan("", "i", &value) == true);
+    ck_assert_int_eq(value, 0);
+
+    // invalid value
+    ck_assert(minmea_scan("foo", "i", &value) == false);
+
+    // missing field
+    ck_assert(minmea_scan("41", "ii", &value, &extra) == false);
+
+    /* optional values */
+    ck_assert(minmea_scan("10", "i;i", &value, &extra) == true);
+    ck_assert_int_eq(value, 10);
+    ck_assert(minmea_scan("20,30", "i;i", &value, &extra) == true);
+    ck_assert_int_eq(value, 20);
+    ck_assert_int_eq(extra, 30);
+    ck_assert(minmea_scan("42,foo", "i;i", &value, &extra) == false);
+}
+END_TEST
+
+START_TEST(test_minmea_scan_s)
+{
+    char value[MINMEA_MAX_SENTENCE_LENGTH];
+
+    ck_assert(minmea_scan(",bar,baz", "s", value) == true);
+    ck_assert_str_eq(value, "");
+    ck_assert(minmea_scan("foo,bar,baz", "s", value) == true);
+    ck_assert_str_eq(value, "foo");
+
+    ck_assert(minmea_scan("dummy", "_;s", value) == true);
+    ck_assert_str_eq(value, "");
+    ck_assert(minmea_scan("dummy,foo", "_;s", value) == true);
+    ck_assert_str_eq(value, "foo");
+    ck_assert(minmea_scan("dummy,", "_;s", value) == true);
+    ck_assert_str_eq(value, "");
+}
+END_TEST
+
+START_TEST(test_minmea_scan_t)
+{
+    char buf[7];
+    buf[sizeof(buf)-1] = 0x42;
+
+    ck_assert(minmea_scan("$GPRM,foo,bar,baz", "t", buf) == false);
+    ck_assert(minmea_scan("GPRMC,foo,bar,baz", "t", buf) == false);
+
+    ck_assert(minmea_scan("$GPRMC,foo,bar,baz", "t", buf) == true);
+    ck_assert_str_eq(buf, "GPRMC");
+
+    ck_assert(buf[sizeof(buf)-1] == 0x42);
+}
+END_TEST
+
+START_TEST(test_minmea_scan_D)
+{
+    struct minmea_date d;
+
+    ck_assert(minmea_scan("$GPXXX,3112", "_D", &d) == false);
+    ck_assert(minmea_scan("$GPXXX,foobar", "_D", &d) == false);
+
+    ck_assert(minmea_scan("$GPXXX,311299", "_D", &d) == true);
+    ck_assert_int_eq(d.day, 31);
+    ck_assert_int_eq(d.month, 12);
+    ck_assert_int_eq(d.year, 99);
+
+    ck_assert(minmea_scan("$GPXXX,,,,,,,,,nope", "_D", &d) == true);
+    ck_assert_int_eq(d.day, -1);
+    ck_assert_int_eq(d.month, -1);
+    ck_assert_int_eq(d.year, -1);
+}
+END_TEST
+
+START_TEST(test_minmea_scan_T)
+{
+    struct minmea_time t;
+
+    ck_assert(minmea_scan("$GPXXX,2359", "_T", &t) == false);
+    ck_assert(minmea_scan("$GPXXX,foobar", "_T", &t) == false);
+
+    ck_assert(minmea_scan("$GPXXX,235960", "_T", &t) == true);
+    ck_assert_int_eq(t.hours, 23);
+    ck_assert_int_eq(t.minutes, 59);
+    ck_assert_int_eq(t.seconds, 60);
+    ck_assert_int_eq(t.microseconds, 0);
+
+    ck_assert(minmea_scan("$GPXXX,213700.001", "_T", &t) == true);
+    ck_assert_int_eq(t.hours, 21);
+    ck_assert_int_eq(t.minutes, 37);
+    ck_assert_int_eq(t.seconds, 0);
+    ck_assert_int_eq(t.microseconds, 1000);
+
+    ck_assert(minmea_scan("$GPXXX,,,,,,,nope", "_T", &t) == true);
+    ck_assert_int_eq(t.hours, -1);
+    ck_assert_int_eq(t.minutes, -1);
+    ck_assert_int_eq(t.seconds, -1);
+    ck_assert_int_eq(t.microseconds, -1);
+}
+END_TEST
+
+START_TEST(test_minmea_scan_complex1)
+{
+    const char *sentence = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47\r\n";
+    char type[6];
+    struct minmea_time t;
+    struct minmea_float latitude; int latitude_direction;
+    struct minmea_float longitude; int longitude_direction;
+    int fix_quality;
+    int satellites;
+    struct minmea_float hdop;
+    struct minmea_float altitude; char altitude_units;
+    struct minmea_float height; char height_units;
+    ck_assert(minmea_scan(sentence, "tTfdfdiiffcfc__",
+        type,
+        &t,
+        &latitude, &latitude_direction,
+        &longitude, &longitude_direction,
+        &fix_quality,
+        &satellites,
+        &hdop,
+        &altitude, &altitude_units,
+        &height, &height_units) == true);
+    ck_assert_str_eq(type, "GPGGA");
+    ck_assert_int_eq(t.hours, 12);
+    ck_assert_int_eq(t.minutes, 35);
+    ck_assert_int_eq(t.seconds, 19);
+    ck_assert_int_eq(latitude.value, 4807038);
+    ck_assert_int_eq(latitude.scale, 1000);
+    ck_assert_int_eq(latitude_direction, 1);
+    ck_assert_int_eq(longitude.value, 1131000);
+    ck_assert_int_eq(longitude.scale, 1000);
+    ck_assert_int_eq(longitude_direction, 1);
+    ck_assert_int_eq(fix_quality, 1);
+    ck_assert_int_eq(satellites, 8);
+    ck_assert_int_eq(hdop.value, 9);
+    ck_assert_int_eq(hdop.scale, 10);
+    ck_assert_int_eq(altitude.value, 5454);
+    ck_assert_int_eq(altitude.scale, 10);
+    ck_assert_int_eq(altitude_units, 'M');
+    ck_assert_int_eq(height.value, 469);
+    ck_assert_int_eq(height.scale, 10);
+    ck_assert_int_eq(height_units, 'M');
+
+}
+END_TEST
+
+START_TEST(test_minmea_scan_complex2)
+{
+    const char *sentence = "$GPBWC,081837,,,,,,T,,M,,N,*13";
+    char type[6];
+    struct minmea_time t;
+    struct minmea_float latitude; int latitude_direction;
+    struct minmea_float longitude; int longitude_direction;
+    struct minmea_float bearing_true; char bearing_true_mark;
+    struct minmea_float bearing_magnetic; char bearing_magnetic_mark;
+    struct minmea_float distance; char distance_units;
+    char name[MINMEA_MAX_SENTENCE_LENGTH];
+    ck_assert(minmea_scan(sentence, "tTfdfdfcfcfcs",
+        type,
+        &t,
+        &latitude, &latitude_direction,
+        &longitude, &longitude_direction,
+        &bearing_true, &bearing_true_mark,
+        &bearing_magnetic, &bearing_magnetic_mark,
+        &distance, &distance_units,
+        name) == true);
+    ck_assert_str_eq(type, "GPBWC");
+    ck_assert_int_eq(t.hours, 8);
+    ck_assert_int_eq(t.minutes, 18);
+    ck_assert_int_eq(t.seconds, 37);
+    ck_assert_int_eq(latitude.scale, 0);
+    ck_assert_int_eq(latitude_direction, 0);
+    ck_assert_int_eq(longitude.scale, 0);
+    ck_assert_int_eq(longitude_direction, 0);
+    ck_assert_int_eq(bearing_true.scale, 0);
+    ck_assert_int_eq(bearing_true_mark, 'T');
+    ck_assert_int_eq(bearing_magnetic.scale, 0);
+    ck_assert_int_eq(bearing_magnetic_mark, 'M');
+    ck_assert_int_eq(distance.scale, 0);
+    ck_assert_int_eq(distance_units, 'N');
+    ck_assert_str_eq(name, "");
+}
+END_TEST
+
+START_TEST(test_minmea_scan_complex3)
+{
+    const char *sentence = "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58";
+    char type[6];
+    struct minmea_time t;
+    struct minmea_float rms_deviation;
+    struct minmea_float semi_major_deviation;
+    struct minmea_float semi_minor_deviation;
+    struct minmea_float semi_major_orientation;
+    struct minmea_float latitude_error_deviation;
+    struct minmea_float longitude_error_deviation;
+    struct minmea_float altitude_error_deviation;
+    ck_assert(minmea_scan(sentence, "tTfffffff",
+        type,
+        &t,
+        &rms_deviation,
+        &semi_major_deviation,
+        &semi_minor_deviation,
+        &semi_major_orientation,
+        &latitude_error_deviation,
+        &longitude_error_deviation,
+        &altitude_error_deviation) == true);
+    ck_assert_str_eq(type, "GPGST");
+    ck_assert_int_eq(t.hours, 2);
+    ck_assert_int_eq(t.minutes, 46);
+    ck_assert_int_eq(t.seconds, 3);
+    ck_assert_int_eq(t.microseconds, 0);
+    ck_assert_int_eq(rms_deviation.value, 32);
+    ck_assert_int_eq(rms_deviation.scale, 10);
+    ck_assert_int_eq(semi_major_deviation.value, 66);
+    ck_assert_int_eq(semi_major_deviation.scale, 10);
+    ck_assert_int_eq(semi_minor_deviation.value, 47);
+    ck_assert_int_eq(semi_minor_deviation.scale, 10);
+    ck_assert_int_eq(semi_major_orientation.value, 473);
+    ck_assert_int_eq(semi_major_orientation.scale, 10);
+    ck_assert_int_eq(latitude_error_deviation.value, 58);
+    ck_assert_int_eq(latitude_error_deviation.scale, 10);
+    ck_assert_int_eq(longitude_error_deviation.value, 56);
+    ck_assert_int_eq(longitude_error_deviation.scale, 10);
+    ck_assert_int_eq(altitude_error_deviation.value, 220);
+    ck_assert_int_eq(altitude_error_deviation.scale, 10);
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gbs1)
+{
+    const char *sentence = "$GNGBS,170556.00,3.0,2.9,8.3,,,,";
+    struct minmea_sentence_gbs frame = {};
+    static const struct minmea_sentence_gbs expected = {
+	    .time = { 17, 5, 56, 0 },
+	    .err_latitude = { 30, 10 },
+	    .err_longitude = { 29, 10 },
+	    .err_altitude = { 83, 10 },
+	    .svid = 0,
+	    .prob = { 0, 0 },
+	    .bias = { 0, 0 },
+	    .stddev = { 0, 0 },
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == false);
+    ck_assert(minmea_parse_gbs(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gbs2)
+{
+    const char *sentence = "$GPGBS,015509.00,-0.031,-0.186,0.219,19,0.000,-0.354,6.972*4D";
+    struct minmea_sentence_gbs frame = {};
+    static const struct minmea_sentence_gbs expected = {
+        .time = { 1, 55, 9 },
+        .err_latitude = { -31, 1000 },
+        .err_longitude = { -186, 1000 },
+        .err_altitude = { 219, 1000 },
+        .svid = 19,
+        .prob = { 0, 1000 },
+        .bias = { -354, 1000 },
+        .stddev = { 6972, 1000 },
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gbs(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_rmc1)
+{
+    const char *sentence = "$GPRMC,081836.75,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E";
+    struct minmea_sentence_rmc frame = {};
+    static const struct minmea_sentence_rmc expected = {
+        .time = { 8, 18, 36, 750000 },
+        .valid = true,
+        .latitude = { -375165, 100 },
+        .longitude = { 1450736, 100 },
+        .speed = { 0, 10 },
+        .course = { 3600, 10 },
+        .date = { 13, 9, 98 },
+        .variation = { 113, 10 },
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == false);
+    ck_assert(minmea_parse_rmc(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_rmc2)
+{
+    const char *sentence = "$GPRMC,,A,3751.65,N,14507.36,W,,,,,";
+    struct minmea_sentence_rmc frame = {};
+    static const struct minmea_sentence_rmc expected = {
+        .time = { -1, -1, -1, -1 },
+        .valid = true,
+        .latitude = { 375165, 100 },
+        .longitude = { -1450736, 100 },
+        .date = { -1, -1, -1 },
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == false);
+    ck_assert(minmea_parse_rmc(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gga1)
+{
+    const char *sentence = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47";
+    struct minmea_sentence_gga frame = {};
+    struct minmea_sentence_gga expected = {};
+    expected.time = (struct minmea_time) { 12, 35, 19, 0 };
+    expected.latitude = (struct minmea_float) { 4807038, 1000 };
+    expected.longitude = (struct minmea_float) { 1131000, 1000 };
+    expected.fix_quality = 1;
+    expected.satellites_tracked = 8;
+    expected.hdop = (struct minmea_float) { 9, 10 };
+    expected.altitude = (struct minmea_float) { 5454, 10 };
+    expected.altitude_units = 'M';
+    expected.height = (struct minmea_float) { 469, 10 };
+    expected.height_units = 'M';
+    expected.dgps_age = (struct minmea_float) { 0, 0 };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gga(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gst1)
+{
+    const char *sentence = "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58";
+    struct minmea_sentence_gst frame = {};
+    struct minmea_sentence_gst expected = {
+        .time = { 2, 46, 3, 0 },
+        .rms_deviation = { 32, 10 },
+        .semi_major_deviation = { 66, 10 },
+        .semi_minor_deviation = { 47, 10 },
+        .semi_major_orientation = { 473, 10 },
+        .latitude_error_deviation = { 58, 10 },
+        .longitude_error_deviation = { 56, 10 },
+        .altitude_error_deviation = { 220, 10 },
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gst(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gsa1)
+{
+    const char *sentence = "$GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39";
+    struct minmea_sentence_gsa frame = {};
+    static const struct minmea_sentence_gsa expected = {
+        .mode = MINMEA_GPGSA_MODE_AUTO,
+        .fix_type = MINMEA_GPGSA_FIX_3D,
+        .sats = { 4, 5, 0, 9, 12, 0, 0, 24, 0, 0, 0, 0 },
+        .pdop = { 25, 10 },
+        .hdop = { 13, 10 },
+        .vdop = { 21, 10 },
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gsa(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gll1)
+{
+    const char *sentence = "$GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41";
+    struct minmea_sentence_gll frame;
+    struct minmea_sentence_gll expected;
+
+    // clear structs before initialization to enable use of memcmp()
+    // todo: add for other structs
+    memset(&frame, 0, sizeof(frame));
+    memset(&expected, 0, sizeof(expected));
+
+    expected.latitude = (struct minmea_float){ 37232475, 10000 };
+    expected.longitude = (struct minmea_float){ -121583416, 10000 };
+    expected.time = (struct minmea_time){ 16, 12, 29, 487000 };
+    expected.status = MINMEA_GLL_STATUS_DATA_VALID;
+    expected.mode = MINMEA_FAA_MODE_AUTONOMOUS;
+
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gll(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gll2)
+{
+    const char *sentence = "$GPGLL,4916.45,N,12311.12,W,225444,A";
+    struct minmea_sentence_gll frame = {};
+    struct minmea_sentence_gll expected = {
+        .latitude = { 491645, 100 },
+        .longitude = { -1231112, 100 },
+        .time = { 22, 54, 44 },
+        .status = MINMEA_GLL_STATUS_DATA_VALID,
+        .mode = 0,
+    };
+
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == false);
+    ck_assert(minmea_parse_gll(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gsv1)
+{
+    const char *sentence = "$GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D";
+    struct minmea_sentence_gsv frame = {};
+    static const struct minmea_sentence_gsv expected = {
+        .total_msgs = 3,
+        .msg_nr = 3,
+        .total_sats = 11,
+        .sats = {
+            {
+                .nr = 22,
+                .elevation = 42,
+                .azimuth = 67,
+                .snr = 42
+            },
+            {
+                .nr = 24,
+                .elevation = 14,
+                .azimuth = 311,
+                .snr = 43
+            },
+            {
+                .nr = 27,
+                .elevation = 5,
+                .azimuth = 244,
+                .snr = 0
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            }
+        }
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gsv(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gsv2)
+{
+    const char *sentence = "$GPGSV,4,2,11,08,51,203,30,09,45,215,28*75";
+    struct minmea_sentence_gsv frame = {};
+    static const struct minmea_sentence_gsv expected = {
+        .total_msgs = 4,
+        .msg_nr = 2,
+        .total_sats = 11,
+        .sats = {
+            {
+                .nr = 8,
+                .elevation = 51,
+                .azimuth = 203,
+                .snr = 30
+            },
+            {
+                .nr = 9,
+                .elevation = 45,
+                .azimuth = 215,
+                .snr = 28
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            }
+        }
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gsv(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gsv3)
+{
+    const char *sentence = "$GPGSV,4,4,13,39,31,170,27*40";
+    struct minmea_sentence_gsv frame = {};
+    static const struct minmea_sentence_gsv expected = {
+        .total_msgs = 4,
+        .msg_nr = 4,
+        .total_sats = 13,
+        .sats = {
+            {
+                .nr = 39,
+                .elevation = 31,
+                .azimuth = 170,
+                .snr = 27
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            }
+        }
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gsv(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gsv4)
+{
+    const char *sentence = "$GPGSV,4,4,13*7B";
+    struct minmea_sentence_gsv frame = {};
+    static const struct minmea_sentence_gsv expected = {
+        .total_msgs = 4,
+        .msg_nr = 4,
+        .total_sats = 13,
+        .sats = {
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            },
+            {
+                .nr = 0,
+                .elevation = 0,
+                .azimuth = 0,
+                .snr = 0
+            }
+        }
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gsv(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_gsv5)
+{
+    const char *sentence = "$GPGSV,4,1,13,02,28,259,33,04,12,212,27,05,34,305,30,07,79,138,*7F";
+    struct minmea_sentence_gsv frame = {};
+    static const struct minmea_sentence_gsv expected = {
+        .total_msgs = 4,
+        .msg_nr = 1,
+        .total_sats = 13,
+        .sats = {
+            {
+                .nr = 2,
+                .elevation = 28,
+                .azimuth = 259,
+                .snr = 33
+            },
+            {
+                .nr = 4,
+                .elevation = 12,
+                .azimuth = 212,
+                .snr = 27
+            },
+            {
+                .nr = 5,
+                .elevation = 34,
+                .azimuth = 305,
+                .snr = 30
+            },
+            {
+                .nr = 7,
+                .elevation = 79,
+                .azimuth = 138,
+                .snr = 0
+            }
+        }
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_gsv(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+
+START_TEST(test_minmea_parse_vtg1)
+{
+    const char *sentence = "$GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48";
+    // clear structs before initialization to enable use of memcmp()
+    struct minmea_sentence_vtg frame = {};
+    struct minmea_sentence_vtg expected = {};
+
+    expected = (struct minmea_sentence_vtg){
+        .true_track_degrees = { 547, 10 },
+        .magnetic_track_degrees = { 344, 10 },
+        .speed_knots = { 55, 10 },
+        .speed_kph = { 102, 10 },
+        .faa_mode = 0,
+    };
+
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_vtg(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_vtg2)
+{
+    const char *sentence = "$GPVTG,188.36,T,,M,0.820,N,1.519,K,A*3F";
+    // clear structs before initialization to enable use of memcmp()
+    struct minmea_sentence_vtg frame = {};
+    struct minmea_sentence_vtg expected = {};
+
+    expected = (struct minmea_sentence_vtg){
+        .true_track_degrees = { 18836, 100 },
+        .magnetic_track_degrees = { 0, 0 },
+        .speed_knots = { 820, 1000 },
+        .speed_kph = { 1519, 1000 },
+        .faa_mode = MINMEA_FAA_MODE_AUTONOMOUS,
+    };
+
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_vtg(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_vtg3)
+{
+    // https://github.com/kosma/minmea/issues/57
+    const char *sentence = "$GNVTG,,,,,,,,,N*2E";
+    // clear structs before initialization to enable use of memcmp()
+    struct minmea_sentence_vtg frame = {};
+    struct minmea_sentence_vtg expected = {};
+    expected = (struct minmea_sentence_vtg){
+        .true_track_degrees = { 0, 0 },
+        .magnetic_track_degrees = { 0, 0 },
+        .speed_knots = { 0, 0 },
+        .speed_kph = { 0, 0 },
+        .faa_mode = MINMEA_FAA_MODE_NOT_VALID,
+    };
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_vtg(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_parse_zda1)
+{
+    const char *sentence = "$GPZDA,160012.71,11,03,2004,-1,00*7D";
+    struct minmea_sentence_zda frame = {};
+    struct minmea_sentence_zda expected = {};
+
+    expected = (struct minmea_sentence_zda) {
+        .time = { 16, 0, 12, 710000 },
+        .date = { 11, 3, 2004 },
+        .hour_offset = -1,
+        .minute_offset = 0,
+    };
+
+    ck_assert(minmea_check(sentence, false) == true);
+    ck_assert(minmea_check(sentence, true) == true);
+    ck_assert(minmea_parse_zda(&frame, sentence) == true);
+    ck_assert(!memcmp(&frame, &expected, sizeof(frame)));
+}
+END_TEST
+
+START_TEST(test_minmea_usage1)
+{
+    const char *sentences[] = {
+        "$GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62",
+        "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47",
+        "$GNGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1",
+        "$GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41",
+        "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58",
+        "$GPVTG,096.5,T,083.5,M,0.0,N,0.0,K,D*22",
+        NULL,
+    };
+
+    for (const char **sentence=sentences; *sentence; sentence++) {
+        switch (minmea_sentence_id(*sentence, false)) {
+            case MINMEA_SENTENCE_RMC: {
+                struct minmea_sentence_rmc frame;
+                ck_assert(minmea_parse_rmc(&frame, *sentence) == true);
+            } break;
+
+            case MINMEA_SENTENCE_GGA: {
+                struct minmea_sentence_gga frame;
+                ck_assert(minmea_parse_gga(&frame, *sentence) == true);
+            } break;
+
+            case MINMEA_SENTENCE_GSA: {
+                struct minmea_sentence_gsa frame;
+                ck_assert(minmea_parse_gsa(&frame, *sentence) == true);
+            } break;
+
+            case MINMEA_SENTENCE_GLL: {
+                struct minmea_sentence_gll frame;
+                ck_assert(minmea_parse_gll(&frame, *sentence) == true);
+            } break;
+
+            case MINMEA_SENTENCE_GST: {
+                struct minmea_sentence_gst frame;
+                ck_assert(minmea_parse_gst(&frame, *sentence) == true);
+            } break;
+
+            case MINMEA_SENTENCE_VTG: {
+                struct minmea_sentence_vtg frame;
+                ck_assert(minmea_parse_vtg(&frame, *sentence) == true);
+            } break;
+
+            default: {
+            } break;
+        }
+
+        char talker[3];
+        char expected[3] = { (*sentence)[1], (*sentence)[2], '\0' };
+        ck_assert(minmea_talker_id(talker, *sentence) == true);
+        ck_assert_str_eq(talker, expected);
+    }
+}
+END_TEST
+
+START_TEST(test_minmea_gettime)
+{
+    struct minmea_date d = { 14, 2, 14 };
+    struct minmea_time t = { 13, 0, 9, 123456 };
+    struct tm tm;
+    struct timespec ts;
+    ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+    ck_assert_int_eq(tm.tm_year, 2014-1900);
+    ck_assert_int_eq(tm.tm_mon, 1);
+    ck_assert_int_eq(tm.tm_mday, 14);
+    ck_assert_int_eq(tm.tm_hour, 13);
+    ck_assert_int_eq(tm.tm_min, 0);
+    ck_assert_int_eq(tm.tm_sec, 9);
+    ck_assert_int_eq(ts.tv_sec, 1392382809);
+    ck_assert_int_eq(ts.tv_nsec, 123456000);
+
+    d.year = -1;
+    ck_assert(minmea_getdatetime(&tm, &d, &t) != 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) != 0);
+    d.year = 14;
+
+    t.hours = -1;
+    ck_assert(minmea_getdatetime(&tm, &d, &t) != 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) != 0);
+    t.hours = 13;
+
+    /* two digit year conversions */
+    d.year = 80;
+    ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+    ck_assert_int_eq(tm.tm_year, 1980-1900);
+    ck_assert_int_eq(ts.tv_sec, 319381209);      /* 1980 */
+    d.year = 37;
+    ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+    ck_assert_int_eq(tm.tm_year, 2037-1900);
+    ck_assert_int_eq(ts.tv_sec, 2118229209);     /* 2037 */
+    /* skip >= 2038 tests on 32-bit time_t platforms */
+    if (sizeof(time_t) == sizeof(int64_t)) {
+        d.year = 79;
+        ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+        ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+        ck_assert_int_eq(tm.tm_year, 2079-1900);
+        ck_assert_int_eq(ts.tv_sec, 3443605209); /* 2079 */
+    }
+
+    /* four digit year conversions */
+    d.year = 1979;
+    ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+    ck_assert_int_eq(tm.tm_year, 1979-1900);
+    ck_assert_int_eq(ts.tv_sec, 287845209);
+    d.year = 1980;
+    ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+    ck_assert_int_eq(tm.tm_year, 1980-1900);
+    ck_assert_int_eq(ts.tv_sec, 319381209);
+    d.year = 2037;
+    ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+    ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+    ck_assert_int_eq(tm.tm_year, 2037-1900);
+    ck_assert_int_eq(ts.tv_sec, 2118229209);
+    /* skip >= 2038 tests on 32-bit time_t platforms */
+    if (sizeof(time_t) == sizeof(int64_t)) {
+        d.year = 2079;
+        ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+        ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+        ck_assert_int_eq(tm.tm_year, 2079-1900);
+        ck_assert_int_eq(ts.tv_sec, 3443605209);
+        d.year = 2080;
+        ck_assert(minmea_getdatetime(&tm, &d, &t) == 0);
+        ck_assert(minmea_gettime(&ts, &d, &t) == 0);
+        ck_assert_int_eq(tm.tm_year, 2080-1900);
+        ck_assert_int_eq(ts.tv_sec, 3475141209);
+    }
+}
+END_TEST
+
+START_TEST(test_minmea_rescale)
+{
+    /* basic and edge cases. */
+    ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 42, 0 }, 3), 0);
+    ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1234, 10 }, 1), 123);
+    ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1235, 10 }, 1), 124);
+    ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1234, 10 }, 1000), 123400);
+
+    /* round towards zero. */
+    ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1234, 10 }, 1), -123);
+    ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1235, 10 }, 1), -124);
+    ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1236, 10 }, 1), -124);
+
+    /* shouldn't overflow on large numbers. */
+    ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 510693608, 100000 }, 10000), 51069361);
+}
+END_TEST
+
+/* The float values used in tests should be exactly representable under IEEE754;
+ * false negatives will occur otherwise. */
+
+#define assert_float_eq(x, y) ck_assert(fabsf((x) - (y)) <= 0.0f)
+
+START_TEST(test_minmea_float)
+{
+    ck_assert(isnan(minmea_tofloat(&(struct minmea_float) { 42, 0 })));
+    assert_float_eq(minmea_tofloat(&(struct minmea_float) { 7, 1 }), 7.0f);
+    assert_float_eq(minmea_tofloat(&(struct minmea_float) { -200, 100 }), -2.0f);
+    assert_float_eq(minmea_tofloat(&(struct minmea_float) { 15, 10 }), 1.5f);
+}
+END_TEST
+
+START_TEST(test_minmea_coord)
+{
+    ck_assert(isnan(minmea_tocoord(&(struct minmea_float) { 42, 0 })));
+    assert_float_eq(minmea_tocoord(&(struct minmea_float) { 4200, 1 }), 42.0f);
+    assert_float_eq(minmea_tocoord(&(struct minmea_float) { 420000, 100 }), 42.0f);
+    assert_float_eq(minmea_tocoord(&(struct minmea_float) { 423000, 100 }), 42.5f);
+}
+END_TEST
+
+static Suite *minmea_suite(void)
+{
+    Suite *s = suite_create ("minmea");
+
+    TCase *tc_checksum = tcase_create("minmea_checksum");
+    tcase_add_test(tc_checksum, test_minmea_checksum);
+    suite_add_tcase(s, tc_checksum);
+
+    TCase *tc_check = tcase_create("minmea_check");
+    tcase_add_test(tc_check, test_minmea_check);
+    suite_add_tcase(s, tc_check);
+
+    TCase *tc_scan = tcase_create("minmea_scan");
+    tcase_add_test(tc_scan, test_minmea_scan_c);
+    tcase_add_test(tc_scan, test_minmea_scan_d);
+    tcase_add_test(tc_scan, test_minmea_scan_f);
+    tcase_add_test(tc_scan, test_minmea_scan_i);
+    tcase_add_test(tc_scan, test_minmea_scan_s);
+    tcase_add_test(tc_scan, test_minmea_scan_t);
+    tcase_add_test(tc_scan, test_minmea_scan_D);
+    tcase_add_test(tc_scan, test_minmea_scan_T);
+    tcase_add_test(tc_scan, test_minmea_scan_complex1);
+    tcase_add_test(tc_scan, test_minmea_scan_complex2);
+    tcase_add_test(tc_scan, test_minmea_scan_complex3);
+    suite_add_tcase(s, tc_scan);
+
+    TCase *tc_parse = tcase_create("minmea_parse");
+    tcase_add_test(tc_parse, test_minmea_parse_gbs1);
+    tcase_add_test(tc_parse, test_minmea_parse_gbs2);
+    tcase_add_test(tc_parse, test_minmea_parse_rmc1);
+    tcase_add_test(tc_parse, test_minmea_parse_rmc2);
+    tcase_add_test(tc_parse, test_minmea_parse_gga1);
+    tcase_add_test(tc_parse, test_minmea_parse_gsa1);
+    tcase_add_test(tc_parse, test_minmea_parse_gll1);
+    tcase_add_test(tc_parse, test_minmea_parse_gll2);
+    tcase_add_test(tc_parse, test_minmea_parse_gst1);
+    tcase_add_test(tc_parse, test_minmea_parse_gsv1);
+    tcase_add_test(tc_parse, test_minmea_parse_gsv2);
+    tcase_add_test(tc_parse, test_minmea_parse_gsv3);
+    tcase_add_test(tc_parse, test_minmea_parse_gsv4);
+    tcase_add_test(tc_parse, test_minmea_parse_gsv5);
+    tcase_add_test(tc_parse, test_minmea_parse_vtg1);
+    tcase_add_test(tc_parse, test_minmea_parse_vtg2);
+    tcase_add_test(tc_parse, test_minmea_parse_vtg3);
+    tcase_add_test(tc_parse, test_minmea_parse_zda1);
+    suite_add_tcase(s, tc_parse);
+
+    TCase *tc_usage = tcase_create("minmea_usage");
+    tcase_add_test(tc_usage, test_minmea_usage1);
+    suite_add_tcase(s, tc_usage);
+
+    TCase *tc_utils = tcase_create("minmea_utils");
+    tcase_add_test(tc_utils, test_minmea_gettime);
+    tcase_add_test(tc_utils, test_minmea_rescale);
+    tcase_add_test(tc_utils, test_minmea_float);
+    tcase_add_test(tc_utils, test_minmea_coord);
+    suite_add_tcase(s, tc_utils);
+
+    return s;
+}
+
+int main(void)
+{
+    int number_failed;
+    Suite *s = minmea_suite();
+    SRunner *sr = srunner_create(s);
+    srunner_run_all(sr, CK_NORMAL);
+    number_failed = srunner_ntests_failed(sr);
+    srunner_free(sr);
+    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+/* vim: set ts=4 sw=4 et: */

+ 0 - 640
minmea.c

@@ -1,640 +0,0 @@
-/*
- * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
- * This program is free software. It comes without any warranty, to the extent
- * permitted by applicable law. You can redistribute it and/or modify it under
- * the terms of the Do What The Fuck You Want To Public License, Version 2, as
- * published by Sam Hocevar. See the COPYING file for more details.
- */
-
-#include "minmea.h"
-
-#include <stdlib.h>
-#include <string.h>
-#include <stdarg.h>
-
-#define boolstr(s) ((s) ? "true" : "false")
-
-static int hex2int(char c) {
-    if(c >= '0' && c <= '9') return c - '0';
-    if(c >= 'A' && c <= 'F') return c - 'A' + 10;
-    if(c >= 'a' && c <= 'f') return c - 'a' + 10;
-    return -1;
-}
-
-uint8_t minmea_checksum(const char* sentence) {
-    // Support senteces with or without the starting dollar sign.
-    if(*sentence == '$') sentence++;
-
-    uint8_t checksum = 0x00;
-
-    // The optional checksum is an XOR of all bytes between "$" and "*".
-    while(*sentence && *sentence != '*') checksum ^= *sentence++;
-
-    return checksum;
-}
-
-bool minmea_check(const char* sentence, bool strict) {
-    uint8_t checksum = 0x00;
-
-    // A valid sentence starts with "$".
-    if(*sentence++ != '$') return false;
-
-    // The optional checksum is an XOR of all bytes between "$" and "*".
-    while(*sentence && *sentence != '*' && isprint((unsigned char)*sentence))
-        checksum ^= *sentence++;
-
-    // If checksum is present...
-    if(*sentence == '*') {
-        // Extract checksum.
-        sentence++;
-        int upper = hex2int(*sentence++);
-        if(upper == -1) return false;
-        int lower = hex2int(*sentence++);
-        if(lower == -1) return false;
-        int expected = upper << 4 | lower;
-
-        // Check for checksum mismatch.
-        if(checksum != expected) return false;
-    } else if(strict) {
-        // Discard non-checksummed frames in strict mode.
-        return false;
-    }
-
-    // The only stuff allowed at this point is a newline.
-    while(*sentence == '\r' || *sentence == '\n') {
-        sentence++;
-    }
-
-    if(*sentence) {
-        return false;
-    }
-
-    return true;
-}
-
-bool minmea_scan(const char* sentence, const char* format, ...) {
-    bool result = false;
-    bool optional = false;
-
-    if(sentence == NULL) return false;
-
-    va_list ap;
-    va_start(ap, format);
-
-    const char* field = sentence;
-#define next_field()                                 \
-    do {                                             \
-        /* Progress to the next field. */            \
-        while(minmea_isfield(*sentence)) sentence++; \
-        /* Make sure there is a field there. */      \
-        if(*sentence == ',') {                       \
-            sentence++;                              \
-            field = sentence;                        \
-        } else {                                     \
-            field = NULL;                            \
-        }                                            \
-    } while(0)
-
-    while(*format) {
-        char type = *format++;
-
-        if(type == ';') {
-            // All further fields are optional.
-            optional = true;
-            continue;
-        }
-
-        if(!field && !optional) {
-            // Field requested but we ran out if input. Bail out.
-            goto parse_error;
-        }
-
-        switch(type) {
-        case 'c': { // Single character field (char).
-            char value = '\0';
-
-            if(field && minmea_isfield(*field)) value = *field;
-
-            *va_arg(ap, char*) = value;
-        } break;
-
-        case 'd': { // Single character direction field (int).
-            int value = 0;
-
-            if(field && minmea_isfield(*field)) {
-                switch(*field) {
-                case 'N':
-                case 'E':
-                    value = 1;
-                    break;
-                case 'S':
-                case 'W':
-                    value = -1;
-                    break;
-                default:
-                    goto parse_error;
-                }
-            }
-
-            *va_arg(ap, int*) = value;
-        } break;
-
-        case 'f': { // Fractional value with scale (struct minmea_float).
-            int sign = 0;
-            int_least32_t value = -1;
-            int_least32_t scale = 0;
-
-            if(field) {
-                while(minmea_isfield(*field)) {
-                    if(*field == '+' && !sign && value == -1) {
-                        sign = 1;
-                    } else if(*field == '-' && !sign && value == -1) {
-                        sign = -1;
-                    } else if(isdigit((unsigned char)*field)) {
-                        int digit = *field - '0';
-                        if(value == -1) value = 0;
-                        if(value > (INT_LEAST32_MAX - digit) / 10) {
-                            /* we ran out of bits, what do we do? */
-                            if(scale) {
-                                /* truncate extra precision */
-                                break;
-                            } else {
-                                /* integer overflow. bail out. */
-                                goto parse_error;
-                            }
-                        }
-                        value = (10 * value) + digit;
-                        if(scale) scale *= 10;
-                    } else if(*field == '.' && scale == 0) {
-                        scale = 1;
-                    } else if(*field == ' ') {
-                        /* Allow spaces at the start of the field. Not NMEA
-                             * conformant, but some modules do this. */
-                        if(sign != 0 || value != -1 || scale != 0) goto parse_error;
-                    } else {
-                        goto parse_error;
-                    }
-                    field++;
-                }
-            }
-
-            if((sign || scale) && value == -1) goto parse_error;
-
-            if(value == -1) {
-                /* No digits were scanned. */
-                value = 0;
-                scale = 0;
-            } else if(scale == 0) {
-                /* No decimal point. */
-                scale = 1;
-            }
-            if(sign) value *= sign;
-
-            *va_arg(ap, struct minmea_float*) = (struct minmea_float){value, scale};
-        } break;
-
-        case 'i': { // Integer value, default 0 (int).
-            int value = 0;
-
-            if(field) {
-                char* endptr;
-                value = strtol(field, &endptr, 10);
-                if(minmea_isfield(*endptr)) goto parse_error;
-            }
-
-            *va_arg(ap, int*) = value;
-        } break;
-
-        case 's': { // String value (char *).
-            char* buf = va_arg(ap, char*);
-
-            if(field) {
-                while(minmea_isfield(*field)) *buf++ = *field++;
-            }
-
-            *buf = '\0';
-        } break;
-
-        case 't': { // NMEA talker+sentence identifier (char *).
-            // This field is always mandatory.
-            if(!field) goto parse_error;
-
-            if(field[0] != '$') goto parse_error;
-            for(int f = 0; f < 5; f++)
-                if(!minmea_isfield(field[1 + f])) goto parse_error;
-
-            char* buf = va_arg(ap, char*);
-            memcpy(buf, field + 1, 5);
-            buf[5] = '\0';
-        } break;
-
-        case 'D': { // Date (int, int, int), -1 if empty.
-            struct minmea_date* date = va_arg(ap, struct minmea_date*);
-
-            int d = -1, m = -1, y = -1;
-
-            if(field && minmea_isfield(*field)) {
-                // Always six digits.
-                for(int f = 0; f < 6; f++)
-                    if(!isdigit((unsigned char)field[f])) goto parse_error;
-
-                char dArr[] = {field[0], field[1], '\0'};
-                char mArr[] = {field[2], field[3], '\0'};
-                char yArr[] = {field[4], field[5], '\0'};
-                d = strtol(dArr, NULL, 10);
-                m = strtol(mArr, NULL, 10);
-                y = strtol(yArr, NULL, 10);
-            }
-
-            date->day = d;
-            date->month = m;
-            date->year = y;
-        } break;
-
-        case 'T': { // Time (int, int, int, int), -1 if empty.
-            struct minmea_time* time_ = va_arg(ap, struct minmea_time*);
-
-            int h = -1, i = -1, s = -1, u = -1;
-
-            if(field && minmea_isfield(*field)) {
-                // Minimum required: integer time.
-                for(int f = 0; f < 6; f++)
-                    if(!isdigit((unsigned char)field[f])) goto parse_error;
-
-                char hArr[] = {field[0], field[1], '\0'};
-                char iArr[] = {field[2], field[3], '\0'};
-                char sArr[] = {field[4], field[5], '\0'};
-                h = strtol(hArr, NULL, 10);
-                i = strtol(iArr, NULL, 10);
-                s = strtol(sArr, NULL, 10);
-                field += 6;
-
-                // Extra: fractional time. Saved as microseconds.
-                if(*field++ == '.') {
-                    uint32_t value = 0;
-                    uint32_t scale = 1000000LU;
-                    while(isdigit((unsigned char)*field) && scale > 1) {
-                        value = (value * 10) + (*field++ - '0');
-                        scale /= 10;
-                    }
-                    u = value * scale;
-                } else {
-                    u = 0;
-                }
-            }
-
-            time_->hours = h;
-            time_->minutes = i;
-            time_->seconds = s;
-            time_->microseconds = u;
-        } break;
-
-        case '_': { // Ignore the field.
-        } break;
-
-        default: { // Unknown.
-            goto parse_error;
-        }
-        }
-
-        next_field();
-    }
-
-    result = true;
-
-parse_error:
-    va_end(ap);
-    return result;
-}
-
-bool minmea_talker_id(char talker[3], const char* sentence) {
-    char type[6];
-    if(!minmea_scan(sentence, "t", type)) return false;
-
-    talker[0] = type[0];
-    talker[1] = type[1];
-    talker[2] = '\0';
-
-    return true;
-}
-
-enum minmea_sentence_id minmea_sentence_id(const char* sentence, bool strict) {
-    if(!minmea_check(sentence, strict)) return MINMEA_INVALID;
-
-    char type[6];
-    if(!minmea_scan(sentence, "t", type)) return MINMEA_INVALID;
-
-    if(!strcmp(type + 2, "GBS")) return MINMEA_SENTENCE_GBS;
-    if(!strcmp(type + 2, "GGA")) return MINMEA_SENTENCE_GGA;
-    if(!strcmp(type + 2, "GLL")) return MINMEA_SENTENCE_GLL;
-    if(!strcmp(type + 2, "GSA")) return MINMEA_SENTENCE_GSA;
-    if(!strcmp(type + 2, "GST")) return MINMEA_SENTENCE_GST;
-    if(!strcmp(type + 2, "GSV")) return MINMEA_SENTENCE_GSV;
-    if(!strcmp(type + 2, "RMC")) return MINMEA_SENTENCE_RMC;
-    if(!strcmp(type + 2, "VTG")) return MINMEA_SENTENCE_VTG;
-    if(!strcmp(type + 2, "ZDA")) return MINMEA_SENTENCE_ZDA;
-
-    return MINMEA_UNKNOWN;
-}
-
-bool minmea_parse_gbs(struct minmea_sentence_gbs* frame, const char* sentence) {
-    // $GNGBS,170556.00,3.0,2.9,8.3,,,,*5C
-    char type[6];
-    if(!minmea_scan(
-           sentence,
-           "tTfffifff",
-           type,
-           &frame->time,
-           &frame->err_latitude,
-           &frame->err_longitude,
-           &frame->err_altitude,
-           &frame->svid,
-           &frame->prob,
-           &frame->bias,
-           &frame->stddev))
-        return false;
-    if(strcmp(type + 2, "GBS")) return false;
-
-    return true;
-}
-
-bool minmea_parse_rmc(struct minmea_sentence_rmc* frame, const char* sentence) {
-    // $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
-    char type[6];
-    char validity;
-    int latitude_direction;
-    int longitude_direction;
-    int variation_direction;
-    if(!minmea_scan(
-           sentence,
-           "tTcfdfdffDfd",
-           type,
-           &frame->time,
-           &validity,
-           &frame->latitude,
-           &latitude_direction,
-           &frame->longitude,
-           &longitude_direction,
-           &frame->speed,
-           &frame->course,
-           &frame->date,
-           &frame->variation,
-           &variation_direction))
-        return false;
-    if(strcmp(type + 2, "RMC")) return false;
-
-    frame->valid = (validity == 'A');
-    frame->latitude.value *= latitude_direction;
-    frame->longitude.value *= longitude_direction;
-    frame->variation.value *= variation_direction;
-
-    return true;
-}
-
-bool minmea_parse_gga(struct minmea_sentence_gga* frame, const char* sentence) {
-    // $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
-    char type[6];
-    int latitude_direction;
-    int longitude_direction;
-
-    if(!minmea_scan(
-           sentence,
-           "tTfdfdiiffcfcf_",
-           type,
-           &frame->time,
-           &frame->latitude,
-           &latitude_direction,
-           &frame->longitude,
-           &longitude_direction,
-           &frame->fix_quality,
-           &frame->satellites_tracked,
-           &frame->hdop,
-           &frame->altitude,
-           &frame->altitude_units,
-           &frame->height,
-           &frame->height_units,
-           &frame->dgps_age))
-        return false;
-    if(strcmp(type + 2, "GGA")) return false;
-
-    frame->latitude.value *= latitude_direction;
-    frame->longitude.value *= longitude_direction;
-
-    return true;
-}
-
-bool minmea_parse_gsa(struct minmea_sentence_gsa* frame, const char* sentence) {
-    // $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
-    char type[6];
-
-    if(!minmea_scan(
-           sentence,
-           "tciiiiiiiiiiiiifff",
-           type,
-           &frame->mode,
-           &frame->fix_type,
-           &frame->sats[0],
-           &frame->sats[1],
-           &frame->sats[2],
-           &frame->sats[3],
-           &frame->sats[4],
-           &frame->sats[5],
-           &frame->sats[6],
-           &frame->sats[7],
-           &frame->sats[8],
-           &frame->sats[9],
-           &frame->sats[10],
-           &frame->sats[11],
-           &frame->pdop,
-           &frame->hdop,
-           &frame->vdop))
-        return false;
-    if(strcmp(type + 2, "GSA")) return false;
-
-    return true;
-}
-
-bool minmea_parse_gll(struct minmea_sentence_gll* frame, const char* sentence) {
-    // $GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41$;
-    char type[6];
-    int latitude_direction;
-    int longitude_direction;
-
-    if(!minmea_scan(
-           sentence,
-           "tfdfdTc;c",
-           type,
-           &frame->latitude,
-           &latitude_direction,
-           &frame->longitude,
-           &longitude_direction,
-           &frame->time,
-           &frame->status,
-           &frame->mode))
-        return false;
-    if(strcmp(type + 2, "GLL")) return false;
-
-    frame->latitude.value *= latitude_direction;
-    frame->longitude.value *= longitude_direction;
-
-    return true;
-}
-
-bool minmea_parse_gst(struct minmea_sentence_gst* frame, const char* sentence) {
-    // $GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58
-    char type[6];
-
-    if(!minmea_scan(
-           sentence,
-           "tTfffffff",
-           type,
-           &frame->time,
-           &frame->rms_deviation,
-           &frame->semi_major_deviation,
-           &frame->semi_minor_deviation,
-           &frame->semi_major_orientation,
-           &frame->latitude_error_deviation,
-           &frame->longitude_error_deviation,
-           &frame->altitude_error_deviation))
-        return false;
-    if(strcmp(type + 2, "GST")) return false;
-
-    return true;
-}
-
-bool minmea_parse_gsv(struct minmea_sentence_gsv* frame, const char* sentence) {
-    // $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74
-    // $GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D
-    // $GPGSV,4,2,11,08,51,203,30,09,45,215,28*75
-    // $GPGSV,4,4,13,39,31,170,27*40
-    // $GPGSV,4,4,13*7B
-    char type[6];
-
-    if(!minmea_scan(
-           sentence,
-           "tiii;iiiiiiiiiiiiiiii",
-           type,
-           &frame->total_msgs,
-           &frame->msg_nr,
-           &frame->total_sats,
-           &frame->sats[0].nr,
-           &frame->sats[0].elevation,
-           &frame->sats[0].azimuth,
-           &frame->sats[0].snr,
-           &frame->sats[1].nr,
-           &frame->sats[1].elevation,
-           &frame->sats[1].azimuth,
-           &frame->sats[1].snr,
-           &frame->sats[2].nr,
-           &frame->sats[2].elevation,
-           &frame->sats[2].azimuth,
-           &frame->sats[2].snr,
-           &frame->sats[3].nr,
-           &frame->sats[3].elevation,
-           &frame->sats[3].azimuth,
-           &frame->sats[3].snr)) {
-        return false;
-    }
-    if(strcmp(type + 2, "GSV")) return false;
-
-    return true;
-}
-
-bool minmea_parse_vtg(struct minmea_sentence_vtg* frame, const char* sentence) {
-    // $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48
-    // $GPVTG,156.1,T,140.9,M,0.0,N,0.0,K*41
-    // $GPVTG,096.5,T,083.5,M,0.0,N,0.0,K,D*22
-    // $GPVTG,188.36,T,,M,0.820,N,1.519,K,A*3F
-    char type[6];
-    char c_true, c_magnetic, c_knots, c_kph, c_faa_mode;
-
-    if(!minmea_scan(
-           sentence,
-           "t;fcfcfcfcc",
-           type,
-           &frame->true_track_degrees,
-           &c_true,
-           &frame->magnetic_track_degrees,
-           &c_magnetic,
-           &frame->speed_knots,
-           &c_knots,
-           &frame->speed_kph,
-           &c_kph,
-           &c_faa_mode))
-        return false;
-    if(strcmp(type + 2, "VTG")) return false;
-    // values are only valid with the accompanying characters
-    if(c_true != 'T') frame->true_track_degrees.scale = 0;
-    if(c_magnetic != 'M') frame->magnetic_track_degrees.scale = 0;
-    if(c_knots != 'N') frame->speed_knots.scale = 0;
-    if(c_kph != 'K') frame->speed_kph.scale = 0;
-    frame->faa_mode = (enum minmea_faa_mode)c_faa_mode;
-
-    return true;
-}
-
-bool minmea_parse_zda(struct minmea_sentence_zda* frame, const char* sentence) {
-    // $GPZDA,201530.00,04,07,2002,00,00*60
-    char type[6];
-
-    if(!minmea_scan(
-           sentence,
-           "tTiiiii",
-           type,
-           &frame->time,
-           &frame->date.day,
-           &frame->date.month,
-           &frame->date.year,
-           &frame->hour_offset,
-           &frame->minute_offset))
-        return false;
-    if(strcmp(type + 2, "ZDA")) return false;
-
-    // check offsets
-    if(abs(frame->hour_offset) > 13 || frame->minute_offset > 59 || frame->minute_offset < 0)
-        return false;
-
-    return true;
-}
-
-int minmea_getdatetime(
-    struct tm* tm,
-    const struct minmea_date* date,
-    const struct minmea_time* time_) {
-    if(date->year == -1 || time_->hours == -1) return -1;
-
-    memset(tm, 0, sizeof(*tm));
-    if(date->year < 80) {
-        tm->tm_year = 2000 + date->year - 1900; // 2000-2079
-    } else if(date->year >= 1900) {
-        tm->tm_year = date->year - 1900; // 4 digit year, use directly
-    } else {
-        tm->tm_year = date->year; // 1980-1999
-    }
-    tm->tm_mon = date->month - 1;
-    tm->tm_mday = date->day;
-    tm->tm_hour = time_->hours;
-    tm->tm_min = time_->minutes;
-    tm->tm_sec = time_->seconds;
-
-    return 0;
-}
-
-int minmea_gettime(
-    struct timespec* ts,
-    const struct minmea_date* date,
-    const struct minmea_time* time_) {
-    struct tm tm;
-    if(minmea_getdatetime(&tm, date, time_)) return -1;
-
-    time_t timestamp = mktime(&tm); /* See README.md if your system lacks timegm(). */
-    if(timestamp != (time_t)-1) {
-        ts->tv_sec = timestamp;
-        ts->tv_nsec = time_->microseconds * 1000;
-        return 0;
-    } else {
-        return -1;
-    }
-}
-
-/* vim: set ts=4 sw=4 et: */