From ce0c3c9d206b67ce423452a97afdf9096fdb376e Mon Sep 17 00:00:00 2001 From: Benjamin Tissoires Date: Thu, 10 Sep 2015 18:39:27 -0400 Subject: [PATCH] roccat: break out the crc check Signed-off-by: Benjamin Tissoires --- src/driver-roccat.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/driver-roccat.c b/src/driver-roccat.c index dd121b56..28ff662f 100644 --- a/src/driver-roccat.c +++ b/src/driver-roccat.c @@ -214,18 +214,50 @@ roccat_button_action_to_raw(const struct ratbag_button_action *action) return 0; } -static inline uint16_t roccat_crc(uint8_t *buf, unsigned int len) +static inline uint16_t +roccat_get_unaligned_u16(uint8_t *buf) +{ + return (buf[1] << 8) | buf[0]; +} + +static inline uint16_t +roccat_compute_crc(uint8_t *buf, unsigned int len) { unsigned i; uint16_t crc = 0; - for (i = 0; i < len; i++) { + if (len < 3) + return 0; + + for (i = 0; i < len - 2; i++) { crc += buf[i]; } return crc; } +static inline int +roccat_crc_is_valid(struct ratbag_device *device, uint8_t *buf, unsigned int len) +{ + unsigned i; + uint16_t crc; + uint16_t given_crc; + + if (len < 3) + return 0; + + crc = roccat_compute_crc(buf, len); + + given_crc = roccat_get_unaligned_u16(&buf[len - 2]); + + log_raw(device->ratbag, + "checksum computed: 0x%04x, checksum given: 0x%04x\n", + crc, + given_crc); + + return crc == given_crc; +} + static int roccat_has_capability(const struct ratbag_device *device, enum ratbag_device_capability cap) @@ -567,9 +599,7 @@ roccat_read_button(struct ratbag_button *button) button->index, button->profile->index, rc < 0 ? strerror(-rc) : "not read enough", rc); } else { - uint16_t crc = roccat_crc(buf, ROCCAT_REPORT_SIZE_MACRO - 2); - - if (crc != macro->checksum) { + if (!roccat_crc_is_valid(device, buf, ROCCAT_REPORT_SIZE_MACRO)) { log_error(device->ratbag, "wrong checksum while reading the macro of button %d of profile %d.\n", button->index,