Skip to content

Commit 1e4146d

Browse files
committed
Bugfixes
1 parent ecbb167 commit 1e4146d

File tree

4 files changed

+75
-25
lines changed

4 files changed

+75
-25
lines changed

libnfc/buses/usbbus.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ int usbbus_prepare() {
8484
}
8585

8686

87-
TODO kenspeckle
88-
beim ende vom programm libusb dinge wieder freigeben
87+
//TODO kenspeckle
88+
//beim ende vom programm libusb dinge wieder freigeben
8989

90-
size_t usbbus_usb_scan(char **connstrings,
90+
size_t usbbus_usb_scan(nfc_connstring connstrings[],
9191
const size_t connstrings_len,
9292
struct usbbus_device *nfc_usb_devices,
9393
const size_t num_nfc_usb_devices,
@@ -132,13 +132,15 @@ size_t usbbus_usb_scan(char **connstrings,
132132
break;
133133
}
134134
if (!found_valid_config) {
135+
libusb_unref_device(dev);
135136
continue;
136137
}
137138
}
138139

139140
libusb_device_handle *udev;
140141
int res = libusb_open(dev, &udev);
141142
if (res < 0 && udev == NULL) {
143+
libusb_unref_device(dev);
142144
continue;
143145
}
144146

@@ -151,6 +153,7 @@ size_t usbbus_usb_scan(char **connstrings,
151153
"Unable to set USB configuration (%s)",
152154
libusb_strerror(res));
153155
libusb_close(udev);
156+
libusb_unref_device(dev);
154157
// we failed to use the device
155158
continue;
156159
}
@@ -160,25 +163,33 @@ size_t usbbus_usb_scan(char **connstrings,
160163
libusb_close(udev);
161164

162165
uint8_t dev_address = libusb_get_device_address(dev);
166+
printf("%s:%03d:%03d",
167+
"Test",
168+
dev_address,
169+
(int) valid_config_idx);
163170
size_t size_new_str = snprintf(
164171
connstrings[device_found],
165-
sizeof(nfc_connstring),
172+
sizeof(connstrings[device_found]),
166173
"%s:%03d:%03d",
167174
usb_driver_name,
168-
dev_address, (int) valid_config_idx);
175+
dev_address,
176+
(int) valid_config_idx);
177+
169178
if (size_new_str >= (int) sizeof(nfc_connstring)) {
170179
// truncation occurred, skipping that one
180+
libusb_unref_device(dev);
171181
continue;
172182
}
173183
device_found++;
174184
// Test if we reach the maximum "wanted" devices
175185
if (device_found == connstrings_len) {
186+
libusb_free_device_list(devices, 0);
176187
return device_found;
177188
}
178189
}
179190
}
180191
}
181-
192+
libusb_free_device_list(devices, 0);
182193
return device_found;
183194
}
184195

@@ -273,15 +284,15 @@ void usbbus_get_usb_device_name(struct libusb_device *dev, libusb_device_handle
273284
}
274285

275286

