nmea-chk.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Library: libcrc
  3. * File: src/nmea-chk.c
  4. * Author: Lammert Bies
  5. *
  6. * This file is licensed under the MIT License as stated below
  7. *
  8. * Copyright (c) 2008-2016 Lammert Bies
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. *
  28. * Description
  29. * -----------
  30. * The source file src/nmea-chk.c contains routines to calculate the checksum
  31. * in NMEA messages.
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include "checksum.h"
  36. /*
  37. * unsigned char *checksum_NMEA( const unsigned char *input_str, unsigned char *result );
  38. *
  39. * The function checksum_NMEA() calculates the checksum of a valid NMEA string.
  40. * The routine does not try to validate the string itself. A leading '$' will
  41. * be ignored, as this character is part of the NMEA sentence, but not part of
  42. * the checksum calculation. The calculation stops, whenever a linefeed,
  43. * carriage return, '*' or end of string is scanned.
  44. *
  45. * Because there is no NMEA syntax checking involved, the function always
  46. * returns with succes, unless a NULL pointer is provided as parameter. The
  47. * return value is a pointer to the result buffer provided by the calling
  48. * application, or NULL in case of error.
  49. *
  50. * The result buffer must be at least three characters long. Two for the
  51. * checksum value and the third to store the EOS. The result buffer is not
  52. * filled when an error occurs.
  53. */
  54. unsigned char * checksum_NMEA( const unsigned char *input_str, unsigned char *result ) {
  55. const unsigned char *ptr;
  56. unsigned char checksum;
  57. if ( input_str == NULL ) return NULL;
  58. if ( result == NULL ) return NULL;
  59. checksum = 0;
  60. ptr = (const unsigned char *) input_str;
  61. if ( *ptr == '$' ) ptr++;
  62. while ( *ptr && *ptr != '\r' && *ptr != '\n' && *ptr != '*' ) checksum ^= *ptr++;
  63. snprintf( (char *) result, 3, "%02X", checksum );
  64. return result;
  65. } /* checksum_NMEA */