Adapted iphoneinfo and iphonesyslog to new API to allow device selection Added iphone_id tool to list devices attached and retrieve device name.

Signed-off-by: Matt Colyer <matt@colyer.name>
diff --git a/dev/Makefile.am b/dev/Makefile.am
index 3fa8f8c..9e06339 100644
--- a/dev/Makefile.am
+++ b/dev/Makefile.am
@@ -3,7 +3,7 @@
 AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS)
 AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS)
 
-bin_PROGRAMS = iphoneclient lckd-client afccheck msyncclient iphoneinfo iphonesyslog
+bin_PROGRAMS = iphoneclient lckd-client afccheck msyncclient iphoneinfo iphonesyslog iphone_id
 
 iphoneclient_SOURCES = main.c
 iphoneclient_LDADD = ../src/libiphone.la
@@ -32,3 +32,8 @@
 iphonesyslog_CFLAGS = $(AM_CFLAGS)
 iphonesyslog_LDFLAGS = $(AM_LDFLAGS)
 iphonesyslog_LDADD = ../src/libiphone.la
+
+iphone_id_SOURCES = iphone_id.c
+iphone_id_CFLAGS = $(AM_CFLAGS)
+iphone_id_LDFLAGS = $(AM_LDFLAGS)
+iphone_id_LDADD = ../src/libiphone.la
diff --git a/dev/iphone_id.c b/dev/iphone_id.c
new file mode 100644
index 0000000..c191608
--- /dev/null
+++ b/dev/iphone_id.c
@@ -0,0 +1,93 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <getopt.h>
+#include <libiphone/libiphone.h>
+#include <usbmuxd.h>
+
+static void usage()
+{
+	printf("usage: iphone_id <device_uuid>\n"
+		"\tdevice_uuid is the 40-digit hexadecimal UUID of the device\n"
+		"\tfor which the name should be retrieved.\n\n"
+		"usage: iphone_id -l\n"
+		"\tList all attached devices.\n");
+	exit(0);
+}
+
+int main(int argc, char **argv)
+{
+	iphone_device_t phone = NULL;
+	iphone_lckd_client_t control = NULL;
+	usbmuxd_scan_result *dev_list;
+	char *devname = NULL;
+	int ret = 0;
+	int c;
+	int i;
+	int opt_list = 0;
+
+	while ((c = getopt(argc, argv, "lh")) != -1) {
+		switch (c) {
+		    case 'l':
+			opt_list = 1;
+			break;
+		    case 'h':
+		    default:
+			usage();
+		}
+	}
+
+	if (argc < 2) {
+		usage();
+	}
+
+	argc -= optind;
+	argv += optind;
+
+	if ((!opt_list) && (strlen(argv[0]) != 40)) {
+		usage();
+	}
+
+	if (opt_list) {
+		if (usbmuxd_scan(&dev_list) < 0) {
+			fprintf(stderr, "ERROR: usbmuxd is not running!\n");
+			return -1;
+		}
+		for (i = 0; dev_list[i].handle > 0; i++) {
+			printf("handle=%d product_id=%04x uuid=%s\n", dev_list[i].handle, dev_list[i].product_id, dev_list[i].serial_number);
+		}
+		return 0;
+	}
+
+	iphone_set_debug(0);
+
+	iphone_get_device_by_uuid(&phone, argv[0]);
+	if (!phone) {
+		fprintf(stderr, "ERROR: No device with UUID=%s attached.\n", argv[0]);
+		return -2;
+	}
+
+	if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
+		iphone_free_device(phone);
+		fprintf(stderr, "ERROR: Connecting to device failed!\n");
+		return -2;
+	}
+
+	if ((IPHONE_E_SUCCESS != lockdownd_get_device_name(control, &devname)) || !devname) {
+		fprintf(stderr, "ERROR: Could not get device name!\n");
+		ret = -2;
+	}
+
+	iphone_lckd_free_client(control);
+	iphone_free_device(phone);
+
+	if (ret == 0) {
+		printf("%s\n", devname);
+	}
+
+	if (devname) {
+		free(devname);
+	}
+
+	return ret;
+}
diff --git a/dev/iphoneinfo.c b/dev/iphoneinfo.c
index 409ad2d..c28eb9e 100644
--- a/dev/iphoneinfo.c
+++ b/dev/iphoneinfo.c
@@ -22,7 +22,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <errno.h>
-#include <usb.h>
 
 #include <libiphone/libiphone.h>
 
@@ -37,7 +36,8 @@
 	iphone_device_t phone = NULL;
 	iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
 	int i;
