Skip to content

Commit 6844146

Browse files
committed
Fix GlasgowSWDInterface serial number discovery.
1 parent e19e380 commit 6844146

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

GlasgowSWDInterface.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,16 @@ string GlasgowSWDInterface::GetSerialNumber(int index)
118118
{
119119
int ret;
120120
int curr_index = 0;
121-
char device_serial[64] = {};
121+
string serial;
122122

123123
ForEachGlasgowDevice([&](libusb_device_descriptor *desc, libusb_device_handle *handle) {
124124
if(curr_index == index) {
125+
char serial_array[64] = {};
125126
if((ret = libusb_get_string_descriptor_ascii(handle, desc->iSerialNumber,
126-
(uint8_t *)device_serial, sizeof(device_serial))) != 0) {
127-
strncpy(device_serial, "(error)", sizeof(device_serial));
127+
(uint8_t *)serial_array, sizeof(serial_array))) < 0) {
128+
serial = string("(error: ") + libusb_error_name(ret) + ")";
129+
} else {
130+
serial = string(serial_array, ret);
128131
}
129132

130133
return true;
@@ -134,7 +137,7 @@ string GlasgowSWDInterface::GetSerialNumber(int index)
134137
}
135138
});
136139

137-
return device_serial;
140+
return serial;
138141
}
139142

140143
string GlasgowSWDInterface::GetDescription(int index)
@@ -216,15 +219,15 @@ GlasgowSWDInterface::GlasgowSWDInterface(const string& serial)
216219

217220
char device_serial[64];
218221
if((ret = libusb_get_string_descriptor_ascii(device_handle, device_desc.iSerialNumber,
219-
(uint8_t *)device_serial, sizeof(device_serial))) != 0) {
222+
(uint8_t *)device_serial, sizeof(device_serial))) < 0) {
220223
LogError("Cannot get serial number for Glasgow device %03d:%03d\n",
221224
libusb_get_bus_number(device),
222225
libusb_get_port_number(device));
223226
libusb_close(device_handle);
224227
continue;
225228
}
226229

227-
if(serial == "" || serial == device_serial) {
230+
if(serial == "" || serial == string(device_serial, ret)) {
228231
m_device = libusb_ref_device(device);
229232
m_handle = device_handle;
230233
m_serial = device_serial;

0 commit comments

Comments
 (0)