|
9 | 9 | #include <cstdio> |
10 | 10 | #include <cstdlib> |
11 | 11 | #include <cstring> |
| 12 | +#include <iomanip> |
| 13 | +#include <list> |
12 | 14 | #include <vector> |
13 | 15 | #include <string> |
| 16 | +#include <sstream> // For std::stringstream |
14 | 17 | #include <stdexcept> |
15 | 18 |
|
16 | 19 | #include "cable.hpp" |
@@ -101,21 +104,46 @@ int libusb_ll::get_devices_list(const cable_t *cable) |
101 | 104 | return static_cast<int>(_usb_dev_list.size()); |
102 | 105 | } |
103 | 106 |
|
| 107 | +struct cable_details_t { |
| 108 | + uint8_t bus; |
| 109 | + uint8_t device; |
| 110 | + uint16_t vid; |
| 111 | + uint16_t pid; |
| 112 | + std::string probe; |
| 113 | + std::string manufacturer; |
| 114 | + std::string serial; |
| 115 | + std::string product; |
| 116 | + cable_details_t(uint8_t& b, uint8_t& d, |
| 117 | + uint16_t& v, uint16_t& p, |
| 118 | + std::string prb, std::string m, |
| 119 | + std::string s, std::string prd): |
| 120 | + bus(b), device(d), vid(v), pid(p), |
| 121 | + probe(prb), manufacturer(m), |
| 122 | + serial(s), product(prd) {} |
| 123 | +}; |
| 124 | +std::string formatHex(uint16_t c, int len) { |
| 125 | + std::stringstream ss; |
| 126 | + ss << "0x"; |
| 127 | + ss << std::hex << std::setfill('0') << std::setw(len) |
| 128 | + << (static_cast<unsigned int>(static_cast<unsigned short>(c)) & 0xFFFF); |
| 129 | + return ss.str(); |
| 130 | +} |
| 131 | + |
| 132 | +std::string formatDec(char c, int len) { |
| 133 | + std::stringstream ss; |
| 134 | + ss << std::setfill('0') << std::setw(len) << std::to_string(c); |
| 135 | + return ss.str(); |
| 136 | +} |
| 137 | + |
104 | 138 | bool libusb_ll::scan() |
105 | 139 | { |
106 | | - char *mess = reinterpret_cast<char *>(malloc(1024)); |
107 | | - if (!mess) { |
108 | | - printError("Error: failed to allocate buffer"); |
109 | | - return false; |
110 | | - } |
| 140 | + std::list<cable_details_t> list_cables; |
| 141 | + size_t manufacturer_len = 12; |
| 142 | + size_t probe_len = 10; |
| 143 | + size_t serial_len = 6; |
111 | 144 |
|
112 | 145 | get_devices_list(nullptr); |
113 | 146 |
|
114 | | - snprintf(mess, 1024, "%3s %3s %-13s %-15s %-12s %-20s %s", |
115 | | - "Bus", "device", "vid:pid", "probe type", "manufacturer", |
116 | | - "serial", "product"); |
117 | | - printSuccess(mess); |
118 | | - |
119 | 147 | for (libusb_device *usb_dev : _usb_dev_list) { |
120 | 148 | bool found = false; |
121 | 149 | struct libusb_device_descriptor desc; |
@@ -171,6 +199,7 @@ bool libusb_ll::scan() |
171 | 199 | libusb_device_handle *handle; |
172 | 200 | int ret = libusb_open(usb_dev, &handle); |
173 | 201 | if (ret != 0) { |
| 202 | + char mess[1024]; |
174 | 203 | snprintf(mess, 1024, |
175 | 204 | "Error: can't open device with vid:vid = 0x%04x:0x%04x. " |
176 | 205 | "Error code %d %s", |
@@ -198,17 +227,50 @@ bool libusb_ll::scan() |
198 | 227 | uint8_t bus_addr = libusb_get_bus_number(usb_dev); |
199 | 228 | uint8_t dev_addr = libusb_get_device_address(usb_dev); |
200 | 229 |
|
201 | | - snprintf(mess, 1024, "%03d %03d 0x%04x:0x%04x %-15s %-12s %-20s %s", |
202 | | - bus_addr, dev_addr, |
203 | | - desc.idVendor, desc.idProduct, |
204 | | - probe_type, imanufacturer, iserial, iproduct); |
| 230 | + list_cables.emplace_back(cable_details_t( |
| 231 | + bus_addr, dev_addr, desc.idVendor, desc.idProduct, |
| 232 | + std::string(probe_type), std::string((const char *)imanufacturer), |
| 233 | + std::string((const char *)iserial), std::string((const char *)iproduct))); |
205 | 234 |
|
206 | | - printInfo(mess); |
| 235 | + if (strlen((const char *)imanufacturer) > manufacturer_len) |
| 236 | + manufacturer_len = strlen((const char *)imanufacturer); |
| 237 | + if (strlen((const char *)probe_type) > probe_len) |
| 238 | + probe_len = strlen((const char *)probe_type); |
| 239 | + if (strlen((const char *)iserial) > serial_len) |
| 240 | + serial_len = strlen((const char *)iserial); |
207 | 241 |
|
208 | 242 | libusb_close(handle); |
209 | 243 | } |
210 | 244 |
|
211 | | - free(mess); |
| 245 | + manufacturer_len++; |
| 246 | + serial_len++; |
| 247 | + probe_len++; |
| 248 | + |
| 249 | + std::stringstream buffer; |
| 250 | + buffer << std::left |
| 251 | + << std::setw(4) << "Bus" |
| 252 | + << std::setw(7) << "device" |
| 253 | + << std::setw(14) << "vid:pid" |
| 254 | + << std::setw(probe_len) << "probe type" |
| 255 | + << std::setw(manufacturer_len) << "manufacturer" |
| 256 | + << std::setw(serial_len) << "serial" |
| 257 | + << "product"; |
| 258 | + printSuccess(buffer.str()); |
| 259 | + |
| 260 | + for (const auto& cable : list_cables) { |
| 261 | + std::stringstream buffer; |
| 262 | + buffer << std::left // Left-align all fields |
| 263 | + << std::setw(4) << formatDec(cable.bus, 3) |
| 264 | + << std::setw(7) << formatDec(cable.device, 3) |
| 265 | + << std::setw(14) |
| 266 | + << (formatHex(cable.vid, 4) + ":" + formatHex(cable.pid, 4)) |
| 267 | + << std::setw(probe_len) << cable.probe |
| 268 | + << std::setw(manufacturer_len) << cable.manufacturer |
| 269 | + << std::setw(serial_len) << cable.serial |
| 270 | + << cable.product; |
| 271 | + |
| 272 | + printInfo(buffer.str()); |
| 273 | + } |
212 | 274 |
|
213 | 275 | return true; |
214 | 276 | } |
0 commit comments