/** * Adds size to the beginning of a packet and a crc32 at the end. Then send the packet. */ void tunerStudioWriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, const void *buf, const uint16_t size) { uint8_t *writeBuffer = tsChannel->writeBuffer; *(uint16_t *) writeBuffer = SWAP_UINT16(size + 1); // packet size including command *(uint8_t *) (writeBuffer + 2) = responseCode; tunerStudioWriteData(tsChannel, writeBuffer, 3); // header // CRC on whole packet uint32_t crc = crc32((void *) (writeBuffer + 2), 1); // command part of CRC crc = crc32inc((void *) buf, crc, (uint32_t) (size)); // combined with packet CRC *(uint32_t *) (writeBuffer) = SWAP_UINT32(crc); if (size > 0) { tunerStudioWriteData(tsChannel, (const uint8_t*)buf, size); // body } tunerStudioWriteData(tsChannel, writeBuffer, 4); // CRC footer }
TEST(util, crc) { ASSERT_EQ(4, efiRound(4.4, 1)); ASSERT_FLOAT_EQ(1.2, efiRound(1.2345, 0.1)); print("*************************************** testCrc\r\n"); const char * A = "A"; ASSERT_EQ( 168, calc_crc((const crc_t *) A, 1)) << "crc8"; uint32_t c = crc32(A, 1); printf("crc32(A)=%x\r\n", c); assertEqualsM("crc32 1", 0xd3d99e8b, c); const char * line = "AbcDEFGF"; c = crc32(line, 8); printf("crc32(line)=%x\r\n", c); assertEqualsM("crc32 line", 0x4775a7b1, c); c = crc32(line, 1); c = crc32inc(line + 1, c, 8 - 1); assertEqualsM("crc32 line inc", 0x4775a7b1, c); }