diff options
Diffstat (limited to 'tools/idevicename.c')
-rw-r--r-- | tools/idevicename.c | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/tools/idevicename.c b/tools/idevicename.c new file mode 100644 index 0000000..69b76f6 --- /dev/null +++ b/tools/idevicename.c | |||
@@ -0,0 +1,159 @@ | |||
1 | /* | ||
2 | * idevicename.c | ||
3 | * Simple utility to get or set the device name | ||
4 | * | ||
5 | * Copyright (c) 2014 Nikias Bassen, All Rights Reserved. | ||
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 "idevicename" | ||
27 | |||
28 | #include <stdio.h> | ||
29 | #include <string.h> | ||
30 | #include <unistd.h> | ||
31 | #include <stdlib.h> | ||
32 | #include <getopt.h> | ||
33 | #ifndef WIN32 | ||
34 | #include <signal.h> | ||
35 | #endif | ||
36 | |||
37 | #include <libimobiledevice/libimobiledevice.h> | ||
38 | #include <libimobiledevice/lockdown.h> | ||
39 | |||
40 | static void print_usage(int argc, char** argv, int is_error) | ||
41 | { | ||
42 | char *name = strrchr(argv[0], '/'); | ||
43 | fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] [NAME]\n", (name ? name + 1: argv[0])); | ||
44 | fprintf(is_error ? stderr : stdout, | ||
45 | "\n" | ||
46 | "Display the device name or set it to NAME if specified.\n" | ||
47 | "\n" | ||
48 | "OPTIONS:\n" | ||
49 | " -u, --udid UDID target specific device by UDID\n" | ||
50 | " -n, --network connect to network device\n" | ||
51 | " -d, --debug enable communication debugging\n" | ||
52 | " -h, --help print usage information\n" | ||
53 | " -v, --version print version information\n" | ||
54 | "\n" | ||
55 | "Homepage: <" PACKAGE_URL ">\n" | ||
56 | "Bug Reports: <" PACKAGE_BUGREPORT ">\n" | ||
57 | ); | ||
58 | } | ||
59 | |||
60 | int main(int argc, char** argv) | ||
61 | { | ||
62 | int c = 0; | ||
63 | const struct option longopts[] = { | ||
64 | { "udid", required_argument, NULL, 'u' }, | ||
65 | { "network", no_argument, NULL, 'n' }, | ||
66 | { "debug", no_argument, NULL, 'd' }, | ||
67 | { "help", no_argument, NULL, 'h' }, | ||
68 | { "version", no_argument, NULL, 'v' }, | ||
69 | { NULL, 0, NULL, 0} | ||
70 | }; | ||
71 | int res = -1; | ||
72 | const char* udid = NULL; | ||
73 | int use_network = 0; | ||
74 | |||
75 | #ifndef WIN32 | ||
76 | signal(SIGPIPE, SIG_IGN); | ||
77 | #endif | ||
78 | |||
79 | while ((c = getopt_long(argc, argv, "du:hnv", longopts, NULL)) != -1) { | ||
80 | switch (c) { | ||
81 | case 'u': | ||
82 | if (!*optarg) { | ||
83 | fprintf(stderr, "ERROR: UDID must not be empty!\n"); | ||
84 | print_usage(argc, argv, 1); | ||
85 | exit(2); | ||
86 | } | ||
87 | udid = optarg; | ||
88 | break; | ||
89 | case 'n': | ||
90 | use_network = 1; | ||
91 | break; | ||
92 | case 'h': | ||
93 | print_usage(argc, argv, 0); | ||
94 | return 0; | ||
95 | case 'd': | ||
96 | idevice_set_debug_level(1); | ||
97 | break; | ||
98 | case 'v': | ||
99 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); | ||
100 | return 0; | ||
101 | default: | ||
102 | print_usage(argc, argv, 1); | ||
103 | return 2; | ||
104 | } | ||
105 | } | ||
106 | |||
107 | argc -= optind; | ||
108 | argv += optind; | ||
109 | |||
110 | if (argc > 1) { | ||
111 | print_usage(argc, argv, 1); | ||
112 | return 2; | ||
113 | } | ||
114 | |||
115 | idevice_t device = NULL; | ||
116 | if (idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX) != IDEVICE_E_SUCCESS) { | ||
117 | if (udid) { | ||
118 | fprintf(stderr, "ERROR: No device found with udid %s.\n", udid); | ||
119 | } else { | ||
120 | fprintf(stderr, "ERROR: No device found.\n"); | ||
121 | } | ||
122 | return -1; | ||
123 | } | ||
124 | |||
125 | lockdownd_client_t lockdown = NULL; | ||
126 | lockdownd_error_t lerr = lockdownd_client_new_with_handshake(device, &lockdown, TOOL_NAME); | ||
127 | if (lerr != LOCKDOWN_E_SUCCESS) { | ||
128 | idevice_free(device); | ||
129 | fprintf(stderr, "ERROR: Could not connect to lockdownd, error code %d\n", lerr); | ||
130 | return -1; | ||
131 | } | ||
132 | |||
133 | if (argc == 0) { | ||
134 | // getting device name | ||
135 | char* name = NULL; | ||
136 | lerr = lockdownd_get_device_name(lockdown, &name); | ||
137 | if (name) { | ||
138 | printf("%s\n", name); | ||
139 | free(name); | ||
140 | res = 0; | ||
141 | } else { | ||
142 | fprintf(stderr, "ERROR: Could not get device name, lockdown error %d\n", lerr); | ||
143 | } | ||
144 | } else { | ||
145 | // setting device name | ||
146 | lerr = lockdownd_set_value(lockdown, NULL, "DeviceName", plist_new_string(argv[0])); | ||
147 | if (lerr == LOCKDOWN_E_SUCCESS) { | ||
148 | printf("device name set to '%s'\n", argv[0]); | ||
149 | res = 0; | ||
150 | } else { | ||
151 | fprintf(stderr, "ERROR: Could not set device name, lockdown error %d\n", lerr); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | lockdownd_client_free(lockdown); | ||
156 | idevice_free(device); | ||
157 | |||
158 | return res; | ||
159 | } | ||