forked from dangowrt/tracertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer_crc16.h
61 lines (48 loc) · 1001 Bytes
/
tracer_crc16.h
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
52
53
54
55
56
57
58
59
60
61
/*
* A direct copy of the CRC function in the documentation
* Protocol-Tracer-MT-5.pdf (PROTOCOL OF TRACER SEREIS AND MT-5 Ver 3)
* found on https://github.com/xxv/tracer/blob/master/c/tracer_crc.c
* modified by Daniel Golle <[email protected]>
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <sys/time.h>
#include <sys/types.h>
uint16_t tracer_crc16(uint8_t *buf, uint16_t len) {
uint8_t crc_i, crc_j, r1, r2, r3, r4;
uint16_t crc;
if (len < 2)
return 0xdead;
r1 = *buf;
buf++;
r2 = *buf;
buf++;
for (crc_i = 0; crc_i < len - 2; crc_i++) {
r3 = *buf;
buf++;
for (crc_j=0; crc_j < 8; crc_j++) {
r4 = r1;
r1 = (r1<<1);
if ((r2 & 0x80) != 0) {
r1++;
}
r2 = r2<<1;
if((r3 & 0x80) != 0) {
r2++;
}
r3 = r3<<1;
if ((r4 & 0x80) != 0) {
r1 = r1^0x10;
r2 = r2^0x41;
}
}
}
crc = r1;
crc = crc << 8 | r2;
return(crc);
}