├── .clusterfuzzlite ├── project.yaml ├── Dockerfile ├── build.sh └── fuzzer.c ├── .gitignore ├── LICENSE.grants ├── .github └── workflows │ ├── c-cpp.yml │ └── cflite_pr.yml ├── COPYING ├── compat ├── minmea_compat_windows.h └── minmea_compat_ti-rtos.h ├── LICENSE.MIT ├── CMakeLists.txt ├── example.c ├── README.md ├── LICENSE.LGPL-3.0 ├── minmea.h ├── minmea.c └── tests.c /.clusterfuzzlite/project.yaml: -------------------------------------------------------------------------------- 1 | language: c 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | cmake-build-*/ 3 | *.o 4 | *~ 5 | *.diff 6 | .*.swp 7 | minmea 8 | tests 9 | example 10 | *.exe 11 | -------------------------------------------------------------------------------- /.clusterfuzzlite/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gcr.io/oss-fuzz-base/base-builder 2 | RUN apt-get update && apt-get install -y make autoconf automake libtool 3 | COPY . $SRC/minmea 4 | COPY .clusterfuzzlite/build.sh $SRC/build.sh 5 | COPY .clusterfuzzlite/*.c $SRC/ 6 | WORKDIR minmea 7 | -------------------------------------------------------------------------------- /.clusterfuzzlite/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | $CC $CFLAGS -c minmea.c 3 | 4 | llvm-ar rcs libfuzz.a *.o 5 | 6 | $CC $CFLAGS $LIB_FUZZING_ENGINE $SRC/fuzzer.c -Wl,--whole-archive $SRC/minmea/libfuzz.a -Wl,--allow-multiple-definition -I$SRC/minmea/ -I$SRC/minmea/compat -o $OUT/fuzzer 7 | -------------------------------------------------------------------------------- /LICENSE.grants: -------------------------------------------------------------------------------- 1 | At your option, if WTFPL is unacceptable for your legal department, I hereby 2 | explicitly grant you the right to use any the following licenses for minmea: 3 | 4 | 1. MIT License 5 | 2. LGPL-3.0 License or later 6 | 7 | See accompanying license files for full license text. 8 | -------------------------------------------------------------------------------- /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v3 10 | - name: Install dependencies 11 | run: sudo apt-get install -y clang-tools check cmake 12 | - name: cmake 13 | run: "( rm -rf build && mkdir build && cd build && cmake .. && make && CTEST_OUTPUT_ON_FAILURE=1 make 14 | test && echo OK )" 15 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /compat/minmea_compat_windows.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2017 Kosma Moczek 3 | * This program is free software. It comes without any warranty, to the extent 4 | * permitted by applicable law. You can redistribute it and/or modify it under 5 | * the terms of the Do What The Fuck You Want To Public License, Version 2, as 6 | * published by Sam Hocevar. See the COPYING file for more details. 7 | */ 8 | 9 | #if defined(_MSC_VER) 10 | 11 | #if !defined(HAVE_STRUCT_TIMESPEC) 12 | struct timespec { 13 | time_t tv_sec; 14 | long tv_nsec; 15 | }; 16 | #endif 17 | 18 | #define inline __inline 19 | #define timegm _mkgmtime 20 | 21 | #endif 22 | 23 | /* vim: set ts=4 sw=4 et: */ 24 | -------------------------------------------------------------------------------- /.clusterfuzzlite/fuzzer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "minmea_compat_ti-rtos.h" 7 | #include "minmea_compat_windows.h" 8 | #include "minmea.h" 9 | 10 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { 11 | if (size < 7) { 12 | return 0; 13 | } 14 | 15 | // Prepare the input data for minmea_parse_rmc 16 | char input[size + 1]; 17 | memcpy(input, data, size); 18 | input[size] = '\0'; 19 | 20 | // Prepare the frame 21 | struct minmea_sentence_rmc frame; 22 | const char *sentence = (const char *)input; 23 | 24 | // Call the target function 25 | bool result = minmea_parse_rmc(&frame, sentence); 26 | (void)result; 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /.github/workflows/cflite_pr.yml: -------------------------------------------------------------------------------- 1 | name: ClusterFuzzLite PR fuzzing 2 | on: 3 | workflow_dispatch: 4 | pull_request: 5 | branches: [ master ] 6 | permissions: read-all 7 | jobs: 8 | PR: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | fail-fast: false 12 | matrix: 13 | sanitizer: [address] 14 | steps: 15 | - name: Build Fuzzers (${{ matrix.sanitizer }}) 16 | id: build 17 | uses: google/clusterfuzzlite/actions/build_fuzzers@v1 18 | with: 19 | sanitizer: ${{ matrix.sanitizer }} 20 | language: c++ 21 | bad-build-check: false 22 | - name: Run Fuzzers (${{ matrix.sanitizer }}) 23 | id: run 24 | uses: google/clusterfuzzlite/actions/run_fuzzers@v1 25 | with: 26 | github-token: ${{ secrets.GITHUB_TOKEN }} 27 | fuzz-seconds: 100 28 | mode: 'code-change' 29 | report-unreproducible-crashes: false 30 | sanitizer: ${{ matrix.sanitizer }} 31 | -------------------------------------------------------------------------------- /compat/minmea_compat_ti-rtos.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2017 Kosma Moczek 3 | * This program is free software. It comes without any warranty, to the extent 4 | * permitted by applicable law. You can redistribute it and/or modify it under 5 | * the terms of the Do What The Fuck You Want To Public License, Version 2, as 6 | * published by Sam Hocevar. See the COPYING file for more details. 7 | */ 8 | 9 | #ifndef MINMEA_COMPAT_H_ 10 | #define MINMEA_COMPAT_H_ 11 | 12 | #if defined(__TI_ARM__) 13 | #include 14 | #include 15 | #elif defined(__IAR_SYSTEMS_ICC__) 16 | #include 17 | #include 18 | #elif defined(gcc) 19 | #include 20 | #include 21 | #endif /* __TI_ARM__ */ 22 | 23 | #define timegm mktime 24 | 25 | #endif /* MINMEA_COMPAT_H */ 26 | 27 | /* vim: set ts=4 sw=4 et: */ 28 | -------------------------------------------------------------------------------- /LICENSE.MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Kosma Moczek 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.3) 2 | 3 | project(minmea) 4 | 5 | option(MINMEA_ENABLE_TESTING "Enable building and running unit tests" ON) 6 | 7 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -Wall -Wextra -Werror -std=c99") 8 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_POSIX_C_SOURCE=199309L -D_BSD_SOURCE -D_DEFAULT_SOURCE -D_DARWIN_C_SOURCE") 9 | 10 | set(minmea_SRCS minmea.c minmea.h) 11 | add_library(minmea ${minmea_SRCS}) 12 | 13 | add_executable(example example.c) 14 | target_link_libraries(example minmea) 15 | 16 | if (MINMEA_ENABLE_TESTING) 17 | enable_testing() 18 | find_package(Threads REQUIRED) # Workaround for https://github.com/libcheck/check/issues/48#issuecomment-322965461 19 | find_package(PkgConfig) 20 | pkg_check_modules(CHECK REQUIRED check) 21 | link_directories(${CHECK_LIBRARY_DIRS}) 22 | 23 | add_executable(tests tests.c) 24 | target_link_libraries(tests minmea ${CHECK_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) 25 | target_include_directories(tests PUBLIC ${CHECK_INCLUDE_DIRS}) 26 | target_compile_options(tests PUBLIC ${CHECK_CFLAGS_OTHER}) 27 | 28 | add_test(NAME tests COMMAND $) 29 | add_test( 30 | NAME clang_static_analysis 31 | COMMAND scan-build make 32 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 33 | ) 34 | list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") 35 | endif() 36 | 37 | include(GNUInstallDirs) 38 | 39 | install(TARGETS minmea 40 | RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" 41 | LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" 42 | ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}") 43 | install(FILES minmea.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 44 | if(MSVC) 45 | install(FILES compat/minmea_compat_windows.h 46 | DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 47 | RENAME minmea_compat.h) 48 | endif() -------------------------------------------------------------------------------- /example.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2014 Kosma Moczek 3 | * This program is free software. It comes without any warranty, to the extent 4 | * permitted by applicable law. You can redistribute it and/or modify it under 5 | * the terms of the Do What The Fuck You Want To Public License, Version 2, as 6 | * published by Sam Hocevar. See the COPYING file for more details. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | #include "minmea.h" 13 | 14 | #define INDENT_SPACES " " 15 | 16 | int main(void) 17 | { 18 | char line[MINMEA_MAX_SENTENCE_LENGTH]; 19 | while (fgets(line, sizeof(line), stdin) != NULL) { 20 | printf("%s", line); 21 | switch (minmea_sentence_id(line, false)) { 22 | case MINMEA_SENTENCE_RMC: { 23 | struct minmea_sentence_rmc frame; 24 | if (minmea_parse_rmc(&frame, line)) { 25 | printf(INDENT_SPACES "$xxRMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d\n", 26 | frame.latitude.value, frame.latitude.scale, 27 | frame.longitude.value, frame.longitude.scale, 28 | frame.speed.value, frame.speed.scale); 29 | printf(INDENT_SPACES "$xxRMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d\n", 30 | minmea_rescale(&frame.latitude, 1000), 31 | minmea_rescale(&frame.longitude, 1000), 32 | minmea_rescale(&frame.speed, 1000)); 33 | printf(INDENT_SPACES "$xxRMC floating point degree coordinates and speed: (%f,%f) %f\n", 34 | minmea_tocoord(&frame.latitude), 35 | minmea_tocoord(&frame.longitude), 36 | minmea_tofloat(&frame.speed)); 37 | } 38 | else { 39 | printf(INDENT_SPACES "$xxRMC sentence is not parsed\n"); 40 | } 41 | } break; 42 | 43 | case MINMEA_SENTENCE_GGA: { 44 | struct minmea_sentence_gga frame; 45 | if (minmea_parse_gga(&frame, line)) { 46 | printf(INDENT_SPACES "$xxGGA: fix quality: %d\n", frame.fix_quality); 47 | } 48 | else { 49 | printf(INDENT_SPACES "$xxGGA sentence is not parsed\n"); 50 | } 51 | } break; 52 | 53 | case MINMEA_SENTENCE_GST: { 54 | struct minmea_sentence_gst frame; 55 | if (minmea_parse_gst(&frame, line)) { 56 | printf(INDENT_SPACES "$xxGST: raw latitude,longitude and altitude error deviation: (%d/%d,%d/%d,%d/%d)\n", 57 | frame.latitude_error_deviation.value, frame.latitude_error_deviation.scale, 58 | frame.longitude_error_deviation.value, frame.longitude_error_deviation.scale, 59 | frame.altitude_error_deviation.value, frame.altitude_error_deviation.scale); 60 | printf(INDENT_SPACES "$xxGST fixed point latitude,longitude and altitude error deviation" 61 | " scaled to one decimal place: (%d,%d,%d)\n", 62 | minmea_rescale(&frame.latitude_error_deviation, 10), 63 | minmea_rescale(&frame.longitude_error_deviation, 10), 64 | minmea_rescale(&frame.altitude_error_deviation, 10)); 65 | printf(INDENT_SPACES "$xxGST floating point degree latitude, longitude and altitude error deviation: (%f,%f,%f)", 66 | minmea_tofloat(&frame.latitude_error_deviation), 67 | minmea_tofloat(&frame.longitude_error_deviation), 68 | minmea_tofloat(&frame.altitude_error_deviation)); 69 | } 70 | else { 71 | printf(INDENT_SPACES "$xxGST sentence is not parsed\n"); 72 | } 73 | } break; 74 | 75 | case MINMEA_SENTENCE_GSV: { 76 | struct minmea_sentence_gsv frame; 77 | if (minmea_parse_gsv(&frame, line)) { 78 | printf(INDENT_SPACES "$xxGSV: message %d of %d\n", frame.msg_nr, frame.total_msgs); 79 | printf(INDENT_SPACES "$xxGSV: satellites in view: %d\n", frame.total_sats); 80 | for (int i = 0; i < 4; i++) 81 | printf(INDENT_SPACES "$xxGSV: sat nr %d, elevation: %d, azimuth: %d, snr: %d dbm\n", 82 | frame.sats[i].nr, 83 | frame.sats[i].elevation, 84 | frame.sats[i].azimuth, 85 | frame.sats[i].snr); 86 | } 87 | else { 88 | printf(INDENT_SPACES "$xxGSV sentence is not parsed\n"); 89 | } 90 | } break; 91 | 92 | case MINMEA_SENTENCE_VTG: { 93 | struct minmea_sentence_vtg frame; 94 | if (minmea_parse_vtg(&frame, line)) { 95 | printf(INDENT_SPACES "$xxVTG: true track degrees = %f\n", 96 | minmea_tofloat(&frame.true_track_degrees)); 97 | printf(INDENT_SPACES " magnetic track degrees = %f\n", 98 | minmea_tofloat(&frame.magnetic_track_degrees)); 99 | printf(INDENT_SPACES " speed knots = %f\n", 100 | minmea_tofloat(&frame.speed_knots)); 101 | printf(INDENT_SPACES " speed kph = %f\n", 102 | minmea_tofloat(&frame.speed_kph)); 103 | } 104 | else { 105 | printf(INDENT_SPACES "$xxVTG sentence is not parsed\n"); 106 | } 107 | } break; 108 | 109 | case MINMEA_SENTENCE_ZDA: { 110 | struct minmea_sentence_zda frame; 111 | if (minmea_parse_zda(&frame, line)) { 112 | printf(INDENT_SPACES "$xxZDA: %d:%d:%d %02d.%02d.%d UTC%+03d:%02d\n", 113 | frame.time.hours, 114 | frame.time.minutes, 115 | frame.time.seconds, 116 | frame.date.day, 117 | frame.date.month, 118 | frame.date.year, 119 | frame.hour_offset, 120 | frame.minute_offset); 121 | } 122 | else { 123 | printf(INDENT_SPACES "$xxZDA sentence is not parsed\n"); 124 | } 125 | } break; 126 | 127 | case MINMEA_INVALID: { 128 | printf(INDENT_SPACES "$xxxxx sentence is not valid\n"); 129 | } break; 130 | 131 | default: { 132 | printf(INDENT_SPACES "$xxxxx sentence is not parsed\n"); 133 | } break; 134 | } 135 | } 136 | 137 | return 0; 138 | } 139 | 140 | /* vim: set ts=4 sw=4 et: */ 141 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # minmea, a lightweight GPS NMEA 0183 parser library 2 | 3 | [![C/C++ 4 | CI](https://github.com/kosma/minmea/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/kosma/minmea/actions/workflows/c-cpp.yml) 5 | 6 | Minmea is a minimalistic GPS parser library written in pure C intended for 7 | resource-constrained platforms, especially microcontrollers and other embedded 8 | systems. 9 | 10 | ## Features 11 | 12 | * Written in ISO C99. 13 | * No dynamic memory allocation. 14 | * No floating point usage in the core library. 15 | * Supports both fixed and floating point values. 16 | * One source file and one header - can't get any simpler. 17 | * Tested under Linux, OS X, Windows and embedded ARM GCC. 18 | * Easily extendable to support new sentences. 19 | * Complete with a test suite and static analysis. 20 | 21 | ## Supported sentences 22 | 23 | * ``GBS`` (Satellite Fault Detection) 24 | * ``GGA`` (Fix Data) 25 | * ``GLL`` (Geographic Position: Latitude/Longitude) 26 | * ``GSA`` (DOP and active satellites) 27 | * ``GST`` (Pseudorange Noise Statistics) 28 | * ``GSV`` (Satellites in view) 29 | * ``RMC`` (Recommended Minimum: position, velocity, time) 30 | * ``VTG`` (Track made good and Ground speed) 31 | * ``ZDA`` (Time & Date - UTC, day, month, year and local time zone) 32 | 33 | Adding support for more sentences is trivial; see ``minmea.c`` source. Good documentation 34 | on NMEA is at https://gpsd.gitlab.io/gpsd/NMEA.html 35 | 36 | ## Compatibility 37 | 38 | Minmea runs out-of-the-box under most Unix-compatible systems. Support for non-Unix systems 39 | (including native Windows builds under MSVC) is provided via compatibility headers: 40 | 41 | 1. Define `MINMEA_INCLUDE_COMPAT` in the build environment. 42 | 2. Add appropriate compatibility header from under `compat/` directory as `minmea_compat.h`. 43 | 44 | If your GPS receiver outputs very long sentences, consider increasing `MINMEA_MAX_SENTENCE_LENGTH` 45 | in your build environment. 46 | 47 | ## Fractional number format 48 | 49 | Internally, minmea stores fractional numbers as pairs of two integers: ``{value, scale}``. 50 | For example, a value of ``"-123.456"`` would be parsed as ``{-123456, 1000}``. As this 51 | format is quite unwieldy, minmea provides the following convenience functions for converting 52 | to either fixed-point or floating-point format: 53 | 54 | * ``minmea_rescale({-123456, 1000}, 10) => -1235`` 55 | * ``minmea_float({-123456, 1000}) => -123.456`` 56 | 57 | The compound type ``struct minmea_float`` uses ``int_least32_t`` internally. Therefore, 58 | the coordinate precision is guaranteed to be at least ``[+-]DDDMM.MMMMM`` (five decimal digits) 59 | or ±2cm LSB at the equator. Note that GPS modules commonly only provide four decimal digits 60 | (``[+-]DDDMM.MMMM``), which equates to ±20cm (0.0001 minute is 0.0001/60 degrees and one degree 61 | is about 111km) at the equator. 62 | 63 | ## Coordinate format 64 | 65 | NMEA uses the clunky ``DDMM.MMMM`` format which, honestly, is not good in the internet era. 66 | Internally, minmea stores it as a fractional number (see above); for practical uses, 67 | the value should be probably converted to the DD.DDDDD floating point format using the 68 | following function: 69 | 70 | * ``minmea_tocoord({-375165, 100}) => -37.860832`` 71 | 72 | The library doesn't perform this conversion automatically for the following reasons: 73 | 74 | * The conversion is not reversible. 75 | * It requires floating point support. 76 | * The user might want to perform this conversion later on or retain the original values. 77 | 78 | ## Example 79 | 80 | ```c 81 | char line[MINMEA_MAX_SENTENCE_LENGTH]; 82 | while (fgets(line, sizeof(line), stdin) != NULL) { 83 | switch (minmea_sentence_id(line, false)) { 84 | case MINMEA_SENTENCE_RMC: { 85 | struct minmea_sentence_rmc frame; 86 | if (minmea_parse_rmc(&frame, line)) { 87 | printf("$RMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d\n", 88 | frame.latitude.value, frame.latitude.scale, 89 | frame.longitude.value, frame.longitude.scale, 90 | frame.speed.value, frame.speed.scale); 91 | printf("$RMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d\n", 92 | minmea_rescale(&frame.latitude, 1000), 93 | minmea_rescale(&frame.longitude, 1000), 94 | minmea_rescale(&frame.speed, 1000)); 95 | printf("$RMC floating point degree coordinates and speed: (%f,%f) %f\n", 96 | minmea_tocoord(&frame.latitude), 97 | minmea_tocoord(&frame.longitude), 98 | minmea_tofloat(&frame.speed)); 99 | } 100 | } break; 101 | 102 | case MINMEA_SENTENCE_GGA: { 103 | struct minmea_sentence_gga frame; 104 | if (minmea_parse_gga(&frame, line)) { 105 | printf("$GGA: fix quality: %d\n", frame.fix_quality); 106 | } 107 | } break; 108 | 109 | case MINMEA_SENTENCE_GSV: { 110 | struct minmea_sentence_gsv frame; 111 | if (minmea_parse_gsv(&frame, line)) { 112 | printf("$GSV: message %d of %d\n", frame.msg_nr, frame.total_msgs); 113 | printf("$GSV: satellites in view: %d\n", frame.total_sats); 114 | for (int i = 0; i < 4; i++) 115 | printf("$GSV: sat nr %d, elevation: %d, azimuth: %d, snr: %d dbm\n", 116 | frame.sats[i].nr, 117 | frame.sats[i].elevation, 118 | frame.sats[i].azimuth, 119 | frame.sats[i].snr); 120 | } 121 | } break; 122 | } 123 | } 124 | ``` 125 | 126 | ## Integration with your project 127 | 128 | Simply add ``minmea.[ch]`` to your project, ``#include "minmea.h"`` and you're 129 | good to go. 130 | 131 | ## Running unit tests 132 | 133 | Building and running the tests requires the following: 134 | 135 | * CMake 136 | * Check Framework (https://libcheck.github.io/check/). 137 | * Clang Static Analyzer (https://clang-analyzer.llvm.org/). 138 | 139 | If you have both in your ``$PATH``, running the tests should be as simple as: 140 | 141 | ``` 142 | mkdir build 143 | cd build 144 | cmake ../ 145 | make 146 | make test 147 | ``` 148 | 149 | ## Building without unit tests 150 | 151 | It's possible to build the library without the unit tests and their dependencies. 152 | 153 | ```console 154 | mkdir build 155 | cd build 156 | cmake -DMINMEA_ENABLE_TESTING=OFF .. 157 | make 158 | ``` 159 | 160 | ## Limitations 161 | 162 | * Only a handful of frames is supported right now. 163 | * There's no support for omitting parts of the library from building. As 164 | a workaround, use the ``-ffunction-sections -Wl,--gc-sections`` linker flags 165 | (or equivalent) to remove the unused functions (parsers) from the final image. 166 | * Some systems lack ``timegm``. On these systems, the recommended course of 167 | action is to build with ``-Dtimegm=mktime`` which will work correctly as long 168 | the system runs in the default ``UTC`` timezone. 169 | 170 | ## Contributing 171 | 172 | 1. Use the GitHub pull request system. 173 | 2. Make sure to follow to existing style (naming, indentation, etc.) 174 | 3. Write unit tests for any new functionality you add. 175 | 4. Be aware you're submitting your work under the repository's license. 176 | 177 | ## Licensing 178 | 179 | Minmea is open source software; see ``COPYING`` for amusement. Email me if the 180 | license bothers you and I'll happily re-license under anything else under the sun. 181 | 182 | ## Author 183 | 184 | Minmea was written by Kosma Moczek <kosma@kosma.pl> and Patryk Szymczak 185 | <patryk.szymczak@gmail.com> at Cloud Your Car, with bugs fixed by countless 186 | good people. 187 | -------------------------------------------------------------------------------- /LICENSE.LGPL-3.0: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /minmea.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2014 Kosma Moczek 3 | * This program is free software. It comes without any warranty, to the extent 4 | * permitted by applicable law. You can redistribute it and/or modify it under 5 | * the terms of the Do What The Fuck You Want To Public License, Version 2, as 6 | * published by Sam Hocevar. See the COPYING file for more details. 7 | */ 8 | 9 | #ifndef MINMEA_H 10 | #define MINMEA_H 11 | 12 | #ifdef __cplusplus 13 | extern "C" { 14 | #endif 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #ifdef MINMEA_INCLUDE_COMPAT 22 | #include 23 | #endif 24 | 25 | #ifndef MINMEA_MAX_SENTENCE_LENGTH 26 | #define MINMEA_MAX_SENTENCE_LENGTH 80 27 | #endif 28 | 29 | enum minmea_sentence_id { 30 | MINMEA_INVALID = -1, 31 | MINMEA_UNKNOWN = 0, 32 | MINMEA_SENTENCE_GBS, 33 | MINMEA_SENTENCE_GGA, 34 | MINMEA_SENTENCE_GLL, 35 | MINMEA_SENTENCE_GSA, 36 | MINMEA_SENTENCE_GST, 37 | MINMEA_SENTENCE_GSV, 38 | MINMEA_SENTENCE_RMC, 39 | MINMEA_SENTENCE_VTG, 40 | MINMEA_SENTENCE_ZDA, 41 | }; 42 | 43 | struct minmea_float { 44 | int_least32_t value; 45 | int_least32_t scale; 46 | }; 47 | 48 | struct minmea_date { 49 | int day; 50 | int month; 51 | int year; 52 | }; 53 | 54 | struct minmea_time { 55 | int hours; 56 | int minutes; 57 | int seconds; 58 | int microseconds; 59 | }; 60 | 61 | // provide backwards compatibility to users expecting a null-terminated string 62 | // instead of a struct 63 | union minmea_type { 64 | char buf[6]; 65 | struct { 66 | char talker_id[2]; 67 | char sentence_id[3]; 68 | char null_terminator; 69 | }; 70 | }; 71 | 72 | struct minmea_sentence_gbs { 73 | union minmea_type type; 74 | struct minmea_time time; 75 | struct minmea_float err_latitude; 76 | struct minmea_float err_longitude; 77 | struct minmea_float err_altitude; 78 | int svid; 79 | struct minmea_float prob; 80 | struct minmea_float bias; 81 | struct minmea_float stddev; 82 | }; 83 | 84 | struct minmea_sentence_rmc { 85 | union minmea_type type; 86 | struct minmea_time time; 87 | bool valid; 88 | struct minmea_float latitude; 89 | struct minmea_float longitude; 90 | struct minmea_float speed; 91 | struct minmea_float course; 92 | struct minmea_date date; 93 | struct minmea_float variation; 94 | }; 95 | 96 | struct minmea_sentence_gga { 97 | union minmea_type type; 98 | struct minmea_time time; 99 | struct minmea_float latitude; 100 | struct minmea_float longitude; 101 | int fix_quality; 102 | int satellites_tracked; 103 | struct minmea_float hdop; 104 | struct minmea_float altitude; char altitude_units; 105 | struct minmea_float height; char height_units; 106 | struct minmea_float dgps_age; 107 | }; 108 | 109 | enum minmea_gll_status { 110 | MINMEA_GLL_STATUS_DATA_VALID = 'A', 111 | MINMEA_GLL_STATUS_DATA_NOT_VALID = 'V', 112 | }; 113 | 114 | // FAA mode added to some fields in NMEA 2.3. 115 | enum minmea_faa_mode { 116 | MINMEA_FAA_MODE_AUTONOMOUS = 'A', 117 | MINMEA_FAA_MODE_DIFFERENTIAL = 'D', 118 | MINMEA_FAA_MODE_ESTIMATED = 'E', 119 | MINMEA_FAA_MODE_MANUAL = 'M', 120 | MINMEA_FAA_MODE_SIMULATED = 'S', 121 | MINMEA_FAA_MODE_NOT_VALID = 'N', 122 | MINMEA_FAA_MODE_PRECISE = 'P', 123 | }; 124 | 125 | struct minmea_sentence_gll { 126 | union minmea_type type; 127 | struct minmea_float latitude; 128 | struct minmea_float longitude; 129 | struct minmea_time time; 130 | char status; 131 | char mode; 132 | }; 133 | 134 | struct minmea_sentence_gst { 135 | union minmea_type type; 136 | struct minmea_time time; 137 | struct minmea_float rms_deviation; 138 | struct minmea_float semi_major_deviation; 139 | struct minmea_float semi_minor_deviation; 140 | struct minmea_float semi_major_orientation; 141 | struct minmea_float latitude_error_deviation; 142 | struct minmea_float longitude_error_deviation; 143 | struct minmea_float altitude_error_deviation; 144 | }; 145 | 146 | enum minmea_gsa_mode { 147 | MINMEA_GPGSA_MODE_AUTO = 'A', 148 | MINMEA_GPGSA_MODE_FORCED = 'M', 149 | }; 150 | 151 | enum minmea_gsa_fix_type { 152 | MINMEA_GPGSA_FIX_NONE = 1, 153 | MINMEA_GPGSA_FIX_2D = 2, 154 | MINMEA_GPGSA_FIX_3D = 3, 155 | }; 156 | 157 | struct minmea_sentence_gsa { 158 | union minmea_type type; 159 | char mode; 160 | int fix_type; 161 | int sats[12]; 162 | struct minmea_float pdop; 163 | struct minmea_float hdop; 164 | struct minmea_float vdop; 165 | }; 166 | 167 | struct minmea_sat_info { 168 | int nr; 169 | int elevation; 170 | int azimuth; 171 | int snr; 172 | }; 173 | 174 | struct minmea_sentence_gsv { 175 | union minmea_type type; 176 | int total_msgs; 177 | int msg_nr; 178 | int total_sats; 179 | struct minmea_sat_info sats[4]; 180 | }; 181 | 182 | struct minmea_sentence_vtg { 183 | union minmea_type type; 184 | struct minmea_float true_track_degrees; 185 | struct minmea_float magnetic_track_degrees; 186 | struct minmea_float speed_knots; 187 | struct minmea_float speed_kph; 188 | enum minmea_faa_mode faa_mode; 189 | }; 190 | 191 | struct minmea_sentence_zda { 192 | union minmea_type type; 193 | struct minmea_time time; 194 | struct minmea_date date; 195 | int hour_offset; 196 | int minute_offset; 197 | }; 198 | 199 | /** 200 | * Calculate raw sentence checksum. Does not check sentence integrity. 201 | */ 202 | uint8_t minmea_checksum(const char *sentence); 203 | 204 | /** 205 | * Check sentence validity and checksum. Returns true for valid sentences. 206 | */ 207 | bool minmea_check(const char *sentence, bool strict); 208 | 209 | /** 210 | * Determine talker identifier. 211 | */ 212 | bool minmea_talker_id(char talker[3], const char *sentence); 213 | 214 | /** 215 | * Get sentence id as string. 216 | */ 217 | const char* minmea_sentence(enum minmea_sentence_id id); 218 | 219 | /** 220 | * Determine sentence identifier. 221 | */ 222 | enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict); 223 | 224 | /** 225 | * Scanf-like processor for NMEA sentences. Supports the following formats: 226 | * c - single character (char *) 227 | * d - direction, returned as 1/-1, default 0 (int *) 228 | * f - fractional, returned as value + scale (struct minmea_float *) 229 | * i - decimal, default zero (int *) 230 | * s - string (char *) 231 | * t - talker identifier and type (union minmea_type *) 232 | * D - date (struct minmea_date *) 233 | * T - time stamp (struct minmea_time *) 234 | * _ - ignore this field 235 | * ; - following fields are optional 236 | * Returns true on success. See library source code for details. 237 | */ 238 | bool minmea_scan(const char *sentence, const char *format, ...); 239 | 240 | /* 241 | * Parse a specific type of sentence. Return true on success. 242 | */ 243 | bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence); 244 | bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence); 245 | bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence); 246 | bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence); 247 | bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence); 248 | bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence); 249 | bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence); 250 | bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence); 251 | bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence); 252 | 253 | /** 254 | * Convert GPS UTC date/time representation to a UNIX calendar time. 255 | */ 256 | int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_); 257 | 258 | /** 259 | * Convert GPS UTC date/time representation to a UNIX timestamp. 260 | */ 261 | int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_); 262 | 263 | /** 264 | * Rescale a fixed-point value to a different scale. Rounds towards zero. 265 | */ 266 | static inline int_least32_t minmea_rescale(const struct minmea_float *f, int_least32_t new_scale) 267 | { 268 | if (f->scale == 0) 269 | return 0; 270 | if (f->scale == new_scale) 271 | return f->value; 272 | if (f->scale > new_scale) 273 | return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale/new_scale/2) / (f->scale/new_scale); 274 | else 275 | return f->value * (new_scale/f->scale); 276 | } 277 | 278 | /** 279 | * Convert a fixed-point value to a floating-point value. 280 | * Returns NaN for "unknown" values. 281 | */ 282 | static inline float minmea_tofloat(const struct minmea_float *f) 283 | { 284 | if (f->scale == 0) 285 | return NAN; 286 | return (float) f->value / (float) f->scale; 287 | } 288 | 289 | /** 290 | * Convert a raw coordinate to a floating point DD.DDD... value. 291 | * Returns NaN for "unknown" values. 292 | */ 293 | static inline float minmea_tocoord(const struct minmea_float *f) 294 | { 295 | if (f->scale == 0) 296 | return NAN; 297 | if (f->scale > (INT_LEAST32_MAX / 100)) 298 | return NAN; 299 | if (f->scale < (INT_LEAST32_MIN / 100)) 300 | return NAN; 301 | int_least32_t degrees = f->value / (f->scale * 100); 302 | int_least32_t minutes = f->value % (f->scale * 100); 303 | return (float) degrees + (float) minutes / (60 * f->scale); 304 | } 305 | 306 | /** 307 | * Check whether a character belongs to the set of characters allowed in a 308 | * sentence data field. 309 | */ 310 | static inline bool minmea_isfield(char c) { 311 | return isprint((unsigned char) c) && c != ',' && c != '*'; 312 | } 313 | 314 | #ifdef __cplusplus 315 | } 316 | #endif 317 | 318 | #endif /* MINMEA_H */ 319 | 320 | /* vim: set ts=4 sw=4 et: */ 321 | -------------------------------------------------------------------------------- /minmea.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2014 Kosma Moczek 3 | * This program is free software. It comes without any warranty, to the extent 4 | * permitted by applicable law. You can redistribute it and/or modify it under 5 | * the terms of the Do What The Fuck You Want To Public License, Version 2, as 6 | * published by Sam Hocevar. See the COPYING file for more details. 7 | */ 8 | 9 | #include "minmea.h" 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | #define boolstr(s) ((s) ? "true" : "false") 16 | #define countof(array) (sizeof(array) / sizeof(array[0])) 17 | 18 | static int hex2int(char c) 19 | { 20 | if (c >= '0' && c <= '9') 21 | return c - '0'; 22 | if (c >= 'A' && c <= 'F') 23 | return c - 'A' + 10; 24 | if (c >= 'a' && c <= 'f') 25 | return c - 'a' + 10; 26 | return -1; 27 | } 28 | 29 | uint8_t minmea_checksum(const char *sentence) 30 | { 31 | // Support senteces with or without the starting dollar sign. 32 | if (*sentence == '$') 33 | sentence++; 34 | 35 | uint8_t checksum = 0x00; 36 | 37 | // The optional checksum is an XOR of all bytes between "$" and "*". 38 | while (*sentence && *sentence != '*') 39 | checksum ^= *sentence++; 40 | 41 | return checksum; 42 | } 43 | 44 | bool minmea_check(const char *sentence, bool strict) 45 | { 46 | uint8_t checksum = 0x00; 47 | 48 | // A valid sentence starts with "$". 49 | if (*sentence++ != '$') 50 | return false; 51 | 52 | // The optional checksum is an XOR of all bytes between "$" and "*". 53 | while (*sentence && *sentence != '*' && isprint((unsigned char) *sentence)) 54 | checksum ^= *sentence++; 55 | 56 | // If checksum is present... 57 | if (*sentence == '*') { 58 | // Extract checksum. 59 | sentence++; 60 | int upper = hex2int(*sentence++); 61 | if (upper == -1) 62 | return false; 63 | int lower = hex2int(*sentence++); 64 | if (lower == -1) 65 | return false; 66 | int expected = upper << 4 | lower; 67 | 68 | // Check for checksum mismatch. 69 | if (checksum != expected) 70 | return false; 71 | } else if (strict) { 72 | // Discard non-checksummed frames in strict mode. 73 | return false; 74 | } 75 | 76 | // The only stuff allowed at this point is a newline. 77 | while (*sentence == '\r' || *sentence == '\n') { 78 | sentence++; 79 | } 80 | 81 | if (*sentence) { 82 | return false; 83 | } 84 | 85 | return true; 86 | } 87 | 88 | bool minmea_scan(const char *sentence, const char *format, ...) 89 | { 90 | bool result = false; 91 | bool optional = false; 92 | 93 | if (sentence == NULL) 94 | return false; 95 | 96 | va_list ap; 97 | va_start(ap, format); 98 | 99 | const char *field = sentence; 100 | #define next_field() \ 101 | do { \ 102 | /* Progress to the next field. */ \ 103 | while (minmea_isfield(*sentence)) \ 104 | sentence++; \ 105 | /* Make sure there is a field there. */ \ 106 | if (*sentence == ',') { \ 107 | sentence++; \ 108 | field = sentence; \ 109 | } else { \ 110 | field = NULL; \ 111 | } \ 112 | } while (0) 113 | 114 | while (*format) { 115 | char type = *format++; 116 | 117 | if (type == ';') { 118 | // All further fields are optional. 119 | optional = true; 120 | continue; 121 | } 122 | 123 | if (!field && !optional) { 124 | // Field requested but we ran out if input. Bail out. 125 | goto parse_error; 126 | } 127 | 128 | switch (type) { 129 | case 'c': { // Single character field (char). 130 | char value = '\0'; 131 | 132 | if (field && minmea_isfield(*field)) 133 | value = *field; 134 | 135 | *va_arg(ap, char *) = value; 136 | } break; 137 | 138 | case 'd': { // Single character direction field (int). 139 | int value = 0; 140 | 141 | if (field && minmea_isfield(*field)) { 142 | switch (*field) { 143 | case 'N': 144 | case 'E': 145 | value = 1; 146 | break; 147 | case 'S': 148 | case 'W': 149 | value = -1; 150 | break; 151 | default: 152 | goto parse_error; 153 | } 154 | } 155 | 156 | *va_arg(ap, int *) = value; 157 | } break; 158 | 159 | case 'f': { // Fractional value with scale (struct minmea_float). 160 | int sign = 0; 161 | int_least32_t value = -1; 162 | int_least32_t scale = 0; 163 | 164 | if (field) { 165 | while (minmea_isfield(*field)) { 166 | if (*field == '+' && !sign && value == -1) { 167 | sign = 1; 168 | } else if (*field == '-' && !sign && value == -1) { 169 | sign = -1; 170 | } else if (isdigit((unsigned char) *field)) { 171 | int digit = *field - '0'; 172 | if (value == -1) 173 | value = 0; 174 | if (value > (INT_LEAST32_MAX-digit) / 10) { 175 | /* we ran out of bits, what do we do? */ 176 | if (scale) { 177 | /* truncate extra precision */ 178 | break; 179 | } else { 180 | /* integer overflow. bail out. */ 181 | goto parse_error; 182 | } 183 | } 184 | value = (10 * value) + digit; 185 | if (scale) 186 | scale *= 10; 187 | } else if (*field == '.' && scale == 0) { 188 | scale = 1; 189 | } else if (*field == ' ') { 190 | /* Allow spaces at the start of the field. Not NMEA 191 | * conformant, but some modules do this. */ 192 | if (sign != 0 || value != -1 || scale != 0) 193 | goto parse_error; 194 | } else { 195 | goto parse_error; 196 | } 197 | field++; 198 | } 199 | } 200 | 201 | if ((sign || scale) && value == -1) 202 | goto parse_error; 203 | 204 | if (value == -1) { 205 | /* No digits were scanned. */ 206 | value = 0; 207 | scale = 0; 208 | } else if (scale == 0) { 209 | /* No decimal point. */ 210 | scale = 1; 211 | } 212 | if (sign) 213 | value *= sign; 214 | 215 | *va_arg(ap, struct minmea_float *) = (struct minmea_float) {value, scale}; 216 | } break; 217 | 218 | case 'i': { // Integer value, default 0 (int). 219 | int value = 0; 220 | 221 | if (field) { 222 | char *endptr; 223 | value = strtol(field, &endptr, 10); 224 | if (minmea_isfield(*endptr)) 225 | goto parse_error; 226 | } 227 | 228 | *va_arg(ap, int *) = value; 229 | } break; 230 | 231 | case 's': { // String value (char *). 232 | char *buf = va_arg(ap, char *); 233 | 234 | if (field) { 235 | while (minmea_isfield(*field)) 236 | *buf++ = *field++; 237 | } 238 | 239 | *buf = '\0'; 240 | } break; 241 | 242 | case 't': { // NMEA talker identifier and type (union minmea_type *). 243 | // This field is always mandatory. 244 | if (!field) 245 | goto parse_error; 246 | 247 | if (field[0] != '$') 248 | goto parse_error; 249 | for (int f=0; f<5; f++) 250 | if (!minmea_isfield(field[1+f])) 251 | goto parse_error; 252 | 253 | union minmea_type *buf = va_arg(ap, union minmea_type *); 254 | memcpy(buf, field+1, (sizeof(*buf) - sizeof(buf->null_terminator))); 255 | buf->null_terminator = '\0'; 256 | } break; 257 | 258 | case 'D': { // Date (int, int, int), -1 if empty. 259 | struct minmea_date *date = va_arg(ap, struct minmea_date *); 260 | 261 | int d = -1, m = -1, y = -1; 262 | 263 | if (field && minmea_isfield(*field)) { 264 | // Always six digits. 265 | for (int f=0; f<6; f++) 266 | if (!isdigit((unsigned char) field[f])) 267 | goto parse_error; 268 | 269 | char dArr[] = {field[0], field[1], '\0'}; 270 | char mArr[] = {field[2], field[3], '\0'}; 271 | char yArr[] = {field[4], field[5], '\0'}; 272 | d = strtol(dArr, NULL, 10); 273 | m = strtol(mArr, NULL, 10); 274 | y = strtol(yArr, NULL, 10); 275 | } 276 | 277 | date->day = d; 278 | date->month = m; 279 | date->year = y; 280 | } break; 281 | 282 | case 'T': { // Time (int, int, int, int), -1 if empty. 283 | struct minmea_time *time_ = va_arg(ap, struct minmea_time *); 284 | 285 | int h = -1, i = -1, s = -1, u = -1; 286 | 287 | if (field && minmea_isfield(*field)) { 288 | // Minimum required: integer time. 289 | for (int f=0; f<6; f++) 290 | if (!isdigit((unsigned char) field[f])) 291 | goto parse_error; 292 | 293 | char hArr[] = {field[0], field[1], '\0'}; 294 | char iArr[] = {field[2], field[3], '\0'}; 295 | char sArr[] = {field[4], field[5], '\0'}; 296 | h = strtol(hArr, NULL, 10); 297 | i = strtol(iArr, NULL, 10); 298 | s = strtol(sArr, NULL, 10); 299 | field += 6; 300 | 301 | // Extra: fractional time. Saved as microseconds. 302 | if (*field++ == '.') { 303 | uint32_t value = 0; 304 | uint32_t scale = 1000000LU; 305 | while (isdigit((unsigned char) *field) && scale > 1) { 306 | value = (value * 10) + (*field++ - '0'); 307 | scale /= 10; 308 | } 309 | u = value * scale; 310 | } else { 311 | u = 0; 312 | } 313 | } 314 | 315 | time_->hours = h; 316 | time_->minutes = i; 317 | time_->seconds = s; 318 | time_->microseconds = u; 319 | } break; 320 | 321 | case '_': { // Ignore the field. 322 | } break; 323 | 324 | default: { // Unknown. 325 | goto parse_error; 326 | } 327 | } 328 | 329 | next_field(); 330 | } 331 | 332 | result = true; 333 | 334 | parse_error: 335 | va_end(ap); 336 | return result; 337 | } 338 | 339 | bool minmea_talker_id(char talker[3], const char *sentence) 340 | { 341 | union minmea_type type; 342 | if (!minmea_scan(sentence, "t", &type)) 343 | return false; 344 | 345 | talker[0] = type.talker_id[0]; 346 | talker[1] = type.talker_id[1]; 347 | talker[2] = '\0'; 348 | 349 | return true; 350 | } 351 | 352 | struct sentence_id_map_entry { 353 | const char *str; 354 | enum minmea_sentence_id id; 355 | }; 356 | 357 | struct sentence_id_map_entry sentence_id_map[] = { 358 | { "INVALID", MINMEA_INVALID }, 359 | { "GBS", MINMEA_SENTENCE_GBS }, 360 | { "GGA", MINMEA_SENTENCE_GGA }, 361 | { "GLL", MINMEA_SENTENCE_GLL }, 362 | { "GSA", MINMEA_SENTENCE_GSA }, 363 | { "GST", MINMEA_SENTENCE_GST }, 364 | { "GSV", MINMEA_SENTENCE_GSV }, 365 | { "RMC", MINMEA_SENTENCE_RMC }, 366 | { "VTG", MINMEA_SENTENCE_VTG }, 367 | { "ZDA", MINMEA_SENTENCE_ZDA }, 368 | }; 369 | 370 | const char* minmea_sentence(enum minmea_sentence_id id) { 371 | if (id < 0 || id >= (int)countof(sentence_id_map)) { 372 | return "UNKNOWN"; 373 | } 374 | return sentence_id_map[id].str; 375 | } 376 | 377 | enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict) 378 | { 379 | if (!minmea_check(sentence, strict)) 380 | return MINMEA_INVALID; 381 | 382 | union minmea_type type; 383 | if (!minmea_scan(sentence, "t", &type)) 384 | return MINMEA_INVALID; 385 | 386 | for (unsigned int i = 0; i < countof(sentence_id_map); i++) { 387 | if (!memcmp(type.sentence_id, sentence_id_map[i].str, sizeof(type.sentence_id))) { 388 | return sentence_id_map[i].id; 389 | } 390 | } 391 | 392 | return MINMEA_UNKNOWN; 393 | } 394 | 395 | bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence) 396 | { 397 | // $GNGBS,170556.00,3.0,2.9,8.3,,,,*5C 398 | if (!minmea_scan(sentence, "tTfffifff", 399 | &frame->type, 400 | &frame->time, 401 | &frame->err_latitude, 402 | &frame->err_longitude, 403 | &frame->err_altitude, 404 | &frame->svid, 405 | &frame->prob, 406 | &frame->bias, 407 | &frame->stddev 408 | )) 409 | return false; 410 | if (memcmp(frame->type.sentence_id, "GBS", sizeof(frame->type.sentence_id))) 411 | return false; 412 | 413 | return true; 414 | } 415 | 416 | bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence) 417 | { 418 | // $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62 419 | char validity; 420 | int latitude_direction; 421 | int longitude_direction; 422 | int variation_direction; 423 | if (!minmea_scan(sentence, "tTcfdfdffDfd", 424 | &frame->type, 425 | &frame->time, 426 | &validity, 427 | &frame->latitude, &latitude_direction, 428 | &frame->longitude, &longitude_direction, 429 | &frame->speed, 430 | &frame->course, 431 | &frame->date, 432 | &frame->variation, &variation_direction)) 433 | return false; 434 | if (memcmp(frame->type.sentence_id, "RMC", sizeof(frame->type.sentence_id))) 435 | return false; 436 | 437 | frame->valid = (validity == 'A'); 438 | frame->latitude.value *= latitude_direction; 439 | frame->longitude.value *= longitude_direction; 440 | frame->variation.value *= variation_direction; 441 | 442 | return true; 443 | } 444 | 445 | bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence) 446 | { 447 | // $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47 448 | int latitude_direction; 449 | int longitude_direction; 450 | 451 | if (!minmea_scan(sentence, "tTfdfdiiffcfcf_", 452 | &frame->type, 453 | &frame->time, 454 | &frame->latitude, &latitude_direction, 455 | &frame->longitude, &longitude_direction, 456 | &frame->fix_quality, 457 | &frame->satellites_tracked, 458 | &frame->hdop, 459 | &frame->altitude, &frame->altitude_units, 460 | &frame->height, &frame->height_units, 461 | &frame->dgps_age)) 462 | return false; 463 | if (memcmp(frame->type.sentence_id, "GGA", sizeof(frame->type.sentence_id))) 464 | return false; 465 | 466 | frame->latitude.value *= latitude_direction; 467 | frame->longitude.value *= longitude_direction; 468 | 469 | return true; 470 | } 471 | 472 | bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence) 473 | { 474 | // $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39 475 | 476 | if (!minmea_scan(sentence, "tciiiiiiiiiiiiifff", 477 | &frame->type, 478 | &frame->mode, 479 | &frame->fix_type, 480 | &frame->sats[0], 481 | &frame->sats[1], 482 | &frame->sats[2], 483 | &frame->sats[3], 484 | &frame->sats[4], 485 | &frame->sats[5], 486 | &frame->sats[6], 487 | &frame->sats[7], 488 | &frame->sats[8], 489 | &frame->sats[9], 490 | &frame->sats[10], 491 | &frame->sats[11], 492 | &frame->pdop, 493 | &frame->hdop, 494 | &frame->vdop)) 495 | return false; 496 | if (memcmp(frame->type.sentence_id, "GSA", sizeof(frame->type.sentence_id))) 497 | return false; 498 | 499 | return true; 500 | } 501 | 502 | bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence) 503 | { 504 | // $GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41$; 505 | int latitude_direction; 506 | int longitude_direction; 507 | 508 | if (!minmea_scan(sentence, "tfdfdTc;c", 509 | &frame->type, 510 | &frame->latitude, &latitude_direction, 511 | &frame->longitude, &longitude_direction, 512 | &frame->time, 513 | &frame->status, 514 | &frame->mode)) 515 | return false; 516 | if (memcmp(frame->type.sentence_id, "GLL", sizeof(frame->type.sentence_id))) 517 | return false; 518 | 519 | frame->latitude.value *= latitude_direction; 520 | frame->longitude.value *= longitude_direction; 521 | 522 | return true; 523 | } 524 | 525 | bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence) 526 | { 527 | // $GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58 528 | if (!minmea_scan(sentence, "tTfffffff", 529 | &frame->type, 530 | &frame->time, 531 | &frame->rms_deviation, 532 | &frame->semi_major_deviation, 533 | &frame->semi_minor_deviation, 534 | &frame->semi_major_orientation, 535 | &frame->latitude_error_deviation, 536 | &frame->longitude_error_deviation, 537 | &frame->altitude_error_deviation)) 538 | return false; 539 | if (memcmp(frame->type.sentence_id, "GST", sizeof(frame->type.sentence_id))) 540 | return false; 541 | 542 | return true; 543 | } 544 | 545 | bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence) 546 | { 547 | // $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74 548 | // $GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D 549 | // $GPGSV,4,2,11,08,51,203,30,09,45,215,28*75 550 | // $GPGSV,4,4,13,39,31,170,27*40 551 | // $GPGSV,4,4,13*7B 552 | 553 | if (!minmea_scan(sentence, "tiii;iiiiiiiiiiiiiiii", 554 | &frame->type, 555 | &frame->total_msgs, 556 | &frame->msg_nr, 557 | &frame->total_sats, 558 | &frame->sats[0].nr, 559 | &frame->sats[0].elevation, 560 | &frame->sats[0].azimuth, 561 | &frame->sats[0].snr, 562 | &frame->sats[1].nr, 563 | &frame->sats[1].elevation, 564 | &frame->sats[1].azimuth, 565 | &frame->sats[1].snr, 566 | &frame->sats[2].nr, 567 | &frame->sats[2].elevation, 568 | &frame->sats[2].azimuth, 569 | &frame->sats[2].snr, 570 | &frame->sats[3].nr, 571 | &frame->sats[3].elevation, 572 | &frame->sats[3].azimuth, 573 | &frame->sats[3].snr 574 | )) { 575 | return false; 576 | } 577 | if (memcmp(frame->type.sentence_id, "GSV", sizeof(frame->type.sentence_id))) 578 | return false; 579 | return true; 580 | } 581 | 582 | bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence) 583 | { 584 | // $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48 585 | // $GPVTG,156.1,T,140.9,M,0.0,N,0.0,K*41 586 | // $GPVTG,096.5,T,083.5,M,0.0,N,0.0,K,D*22 587 | // $GPVTG,188.36,T,,M,0.820,N,1.519,K,A*3F 588 | char c_true, c_magnetic, c_knots, c_kph, c_faa_mode; 589 | 590 | if (!minmea_scan(sentence, "t;fcfcfcfcc", 591 | &frame->type, 592 | &frame->true_track_degrees, 593 | &c_true, 594 | &frame->magnetic_track_degrees, 595 | &c_magnetic, 596 | &frame->speed_knots, 597 | &c_knots, 598 | &frame->speed_kph, 599 | &c_kph, 600 | &c_faa_mode)) 601 | return false; 602 | if (memcmp(frame->type.sentence_id, "VTG", sizeof(frame->type.sentence_id))) 603 | return false; 604 | // values are only valid with the accompanying characters 605 | if (c_true != 'T') 606 | frame->true_track_degrees.scale = 0; 607 | if (c_magnetic != 'M') 608 | frame->magnetic_track_degrees.scale = 0; 609 | if (c_knots != 'N') 610 | frame->speed_knots.scale = 0; 611 | if (c_kph != 'K') 612 | frame->speed_kph.scale = 0; 613 | frame->faa_mode = (enum minmea_faa_mode)c_faa_mode; 614 | 615 | return true; 616 | } 617 | 618 | bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence) 619 | { 620 | // $GPZDA,201530.00,04,07,2002,00,00*60 621 | if(!minmea_scan(sentence, "tTiiiii", 622 | &frame->type, 623 | &frame->time, 624 | &frame->date.day, 625 | &frame->date.month, 626 | &frame->date.year, 627 | &frame->hour_offset, 628 | &frame->minute_offset)) 629 | return false; 630 | if (memcmp(frame->type.sentence_id, "ZDA", sizeof(frame->type.sentence_id))) 631 | return false; 632 | 633 | // check offsets 634 | if (abs(frame->hour_offset) > 13 || 635 | frame->minute_offset > 59 || 636 | frame->minute_offset < 0) 637 | return false; 638 | 639 | return true; 640 | } 641 | 642 | int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_) 643 | { 644 | if (date->year == -1 || time_->hours == -1) 645 | return -1; 646 | 647 | memset(tm, 0, sizeof(*tm)); 648 | if (date->year < 80) { 649 | tm->tm_year = 2000 + date->year - 1900; // 2000-2079 650 | } else if (date->year >= 1900) { 651 | tm->tm_year = date->year - 1900; // 4 digit year, use directly 652 | } else { 653 | tm->tm_year = date->year; // 1980-1999 654 | } 655 | tm->tm_mon = date->month - 1; 656 | tm->tm_mday = date->day; 657 | tm->tm_hour = time_->hours; 658 | tm->tm_min = time_->minutes; 659 | tm->tm_sec = time_->seconds; 660 | 661 | return 0; 662 | } 663 | 664 | int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_) 665 | { 666 | struct tm tm; 667 | if (minmea_getdatetime(&tm, date, time_)) 668 | return -1; 669 | 670 | time_t timestamp = timegm(&tm); /* See README.md if your system lacks timegm(). */ 671 | if (timestamp != (time_t)-1) { 672 | ts->tv_sec = timestamp; 673 | ts->tv_nsec = time_->microseconds * 1000; 674 | return 0; 675 | } else { 676 | return -1; 677 | } 678 | } 679 | 680 | /* vim: set ts=4 sw=4 et: */ 681 | -------------------------------------------------------------------------------- /tests.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2014 Kosma Moczek 3 | * This program is free software. It comes without any warranty, to the extent 4 | * permitted by applicable law. You can redistribute it and/or modify it under 5 | * the terms of the Do What The Fuck You Want To Public License, Version 2, as 6 | * published by Sam Hocevar. See the COPYING file for more details. 7 | */ 8 | 9 | #pragma GCC diagnostic ignored "-Wmissing-field-initializers" 10 | #pragma GCC diagnostic ignored "-Wtype-limits" 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #include "minmea.h" 17 | 18 | static const char *valid_sentences_nochecksum[] = { 19 | "$GPTXT,xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 20 | "$GPTXT,xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 21 | "xxxxxxxxxxxxxxxxxxxxxxxxxxx", 22 | "$GPTXT,hello\n", 23 | "$GPTXT,hello\r", 24 | "$GPTXT,hello\r\n", 25 | "$GPTXT,hello\r\n\r\n", 26 | "$GPTXT,hello\n\r\r\n", 27 | NULL, 28 | }; 29 | 30 | static const char *valid_sentences_checksum[] = { 31 | "$GPTXT,01,01,02,ANTSTATUS=INIT*25", 32 | "$GPRMC,,V,,,,,,,,,,N*53", 33 | "$GPVTG,,,,,,,,,N*30", 34 | "$GPGGA,,,,,,0,00,99.99,,,,,,*48", 35 | "$GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30", 36 | "$GPGLL,,,,,,V,N*64", 37 | "$GPXTE,A,A,0.67,L,N*6F", 38 | "$GPXTE,A,A,0.67,L,N*6f", 39 | "$GPGGA,123204.00,5106.94086,N,01701.51680,E,1,06,3.86,127.9,M,40.5,M,,*51", 40 | "$GPGSA,A,3,02,08,09,05,04,26,,,,,,,4.92,3.86,3.05*00", 41 | "$GPGSV,4,1,13,02,28,259,33,04,12,212,27,05,34,305,30,07,79,138,*7F", 42 | "$GPGSV,4,2,13,08,51,203,30,09,45,215,28,10,69,197,19,13,47,081,*76", 43 | "$GPGSV,4,3,13,16,20,040,17,26,08,271,30,28,01,168,18,33,24,219,27*74", 44 | "$GPGSV,4,4,13,39,31,170,27*40", 45 | "$GPGLL,5106.94086,N,01701.51680,E,123204.00,A,A*63", 46 | "$GPRMC,123205.00,A,5106.94085,N,01701.51689,E,0.016,,280214,,,A*7B", 47 | "$GPVTG,,T,,M,0.016,N,0.030,K,A*27", 48 | "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58", 49 | "$GPZDA,160012.71,11,03,2004,-1,00*7D", 50 | "$GNGBS,170556.00,3.0,2.9,8.3,,,,*5C", 51 | NULL, 52 | }; 53 | 54 | static const char *invalid_sentences[] = { 55 | "$GPTXT,01,01,02,ANTSTATUS=INIT*26", 56 | "$GPRMC,,V,,,,,,,,,,N*532", 57 | "$GPVTG,,,,\xff,,,,,N*30", 58 | "$$GPGGA,,,,,,0,00,99.99,,,,,,*48", 59 | "GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30", 60 | "gps: $GPGLL,,,,,,V,N", 61 | "$GPXTE,A,A,0.67,L,N*6e", 62 | "$GPXTE,A,A,0.67,L,N*6g", 63 | "$GPTXT,hello\n ", 64 | "$GPTXT,hello\r*24", 65 | "$GPTXT,hello\r\n$", 66 | NULL, 67 | }; 68 | 69 | START_TEST(test_minmea_checksum) 70 | { 71 | ck_assert_int_eq(minmea_checksum(""), 0x00); 72 | ck_assert_int_eq(minmea_checksum("$"), 0x00); 73 | ck_assert_int_eq(minmea_checksum("*"), 0x00); 74 | ck_assert_int_eq(minmea_checksum("$*"), 0x00); 75 | ck_assert_int_eq(minmea_checksum("$GPTXT,01,01,02,ANTSTATUS=INIT*25"), 0x25); 76 | ck_assert_int_eq(minmea_checksum("$GPTXT,01,01,02,ANTSTATUS=INIT"), 0x25); 77 | ck_assert_int_eq(minmea_checksum("GPTXT,01,01,02,ANTSTATUS=INIT*25"), 0x25); 78 | ck_assert_int_eq(minmea_checksum("GPTXT,01,01,02,ANTSTATUS=INIT"), 0x25); 79 | ck_assert_int_eq(minmea_checksum("$GPXTE,A,A,0.67,L,N*6F"), 0x6f); 80 | ck_assert_int_eq(minmea_checksum("GPXTE,A,A,0.67,L,N*6f"), 0x6f); 81 | } 82 | END_TEST 83 | 84 | START_TEST(test_minmea_check) 85 | { 86 | for (const char **sentence=valid_sentences_nochecksum; *sentence; sentence++) { 87 | ck_assert_msg(minmea_check(*sentence, false) == true, "%s", *sentence); 88 | ck_assert_msg(minmea_check(*sentence, true) == false, "%s", *sentence); 89 | } 90 | 91 | for (const char **sentence=valid_sentences_checksum; *sentence; sentence++) { 92 | ck_assert_msg(minmea_check(*sentence, false) == true, "%s", *sentence); 93 | ck_assert_msg(minmea_check(*sentence, true) == true, "%s", *sentence); 94 | } 95 | 96 | for (const char **sentence=invalid_sentences; *sentence; sentence++) { 97 | ck_assert_msg(minmea_check(*sentence, false) == false, "%s", *sentence); 98 | ck_assert_msg(minmea_check(*sentence, true) == false, "%s", *sentence); 99 | } 100 | } 101 | END_TEST 102 | 103 | START_TEST(test_minmea_scan_c) 104 | { 105 | char ch, extra; 106 | 107 | ck_assert(minmea_scan("A,123.45", "c", &ch) == true); 108 | ck_assert_int_eq(ch, 'A'); 109 | 110 | ck_assert(minmea_scan("WUT,123.45", "c", &ch) == true); 111 | ck_assert_int_eq(ch, 'W'); 112 | 113 | ck_assert(minmea_scan(",123.45", "c", &ch) == true); 114 | ck_assert_int_eq(ch, '\0'); 115 | 116 | ck_assert(minmea_scan("A,B", "cc", &ch, &extra) == true); 117 | ck_assert_int_eq(ch, 'A'); 118 | ck_assert_int_eq(extra, 'B'); 119 | 120 | ck_assert(minmea_scan("C", "cc", &ch, &extra) == false); 121 | 122 | ck_assert(minmea_scan("D", "c;c", &ch, &extra) == true); 123 | ck_assert_int_eq(ch, 'D'); 124 | ck_assert_int_eq(extra, '\0'); 125 | ck_assert(minmea_scan("E,F", "c;c", &ch, &extra) == true); 126 | ck_assert_int_eq(ch, 'E'); 127 | ck_assert_int_eq(extra, 'F'); 128 | } 129 | END_TEST 130 | 131 | START_TEST(test_minmea_scan_d) 132 | { 133 | int direction; 134 | 135 | ck_assert(minmea_scan("K", "d", &direction) == false); 136 | 137 | ck_assert(minmea_scan("", "d", &direction) == true); 138 | ck_assert(minmea_scan(",foo", "d", &direction) == true); 139 | ck_assert_int_eq(direction, 0); 140 | ck_assert(minmea_scan("N", "d", &direction) == true); 141 | ck_assert_int_eq(direction, 1); 142 | ck_assert(minmea_scan("S,foo", "d", &direction) == true); 143 | ck_assert_int_eq(direction, -1); 144 | ck_assert(minmea_scan("W", "d", &direction) == true); 145 | ck_assert_int_eq(direction, -1); 146 | ck_assert(minmea_scan("E,foo", "d", &direction) == true); 147 | ck_assert_int_eq(direction, 1); 148 | } 149 | END_TEST 150 | 151 | START_TEST(test_minmea_scan_f) 152 | { 153 | struct minmea_float f; 154 | 155 | ck_assert(minmea_scan("-", "f", &f) == false); 156 | ck_assert(minmea_scan("10-", "f", &f) == false); 157 | ck_assert(minmea_scan("+-10", "f", &f) == false); 158 | ck_assert(minmea_scan("12..45", "f", &f) == false); 159 | ck_assert(minmea_scan("blah", "f", &f) == false); 160 | ck_assert(minmea_scan("12.3.4", "f", &f) == false); 161 | 162 | ck_assert(minmea_scan(",", "f", &f) == true); 163 | ck_assert_int_eq(f.scale, 0); 164 | ck_assert(minmea_scan("", "f", &f) == true); 165 | ck_assert_int_eq(f.scale, 0); 166 | 167 | ck_assert(minmea_scan("42", "f", &f) == true); 168 | ck_assert_int_eq(f.value, 42); 169 | ck_assert_int_eq(f.scale, 1); 170 | 171 | ck_assert(minmea_scan("15.345", "f", &f) == true); 172 | ck_assert_int_eq(f.value, 15345); 173 | ck_assert_int_eq(f.scale, 1000); 174 | 175 | ck_assert(minmea_scan("-1.23,V", "f", &f) == true); 176 | ck_assert_int_eq(f.value, -123); 177 | ck_assert_int_eq(f.scale, 100); 178 | 179 | /* the guaranteed range is 32 bits which translates to +-180 degrees 180 | * with 5 decimal digits. make sure we support that. */ 181 | ck_assert(minmea_scan("18000.00000", "f", &f) == true); 182 | ck_assert_int_eq(f.value, 1800000000); 183 | ck_assert_int_eq(f.scale, 100000); 184 | ck_assert(minmea_scan("-18000.00000", "f", &f) == true); 185 | ck_assert_int_eq(f.value, -1800000000); 186 | ck_assert_int_eq(f.scale, 100000); 187 | 188 | if (sizeof(int_least32_t) == 4) { 189 | /* fits in 32 bits */ 190 | ck_assert(minmea_scan("2147483647", "f", &f) == true); 191 | ck_assert_int_eq(f.value, 2147483647); 192 | ck_assert_int_eq(f.scale, 1); 193 | /* doesn't fit, truncate precision */ 194 | ck_assert(minmea_scan("2147483.648", "f", &f) == true); 195 | ck_assert_int_eq(f.value, 214748364); 196 | ck_assert_int_eq(f.scale, 100); 197 | /* doesn't fit, bail out */ 198 | ck_assert(minmea_scan("2147483648", "f", &f) == false); 199 | } else if (sizeof(int_least32_t) == 8) { 200 | /* Casting to int64_t is ugly, but otherwise we get this on machines with 32-bit int_least32_t: 201 | * error: comparison is always false due to limited range of data type [-Werror=type-limits] */ 202 | /* fits in 64 bits */ 203 | ck_assert(minmea_scan("9223372036854775807", "f", &f) == true); 204 | ck_assert_int_eq((int64_t) f.value, 9223372036854775807LL); 205 | ck_assert_int_eq(f.scale, 1); 206 | /* doesn't fit, truncate precision */ 207 | ck_assert(minmea_scan("9223372036854775.808", "f", &f) == true); 208 | ck_assert_int_eq((int64_t) f.value, 922337203685477580LL); 209 | ck_assert_int_eq(f.scale, 100); 210 | /* doesn't fit, bail out */ 211 | ck_assert(minmea_scan("9223372036854775808", "f", &f) == false); 212 | } else { 213 | ck_abort_msg("your platform is esoteric. please fix this unit test."); 214 | } 215 | 216 | /* optional f.values */ 217 | ck_assert(minmea_scan("foo", "_;f", &f) == true); 218 | ck_assert_int_eq(f.scale, 0); 219 | ck_assert(minmea_scan("foo,", "_;f", &f) == true); 220 | ck_assert_int_eq(f.scale, 0); 221 | ck_assert(minmea_scan("foo,12.3", "_;f", &f) == true); 222 | ck_assert_int_eq(f.value, 123); 223 | ck_assert_int_eq(f.scale, 10); 224 | 225 | /* accept spaces at the start of the field. some modules do this, unfortunately. */ 226 | ck_assert(minmea_scan(" -1.23,V", "f", &f) == true); 227 | ck_assert_int_eq(f.value, -123); 228 | ck_assert_int_eq(f.scale, 100); 229 | ck_assert(minmea_scan(" -4.56,V", "f", &f) == true); 230 | ck_assert_int_eq(f.value, -456); 231 | ck_assert_int_eq(f.scale, 100); 232 | ck_assert(minmea_scan("-3.33 ,V", "f", &f) == false); 233 | ck_assert(minmea_scan(" -3.33 ,V", "f", &f) == false); 234 | ck_assert(minmea_scan("-3. 33,V", "f", &f) == false); 235 | ck_assert(minmea_scan("0 .0,V", "f", &f) == false); 236 | } 237 | END_TEST 238 | 239 | START_TEST(test_minmea_scan_i) 240 | { 241 | int value, extra; 242 | 243 | // valid parses 244 | ck_assert(minmea_scan("14", "i", &value) == true); 245 | ck_assert_int_eq(value, 14); 246 | ck_assert(minmea_scan("-1234", "i", &value) == true); 247 | ck_assert_int_eq(value, -1234); 248 | 249 | // empty field 250 | ck_assert(minmea_scan("", "i", &value) == true); 251 | ck_assert_int_eq(value, 0); 252 | 253 | // invalid value 254 | ck_assert(minmea_scan("foo", "i", &value) == false); 255 | 256 | // missing field 257 | ck_assert(minmea_scan("41", "ii", &value, &extra) == false); 258 | 259 | /* optional values */ 260 | ck_assert(minmea_scan("10", "i;i", &value, &extra) == true); 261 | ck_assert_int_eq(value, 10); 262 | ck_assert(minmea_scan("20,30", "i;i", &value, &extra) == true); 263 | ck_assert_int_eq(value, 20); 264 | ck_assert_int_eq(extra, 30); 265 | ck_assert(minmea_scan("42,foo", "i;i", &value, &extra) == false); 266 | } 267 | END_TEST 268 | 269 | START_TEST(test_minmea_scan_s) 270 | { 271 | char value[MINMEA_MAX_SENTENCE_LENGTH]; 272 | 273 | ck_assert(minmea_scan(",bar,baz", "s", value) == true); 274 | ck_assert_str_eq(value, ""); 275 | ck_assert(minmea_scan("foo,bar,baz", "s", value) == true); 276 | ck_assert_str_eq(value, "foo"); 277 | 278 | ck_assert(minmea_scan("dummy", "_;s", value) == true); 279 | ck_assert_str_eq(value, ""); 280 | ck_assert(minmea_scan("dummy,foo", "_;s", value) == true); 281 | ck_assert_str_eq(value, "foo"); 282 | ck_assert(minmea_scan("dummy,", "_;s", value) == true); 283 | ck_assert_str_eq(value, ""); 284 | } 285 | END_TEST 286 | 287 | START_TEST(test_minmea_scan_t) 288 | { 289 | union minmea_type type; 290 | 291 | ck_assert(minmea_scan("$GPRM,foo,bar,baz", "t", type) == false); 292 | ck_assert(minmea_scan("GPRMC,foo,bar,baz", "t", type) == false); 293 | ck_assert(minmea_scan("$GPRMC,foo,bar,baz", "t", &type) == true); 294 | ck_assert(memcmp(type.talker_id, "GP", sizeof(type.talker_id)) == 0); 295 | ck_assert(memcmp(type.sentence_id, "RMC", sizeof(type.sentence_id)) == 0); 296 | } 297 | END_TEST 298 | 299 | START_TEST(test_minmea_scan_t_str) 300 | { 301 | //"type" used to be a string and legacy code may still rely on that. See 302 | //for example, minmea_sentence_id(). Test that assumption remains valid here. 303 | //Seed array with non-null bytes to ensure all is correctly populated. 304 | char type[] = { 'X', 'X', 'X', 'X', 'X', 'X' }; 305 | ck_assert(minmea_scan("$GPRMC,foo,bar,baz", "t", &type) == true); 306 | ck_assert(strcmp(type, "GPRMC") == 0); 307 | } 308 | END_TEST 309 | 310 | START_TEST(test_minmea_scan_D) 311 | { 312 | struct minmea_date d; 313 | 314 | ck_assert(minmea_scan("$GPXXX,3112", "_D", &d) == false); 315 | ck_assert(minmea_scan("$GPXXX,foobar", "_D", &d) == false); 316 | 317 | ck_assert(minmea_scan("$GPXXX,311299", "_D", &d) == true); 318 | ck_assert_int_eq(d.day, 31); 319 | ck_assert_int_eq(d.month, 12); 320 | ck_assert_int_eq(d.year, 99); 321 | 322 | ck_assert(minmea_scan("$GPXXX,,,,,,,,,nope", "_D", &d) == true); 323 | ck_assert_int_eq(d.day, -1); 324 | ck_assert_int_eq(d.month, -1); 325 | ck_assert_int_eq(d.year, -1); 326 | } 327 | END_TEST 328 | 329 | START_TEST(test_minmea_scan_T) 330 | { 331 | struct minmea_time t; 332 | 333 | ck_assert(minmea_scan("$GPXXX,2359", "_T", &t) == false); 334 | ck_assert(minmea_scan("$GPXXX,foobar", "_T", &t) == false); 335 | 336 | ck_assert(minmea_scan("$GPXXX,235960", "_T", &t) == true); 337 | ck_assert_int_eq(t.hours, 23); 338 | ck_assert_int_eq(t.minutes, 59); 339 | ck_assert_int_eq(t.seconds, 60); 340 | ck_assert_int_eq(t.microseconds, 0); 341 | 342 | ck_assert(minmea_scan("$GPXXX,213700.001", "_T", &t) == true); 343 | ck_assert_int_eq(t.hours, 21); 344 | ck_assert_int_eq(t.minutes, 37); 345 | ck_assert_int_eq(t.seconds, 0); 346 | ck_assert_int_eq(t.microseconds, 1000); 347 | 348 | ck_assert(minmea_scan("$GPXXX,,,,,,,nope", "_T", &t) == true); 349 | ck_assert_int_eq(t.hours, -1); 350 | ck_assert_int_eq(t.minutes, -1); 351 | ck_assert_int_eq(t.seconds, -1); 352 | ck_assert_int_eq(t.microseconds, -1); 353 | } 354 | END_TEST 355 | 356 | START_TEST(test_minmea_scan_complex1) 357 | { 358 | const char *sentence = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47\r\n"; 359 | union minmea_type type; 360 | struct minmea_time t; 361 | struct minmea_float latitude; int latitude_direction; 362 | struct minmea_float longitude; int longitude_direction; 363 | int fix_quality; 364 | int satellites; 365 | struct minmea_float hdop; 366 | struct minmea_float altitude; char altitude_units; 367 | struct minmea_float height; char height_units; 368 | ck_assert(minmea_scan(sentence, "tTfdfdiiffcfc__", 369 | &type, 370 | &t, 371 | &latitude, &latitude_direction, 372 | &longitude, &longitude_direction, 373 | &fix_quality, 374 | &satellites, 375 | &hdop, 376 | &altitude, &altitude_units, 377 | &height, &height_units) == true); 378 | ck_assert(memcmp(type.talker_id, "GP", sizeof(type.talker_id)) == 0); 379 | ck_assert(memcmp(type.sentence_id, "GGA", sizeof(type.sentence_id)) == 0); 380 | ck_assert_int_eq(t.hours, 12); 381 | ck_assert_int_eq(t.minutes, 35); 382 | ck_assert_int_eq(t.seconds, 19); 383 | ck_assert_int_eq(latitude.value, 4807038); 384 | ck_assert_int_eq(latitude.scale, 1000); 385 | ck_assert_int_eq(latitude_direction, 1); 386 | ck_assert_int_eq(longitude.value, 1131000); 387 | ck_assert_int_eq(longitude.scale, 1000); 388 | ck_assert_int_eq(longitude_direction, 1); 389 | ck_assert_int_eq(fix_quality, 1); 390 | ck_assert_int_eq(satellites, 8); 391 | ck_assert_int_eq(hdop.value, 9); 392 | ck_assert_int_eq(hdop.scale, 10); 393 | ck_assert_int_eq(altitude.value, 5454); 394 | ck_assert_int_eq(altitude.scale, 10); 395 | ck_assert_int_eq(altitude_units, 'M'); 396 | ck_assert_int_eq(height.value, 469); 397 | ck_assert_int_eq(height.scale, 10); 398 | ck_assert_int_eq(height_units, 'M'); 399 | 400 | } 401 | END_TEST 402 | 403 | START_TEST(test_minmea_scan_complex2) 404 | { 405 | const char *sentence = "$GPBWC,081837,,,,,,T,,M,,N,*13"; 406 | union minmea_type type; 407 | struct minmea_time t; 408 | struct minmea_float latitude; int latitude_direction; 409 | struct minmea_float longitude; int longitude_direction; 410 | struct minmea_float bearing_true; char bearing_true_mark; 411 | struct minmea_float bearing_magnetic; char bearing_magnetic_mark; 412 | struct minmea_float distance; char distance_units; 413 | char name[MINMEA_MAX_SENTENCE_LENGTH]; 414 | ck_assert(minmea_scan(sentence, "tTfdfdfcfcfcs", 415 | &type, 416 | &t, 417 | &latitude, &latitude_direction, 418 | &longitude, &longitude_direction, 419 | &bearing_true, &bearing_true_mark, 420 | &bearing_magnetic, &bearing_magnetic_mark, 421 | &distance, &distance_units, 422 | name) == true); 423 | ck_assert(memcmp(type.talker_id, "GP", sizeof(type.talker_id)) == 0); 424 | ck_assert(memcmp(type.sentence_id, "BWC", sizeof(type.sentence_id)) == 0); 425 | ck_assert_int_eq(t.hours, 8); 426 | ck_assert_int_eq(t.minutes, 18); 427 | ck_assert_int_eq(t.seconds, 37); 428 | ck_assert_int_eq(latitude.scale, 0); 429 | ck_assert_int_eq(latitude_direction, 0); 430 | ck_assert_int_eq(longitude.scale, 0); 431 | ck_assert_int_eq(longitude_direction, 0); 432 | ck_assert_int_eq(bearing_true.scale, 0); 433 | ck_assert_int_eq(bearing_true_mark, 'T'); 434 | ck_assert_int_eq(bearing_magnetic.scale, 0); 435 | ck_assert_int_eq(bearing_magnetic_mark, 'M'); 436 | ck_assert_int_eq(distance.scale, 0); 437 | ck_assert_int_eq(distance_units, 'N'); 438 | ck_assert_str_eq(name, ""); 439 | } 440 | END_TEST 441 | 442 | START_TEST(test_minmea_scan_complex3) 443 | { 444 | const char *sentence = "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58"; 445 | union minmea_type type; 446 | struct minmea_time t; 447 | struct minmea_float rms_deviation; 448 | struct minmea_float semi_major_deviation; 449 | struct minmea_float semi_minor_deviation; 450 | struct minmea_float semi_major_orientation; 451 | struct minmea_float latitude_error_deviation; 452 | struct minmea_float longitude_error_deviation; 453 | struct minmea_float altitude_error_deviation; 454 | ck_assert(minmea_scan(sentence, "tTfffffff", 455 | &type, 456 | &t, 457 | &rms_deviation, 458 | &semi_major_deviation, 459 | &semi_minor_deviation, 460 | &semi_major_orientation, 461 | &latitude_error_deviation, 462 | &longitude_error_deviation, 463 | &altitude_error_deviation) == true); 464 | ck_assert(memcmp(type.talker_id, "GP", sizeof(type.talker_id)) == 0); 465 | ck_assert(memcmp(type.sentence_id, "GST", sizeof(type.sentence_id)) == 0); 466 | ck_assert_int_eq(t.hours, 2); 467 | ck_assert_int_eq(t.minutes, 46); 468 | ck_assert_int_eq(t.seconds, 3); 469 | ck_assert_int_eq(t.microseconds, 0); 470 | ck_assert_int_eq(rms_deviation.value, 32); 471 | ck_assert_int_eq(rms_deviation.scale, 10); 472 | ck_assert_int_eq(semi_major_deviation.value, 66); 473 | ck_assert_int_eq(semi_major_deviation.scale, 10); 474 | ck_assert_int_eq(semi_minor_deviation.value, 47); 475 | ck_assert_int_eq(semi_minor_deviation.scale, 10); 476 | ck_assert_int_eq(semi_major_orientation.value, 473); 477 | ck_assert_int_eq(semi_major_orientation.scale, 10); 478 | ck_assert_int_eq(latitude_error_deviation.value, 58); 479 | ck_assert_int_eq(latitude_error_deviation.scale, 10); 480 | ck_assert_int_eq(longitude_error_deviation.value, 56); 481 | ck_assert_int_eq(longitude_error_deviation.scale, 10); 482 | ck_assert_int_eq(altitude_error_deviation.value, 220); 483 | ck_assert_int_eq(altitude_error_deviation.scale, 10); 484 | } 485 | END_TEST 486 | 487 | START_TEST(test_minmea_parse_gbs1) 488 | { 489 | const char *sentence = "$GNGBS,170556.00,3.0,2.9,8.3,,,,"; 490 | struct minmea_sentence_gbs frame = {}; 491 | static const struct minmea_sentence_gbs expected = { 492 | .type = { .talker_id = "GN", .sentence_id = "GBS", .null_terminator = '\0' }, 493 | .time = { 17, 5, 56, 0 }, 494 | .err_latitude = { 30, 10 }, 495 | .err_longitude = { 29, 10 }, 496 | .err_altitude = { 83, 10 }, 497 | .svid = 0, 498 | .prob = { 0, 0 }, 499 | .bias = { 0, 0 }, 500 | .stddev = { 0, 0 }, 501 | }; 502 | ck_assert(minmea_check(sentence, false) == true); 503 | ck_assert(minmea_check(sentence, true) == false); 504 | ck_assert(minmea_parse_gbs(&frame, sentence) == true); 505 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 506 | } 507 | END_TEST 508 | 509 | START_TEST(test_minmea_parse_gbs2) 510 | { 511 | const char *sentence = "$GPGBS,015509.00,-0.031,-0.186,0.219,19,0.000,-0.354,6.972*4D"; 512 | struct minmea_sentence_gbs frame = {}; 513 | static const struct minmea_sentence_gbs expected = { 514 | .type = { .talker_id = "GP", .sentence_id = "GBS", .null_terminator = '\0' }, 515 | .time = { 1, 55, 9 }, 516 | .err_latitude = { -31, 1000 }, 517 | .err_longitude = { -186, 1000 }, 518 | .err_altitude = { 219, 1000 }, 519 | .svid = 19, 520 | .prob = { 0, 1000 }, 521 | .bias = { -354, 1000 }, 522 | .stddev = { 6972, 1000 }, 523 | }; 524 | ck_assert(minmea_check(sentence, false) == true); 525 | ck_assert(minmea_check(sentence, true) == true); 526 | ck_assert(minmea_parse_gbs(&frame, sentence) == true); 527 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 528 | } 529 | END_TEST 530 | 531 | START_TEST(test_minmea_parse_rmc1) 532 | { 533 | const char *sentence = "$GPRMC,081836.75,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E"; 534 | struct minmea_sentence_rmc frame = {}; 535 | static const struct minmea_sentence_rmc expected = { 536 | .type = { .talker_id = "GP", .sentence_id = "RMC", .null_terminator = '\0' }, 537 | .time = { 8, 18, 36, 750000 }, 538 | .valid = true, 539 | .latitude = { -375165, 100 }, 540 | .longitude = { 1450736, 100 }, 541 | .speed = { 0, 10 }, 542 | .course = { 3600, 10 }, 543 | .date = { 13, 9, 98 }, 544 | .variation = { 113, 10 }, 545 | }; 546 | ck_assert(minmea_check(sentence, false) == true); 547 | ck_assert(minmea_check(sentence, true) == false); 548 | ck_assert(minmea_parse_rmc(&frame, sentence) == true); 549 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 550 | } 551 | END_TEST 552 | 553 | START_TEST(test_minmea_parse_rmc2) 554 | { 555 | const char *sentence = "$GPRMC,,A,3751.65,N,14507.36,W,,,,,"; 556 | struct minmea_sentence_rmc frame = {}; 557 | static const struct minmea_sentence_rmc expected = { 558 | .type = { .talker_id = "GP", .sentence_id = "RMC", .null_terminator = '\0' }, 559 | .time = { -1, -1, -1, -1 }, 560 | .valid = true, 561 | .latitude = { 375165, 100 }, 562 | .longitude = { -1450736, 100 }, 563 | .date = { -1, -1, -1 }, 564 | }; 565 | ck_assert(minmea_check(sentence, false) == true); 566 | ck_assert(minmea_check(sentence, true) == false); 567 | ck_assert(minmea_parse_rmc(&frame, sentence) == true); 568 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 569 | } 570 | END_TEST 571 | 572 | START_TEST(test_minmea_parse_gga1) 573 | { 574 | const char *sentence = "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47"; 575 | struct minmea_sentence_gga frame = {}; 576 | struct minmea_sentence_gga expected = {}; 577 | expected.type = (union minmea_type){ .talker_id = "GP", .sentence_id = "GGA", .null_terminator = '\0' }; 578 | expected.time = (struct minmea_time) { 12, 35, 19, 0 }; 579 | expected.latitude = (struct minmea_float) { 4807038, 1000 }; 580 | expected.longitude = (struct minmea_float) { 1131000, 1000 }; 581 | expected.fix_quality = 1; 582 | expected.satellites_tracked = 8; 583 | expected.hdop = (struct minmea_float) { 9, 10 }; 584 | expected.altitude = (struct minmea_float) { 5454, 10 }; 585 | expected.altitude_units = 'M'; 586 | expected.height = (struct minmea_float) { 469, 10 }; 587 | expected.height_units = 'M'; 588 | expected.dgps_age = (struct minmea_float) { 0, 0 }; 589 | ck_assert(minmea_check(sentence, false) == true); 590 | ck_assert(minmea_check(sentence, true) == true); 591 | ck_assert(minmea_parse_gga(&frame, sentence) == true); 592 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 593 | } 594 | END_TEST 595 | 596 | 597 | START_TEST(test_minmea_parse_gst1) 598 | { 599 | const char *sentence = "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58"; 600 | struct minmea_sentence_gst frame = {}; 601 | struct minmea_sentence_gst expected = {}; 602 | expected.type = (union minmea_type){ .talker_id = "GP", .sentence_id = "GST", .null_terminator = '\0' }; 603 | expected.time = (struct minmea_time){ 2, 46, 3, 0 }; 604 | expected.rms_deviation = (struct minmea_float){ 32, 10 }; 605 | expected.semi_major_deviation = (struct minmea_float){ 66, 10 }; 606 | expected.semi_minor_deviation = (struct minmea_float){ 47, 10 }; 607 | expected.semi_major_orientation = (struct minmea_float){ 473, 10 }; 608 | expected.latitude_error_deviation = (struct minmea_float){ 58, 10 }; 609 | expected.longitude_error_deviation = (struct minmea_float){ 56, 10 }; 610 | expected.altitude_error_deviation = (struct minmea_float){ 220, 10 }; 611 | 612 | ck_assert(minmea_check(sentence, false) == true); 613 | ck_assert(minmea_check(sentence, true) == true); 614 | ck_assert(minmea_parse_gst(&frame, sentence) == true); 615 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 616 | } 617 | END_TEST 618 | 619 | START_TEST(test_minmea_parse_gsa1) 620 | { 621 | const char *sentence = "$GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39"; 622 | struct minmea_sentence_gsa frame = {}; 623 | static const struct minmea_sentence_gsa expected = { 624 | .type = { .talker_id = "GP", .sentence_id = "GSA", .null_terminator = '\0' }, 625 | .mode = MINMEA_GPGSA_MODE_AUTO, 626 | .fix_type = MINMEA_GPGSA_FIX_3D, 627 | .sats = { 4, 5, 0, 9, 12, 0, 0, 24, 0, 0, 0, 0 }, 628 | .pdop = { 25, 10 }, 629 | .hdop = { 13, 10 }, 630 | .vdop = { 21, 10 }, 631 | }; 632 | ck_assert(minmea_check(sentence, false) == true); 633 | ck_assert(minmea_check(sentence, true) == true); 634 | ck_assert(minmea_parse_gsa(&frame, sentence) == true); 635 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 636 | } 637 | END_TEST 638 | 639 | START_TEST(test_minmea_parse_gll1) 640 | { 641 | const char *sentence = "$GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41"; 642 | struct minmea_sentence_gll frame; 643 | struct minmea_sentence_gll expected; 644 | 645 | // clear structs before initialization to enable use of memcmp() 646 | // todo: add for other structs 647 | memset(&frame, 0, sizeof(frame)); 648 | memset(&expected, 0, sizeof(expected)); 649 | 650 | expected.type = (union minmea_type){ .talker_id = "GP", .sentence_id = "GLL", .null_terminator = '\0' }; 651 | expected.latitude = (struct minmea_float){ 37232475, 10000 }; 652 | expected.longitude = (struct minmea_float){ -121583416, 10000 }; 653 | expected.time = (struct minmea_time){ 16, 12, 29, 487000 }; 654 | expected.status = MINMEA_GLL_STATUS_DATA_VALID; 655 | expected.mode = MINMEA_FAA_MODE_AUTONOMOUS; 656 | 657 | ck_assert(minmea_check(sentence, false) == true); 658 | ck_assert(minmea_check(sentence, true) == true); 659 | ck_assert(minmea_parse_gll(&frame, sentence) == true); 660 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 661 | } 662 | END_TEST 663 | 664 | START_TEST(test_minmea_parse_gll2) 665 | { 666 | const char *sentence = "$GPGLL,4916.45,N,12311.12,W,225444,A"; 667 | struct minmea_sentence_gll frame = {}; 668 | struct minmea_sentence_gll expected = { 669 | .type = { .talker_id = "GP", .sentence_id = "GLL", .null_terminator = '\0' }, 670 | .latitude = { 491645, 100 }, 671 | .longitude = { -1231112, 100 }, 672 | .time = { 22, 54, 44 }, 673 | .status = MINMEA_GLL_STATUS_DATA_VALID, 674 | .mode = 0, 675 | }; 676 | 677 | ck_assert(minmea_check(sentence, false) == true); 678 | ck_assert(minmea_check(sentence, true) == false); 679 | ck_assert(minmea_parse_gll(&frame, sentence) == true); 680 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 681 | } 682 | END_TEST 683 | 684 | START_TEST(test_minmea_parse_gsv1) 685 | { 686 | const char *sentence = "$GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D"; 687 | struct minmea_sentence_gsv frame = {}; 688 | static const struct minmea_sentence_gsv expected = { 689 | .type = { .talker_id = "GP", .sentence_id = "GSV", .null_terminator = '\0' }, 690 | .total_msgs = 3, 691 | .msg_nr = 3, 692 | .total_sats = 11, 693 | .sats = { 694 | { 695 | .nr = 22, 696 | .elevation = 42, 697 | .azimuth = 67, 698 | .snr = 42 699 | }, 700 | { 701 | .nr = 24, 702 | .elevation = 14, 703 | .azimuth = 311, 704 | .snr = 43 705 | }, 706 | { 707 | .nr = 27, 708 | .elevation = 5, 709 | .azimuth = 244, 710 | .snr = 0 711 | }, 712 | { 713 | .nr = 0, 714 | .elevation = 0, 715 | .azimuth = 0, 716 | .snr = 0 717 | } 718 | } 719 | }; 720 | ck_assert(minmea_check(sentence, false) == true); 721 | ck_assert(minmea_check(sentence, true) == true); 722 | ck_assert(minmea_parse_gsv(&frame, sentence) == true); 723 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 724 | } 725 | END_TEST 726 | 727 | START_TEST(test_minmea_parse_gsv2) 728 | { 729 | const char *sentence = "$GPGSV,4,2,11,08,51,203,30,09,45,215,28*75"; 730 | struct minmea_sentence_gsv frame = {}; 731 | static const struct minmea_sentence_gsv expected = { 732 | .type = { .talker_id = "GP", .sentence_id = "GSV", .null_terminator = '\0' }, 733 | .total_msgs = 4, 734 | .msg_nr = 2, 735 | .total_sats = 11, 736 | .sats = { 737 | { 738 | .nr = 8, 739 | .elevation = 51, 740 | .azimuth = 203, 741 | .snr = 30 742 | }, 743 | { 744 | .nr = 9, 745 | .elevation = 45, 746 | .azimuth = 215, 747 | .snr = 28 748 | }, 749 | { 750 | .nr = 0, 751 | .elevation = 0, 752 | .azimuth = 0, 753 | .snr = 0 754 | }, 755 | { 756 | .nr = 0, 757 | .elevation = 0, 758 | .azimuth = 0, 759 | .snr = 0 760 | } 761 | } 762 | }; 763 | ck_assert(minmea_check(sentence, false) == true); 764 | ck_assert(minmea_check(sentence, true) == true); 765 | ck_assert(minmea_parse_gsv(&frame, sentence) == true); 766 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 767 | } 768 | END_TEST 769 | 770 | START_TEST(test_minmea_parse_gsv3) 771 | { 772 | const char *sentence = "$GPGSV,4,4,13,39,31,170,27*40"; 773 | struct minmea_sentence_gsv frame = {}; 774 | static const struct minmea_sentence_gsv expected = { 775 | .type = { .talker_id = "GP", .sentence_id = "GSV", .null_terminator = '\0' }, 776 | .total_msgs = 4, 777 | .msg_nr = 4, 778 | .total_sats = 13, 779 | .sats = { 780 | { 781 | .nr = 39, 782 | .elevation = 31, 783 | .azimuth = 170, 784 | .snr = 27 785 | }, 786 | { 787 | .nr = 0, 788 | .elevation = 0, 789 | .azimuth = 0, 790 | .snr = 0 791 | }, 792 | { 793 | .nr = 0, 794 | .elevation = 0, 795 | .azimuth = 0, 796 | .snr = 0 797 | }, 798 | { 799 | .nr = 0, 800 | .elevation = 0, 801 | .azimuth = 0, 802 | .snr = 0 803 | } 804 | } 805 | }; 806 | ck_assert(minmea_check(sentence, false) == true); 807 | ck_assert(minmea_check(sentence, true) == true); 808 | ck_assert(minmea_parse_gsv(&frame, sentence) == true); 809 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 810 | } 811 | END_TEST 812 | 813 | START_TEST(test_minmea_parse_gsv4) 814 | { 815 | const char *sentence = "$GPGSV,4,4,13*7B"; 816 | struct minmea_sentence_gsv frame = {}; 817 | static const struct minmea_sentence_gsv expected = { 818 | .type = { .talker_id = "GP", .sentence_id = "GSV", .null_terminator = '\0' }, 819 | .total_msgs = 4, 820 | .msg_nr = 4, 821 | .total_sats = 13, 822 | .sats = { 823 | { 824 | .nr = 0, 825 | .elevation = 0, 826 | .azimuth = 0, 827 | .snr = 0 828 | }, 829 | { 830 | .nr = 0, 831 | .elevation = 0, 832 | .azimuth = 0, 833 | .snr = 0 834 | }, 835 | { 836 | .nr = 0, 837 | .elevation = 0, 838 | .azimuth = 0, 839 | .snr = 0 840 | }, 841 | { 842 | .nr = 0, 843 | .elevation = 0, 844 | .azimuth = 0, 845 | .snr = 0 846 | } 847 | } 848 | }; 849 | ck_assert(minmea_check(sentence, false) == true); 850 | ck_assert(minmea_check(sentence, true) == true); 851 | ck_assert(minmea_parse_gsv(&frame, sentence) == true); 852 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 853 | } 854 | END_TEST 855 | 856 | START_TEST(test_minmea_parse_gsv5) 857 | { 858 | const char *sentence = "$GPGSV,4,1,13,02,28,259,33,04,12,212,27,05,34,305,30,07,79,138,*7F"; 859 | struct minmea_sentence_gsv frame = {}; 860 | static const struct minmea_sentence_gsv expected = { 861 | .type = { .talker_id = "GP", .sentence_id = "GSV", .null_terminator = '\0' }, 862 | .total_msgs = 4, 863 | .msg_nr = 1, 864 | .total_sats = 13, 865 | .sats = { 866 | { 867 | .nr = 2, 868 | .elevation = 28, 869 | .azimuth = 259, 870 | .snr = 33 871 | }, 872 | { 873 | .nr = 4, 874 | .elevation = 12, 875 | .azimuth = 212, 876 | .snr = 27 877 | }, 878 | { 879 | .nr = 5, 880 | .elevation = 34, 881 | .azimuth = 305, 882 | .snr = 30 883 | }, 884 | { 885 | .nr = 7, 886 | .elevation = 79, 887 | .azimuth = 138, 888 | .snr = 0 889 | } 890 | } 891 | }; 892 | ck_assert(minmea_check(sentence, false) == true); 893 | ck_assert(minmea_check(sentence, true) == true); 894 | ck_assert(minmea_parse_gsv(&frame, sentence) == true); 895 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 896 | } 897 | END_TEST 898 | 899 | 900 | START_TEST(test_minmea_parse_vtg1) 901 | { 902 | const char *sentence = "$GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48"; 903 | // clear structs before initialization to enable use of memcmp() 904 | struct minmea_sentence_vtg frame = {}; 905 | struct minmea_sentence_vtg expected = {}; 906 | 907 | expected = (struct minmea_sentence_vtg){ 908 | .type = { .talker_id = "GP", .sentence_id = "VTG", .null_terminator = '\0' }, 909 | .true_track_degrees = { 547, 10 }, 910 | .magnetic_track_degrees = { 344, 10 }, 911 | .speed_knots = { 55, 10 }, 912 | .speed_kph = { 102, 10 }, 913 | .faa_mode = 0, 914 | }; 915 | 916 | ck_assert(minmea_check(sentence, false) == true); 917 | ck_assert(minmea_check(sentence, true) == true); 918 | ck_assert(minmea_parse_vtg(&frame, sentence) == true); 919 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 920 | } 921 | END_TEST 922 | 923 | START_TEST(test_minmea_parse_vtg2) 924 | { 925 | const char *sentence = "$GPVTG,188.36,T,,M,0.820,N,1.519,K,A*3F"; 926 | // clear structs before initialization to enable use of memcmp() 927 | struct minmea_sentence_vtg frame = {}; 928 | struct minmea_sentence_vtg expected = {}; 929 | 930 | expected = (struct minmea_sentence_vtg){ 931 | .type = { .talker_id = "GP", .sentence_id = "VTG", .null_terminator = '\0' }, 932 | .true_track_degrees = { 18836, 100 }, 933 | .magnetic_track_degrees = { 0, 0 }, 934 | .speed_knots = { 820, 1000 }, 935 | .speed_kph = { 1519, 1000 }, 936 | .faa_mode = MINMEA_FAA_MODE_AUTONOMOUS, 937 | }; 938 | 939 | ck_assert(minmea_check(sentence, false) == true); 940 | ck_assert(minmea_check(sentence, true) == true); 941 | ck_assert(minmea_parse_vtg(&frame, sentence) == true); 942 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 943 | } 944 | END_TEST 945 | 946 | START_TEST(test_minmea_parse_vtg3) 947 | { 948 | // https://github.com/kosma/minmea/issues/57 949 | const char *sentence = "$GNVTG,,,,,,,,,N*2E"; 950 | // clear structs before initialization to enable use of memcmp() 951 | struct minmea_sentence_vtg frame = {}; 952 | struct minmea_sentence_vtg expected = {}; 953 | expected = (struct minmea_sentence_vtg){ 954 | .type = { .talker_id = "GN", .sentence_id = "VTG", .null_terminator = '\0' }, 955 | .true_track_degrees = { 0, 0 }, 956 | .magnetic_track_degrees = { 0, 0 }, 957 | .speed_knots = { 0, 0 }, 958 | .speed_kph = { 0, 0 }, 959 | .faa_mode = MINMEA_FAA_MODE_NOT_VALID, 960 | }; 961 | ck_assert(minmea_check(sentence, false) == true); 962 | ck_assert(minmea_check(sentence, true) == true); 963 | ck_assert(minmea_parse_vtg(&frame, sentence) == true); 964 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 965 | } 966 | END_TEST 967 | 968 | START_TEST(test_minmea_parse_zda1) 969 | { 970 | const char *sentence = "$GPZDA,160012.71,11,03,2004,-1,00*7D"; 971 | struct minmea_sentence_zda frame = {}; 972 | struct minmea_sentence_zda expected = {}; 973 | 974 | expected = (struct minmea_sentence_zda) { 975 | .type = { .talker_id = "GP", .sentence_id = "ZDA", .null_terminator = '\0' }, 976 | .time = { 16, 0, 12, 710000 }, 977 | .date = { 11, 3, 2004 }, 978 | .hour_offset = -1, 979 | .minute_offset = 0, 980 | }; 981 | 982 | ck_assert(minmea_check(sentence, false) == true); 983 | ck_assert(minmea_check(sentence, true) == true); 984 | ck_assert(minmea_parse_zda(&frame, sentence) == true); 985 | ck_assert(!memcmp(&frame, &expected, sizeof(frame))); 986 | } 987 | END_TEST 988 | 989 | START_TEST(test_minmea_usage1) 990 | { 991 | const char *sentences[] = { 992 | "$GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62", 993 | "$GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47", 994 | "$GNGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1", 995 | "$GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41", 996 | "$GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58", 997 | "$GPVTG,096.5,T,083.5,M,0.0,N,0.0,K,D*22", 998 | NULL, 999 | }; 1000 | 1001 | for (const char **sentence=sentences; *sentence; sentence++) { 1002 | switch (minmea_sentence_id(*sentence, false)) { 1003 | case MINMEA_SENTENCE_RMC: { 1004 | struct minmea_sentence_rmc frame; 1005 | ck_assert(minmea_parse_rmc(&frame, *sentence) == true); 1006 | } break; 1007 | 1008 | case MINMEA_SENTENCE_GGA: { 1009 | struct minmea_sentence_gga frame; 1010 | ck_assert(minmea_parse_gga(&frame, *sentence) == true); 1011 | } break; 1012 | 1013 | case MINMEA_SENTENCE_GSA: { 1014 | struct minmea_sentence_gsa frame; 1015 | ck_assert(minmea_parse_gsa(&frame, *sentence) == true); 1016 | } break; 1017 | 1018 | case MINMEA_SENTENCE_GLL: { 1019 | struct minmea_sentence_gll frame; 1020 | ck_assert(minmea_parse_gll(&frame, *sentence) == true); 1021 | } break; 1022 | 1023 | case MINMEA_SENTENCE_GST: { 1024 | struct minmea_sentence_gst frame; 1025 | ck_assert(minmea_parse_gst(&frame, *sentence) == true); 1026 | } break; 1027 | 1028 | case MINMEA_SENTENCE_VTG: { 1029 | struct minmea_sentence_vtg frame; 1030 | ck_assert(minmea_parse_vtg(&frame, *sentence) == true); 1031 | } break; 1032 | 1033 | default: { 1034 | } break; 1035 | } 1036 | 1037 | char talker[3]; 1038 | char expected[3] = { (*sentence)[1], (*sentence)[2], '\0' }; 1039 | ck_assert(minmea_talker_id(talker, *sentence) == true); 1040 | ck_assert_str_eq(talker, expected); 1041 | } 1042 | } 1043 | END_TEST 1044 | 1045 | START_TEST(test_minmea_gettime) 1046 | { 1047 | struct minmea_date d = { 14, 2, 14 }; 1048 | struct minmea_time t = { 13, 0, 9, 123456 }; 1049 | struct tm tm; 1050 | struct timespec ts; 1051 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1052 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1053 | ck_assert_int_eq(tm.tm_year, 2014-1900); 1054 | ck_assert_int_eq(tm.tm_mon, 1); 1055 | ck_assert_int_eq(tm.tm_mday, 14); 1056 | ck_assert_int_eq(tm.tm_hour, 13); 1057 | ck_assert_int_eq(tm.tm_min, 0); 1058 | ck_assert_int_eq(tm.tm_sec, 9); 1059 | ck_assert_int_eq(ts.tv_sec, 1392382809); 1060 | ck_assert_int_eq(ts.tv_nsec, 123456000); 1061 | 1062 | d.year = -1; 1063 | ck_assert(minmea_getdatetime(&tm, &d, &t) != 0); 1064 | ck_assert(minmea_gettime(&ts, &d, &t) != 0); 1065 | d.year = 14; 1066 | 1067 | t.hours = -1; 1068 | ck_assert(minmea_getdatetime(&tm, &d, &t) != 0); 1069 | ck_assert(minmea_gettime(&ts, &d, &t) != 0); 1070 | t.hours = 13; 1071 | 1072 | /* two digit year conversions */ 1073 | d.year = 80; 1074 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1075 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1076 | ck_assert_int_eq(tm.tm_year, 1980-1900); 1077 | ck_assert_int_eq(ts.tv_sec, 319381209); /* 1980 */ 1078 | d.year = 37; 1079 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1080 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1081 | ck_assert_int_eq(tm.tm_year, 2037-1900); 1082 | ck_assert_int_eq(ts.tv_sec, 2118229209); /* 2037 */ 1083 | /* skip >= 2038 tests on 32-bit time_t platforms */ 1084 | if (sizeof(time_t) == sizeof(int64_t)) { 1085 | d.year = 79; 1086 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1087 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1088 | ck_assert_int_eq(tm.tm_year, 2079-1900); 1089 | ck_assert_int_eq(ts.tv_sec, 3443605209); /* 2079 */ 1090 | } 1091 | 1092 | /* four digit year conversions */ 1093 | d.year = 1979; 1094 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1095 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1096 | ck_assert_int_eq(tm.tm_year, 1979-1900); 1097 | ck_assert_int_eq(ts.tv_sec, 287845209); 1098 | d.year = 1980; 1099 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1100 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1101 | ck_assert_int_eq(tm.tm_year, 1980-1900); 1102 | ck_assert_int_eq(ts.tv_sec, 319381209); 1103 | d.year = 2037; 1104 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1105 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1106 | ck_assert_int_eq(tm.tm_year, 2037-1900); 1107 | ck_assert_int_eq(ts.tv_sec, 2118229209); 1108 | /* skip >= 2038 tests on 32-bit time_t platforms */ 1109 | if (sizeof(time_t) == sizeof(int64_t)) { 1110 | d.year = 2079; 1111 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1112 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1113 | ck_assert_int_eq(tm.tm_year, 2079-1900); 1114 | ck_assert_int_eq(ts.tv_sec, 3443605209); 1115 | d.year = 2080; 1116 | ck_assert(minmea_getdatetime(&tm, &d, &t) == 0); 1117 | ck_assert(minmea_gettime(&ts, &d, &t) == 0); 1118 | ck_assert_int_eq(tm.tm_year, 2080-1900); 1119 | ck_assert_int_eq(ts.tv_sec, 3475141209); 1120 | } 1121 | } 1122 | END_TEST 1123 | 1124 | START_TEST(test_minmea_rescale) 1125 | { 1126 | /* basic and edge cases. */ 1127 | ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 42, 0 }, 3), 0); 1128 | ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1234, 10 }, 1), 123); 1129 | ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1235, 10 }, 1), 124); 1130 | ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 1234, 10 }, 1000), 123400); 1131 | 1132 | /* round towards zero. */ 1133 | ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1234, 10 }, 1), -123); 1134 | ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1235, 10 }, 1), -124); 1135 | ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { -1236, 10 }, 1), -124); 1136 | 1137 | /* shouldn't overflow on large numbers. */ 1138 | ck_assert_int_eq(minmea_rescale(&(struct minmea_float) { 510693608, 100000 }, 10000), 51069361); 1139 | } 1140 | END_TEST 1141 | 1142 | /* The float values used in tests should be exactly representable under IEEE754; 1143 | * false negatives will occur otherwise. */ 1144 | 1145 | #define assert_float_eq(x, y) ck_assert(fabsf((x) - (y)) <= 0.0f) 1146 | 1147 | START_TEST(test_minmea_float) 1148 | { 1149 | ck_assert(isnan(minmea_tofloat(&(struct minmea_float) { 42, 0 }))); 1150 | assert_float_eq(minmea_tofloat(&(struct minmea_float) { 7, 1 }), 7.0f); 1151 | assert_float_eq(minmea_tofloat(&(struct minmea_float) { -200, 100 }), -2.0f); 1152 | assert_float_eq(minmea_tofloat(&(struct minmea_float) { 15, 10 }), 1.5f); 1153 | } 1154 | END_TEST 1155 | 1156 | START_TEST(test_minmea_coord) 1157 | { 1158 | ck_assert(isnan(minmea_tocoord(&(struct minmea_float) { 42, 0 }))); 1159 | assert_float_eq(minmea_tocoord(&(struct minmea_float) { 4200, 1 }), 42.0f); 1160 | assert_float_eq(minmea_tocoord(&(struct minmea_float) { 420000, 100 }), 42.0f); 1161 | assert_float_eq(minmea_tocoord(&(struct minmea_float) { 423000, 100 }), 42.5f); 1162 | } 1163 | END_TEST 1164 | 1165 | static Suite *minmea_suite(void) 1166 | { 1167 | Suite *s = suite_create ("minmea"); 1168 | 1169 | TCase *tc_checksum = tcase_create("minmea_checksum"); 1170 | tcase_add_test(tc_checksum, test_minmea_checksum); 1171 | suite_add_tcase(s, tc_checksum); 1172 | 1173 | TCase *tc_check = tcase_create("minmea_check"); 1174 | tcase_add_test(tc_check, test_minmea_check); 1175 | suite_add_tcase(s, tc_check); 1176 | 1177 | TCase *tc_scan = tcase_create("minmea_scan"); 1178 | tcase_add_test(tc_scan, test_minmea_scan_c); 1179 | tcase_add_test(tc_scan, test_minmea_scan_d); 1180 | tcase_add_test(tc_scan, test_minmea_scan_f); 1181 | tcase_add_test(tc_scan, test_minmea_scan_i); 1182 | tcase_add_test(tc_scan, test_minmea_scan_s); 1183 | tcase_add_test(tc_scan, test_minmea_scan_t); 1184 | tcase_add_test(tc_scan, test_minmea_scan_t_str); 1185 | tcase_add_test(tc_scan, test_minmea_scan_D); 1186 | tcase_add_test(tc_scan, test_minmea_scan_T); 1187 | tcase_add_test(tc_scan, test_minmea_scan_complex1); 1188 | tcase_add_test(tc_scan, test_minmea_scan_complex2); 1189 | tcase_add_test(tc_scan, test_minmea_scan_complex3); 1190 | suite_add_tcase(s, tc_scan); 1191 | 1192 | TCase *tc_parse = tcase_create("minmea_parse"); 1193 | tcase_add_test(tc_parse, test_minmea_parse_gbs1); 1194 | tcase_add_test(tc_parse, test_minmea_parse_gbs2); 1195 | tcase_add_test(tc_parse, test_minmea_parse_rmc1); 1196 | tcase_add_test(tc_parse, test_minmea_parse_rmc2); 1197 | tcase_add_test(tc_parse, test_minmea_parse_gga1); 1198 | tcase_add_test(tc_parse, test_minmea_parse_gsa1); 1199 | tcase_add_test(tc_parse, test_minmea_parse_gll1); 1200 | tcase_add_test(tc_parse, test_minmea_parse_gll2); 1201 | tcase_add_test(tc_parse, test_minmea_parse_gst1); 1202 | tcase_add_test(tc_parse, test_minmea_parse_gsv1); 1203 | tcase_add_test(tc_parse, test_minmea_parse_gsv2); 1204 | tcase_add_test(tc_parse, test_minmea_parse_gsv3); 1205 | tcase_add_test(tc_parse, test_minmea_parse_gsv4); 1206 | tcase_add_test(tc_parse, test_minmea_parse_gsv5); 1207 | tcase_add_test(tc_parse, test_minmea_parse_vtg1); 1208 | tcase_add_test(tc_parse, test_minmea_parse_vtg2); 1209 | tcase_add_test(tc_parse, test_minmea_parse_vtg3); 1210 | tcase_add_test(tc_parse, test_minmea_parse_zda1); 1211 | suite_add_tcase(s, tc_parse); 1212 | 1213 | TCase *tc_usage = tcase_create("minmea_usage"); 1214 | tcase_add_test(tc_usage, test_minmea_usage1); 1215 | suite_add_tcase(s, tc_usage); 1216 | 1217 | TCase *tc_utils = tcase_create("minmea_utils"); 1218 | tcase_add_test(tc_utils, test_minmea_gettime); 1219 | tcase_add_test(tc_utils, test_minmea_rescale); 1220 | tcase_add_test(tc_utils, test_minmea_float); 1221 | tcase_add_test(tc_utils, test_minmea_coord); 1222 | suite_add_tcase(s, tc_utils); 1223 | 1224 | return s; 1225 | } 1226 | 1227 | int main(void) 1228 | { 1229 | int number_failed; 1230 | Suite *s = minmea_suite(); 1231 | SRunner *sr = srunner_create(s); 1232 | srunner_run_all(sr, CK_NORMAL); 1233 | number_failed = srunner_ntests_failed(sr); 1234 | srunner_free(sr); 1235 | return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; 1236 | } 1237 | 1238 | /* vim: set ts=4 sw=4 et: */ 1239 | --------------------------------------------------------------------------------