diff --git a/libdfu/dfu-context.c b/libdfu/dfu-context.c index d8d460f01..80b4633be 100644 --- a/libdfu/dfu-context.c +++ b/libdfu/dfu-context.c @@ -509,6 +509,46 @@ dfu_context_get_device_by_vid_pid (DfuContext *context, return g_object_ref (device); } +/** + * dfu_context_get_device_by_platform_id: + * @context: a #DfuContext + * @platform_id: a platform ID + * @error: a #GError, or %NULL + * + * Finds a device in the context with a specific platform ID. + * + * Return value: (transfer full): a #DfuDevice for success, or %NULL for an error + * + * Since: 0.5.4 + **/ +DfuDevice * +dfu_context_get_device_by_platform_id (DfuContext *context, + const gchar *platform_id, + GError **error) +{ + DfuContextPrivate *priv = GET_PRIVATE (context); + DfuContextItem *item; + guint i; + + g_return_val_if_fail (DFU_IS_CONTEXT (context), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + /* search all devices */ + for (i = 0; i < priv->devices->len; i++) { + item = g_ptr_array_index (priv->devices, i); + if (g_strcmp0 (dfu_device_get_platform_id (item->device), + platform_id) == 0) { + return g_object_ref (item->device); + } + } + g_set_error (error, + DFU_ERROR, + DFU_ERROR_NOT_FOUND, + "no device matches for %s", + platform_id); + return NULL; +} + /** * dfu_context_get_device_default: * @context: a #DfuContext diff --git a/libdfu/dfu-context.h b/libdfu/dfu-context.h index 69b6fc878..3b194134d 100644 --- a/libdfu/dfu-context.h +++ b/libdfu/dfu-context.h @@ -65,6 +65,9 @@ DfuDevice *dfu_context_get_device_by_vid_pid (DfuContext *context, guint16 vid, guint16 pid, GError **error); +DfuDevice *dfu_context_get_device_by_platform_id (DfuContext *context, + const gchar *platform_id, + GError **error); DfuDevice *dfu_context_get_device_default (DfuContext *context, GError **error); diff --git a/libdfu/dfu-device.c b/libdfu/dfu-device.c index d79d9d033..e3531710f 100644 --- a/libdfu/dfu-device.c +++ b/libdfu/dfu-device.c @@ -731,6 +731,24 @@ dfu_device_get_target_by_alt_name (DfuDevice *device, return NULL; } +/** + * dfu_device_get_platform_id: + * @device: a #DfuDevice + * + * Gets the platform ID which normally corresponds to the port in some way. + * + * Return value: string or %NULL + * + * Since: 0.5.4 + **/ +const gchar * +dfu_device_get_platform_id (DfuDevice *device) +{ + DfuDevicePrivate *priv = GET_PRIVATE (device); + g_return_val_if_fail (DFU_IS_DEVICE (device), NULL); + return g_usb_device_get_platform_id (priv->dev); +} + /** * dfu_device_get_runtime_vid: * @device: a #DfuDevice diff --git a/libdfu/dfu-device.h b/libdfu/dfu-device.h index 2b220c3fa..d8dbf5136 100644 --- a/libdfu/dfu-device.h +++ b/libdfu/dfu-device.h @@ -78,6 +78,7 @@ gboolean dfu_device_open (DfuDevice *device, GError **error); gboolean dfu_device_close (DfuDevice *device, GError **error); +const gchar *dfu_device_get_platform_id (DfuDevice *device); GPtrArray *dfu_device_get_targets (DfuDevice *device); DfuTarget *dfu_device_get_target_by_alt_setting (DfuDevice *device, guint8 alt_setting,