276-
void usbbus_get_device(uint8_t dev_address, struct libusb_device * dev, struct libusb_device_handle * dev_handle) {
287+
void usbbus_get_device(uint8_t dev_address, struct libusb_device ** dev, struct libusb_device_handle ** dev_handle) {
277288
struct libusb_device ** device_list;
278289
ssize_t num_devices = libusb_get_device_list(ctx, &device_list);
279290
for (size_t i = 0; i < num_devices; i++) {
280291
if (libusb_get_device_address(device_list[i]) != dev_address) {
281292
continue;
282293
} else {
283-
dev = device_list[i];
284-
int res = libusb_open(dev, &dev_handle);
294+
*dev = device_list[i];
295+
int res = libusb_open(*dev, dev_handle);
285296
if (res != 0 || dev_handle == NULL) {
286297
log_put(LOG_GROUP,LOG_CATEGORY,NFC_LOG_PRIORITY_ERROR,
287298
"Unable to open libusb device (%s)",libusb_strerror(res));
@@ -293,10 +304,15 @@ void usbbus_get_device(uint8_t dev_address, struct libusb_device * dev, struct l
293304
// libusb works with a reference counter which is set to 1 for each device when calling libusb_get_device_list and increased
294305
// by libusb_open. Thus we decrease the counter by 1 for all devices and only the "real" device will survive
295306
libusb_free_device_list(device_list, num_devices);
296-
297307
}
298308

299309

310+
void usbbus_close(struct libusb_device * dev, struct libusb_device_handle * dev_handle) {
311+
libusb_close(dev_handle);
312+
libusb_unref_device(dev);
313+
libusb_exit(ctx);
314+
}
315+
300316
uint16_t usbbus_get_vendor_id(struct libusb_device *dev) {
301317
struct libusb_device_descriptor descriptor;
302318
libusb_get_device_descriptor(dev, &descriptor);

libnfc/buses/usbbus.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
#define __NFC_BUS_USBBUS_H__
3131

3232
#include <libusb-1.0/libusb.h>
33+
#include <stdlib.h>
34+
#include "nfc/nfc-types.h"
3335

34-
#define EMPTY_STRING "\0";
36+
#define EMPTY_STRING ((unsigned char *)"\0")
3537

3638
struct usbbus_device {
3739
uint16_t vendor_id;
@@ -44,10 +46,11 @@ struct usbbus_device {
4446

4547
int usbbus_prepare();
4648

47-
size_t usbbus_usb_scan(char ** connstrings, size_t connstrings_len, struct usbbus_device * nfc_usb_devices, size_t num_nfc_usb_devices, char * usb_driver_name);
49+
size_t usbbus_usb_scan(nfc_connstring connstrings[], size_t connstrings_len, struct usbbus_device * nfc_usb_devices, size_t num_nfc_usb_devices, char * usb_driver_name);
4850
void usbbus_get_usb_endpoints(struct libusb_device *dev, uint8_t * endpoint_in, uint8_t * endpoint_out, uint16_t * max_packet_size);
4951
void usbbus_get_usb_device_name(struct libusb_device * dev, libusb_device_handle *udev, char *buffer, size_t len);
50-
void usbbus_get_device(uint8_t dev_address, struct libusb_device * dev, struct libusb_device_handle * dev_handle);
52+
void usbbus_get_device(uint8_t dev_address, struct libusb_device ** dev, struct libusb_device_handle ** dev_handle);
53+
void usbbus_close(struct libusb_device * dev, struct libusb_device_handle * dev_handle);
5154
uint16_t usbbus_get_vendor_id(struct libusb_device * dev);
5255
uint16_t usbbus_get_product_id(struct libusb_device * dev);
5356
int usbbus_get_num_alternate_settings(struct libusb_device *dev, uint8_t config_idx);

libnfc/drivers/acr122_usb.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const
297297
devices[i].name = acr122_usb_supported_devices[i].name;
298298
devices[i].max_packet_size = acr122_usb_supported_devices[i].max_packet_size;
299299
}
300-
return usbbus_usb_scan((char **) connstrings, connstrings_len, devices, num_acr122_usb_supported_device, ACR122_USB_DRIVER_NAME);
300+
return usbbus_usb_scan(connstrings, connstrings_len, devices, num_acr122_usb_supported_device, ACR122_USB_DRIVER_NAME);
301301
}
302302

303303

@@ -337,6 +337,8 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) {
337337
connstring_decode_level,
338338
connstring);
339339
if (connstring_decode_level < 2) {
340+
free(dev_address_str);
341+
free(config_idx_str);
340342
return NULL;
341343
}
342344

@@ -352,7 +354,8 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) {
352354
.uiEndPointIn = 0,
353355
.uiEndPointOut = 0,
354356
};
355-
usbbus_get_device(dev_addres, data.dev, data.pudh);
357+
358+
usbbus_get_device(dev_addres, &data.dev, &data.pudh);
356359
// Reset device
357360
libusb_reset_device(data.pudh);
358361

@@ -368,6 +371,8 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) {
368371
libusb_strerror(res));
369372
libusb_close(data.pudh);
370373
// we failed to use the specified device
374+
free(dev_address_str);
375+
free(config_idx_str);
371376
return NULL;
372377
}
373378

@@ -383,6 +388,8 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) {
383388
libusb_strerror(res));
384389
libusb_close(data.pudh);
385390
// we failed to use the specified device
391+
free(dev_address_str);
392+
free(config_idx_str);
386393
return NULL;
387394
}
388395
}
@@ -391,6 +398,8 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) {
391398
pnd = nfc_device_new(context, connstring);
392399
if (!pnd) {
393400
perror("malloc");
401+
free(dev_address_str);
402+
free(config_idx_str);
394403
return NULL;
395404
}
396405
acr122_usb_get_usb_device_name(data.dev, data.pudh, pnd->name, sizeof(pnd->name));
@@ -399,6 +408,8 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) {
399408
if (!pnd->driver_data) {
400409
perror("malloc");
401410
nfc_device_free(pnd);
411+
free(dev_address_str);
412+
free(config_idx_str);
402413
return NULL;
403414
}
404415
*DRIVER_DATA(pnd) = data;
@@ -407,6 +418,8 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) {
407418
if (pn53x_data_new(pnd, &acr122_usb_io) == NULL) {
408419
perror("malloc");
409420
nfc_device_free(pnd);
421+
free(dev_address_str);
422+
free(config_idx_str);
410423
return NULL;
411424
}
412425

@@ -418,10 +431,14 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) {
418431
if (acr122_usb_init(pnd) < 0) {
419432
libusb_close(data.pudh);
420433
nfc_device_free(pnd);
434+
free(dev_address_str);
435+
free(config_idx_str);
421436
return NULL;
422437
}
423438
DRIVER_DATA(pnd)->abort_flag = false;
424439

440+
free(dev_address_str);
441+
free(config_idx_str);
425442
return pnd;
426443
}
427444

