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 29, 2024
1 parent f2db043 commit 6fb84a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
18 changes: 12 additions & 6 deletions error_detection/error_detection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@

#include "error_detection.h"

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

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++) {
if (crc & CRC8_MSB) {
crc = (crc << 1U) ^ CRC8_POLYNOMIAL;
for (auto const val : data) {
crc ^= val;
constexpr int BITS_PER_BYTE = 8;
for (int b = 0; b < BITS_PER_BYTE; b++) {
if (static_cast<bool>(crc & CRC8_MSB)) {
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 6fb84a9

Please sign in to comment.