-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmea-cksum.c
51 lines (40 loc) · 863 Bytes
/
nmea-cksum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NMEA_EPILOGUE_LENGTH 5 /* -> strlen("*xx\r\n") */
int main(int ac, char **av)
{
uint8_t cksum = 0;
char *s;
char *res;
size_t cur = 0;
uint8_t nodollar = 1;
if (ac < 2 || strlen(av[1]) == 0) {
fprintf(stderr, "usage: %s NMEA_STRING\nexiting...", av[0]);
return EXIT_FAILURE;
}
s = av[1];
if (s[cur] == '$') {
cur++; /* discards the '$' */
nodollar = 0;
}
for (; s[cur]; cur++) {
if (s[cur] == '*') {
s[cur] = '\0';
break;
}
cksum ^= s[cur];
}
res = (char *) malloc((cur + 1 + NMEA_EPILOGUE_LENGTH) * sizeof(char));
if (!res) {
fprintf(stderr, "memory exhausted\nexiting...\n");
return EXIT_FAILURE;
}
sprintf(res, "%s*%02X", s, cksum);
if (nodollar)
putchar('$');
printf("%s\n", res);
free(res);
return EXIT_SUCCESS;
}