summaryrefslogtreecommitdiffstats
path: root/tools/iphoneinfo.c
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2009-07-28 10:58:50 +0200
committerGravatar Martin Szulecki2009-07-28 10:58:50 +0200
commite169cf993dbf7515cad3da03993a4d224d6cf181 (patch)
tree6aca7a4e59779232d9b50bd06ce25abf4ec1f294 /tools/iphoneinfo.c
parentc443a1fbc2fe5e9858015193628ef66623535766 (diff)
downloadlibimobiledevice-e169cf993dbf7515cad3da03993a4d224d6cf181.tar.gz
libimobiledevice-e169cf993dbf7515cad3da03993a4d224d6cf181.tar.bz2
Move production ready tools into tools/ and do not install the dev/ ones
Diffstat (limited to 'tools/iphoneinfo.c')
-rw-r--r--tools/iphoneinfo.c281
1 files changed, 281 insertions, 0 deletions
diff --git a/tools/iphoneinfo.c b/tools/iphoneinfo.c
new file mode 100644
index 0000000..7e275b2
--- /dev/null
+++ b/tools/iphoneinfo.c
@@ -0,0 +1,281 @@
1/*
2 * iphoneinfo.c
3 * Simple utility to show information about an attached device
4 *
5 * Copyright (c) 2009 Martin Szulecki 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#include <stdio.h>
23#include <string.h>
24#include <errno.h>
25#include <stdlib.h>
26
27#include <libiphone/libiphone.h>
28#include <libiphone/lockdown.h>
29
30#define FORMAT_KEY_VALUE 1
31#define FORMAT_XML 2
32
33static const char *domains[] = {
34 "com.apple.disk_usage",
35 "com.apple.mobile.battery",
36/* FIXME: For some reason lockdownd segfaults on this, works sometimes though
37 "com.apple.mobile.debug",. */
38 "com.apple.xcode.developerdomain",
39 "com.apple.international",
40 "com.apple.mobile.mobile_application_usage",
41 "com.apple.mobile.backup",
42 "com.apple.mobile.user_preferences",
43 "com.apple.mobile.sync_data_class",
44 "com.apple.mobile.software_behavior",
45 "com.apple.mobile.iTunes.SQLMusicLibraryPostProcessCommands",
46 "com.apple.iTunes",
47 "com.apple.mobile.iTunes.store",
48 "com.apple.mobile.iTunes",
49 NULL
50};
51
52int is_domain_known(char *domain);
53void print_usage(int argc, char **argv);
54void plist_node_to_string(plist_t *node);
55void plist_children_to_string(plist_t *node);
56
57int main(int argc, char *argv[])
58{
59 lockdownd_client_t client = NULL;
60 iphone_device_t phone = NULL;
61 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
62 int i;
63 int format = FORMAT_KEY_VALUE;
64 char uuid[41];
65 char *domain = NULL;
66 char *key = NULL;
67 char *xml_doc = NULL;
68 uint32_t xml_length;
69 plist_t node = NULL;
70 uuid[0] = 0;
71
72 /* parse cmdline args */
73 for (i = 1; i < argc; i++) {
74 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
75 iphone_set_debug_mask(DBGMASK_ALL);
76 iphone_set_debug_level(1);
77 continue;
78 }
79 else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) {
80 i++;
81 if (!argv[i] || (strlen(argv[i]) != 40)) {
82 print_usage(argc, argv);
83 return 0;
84 }
85 strcpy(uuid, argv[i]);
86 continue;
87 }
88 else if (!strcmp(argv[i], "-q") || !strcmp(argv[i], "--domain")) {
89 i++;
90 if (!argv[i] || (strlen(argv[i]) < 4)) {
91 print_usage(argc, argv);
92 return 0;
93 }
94 if (!is_domain_known(argv[i])) {
95 fprintf(stderr, "WARNING: Sending query with unknown domain \"%s\".\n", argv[i]);
96 }
97 domain = strdup(argv[i]);
98 continue;
99 }
100 else if (!strcmp(argv[i], "-k") || !strcmp(argv[i], "--key")) {
101 i++;
102 if (!argv[i] || (strlen(argv[i]) <= 1)) {
103 print_usage(argc, argv);
104 return 0;
105 }
106 key = strdup(argv[i]);
107 continue;
108 }
109 else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "--xml")) {
110 format = FORMAT_XML;
111 continue;
112 }
113 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
114 print_usage(argc, argv);
115 return 0;
116 }
117 else {
118 print_usage(argc, argv);
119 return 0;
120 }
121 }
122
123 if (uuid[0] != 0) {
124 ret = iphone_get_device_by_uuid(&phone, uuid);
125 if (ret != IPHONE_E_SUCCESS) {
126 printf("No device found with uuid %s, is it plugged in?\n", uuid);
127 return -1;
128 }
129 }
130 else
131 {
132 ret = iphone_get_device(&phone);
133 if (ret != IPHONE_E_SUCCESS) {
134 printf("No device found, is it plugged in?\n");
135 return -1;
136 }
137 }
138
139 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client)) {
140 iphone_device_free(phone);
141 return -1;
142 }
143
144 /* run query and output information */
145 if(lockdownd_get_value(client, domain, key, &node) == LOCKDOWN_E_SUCCESS)
146 {
147 if (plist_get_node_type(node) == PLIST_DICT) {
148 if (plist_get_first_child(node))
149 {
150 switch (format) {
151 case FORMAT_XML:
152 plist_to_xml(node, &xml_doc, &xml_length);
153 printf(xml_doc);
154 free(xml_doc);
155 break;
156 case FORMAT_KEY_VALUE:
157 default:
158 plist_children_to_string(node);
159 break;
160 }
161 }
162 }
163 else if(node && (key != NULL))
164 plist_node_to_string(node);
165 if (node)
166 plist_free(node);
167 node = NULL;
168 }
169
170 if (domain != NULL)
171 free(domain);
172 lockdownd_client_free(client);
173 iphone_device_free(phone);
174
175 return 0;
176}
177
178int is_domain_known(char *domain)
179{
180 int i = 0;
181 while (domains[i] != NULL) {
182 if (strstr(domain, domains[i++])) {
183 return 1;
184 }
185 }
186 return 0;
187}
188
189void print_usage(int argc, char **argv)
190{
191 int i = 0;
192 char *name = NULL;
193
194 name = strrchr(argv[0], '/');
195 printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
196 printf("Show information about the first connected iPhone/iPod Touch.\n\n");
197 printf(" -d, --debug\t\tenable communication debugging\n");
198 printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n");
199 printf(" -q, --domain NAME\tset domain of query to NAME. Default: None\n");
200 printf(" -k, --key NAME\tonly query key specified by NAME. Default: All keys.\n");
201 printf(" -x, --xml\t\toutput information as xml plist instead of key/value pairs\n");
202 printf(" -h, --help\t\tprints usage information\n");
203 printf("\n");
204 printf(" Known domains are:\n\n");
205 while (domains[i] != NULL) {
206 printf(" %s\n", domains[i++]);
207 }
208 printf("\n");
209}
210
211void plist_node_to_string(plist_t *node)
212{
213 char *s = NULL;
214 double d;
215 uint8_t b;
216
217 uint64_t u = 0;
218
219 plist_type t;
220
221 if (!node)
222 return;
223
224 t = plist_get_node_type(node);
225
226 switch (t) {
227 case PLIST_BOOLEAN:
228 plist_get_bool_val(node, &b);
229 printf("%s\n", (b ? "true" : "false"));
230 break;
231
232 case PLIST_UINT:
233 plist_get_uint_val(node, &u);
234 printf("%llu\n", (long long)u);
235 break;
236
237 case PLIST_REAL:
238 plist_get_real_val(node, &d);
239 printf("%f\n", d);
240 break;
241
242 case PLIST_STRING:
243 plist_get_string_val(node, &s);
244 printf("%s\n", s);
245 free(s);
246 break;
247
248 case PLIST_KEY:
249 plist_get_key_val(node, &s);
250 printf("%s: ", s);
251 free(s);
252 break;
253
254 case PLIST_DATA:
255 printf("\n");
256 break;
257 case PLIST_DATE:
258 printf("\n");
259 break;
260 case PLIST_ARRAY:
261 case PLIST_DICT:
262 printf("\n");
263 plist_children_to_string(node);
264 break;
265 default:
266 break;
267 }
268}
269
270void plist_children_to_string(plist_t *node)
271{
272 /* iterate over key/value pairs */
273 for (
274 node = plist_get_first_child(node);
275 node != NULL;
276 node = plist_get_next_sibling(node)
277 ) {
278 plist_node_to_string(node);
279 }
280}
281