summaryrefslogtreecommitdiff
path: root/driver_greis_checksum.c
blob: e7f6951888cf93e13aa117c01ab465192533d3ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 * Checksum for the GNSS Receiver External Interface Specification (GREIS).
 *
 * This file is Copyright (c) 2017 Virgin Orbit
 * SPDX-License-Identifier: BSD-2-clause
 */

#include <limits.h>

#include "driver_greis.h"

static inline unsigned char greis_rotate_left(unsigned char val)
{
    /* left circular rotation by two bits */
    return (val << 2) | (val >> (CHAR_BIT - 2));
}

unsigned char greis_checksum(const unsigned char *src, int count)
{
    unsigned char res = 0;
    while (count--)
	res = greis_rotate_left(res) ^ *src++;
    return greis_rotate_left(res);
}