driver_greis_checksum.c 646 B

123456789101112131415161718192021222324252627
  1. /*
  2. * Checksum for the GNSS Receiver External Interface Specification (GREIS).
  3. *
  4. * This file is Copyright (c) 2017-2018 Virgin Orbit
  5. * SPDX-License-Identifier: BSD-2-clause
  6. */
  7. #include "gpsd_config.h" /* must be before all includes */
  8. #include <limits.h>
  9. #include "driver_greis.h"
  10. static inline unsigned char greis_rotate_left(unsigned char val)
  11. {
  12. /* left circular rotation by two bits */
  13. return (val << 2) | (val >> (CHAR_BIT - 2));
  14. }
  15. unsigned char greis_checksum(const unsigned char *src, int count)
  16. {
  17. unsigned char res = 0;
  18. while (count--)
  19. res = greis_rotate_left(res) ^ *src++;
  20. return greis_rotate_left(res);
  21. }