Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++ wrapper for libusb, add try/catch to prevent app crashes #63

Closed

Conversation

diablodale
Copy link
Contributor

@diablodale diablodale commented May 18, 2023

Adds c++ wrappers for the minimum set of features needed without introducing major Xlink code refactoring. fixes #59
I can't move much further until the questions below are answered.

  • new dai::libusb namespace
  • Uses the constructor approach for wrappers (not factory)
  • Added usb_error for exceptions
  • Added usb_context, usb_device, device_list, device_handle, config_descriptor
  • Added methods like device_handle::claim_interface(), device_handle::set_configuration(), etc. to manage interfaces and fix resource leak bugs I found
  • Workaround for thread-unsafe libusb_get_device_list()

Questions

  1. can we shift XLink to be a C++14 project? the cmake's in XLink are still set to c++11. If no, then I'll need to change some code.
  2. what code should make mvLog entries. Should that be in the wrapper? Or should the Caller do it with catch() { mvLog("had an error: " + myexception.what()); }. Pros and Cons for both. This is more a style and feature choice. The wrapper classes do the logging as it matched the majority of use cases.
  3. Can I remove extern C from refLibusbDeviceByName()? I don't see a need for it. Does the device firmware code need it? I removed it.
  4. Code comments in refLibusbDeviceByName() say it filters by Myraid devices. It actually doesn't. This can be added if needed.
  5. I would like to pass between a few functions wrapper classes instead of raw pointers. Does your team have any concerns? Already changed some func signatures to use the new classes; usually as a reference.

- coordinate usb_host.cpp mutex and usb_mx_id_cache init
- make usb_mx_id_cache apis noexcept
- adds exception handling, RAII for device list
- add claim/release interfaces so to always release
  interfaces before closing handle. Fixes several
  error handling scenarios in XLink
- test was failing due to stucts not being init, they would
  contain random data in fields expecting null
- better libusb call parameter checking
- adds control of loglevel and throw/not
- update refLibusbDeviceByName() to have c++
  signature and use RAII ref counting
- add function-wide try/catch in getUSBDevices() and nested try
  to catch usb-specific errors
- removed/simplified code that is now in the RAII wrappers
- adjusted several function signatures to pass const usb_device&
  instead of raw pointers that cannot be safely ref counted
…_ascii()

- libusb_get_device_list() is not thread-safe, workaround with mutex
  in device_list class
- fix get_port_numbers()
- optimize device_handle::set_configuration()
- Fixes luxonis#61 major issues
- get bcdUSB spec version from device description during
  usb_boot() and use with send_file() during boot
- add MVLOG_DEBUG log for usb spec during usb_boot()
- add NOMINMAX to cmake for win32; well known need on modern code;
  was already in depthai-core
- begin refactoring of usb_host.cpp file to eliminate
  redundant code and collapse code into simplier
  functions using the libusb wrappers
- add simple dai::details::span that works on C++11
  and when c++20 is available will use that instead
- RAII partially implemented due to non-RAII
  capable fd lookup container
- fixes luxonis#67
- fixes mingw compile since errant code followed
  Windows macro logic
- behavior is undefined/crash using resource methods when `get() == nullptr`
- note: it does not have all std::span features
- reduces risk that callers will use a raw libusb_device*
  without managing its ownership and lifetime
- simplifies 2-step (get iter, create usb_device)
  to 1-step (get usb_device)
@diablodale
Copy link
Contributor Author

I've pushed the last commit to this branch. It includes markdown and mermaid documentation for usage and object model for the libusb wrapper. Its really quite simple. 🔢 This wrapper replaces much of the old Xlink C-style code for its usb transport.

Moving forward, fixes from this PR and more will usually be in my fork at https://github.com/diablodale/XLink/
Closing.

@diablodale diablodale closed this Jul 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants