From c6b9cc0c2af2931ffe14bc007138b84d69d2b586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Fri, 5 Feb 2021 22:50:54 +0000 Subject: [PATCH] openinput: add error support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe LaĆ­ns --- src/driver-openinput.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/driver-openinput.c b/src/driver-openinput.c index fa39d804..7ca2241a 100644 --- a/src/driver-openinput.c +++ b/src/driver-openinput.c @@ -33,6 +33,7 @@ #define OI_REPORT_LONG_SIZE 32 #define OI_REPORT_MAX_SIZE OI_REPORT_LONG_SIZE +#define OI_REPORT_DATA_INDEX 3 /* protocol function pages */ #define OI_PAGE_INFO 0x00 @@ -44,6 +45,11 @@ #define OI_FUNCTION_VERSION 0x00 #define OI_FUNCTION_FW_INFO 0x01 +/* error page (0xFF) */ +#define OI_ERROR_INVALID_VALUE 0x01 +#define OI_ERROR_UNSUPPORTED_FUNCTION 0x02 +#define OI_ERROR_CUSTOM 0xFE + static unsigned int report_rates[] = { 125, 250, 500, 750, 1000 }; @@ -64,6 +70,31 @@ struct oi_report_t { uint8_t data[29]; } __attribute__((__packed__)); +static const char* +openinput_get_error_string(struct oi_report_t *report) +{ + char help_str[OI_REPORT_LONG_SIZE - OI_REPORT_DATA_INDEX + 1] = {0}; + char *str; + + switch (report->function) { + case OI_ERROR_INVALID_VALUE: + xasprintf(&str, "Invalid value (in position %u)", report->data[2]); + break; + case OI_ERROR_CUSTOM: + memcpy(help_str, report->data, sizeof(help_str)-1); /* copy error string */ + xasprintf(&str, "Custom error (%s)", help_str); + break; + case OI_ERROR_UNSUPPORTED_FUNCTION: + xasprintf(&str, "Unsupported function (0x%02x, 0x%02x)", report->data[0], report->data[1]); + break; + default: + xasprintf(&str, "Unkown error (%u)", report->function); + break; + } + + return str; +} + static size_t openinput_get_report_size(unsigned int report) { @@ -102,7 +133,11 @@ openinput_send_report(struct ratbag_device *device, struct oi_report_t *report) memcpy(report, buffer, openinput_get_report_size(buffer[0])); - // TODO: check for error + /* check for error */ + if (report->function_page == OI_PAGE_ERROR) { + log_error(device->ratbag, "openinput: %s\n", openinput_get_error_string(report)); + return report->function; + } return 0; }