altos: Use FuDeviceLocker

This commit is contained in:
Richard Hughes 2017-09-07 10:16:44 +01:00
parent 5b4d7dce0a
commit e3e7a7b7d1

View File

@ -445,6 +445,7 @@ fu_device_altos_write_firmware (FuDeviceAltos *device,
const gsize data_len; const gsize data_len;
guint flash_len; guint flash_len;
g_autoptr(FuAltosFirmware) altos_firmware = NULL; g_autoptr(FuAltosFirmware) altos_firmware = NULL;
g_autoptr(FuDeviceLocker) locker = NULL;
g_autoptr(GString) buf = g_string_new (NULL); g_autoptr(GString) buf = g_string_new (NULL);
/* check kind */ /* check kind */
@ -505,7 +506,11 @@ fu_device_altos_write_firmware (FuDeviceAltos *device,
} }
/* open tty for download */ /* open tty for download */
if (!fu_device_altos_tty_open (device, error)) locker = fu_device_locker_new_full (device,
(FuDeviceLockerFunc) fu_device_altos_tty_open,
(FuDeviceLockerFunc) fu_device_altos_tty_close,
error);
if (locker == NULL)
return FALSE; return FALSE;
for (guint i = 0; i < flash_len; i+= 0x100) { for (guint i = 0; i < flash_len; i+= 0x100) {
g_autoptr(GString) str = NULL; g_autoptr(GString) str = NULL;
@ -574,10 +579,6 @@ fu_device_altos_write_firmware (FuDeviceAltos *device,
progress_data); progress_data);
} }
/* done */
if (!fu_device_altos_tty_close (device, error))
return FALSE;
/* success */ /* success */
return TRUE; return TRUE;
} }
@ -590,6 +591,7 @@ fu_device_altos_read_firmware (FuDeviceAltos *device,
{ {
FuDeviceAltosPrivate *priv = GET_PRIVATE (device); FuDeviceAltosPrivate *priv = GET_PRIVATE (device);
guint flash_len; guint flash_len;
g_autoptr(FuDeviceLocker) locker = NULL;
g_autoptr(GString) buf = g_string_new (NULL); g_autoptr(GString) buf = g_string_new (NULL);
/* check kind */ /* check kind */
@ -621,7 +623,11 @@ fu_device_altos_read_firmware (FuDeviceAltos *device,
} }
/* open tty for download */ /* open tty for download */
if (!fu_device_altos_tty_open (device, error)) locker = fu_device_locker_new_full (device,
(FuDeviceLockerFunc) fu_device_altos_tty_open,
(FuDeviceLockerFunc) fu_device_altos_tty_close,
error);
if (locker == NULL)
return NULL; return NULL;
for (guint i = priv->addr_base; i < priv->addr_bound; i+= 0x100) { for (guint i = priv->addr_base; i < priv->addr_bound; i+= 0x100) {
g_autoptr(GString) str = NULL; g_autoptr(GString) str = NULL;
@ -640,10 +646,6 @@ fu_device_altos_read_firmware (FuDeviceAltos *device,
g_string_append_len (buf, str->str, str->len); g_string_append_len (buf, str->str, str->len);
} }
/* done */
if (!fu_device_altos_tty_close (device, error))
return NULL;
/* success */ /* success */
return g_bytes_new (buf->str, buf->len); return g_bytes_new (buf->str, buf->len);
} }
@ -652,23 +654,26 @@ static gboolean
fu_device_altos_probe_bootloader (FuDeviceAltos *device, GError **error) fu_device_altos_probe_bootloader (FuDeviceAltos *device, GError **error)
{ {
FuDeviceAltosPrivate *priv = GET_PRIVATE (device); FuDeviceAltosPrivate *priv = GET_PRIVATE (device);
g_autoptr(FuDeviceLocker) locker = NULL;
g_auto(GStrv) lines = NULL; g_auto(GStrv) lines = NULL;
g_autoptr(GString) str = NULL; g_autoptr(GString) str = NULL;
/* get tty for upload */ /* get tty for upload */
if (!fu_device_altos_find_tty (device, error)) if (!fu_device_altos_find_tty (device, error))
return FALSE; return FALSE;
if (!fu_device_altos_tty_open (device, error)) locker = fu_device_locker_new_full (device,
(FuDeviceLockerFunc) fu_device_altos_tty_open,
(FuDeviceLockerFunc) fu_device_altos_tty_close,
error);
if (locker == NULL)
return FALSE; return FALSE;
/* get the version information */ /* get the version information */
if (!fu_device_altos_tty_write (device, "v\n", -1, error)) if (!fu_device_altos_tty_write (device, "v\n", -1, error))
return FALSE; return FALSE;
str = fu_device_altos_tty_read (device, 100, -1, error); str = fu_device_altos_tty_read (device, 100, -1, error);
if (str == NULL) { if (str == NULL)
fu_device_altos_tty_close (device, NULL);
return FALSE; return FALSE;
}
/* parse each line */ /* parse each line */
lines = g_strsplit_set (str->str, "\n\r", -1); lines = g_strsplit_set (str->str, "\n\r", -1);
@ -710,10 +715,6 @@ fu_device_altos_probe_bootloader (FuDeviceAltos *device, GError **error)
g_debug ("unknown data: '%s'", lines[i]); g_debug ("unknown data: '%s'", lines[i]);
} }
/* done */
if (!fu_device_altos_tty_close (device, error))
return FALSE;
return TRUE; return TRUE;
} }
@ -731,9 +732,11 @@ fu_device_altos_probe (FuDeviceAltos *device, GError **error)
const gchar *version_prefix = "ChaosKey-hw-1.0-sw-"; const gchar *version_prefix = "ChaosKey-hw-1.0-sw-";
guint8 version_idx; guint8 version_idx;
g_autofree gchar *version = NULL; g_autofree gchar *version = NULL;
g_autoptr(FuDeviceLocker) locker = NULL;
/* open */ /* open */
if (!g_usb_device_open (priv->usb_device, error)) locker = fu_device_locker_new (priv->usb_device, error);
if (locker == NULL)
return FALSE; return FALSE;
/* get string */ /* get string */
@ -741,22 +744,17 @@ fu_device_altos_probe (FuDeviceAltos *device, GError **error)
version = g_usb_device_get_string_descriptor (priv->usb_device, version = g_usb_device_get_string_descriptor (priv->usb_device,
version_idx, version_idx,
error); error);
if (version == NULL) { if (version == NULL)
g_usb_device_close (priv->usb_device, NULL);
return FALSE; return FALSE;
}
if (!g_str_has_prefix (version, version_prefix)) { if (!g_str_has_prefix (version, version_prefix)) {
g_set_error (error, g_set_error (error,
FWUPD_ERROR, FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED, FWUPD_ERROR_NOT_SUPPORTED,
"not a ChaosKey v1.0 device: %s", "not a ChaosKey v1.0 device: %s",
version); version);
g_usb_device_close (priv->usb_device, NULL);
return FALSE; return FALSE;
} }
fu_device_set_version (FU_DEVICE (device), version + 19); fu_device_set_version (FU_DEVICE (device), version + 19);
if (!g_usb_device_close (priv->usb_device, error))
return FALSE;
} }
/* success */ /* success */