Skip to content

Commit

Permalink
add mermaid diagram to libusb wrapper docs
Browse files Browse the repository at this point in the history
  • Loading branch information
diablodale committed Jul 4, 2023
1 parent 0aaf58a commit f78945d
Showing 1 changed file with 113 additions and 5 deletions.
118 changes: 113 additions & 5 deletions src/pc/protocols/wrap_libusb.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from this base resource type.

`dp::libusb` is currently focused on the needs of XLink; the protocol to communicate with
Intel Movidius/Myriad devices. A `std::shared_ptr` base for resources was considered for
some use cases. It was not implemented due to the limited needs of XLink.
some use cases. It is not implemented due to the limited needs of XLink; a single owner.

Many raw libusb types, e.g. `libusb_device_descriptor`, are used in their simple POD form.
Some libusb resources are not implemented, e.g. asynchronous device I/O.
Expand All @@ -34,13 +34,121 @@ Most behaviors of `dp::libusb` resources derive from `std::unique_ptr`.
with error text sourced from `libusb_strerror()`.
6. Raw resource pointers can be retrieved with `get()` to use with libusb C apis.

## Types Overview and Hierarchy
## Object Model and Types

mermaid tbd
All libusb wrapper classes are derived from `unique_resource_ptr<libusb_****, disposeFunc>`
where `libusb_****` is the native libusb POD struct and `disposeFunc` is the native libusb disposing function.

`unique_resource_ptr<>` is a helper class that inherits from `std::unique_ptr<libusb_****, unique_resource_deleter<libusb_****, disposeFunc>>`. It uses the normal `std::unique_ptr` custom deleter feature to automatically call `*disposeFunc(unique_ptr::get())`.

* `usb_context` enhances raw `libusb_context*`
* `device_list` container exposes iterators to raw `libusb_device*`
* `device_list` stl container generates and grants access to all `usb_device` on the host
* `usb_device` enhances raw `libusb_device*`
* `device_handle` enhances raw `libusb_device_handle*`
* `config_descriptor` enhances raw `libusb_config_descriptor*`
* remaining libusb types are used unchanged
* all remaining libusb classes and structs are used unchanged

The typical process is

1. Create a `usb_context`
2. From that context get a `device_list`
3. From that list choose a `usb_device`
4. From that device get a `device_handle`
5. Operate and transfer on that handle
6. All resource lifetimes are RAII managed including resource release/free/exiting.

### Class diagram

In this diagram, the inheritance `public unique_resource_ptr<libusb_****, disposeFunc>` is abbreviated `resource<libusb_****, libusb_disposeFunction>`.

