summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/idevice_id.c118
1 files changed, 75 insertions, 43 deletions
diff --git a/tools/idevice_id.c b/tools/idevice_id.c
index 8d888c0..963d1d5 100644
--- a/tools/idevice_id.c
+++ b/tools/idevice_id.c
@@ -2,7 +2,7 @@
2 * idevice_id.c 2 * idevice_id.c
3 * Prints device name or a list of attached devices 3 * Prints device name or a list of attached devices
4 * 4 *
5 * Copyright (C) 2010 Nikias Bassen <nikias@gmx.li> 5 * Copyright (C) 2010-2018 Nikias Bassen <nikias@gmx.li>
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public 8 * modify it under the terms of the GNU Lesser General Public
@@ -26,10 +26,7 @@
26#include <stdio.h> 26#include <stdio.h>
27#include <string.h> 27#include <string.h>
28#include <stdlib.h> 28#include <stdlib.h>
29#ifndef WIN32 29#include <getopt.h>
30#include <signal.h>
31#endif
32
33#include <libimobiledevice/libimobiledevice.h> 30#include <libimobiledevice/libimobiledevice.h>
34#include <libimobiledevice/lockdown.h> 31#include <libimobiledevice/lockdown.h>
35 32
@@ -37,65 +34,90 @@
37#define MODE_SHOW_ID 1 34#define MODE_SHOW_ID 1
38#define MODE_LIST_DEVICES 2 35#define MODE_LIST_DEVICES 2
39 36
40static void print_usage(int argc, char **argv) 37static void print_usage(int argc, char **argv, int is_error)
41{ 38{
42 char *name = NULL; 39 char *name = NULL;
43
44 name = strrchr(argv[0], '/'); 40 name = strrchr(argv[0], '/');
45 printf("Usage: %s [OPTIONS] [UDID]\n", (name ? name + 1: argv[0])); 41 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] [UDID]\n", (name ? name + 1: argv[0]));
46 printf("Prints device name or a list of attached devices.\n\n"); 42 fprintf(is_error ? stderr : stdout,
47 printf(" UDID is the unique device identifier of the device\n"); 43 "Prints device name or a list of attached devices.\n\n" \
48 printf(" for which the name should be retrieved.\n\n"); 44 " If UDID is given, the name of the connected device with that UDID" \
49 printf(" -l, --list\t\tlist UDID of all attached devices\n"); 45 " will be retrieved.\n\n" \
50 printf(" -d, --debug\t\tenable communication debugging\n"); 46 " -l, --list list UDIDs of all devices attached via USB\n" \
51 printf(" -h, --help\t\tprints usage information\n"); 47 " -n, --network list UDIDs of all devices available via network\n" \
52 printf("\n"); 48 " -d, --debug enable communication debugging\n" \
53 printf("Homepage: <" PACKAGE_URL ">\n"); 49 " -h, --help prints usage information\n" \
50 "\n" \
51 "Homepage: <" PACKAGE_URL ">\n"
52 );
54} 53}
55 54
56int main(int argc, char **argv) 55int main(int argc, char **argv)
57{ 56{
58 idevice_t device = NULL; 57 idevice_t device = NULL;
59 lockdownd_client_t client = NULL; 58 lockdownd_client_t client = NULL;
60 char **dev_list = NULL; 59 idevice_info_t *dev_list = NULL;
61 char *device_name = NULL; 60 char *device_name = NULL;
62 int ret = 0; 61 int ret = 0;
63 int i; 62 int i;
64 int mode = MODE_SHOW_ID; 63 int mode = MODE_SHOW_ID;
64 int include_usb = 0;
65 int include_network = 0;
65 const char* udid = NULL; 66 const char* udid = NULL;
66 67
67#ifndef WIN32 68 int c = 0;
68 signal(SIGPIPE, SIG_IGN); 69 const struct option longopts[] = {
69#endif 70 { "debug", no_argument, NULL, 'd' },
70 /* parse cmdline args */ 71 { "help", no_argument, NULL, 'h' },
71 for (i = 1; i < argc; i++) { 72 { "list", no_argument, NULL, 'l' },
72 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { 73 { "network", no_argument, NULL, 'n' },
74 { NULL, 0, NULL, 0}
75 };
76
77 while ((c = getopt_long(argc, argv, "dhln", longopts, NULL)) != -1) {
78 switch (c) {
79 case 'd':
73 idevice_set_debug_level(1); 80 idevice_set_debug_level(1);
74 continue; 81 break;
75 } 82 case 'h':
76 else if (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--list")) { 83 print_usage(argc, argv, 0);
84 exit(EXIT_SUCCESS);
85 case 'l':
77 mode = MODE_LIST_DEVICES; 86 mode = MODE_LIST_DEVICES;
78 continue; 87 include_usb = 1;
79 } 88 break;
80 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { 89 case 'n':
81 print_usage(argc, argv); 90 mode = MODE_LIST_DEVICES;
82 return 0; 91 include_network = 1;
92 break;
93 default:
94 print_usage(argc, argv, 1);
95 exit(EXIT_FAILURE);
83 } 96 }
84 } 97 }
98 argc -= optind;
99 argv += optind;
85 100
86 /* check if udid was passed */ 101 if (mode == MODE_SHOW_ID && argc != 1) {
87 if (mode == MODE_SHOW_ID) { 102 print_usage(argc + optind, argv - optind, 1);
88 i--; 103 exit(EXIT_FAILURE);
89 if (argc < 2 || !argv[i] || !*argv[i]) { 104 }
90 print_usage(argc, argv); 105 udid = argv[0];
91 return 0; 106
92 } 107 enum idevice_connection_type conn_type = 0;
93 udid = argv[i]; 108 enum idevice_options opts = 0;
109 if (include_usb) {
110 conn_type |= CONNECTION_USBMUXD;
111 opts |= IDEVICE_LOOKUP_USBMUX;
112 }
113 if (include_network) {
114 conn_type |= CONNECTION_NETWORK;
115 opts |= IDEVICE_LOOKUP_NETWORK;
94 } 116 }
95 117
96 switch (mode) { 118 switch (mode) {
97 case MODE_SHOW_ID: 119 case MODE_SHOW_ID:
98 idevice_new(&device, udid); 120 idevice_new_with_options(&device, udid, opts);
99 if (!device) { 121 if (!device) {
100 fprintf(stderr, "ERROR: No device with UDID %s attached.\n", udid); 122 fprintf(stderr, "ERROR: No device with UDID %s attached.\n", udid);
101 return -2; 123 return -2;
@@ -126,14 +148,24 @@ int main(int argc, char **argv)
126 return ret; 148 return ret;
127 case MODE_LIST_DEVICES: 149 case MODE_LIST_DEVICES:
128 default: 150 default:
129 if (idevice_get_device_list(&dev_list, &i) < 0) { 151 if (idevice_get_device_list_extended(&dev_list, &i) < 0) {
130 fprintf(stderr, "ERROR: Unable to retrieve device list!\n"); 152 fprintf(stderr, "ERROR: Unable to retrieve device list!\n");
131 return -1; 153 return -1;
132 } 154 }
133 for (i = 0; dev_list[i] != NULL; i++) { 155 for (i = 0; dev_list[i] != NULL; i++) {
134 printf("%s\n", dev_list[i]); 156 if (dev_list[i]->conn_type & conn_type) {
157 printf("%s", dev_list[i]->udid);
158 if (include_usb && include_network) {
159 if (dev_list[i]->conn_type == CONNECTION_NETWORK) {
160 printf(" (WiFi)");
161 } else {
162 printf(" (USB)");
163 }
164 }
165 printf("\n");
166 }
135 } 167 }
136 idevice_device_list_free(dev_list); 168 idevice_device_list_extended_free(dev_list);
137 return 0; 169 return 0;
138 } 170 }
139} 171}