コード例 #1
0
int main(int argc, char *argv[])
{
	libusb_hotplug_callback_handle hp[2];
	int product_id, vendor_id, class_id;
	int rc;

	vendor_id  = (argc > 1) ? strtol (argv[1], NULL, 0) : 0x045a;
	product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005;
	class_id   = (argc > 3) ? strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;

	rc = libusb_init (NULL);
	if (rc < 0)
	{
		printf("failed to initialise libusb: %s\n", libusb_error_name(rc));
		return EXIT_FAILURE;
	}

	if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
		printf ("Hotplug capabilites are not supported on this platform\n");
		libusb_exit (NULL);
		return EXIT_FAILURE;
	}

	rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
		product_id, class_id, hotplug_callback, NULL, &hp[0]);
	if (LIBUSB_SUCCESS != rc) {
		fprintf (stderr, "Error registering callback 0\n");
		libusb_exit (NULL);
		return EXIT_FAILURE;
	}

	rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
		product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
	if (LIBUSB_SUCCESS != rc) {
		fprintf (stderr, "Error registering callback 1\n");
		libusb_exit (NULL);
		return EXIT_FAILURE;
	}

	while (done < 2) {
		rc = libusb_handle_events (NULL);
		if (rc < 0)
			printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc));
	}

	if (handle) {
		libusb_close (handle);
	}

	libusb_exit (NULL);

	return EXIT_SUCCESS;
}
コード例 #2
0
UsbNotifier::UsbNotifier(int vendor, int product, QObject *parent)
    : QThread(parent)
    , d(new UsbNotifierPrivate)
{
    // register to the QMetaSystem so Qt knows how to pass arount the object
    qRegisterMetaType<UsbDevice>("UsbDevice");

    if (!s_me) {
        s_me = this;
    }

    d->vendor = vendor;
    d->product = product;

    libusb_hotplug_callback_handle attachHandle;
    libusb_hotplug_callback_handle detachHandle;
    libusb_init(NULL);
    libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_WARNING);

    if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
        // TODO setup hotplug callbacks
        qDebug("This device correctly supports callback");
    } else {
        // TODO use thread with manual update
    }

    int result = libusb_hotplug_register_callback(NULL
                                                , LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED
                                                , LIBUSB_HOTPLUG_ENUMERATE
                                                , d->vendor == 0 ? LIBUSB_HOTPLUG_MATCH_ANY : d->vendor
                                                , d->product == 0 ? LIBUSB_HOTPLUG_MATCH_ANY : d->product
                                                , LIBUSB_HOTPLUG_MATCH_ANY
                                                , &deviceInsertCallback
                                                , NULL
                                                , &attachHandle);

    // TODO handle callback registration fail
    qDebug() << "CALLBACK BIND RESULT: " << result;

    result = libusb_hotplug_register_callback(NULL
                                            , LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT
                                            , LIBUSB_HOTPLUG_ENUMERATE
                                            , d->vendor == 0 ? LIBUSB_HOTPLUG_MATCH_ANY : d->vendor
                                            , d->product == 0 ? LIBUSB_HOTPLUG_MATCH_ANY : d->product
                                            , LIBUSB_HOTPLUG_MATCH_ANY
                                            , &deviceDetachCallback
                                            , NULL
                                            , &detachHandle);

    // TODO handle callback registration fail
    qDebug() << "CALLBACK BIND RESULT: " << result;
}
コード例 #3
0
ファイル: usb.c プロジェクト: GalliumOS/cups-filters
void usb_register_callback(struct usb_sock_t *usb)
{
	IGNORE(usb);

	int status = libusb_hotplug_register_callback(
			NULL,
			LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
			// Note: libusb's enum has no default value
			// a bug has been filled with libusb.
			// Please switch the below line to 0
			// once the issue has been fixed in
			// deployed versions of libusb
			// https://github.com/libusb/libusb/issues/35
			// 0,
			LIBUSB_HOTPLUG_ENUMERATE,
			g_options.vendor_id,
			g_options.product_id,
			LIBUSB_HOTPLUG_MATCH_ANY,
			&usb_exit_on_unplug,
			NULL,
			NULL);
	if (status == LIBUSB_SUCCESS) {
		pthread_t thread_handle;
		pthread_create(&thread_handle, NULL, &usb_pump_events, NULL);
		NOTE("Registered unplug callback");
	} else
		ERR("Failed to register unplug callback");
}
コード例 #4
0
ファイル: usb-helpers.c プロジェクト: RC-MODULE/matlab
int ncusb_watch_for_device(libusb_context *ctx,
			  struct ncusb_devwatch_data* dwatch)
{
	return libusb_hotplug_register_callback(ctx, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
						LIBUSB_HOTPLUG_ENUMERATE, dwatch->vid, dwatch->pid,
						LIBUSB_HOTPLUG_MATCH_ANY,
						hotplug_callback_fn,
						dwatch, NULL);
}
コード例 #5
0
ファイル: main.c プロジェクト: lparam/BBBlfs
int
main(int argc, const char *argv[]) {
    int rc;
    libusb_context *ctx = NULL;
    libusb_hotplug_callback_handle handles[3];

    _init(ctx);

    if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
        printf("Hotplug not supported by this build of libusb\n");
        libusb_exit (NULL);
        return EXIT_FAILURE;
    }

    rc = libusb_hotplug_register_callback(ctx,
      LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
      0, ROMVID, ROMPID, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL, &handles[0]);

    rc = libusb_hotplug_register_callback(ctx,
      LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
      0, SPLVID, SPLPID, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL, &handles[1]);

    rc = libusb_hotplug_register_callback(ctx,
      LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
      0, UBOOTVID, UBOOTPID, LIBUSB_HOTPLUG_MATCH_ANY, hotplug_callback, NULL, &handles[2]);

    if (LIBUSB_SUCCESS != rc) {
        fprintf(stderr, "Error registering callback 0\n");
        libusb_exit(NULL);
        return EXIT_FAILURE;
    }

    while (!finished) {
        rc = libusb_handle_events(ctx);
        if (rc) {
			printf("libusb_handle_events() failed: %s\n", libusb_error_name(rc));
        }
    }

    _destroy(ctx);

    return 0;
}
コード例 #6
0
ファイル: hotplugtest.c プロジェクト: protomouse/dualpair
int main (int argc, char *argv[]) {
  libusb_hotplug_callback_handle hp[2];
  int product_id, vendor_id, class_id;
  int rc;

  vendor_id  = (argc > 1) ? strtol (argv[1], NULL, 0) : 0x045a;
  product_id = (argc > 2) ? strtol (argv[2], NULL, 0) : 0x5005;
  class_id   = (argc > 3) ? strtol (argv[3], NULL, 0) : LIBUSB_HOTPLUG_MATCH_ANY;

  libusb_init (NULL);

  if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
    printf ("Hotplug not supported by this build of libusb\n");
    libusb_exit (NULL);
    return EXIT_FAILURE;
  }

  rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
                                         product_id, class_id, hotplug_callback, NULL, &hp[0]);
  if (LIBUSB_SUCCESS != rc) {
    fprintf (stderr, "Error registering callback 0\n");
    libusb_exit (NULL);
    return EXIT_FAILURE;
  }

  rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
                                         product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
  if (LIBUSB_SUCCESS != rc) {
    fprintf (stderr, "Error registering callback 1\n");
    libusb_exit (NULL);
    return EXIT_FAILURE;
  }

  while (done < 2) {
    libusb_handle_events (NULL);
  }

  libusb_exit (NULL);
}
コード例 #7
0
ファイル: gusb-context.c プロジェクト: nacho/libgusb
static gboolean
g_usb_context_initable_init (GInitable     *initable,
                             GCancellable  *cancellable,
                             GError       **error)
{
	GUsbContext *context = G_USB_CONTEXT (initable);
	GUsbContextPrivate *priv;
	gint rc;
	libusb_context *ctx;

	priv = context->priv;

	rc = libusb_init (&ctx);
	if (rc < 0) {
		g_set_error (error,
		             G_USB_CONTEXT_ERROR,
		             G_USB_CONTEXT_ERROR_INTERNAL,
		             "failed to init libusb: %s [%i]",
		             g_usb_strerror (rc), rc);
		return FALSE;
	}

	priv->main_ctx = g_main_context_ref (g_main_context_default ());
	priv->ctx = ctx;
	priv->thread_event_run = 1;
	priv->thread_event = g_thread_new ("GUsbEventThread",
	                                   g_usb_context_event_thread_cb,
	                                   context);

	/* watch for add/remove */
	if (libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
		rc = libusb_hotplug_register_callback (priv->ctx,
		                                       LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
		                                       LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
		                                       0,
		                                       LIBUSB_HOTPLUG_MATCH_ANY,
		                                       LIBUSB_HOTPLUG_MATCH_ANY,
		                                       LIBUSB_HOTPLUG_MATCH_ANY,
		                                       g_usb_context_hotplug_cb,
		                                       context,
		                                       &context->priv->hotplug_id);
		if (rc != LIBUSB_SUCCESS) {
			g_warning ("Error creating a hotplug callback: %s",
			           g_usb_strerror (rc));
		}
	}

	return TRUE;
}
コード例 #8
0
ファイル: backend.cpp プロジェクト: Cortexelus/rockbox
/**
 * HWStubBackendHelper
 */
