summaryrefslogtreecommitdiffstats
path: root/dev/main.c
blob: c68427b8064c3e3b8d727bfdea69d16d695222d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * main.c
 * Rudimentary interface to the iPhone
 *
 * Copyright (c) 2008 Zach C. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <usb.h>

#include <libxml/parser.h>
#include <libxml/tree.h>

#include <libiphone/libiphone.h>

void perform_syncWillStart(iphone_device_t phone, iphone_lckd_client_t control)
{
	int nport = 0;
	iphone_np_client_t np;

	iphone_lckd_start_service(control, "com.apple.mobile.notification_proxy", &nport);
	if (nport) {
		printf("::::::::::::::: np was started ::::::::::::\n");
		iphone_np_new_client(phone, 3555, nport, &np);
		if (np) {
			printf("::::::::: PostNotification com.apple.itunes-mobdev.syncWillStart\n");
			iphone_np_post_notification(np, "com.apple.itunes-mobdev.syncWillStart");
			iphone_np_free_client(np);
		}
	} else {
		printf("::::::::::::::: np was NOT started ::::::::::::\n");
	}
}

void perform_syncDidStart(iphone_device_t phone, iphone_lckd_client_t control)
{
	int nport = 0;
	iphone_np_client_t np;

	iphone_lckd_start_service(control, "com.apple.mobile.notification_proxy", &nport);
	if (nport) {
		printf("::::::::::::::: np was started ::::::::::::\n");
		sleep(1);
		iphone_np_new_client(phone, 3555, nport, &np);
		if (np) {
			printf("::::::::: PostNotification com.apple.itunes-mobdev.syncDidStart\n");
			iphone_np_post_notification(np, "com.apple.itunes-mobdev.syncDidStart");
			iphone_np_free_client(np);
		}
	} else {
		printf("::::::::::::::: np was NOT started ::::::::::::\n");
	}
}

int main(int argc, char *argv[])
{
	int bytes = 0, port = 0, i = 0;
	iphone_lckd_client_t control = NULL;
	iphone_device_t phone = NULL;
	iphone_afc_file_t lockfile = NULL;

	if (argc > 1 && !strcasecmp(argv[1], "--debug")) {
		iphone_set_debug(1);
	} else {
		iphone_set_debug(0);
	}

	if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
		printf("No iPhone found, is it plugged in?\n");
		return -1;
	}

	if (IPHONE_E_SUCCESS != iphone_lckd_new_client(phone, &control)) {
		iphone_free_device(phone);
		return -1;
	}

	char *uid = NULL;
	if (IPHONE_E_SUCCESS == lockdownd_get_device_uid(control, &uid)) {
		printf("DeviceUniqueID : %s\n", uid);
		free(uid);
	}

	iphone_lckd_start_service(control, "com.apple.afc", &port);

	if (port) {
		iphone_afc_client_t afc = NULL;
		iphone_afc_new_client(phone, 3432, port, &afc);
		if (afc) {
			perform_syncWillStart(phone, control);

			iphone_afc_open_file(afc, "/com.apple.itunes.lock_sync", IPHONE_AFC_FILE_WRITE, &lockfile);
			if (lockfile) {
				printf("locking file\n");
				iphone_afc_lock_file(afc, lockfile, 2 | 4);

				perform_syncDidStart(phone, control);
			}

			char **dirs = NULL;
			iphone_afc_get_dir_list(afc, "/eafaedf", &dirs);
			if (!dirs)
				iphone_afc_get_dir_list(afc, "/", &dirs);
			printf("Directory time.\n");
			for (i = 0; dirs[i]; i++) {
				printf("/%s\n", dirs[i]);
			}

			g_strfreev(dirs);
			iphone_afc_get_devinfo(afc, &dirs);
			if (dirs) {
				for (i = 0; dirs[i]; i += 2) {
					printf("%s: %s\n", dirs[i], dirs[i + 1]);
				}
			}
			g_strfreev(dirs);

			iphone_afc_file_t my_file = NULL;
			struct stat stbuf;
			iphone_afc_get_file_attr(afc, "/iTunesOnTheGoPlaylist.plist", &stbuf);
			if (IPHONE_E_SUCCESS ==
				iphone_afc_open_file(afc, "/iTunesOnTheGoPlaylist.plist", IPHONE_AFC_FILE_READ, &my_file) && my_file) {
				printf("A file size: %i\n", stbuf.st_size);
				char *file_data = (char *) malloc(sizeof(char) * stbuf.st_size);
				iphone_afc_read_file(afc, my_file, file_data, stbuf.st_size, &bytes);
				if (bytes >= 0) {
					printf("The file's data:\n");
					fwrite(file_data, 1, bytes, stdout);
				}
				printf("\nClosing my file.\n");
				iphone_afc_close_file(afc, my_file);
				free(file_data);
			} else
				printf("couldn't open a file\n");

			iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_WRITE, &my_file);
			if (my_file) {
				char *outdatafile = strdup("this is a bitchin text file\n");
				iphone_afc_write_file(afc, my_file, outdatafile, strlen(outdatafile), &bytes);
				free(outdatafile);
				if (bytes > 0)
					printf("Wrote a surprise. ;)\n");
				else
					printf("I wanted to write a surprise, but... :(\n");
				iphone_afc_close_file(afc, my_file);
			}
			printf("Deleting a file...\n");
			bytes = iphone_afc_delete_file(afc, "/delme");
			if (bytes)
				printf("Success.\n");
			else
				printf("Failure. (expected unless you have a /delme file on your phone)\n");

			printf("Renaming a file...\n");
			bytes = iphone_afc_rename_file(afc, "/renme", "/renme2");
			if (bytes > 0)
				printf("Success.\n");
			else
				printf("Failure. (expected unless you have a /renme file on your phone)\n");

			printf("Seek & read\n");
			iphone_afc_open_file(afc, "/readme.libiphone.fx", IPHONE_AFC_FILE_READ, &my_file);
			if (IPHONE_E_SUCCESS != iphone_afc_seek_file(afc, my_file, 5))
				printf("WARN: SEEK DID NOT WORK\n");
			char *threeletterword = (char *) malloc(sizeof(char) * 5);
			iphone_afc_read_file(afc, my_file, threeletterword, 3, &bytes);
			threeletterword[3] = '\0';
			if (bytes > 0)
				printf("Result: %s\n", threeletterword);
			else
				printf("Couldn't read!\n");
			free(threeletterword);
			iphone_afc_close_file(afc, my_file);
		}

		if (lockfile) {
			printf("XXX sleeping 2 seconds\n");
			sleep(2);

			printf("XXX unlocking file\n");
			iphone_afc_lock_file(afc, lockfile, 8 | 4);

			printf("XXX closing file\n");
			iphone_afc_close_file(afc, lockfile);
		}
		iphone_afc_free_client(afc);
	} else {
		printf("Start service failure.\n");
	}

	printf("All done.\n");

	iphone_lckd_free_client(control);
	iphone_free_device(phone);

	return 0;
}