summaryrefslogtreecommitdiffstats
path: root/dev/iphoneclient.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 /dev/iphoneclient.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 'dev/iphoneclient.c')
-rw-r--r--dev/iphoneclient.c255
1 files changed, 255 insertions, 0 deletions
diff --git a/dev/iphoneclient.c b/dev/iphoneclient.c
new file mode 100644
index 0000000..bb5dfdd
--- /dev/null
+++ b/dev/iphoneclient.c
@@ -0,0 +1,255 @@
1/*
2 * main.c
3 * Rudimentary interface to the iPhone
4 *
5 * Copyright (c) 2008 Zach C. 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 <usb.h>
26#include <glib.h>
27
28#include <libiphone/libiphone.h>
29#include <libiphone/lockdown.h>
30#include <libiphone/afc.h>
31#include <libiphone/notification_proxy.h>
32#include "../src/utils.h"
33
34static void notifier(const char *notification)
35{
36 printf("---------------------------------------------------------\n");
37 printf("------> Notification received: %s\n", notification);
38 printf("---------------------------------------------------------\n");
39}
40
41static void perform_notification(iphone_device_t phone, lockdownd_client_t client, const char *notification)
42{
43 int nport = 0;
44 np_client_t np;
45
46 lockdownd_start_service(client, "com.apple.mobile.notification_proxy", &nport);
47 if (nport) {
48 printf("::::::::::::::: np was started ::::::::::::\n");
49 np_client_new(phone, nport, &np);
50 if (np) {
51 printf("::::::::: PostNotification %s\n", notification);
52 np_post_notification(np, notification);
53 np_client_free(np);
54 }
55 } else {
56 printf("::::::::::::::: np was NOT started ::::::::::::\n");
57 }
58}
59
60int main(int argc, char *argv[])
61{
62 unsigned int bytes = 0;
63 int port = 0, i = 0;
64 int npp;
65 lockdownd_client_t client = NULL;
66 iphone_device_t phone = NULL;
67 uint64_t lockfile = 0;
68 np_client_t gnp = NULL;
69
70 if (argc > 1 && !strcasecmp(argv[1], "--debug")) {
71 iphone_set_debug_level(1);
72 iphone_set_debug_mask(DBGMASK_ALL);
73 } else {
74 iphone_set_debug_level(0);
75 iphone_set_debug_mask(DBGMASK_NONE);
76 }
77
78 if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
79 printf("No iPhone found, is it plugged in?\n");
80 return -1;
81 }
82
83 char *uuid = NULL;
84 if (IPHONE_E_SUCCESS == iphone_device_get_uuid(phone, &uuid)) {
85 printf("DeviceUniqueID : %s\n", uuid);
86 }
87 if (uuid)
88 free(uuid);
89
90 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client)) {
91 iphone_device_free(phone);
92 printf("Exiting.\n");
93 return -1;
94 }
95
96 char *nnn = NULL;
97 if (LOCKDOWN_E_SUCCESS == lockdownd_get_device_name(client, &nnn)) {
98 printf("DeviceName : %s\n", nnn);
99 free(nnn);
100 }
101
102 lockdownd_start_service(client, "com.apple.afc", &port);
103
104 if (port) {
105 afc_client_t afc = NULL;
106 afc_client_new(phone, port, &afc);
107 if (afc) {
108 lockdownd_start_service(client, "com.apple.mobile.notification_proxy", &npp);
109 if (npp) {
110 printf("Notification Proxy started.\n");
111 np_client_new(phone, npp, &gnp);
112 } else {
113 printf("ERROR: Notification proxy could not be started.\n");
114 }
115 if (gnp) {
116 const char *nspec[5] = {
117 NP_SYNC_CANCEL_REQUEST,
118 NP_SYNC_SUSPEND_REQUEST,
119 NP_SYNC_RESUME_REQUEST,
120 NP_ITDBPREP_DID_END,
121 NULL
122 };
123 np_observe_notifications(gnp, nspec);
124 np_set_notify_callback(gnp, notifier);
125 }
126
127 perform_notification(phone, client, NP_SYNC_WILL_START);
128
129 afc_file_open(afc, "/com.apple.itunes.lock_sync", AFC_FOPEN_RW, &lockfile);
130 if (lockfile) {
131 printf("locking file\n");
132 afc_file_lock(afc, lockfile, AFC_LOCK_EX);
133
134 perform_notification(phone, client, NP_SYNC_DID_START);
135 }
136
137 char **dirs = NULL;
138 afc_read_directory(afc, "/eafaedf", &dirs);
139 if (!dirs)
140 afc_read_directory(afc, "/", &dirs);
141 printf("Directory time.\n");
142 for (i = 0; dirs[i]; i++) {
143 printf("/%s\n", dirs[i]);
144 }
145
146 g_strfreev(dirs);
147
148 dirs = NULL;
149 afc_get_device_info(afc, &dirs);
150 if (dirs) {
151 for (i = 0; dirs[i]; i += 2) {
152 printf("%s: %s\n", dirs[i], dirs[i + 1]);
153 }
154 }
155 g_strfreev(dirs);
156
157 uint64_t my_file = 0;
158 char **info = NULL;
159 uint64_t fsize = 0;
160 if (AFC_E_SUCCESS == afc_get_file_info(afc, "/readme.libiphone.fx", &info) && info) {
161 for (i = 0; info[i]; i += 2) {
162 printf("%s: %s\n", info[i], info[i+1]);
163 if (!strcmp(info[i], "st_size")) {
164 fsize = atoll(info[i+1]);
165 }
166 }
167 }
168
169 if (AFC_E_SUCCESS ==
170 afc_file_open(afc, "/readme.libiphone.fx", AFC_FOPEN_RDONLY, &my_file) && my_file) {
171 printf("A file size: %llu\n", (long long)fsize);
172 char *file_data = (char *) malloc(sizeof(char) * fsize);
173 afc_file_read(afc, my_file, file_data, fsize, &bytes);
174 if (bytes > 0) {
175 printf("The file's data:\n");
176 fwrite(file_data, 1, bytes, stdout);
177 }
178 printf("\nClosing my file.\n");
179 afc_file_close(afc, my_file);
180 free(file_data);
181 } else
182 printf("couldn't open a file\n");
183
184 afc_file_open(afc, "/readme.libiphone.fx", AFC_FOPEN_WR, &my_file);
185 if (my_file) {
186 char *outdatafile = strdup("this is a bitchin text file\n");
187 afc_file_write(afc, my_file, outdatafile, strlen(outdatafile), &bytes);
188 free(outdatafile);
189 if (bytes > 0)
190 printf("Wrote a surprise. ;)\n");
191 else
192 printf("I wanted to write a surprise, but... :(\n");
193 afc_file_close(afc, my_file);
194 }
195 printf("Deleting a file...\n");
196 bytes = afc_remove_path(afc, "/delme");
197 if (bytes)
198 printf("Success.\n");
199 else
200 printf("Failure. (expected unless you have a /delme file on your phone)\n");
201
202 printf("Renaming a file...\n");
203 bytes = afc_rename_path(afc, "/renme", "/renme2");
204 if (bytes > 0)
205 printf("Success.\n");
206 else
207 printf("Failure. (expected unless you have a /renme file on your phone)\n");
208
209 printf("Seek & read\n");
210 afc_file_open(afc, "/readme.libiphone.fx", AFC_FOPEN_RDONLY, &my_file);
211 if (AFC_E_SUCCESS != afc_file_seek(afc, my_file, 5, SEEK_CUR))
212 printf("WARN: SEEK DID NOT WORK\n");
213 char *threeletterword = (char *) malloc(sizeof(char) * 5);
214 afc_file_read(afc, my_file, threeletterword, 3, &bytes);
215 threeletterword[3] = '\0';
216 if (bytes > 0)
217 printf("Result: %s\n", threeletterword);
218 else
219 printf("Couldn't read!\n");
220 free(threeletterword);
221 afc_file_close(afc, my_file);
222 }
223
224 if (gnp && lockfile) {
225 printf("XXX sleeping\n");
226 sleep(5);
227
228 printf("XXX unlocking file\n");
229 afc_file_lock(afc, lockfile, AFC_LOCK_UN);
230
231 printf("XXX closing file\n");
232 afc_file_close(afc, lockfile);
233
234 printf("XXX sleeping\n");
235 sleep(5);
236 //perform_notification(phone, client, NP_SYNC_DID_FINISH);
237 }
238
239 if (gnp) {
240 np_client_free(gnp);
241 gnp = NULL;
242 }
243
244 afc_client_free(afc);
245 } else {
246 printf("Start service failure.\n");
247 }
248
249 printf("All done.\n");
250
251 lockdownd_client_free(client);
252 iphone_device_free(phone);
253
254 return 0;
255}