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
|
/*
* afccheck.c
* creates threads and check communication through AFC is done rigth
*
* Copyright (c) 2008 Jonathan Beck 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <libiphone/libiphone.h>
#include <libiphone/lockdown.h>
#include <libiphone/afc.h>
#define BUFFER_SIZE 20000
#define NB_THREADS 10
typedef struct {
afc_client_t afc;
int id;
} param;
void check_afc(gpointer data)
{
//prepare a buffer
unsigned int buffersize = BUFFER_SIZE * sizeof(unsigned int);
int *buf = (int *) malloc(buffersize);
int *buf2 = (int *) malloc(buffersize);
unsigned int bytes = 0;
//fill buffer
int i = 0;
for (i = 0; i < BUFFER_SIZE; i++) {
buf[i] = ((param *) data)->id * i;
}
//now writes buffer on iphone
uint64_t file = 0;
char path[50];
sprintf(path, "/Buf%i", ((param *) data)->id);
afc_open_file(((param *) data)->afc, path, AFC_FOPEN_RW, &file);
afc_write_file(((param *) data)->afc, file, (char *) buf, buffersize, &bytes);
afc_close_file(((param *) data)->afc, file);
file = 0;
if (bytes != buffersize)
printf("Write operation failed\n");
//now read it
bytes = 0;
afc_open_file(((param *) data)->afc, path, AFC_FOPEN_RDONLY, &file);
afc_read_file(((param *) data)->afc, file, (char *) buf2, buffersize, &bytes);
afc_close_file(((param *) data)->afc, file);
if (bytes != buffersize)
printf("Read operation failed\n");
//compare buffers
for (i = 0; i < BUFFER_SIZE; i++) {
if (buf[i] != buf2[i]) {
printf("Buffers are differents, stream corrupted\n");
break;
}
}
//cleanup
afc_delete_file(((param *) data)->afc, path);
g_thread_exit(0);
}
int main(int argc, char *argv[])
{
lockdownd_client_t client = NULL;
iphone_device_t phone = NULL;
GError *err;
int port = 0;
afc_client_t afc = NULL;
if (argc > 1 && !strcasecmp(argv[1], "--debug")) {
iphone_set_debug_level(1);
iphone_set_debug_mask(DBGMASK_ALL);
} else {
iphone_set_debug_level(0);
iphone_set_debug_mask(DBGMASK_NONE);
}
if (IPHONE_E_SUCCESS != iphone_get_device(&phone)) {
printf("No iPhone found, is it plugged in?\n");
return 1;
}
if (LOCKDOWN_E_SUCCESS != lockdownd_client_new(phone, &client)) {
iphone_device_free(phone);
return 1;
}
if (LOCKDOWN_E_SUCCESS == lockdownd_start_service(client, "com.apple.afc", &port) && !port) {
lockdownd_client_free(client);
iphone_device_free(phone);
fprintf(stderr, "Something went wrong when starting AFC.");
return 1;
}
afc_new_client(phone, port, &afc);
//makes sure thread environment is available
if (!g_thread_supported())
g_thread_init(NULL);
GThread *threads[NB_THREADS];
param data[NB_THREADS];
int i = 0;
for (i = 0; i < NB_THREADS; i++) {
data[i].afc = afc;
data[i].id = i + 1;
threads[i] = g_thread_create((GThreadFunc) check_afc, data + i, TRUE, &err);
}
for (i = 0; i < NB_THREADS; i++) {
g_thread_join(threads[i]);
}
lockdownd_client_free(client);
iphone_device_free(phone);
return 0;
}
|