-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathchecksum.cpp
More file actions
48 lines (45 loc) · 1.17 KB
/
Copy pathchecksum.cpp
File metadata and controls
48 lines (45 loc) · 1.17 KB
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
#include "checksum.h"
namespace esphome {
namespace uartex {
uint16_t compute_checksum(CHECKSUM checksum, const std::vector<uint8_t> &header, const std::vector<uint8_t> &data)
{
uint16_t crc = 0;
uint8_t temp = 0;
switch (checksum)
{
case CHECKSUM_XOR:
for (uint8_t byte : header) { crc ^= byte; }
for (uint8_t byte : data) { crc ^= byte; }
break;
case CHECKSUM_ADD:
for (uint8_t byte : header) { crc += byte; }
for (uint8_t byte : data) { crc += byte; }
break;
case CHECKSUM_XOR_NO_HEADER:
for (uint8_t byte : data) { crc ^= byte; }
break;
case CHECKSUM_ADD_NO_HEADER:
for (uint8_t byte : data) { crc += byte; }
break;
case CHECKSUM_XOR_ADD:
for (uint8_t byte : header)
{
crc += byte;
temp ^= byte;
}
for (uint8_t byte : data)
{
crc += byte;
temp ^= byte;
}
crc += temp;
crc = ((uint16_t) temp << 8) | (crc & 0xFF);
break;
case CHECKSUM_NONE:
case CHECKSUM_CUSTOM:
break;
}
return crc;
}
} // namespace uartex
} // namespace esphome