HWStubBackendHelper::HWStubBackendHelper()
{
    libusb_init(NULL);
#ifdef LIBUSB_NO_HOTPLUG
    m_hotplug = false;
#else
    m_hotplug = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG);
    if(m_hotplug)
    {
        m_hotplug = LIBUSB_SUCCESS == libusb_hotplug_register_callback(
            NULL,   (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
            LIBUSB_HOTPLUG_ENUMERATE, HWSTUB_USB_VID, HWSTUB_USB_PID, HWSTUB_CLASS,
            &HWStubBackendHelper::HotPlugCallback, reinterpret_cast< void* >(this), &m_hotplug_handle);
    }
#endif
}
コード例 #9
0
ファイル: backend.cpp プロジェクト: milaq/rockbox
/**
 * HWStubBackendHelper
 */
HWStubBackendHelper::HWStubBackendHelper()
{
#ifdef LIBUSB_NO_HOTPLUG
    m_hotplug = false;
#else
    m_hotplug = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG);
    if(m_hotplug)
    {
        int evt = LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
            LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT;
        m_hotplug = LIBUSB_SUCCESS == libusb_hotplug_register_callback(
            NULL, (libusb_hotplug_event)evt, LIBUSB_HOTPLUG_ENUMERATE,
            LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY,
            &HWStubBackendHelper::HotPlugCallback, reinterpret_cast< void* >(this),
            &m_hotplug_handle);
    }
