diff --git a/components/brmesh/network.cpp b/components/brmesh/network.cpp index 68f7c26..a521317 100644 --- a/components/brmesh/network.cpp +++ b/components/brmesh/network.cpp @@ -65,29 +65,15 @@ uint8_t crc(const std::span &bytes, uint8_t init = 0x00) { return init; } -uint16_t crc16(uint8_t byte, uint16_t crc) { - crc ^= static_cast(byte) << 8; - for (int j=0; j<4; ++j) { - uint16_t tmp = crc << 1; - if (crc & 0x8000) - tmp ^= 0x1021; - crc = tmp << 1; - if (tmp & 0x8000) - crc ^= 0x1021; - } - return crc; -} - -uint16_t crc16(const std::array &addr, const std::span &data, const uint16_t seed = 0xFFFF) { - auto crc = seed; - - for (auto it = addr.rbegin(); it != addr.rend(); ++it) - crc = crc16(*it, crc); - - for (const auto &byte:data) - crc = crc16(reverse_bits(byte), crc); - - return ~reverse_bits(crc); +uint16_t crc16(const std::span &data, const uint16_t seed = 0xFFFF) { + return ~esphome::crc16( + data.data(), + data.size(), + seed, + 0x1021, + true, // refin + true // refout + ); } std::span inner(const std::span &buffer, Light::Id id, const std::span &light_data) { @@ -158,7 +144,7 @@ std::span command(const std::span &buffer, const Key &key, Lig } std::span payload(const std::span &buffer, const Key &key, Light::Id id, uint8_t seq, const std::span &light_data) { - constexpr std::array ADDRESS = {0xC1, 0xC2, 0xC3}; + constexpr std::array ADDRESS = { 0xC3, 0x43, 0x83 }; constexpr std::array PREFIX = { 0x71, 0x0F, 0x55 }; const auto prefix = subspan(buffer, 0, PREFIX.size()); @@ -188,10 +174,9 @@ std::span payload(const std::span &buffer, const Key &key, Lig std::copy(PREFIX.cbegin(), PREFIX.cend(), prefix.begin()); // copy - std::reverse_copy(ADDRESS.cbegin(), ADDRESS.cend(), address.begin()); + std::copy(ADDRESS.cbegin(), ADDRESS.cend(), address.begin()); - - uint16_t crc_value = crc16(ADDRESS, command); + uint16_t crc_value = crc16(buffer.subspan(prefix.size(), address.size() + command.size())); crc[0] = crc_value; crc[1] = crc_value >> 8; @@ -200,8 +185,6 @@ std::span payload(const std::span &buffer, const Key &key, Lig for (auto &byte:prefix) byte = reverse_bits(byte); - for (auto &byte:address) - byte = reverse_bits(byte); //ESP_LOGV(TAG, "%c%02hhX: reverse: %s", id.kind(), id.value(), // format_hex_pretty(&result[0], result.size()).c_str());