ICP_ErrorType ICP_Init(void) {

   Logging::print("ICP_Init()\n");

   ICP_ErrorType rc = bdm_usb_init();

   initialised = (rc == ICP_RC_OK);

   return rc;
}
示例#2
0
//**********************************************************
//!
//! Find all USBDM devices attached to the computer
//!
//!  @param deviceCount Number of devices found.  This is set
//!                     to zero on any error.
//!
//!  @return\n
//!       ICP_RC_OK        - success - found at least 1 device \n
//!       ICP_RC_USB_ERROR - no device found or various errors
//!
ICP_ErrorType bdm_usb_findDevices(unsigned *devCount) {

//   print("bdm_usb_find_devices()\n");

   *devCount = 0; // Assume failure

   ICP_ErrorType rc = ICP_RC_OK;

   // Release any currently referenced devices
   bdm_usb_releaseDevices();

   if (!initialised) {
      print("bdm_usb_find_devices() - Not initialised! \n");
      rc = bdm_usb_init(); // try again
      if (rc != ICP_RC_OK) {
         return rc;
      }
   }
   // discover all USB devices
   libusb_device **list;

   ssize_t cnt = libusb_get_device_list(NULL, &list);
   if (cnt < 0) {
      print("bdm_usb_find_devices() - libusb_get_device_list() failed! \n");
      return ICP_RC_USB_ERROR;
   }

   // Copy the ones we are interested in to our own list
   deviceCount = 0;
   for (int deviceIndex=0; deviceIndex<cnt; deviceIndex++) {
      // Check each device and copy any devices to local list
//      print("bdm_usb_find_devices() ==> checking device #%d\n", deviceIndex);
      libusb_device *currentDevice = list[deviceIndex];
      libusb_device_descriptor deviceDescriptor;
      int rc = libusb_get_device_descriptor(currentDevice, &deviceDescriptor);
      if (rc != LIBUSB_SUCCESS) {
         continue; // Skip device
      }
//      print("bdm_usb_find_devices() ==> Checking device VID=%4.4X, PID=%4.4X\n",
//            deviceDescriptor.idVendor, deviceDescriptor.idProduct);
      if ((deviceDescriptor.idVendor==JS16_BOOT_VID)&&(deviceDescriptor.idProduct==JS16_BOOT_PID)) {
         // Found a device
//         print("bdm_usb_find_devices() ==> found USBDM device #%d\n", deviceIndex);
         bdmDevices[deviceCount++] = currentDevice; // Record found device
         libusb_ref_device(currentDevice);          // Reference so we don't lose it
         bdmDevices[deviceCount]=NULL;              // Terminate the list again
         if (deviceCount>MAX_BDM_DEVICES) {
            break;
         }
      }
   }
   // Free the original list (devices referenced above are still referenced)
   libusb_free_device_list(list, true);

   *devCount = deviceCount;

//   print("bdm_usb_find_devices() ==> %d\n", deviceCount);

   if(deviceCount>0) {
      return ICP_RC_OK;
   }
   else {
      return ICP_RC_NO_JS16_DEVICE;
   }
}