#endif /* LIBUSB_NO_HOTPLUG */
}
コード例 #10
0
void HotplugCallbackAdapter::init(libusb_context* ctx) {
    libusb_hotplug_callback_handle h;
    
    Q_ASSERT(_p);
    Q_ASSERT(_p->handle == 0);
    Q_ASSERT(ctx != 0);
    
    int rc = libusb_hotplug_register_callback(ctx,
                                              (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED + LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), 
                                              LIBUSB_HOTPLUG_ENUMERATE, 
                                              LIBUSB_HOTPLUG_MATCH_ANY, 
                                              LIBUSB_HOTPLUG_MATCH_ANY,
                                              LIBUSB_HOTPLUG_MATCH_ANY, 
                                              hotplug_callback, 
                                              this,
                                              &h);
    if(rc == 0)
        _p->handle = h;
}
コード例 #11
0
ファイル: fcserver.cpp プロジェクト: Elfkay/fadecandy
bool FCServer::startUSB(libusb_context *usb)
{
    mUSB = usb;

    // Enumerate all attached devices, and get notified of hotplug events
    libusb_hotplug_register_callback(mUSB,
        libusb_hotplug_event(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
                             LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
        LIBUSB_HOTPLUG_ENUMERATE,
        LIBUSB_HOTPLUG_MATCH_ANY,
        LIBUSB_HOTPLUG_MATCH_ANY,
        LIBUSB_HOTPLUG_MATCH_ANY,
        cbHotplug, this, 0);

    // On platforms without real USB hotplug, emulate it with a polling thread
    if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
        mUSBHotplugThread = new tthread::thread(usbHotplugThreadFunc, this);
    }

    return true;
}
コード例 #12
0
static void *libusb_hid_init(void)
{
   unsigned i, count;
   int ret;
   struct libusb_device **devices;
   libusb_hid_t *hid = (libusb_hid_t*)calloc(1, sizeof(*hid));

   if (!hid)
      goto error;

   ret = libusb_init(&hid->ctx);

   if (ret < 0)
      goto error;

   if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
      goto error;

   hid->slots = pad_connection_init(MAX_USERS);

   if (!hid->slots)
      goto error;

   count = libusb_get_device_list(hid->ctx, &devices);

   for (i = 0; i < count; i++)
   {
      struct libusb_device_descriptor desc;
      libusb_get_device_descriptor(devices[i], &desc);

      if (desc.idVendor > 0 && desc.idProduct > 0)
         add_adapter(hid, devices[i]);
   }

   if (count > 0)
      libusb_free_device_list(devices, 1);

   ret = libusb_hotplug_register_callback(
         hid->ctx,
         (libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
         LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
         (libusb_hotplug_flag)LIBUSB_HOTPLUG_ENUMERATE,
         LIBUSB_HOTPLUG_MATCH_ANY,
         LIBUSB_HOTPLUG_MATCH_ANY,
         LIBUSB_HOTPLUG_MATCH_ANY,
         libusb_hid_hotplug_callback,
         hid,
         &hid->hp);

   if (ret != LIBUSB_SUCCESS)
   {
      RARCH_ERR("Error creating a hotplug callback.\n");
      goto error;
   }

   hid->poll_thread = sthread_create(poll_thread, hid);

   if (!hid->poll_thread)
   {
      RARCH_ERR("Error creating polling thread");
      goto error;
   }

   return hid;

error:
   if (hid)
      libusb_hid_free(hid);
   return NULL;
}
コード例 #13
0
int main(int argc, char *argv[])
{
  libusb_hotplug_callback_handle hp[2];
  int product_id, vendor_id, class_id = LIBUSB_HOTPLUG_MATCH_ANY;
  g_Command_Attach = "";
  g_Command_Detach = "";
  int rc;
  char flag_initial_search = 0;
  
  /* getopt */
  while ((rc = getopt(argc, argv, "c:a:d:ih")) != -1) {
    switch (rc) {
      case 'c':
        class_id = strtol(optarg, NULL, 0);
        break;
      case 'a':
        g_Command_Attach = optarg;
        break;
      case 'd':
        g_Command_Detach = optarg;
        break;
      case 'i':
        flag_initial_search = 1;
        break;
      case 'h':
      default: /* '?' */
        print_usage(argv[0]);
        exit(EXIT_FAILURE);
    }
  }
  if (optind + 2 > argc)
  {
    print_usage(argv[0]);
    exit(EXIT_FAILURE);
  }
  vendor_id = strtol(argv[optind++], NULL, 0);
  product_id = strtol(argv[optind++], NULL, 0);
  
  /* 初期化 */
  rc = libusb_init (NULL);
  if (rc < 0)
  {
    fprintf(stderr, "failed to initialise libusb: %s\n", libusb_error_name(rc));
    return EXIT_FAILURE;
  }

  /* シグナルハンドリング */
  signal(SIGINT, finish);
  signal(SIGTERM, finish);

  /* ホットプラグ対応のプラットフォームか調べる */
  if (!libusb_has_capability (LIBUSB_CAP_HAS_HOTPLUG)) {
    fprintf (stderr, "Hotplug capabilites are not supported on this platform\n");
    libusb_exit (NULL);
    return EXIT_FAILURE;
  }

  /* (オプションi)起動した時点での状態から目的のUSBデバイスを探す。 */
  if (flag_initial_search)
  {
    if (find_device(vendor_id, product_id))
    {
      func_attached();
    }
    else
    {
      func_detached();
    }
  }

  /* デバイスが接続されたら実行される関数を登録 */
  rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id,
                                         product_id, class_id, hotplug_callback, NULL, &hp[0]);
  if (LIBUSB_SUCCESS != rc) {
    fprintf (stderr, "Error registering callback 0\n");
    libusb_exit (NULL);
    return EXIT_FAILURE;
  }

  /* デバイスが切断されたら実行される関数を登録 */
  rc = libusb_hotplug_register_callback (NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id,
                                         product_id,class_id, hotplug_callback_detach, NULL, &hp[1]);
  if (LIBUSB_SUCCESS != rc) {
    fprintf (stderr, "Error registering callback 1\n");
    libusb_exit (NULL);
    return EXIT_FAILURE;
  }

  /* メインループ。イベントを回し続ける */
  while (1) {
    rc = libusb_handle_events (NULL);
    if (rc < 0)
      fprintf(stderr, "libusb_handle_events() failed: %s\n", libusb_error_name(rc));
  }

  /* 到達不能コード。終了処理はシグナルにより呼び出される。 */
  /* libusb_exit (NULL); */
}