|
16 | 16 |
|
17 | 17 | #include "UsbCamera.h" |
18 | 18 |
|
| 19 | +#include <cstring> |
19 | 20 | #include <stdexcept> |
20 | 21 |
|
21 | 22 | namespace tis |
@@ -109,8 +110,68 @@ bool UsbCamera::open() |
109 | 110 | { |
110 | 111 | try |
111 | 112 | { |
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); |
114 | 175 |
|
115 | 176 | if (this->dev_handle == NULL) |
116 | 177 | { |
|
0 commit comments