/** * g_usb_context_find_by_vid_pid: * @context: a #GUsbContext * @vid: a vendor ID * @pid: a product ID * @error: A #GError or %NULL * * Finds a device based on its bus and address values. * * Return value: (transfer full): a new #GUsbDevice, or %NULL if not found. * * Since: 0.2.2 **/ GUsbDevice * g_usb_context_find_by_vid_pid (GUsbContext *context, guint16 vid, guint16 pid, GError **error) { GUsbContextPrivate *priv; GUsbDevice *device = NULL; guint i; g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); priv = context->priv; g_usb_context_enumerate (context); for (i = 0; i < priv->devices->len; i++) { GUsbDevice *curr = g_ptr_array_index (priv->devices, i); if (g_usb_device_get_vid (curr) == vid && g_usb_device_get_pid (curr) == pid) { device = g_object_ref (curr); break; } } if (device == NULL) { g_set_error (error, G_USB_DEVICE_ERROR, G_USB_DEVICE_ERROR_NO_DEVICE, "Failed to find device %04x:%04x", vid, pid); } return device; }
/** * g_usb_context_find_by_platform_id: * @context: a #GUsbContext * @platform_id: a platform id, e.g. "usb:00:03:03:02" * @error: A #GError or %NULL * * Finds a device based on its platform id value. * * Return value: (transfer full): a new #GUsbDevice, or %NULL if not found. * * Since: 0.2.4 **/ GUsbDevice * g_usb_context_find_by_platform_id (GUsbContext *context, const gchar *platform_id, GError **error) { GUsbContextPrivate *priv; GUsbDevice *device = NULL; guint i; g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); priv = context->priv; g_usb_context_enumerate (context); for (i = 0; i < priv->devices->len; i++) { GUsbDevice *curr = g_ptr_array_index (priv->devices, i); if (g_strcmp0 (g_usb_device_get_platform_id (curr), platform_id) == 0) { device = g_object_ref (curr); break; } } if (device == NULL) { g_set_error (error, G_USB_DEVICE_ERROR, G_USB_DEVICE_ERROR_NO_DEVICE, "Failed to find device %s", platform_id); } return device; }
static gboolean fu_provider_chug_coldplug (FuProvider *provider, GError **error) { FuProviderChug *provider_chug = FU_PROVIDER_CHUG (provider); FuProviderChugPrivate *priv = GET_PRIVATE (provider_chug); g_usb_context_enumerate (priv->usb_ctx); return TRUE; }
/** * g_usb_context_get_devices: * @context: a #GUsbContext * * Return value: (transfer full) (element-type GUsbDevice): a new #GPtrArray of #GUsbDevice's. * * Since: 0.2.2 **/ GPtrArray * g_usb_context_get_devices (GUsbContext *context) { g_return_val_if_fail (G_USB_IS_CONTEXT (context), NULL); g_usb_context_enumerate (context); return g_ptr_array_ref (context->priv->devices); }
/** * dfu_context_enumerate: * @context: a #DfuContext * @error: a #GError, or %NULL * * Opens a DFU-capable context. * * Return value: %TRUE for success * * Since: 0.5.4 **/ gboolean dfu_context_enumerate (DfuContext *context, GError **error) { DfuContextPrivate *priv = GET_PRIVATE (context); g_return_val_if_fail (DFU_IS_CONTEXT (context), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); g_usb_context_enumerate (priv->usb_ctx); return TRUE; }
static void ch_assemble_startup_cb (GApplication *application, ChAssemblePrivate *priv) { g_autoptr(GError) error = NULL; gint retval; GtkWidget *box; GtkWidget *main_window; /* get UI */ priv->builder = gtk_builder_new (); retval = gtk_builder_add_from_file (priv->builder, CH_DATA "/ch-assemble.ui", &error); if (retval == 0) { g_warning ("failed to load ui: %s", error->message); return; } /* add application specific icons to search path */ gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (), CH_DATA G_DIR_SEPARATOR_S "icons"); main_window = GTK_WIDGET (gtk_builder_get_object (priv->builder, "window_assemble")); gtk_application_add_window (priv->application, GTK_WINDOW (main_window)); gtk_widget_set_size_request (main_window, 600, 400); /* Set initial state */ box = GTK_WIDGET (gtk_builder_get_object (priv->builder, "box1")); gtk_box_pack_start (GTK_BOX (box), GTK_WIDGET (priv->sample_widget), TRUE, TRUE, 0); ch_assemble_set_label (priv, _("No device detected")); ch_assemble_set_color (priv, 0.5f, 0.5f, 0.5f); /* is the colorhug already plugged in? */ g_usb_context_enumerate (priv->usb_ctx); /* show main UI */ gtk_window_maximize (GTK_WINDOW (main_window)); gtk_widget_show (main_window); }
int main (int argc, char **argv) { gsize len; guint i; g_autofree guint8 *data = NULL; g_autoptr(EbitdoDevice) dev = NULL; g_autoptr(GBytes) fw = NULL; g_autoptr(GError) error = NULL; g_autoptr(GPtrArray) devices = NULL; g_autoptr(GUsbContext) usb_ctx = NULL; g_setenv ("G_MESSAGES_DEBUG", "all", TRUE); /* require filename */ if (argc != 2) { g_print ("USAGE: %s <filename>\n", argv[0]); return 1; } /* get the device */ usb_ctx = g_usb_context_new (&error); if (usb_ctx == NULL) { g_print ("Failed to open USB devices: %s\n", error->message); return 1; } g_usb_context_enumerate (usb_ctx); devices = g_usb_context_get_devices (usb_ctx); for (i = 0; i < devices->len; i++) { GUsbDevice *usb_dev_tmp = g_ptr_array_index (devices, i); g_autoptr(EbitdoDevice) dev_tmp = ebitdo_device_new (usb_dev_tmp); if (ebitdo_device_get_kind (dev_tmp) != EBITDO_DEVICE_KIND_UNKNOWN) { dev = g_object_ref (dev_tmp); break; } } /* nothing supported */ if (dev == NULL) { g_print ("No supported device plugged in!\n"); return 1; } g_debug ("found %s [%04x:%04x]", ebitdo_device_kind_to_string (ebitdo_device_get_kind (dev)), g_usb_device_get_vid (ebitdo_device_get_usb_device (dev)), g_usb_device_get_pid (ebitdo_device_get_usb_device (dev))); /* open device */ if (!ebitdo_device_open (dev, &error)) { g_print ("Failed to open USB device: %s\n", error->message); return 1; } g_print ("Device Firmware Ver: %s\n", ebitdo_device_get_version (dev)); g_print ("Device Verification ID:\n"); for (i = 0; i < 9; i++) g_print ("\t%u = 0x%08x\n", i, ebitdo_device_get_serial(dev)[i]); /* not in bootloader mode, so print what to do */ if (ebitdo_device_get_kind (dev) != EBITDO_DEVICE_KIND_BOOTLOADER) { g_print ("1. Disconnect the controller\n"); switch (ebitdo_device_get_kind (dev)) { case EBITDO_DEVICE_KIND_FC30: case EBITDO_DEVICE_KIND_NES30: case EBITDO_DEVICE_KIND_SFC30: case EBITDO_DEVICE_KIND_SNES30: g_print ("2. Hold down L+R+START for 3 seconds until " "both LED lights flashing.\n"); break; case EBITDO_DEVICE_KIND_FC30PRO: case EBITDO_DEVICE_KIND_NES30PRO: g_print ("2. Hold down RETURN+POWER for 3 seconds until " "both LED lights flashing.\n"); break; case EBITDO_DEVICE_KIND_FC30_ARCADE: g_print ("2. Hold down L1+R1+HOME for 3 seconds until " "both blue LED and green LED blink.\n"); break; default: g_print ("2. Do what it says in the manual.\n"); break; } g_print ("3. Connect controller\n"); return 1; } /* load firmware file */ if (!g_file_get_contents (argv[1], (gchar **) &data, &len, &error)) { g_print ("Failed to load file: %s\n", error->message); return 1; } /* update with data blob */ fw = g_bytes_new (data, len); if (!ebitdo_device_write_firmware (dev, fw, ebitdo_write_progress_cb, NULL, &error)) { g_print ("Failed to write firmware: %s\n", error->message); return 1; } /* close device */ if (!ebitdo_device_close (dev, &error)) { g_print ("Failed to close USB device: %s\n", error->message); return 1; } /* success */ g_print ("Now turn off the controller with the power button.\n"); return 0; }