Skip to content

Commit d1a6df0

Browse files
committed
User serial when iteratung usb-devices for opening dev instance
1 parent 018cb3a commit d1a6df0

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Apply firmware
2525
```
2626
tcam-firmware-update -ud <serial number> -f firmware-file
2727
```
28+
It is recommended to have only one camera attached to the system when updating the firmware.
2829

2930
Switch camera to UVC/proprietary mode
3031

src/UsbCamera.cpp

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "UsbCamera.h"
1818

19+
#include <cstring>
1920
#include <stdexcept>
2021

2122
namespace tis
@@ -109,8 +110,68 @@ bool UsbCamera::open()
109110
{
110111
try
111112
{
112-
this->dev_handle = libusb_open_device_with_vid_pid(
113-
usb_session->get_session(), dev.idVendor, dev.idProduct);
113+
// List all devices.
114+
libusb_device** devs;
115+
int cnt = libusb_get_device_list(usb_session->get_session(), &devs);
116+
if (cnt < 0)
117+
{
118+
throw std::runtime_error("Unable to retrieve device list. " + std::to_string(cnt));
119+
}
120+
121+
// Look for the device with matching serial number and store its device
122+
// handle.
123+
for (ssize_t i = 0; i < cnt; i++)
124+
{
125+
// Get device descriptor for current device in list.
126+
libusb_device_descriptor desc;
127+
int r = libusb_get_device_descriptor(devs[i], &desc);
128+
if (r < 0)
129+
{
130+
throw std::runtime_error("Unable to retrieve device descriptor. "
131+
+ std::to_string(r));
132+
}
133+
134+
// Ignore device if not from TIS or otherwise needed.
135+
if (desc.idVendor != 0x199e && desc.idVendor != 0xeb1a && desc.idVendor != 0x04b4)
136+
{
137+
continue;
138+
}
139+
// Get device handle.
140+
libusb_device_handle* dh;
141+
r = libusb_open(devs[i], &dh);
142+
if (r < 0)
143+
{
144+
throw std::runtime_error("Unable to open device. " + std::to_string(r));
145+
}
146+
147+
// Get ASCII representation of device serial number.
148+
char serial[256];
149+
r = libusb_get_string_descriptor_ascii(dh,
150+
desc.iSerialNumber,
151+
(unsigned char*)serial,
152+
sizeof(serial));
153+
if (r < 0)
154+
{
155+
throw std::runtime_error(
156+
"Unable to get ASCII representation of device serial number. "
157+
+ std::to_string(r));
158+
}
159+
160+
// Ignore device if it does not have the expected serial number.
161+
if (strcmp(serial, dev.serial) != 0)
162+
{
163+
// Close device and continue to next device in list.
164+
libusb_close(dh);
165+
continue;
166+
}
167+
168+
// Device handle of desired camera has been retrieved successfully, assign
169+
// to member variable and break loop.
170+
this->dev_handle = dh;
171+
break;
172+
}
173+
// Free device list.
174+
libusb_free_device_list(devs, 1);
114175

115176
if (this->dev_handle == NULL)
116177
{

0 commit comments

Comments
 (0)