@@ -439,7 +456,8 @@ acr122_usb_close(nfc_device *pnd) {
439456
libusb_strerror(res));
440457
}
441458

442-
libusb_close(DRIVER_DATA(pnd)->pudh);
459+
usbbus_close(DRIVER_DATA(pnd)->dev, DRIVER_DATA(pnd)->pudh);
460+
443461
pn53x_data_free(pnd);
444462
nfc_device_free(pnd);
445463
}

libnfc/drivers/pn53x_usb.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s
282282
devices[i].name = pn53x_usb_supported_devices[i].name;
283283
devices[i].max_packet_size = pn53x_usb_supported_devices[i].uiMaxPacketSize;
284284
}
285-
return usbbus_usb_scan((char **) connstrings, connstrings_len, devices, num_pn53x_usb_supported_devices, PN53X_USB_DRIVER_NAME);
285+
return usbbus_usb_scan(connstrings, connstrings_len, devices, num_pn53x_usb_supported_devices, PN53X_USB_DRIVER_NAME);
286286
}
287287

288288
bool
@@ -341,10 +341,7 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) {
341341
.possibly_corrupted_usbdesc = false,
342342
};
343343

344-
usbbus_get_device(dev_addres, data.dev, data.pudh);
345-
346-
347-
344+
usbbus_get_device(dev_addres, &data.dev, &data.pudh);
348345

349346
// Retrieve end points, using hardcoded defaults if available
350347
// or using the descriptors otherwise.
@@ -370,8 +367,10 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) {
370367
usbbus_get_product_id(data.dev),
371368
data.configIdx);
372369
}
373-
libusb_close(data.pudh);
374370
// we failed to use the specified device
371+
usbbus_close(data.dev, data.pudh);
372+
free(dev_address_str);
373+
free(config_idx_str);
375374
return NULL;
376375
}
377376

@@ -382,15 +381,20 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) {
382381
NFC_LOG_PRIORITY_ERROR,
383382
"Unable to claim USB interface (%s)",
384383
libusb_strerror(res));
385-
libusb_close(data.pudh);
386384
// we failed to use the specified device
385+
usbbus_close(data.dev, data.pudh);
386+
free(dev_address_str);
387+
free(config_idx_str);
387388
return NULL;
388389
}
389390
data.model = pn53x_usb_get_device_model(usbbus_get_vendor_id(data.dev), usbbus_get_product_id(data.dev));
390391
// Allocate memory for the device info and specification, fill it and return the info
391392
pnd = nfc_device_new(context, connstring);
392393
if (!pnd) {
393394
perror("malloc");
395+
usbbus_close(data.dev, data.pudh);
396+
free(dev_address_str);
397+
free(config_idx_str);
394398
return NULL;
395399
}
396400
pn53x_usb_get_usb_device_name(data.dev, data.pudh, pnd->name, sizeof(pnd->name));
@@ -399,6 +403,9 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) {
399403
if (!pnd->driver_data) {
400404
perror("malloc");
401405
nfc_device_free(pnd);
406+
usbbus_close(data.dev, data.pudh);
407+
free(dev_address_str);
408+
free(config_idx_str);
402409
return NULL;
403410
}
404411
*DRIVER_DATA(pnd) = data;
@@ -407,6 +414,9 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) {
407414
if (pn53x_data_new(pnd, &pn53x_usb_io) == NULL) {
408415
perror("malloc");
409416
nfc_device_free(pnd);
417+
usbbus_close(data.dev, data.pudh);
418+
free(dev_address_str);
419+
free(config_idx_str);
410420
return NULL;
411421
}
412422

@@ -436,8 +446,10 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) {
436446
// HACK2: Then send a GetFirmware command to resync USB toggle bit between host & device
437447
// in case host used set_configuration and expects the device to have reset its toggle bit, which PN53x doesn't do
438448
if (pn53x_usb_init(pnd) < 0) {
439-
libusb_close(data.pudh);
440449
nfc_device_free(pnd);
450+
usbbus_close(data.dev, data.pudh);
451+
free(dev_address_str);
452+
free(config_idx_str);
441453
return NULL;
442454
}
443455
DRIVER_DATA(pnd)->abort_flag = false;
@@ -460,7 +472,8 @@ pn53x_usb_close(nfc_device *pnd) {
460472

461473
pn53x_idle(pnd);
462474

463-
libusb_close(DRIVER_DATA(pnd)->pudh);
475+
usbbus_close(DRIVER_DATA(pnd)->dev, DRIVER_DATA(pnd)->pudh);
476+
464477
pn53x_data_free(pnd);
465478
nfc_device_free(pnd);
466479
}

0 commit comments

Comments
 (0)