Debian Codegraph @

tools/idevice_id.c

raw
1 /*
2 * idevice_id.c
3 * Prints device name or a list of attached devices
4 *
5 * Copyright (C) 2010-2018 Nikias Bassen <[email protected]>
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
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <getopt.h>
32 #include <libimobiledevice/libimobiledevice.h>
33 #include <libimobiledevice/lockdown.h>
34
35 #define MODE_NONE 0
36 #define MODE_SHOW_ID 1
37 #define MODE_LIST_DEVICES 2
38
39 static void print_usage(int argc, char **argv, int is_error)
40 {
41 char *name = strrchr(argv[0], '/');
42 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] [UDID]\n", (name ? name + 1: argv[0]));
43 fprintf(is_error ? stderr : stdout,
44 "\n"
45 "List attached devices or print device name of given device.\n"
46 "\n" \
47 " If UDID is given, the name of the connected device with that UDID"
48 " will be retrieved.\n"
49 "\n" \
50 "OPTIONS:\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 );
60 }
61
62 int main(int argc, char **argv)
63 {
64 idevice_t device = NULL;
65 lockdownd_client_t client = NULL;
66 idevice_info_t *dev_list = NULL;
67 char *device_name = NULL;
68 int ret = 0;
69 int i;
70 int mode = MODE_LIST_DEVICES;
71 int include_usb = 0;
72 int include_network = 0;
73 const char* udid = NULL;
74
75 int c = 0;
76 const struct option longopts[] = {
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':
88 idevice_set_debug_level(1);
89 break;
90 case 'h':
91 print_usage(argc, argv, 0);
92 exit(EXIT_SUCCESS);
93 case 'l':
94 mode = MODE_LIST_DEVICES;
95 include_usb = 1;
96 break;
97 case 'n':
98 mode = MODE_LIST_DEVICES;
99 include_network = 1;
100 break;
101 case 'v':
102 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
103 return 0;
104 default:
105 print_usage(argc, argv, 1);
106 exit(EXIT_FAILURE);
107 }
108 }
109 argc -= optind;
110 argv += optind;
111
112 if (argc == 1) {
113 mode = MODE_SHOW_ID;
114 } else if (argc == 0 && optind == 1) {
115 include_usb = 1;
116 include_network = 1;
117 }
118 udid = argv[0];
119
120 switch (mode) {
121 case MODE_SHOW_ID:
122 idevice_new_with_options(&device, udid, IDEVICE_LOOKUP_USBMUX | IDEVICE_LOOKUP_NETWORK);
123 if (!device) {
124 fprintf(stderr, "ERROR: No device with UDID %s attached.\n", udid);
125 return -2;
126 }
127
128 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(device, &client, TOOL_NAME)) {
129 idevice_free(device);
130 fprintf(stderr, "ERROR: Connecting to device failed!\n");
131 return -2;
132 }
133
134 if ((LOCKDOWN_E_SUCCESS != lockdownd_get_device_name(client, &device_name)) || !device_name) {
135 fprintf(stderr, "ERROR: Could not get device name!\n");
136 ret = -2;
137 }
138
139 lockdownd_client_free(client);
140 idevice_free(device);
141
142 if (ret == 0) {
143 printf("%s\n", device_name);
144 }
145
146 if (device_name) {
147 free(device_name);
148 }
149 break;
150
151 case MODE_LIST_DEVICES:
152 default:
153 if (idevice_get_device_list_extended(&dev_list, &i) < 0) {
154 fprintf(stderr, "ERROR: Unable to retrieve device list!\n");
155 return -1;
156 }
157 for (i = 0; dev_list[i] != NULL; 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");
169 }
170 idevice_device_list_extended_free(dev_list);
171 break;
172 }
173 return ret;
174 }