diff options
| author | 2008-10-17 22:58:50 -0700 | |
|---|---|---|
| committer | 2008-10-25 14:51:23 +0200 | |
| commit | bbd813da84c1ff8484bbd3eb7f575775271424da (patch) | |
| tree | 6ce51f659e09d45646f393d04eb13adeac8aed8f /src/iphone.c | |
| parent | 37fff2713545f7d660097842acfeb67e6eb5026c (diff) | |
| download | libimobiledevice-bbd813da84c1ff8484bbd3eb7f575775271424da.tar.gz libimobiledevice-bbd813da84c1ff8484bbd3eb7f575775271424da.tar.bz2 | |
Change iphone_debug to be an API call
Change the iphone_debug to use the new API
Diffstat (limited to 'src/iphone.c')
| -rw-r--r-- | src/iphone.c | 90 |
1 files changed, 49 insertions, 41 deletions
diff --git a/src/iphone.c b/src/iphone.c index c14d61b..4980078 100644 --- a/src/iphone.c +++ b/src/iphone.c | |||
| @@ -27,7 +27,18 @@ | |||
| 27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
| 28 | #include <string.h> | 28 | #include <string.h> |
| 29 | 29 | ||
| 30 | extern int debug; | 30 | int 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 | */ | ||
| 38 | void iphone_set_debug(int level) | ||
| 39 | { | ||
| 40 | iphone_debug = level; | ||
| 41 | } | ||
| 31 | 42 | ||
| 32 | /** | 43 | /** |
| 33 | * Given a USB bus and device number, returns a device handle to the iPhone on | 44 | * Given a USB bus and device number, returns a device handle to the iPhone on |
| @@ -42,8 +53,7 @@ extern int debug; | |||
| 42 | * descriptor on return. | 53 | * descriptor on return. |
| 43 | * @return IPHONE_E_SUCCESS if ok, otherwise an error code. | 54 | * @return IPHONE_E_SUCCESS if ok, otherwise an error code. |
| 44 | */ | 55 | */ |
| 45 | iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, | 56 | iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t * device) |
| 46 | iphone_device_t *device) | ||
| 47 | { | 57 | { |
| 48 | struct usb_bus *bus, *busses; | 58 | struct usb_bus *bus, *busses; |
| 49 | struct usb_device *dev; | 59 | struct usb_device *dev; |
| @@ -69,26 +79,26 @@ iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, | |||
| 69 | 79 | ||
| 70 | // Set the device configuration | 80 | // Set the device configuration |
| 71 | for (bus = busses; bus; bus = bus->next) | 81 | for (bus = busses; bus; bus = bus->next) |
| 72 | if (bus->location == bus_n) | 82 | if (bus->location == bus_n) |
| 73 | for (dev = bus->devices; dev != NULL; dev = dev->next) | 83 | for (dev = bus->devices; dev != NULL; dev = dev->next) |
| 74 | if (dev->devnum == dev_n) { | 84 | if (dev->devnum == dev_n) { |
| 75 | phone->__device = dev; | 85 | phone->__device = dev; |
| 76 | phone->device = usb_open(phone->__device); | 86 | phone->device = usb_open(phone->__device); |
| 77 | usb_set_configuration(phone->device, 3); | 87 | usb_set_configuration(phone->device, 3); |
| 78 | usb_claim_interface(phone->device, 1); | 88 | usb_claim_interface(phone->device, 1); |
| 79 | goto found; | 89 | goto found; |
| 80 | } | 90 | } |
| 81 | 91 | ||
| 82 | iphone_free_device(phone); | 92 | iphone_free_device(phone); |
| 83 | if (debug) | 93 | if (iphone_debug) |
| 84 | fprintf(stderr, "iphone_get_specific_device: iPhone not found\n"); | 94 | fprintf(stderr, "iphone_get_specific_device: iPhone not found\n"); |
| 85 | return IPHONE_E_NO_DEVICE; | 95 | return IPHONE_E_NO_DEVICE; |
| 86 | 96 | ||
| 87 | found: | 97 | found: |
| 88 | // Send the version command to the phone | 98 | // Send the version command to the phone |
| 89 | version = version_header(); | 99 | version = version_header(); |
| 90 | 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); |
| 91 | if (bytes < 20 && debug) { | 101 | if (bytes < 20 && iphone_debug) { |
| 92 | fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); | 102 | fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n"); |
| 93 | if (bytes < 0) { | 103 | if (bytes < 0) { |
| 94 | fprintf(stderr, "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", |
| @@ -102,9 +112,9 @@ found: | |||
| 102 | if (bytes < 20) { | 112 | if (bytes < 20) { |
| 103 | free(version); | 113 | free(version); |
| 104 | iphone_free_device(phone); | 114 | iphone_free_device(phone); |
| 105 | if (debug) | 115 | if (iphone_debug) |
| 106 | fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); | 116 | fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); |
| 107 | if (debug && bytes < 0) | 117 | if (iphone_debug && bytes < 0) |
| 108 | fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", | 118 | fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", |
| 109 | bytes, usb_strerror(), strerror(-bytes)); | 119 | bytes, usb_strerror(), strerror(-bytes)); |
| 110 | return IPHONE_E_NOT_ENOUGH_DATA; | 120 | return IPHONE_E_NOT_ENOUGH_DATA; |
| @@ -120,13 +130,13 @@ found: | |||
| 120 | // Bad header | 130 | // Bad header |
| 121 | iphone_free_device(phone); | 131 | iphone_free_device(phone); |
| 122 | free(version); | 132 | free(version); |
| 123 | if (debug) | 133 | if (iphone_debug) |
| 124 | fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); | 134 | fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); |
| 125 | return IPHONE_E_BAD_HEADER; | 135 | return IPHONE_E_BAD_HEADER; |
| 126 | } | 136 | } |
| 127 | 137 | ||
| 128 | // If it got to this point it's gotta be bad | 138 | // If it got to this point it's gotta be bad |
| 129 | if (debug) | 139 | if (iphone_debug) |
| 130 | fprintf(stderr, "get_iPhone(): Unknown error.\n"); | 140 | fprintf(stderr, "get_iPhone(): Unknown error.\n"); |
| 131 | iphone_free_device(phone); | 141 | iphone_free_device(phone); |
| 132 | free(version); | 142 | free(version); |
| @@ -149,24 +159,22 @@ found: | |||
| 149 | * will be filled with a handle to the device. | 159 | * will be filled with a handle to the device. |
| 150 | * @return IPHONE_E_SUCCESS if ok, otherwise an error code. | 160 | * @return IPHONE_E_SUCCESS if ok, otherwise an error code. |
| 151 | */ | 161 | */ |
| 152 | iphone_error_t iphone_get_device(iphone_device_t *device) | 162 | iphone_error_t iphone_get_device(iphone_device_t * device) |
| 153 | { | 163 | { |
| 154 | struct usb_bus *bus, *busses; | 164 | struct usb_bus *bus, *busses; |
| 155 | struct usb_device *dev; | 165 | struct usb_device *dev; |
| 156 | 166 | ||
| 157 | usb_init(); | 167 | usb_init(); |
| 158 | usb_find_busses(); | 168 | usb_find_busses(); |
| 159 | usb_find_devices(); | 169 | usb_find_devices(); |
| 160 | 170 | ||
| 161 | for (bus = usb_get_busses(); bus != NULL; bus = bus->next) | 171 | for (bus = usb_get_busses(); bus != NULL; bus = bus->next) |
| 162 | for (dev = bus->devices; dev != NULL; dev = dev->next) | 172 | for (dev = bus->devices; dev != NULL; dev = dev->next) |
| 163 | if (dev->descriptor.idVendor == 0x05ac | 173 | if (dev->descriptor.idVendor == 0x05ac |
| 164 | && dev->descriptor.idProduct >= 0x1290 | 174 | && dev->descriptor.idProduct >= 0x1290 && dev->descriptor.idProduct <= 0x1293) |
| 165 | && dev->descriptor.idProduct <= 0x1293) | 175 | return iphone_get_specific_device(bus->location, dev->devnum, device); |
| 166 | return iphone_get_specific_device(bus->location, dev->devnum, | ||
| 167 | device); | ||
| 168 | 176 | ||
| 169 | return IPHONE_E_NO_DEVICE; | 177 | return IPHONE_E_NO_DEVICE; |
| 170 | } | 178 | } |
| 171 | 179 | ||
| 172 | /** Cleans up an iPhone structure, then frees the structure itself. | 180 | /** Cleans up an iPhone structure, then frees the structure itself. |
| @@ -210,12 +218,12 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) | |||
| 210 | 218 | ||
| 211 | if (!phone) | 219 | if (!phone) |
| 212 | return -1; | 220 | return -1; |
| 213 | if (debug) | 221 | if (iphone_debug) |
| 214 | fprintf(stderr, "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); |
| 215 | 223 | ||
| 216 | bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); | 224 | bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); |
| 217 | if (bytes < datalen) { | 225 | if (bytes < datalen) { |
| 218 | if (debug && bytes < 0) | 226 | if (iphone_debug && bytes < 0) |
| 219 | fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), | 227 | fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), |
| 220 | strerror(-bytes)); | 228 | strerror(-bytes)); |
| 221 | return -1; | 229 | return -1; |
| @@ -242,12 +250,12 @@ int recv_from_phone(iphone_device_t phone, char *data, int datalen) | |||
| 242 | 250 | ||
| 243 | if (!phone) | 251 | if (!phone) |
| 244 | return -1; | 252 | return -1; |
| 245 | if (debug) | 253 | if (iphone_debug) |
| 246 | fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen); | 254 | fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen); |
| 247 | 255 | ||
| 248 | bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); | 256 | bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); |
| 249 | if (bytes < 0) { | 257 | if (bytes < 0) { |
| 250 | if (debug) | 258 | if (iphone_debug) |
| 251 | fprintf(stderr, "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(), |
| 252 | strerror(-bytes)); | 260 | strerror(-bytes)); |
| 253 | return -1; | 261 | return -1; |
