summaryrefslogtreecommitdiffstats
path: root/src/iphone.c
diff options
context:
space:
mode:
authorGravatar Matt Colyer2008-08-13 23:21:04 -0700
committerGravatar Matt Colyer2008-08-13 23:21:04 -0700
commitb39a0c05e41f5dca5931cd3d550ef0c2a4142e4b (patch)
treef765ebe786886ded0a74521c9fa1c13b32e2837b /src/iphone.c
parentf281e24cca43149db1f6077a8b42e456393a8856 (diff)
downloadlibimobiledevice-b39a0c05e41f5dca5931cd3d550ef0c2a4142e4b.tar.gz
libimobiledevice-b39a0c05e41f5dca5931cd3d550ef0c2a4142e4b.tar.bz2
Minor cleanups, refactored and commented iphone.c.
Diffstat (limited to 'src/iphone.c')
-rw-r--r--src/iphone.c128
1 files changed, 77 insertions, 51 deletions
diff --git a/src/iphone.c b/src/iphone.c
index 558dd9a..104418f 100644
--- a/src/iphone.c
+++ b/src/iphone.c
@@ -29,30 +29,36 @@
29 29
30extern int debug; 30extern int debug;
31 31
32/** 32/** Gets a handle to an iPhone
33 * 33 *
34 * @return A structure with data on the first iPhone it finds. (Or NULL, on 34 * @return A structure with data on the first iPhone it finds. (Or NULL, on
35 * error) 35 * error)
36 */ 36 */
37iPhone *get_iPhone() { 37iPhone *get_iPhone() {
38 iPhone *phone = (iPhone*)malloc(sizeof(iPhone)); 38 iPhone *phone = (iPhone*)malloc(sizeof(iPhone));
39 usbmux_version_header *version = version_header(); 39 usbmux_version_header *version = version_header();
40 struct usb_bus *bus, *busses;
41 struct usb_device *dev;
40 42
41 // initialize the struct 43 // Initialize the struct
42 phone->device = NULL; 44 phone->device = NULL;
43 phone->__device = NULL; 45 phone->__device = NULL;
44 phone->buffer = NULL; 46 phone->buffer = NULL;
45 47
46 // Initialize libusb. 48 // Initialize libusb
47 usb_init(); 49 usb_init();
48 usb_find_busses(); 50 usb_find_busses();
49 usb_find_devices(); 51 usb_find_devices();
50 struct usb_bus *busses = usb_get_busses(), *bus; 52 busses = usb_get_busses();
51 struct usb_device *dev;
52 53
53 for (bus = busses; bus; bus = bus->next) { 54 for (bus = busses; bus; bus = bus->next) {
54 for (dev = bus->devices; dev; dev = dev->next) { 55 for (dev = bus->devices; dev; dev = dev->next) {
55 if (dev->descriptor.idVendor == 0x05ac && (dev->descriptor.idProduct == 0x1290 || dev->descriptor.idProduct == 0x1291 || dev->descriptor.idProduct == 0x1292)) { 56 if (dev->descriptor.idVendor == 0x05ac &&
57 (dev->descriptor.idProduct == 0x1290 ||
58 dev->descriptor.idProduct == 0x1291 ||
59 dev->descriptor.idProduct == 0x1292
60 )
61 ) {
56 phone->__device = dev; 62 phone->__device = dev;
57 phone->device = usb_open(phone->__device); 63 phone->device = usb_open(phone->__device);
58 usb_reset(phone->device); 64 usb_reset(phone->device);
@@ -60,12 +66,18 @@ iPhone *get_iPhone() {
60 } 66 }
61 } 67 }
62 68
63 phone->device = NULL; // :( sorry Daniel 69 phone->device = NULL;
64 phone->__device = NULL; // :( sorry Daniel 70 phone->__device = NULL;
65 71
66 for (bus = busses; bus; bus = bus->next) { // do it again as per libusb documentation 72 // Set the device configuration
73 for (bus = busses; bus; bus = bus->next) {
67 for (dev = bus->devices; dev; dev = dev->next) { 74 for (dev = bus->devices; dev; dev = dev->next) {
68 if (dev->descriptor.idVendor == 0x05ac && (dev->descriptor.idProduct == 0x1290 || dev->descriptor.idProduct == 0x1291 || dev->descriptor.idProduct == 0x1292)) { 75 if (dev->descriptor.idVendor == 0x05ac &&
76 (dev->descriptor.idProduct == 0x1290 ||
77 dev->descriptor.idProduct == 0x1291 ||
78 dev->descriptor.idProduct == 0x1292
79 )
80 ) {
69 phone->__device = dev; 81 phone->__device = dev;
70 phone->device = usb_open(phone->__device); 82 phone->device = usb_open(phone->__device);
71 usb_set_configuration(phone->device, 3); 83 usb_set_configuration(phone->device, 3);
@@ -76,58 +88,73 @@ iPhone *get_iPhone() {
76 if (phone->__device && phone->device) break; 88 if (phone->__device && phone->device) break;
77 } 89 }
78 90
79 if (!phone->device || !phone->__device) { // nothing connected 91 // Check to see if we are connected
92 if (!phone->device || !phone->__device) {
80 free_iPhone(phone); 93 free_iPhone(phone);
81 if (debug) printf("get_iPhone(): iPhone not found\n"); 94 if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n");
82 return NULL; 95 return NULL;
83 } 96 }
84 97
85 // Okay, initialize the phone now. 98 // Send the version command to the phone
86 int bytes = 0; 99 int bytes = 0;
87 bytes = usb_bulk_write(phone->device, BULKOUT, (char*)version, sizeof(*version), 800); 100 bytes = usb_bulk_write(phone->device, BULKOUT, (char*)version, sizeof(*version), 800);
88 if (bytes < 20 && debug) { 101 if (bytes < 20 && debug) {
89 printf("get_iPhone(): libusb did NOT send enough!\n"); 102 fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n");
90 if (bytes < 0) { 103 if (bytes < 0) {
91 printf("get_iPhone(): libusb gave me the error %d: %s (%s)\n", 104 fprintf(stderr, "get_iPhone(): libusb gave me the error %d: %s (%s)\n",
92 bytes, usb_strerror(), strerror(-bytes)); 105 bytes, usb_strerror(), strerror(-bytes));
93 } 106 }
94 } 107 }
108
109 // Read the phone's response
95 bytes = usb_bulk_read(phone->device, BULKIN, (char*)version, sizeof(*version), 800); 110 bytes = usb_bulk_read(phone->device, BULKIN, (char*)version, sizeof(*version), 800);
111
112 // Check for bad response
96 if (bytes < 20) { 113 if (bytes < 20) {
97 free_iPhone(phone); 114 free_iPhone(phone);
98 if (debug) printf("get_iPhone(): Invalid version message -- header too short.\n"); 115 free(version);
99 if (debug && bytes < 0) printf("get_iPhone(): libusb error message %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes)); 116 if (debug) fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n");
117 if (debug && bytes < 0) fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
118 bytes, usb_strerror(), strerror(-bytes));
119 return NULL;
120 }
121
122 // Check for correct version
123 if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) {
124 // We're all ready to roll.
125 fprintf(stderr, "get_iPhone() success\n");
126 free(version);
127 return phone;
128 } else {
129 // Bad header
130 free_iPhone(phone);
131 free(version);
132 if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
100 return NULL; 133 return NULL;
101 } else {
102 if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) {
103 // We're all ready to roll.
104 printf("get_iPhone() success\n");
105 return phone;
106 } else { // BAD HEADER
107 free_iPhone(phone);
108 if (debug) printf("get_iPhone(): Received a bad header/invalid version number.");
109 return NULL;
110 }
111 } 134 }
112 135
113 if (debug) printf("get_iPhone(): Unknown error.\n"); 136
114 return NULL; // if it got to this point it's gotta be bad 137 // If it got to this point it's gotta be bad
138 if (debug) fprintf(stderr, "get_iPhone(): Unknown error.\n");
139 free_iPhone(phone);
140 free(version);
141 return NULL;
115} 142}
116 143
117/** Cleans up an iPhone structure, then frees the structure itself. 144/** Cleans up an iPhone structure, then frees the structure itself.
118 * This is a library-level function; deals directly with the iPhone to tear 145 * This is a library-level function; deals directly with the iPhone to tear
119 * down relations, but otherwise is mostly internal. 146 * down relations, but otherwise is mostly internal.
120 * 147 *
121 * @param victim A pointer to an iPhone structure. 148 * @param phone A pointer to an iPhone structure.
122 */ 149 */
123void free_iPhone(iPhone *victim) { 150void free_iPhone(iPhone *phone) {
124 if (victim->buffer) free(victim->buffer); 151 if (phone->buffer) free(phone->buffer);
125 if (victim->device) { 152 if (phone->device) {
126 usb_release_interface(victim->device, 1); 153 usb_release_interface(phone->device, 1);
127 usb_reset(victim->device); 154 usb_reset(phone->device);
128 usb_close(victim->device); 155 usb_close(phone->device);
129 } 156 }
130 free(victim); 157 free(phone);
131} 158}
132 159
133/** Sends data to the phone 160/** Sends data to the phone
@@ -139,16 +166,15 @@ void free_iPhone(iPhone *victim) {
139 * @return The number of bytes sent, or -1 on error or something. 166 * @return The number of bytes sent, or -1 on error or something.
140 */ 167 */
141int send_to_phone(iPhone *phone, char *data, int datalen) { 168int send_to_phone(iPhone *phone, char *data, int datalen) {
142 if (!phone) return -1;
143 int bytes = 0; 169 int bytes = 0;
144 // it may die here 170
145 if (debug) printf("dying here?\ndatalen = %i\ndata = %p\n", datalen, data); 171 if (!phone) return -1;
172 if (debug) fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
146 173
147 bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); 174 bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800);
148 if (debug) printf("noooo...?\n");
149 if (bytes < datalen) { 175 if (bytes < datalen) {
150 if(debug && bytes < 0) 176 if(debug && bytes < 0)
151 printf("send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), strerror(-bytes)); 177 fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), strerror(-bytes));
152 return -1; 178 return -1;
153 } else { 179 } else {
154 return bytes; 180 return bytes;
@@ -157,8 +183,7 @@ int send_to_phone(iPhone *phone, char *data, int datalen) {
157 return -1; 183 return -1;
158} 184}
159 185
160/** 186/** This function is a low-level (i.e. direct to iPhone) function.
161 * This function is a low-level (i.e. direct to iPhone) function.
162 * 187 *
163 * @param phone The iPhone to receive data from 188 * @param phone The iPhone to receive data from
164 * @param data Where to put data read 189 * @param data Where to put data read
@@ -167,15 +192,16 @@ int send_to_phone(iPhone *phone, char *data, int datalen) {
167 * @return How many bytes were read in, or -1 on error. 192 * @return How many bytes were read in, or -1 on error.
168 */ 193 */
169int recv_from_phone(iPhone *phone, char *data, int datalen) { 194int recv_from_phone(iPhone *phone, char *data, int datalen) {
170 if (!phone) return -1;
171 int bytes = 0; 195 int bytes = 0;
172 if (debug) printf("recv_from_phone(): attempting to receive %i bytes\n", datalen); 196
197 if (!phone) return -1;
198 if (debug) fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
199
173 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); 200 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
174 if(bytes < 0) 201 if (bytes < 0) {
175 { 202 if(debug) fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
176 if(debug) printf("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
177 return -1; 203 return -1;
178 } 204 }
205
179 return bytes; 206 return bytes;
180} 207}
181