diff options
author | Mark Rawling <mwr@google.com> | 2022-06-22 04:10:21 +0000 |
---|---|---|
committer | Android Partner Code Review <android-gerrit-partner@google.com> | 2022-06-22 04:10:21 +0000 |
commit | baf30deb40746ded81ac12523674897e674fef89 (patch) | |
tree | 4433e340ce0ef73350aeb870dd902b98d490a439 | |
parent | 4c349bf85fc5efd40bb589ca9805f01994db136a (diff) | |
parent | 0133804f3df613274a6c2620f89a119be04f04bc (diff) | |
download | nanohub-baf30deb40746ded81ac12523674897e674fef89.tar.gz |
Merge "Add fast, inexpensive static CRC scheme" into android-msm-eos-5.4
-rw-r--r-- | comms.c | 59 |
1 files changed, 45 insertions, 14 deletions
@@ -33,6 +33,15 @@ #define BUSY_DELAY_MAX_US 100000 #define BUSY_BACKOFF_RATE 2 +// CRC Schemes: +// CRC_CRC32 Expensive on CPU, battery and bandwidth, catches random bit flips. +// Good for hw validation, but not production. +// CRC_STATIC Fast, inexpensive, catches protocol errors but not bit flips. + +#define CRC_CRC32 +#undef CRC_STATIC +#define CRC_STATIC_VALUE 0x5A5A5A5A + static const uint32_t crc_table[] = { 0x00000000, 0x04C11DB7, 0x09823B6E, 0x0D4326D9, 0x130476DC, 0x17C56B6B, 0x1A864DB2, 0x1E475005, @@ -71,6 +80,40 @@ uint32_t crc32(const uint8_t *buffer, int length, uint32_t crc) return crc; } +void crc_create(struct nanohub_packet *packet) +{ + struct nanohub_packet_crc crc; +#if defined(CRC_CRC32) + crc.crc = crc32((uint8_t *)packet, + sizeof(struct nanohub_packet) + packet->len, ~0); +#elif defined(CRC_STATIC) + crc.crc = CRC_STATIC_VALUE; +#endif + memcpy(&packet->data[packet->len], &crc.crc, + sizeof(struct nanohub_packet_crc)); +} + +int crc_verify(struct nanohub_packet *packet) +{ + struct nanohub_packet_crc crc; + int cmp; + + // first check static crc + crc.crc = CRC_STATIC_VALUE; + cmp = memcmp(&crc.crc, &packet->data[packet->len], + sizeof(struct nanohub_packet_crc)); + if (cmp == 0) + return 0; + + // check computed crc + crc.crc = crc32((uint8_t *)packet, + sizeof(struct nanohub_packet) + packet->len, ~0); + cmp = memcmp(&crc.crc, &packet->data[packet->len], + sizeof(struct nanohub_packet_crc)); + + return cmp; +} + static inline size_t pad(size_t length) { return (length + 3) & ~3; @@ -96,7 +139,6 @@ static int packet_create(struct nanohub_packet *packet, uint32_t seq, uint32_t reason, uint8_t id, uint8_t len, const uint8_t *data, bool user) { - struct nanohub_packet_crc crc; int ret = sizeof(struct nanohub_packet) + len + sizeof(struct nanohub_packet_crc); @@ -115,11 +157,7 @@ static int packet_create(struct nanohub_packet *packet, uint32_t seq, memcpy(packet->data, data, len); } } - crc.crc = - crc32((uint8_t *) packet, - sizeof(struct nanohub_packet) + len, ~0); - memcpy(&packet->data[len], &crc.crc, - sizeof(struct nanohub_packet_crc)); + crc_create(packet); } else { ret = ERROR_NACK; } @@ -132,14 +170,7 @@ static int packet_verify(struct nanohub_packet *packet) struct nanohub_packet_crc crc; int cmp; - crc.crc = - crc32((uint8_t *) packet, - sizeof(struct nanohub_packet) + packet->len, ~0); - - cmp = - memcmp(&crc.crc, &packet->data[packet->len], - sizeof(struct nanohub_packet_crc)); - + cmp = crc_verify(packet); if (cmp != 0) { uint8_t *ptr = (uint8_t *)packet; pr_debug("nanohub: gen crc: %08x, got crc: %08x\n", crc.crc, |