summaryrefslogtreecommitdiffstats
path: root/tools/iphone_id.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/iphone_id.c')
-rw-r--r--tools/iphone_id.c130
1 files changed, 72 insertions, 58 deletions
diff --git a/tools/iphone_id.c b/tools/iphone_id.c
index f68fc8b..835e214 100644
--- a/tools/iphone_id.c
+++ b/tools/iphone_id.c
@@ -1,19 +1,27 @@
1#include <stdio.h> 1#include <stdio.h>
2#include <string.h> 2#include <string.h>
3#include <stdlib.h> 3#include <stdlib.h>
4#include <getopt.h>
5#include <libiphone/libiphone.h> 4#include <libiphone/libiphone.h>
6#include <libiphone/lockdown.h> 5#include <libiphone/lockdown.h>
7#include <usbmuxd.h> 6#include <usbmuxd.h>
8 7
9static void usage() 8#define MODE_NONE 0
9#define MODE_SHOW_ID 1
10#define MODE_LIST_DEVICES 2
11
12static void print_usage(int argc, char **argv)
10{ 13{
11 printf("usage: iphone_id <device_uuid>\n" 14 char *name = NULL;
12 "\tdevice_uuid is the 40-digit hexadecimal UUID of the device\n" 15
13 "\tfor which the name should be retrieved.\n\n" 16 name = strrchr(argv[0], '/');
14 "usage: iphone_id -l\n" 17 printf("Usage: %s [OPTIONS] [UUID]\n", (name ? name + 1: argv[0]));
15 "\tList all attached devices.\n"); 18 printf("Prints device name or a list of attached iPhone/iPod Touch devices.\n\n");
16 exit(0); 19 printf(" The UUID is a 40-digit hexadecimal number of the device\n");
20 printf(" for which the name should be retrieved.\n\n");
21 printf(" -l, --list\t\tlist all attached devices\n");
22 printf(" -d, --debug\t\tenable communication debugging\n");
23 printf(" -h, --help\t\tprints usage information\n");
24 printf("\n");
17} 25}
18 26
19int main(int argc, char **argv) 27int main(int argc, char **argv)
@@ -23,33 +31,71 @@ int main(int argc, char **argv)
23 usbmuxd_scan_result *dev_list; 31 usbmuxd_scan_result *dev_list;
24 char *devname = NULL; 32 char *devname = NULL;
25 int ret = 0; 33 int ret = 0;
26 int c;
27 int i; 34 int i;
28 int opt_list = 0; 35 int mode = MODE_SHOW_ID;
36 char uuid[41];
37 uuid[0] = 0;
29 38
30 while ((c = getopt(argc, argv, "lh")) != -1) { 39 /* parse cmdline args */
31 switch (c) { 40 for (i = 1; i < argc; i++) {
32 case 'l': 41 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
33 opt_list = 1; 42 iphone_set_debug_mask(DBGMASK_ALL);
34 break; 43 iphone_set_debug_level(1);
35 case 'h': 44 continue;
36 default: 45 }
37 usage(); 46 else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list")) {
47 mode = MODE_LIST_DEVICES;
48 continue;
49 }
50 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
51 print_usage(argc, argv);
52 return 0;
38 } 53 }
39 } 54 }
40 55
41 if (argc < 2) { 56 /* check if uuid was passed */
42 usage(); 57 if (mode == MODE_SHOW_ID) {
58 i--;
59 if (!argv[i] || (strlen(argv[i]) != 40)) {
60 print_usage(argc, argv);
61 return 0;
62 }
63 strcpy(uuid, argv[i]);
43 } 64 }
44 65
45 argc -= optind; 66 switch (mode) {
46 argv += optind; 67 case MODE_SHOW_ID:
68 iphone_get_device_by_uuid(&phone, uuid);
69 if (!phone) {
70 fprintf(stderr, "ERROR: No device with UUID=%s attached.\n", uuid);
71 return -2;
72 }
47 73
48 if ((!opt_list) && (strlen(argv[0]) != 40)) { 74 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client)) {
49 usage(); 75 iphone_device_free(phone);
50 } 76 fprintf(stderr, "ERROR: Connecting to device failed!\n");
77 return -2;
78 }
79
80 if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &devname)) || !devname) {
81 fprintf(stderr, "ERROR: Could not get device name!\n");
82 ret = -2;
83 }
84
85 lockdownd_client_free(client);
86 iphone_device_free(phone);
87
88 if (ret == 0) {
89 printf("%s\n", devname);
90 }
91
92 if (devname) {
93 free(devname);
94 }
51 95
52 if (opt_list) { 96 return ret;
97 case MODE_LIST_DEVICES:
98 default:
53 if (usbmuxd_scan(&dev_list) < 0) { 99 if (usbmuxd_scan(&dev_list) < 0) {
54 fprintf(stderr, "ERROR: usbmuxd is not running!\n"); 100 fprintf(stderr, "ERROR: usbmuxd is not running!\n");
55 return -1; 101 return -1;
@@ -59,36 +105,4 @@ int main(int argc, char **argv)
59 } 105 }
60 return 0; 106 return 0;
61 } 107 }
62
63 iphone_set_debug_level(0);
64
65 iphone_get_device_by_uuid(&phone, argv[0]);
66 if (!phone) {
67 fprintf(stderr, "ERROR: No device with UUID=%s attached.\n", argv[0]);
68 return -2;
69 }
70
71 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client)) {
72 iphone_device_free(phone);
73 fprintf(stderr, "ERROR: Connecting to device failed!\n");
74 return -2;
75 }
76
77 if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &devname)) || !devname) {
78 fprintf(stderr, "ERROR: Could not get device name!\n");
79 ret = -2;
80 }
81
82 lockdownd_client_free(client);
83 iphone_device_free(phone);
84
85 if (ret == 0) {
86 printf("%s\n", devname);
87 }
88
89 if (devname) {
90 free(devname);
91 }
92
93 return ret;
94} 108}