[Click to use Mermaid Live](https://mermaid.live/edit#pako:eNq1V1uP2kYU_iuWI0WADAvGMZdIedimVfPQviRPCZE12Md4tPaMOzMmkC389p6xjT027GZTtTywzDnfd-5z2Uc75BHYaztMiZTvKdkJkm1YRAWEinJmfbrfsA2z8FPIbRBypuCgrPH4nRXBnoYQpFSqCmAISoAmVLLWQLU2-QlhUXobgd5iukOJDAXNFRcVqvouA-4E9Vgp9EeA5IUI4ZzSrQF55XlvX83dt7UUDlSdK9LpkmVl1kzFMCtVqunaGqEMhF7wWH_3c-3VY2AEMbRq9ztQgYkZttRzR34hxALgKQbPQRAs0ZeviL8VzRZ2lHV0AVUVx0iQfgfTqlTYHSKioEk5yEAlPJIBKhoDcjKZXGi9ShotfaY_FaLXnoIJiGvVueW2-EGHPWrq1NIGuRLdIukSdCbvYqTUYUsGw6HB0D26GsNBJfnAIjj0sLVpA9sDoK-AFdkWrjQ5F6pWySeK2Qn8x_Wsgb2yhimXZj271WjZTTmrqj1HqCfbseRRvof976Wm4X8TJA9Qc-nJzZINbmyLuh0tHKtAs4CiMxET1De__izLNjQLkgKR8CJs2-MCpxkPvc4OuFJ2Vh-Y6lmSSlBmTktAZEjpoBX050a7IIXiyFEkTIIHEAzSIBJ0j1Oy5TztgTNyCHISPmhvesMCi3KO-Tnl_u3F8xT4ZwZ3W6QPgRKEyRgjat2NtkWMEseq_n7sen-K1Zwl9yXr3_lB4QUoIHIsRTPghZmVdiN42toT8FcBUn065siuF461J2mBa6qb8oSna9vlMIkiV_9JtL1dfnXe_OBm64J7u728M65A506ZeqdbewQ4lnnSmRv0mtXP5ZJvgCljCuPx39WLoFy2172hxSNCQdYBMIpdCi45B3ia14YqBa7NJwEjGUicdbASSPE-lLpwZmVND9aXjS1VtF533NpfL8W-cY9VvMfuVVQKB-X3L_iUGj6rdaxvCVE_gelF0SuqEUpXY1ozBu_-qEAOX0LSIfwf1H5Vmz427WhFr1N1meLRaORYEZU5Xl2_FSx8ZzSqfC-1LLxIQpKm0hoZ-M5NcjuGzpBhMDfELwyosX-yHTsDkREa4Qu71G5slUAGG3uNPyOISZGqjb1hGqrvgI9HFtprJfBIsos8IgrqN3lX-GtEcc_Z65ikEoUpJxHg8tFWeLihrx2-DtFitUu1vBApihOlcrm-u9PqyY6qpNhOQp7dSRolRKhkv_LvfNdfEncO_mJO3sznUbidrZax683iaDGducQ-nRw7J0xbPdjrsed6E89feSt_OvP82dJ17COKV-5ksZj6b1bucjGfrvwlsr5zjnnMED6bekvXnS7n_nI29xwbynz-qP8V0X9KH59LQpnk6R86Wkid) if you can not see the chart below.

```mermaid
classDiagram
direction TB
usb_context --> device_list
device_list --> usb_device
usb_device --> device_handle
usb_device --> config_descriptor
class usb_context {
resource~libusb_context#44;#32;libusb_exit~
}
class device_list {
stl#32;container#32;of#32;usb_device
device_list(usb_context) libusb_get_device_list()
~device_list() libusb_free_device_list()
operator[]() usb_device
begin() usb_device_iterator
size()
standard_container_methods_and_iterators...()
}
class usb_device {
resource~libusb_device#44;#32;libusb_unref_device~
usb_device(libusb_device*) libusb_ref_device(ptr)
open() device_handle(libusb_open(get()))
get_config_descriptor(configIndex)
get_device_descriptor()
get_bus_number()
get_port_numbers()
}
class device_handle {
resource~libusb_device_handle#44;#32;libusb_close~
device_handle(usb_device) libusb_open()
device_handle(usb_context, sysDevHandle) libusb_wrap_sys_device()
get_device() libusb_get_device(get())
claim_interface(interfaceNumber)
release_interface(interfaceNumber)
get_configuration()
set_configuration(configurationInt)
get_string_descriptor_ascii(descriptorIndex)
set_auto_detach_kernel_driver(bool)
set_max_packet_size(endpoint, size)
get_max_packet_size(endpoint)
get_device_descriptor()
bulk_transfer(endpoint, *buffer, bufferSize)
bulk_transfer(endpoint, containerBuffer)
bulk_transfer(endpoint, *buffer, bufferSize, *transferred, timeout)
control_transfer(requestType, request, value, index, *buffer, bufferSize, timeout)
interrupt_transfer(endpoint, *buffer, bufferSize, *transferred, timeout)
}
class config_descriptor {
resource~libusb_config_descriptor#44;#32;libusb_free_config_descriptor~
config_descriptor(usb_device, configIndex) libusb_get_config_descriptor()
}
transfer_error --|> usb_error
usb_error --|> system_error
unique_resource_ptr --|> unique_ptr
namespace helpers {
class system_error ["std::system_error"] {
}
class usb_error {
usb_error(errorCode)
usb_error(errorCode, what)
usb_error(errorCode, what)
}
class transfer_error {
transfer_error(errorCode, transferredBytes)
transfer_error(errorCode, what, transferredBytes)
transfer_error(errorCode, what, transferredBytes)
}
class unique_ptr ["std::unique_ptr&lt;libusb_***, disposeFunc>"] {
~unique_ptr() calls *disposeFunc(get())
}
class unique_resource_ptr ["unique_resource_ptr&lt;libusb_***, disposeFunc>"] {
}
}
```

0 comments on commit f78945d

Please sign in to comment.