summaryrefslogtreecommitdiffstats
path: root/src/iphone.c
diff options
context:
space:
mode:
authorGravatar Matt Colyer2008-09-01 15:04:31 -0700
committerGravatar Matt Colyer2008-09-01 15:04:31 -0700
commit2b05e48cb4a90dfc94ff584124f08e431398bb1a (patch)
treee0c8255e2cd5592a31295ac8ce89d8846feb7043 /src/iphone.c
parent7ac3d681889a6a8f9987837ace5465f2967cfff9 (diff)
downloadlibimobiledevice-2b05e48cb4a90dfc94ff584124f08e431398bb1a.tar.gz
libimobiledevice-2b05e48cb4a90dfc94ff584124f08e431398bb1a.tar.bz2
Enforce a modified kr style.
Use "make indent" from now on before committing.
Diffstat (limited to 'src/iphone.c')
-rw-r--r--src/iphone.c109
1 files changed, 62 insertions, 47 deletions
diff --git a/src/iphone.c b/src/iphone.c
index 68963fe..2c4c541 100644
--- a/src/iphone.c
+++ b/src/iphone.c
@@ -27,43 +27,42 @@
27#include <stdlib.h> 27#include <stdlib.h>
28#include <string.h> 28#include <string.h>
29 29
30extern int debug; 30extern int debug;
31 31
32/** Gets a handle to an iPhone 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_error_t iphone_get_device ( iphone_device_t *device ){ 37iphone_error_t iphone_get_device(iphone_device_t * device)
38{
38 //check we can actually write in device 39 //check we can actually write in device
39 if (!device || (device && *device)) 40 if (!device || (device && *device))
40 return IPHONE_E_INVALID_ARG; 41 return IPHONE_E_INVALID_ARG;
41 42
42 struct usb_bus *bus, *busses; 43 struct usb_bus *bus, *busses;
43 struct usb_device *dev; 44 struct usb_device *dev;
44 iphone_device_t phone = (iphone_device_t)malloc(sizeof(struct iphone_device_int)); 45 iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int));
45 46
46 // Initialize the struct 47 // Initialize the struct
47 phone->device = NULL; 48 phone->device = NULL;
48 phone->__device = NULL; 49 phone->__device = NULL;
49 phone->buffer = NULL; 50 phone->buffer = NULL;
50 51
51 // Initialize libusb 52 // Initialize libusb
52 usb_init(); 53 usb_init();
53 usb_find_busses(); 54 usb_find_busses();
54 usb_find_devices(); 55 usb_find_devices();
55 busses = usb_get_busses(); 56 busses = usb_get_busses();
56 57
57 58
58 // Set the device configuration 59 // Set the device configuration
59 for (bus = busses; bus; bus = bus->next) { 60 for (bus = busses; bus; bus = bus->next) {
60 for (dev = bus->devices; dev; dev = dev->next) { 61 for (dev = bus->devices; dev; dev = dev->next) {
61 if (dev->descriptor.idVendor == 0x05ac && 62 if (dev->descriptor.idVendor == 0x05ac &&
62 (dev->descriptor.idProduct == 0x1290 || 63 (dev->descriptor.idProduct == 0x1290 ||
63 dev->descriptor.idProduct == 0x1291 || 64 dev->descriptor.idProduct == 0x1291 || dev->descriptor.idProduct == 0x1292)
64 dev->descriptor.idProduct == 0x1292 65 ) {
65 )
66 ) {
67 phone->__device = dev; 66 phone->__device = dev;
68 phone->device = usb_open(phone->__device); 67 phone->device = usb_open(phone->__device);
69 usb_set_configuration(phone->device, 3); 68 usb_set_configuration(phone->device, 3);
@@ -71,20 +70,21 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
71 break; 70 break;
72 } 71 }
73 } 72 }
74 if (phone->__device && phone->device) break; 73 if (phone->__device && phone->device)
74 break;
75 } 75 }
76 76
77 // Check to see if we are connected 77 // Check to see if we are connected
78 if (!phone->device || !phone->__device) { 78 if (!phone->device || !phone->__device) {
79 iphone_free_device(phone); 79 iphone_free_device(phone);
80 if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n"); 80 if (debug)
81 fprintf(stderr, "get_iPhone(): iPhone not found\n");
81 return IPHONE_E_NO_DEVICE; 82 return IPHONE_E_NO_DEVICE;
82 } 83 }
83
84 // Send the version command to the phone 84 // Send the version command to the phone
85 int bytes = 0; 85 int bytes = 0;
86 usbmux_version_header *version = version_header(); 86 usbmux_version_header *version = version_header();
87 bytes = usb_bulk_write(phone->device, BULKOUT, (char*)version, sizeof(*version), 800); 87 bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
88 if (bytes < 20 && debug) { 88 if (bytes < 20 && debug) {
89 fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); 89 fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n");
90 if (bytes < 0) { 90 if (bytes < 0) {
@@ -92,20 +92,20 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
92 bytes, usb_strerror(), strerror(-bytes)); 92 bytes, usb_strerror(), strerror(-bytes));
93 } 93 }
94 } 94 }
95
96 // Read the phone's response 95 // Read the phone's response
97 bytes = usb_bulk_read(phone->device, BULKIN, (char*)version, sizeof(*version), 800); 96 bytes = usb_bulk_read(phone->device, BULKIN, (char *) version, sizeof(*version), 800);
98 97
99 // Check for bad response 98 // Check for bad response
100 if (bytes < 20) { 99 if (bytes < 20) {
101 free(version); 100 free(version);
102 iphone_free_device(phone); 101 iphone_free_device(phone);
103 if (debug) fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); 102 if (debug)
104 if (debug && bytes < 0) fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", 103 fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n");
105 bytes, usb_strerror(), strerror(-bytes)); 104 if (debug && bytes < 0)
105 fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
106 bytes, usb_strerror(), strerror(-bytes));
106 return IPHONE_E_NOT_ENOUGH_DATA; 107 return IPHONE_E_NOT_ENOUGH_DATA;
107 } 108 }
108
109 // Check for correct version 109 // Check for correct version
110 if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) { 110 if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) {
111 // We're all ready to roll. 111 // We're all ready to roll.
@@ -117,15 +117,17 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
117 // Bad header 117 // Bad header
118 iphone_free_device(phone); 118 iphone_free_device(phone);
119 free(version); 119 free(version);
120 if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); 120 if (debug)
121 fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
121 return IPHONE_E_BAD_HEADER; 122 return IPHONE_E_BAD_HEADER;
122 } 123 }
123 124
124 // If it got to this point it's gotta be bad 125 // If it got to this point it's gotta be bad
125 if (debug) fprintf(stderr, "get_iPhone(): Unknown error.\n"); 126 if (debug)
127 fprintf(stderr, "get_iPhone(): Unknown error.\n");
126 iphone_free_device(phone); 128 iphone_free_device(phone);
127 free(version); 129 free(version);
128 return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad 130 return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
129} 131}
130 132
131/** Cleans up an iPhone structure, then frees the structure itself. 133/** Cleans up an iPhone structure, then frees the structure itself.
@@ -134,8 +136,10 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
134 * 136 *
135 * @param phone A pointer to an iPhone structure. 137 * @param phone A pointer to an iPhone structure.
136 */ 138 */
137iphone_error_t iphone_free_device ( iphone_device_t device ) { 139iphone_error_t iphone_free_device(iphone_device_t device)
138 if (!device) return IPHONE_E_INVALID_ARG; 140{
141 if (!device)
142 return IPHONE_E_INVALID_ARG;
139 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 143 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
140 144
141 if (device->buffer) { 145 if (device->buffer) {
@@ -150,7 +154,7 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) {
150 free(device); 154 free(device);
151 return ret; 155 return ret;
152} 156}
153 157
154/** Sends data to the phone 158/** Sends data to the phone
155 * This is a low-level (i.e. directly to phone) function. 159 * This is a low-level (i.e. directly to phone) function.
156 * 160 *
@@ -159,22 +163,27 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) {
159 * @param datalen The length of the data 163 * @param datalen The length of the data
160 * @return The number of bytes sent, or -1 on error or something. 164 * @return The number of bytes sent, or -1 on error or something.
161 */ 165 */
162int send_to_phone(iphone_device_t phone, char *data, int datalen) { 166int send_to_phone(iphone_device_t phone, char *data, int datalen)
163 if (!phone) return -1; 167{
168 if (!phone)
169 return -1;
164 int bytes = 0; 170 int bytes = 0;
165 171
166 if (!phone) return -1; 172 if (!phone)
167 if (debug) fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data); 173 return -1;
174 if (debug)
175 fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
168 176
169 bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); 177 bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800);
170 if (bytes < datalen) { 178 if (bytes < datalen) {
171 if(debug && bytes < 0) 179 if (debug && bytes < 0)
172 fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), strerror(-bytes)); 180 fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
181 strerror(-bytes));
173 return -1; 182 return -1;
174 } else { 183 } else {
175 return bytes; 184 return bytes;
176 } 185 }
177 186
178 return -1; 187 return -1;
179} 188}
180 189
@@ -186,18 +195,24 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) {
186 * 195 *
187 * @return How many bytes were read in, or -1 on error. 196 * @return How many bytes were read in, or -1 on error.
188 */ 197 */
189int recv_from_phone(iphone_device_t phone, char *data, int datalen) { 198int recv_from_phone(iphone_device_t phone, char *data, int datalen)
190 if (!phone) return -1; 199{
200 if (!phone)
201 return -1;
191 int bytes = 0; 202 int bytes = 0;
192 203
193 if (!phone) return -1; 204 if (!phone)
194 if (debug) fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen); 205 return -1;
195 206 if (debug)
207 fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
208
196 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); 209 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
197 if (bytes < 0) { 210 if (bytes < 0) {
198 if(debug) fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes)); 211 if (debug)
212 fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
213 strerror(-bytes));
199 return -1; 214 return -1;
200 } 215 }
201 216
202 return bytes; 217 return bytes;
203} 218}