diff options
author | Richard Hughes <richard@hughsie.com> | 2014-09-11 22:30:59 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2014-09-12 15:40:45 +0100 |
commit | f27c9ecf2f2f98e49537e3386cc11478fa80ab55 (patch) | |
tree | 45d7f84aeb32fce5ca3cd98b5184b225bf689963 | |
parent | f24373dc3326124560062eb08a3a627fb6b35298 (diff) | |
download | colord-f27c9ecf2f2f98e49537e3386cc11478fa80ab55.tar.gz |
colorhug: Add ch_device_check_firmware()
This returns an error if the firmware binary data is invalid.
-rw-r--r-- | lib/colorhug/ch-device.c | 68 | ||||
-rw-r--r-- | lib/colorhug/ch-device.h | 5 |
2 files changed, 73 insertions, 0 deletions
diff --git a/lib/colorhug/ch-device.c b/lib/colorhug/ch-device.c index b5b17b7..6c9ea78 100644 --- a/lib/colorhug/ch-device.c +++ b/lib/colorhug/ch-device.c @@ -513,3 +513,71 @@ ch_device_write_command (GUsbDevice *device, return helper.ret; } + +/** + * ch_device_check_firmware: + * @data: firmware binary data + * @data_len: size of @data + * + * Checks the firmware is suitable for the ColorHug device that is attached. + * + * Return value: %TRUE if the command was executed successfully. + * + * Since: 1.2.3 + **/ +gboolean +ch_device_check_firmware (GUsbDevice *device, + const guint8 *data, + gsize data_len, + GError **error) +{ + ChDeviceMode device_mode_fw; + + /* this is only a heuristic */ + device_mode_fw = ch_device_mode_from_firmware (data, data_len); + switch (ch_device_get_mode (device)) { + case CH_DEVICE_MODE_LEGACY: + case CH_DEVICE_MODE_BOOTLOADER: + case CH_DEVICE_MODE_FIRMWARE: + /* fw versions < 1.2.2 has no magic bytes */ + if (device_mode_fw == CH_DEVICE_MODE_FIRMWARE2 || + device_mode_fw == CH_DEVICE_MODE_FIRMWARE_PLUS) { + g_set_error (error, + CH_DEVICE_ERROR, + CH_ERROR_INVALID_VALUE, + "This firmware is not designed for " + "ColorHug (identifier is '%s')", + ch_device_mode_to_string (device_mode_fw)); + return FALSE; + } + break; + case CH_DEVICE_MODE_BOOTLOADER2: + case CH_DEVICE_MODE_FIRMWARE2: + if (device_mode_fw != CH_DEVICE_MODE_FIRMWARE2) { + g_set_error (error, + CH_DEVICE_ERROR, + CH_ERROR_INVALID_VALUE, + "This firmware is not designed for " + "ColorHug2 (identifier is '%s')", + ch_device_mode_to_string (device_mode_fw)); + return FALSE; + } + break; + case CH_DEVICE_MODE_BOOTLOADER_PLUS: + case CH_DEVICE_MODE_FIRMWARE_PLUS: + if (device_mode_fw != CH_DEVICE_MODE_FIRMWARE_PLUS) { + g_set_error (error, + CH_DEVICE_ERROR, + CH_ERROR_INVALID_VALUE, + "This firmware is not designed for " + "ColorHug+ (identifier is '%s')", + ch_device_mode_to_string (device_mode_fw)); + return FALSE; + } + break; + default: + g_assert_not_reached (); + break; + } + return TRUE; +} diff --git a/lib/colorhug/ch-device.h b/lib/colorhug/ch-device.h index 76040bf..604fd00 100644 --- a/lib/colorhug/ch-device.h +++ b/lib/colorhug/ch-device.h @@ -63,6 +63,11 @@ gboolean ch_device_write_command (GUsbDevice *device, GCancellable *cancellable, GError **error) G_GNUC_WARN_UNUSED_RESULT; +gboolean ch_device_check_firmware (GUsbDevice *device, + const guint8 *data, + gsize data_len, + GError **error) + G_GNUC_WARN_UNUSED_RESULT; G_END_DECLS |