-	int bus_n = -1, dev_n = -1;
+	char uuid[41];
+	uuid[0] = 0;
 
 	/* parse cmdline args */
 	for (i = 1; i < argc; i++) {
@@ -45,11 +45,13 @@
 			iphone_set_debug_mask(DBGMASK_ALL);
 			continue;
 		}
-		else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--usb")) {
-			if (sscanf(argv[++i], "%d,%d", &bus_n, &dev_n) < 2) {
+		else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) {
+			i++;
+			if (!argv[i] || (strlen(argv[i]) != 40)) {
 				print_usage(argc, argv);
 				return 0;
 			}
+			strcpy(uuid, argv[i]);
 			continue;
 		}
 		else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
@@ -62,21 +64,21 @@
 		}
 	}
 
-/*	if (bus_n != -1) {
-		ret = iphone_get_specific_device(bus_n, dev_n, &phone);
+	if (uuid[0] != 0) {
+		ret = iphone_get_device_by_uuid(&phone, uuid);
 		if (ret != IPHONE_E_SUCCESS) {
-			printf("No device found for usb bus %d and dev %d, is it plugged in?\n", bus_n, dev_n);
+			printf("No device found with uuid %s, is it plugged in?\n", uuid);
 			return -1;
 		}
 	}
 	else
-	{*/
+	{
 		ret = iphone_get_device(&phone);
 		if (ret != IPHONE_E_SUCCESS) {
 			printf("No device found, is it plugged in?\n");
 			return -1;
 		}
-/*	}*/
+	}
 
 	if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
 		iphone_free_device(phone);
@@ -108,7 +110,7 @@
 	printf("Usage: %s [OPTIONS]\n", (strrchr(argv[0], '/') + 1));
 	printf("Show information about the first connected iPhone/iPod Touch.\n\n");
 	printf("  -d, --debug\t\tenable communication debugging\n");
-	printf("  -u, --usb=BUS,DEV\ttarget specific device by usb bus/dev number\n");
+	printf("  -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n");
 	printf("  -h, --help\t\tprints usage information\n");
 	printf("\n");
 }
diff --git a/dev/syslog_relay.c b/dev/syslog_relay.c
index 6fc981b..3407f2f 100644
--- a/dev/syslog_relay.c
+++ b/dev/syslog_relay.c
@@ -24,7 +24,6 @@
 #include <errno.h>
 #include <netinet/in.h>
 #include <signal.h>
-#include <usb.h>
 
 #include <libiphone/libiphone.h>
 #include <usbmuxd.h>
@@ -48,8 +47,9 @@
 	iphone_device_t phone = NULL;
 	iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
 	int i;
-	int bus_n = -1, dev_n = -1;
+	char uuid[41];
 	int port = 0;
+	uuid[0] = 0;
 
 	signal(SIGINT, clean_exit);
 	signal(SIGQUIT, clean_exit);
@@ -62,11 +62,13 @@
 			iphone_set_debug_mask(DBGMASK_ALL);
 			continue;
 		}
-		else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--usb")) {
-			if (sscanf(argv[++i], "%d,%d", &bus_n, &dev_n) < 2) {
+		else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) {
+			i++;
+			if (!argv[i] || (strlen(argv[i]) != 40)) {
 				print_usage(argc, argv);
 				return 0;
 			}
+			strcpy(uuid, argv[i]);
 			continue;
 		}
 		else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
@@ -79,21 +81,21 @@
 		}
 	}
 
-/*	if (bus_n != -1) {
-		ret = iphone_get_specific_device(bus_n, dev_n, &phone);
+	if (uuid[0] != 0) {
+		ret = iphone_get_device_by_uuid(&phone, uuid);
 		if (ret != IPHONE_E_SUCCESS) {
-			printf("No device found for usb bus %d and dev %d, is it plugged in?\n", bus_n, dev_n);
+			printf("No device found with uuid %s, is it plugged in?\n", uuid);
 			return -1;
 		}
 	}
 	else
-	{*/
+	{
 		ret = iphone_get_device(&phone);
 		if (ret != IPHONE_E_SUCCESS) {
 			printf("No device found, is it plugged in?\n");
 			return -1;
 		}
-/*	}*/
+	}
 
 	if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
 		iphone_free_device(phone);
@@ -152,7 +154,7 @@
 	printf("Usage: %s [OPTIONS]\n", (strrchr(argv[0], '/') + 1));
 	printf("Relay syslog of a connected iPhone/iPod Touch.\n\n");
 	printf("  -d, --debug\t\tenable communication debugging\n");
-	printf("  -u, --usb=BUS,DEV\ttarget specific device by usb bus/dev number\n");
+	printf("  -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n");
 	printf("  -h, --help\t\tprints usage information\n");
 	printf("\n");
 }