Skip to content

rs usb api and implementations added#3828

Merged
ev-mp merged 3 commits into
realsenseai:developmentfrom
matkatz:rsusb-api
May 16, 2019
Merged

rs usb api and implementations added#3828
ev-mp merged 3 commits into
realsenseai:developmentfrom
matkatz:rsusb-api

Conversation

@matkatz

@matkatz matkatz commented Apr 23, 2019

Copy link
Copy Markdown
Contributor

This PR creates a common USB API for the different operating systems.
As first step this API will be used for cross platform FW update API and tools.
In the future this infrastructure will allow single implementation for UVC/HID/TM2 for all the supported operating systems.

rsusb

DSO-10947

@matkatz matkatz requested review from dorodnic and ev-mp April 23, 2019 19:12

std::shared_ptr<usb_device> android_backend::create_usb_device(usb_device_info info) const {
throw std::runtime_error("create_usb_device Not supported");
std::shared_ptr<command_transfer> android_backend::create_usb_device(usb_device_info info) const {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method create_usb_device returns command_transfer ?
If so - please use "_dev" suffix as command_transfer is too vague
Also is this intended to work with non-Realsense devices, such as platform cam ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original usb_device was an empty class which inherited command_transfer, since I removed usb_device I needed to replace it with command_transfer but I don't want to make more changes to the backend API in this PR unless it is necessary .

Comment thread src/android/android-backend.cpp Outdated
}

std::vector<usb_device_info> android_backend::query_usb_devices() const {
std::vector<usb_device_info> results;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls change the name to query_usb_devices_**info** for consistency


std::shared_ptr<device_watcher> android_backend::create_device_watcher() const {
return std::make_shared<usb_host::device_watcher>();
return device_watcher_usbhost::instance();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no need to wrap a singleton with shared_ptr.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to implement the backend API:
std::shared_ptr<device_watcher> create_device_watcher() const override;

Comment thread src/android/android-uvc.cpp Outdated
uvc->vid = dev->get_info().vid;
uvc->pid = dev->get_info().pid;
usbhost_open(uvc.get(), mi);
return uvc;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function will open the first device with matched MI. How can one query the 2nd or 3rd device ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/android/android-uvc.cpp Outdated
if (device->deviceData.ctrl_if.bInterfaceNumber == mi) {
_device = device;
if (state == D0 && _power_state == D3) {
uint16_t vid = _info.vid, pid = _info.pid, mi = _info.mi;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vid/pid seem unused in this segment - pls recheck

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will remove

Comment thread src/libusb/device-libusb.cpp Outdated
{
auto i = entry.second;
if(filter == USB_SUBCLASS_ANY ||
i->get_subclass() & filter ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put parentheses

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function will be removed

Comment thread src/libusb/device-libusb.h Outdated

virtual const usb_device_info get_info() const override { return _infos[0]; }
virtual const std::vector<usb_device_info> get_subdevices_infos() const override { return _infos; }
virtual const rs_usb_interface get_interface(uint8_t interface_number) const override { return _interfaces.at(interface_number); }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will throw if the index is not in the map

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_interface(uint8_t interface_number) will be removed, leaving only get_interfaces()

Comment thread src/libusb/enumerator-libusb.cpp Outdated
libusb_device_descriptor desc = { 0 };

auto rc = libusb_get_device_descriptor(lusb_device, &desc);
if (desc.idVendor != 0x8086)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For review - probably requires some sort of a mask - T265 , etc'
Log the devices that are filtered out

@matkatz matkatz May 3, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

devices are filtered on context.cpp via pick__devices

I have also removed the 0x8086 filter

Comment thread src/libusb/enumerator-libusb.cpp Outdated
break;
}
}
libusb_free_device_list(list, 0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A common pattern in this and the next functions:
libusb_get_device_list
...
libusb_free_device_list

  • Needs a common RAII support

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will make RAII

Comment thread src/libusb/handle-libusb.h Outdated
class handle_libusb
{
public:
handle_libusb(libusb_device* device, uint8_t interface, int timeout_ms = 100) :

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment how this value was selected

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timeout will be removed from the handle signature

Comment thread src/libusb/messenger-libusb.cpp Outdated

}

bool usb_messenger_libusb::reset_endpoint(std::shared_ptr<usb_endpoint> endpoint)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be made const

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/libusb/messenger-libusb.cpp Outdated
if(rv)
LOG_DEBUG("USB pipe " << ep << " reset successfully");
else
LOG_DEBUG("Failed to reset the USB pipe " << ep);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual result of the transfer is not registered (reduced to bool) will make it harder for debugging and maintenance

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usb status added to the log

@ev-mp ev-mp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A huge transformation.
We'll need multiple things to review and polish.
The one thing - the class diagram should clearly depict the inheritance tree with all the interfaces and concrete classes. It is very hard to navigate otherwise

Comment thread src/libusb/messenger-libusb.cpp Outdated

int usb_messenger_libusb::control_transfer(int request_type, int request, int value, int index, uint8_t* buffer, uint32_t length, uint32_t timeout_ms)
{
handle_libusb handle(_device->get_device(), index & 0xFF, timeout_ms);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please either perform a static_cast or add a note that the actual index is within 0-255.
You also need to check the input against things such as 0xabcd0011, which could be interpret as 0x11 without checking the higher bits

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the upper bytes are required for control transfer, the mask is extracting the interface number

Comment thread src/libusb/messenger-libusb.cpp Outdated
return rv;
}

int usb_messenger_libusb::bulk_transfer(const std::shared_ptr<usb_endpoint>& endpoint, uint8_t* buffer, uint32_t length, uint32_t timeout_ms)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PLs make the in and out length param type identical (preferably int)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returned value replaced with usb status

Comment thread src/mf/mf-backend.cpp
}

std::shared_ptr<usb_device> wmf_backend::create_usb_device(usb_device_info info) const
std::shared_ptr<command_transfer> wmf_backend::create_usb_device(usb_device_info info) const

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the param const&

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to avoid changes to the backend API at this point

Comment thread src/mock/recorder.cpp Outdated
{
auto dev = _source->create_usb_device(info);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailing WS

Comment thread src/mock/recorder.cpp
@@ -1136,7 +1136,7 @@ namespace librealsense
return _rec->load_uvc_device_info_list();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation

Comment thread src/winusb/device-winusb.cpp Outdated
for (auto&& entry : _interfaces)
{
auto i = entry.second;
if(filter == USB_SUBCLASS_ANY ||

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is multiplied several times (counted 3) - can it be refactored?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will be removed

Comment thread src/winusb/endpoint-winusb.h Outdated
virtual endpoint_type get_type() const override { return _type; }
virtual endpoint_direction get_direction() const override
{
return _address >= USB_ENDPOINT_DIRECTION_READ ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is also C&P

Comment thread src/winusb/enumerator-winusb.cpp Outdated
return false;
for (auto&& guid : guids)
{
for (auto&& id : query_by_interface(guid, L"8086"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 8086 mask may not recognize T265

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will remove the filter

Comment thread src/winusb/enumerator-winusb.cpp Outdated

for (auto&& guid : guids)
{
for (auto&& id : query_by_interface(guid, L"8086"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above


switch (devices.size())
{
case 0: printf("\nno USB device found\n\n"); break;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case will not be invoked - terminated on previous REQUIRE(devices.size());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@ev-mp ev-mp merged commit f51b203 into realsenseai:development May 16, 2019
@matkatz matkatz deleted the rsusb-api branch July 9, 2019 12:51
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