Skip to content

Commit

Permalink
Change interface for agi::error_detection::crc8 and fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nickchan2 committed Mar 30, 2024
1 parent f2db043 commit b233ecb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 7 additions & 5 deletions error_detection/error_detection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@

#include "error_detection.h"

uint8_t agi::error_detection::crc8(const uint8_t* data, std::size_t length) {
#include <climits>

uint8_t agi::error_detection::crc8(std::span<uint8_t> data) {
constexpr uint8_t CRC8_POLYNOMIAL = 0x07; // CRC-8 Generator polynomial
constexpr uint8_t CRC8_MSB = (1U << 7U); // Most significat bit of byte

uint8_t crc = 0x00;

for (std::size_t i = 0; i < length; i++) {
crc ^= data[i];
for (int b = 0; b < 8; b++) {
for (const uint8_t val : data) {
crc ^= val;
for (int b = 0; b < CHAR_BIT; b++) {
if (crc & CRC8_MSB) {
crc = (crc << 1U) ^ CRC8_POLYNOMIAL;
crc = (crc << 1U) ^ CRC8_POLYNOMIAL; // NOLINT(*-signed-bitwise)
} else {
crc <<= 1U;
}
Expand Down
7 changes: 4 additions & 3 deletions error_detection/error_detection.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

#pragma once

#include <cstdint>
#include <cstddef>
#include <cstdint>
#include <span>

namespace agi::error_detection {

/**
* @brief Compute 8 bit checksum on data buffer using polynomial division based CRC-8 algorithm
*
* @param data, length
* @param data
* @return 8 bit CRC checksum
*/
uint8_t crc8(const uint8_t* data, std::size_t length);
uint8_t crc8(std::span<uint8_t> data);

} // namespace agi::error_detection

0 comments on commit b233ecb

Please sign in to comment.