예제 #1
0
파일: gusb-main.c 프로젝트: hughsie/libgusb
/**
 * gusb_cmd_watch:
 **/
static gboolean
gusb_cmd_watch (GUsbCmdPrivate *priv, gchar **values, GError **error)
{
	gboolean ret = TRUE;
	GPtrArray *devices;
	guint i;
	GUsbDevice *device;
	GMainLoop *loop;

	devices = g_usb_context_get_devices (priv->usb_ctx);
	for (i = 0; i < devices->len; i++) {
		device = g_ptr_array_index (devices, i);
		g_print ("device %s already present %x:%x\n",
			 g_usb_device_get_platform_id (device),
			 g_usb_device_get_bus (device),
			 g_usb_device_get_address (device));
		gusb_main_device_open (device);
	}

	loop = g_main_loop_new (NULL, FALSE);
	g_signal_connect (priv->usb_ctx, "device-added",
			  G_CALLBACK (gusb_device_list_added_cb),
			  priv);
	g_signal_connect (priv->usb_ctx, "device-removed",
			  G_CALLBACK (gusb_device_list_removed_cb),
			  priv);
	g_main_loop_run (loop);

	g_main_loop_unref (loop);
	g_ptr_array_unref (devices);
	return ret;
}
예제 #2
0
파일: gusb-main.c 프로젝트: hughsie/libgusb
/**
 * gusb_cmd_show:
 **/
static gboolean
gusb_cmd_show (GUsbCmdPrivate *priv, gchar **values, GError **error)
{
	GNode *n;
	GNode *node;
	guint i;
	GUsbDevice *device;
	GUsbDevice *parent;
	GPtrArray *devices = NULL;

	/* sort */
	devices = g_usb_context_get_devices (priv->usb_ctx);
	g_ptr_array_sort (devices, gusb_devices_sort_by_platform_id_cb);

	/* make a tree of the devices */
	node = g_node_new (NULL);
	for (i = 0; i < devices->len; i++) {
		device = g_ptr_array_index (devices, i);

		parent = g_usb_device_get_parent (device);
		if (parent == NULL) {
			g_node_append_data (node, device);
			continue;
		}
		n = g_node_find (node, G_PRE_ORDER, G_TRAVERSE_ALL, parent);
		if (n == NULL) {
			g_set_error (error, 1, 0,
				     "no parent node for %s",
				     g_usb_device_get_platform_id (device));
			return FALSE;
		}
		g_node_append_data (n, device);

	}

	g_ptr_array_unref (devices);
	g_node_traverse (node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, moo_cb, priv);

	return TRUE;
}
예제 #3
0
파일: ebitdo-tool.c 프로젝트: attente/fwupd
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;
}