static void dfu_context_init (DfuContext *context) { DfuContextPrivate *priv = GET_PRIVATE (context); priv->timeout = 5000; priv->devices = g_ptr_array_new_with_free_func ((GDestroyNotify) dfu_context_device_free); priv->usb_ctx = g_usb_context_new (NULL); g_signal_connect (priv->usb_ctx, "device-added", G_CALLBACK (dfu_context_device_added_cb), context); g_signal_connect (priv->usb_ctx, "device-removed", G_CALLBACK (dfu_context_device_removed_cb), context); }
static void fu_provider_chug_init (FuProviderChug *provider_chug) { FuProviderChugPrivate *priv = GET_PRIVATE (provider_chug); priv->devices = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) fu_provider_chug_device_free); priv->usb_ctx = g_usb_context_new (NULL); priv->device_queue = ch_device_queue_new (); g_signal_connect (priv->usb_ctx, "device-added", G_CALLBACK (fu_provider_chug_device_added_cb), provider_chug); g_signal_connect (priv->usb_ctx, "device-removed", G_CALLBACK (fu_provider_chug_device_removed_cb), provider_chug); }
/** * cd_sensor_init: **/ static void cd_sensor_init (CdSensor *sensor) { CdSensorPrivate *priv = GET_PRIVATE (sensor); priv->state = CD_SENSOR_STATE_IDLE; priv->mode = CD_SENSOR_CAP_UNKNOWN; #ifdef HAVE_GUSB priv->usb_ctx = g_usb_context_new (NULL); #endif priv->options = g_hash_table_new_full (g_str_hash, g_str_equal, (GDestroyNotify) g_free, (GDestroyNotify) g_variant_unref); priv->metadata = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); }
int main (int argc, char *argv[]) { gboolean ret; GError *error = NULL; GMainLoop *loop = NULL; GUsbContext *ctx; GUsbDevice *device = NULL; int retval = 0; GUsbDeviceList *list = NULL; ChDeviceQueue *device_queue; /* setup usb */ g_type_init (); ctx = g_usb_context_new (&error); if (ctx == NULL) { g_warning ("Cannot connect to USB : %s", error->message); g_error_free (error); retval = 1; goto out; } list = g_usb_device_list_new (ctx); device_queue = ch_device_queue_new (); ret = connect_device (list, &device, &error); if (!ret) { g_warning ("Cannot connect to device : %s", error->message); g_error_free (error); retval = 1; goto out; } /* reset device so it boots back into bootloader mode */ g_warning ("Switching to bootloader mode\n"); ch_device_queue_reset (device_queue, device); ret = ch_device_queue_process (device_queue, CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, NULL, &error); if (!ret) { g_warning ("Failed to reboot : %s", error->message); g_error_free (error); retval = 1; goto out; } /* wait for device to re-appear */ loop = g_main_loop_new (NULL, FALSE); g_timeout_add (5000, quit_loop_cb, loop); g_main_loop_run (loop); g_object_unref (device); ret = connect_device (list, &device, &error); if (!ret) { g_warning ("Cannot connect to device : %s", error->message); g_error_free (error); retval = 1; goto out; } /* boot into firmware mode */ g_warning ("Switching to firmware mode\n"); ch_device_queue_boot_flash (device_queue, device); ret = ch_device_queue_process (device_queue, CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, NULL, &error); if (!ret) { g_warning ("Failed to boot into firmware mode : %s", error->message); g_error_free (error); retval = 1; goto out; } /* wait for device to re-appear */ g_timeout_add (5000, quit_loop_cb, loop); g_main_loop_run (loop); g_object_unref (device); ret = connect_device (list, &device, &error); if (!ret) { g_warning ("Cannot connect to device : %s", error->message); g_error_free (error); retval = 1; goto out; } /* turn on LEDs */ g_warning ("Turning on LEDs\n"); ch_device_queue_set_leds (device_queue, device, 3, 0, 0, 0); ret = ch_device_queue_process (device_queue, CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, NULL, &error); if (!ret) { g_warning ("Failed to turn on LEDs : %s", error->message); g_error_free (error); retval = 1; goto out; } /* success */ g_warning ("ALL OKAY\n"); out: if (loop != NULL) g_main_loop_unref (loop); if (ctx != NULL) g_object_unref (ctx); if (device_queue != NULL) g_object_unref (device_queue); if (device != NULL) g_object_unref (device); if (list != NULL) g_object_unref (list); return retval; }
/** * main: **/ int main (int argc, char *argv[]) { gboolean ret; gboolean verbose = FALSE; gchar *cmd_descriptions = NULL; gchar *options_help = NULL; GError *error = NULL; gint retval = 0; GUsbCmdPrivate *priv; const GOptionEntry options[] = { { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Show extra debugging information", NULL }, { NULL} }; setlocale (LC_ALL, ""); /* create helper object */ priv = g_slice_new0 (GUsbCmdPrivate); priv->context = g_option_context_new ("GUSB Console Program"); g_option_context_add_main_entries (priv->context, options, NULL); g_option_context_parse (priv->context, &argc, &argv, NULL); /* verbose? */ if (verbose) { g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL); g_log_set_handler ("GUsb", G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_DEBUG | G_LOG_LEVEL_WARNING, gusb_log_handler_cb, NULL); } else { /* hide all debugging */ g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL); g_log_set_handler ("GUsb", G_LOG_LEVEL_DEBUG, gusb_log_ignore_cb, NULL); } /* GUsbContext */ priv->usb_ctx = g_usb_context_new (NULL); g_usb_context_set_flags (priv->usb_ctx, G_USB_CONTEXT_FLAGS_AUTO_OPEN_DEVICES); /* add commands */ priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) gusb_cmd_item_free); gusb_cmd_add (priv->cmd_array, "show", "Show currently connected devices", gusb_cmd_show); gusb_cmd_add (priv->cmd_array, "watch", "Watch devices as they come and go", gusb_cmd_watch); gusb_cmd_add (priv->cmd_array, "replug", "Watch a device as it reconnects", gusb_cmd_replug); /* sort by command name */ g_ptr_array_sort (priv->cmd_array, (GCompareFunc) gusb_sort_command_name_cb); /* get a list of the commands */ cmd_descriptions = gusb_cmd_get_descriptions (priv->cmd_array); g_option_context_set_summary (priv->context, cmd_descriptions); /* nothing specified */ if (argc < 2) { options_help = g_option_context_get_help (priv->context, TRUE, NULL); g_print ("%s", options_help); goto out; } /* run the specified command */ ret = gusb_cmd_run (priv, argv[1], (gchar**) &argv[2], &error); if (!ret) { g_print ("%s\n", error->message); g_error_free (error); retval = 1; goto out; } out: if (priv != NULL) { if (priv->cmd_array != NULL) g_ptr_array_unref (priv->cmd_array); if (priv->usb_ctx != NULL) g_object_unref (priv->usb_ctx); g_option_context_free (priv->context); g_slice_free (GUsbCmdPrivate, priv); } /* free state */ g_free (options_help); g_free (cmd_descriptions); return retval; }
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; }
int main (int argc, char **argv) { ChAssemblePrivate *priv; gboolean ret; gboolean verbose = FALSE; GError *error = NULL; GOptionContext *context; int status = 0; const GOptionEntry options[] = { { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, /* TRANSLATORS: command line option */ _("Show extra debugging information"), NULL }, { NULL} }; setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); gtk_init (&argc, &argv); /* TRANSLATORS: A program to assemble calibrate the hardware */ context = g_option_context_new (_("ColorHug assembly tester")); g_option_context_add_group (context, gtk_get_option_group (TRUE)); g_option_context_add_main_entries (context, options, NULL); ret = g_option_context_parse (context, &argc, &argv, &error); if (!ret) { g_warning ("%s: %s", _("Failed to parse command line options"), error->message); g_error_free (error); } g_option_context_free (context); priv = g_new0 (ChAssemblePrivate, 1); priv->sample_widget = CD_SAMPLE_WIDGET (cd_sample_widget_new ()); priv->usb_ctx = g_usb_context_new (NULL); priv->device_queue = ch_device_queue_new (); g_signal_connect (priv->usb_ctx, "device-added", G_CALLBACK (ch_assemble_device_added_cb), priv); g_signal_connect (priv->usb_ctx, "device-removed", G_CALLBACK (ch_assemble_device_removed_cb), priv); /* ensure single instance */ priv->application = gtk_application_new ("com.hughski.ColorHug.Assemble", 0); g_signal_connect (priv->application, "startup", G_CALLBACK (ch_assemble_startup_cb), priv); g_signal_connect (priv->application, "activate", G_CALLBACK (ch_assemble_activate_cb), priv); /* set verbose? */ if (verbose) { g_setenv ("COLORHUG_VERBOSE", "1", FALSE); } else { g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, ch_assemble_ignore_cb, NULL); } /* wait */ status = g_application_run (G_APPLICATION (priv->application), argc, argv); g_object_unref (priv->application); if (priv->device_queue != NULL) g_object_unref (priv->device_queue); if (priv->usb_ctx != NULL) g_object_unref (priv->usb_ctx); if (priv->builder != NULL) g_object_unref (priv->builder); g_free (priv); return status; }