From f5df7f0ec9cc0bd12cbafd307e280e855348ffb4 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Mon, 24 Aug 2015 13:36:56 +1000 Subject: [PATCH] hidpp10: split the commands into helper functions Signed-off-by: Peter Hutterer --- src/hidpp10.c | 87 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/src/hidpp10.c b/src/hidpp10.c index 453a4619..ea0490da 100644 --- a/src/hidpp10.c +++ b/src/hidpp10.c @@ -228,32 +228,40 @@ void hidpp10_list_devices(struct ratbag_device *device) { } static int -hidpp10_get_device_info(struct ratbag_device *device, struct hidpp10_device *dev) +hidpp10_get_pairing_information(struct ratbag_device *device, struct hidpp10_device *dev) { - unsigned idx = dev->index; + unsigned int idx = dev->index; union hidpp10_message pairing_information = CMD_PAIRING_INFORMATION(idx, DEVICE_PAIRING_INFORMATION); union hidpp10_message device_name = CMD_PAIRING_INFORMATION(idx, DEVICE_NAME); - union hidpp10_message firmware_information = CMD_DEVICE_FIRMWARE_INFORMATION(idx, FIRMWARE_INFO_ITEM_FW_NAME_AND_VERSION(1)); - union hidpp10_message build_information = CMD_DEVICE_FIRMWARE_INFORMATION(idx, FIRMWARE_INFO_ITEM_FW_BUILD_NUMBER(1)); - union hidpp10_message notifications = CMD_HIDPP_NOTIFICATIONS(idx, GET_REGISTER_REQ); - union hidpp10_message features = CMD_ENABLE_INDIVIDUAL_FEATURES(idx, GET_REGISTER_REQ); size_t name_size; int res; res = hidpp10_request_command(device, &pairing_information); - if (res == 0) { - dev->report_interval = pairing_information.msg.string[2]; - dev->wpid = (pairing_information.msg.string[3] << 8) | - pairing_information.msg.string[4]; - dev->device_type = pairing_information.msg.string[7]; + if (res) + return -1; - res = hidpp10_request_command(device, &device_name); - if (res) - return -1; - name_size = device_name.msg.string[1]; - memcpy(dev->name, &device_name.msg.string[2], sizeof(device_name.msg.string)); - dev->name[min(name_size, sizeof(dev->name) - 1)] = '\0'; - } + dev->report_interval = pairing_information.msg.string[2]; + dev->wpid = (pairing_information.msg.string[3] << 8) | + pairing_information.msg.string[4]; + dev->device_type = pairing_information.msg.string[7]; + + res = hidpp10_request_command(device, &device_name); + if (res) + return -1; + name_size = device_name.msg.string[1]; + memcpy(dev->name, &device_name.msg.string[2], sizeof(device_name.msg.string)); + dev->name[min(name_size, sizeof(dev->name) - 1)] = '\0'; + + return 0; +} + +static int +hidpp10_get_firmare_information(struct ratbag_device *device, struct hidpp10_device *dev) +{ + unsigned idx = dev->index; + union hidpp10_message firmware_information = CMD_DEVICE_FIRMWARE_INFORMATION(idx, FIRMWARE_INFO_ITEM_FW_NAME_AND_VERSION(1)); + union hidpp10_message build_information = CMD_DEVICE_FIRMWARE_INFORMATION(idx, FIRMWARE_INFO_ITEM_FW_BUILD_NUMBER(1)); + int res; /* * This may fail on some devices @@ -271,13 +279,46 @@ hidpp10_get_device_info(struct ratbag_device *device, struct hidpp10_device *dev build_information.msg.string[2]; } - res = hidpp10_request_command(device, ¬ifications); - if (res) - return 0; + return 0; +} + +static int +hidpp10_get_individual_features(struct ratbag_device *device, struct hidpp10_device *dev) +{ + unsigned idx = dev->index; + union hidpp10_message features = CMD_ENABLE_INDIVIDUAL_FEATURES(idx, GET_REGISTER_REQ); + int res; res = hidpp10_request_command(device, &features); - if (res) - return 0; + if (res == 0) { + /* do something */ + } + + return res; +} + +static int +hidpp10_get_hidpp_notifications(struct ratbag_device *device, struct hidpp10_device *dev) +{ + unsigned idx = dev->index; + union hidpp10_message notifications = CMD_HIDPP_NOTIFICATIONS(idx, GET_REGISTER_REQ); + int res; + + res = hidpp10_request_command(device, ¬ifications); + if (res == 0) { + /* do something */ + } + + return res; +} + +static int +hidpp10_get_device_info(struct ratbag_device *device, struct hidpp10_device *dev) +{ + hidpp10_get_pairing_information(device, dev); + hidpp10_get_firmare_information(device, dev); + hidpp10_get_individual_features(device, dev); + hidpp10_get_hidpp_notifications(device, dev); return 0; }