diff options
Diffstat (limited to 'tools/idevice_id.c')
-rw-r--r-- | tools/idevice_id.c | 169 |
1 files changed, 118 insertions, 51 deletions
diff --git a/tools/idevice_id.c b/tools/idevice_id.c index 1facb60..540a6f2 100644 --- a/tools/idevice_id.c +++ b/tools/idevice_id.c | |||
@@ -1,6 +1,34 @@ | |||
1 | /* | ||
2 | * idevice_id.c | ||
3 | * Prints device name or a list of attached devices | ||
4 | * | ||
5 | * Copyright (C) 2010-2018 Nikias Bassen <nikias@gmx.li> | ||
6 | * | ||
7 | * This library is free software; you can redistribute it and/or | ||
8 | * modify it under the terms of the GNU Lesser General Public | ||
9 | * License as published by the Free Software Foundation; either | ||
10 | * version 2.1 of the License, or (at your option) any later version. | ||
11 | * | ||
12 | * This library is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | * Lesser General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU Lesser General Public | ||
18 | * License along with this library; if not, write to the Free Software | ||
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #ifdef HAVE_CONFIG_H | ||
23 | #include <config.h> | ||
24 | #endif | ||
25 | |||
26 | #define TOOL_NAME "idevice_id" | ||
27 | |||
1 | #include <stdio.h> | 28 | #include <stdio.h> |
2 | #include <string.h> | 29 | #include <string.h> |
3 | #include <stdlib.h> | 30 | #include <stdlib.h> |
31 | #include <getopt.h> | ||
4 | #include <libimobiledevice/libimobiledevice.h> | 32 | #include <libimobiledevice/libimobiledevice.h> |
5 | #include <libimobiledevice/lockdown.h> | 33 | #include <libimobiledevice/lockdown.h> |
6 | 34 | ||
@@ -8,100 +36,139 @@ | |||
8 | #define MODE_SHOW_ID 1 | 36 | #define MODE_SHOW_ID 1 |
9 | #define MODE_LIST_DEVICES 2 | 37 | #define MODE_LIST_DEVICES 2 |
10 | 38 | ||
11 | static void print_usage(int argc, char **argv) | 39 | static void print_usage(int argc, char **argv, int is_error) |
12 | { | 40 | { |
13 | char *name = NULL; | 41 | char *name = strrchr(argv[0], '/'); |
14 | 42 | fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] [UDID]\n", (name ? name + 1: argv[0])); | |
15 | name = strrchr(argv[0], '/'); | 43 | fprintf(is_error ? stderr : stdout, |
16 | printf("Usage: %s [OPTIONS] [UUID]\n", (name ? name + 1: argv[0])); | 44 | "\n" |
17 | printf("Prints device name or a list of attached iPhone/iPod Touch devices.\n\n"); | 45 | "List attached devices or print device name of given device.\n" |
18 | printf(" The UUID is a 40-digit hexadecimal number of the device\n"); | 46 | "\n" \ |
19 | printf(" for which the name should be retrieved.\n\n"); | 47 | " If UDID is given, the name of the connected device with that UDID" |
20 | printf(" -l, --list\t\tlist UUID of all attached devices\n"); | 48 | " will be retrieved.\n" |
21 | printf(" -d, --debug\t\tenable communication debugging\n"); | 49 | "\n" \ |
22 | printf(" -h, --help\t\tprints usage information\n"); | 50 | "OPTIONS:\n" |
23 | printf("\n"); | 51 | " -l, --list list UDIDs of all devices attached via USB\n" |
52 | " -n, --network list UDIDs of all devices available via network\n" | ||
53 | " -d, --debug enable communication debugging\n" | ||
54 | " -h, --help prints usage information\n" | ||
55 | " -v, --version prints version information\n" | ||
56 | "\n" | ||
57 | "Homepage: <" PACKAGE_URL ">\n" | ||
58 | "Bug Reports: <" PACKAGE_BUGREPORT ">\n" | ||
59 | ); | ||
24 | } | 60 | } |
25 | 61 | ||
26 | int main(int argc, char **argv) | 62 | int main(int argc, char **argv) |
27 | { | 63 | { |
28 | idevice_t phone = NULL; | 64 | idevice_t device = NULL; |
29 | lockdownd_client_t client = NULL; | 65 | lockdownd_client_t client = NULL; |
30 | char **dev_list = NULL; | 66 | idevice_info_t *dev_list = NULL; |
31 | char *devname = NULL; | 67 | char *device_name = NULL; |
32 | int ret = 0; | 68 | int ret = 0; |
33 | int i; | 69 | int i; |
34 | int mode = MODE_SHOW_ID; | 70 | int mode = MODE_LIST_DEVICES; |
35 | char uuid[41]; | 71 | int include_usb = 0; |
36 | uuid[0] = 0; | 72 | int include_network = 0; |
73 | const char* udid = NULL; | ||
37 | 74 | ||
38 | /* parse cmdline args */ | 75 | int c = 0; |
39 | for (i = 1; i < argc; i++) { | 76 | const struct option longopts[] = { |
40 | if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { | 77 | { "debug", no_argument, NULL, 'd' }, |
78 | { "help", no_argument, NULL, 'h' }, | ||
79 | { "list", no_argument, NULL, 'l' }, | ||
80 | { "network", no_argument, NULL, 'n' }, | ||
81 | { "version", no_argument, NULL, 'v' }, | ||
82 | { NULL, 0, NULL, 0} | ||
83 | }; | ||
84 | |||
85 | while ((c = getopt_long(argc, argv, "dhlnv", longopts, NULL)) != -1) { | ||
86 | switch (c) { | ||
87 | case 'd': | ||
41 | idevice_set_debug_level(1); | 88 | idevice_set_debug_level(1); |
42 | continue; | 89 | break; |
43 | } | 90 | case 'h': |
44 | else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list")) { | 91 | print_usage(argc, argv, 0); |
92 | exit(EXIT_SUCCESS); | ||
93 | case 'l': | ||
45 | mode = MODE_LIST_DEVICES; | 94 | mode = MODE_LIST_DEVICES; |
46 | continue; | 95 | include_usb = 1; |
47 | } | 96 | break; |
48 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { | 97 | case 'n': |
49 | print_usage(argc, argv); | 98 | mode = MODE_LIST_DEVICES; |
99 | include_network = 1; | ||
100 | break; | ||
101 | case 'v': | ||
102 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); | ||
50 | return 0; | 103 | return 0; |
104 | default: | ||
105 | print_usage(argc, argv, 1); | ||
106 | exit(EXIT_FAILURE); | ||
51 | } | 107 | } |
52 | } | 108 | } |
109 | argc -= optind; | ||
110 | argv += optind; | ||
53 | 111 | ||
54 | /* check if uuid was passed */ | 112 | if (argc == 1) { |
55 | if (mode == MODE_SHOW_ID) { | 113 | mode = MODE_SHOW_ID; |
56 | i--; | 114 | } else if (argc == 0 && optind == 1) { |
57 | if (!argv[i] || (strlen(argv[i]) != 40)) { | 115 | include_usb = 1; |
58 | print_usage(argc, argv); | 116 | include_network = 1; |
59 | return 0; | ||
60 | } | ||
61 | strcpy(uuid, argv[i]); | ||
62 | } | 117 | } |
118 | udid = argv[0]; | ||
63 | 119 | ||
64 | switch (mode) { | 120 | switch (mode) { |
65 | case MODE_SHOW_ID: | 121 | case MODE_SHOW_ID: |
66 | idevice_new(&phone, uuid); | 122 | idevice_new_with_options(&device, udid, IDEVICE_LOOKUP_USBMUX | IDEVICE_LOOKUP_NETWORK); |
67 | if (!phone) { | 123 | if (!device) { |
68 | fprintf(stderr, "ERROR: No device with UUID=%s attached.\n", uuid); | 124 | fprintf(stderr, "ERROR: No device with UDID %s attached.\n", udid); |
69 | return -2; | 125 | return -2; |
70 | } | 126 | } |
71 | 127 | ||
72 | if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client, "idevice_id")) { | 128 | if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(device, &client, TOOL_NAME)) { |
73 | idevice_free(phone); | 129 | idevice_free(device); |
74 | fprintf(stderr, "ERROR: Connecting to device failed!\n"); | 130 | fprintf(stderr, "ERROR: Connecting to device failed!\n"); |
75 | return -2; | 131 | return -2; |
76 | } | 132 | } |
77 | 133 | ||
78 | if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &devname)) || !devname) { | 134 | if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &device_name)) || !device_name) { |
79 | fprintf(stderr, "ERROR: Could not get device name!\n"); | 135 | fprintf(stderr, "ERROR: Could not get device name!\n"); |
80 | ret = -2; | 136 | ret = -2; |
81 | } | 137 | } |
82 | 138 | ||
83 | lockdownd_client_free(client); | 139 | lockdownd_client_free(client); |
84 | idevice_free(phone); | 140 | idevice_free(device); |
85 | 141 | ||
86 | if (ret == 0) { | 142 | if (ret == 0) { |
87 | printf("%s\n", devname); | 143 | printf("%s\n", device_name); |
88 | } | 144 | } |
89 | 145 | ||
90 | if (devname) { | 146 | if (device_name) { |
91 | free(devname); | 147 | free(device_name); |
92 | } | 148 | } |
149 | break; | ||
93 | 150 | ||
94 | return ret; | ||
95 | case MODE_LIST_DEVICES: | 151 | case MODE_LIST_DEVICES: |
96 | default: | 152 | default: |
97 | if (idevice_get_device_list(&dev_list, &i) < 0) { | 153 | if (idevice_get_device_list_extended(&dev_list, &i) < 0) { |
98 | fprintf(stderr, "ERROR: Unable to retrieve device list!\n"); | 154 | fprintf(stderr, "ERROR: Unable to retrieve device list!\n"); |
99 | return -1; | 155 | return -1; |
100 | } | 156 | } |
101 | for (i = 0; dev_list[i] != NULL; i++) { | 157 | for (i = 0; dev_list[i] != NULL; i++) { |
102 | printf("%s\n", dev_list[i]); | 158 | if (dev_list[i]->conn_type == CONNECTION_USBMUXD && !include_usb) continue; |
159 | if (dev_list[i]->conn_type == CONNECTION_NETWORK && !include_network) continue; | ||
160 | printf("%s", dev_list[i]->udid); | ||
161 | if (include_usb && include_network) { | ||
162 | if (dev_list[i]->conn_type == CONNECTION_NETWORK) { | ||
163 | printf(" (Network)"); | ||
164 | } else { | ||
165 | printf(" (USB)"); | ||
166 | } | ||
167 | } | ||
168 | printf("\n"); | ||
103 | } | 169 | } |
104 | idevice_device_list_free(dev_list); | 170 | idevice_device_list_extended_free(dev_list); |
105 | return 0; | 171 | break; |
106 | } | 172 | } |
173 | return ret; | ||
107 | } | 174 | } |