From a93c66e2bff0065ad2ac93998048f065e51a784c Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Tue, 8 Sep 2009 20:42:00 +0200 Subject: Protocol definition updated. renamed: usbmuxd_scan() -> usbmuxd_get_device_list() new: usbmuxd_get_device_by_uuid() --- libusbmuxd/libusbmuxd.c | 47 +++++++++++++++++++++++++++++++++++++++++------ libusbmuxd/usbmuxd.h | 24 ++++++++++++++++++++---- tools/iproxy.c | 2 +- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c index 584e930..a00ceb0 100644 --- a/libusbmuxd/libusbmuxd.c +++ b/libusbmuxd/libusbmuxd.c @@ -296,7 +296,7 @@ int usbmuxd_unsubscribe() return 0; } -int usbmuxd_scan(usbmuxd_device_info_t ** available_devices) +int usbmuxd_get_device_list(usbmuxd_device_info_t **device_list) { struct usbmuxd_listen_request s_req; int sfd; @@ -340,7 +340,7 @@ int usbmuxd_scan(usbmuxd_device_info_t ** available_devices) return -1; } - *available_devices = NULL; + *device_list = NULL; // receive device list while (1) { if (recv_buf_timeout(sfd, &hdr, sizeof(hdr), 0, 1000) == sizeof(hdr)) { @@ -362,7 +362,7 @@ int usbmuxd_scan(usbmuxd_device_info_t ** available_devices) fprintf(stderr, "%s: received less data than specified in header!\n", __func__); } else { - newlist = (usbmuxd_device_info_t *) realloc(*available_devices, sizeof(usbmuxd_device_info_t) * (dev_cnt + 1)); + newlist = (usbmuxd_device_info_t *) realloc(*device_list, sizeof(usbmuxd_device_info_t) * (dev_cnt + 1)); if (newlist) { newlist[dev_cnt].handle = (int) dev_info.device_id; @@ -373,7 +373,7 @@ int usbmuxd_scan(usbmuxd_device_info_t ** available_devices) memcpy(newlist[dev_cnt].uuid, dev_info.serial_number, sizeof(newlist[dev_cnt].uuid)); - *available_devices = newlist; + *device_list = newlist; dev_cnt++; } else { fprintf(stderr, @@ -390,13 +390,48 @@ int usbmuxd_scan(usbmuxd_device_info_t ** available_devices) } // terminating zero record - newlist = (usbmuxd_device_info_t*) realloc(*available_devices, sizeof(usbmuxd_device_info_t) * (dev_cnt + 1)); + newlist = (usbmuxd_device_info_t*) realloc(*device_list, sizeof(usbmuxd_device_info_t) * (dev_cnt + 1)); memset(newlist + dev_cnt, 0, sizeof(usbmuxd_device_info_t)); - *available_devices = newlist; + *device_list = newlist; return dev_cnt; } +int usbmuxd_get_device_by_uuid(const char *uuid, usbmuxd_device_info_t *device) +{ + usbmuxd_device_info_t *dev_list = NULL; + + if (!device) { + return -EINVAL; + } + if (usbmuxd_get_device_list(&dev_list) < 0) { + return -ENODEV; + } + + int i; + int result = 0; + for (i = 0; dev_list[i].handle > 0; i++) { + if (!uuid) { + device->handle = dev_list[i].handle; + device->product_id = dev_list[i].product_id; + strcpy(device->uuid, dev_list[i].uuid); + result = 1; + break; + } + if (!strcmp(uuid, dev_list[i].uuid)) { + device->handle = dev_list[i].handle; + device->product_id = dev_list[i].product_id; + strcpy(device->uuid, dev_list[i].uuid); + result = 1; + break; + } + } + + free(dev_list); + + return result; +} + int usbmuxd_connect(const int handle, const unsigned short port) { int sfd; diff --git a/libusbmuxd/usbmuxd.h b/libusbmuxd/usbmuxd.h index a4e7e4a..106f921 100644 --- a/libusbmuxd/usbmuxd.h +++ b/libusbmuxd/usbmuxd.h @@ -57,12 +57,28 @@ int usbmuxd_unsubscribe(); /** * Contacts usbmuxd and retrieves a list of connected devices. * - * @param available_devices pointer to an array of usbmuxd_device_info_t - * that will hold records of the connected devices. + * @param device_list A pointer to an array of usbmuxd_device_info_t + * that will hold records of the connected devices. The last record + * is a null-terminated record with all fields set to 0/NULL. + * @note The user has to free the list returned. * - * @return number of available devices, zero on no devices, or negative on error + * @return number of attached devices, zero on no devices, or negative + * if an error occured. */ -int usbmuxd_scan(usbmuxd_device_info_t **available_devices); +int usbmuxd_get_device_list(usbmuxd_device_info_t **device_list); + +/** + * Gets device information for the device specified by uuid. + * + * @param uuid A device uuid of the device to look for. If uuid is NULL, + * This function will return the first device found. + * @param device Pointer to a previously allocated (or static) + * usbmuxd_device_info_t that will be filled with the device info. + * + * @return 0 if no matching device is connected, 1 if the device was found, + * or a negative value on error. + */ +int usbmuxd_get_device_by_uuid(const char *uuid, usbmuxd_device_info_t *device); /** * Request proxy connect to diff --git a/tools/iproxy.c b/tools/iproxy.c index f10d701..094ae75 100644 --- a/tools/iproxy.c +++ b/tools/iproxy.c @@ -153,7 +153,7 @@ void *acceptor_thread(void *arg) cdata = (struct client_data*)arg; - if ((count = usbmuxd_scan(&dev_list)) < 0) { + if ((count = usbmuxd_get_device_list(&dev_list)) < 0) { printf("Connecting to usbmuxd failed, terminating.\n"); free(dev_list); return NULL; -- cgit v1.1-32-gdbae