From 132fc2121259164ad91ccc8a24f438c0cfc7d3b4 Mon Sep 17 00:00:00 2001 From: Yaroslav Chvanov Date: Sat, 29 Jan 2022 19:55:22 +0300 Subject: [PATCH] sinowealth: sanity check output of all raw data getters Well, of almost all, config getter's output is not checked as it may have seemingly incorrect data in some cases. Potentially some mice may not support some commands but look like they do. You can see this guard in action if you run `sinowealth_print_long_lod_and_anglesnapping` after any other command that sets 6 byte long buffer on non-long mice. --- src/driver-sinowealth.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/driver-sinowealth.c b/src/driver-sinowealth.c index 23ccd736..48ba9308 100644 --- a/src/driver-sinowealth.c +++ b/src/driver-sinowealth.c @@ -431,6 +431,12 @@ sinowealth_get_active_profile(struct ratbag_device *device) return -1; } + /* Check if we got a response for correct command. */ + if (buf[1] != SINOWEALTH_CMD_PROFILE) { + log_error(device->ratbag, "Couldn't read profile index, got result of command %#x instead\n", buf[1]); + return -1; + } + unsigned int index = buf[2] - 1; drv_data->current_profile_index = index; @@ -484,6 +490,12 @@ sinowealth_get_fw_version(struct ratbag_device *device, char buf[4]) return -1; } + /* Check if we got a response for correct command. */ + if (version[1] != SINOWEALTH_CMD_FIRMWARE_VERSION) { + log_error(device->ratbag, "Couldn't read firmware version, got result of command %#x instead\n", version[1]); + return -1; + } + memcpy(buf, version + 2, 4); return 0; @@ -510,6 +522,12 @@ sinowealth_get_debounce_time(struct ratbag_device *device) return -1; } + /* Check if we got a response for correct command. */ + if (buf[1] != SINOWEALTH_CMD_DEBOUNCE) { + log_error(device->ratbag, "Couldn't read debounce time, got result of command %#x instead\n", buf[1]); + return -1; + } + return buf[2] * 2; } @@ -538,6 +556,13 @@ sinowealth_print_long_lod_and_anglesnapping(struct ratbag_device *device) log_error(device->ratbag, "Couldn't read LOD and angle snapping: %d\n", rc); return -1; } + + /* Check if we got a response for correct command. */ + if (buf[1] != SINOWEALTH_CMD_LONG_ANGLESNAPPING_AND_LOD) { + log_error(device->ratbag, "Couldn't read LOD and angle snapping, got result of command %#x instead\n", buf[1]); + return -1; + } + log_info(device->ratbag, "LOD is high: %u\n", buf[2] - 1); log_info(device->ratbag, "Angle snapping enabled: %u\n", buf[3]);