/// <summary> /// Refreshes the <see cref="MonoUsbProfile"/> list. /// </summary> /// <remarks> /// <para>This is your entry point into finding a USB device to operate.</para> /// <para>This return value of this function indicates the number of devices in the resultant list.</para> /// <para>The <see cref="MonoUsbProfileList"/> has a crude form of built-in device notification that works on all platforms. By adding an event handler to the <see cref="AddRemoveEvent"/> changes in the device profile list are reported when <see cref="Refresh"/> is called.</para> /// </remarks> /// <param name="sessionHandle">A valid <see cref="MonoUsbSessionHandle"/>.</param> /// <returns>The number of devices in the outputted list, or <see cref="MonoUsbError.ErrorNoMem"/> on memory allocation failure.</returns> /// <example> /// <code source="..\MonoLibUsb\MonoUsb.ShowInfo\ShowInfo.cs" lang="cs"/> /// </example> public int Refresh(MonoUsbSessionHandle sessionHandle) { lock (LockProfileList) { MonoUsbProfileList newList = new MonoUsbProfileList(); MonoUsbProfileListHandle monoUSBProfileListHandle; int ret = MonoUsbApi.GetDeviceList(sessionHandle, out monoUSBProfileListHandle); if (ret < 0 || monoUSBProfileListHandle.IsInvalid) { #if LIBUSBDOTNET UsbError.Error(ErrorCode.MonoApiError, ret, "Refresh:GetDeviceList Failed", this); #else System.Diagnostics.Debug.Print("libusb_get_device_list failed:{0} {1}", (MonoUsbError)ret, MonoUsbApi.StrError((MonoUsbError)ret)); #endif return(ret); } int stopCount = ret; foreach (MonoUsbProfileHandle deviceProfileHandle in monoUSBProfileListHandle) { newList.mList.Add(new MonoUsbProfile(deviceProfileHandle)); stopCount--; if (stopCount <= 0) { break; } } syncWith(newList); monoUSBProfileListHandle.Close(); return(ret); } }
/// <summary> /// Refreshes the <see cref="MonoUsbProfile"/> list. /// </summary> /// <remarks> /// <para>This is your entry point into finding a USB device to operate.</para> /// <para>This return value of this function indicates the number of devices in the resultant list.</para> /// <para>The <see cref="MonoUsbProfileList"/> has a crude form of built-in device notification that works on all platforms. By adding an event handler to the <see cref="AddRemoveEvent"/> changes in the device profile list are reported when <see cref="Refresh"/> is called.</para> /// </remarks> /// <param name="sessionHandle">A valid <see cref="MonoUsbSessionHandle"/>.</param> /// <returns>The number of devices in the outputted list, or <see cref="MonoUsbError.ErrorNoMem"/> on memory allocation failure.</returns> /// <example> /// <code source="..\MonoLibUsb\MonoUsb.ShowInfo\ShowInfo.cs" lang="cs"/> /// </example> public int Refresh(MonoUsbSessionHandle sessionHandle) { lock (LockProfileList) { MonoUsbProfileList newList = new MonoUsbProfileList(); MonoUsbProfileListHandle monoUSBProfileListHandle; int ret = MonoUsbApi.GetDeviceList(sessionHandle, out monoUSBProfileListHandle); if (ret < 0 || monoUSBProfileListHandle.IsInvalid) { MonoUsbErrorMessage.Error(ErrorCode.MonoApiError, ret, "Refresh:GetDeviceList Failed", this); return(ret); } int stopCount = ret; foreach (MonoUsbProfileHandle deviceProfileHandle in monoUSBProfileListHandle) { newList.mList.Add(new MonoUsbProfile(deviceProfileHandle)); stopCount--; if (stopCount <= 0) { break; } } syncWith(newList); monoUSBProfileListHandle.Dispose(); return(ret); } }
private void syncWith(MonoUsbProfileList newList) { SetDiscovered(false); newList.SetDiscovered(true); int iNewProfiles = newList.mList.Count; for (int iNewProfile = 0; iNewProfile < iNewProfiles; iNewProfile++) { MonoUsbProfile newProfile = newList.mList[iNewProfile]; int iFoundOldIndex; if ((iFoundOldIndex = mList.IndexOf(newProfile)) == -1) { //Console.WriteLine("DeviceDiscovery: Added: {0}", newProfile.ProfileHandle.DangerousGetHandle()); newProfile.mDiscovered = true; //Super hackish way to add serial MonoUsbDevice dev = new MonoUsbDevice(ref newProfile); newProfile.mSerial = dev.Info.SerialString; mList.Add(newProfile); FireAddRemove(newProfile, AddRemoveType.Added); } else { //Console.WriteLine("DeviceDiscovery: Unchanged: Orig:{0} New:{1}", mList[iFoundOldIndex].ProfileHandle.DangerousGetHandle(), newProfile.ProfileHandle.DangerousGetHandle()); mList[iFoundOldIndex].mDiscovered = true; newProfile.mDiscovered = false; } } newList.mList.RemoveAll(FindDiscoveredFn); newList.Close(); foreach (MonoUsbProfile deviceProfile in mList) { if (!deviceProfile.mDiscovered) { // Close Unplugged device profiles. //Console.WriteLine("DeviceDiscovery: Removed: {0}", deviceProfile.ProfileHandle.DangerousGetHandle()); FireAddRemove(deviceProfile, AddRemoveType.Removed); deviceProfile.Close(); } } // Remove Unplugged device profiles. mList.RemoveAll(FindUnDiscoveredFn); }
private void BeginFreiburgSyncSession() { if (sessionHandle.IsInvalid) { throw new ExternalException("Failed to initialize context."); } MonoUsbApi.SetDebug(sessionHandle, 0); profileList = new MonoLibUsb.Profile.MonoUsbProfileList(); // The list is initially empty. // Each time refresh is called the list contents are updated. int profileListRefreshResult; profileListRefreshResult = profileList.Refresh(sessionHandle); if (profileListRefreshResult < 0) { throw new ExternalException("Failed to retrieve device list."); } Debug.Log(profileListRefreshResult.ToString() + " device(s) found."); // Iterate through the profile list. // If we find the device, write 00000000 to its endpoint 2. foreach (MonoLibUsb.Profile.MonoUsbProfile profile in profileList) { if (profile.DeviceDescriptor.ProductID == FREIBURG_SYNCBOX_PRODUCT_ID && profile.DeviceDescriptor.VendorID == FREIBURG_SYNCBOX_VENDOR_ID) { freiburgSyncboxProfile = profile; } } if (freiburgSyncboxProfile == null) { throw new ExternalException("None of the connected USB devices were identified as a Freiburg syncbox."); } freiburgSyncboxDeviceHandle = new MonoUsbDeviceHandle(freiburgSyncboxProfile.ProfileHandle); freiburgSyncboxDeviceHandle = freiburgSyncboxProfile.OpenDeviceHandle(); if (freiburgSyncboxDeviceHandle == null) { throw new ExternalException("The ftd USB device was found but couldn't be opened"); } StartCoroutine(FreiburgPulse()); }