summaryrefslogtreecommitdiffstats
path: root/src/iphone.c
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-10-25 16:11:27 +0200
committerGravatar Jonathan Beck2008-10-25 16:52:20 +0200
commit0b2cfd2c7c6211ff5902e48720c34067f238ce90 (patch)
tree9c30578b4288795e7589c959caa08f3547ca0b88 /src/iphone.c
parentbbd813da84c1ff8484bbd3eb7f575775271424da (diff)
downloadlibimobiledevice-0b2cfd2c7c6211ff5902e48720c34067f238ce90.tar.gz
libimobiledevice-0b2cfd2c7c6211ff5902e48720c34067f238ce90.tar.bz2
Handle debugging through utilitary functions
Diffstat (limited to 'src/iphone.c')
-rw-r--r--src/iphone.c56
1 files changed, 19 insertions, 37 deletions
diff --git a/src/iphone.c b/src/iphone.c
index 4980078..b7f6cc4 100644
--- a/src/iphone.c
+++ b/src/iphone.c
@@ -21,24 +21,13 @@
21 21
22#include "usbmux.h" 22#include "usbmux.h"
23#include "iphone.h" 23#include "iphone.h"
24#include "utils.h"
24#include <arpa/inet.h> 25#include <arpa/inet.h>
25#include <usb.h> 26#include <usb.h>
26#include <stdio.h> 27#include <stdio.h>
27#include <stdlib.h> 28#include <stdlib.h>
28#include <string.h> 29#include <string.h>
29 30
30int iphone_debug = 0;
31
32/**
33 * Sets the level of debugging. Currently the only acceptable values are 0 and
34 * 1.
35 *
36 * @param level Set to 0 for no debugging or 1 for debugging.
37 */
38void iphone_set_debug(int level)
39{
40 iphone_debug = level;
41}
42 31
43/** 32/**
44 * Given a USB bus and device number, returns a device handle to the iPhone on 33 * Given a USB bus and device number, returns a device handle to the iPhone on
@@ -90,19 +79,19 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
90 } 79 }
91 80
92 iphone_free_device(phone); 81 iphone_free_device(phone);
93 if (iphone_debug) 82
94 fprintf(stderr, "iphone_get_specific_device: iPhone not found\n"); 83 log_debug_msg("iphone_get_specific_device: iPhone not found\n");
95 return IPHONE_E_NO_DEVICE; 84 return IPHONE_E_NO_DEVICE;
96 85
97 found: 86 found:
98 // Send the version command to the phone 87 // Send the version command to the phone
99 version = version_header(); 88 version = version_header();
100 bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800); 89 bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
101 if (bytes < 20 && iphone_debug) { 90 if (bytes < 20) {
102 fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); 91 log_debug_msg("get_iPhone(): libusb did NOT send enough!\n");
103 if (bytes < 0) { 92 if (bytes < 0) {
104 fprintf(stderr, "get_iPhone(): libusb gave me the error %d: %s (%s)\n", 93 log_debug_msg("get_iPhone(): libusb gave me the error %d: %s (%s)\n",
105 bytes, usb_strerror(), strerror(-bytes)); 94 bytes, usb_strerror(), strerror(-bytes));
106 } 95 }
107 } 96 }
108 // Read the phone's response 97 // Read the phone's response
@@ -112,11 +101,9 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
112 if (bytes < 20) { 101 if (bytes < 20) {
113 free(version); 102 free(version);
114 iphone_free_device(phone); 103 iphone_free_device(phone);
115 if (iphone_debug) 104 log_debug_msg("get_iPhone(): Invalid version message -- header too short.\n");
116 fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); 105 if (bytes < 0)
117 if (iphone_debug && bytes < 0) 106 log_debug_msg("get_iPhone(): libusb error message %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
118 fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
119 bytes, usb_strerror(), strerror(-bytes));
120 return IPHONE_E_NOT_ENOUGH_DATA; 107 return IPHONE_E_NOT_ENOUGH_DATA;
121 } 108 }
122 // Check for correct version 109 // Check for correct version
@@ -130,14 +117,12 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t
130 // Bad header 117 // Bad header
131 iphone_free_device(phone); 118 iphone_free_device(phone);
132 free(version); 119 free(version);
133 if (iphone_debug) 120 log_debug_msg("get_iPhone(): Received a bad header/invalid version number.");
134 fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
135 return IPHONE_E_BAD_HEADER; 121 return IPHONE_E_BAD_HEADER;
136 } 122 }
137 123
138 // If it got to this point it's gotta be bad 124 // If it got to this point it's gotta be bad
139 if (iphone_debug) 125 log_debug_msg("get_iPhone(): Unknown error.\n");
140 fprintf(stderr, "get_iPhone(): Unknown error.\n");
141 iphone_free_device(phone); 126 iphone_free_device(phone);
142 free(version); 127 free(version);
143 return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad 128 return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
@@ -218,14 +203,13 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen)
218 203
219 if (!phone) 204 if (!phone)
220 return -1; 205 return -1;
221 if (iphone_debug) 206 log_debug_msg("send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
222 fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
223 207
224 bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); 208 bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800);
225 if (bytes < datalen) { 209 if (bytes < datalen) {
226 if (iphone_debug && bytes < 0) 210 if (bytes < 0)
227 fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), 211 log_debug_msg("send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
228 strerror(-bytes)); 212 strerror(-bytes));
229 return -1; 213 return -1;
230 } else { 214 } else {
231 return bytes; 215 return bytes;
@@ -250,14 +234,12 @@ int recv_from_phone(iphone_device_t phone, char *data, int datalen)
250 234
251 if (!phone) 235 if (!phone)
252 return -1; 236 return -1;
253 if (iphone_debug) 237 log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen);
254 fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
255 238
256 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); 239 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
257 if (bytes < 0) { 240 if (bytes < 0) {
258 if (iphone_debug) 241 log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
259 fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), 242 strerror(-bytes));
260 strerror(-bytes));
261 return -1; 243 return -1;
262 } 244 }
263 245