Skip to content

Commit 3cb8890

Browse files
committed
implement fnusb_get_serial()
Signed-off-by: Benn Snyder <[email protected]>
1 parent 3ee9794 commit 3cb8890

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/usb_libusb10.c

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,15 @@ FN_INTERNAL libusb_device * fnusb_find_sibling_device(freenect_context* ctx, lib
151151
return NULL;
152152
}
153153

154-
FN_INTERNAL char* fnusb_get_serial(freenect_context* ctx, libusb_device* device, libusb_device_handle* handle)
154+
FN_INTERNAL char* usb_get_serial(freenect_context* ctx, libusb_device* device, libusb_device_handle* handle)
155155
{
156156
if (ctx == NULL) return NULL;
157157

158-
if (device == NULL && handle != NULL) {
158+
if (device == NULL) {
159+
if (handle == NULL) {
160+
FN_WARNING("No handle or device for serial\n");
161+
return NULL;
162+
}
159163
device = libusb_get_device(handle); // no need to free or unref
160164
}
161165

@@ -170,6 +174,7 @@ FN_INTERNAL char* fnusb_get_serial(freenect_context* ctx, libusb_device* device,
170174

171175
// Verify that a serial number exists to query. If not, don't touch the device.
172176
if (desc.iSerialNumber == 0) {
177+
FN_WARNING("Device has no serial number\n");
173178
return NULL;
174179
}
175180

@@ -184,7 +189,6 @@ FN_INTERNAL char* fnusb_get_serial(freenect_context* ctx, libusb_device* device,
184189
handle = localHandle;
185190
}
186191

187-
// Read string descriptor referring to serial number.
188192
unsigned char serial[256]; // String descriptors are at most 256 bytes.
189193
res = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, serial, 256);
190194

@@ -197,7 +201,17 @@ FN_INTERNAL char* fnusb_get_serial(freenect_context* ctx, libusb_device* device,
197201
return NULL;
198202
}
199203

200-
return strdup(serial);
204+
const char* const K4W_1473_SERIAL = "0000000000000000";
205+
if (strncmp((const char*)serial, K4W_1473_SERIAL, 16) == 0) {
206+
return NULL; // K4W and 1473 provide an empty serial; more easily handled as NULL.
207+
}
208+
209+
return strndup((const char*)serial, sizeof(serial));
210+
}
211+
212+
FN_INTERNAL char* fnusb_get_serial(fnusb_dev* device)
213+
{
214+
return usb_get_serial(device->parent->parent, device->dev, device->dev_handle);
201215
}
202216

203217
FN_INTERNAL int fnusb_list_device_attributes(freenect_context *ctx, struct freenect_device_attributes** attribute_list)
@@ -232,16 +246,13 @@ FN_INTERNAL int fnusb_list_device_attributes(freenect_context *ctx, struct freen
232246
continue;
233247
}
234248

235-
const char* serial = fnusb_get_serial(ctx, camera_device, NULL);
236-
237-
// K4W and 1473 don't provide a camera serial; use audio serial instead.
238-
const char* const K4W_1473_SERIAL = "0000000000000000";
239-
if (serial == NULL || strncmp((const char*)serial, K4W_1473_SERIAL, 16) == 0)
249+
char* serial = usb_get_serial(ctx, camera_device, NULL);
250+
if (serial == NULL)
240251
{
241252
libusb_device* audio_device = fnusb_find_sibling_device(ctx, camera_device, devs, count, &fnusb_is_audio);
242253
if (audio_device != NULL)
243254
{
244-
const char* audio_serial = fnusb_get_serial(ctx, audio_device, NULL);
255+
char* audio_serial = usb_get_serial(ctx, audio_device, NULL);
245256
if (audio_serial) {
246257
free(serial);
247258
serial = audio_serial;

src/usb_libusb10.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ typedef struct {
7676
} fnusb_isoc_stream;
7777

7878
int fnusb_num_devices(freenect_context *ctx);
79+
char* fnusb_get_serial(fnusb_dev* dev);
7980
int fnusb_list_device_attributes(freenect_context *ctx, struct freenect_device_attributes** attribute_list);
8081

8182
int fnusb_init(fnusb_ctx *ctx, freenect_usb_context *usb_ctx);

0 commit comments

Comments
 (0)