.. _program_listing_file_libspookyaction_include_pn532_checksum.hpp: Program Listing for File checksum.hpp ===================================== |exhale_lsh| :ref:`Return to documentation for file ` (``libspookyaction/include/pn532/checksum.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // // Created by Pietro Saccardi on 22/12/2020. // #ifndef PN532_BITS_ALGO_HPP #define PN532_BITS_ALGO_HPP #include #include #include #include namespace pn532 { [[nodiscard]] inline std::uint8_t compute_checksum(std::uint8_t byte); template [[nodiscard]] std::uint8_t compute_checksum(ByteIterator begin, ByteIterator end); template [[nodiscard]] std::uint8_t compute_checksum(std::uint8_t sum_init, ByteIterator begin, ByteIterator end); template [[nodiscard]] bool checksum(ByteIterator begin, ByteIterator end); #ifndef DOXYGEN_SHOULD_SKIP_THIS namespace bits { [[nodiscard]] inline std::array length_and_checksum_short(std::uint8_t length); [[nodiscard]] inline std::array length_and_checksum_long(std::uint16_t length); [[nodiscard]] inline std::pair check_length_checksum(std::array const &data); [[nodiscard]] inline std::pair check_length_checksum(std::array const &data); [[nodiscard]] inline std::uint8_t host_to_pn532_command(command_code cmd); [[nodiscard]] inline command_code pn532_to_host_command(std::uint8_t cmd); }// namespace bits #endif }// namespace pn532 namespace pn532 { std::uint8_t compute_checksum(std::uint8_t byte) { return ~byte + 1; } template std::uint8_t compute_checksum(ByteIterator begin, ByteIterator end) { return compute_checksum(0, begin, end); } template std::uint8_t compute_checksum(std::uint8_t sum_init, ByteIterator begin, ByteIterator end) { return compute_checksum(std::accumulate(begin, end, sum_init)); } template bool checksum(ByteIterator begin, ByteIterator end) { return (std::accumulate(begin, end, 0) & 0xff) == 0; } namespace bits { std::array length_and_checksum_short(std::uint8_t length) { return {length, compute_checksum(length)}; } std::array length_and_checksum_long(std::uint16_t length) { const std::array bits = {std::uint8_t(length >> 8), std::uint8_t(length & 0xff)}; return {bits[0], bits[1], compute_checksum(std::begin(bits), std::end(bits))}; } std::pair check_length_checksum(std::array const &data) { return {data[0], checksum(std::begin(data), std::end(data))}; } std::pair check_length_checksum(std::array const &data) { return {(std::uint16_t(data[0]) << 8) | data[1], checksum(std::begin(data), std::end(data))}; } std::uint8_t host_to_pn532_command(command_code cmd) { return static_cast(cmd); } command_code pn532_to_host_command(std::uint8_t cmd) { return static_cast(cmd - 1); } }// namespace bits }// namespace pn532 #endif//PN532_BITS_ALGO_HPP