diff options
Diffstat (limited to 'src/iphone.c')
| -rw-r--r-- | src/iphone.c | 247 |
1 files changed, 0 insertions, 247 deletions
diff --git a/src/iphone.c b/src/iphone.c deleted file mode 100644 index b7f6cc4..0000000 --- a/src/iphone.c +++ /dev/null | |||
| @@ -1,247 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * iphone.c | ||
| 3 | * Functions for creating and initializing iPhone structures. | ||
| 4 | * | ||
| 5 | * Copyright (c) 2008 Zach C. All Rights Reserved. | ||
| 6 | * | ||
| 7 | * This library is free software; you can redistribute it and/or | ||
| 8 | * modify it under the terms of the GNU Lesser General Public | ||
| 9 | * License as published by the Free Software Foundation; either | ||
| 10 | * version 2.1 of the License, or (at your option) any later version. | ||
| 11 | * | ||
| 12 | * This library is distributed in the hope that it will be useful, | ||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 15 | * Lesser General Public License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public | ||
| 18 | * License along with this library; if not, write to the Free Software | ||
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "usbmux.h" | ||
| 23 | #include "iphone.h" | ||
| 24 | #include "utils.h" | ||
| 25 | #include <arpa/inet.h> | ||
| 26 | #include <usb.h> | ||
| 27 | #include <stdio.h> | ||
| 28 | #include <stdlib.h> | ||
| 29 | #include <string.h> | ||
| 30 | |||
| 31 | |||
| 32 | /** | ||
| 33 | * Given a USB bus and device number, returns a device handle to the iPhone on | ||
| 34 | * that bus. To aid compatibility with future devices, this function does not | ||
| 35 | * check the vendor and device IDs! To do that, you should use | ||
| 36 | * iphone_get_device() or a system-specific API (e.g. HAL). | ||
| 37 | * | ||
| 38 | * @param bus_n The USB bus number. | ||
| 39 | * @param dev_n The USB device number. | ||
| 40 | * @param device A pointer to a iphone_device_t, which must be set to NULL upon | ||
| 41 | * calling iphone_get_specific_device, which will be filled with a device | ||
| 42 | * descriptor on return. | ||
| 43 | * @return IPHONE_E_SUCCESS if ok, otherwise an error code. | ||
| 44 | */ | ||
| 45 | iphone_error_t iphone_get_specific_device(int bus_n, int dev_n, iphone_device_t * device) | ||
| 46 | { | ||
| 47 | struct usb_bus *bus, *busses; | ||
| 48 | struct usb_device *dev; | ||
| 49 | usbmux_version_header *version; | ||
| 50 | int bytes = 0; | ||
| 51 | |||
| 52 | //check we can actually write in device | ||
| 53 | if (!device || (device && *device)) | ||
| 54 | return IPHONE_E_INVALID_ARG; | ||
| 55 | |||
| 56 | iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int)); | ||
| 57 | |||
| 58 | // Initialize the struct | ||
| 59 | phone->device = NULL; | ||
| 60 | phone->__device = NULL; | ||
| 61 | phone->buffer = NULL; | ||
| 62 | |||
| 63 | // Initialize libusb | ||
| 64 | usb_init(); | ||
| 65 | usb_find_busses(); | ||
| 66 | usb_find_devices(); | ||
| 67 | busses = usb_get_busses(); | ||
| 68 | |||
| 69 | // Set the device configuration | ||
| 70 | for (bus = busses; bus; bus = bus->next) | ||
| 71 | if (bus->location == bus_n) | ||
| 72 | for (dev = bus->devices; dev != NULL; dev = dev->next) | ||
| 73 | if (dev->devnum == dev_n) { | ||
| 74 | phone->__device = dev; | ||
| 75 | phone->device = usb_open(phone->__device); | ||
| 76 | usb_set_configuration(phone->device, 3); | ||
| 77 | usb_claim_interface(phone->device, 1); | ||
| 78 | goto found; | ||
| 79 | } | ||
| 80 | |||
| 81 | iphone_free_device(phone); | ||
| 82 | |||
| 83 | log_debug_msg("iphone_get_specific_device: iPhone not found\n"); | ||
| 84 | return IPHONE_E_NO_DEVICE; | ||
| 85 | |||
| 86 | found: | ||
| 87 | // Send the version command to the phone | ||
| 88 | version = version_header(); | ||
| 89 | bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800); | ||
| 90 | if (bytes < 20) { | ||
| 91 | log_debug_msg("get_iPhone(): libusb did NOT send enough!\n"); | ||
| 92 | if (bytes < 0) { | ||
| 93 | log_debug_msg("get_iPhone(): libusb gave me the error %d: %s (%s)\n", | ||
| 94 | bytes, usb_strerror(), strerror(-bytes)); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | // Read the phone's response | ||
| 98 | bytes = usb_bulk_read(phone->device, BULKIN, (char *) version, sizeof(*version), 800); | ||
| 99 | |||
| 100 | // Check for bad response | ||
| 101 | if (bytes < 20) { | ||
| 102 | free(version); | ||
| 103 | iphone_free_device(phone); | ||
| 104 | log_debug_msg("get_iPhone(): Invalid version message -- header too short.\n"); | ||
| 105 | if (bytes < 0) | ||
| 106 | log_debug_msg("get_iPhone(): libusb error message %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes)); | ||
| 107 | return IPHONE_E_NOT_ENOUGH_DATA; | ||
| 108 | } | ||
| 109 | // Check for correct version | ||
| 110 | if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) { | ||
| 111 | // We're all ready to roll. | ||
| 112 | fprintf(stderr, "get_iPhone() success\n"); | ||
| 113 | free(version); | ||
| 114 | *device = phone; | ||
| 115 | return IPHONE_E_SUCCESS; | ||
| 116 | } else { | ||
| 117 | // Bad header | ||
| 118 | iphone_free_device(phone); | ||
| 119 | free(version); | ||
| 120 | log_debug_msg("get_iPhone(): Received a bad header/invalid version number."); | ||
| 121 | return IPHONE_E_BAD_HEADER; | ||
| 122 | } | ||
| 123 | |||
| 124 | // If it got to this point it's gotta be bad | ||
| 125 | log_debug_msg("get_iPhone(): Unknown error.\n"); | ||
| 126 | iphone_free_device(phone); | ||
| 127 | free(version); | ||
| 128 | return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad | ||
| 129 | } | ||
| 130 | |||
| 131 | /** | ||
| 132 | * Scans all USB busses and devices for a known AFC-compatible device and | ||
| 133 | * returns a handle to the first such device it finds. Known devices include | ||
| 134 | * those with vendor ID 0x05ac and product ID between 0x1290 and 0x1293 | ||
| 135 | * inclusive. | ||
| 136 | * | ||
| 137 | * This function is convenient, but on systems where higher-level abstractions | ||
| 138 | * (such as HAL) are available it may be preferable to use | ||
| 139 | * iphone_get_specific_device instead, because it can deal with multiple | ||
| 140 | * connected devices as well as devices not known to libiphone. | ||
| 141 | * | ||
| 142 | * @param device Upon calling this function, a pointer to a location of type | ||
| 143 | * iphone_device_t, which must have the value NULL. On return, this location | ||
| 144 | * will be filled with a handle to the device. | ||
| 145 | * @return IPHONE_E_SUCCESS if ok, otherwise an error code. | ||
| 146 | */ | ||
| 147 | iphone_error_t iphone_get_device(iphone_device_t * device) | ||
| 148 | { | ||
| 149 | struct usb_bus *bus, *busses; | ||
| 150 | struct usb_device *dev; | ||
| 151 | |||
| 152 | usb_init(); | ||
| 153 | usb_find_busses(); | ||
| 154 | usb_find_devices(); | ||
| 155 | |||
| 156 | for (bus = usb_get_busses(); bus != NULL; bus = bus->next) | ||
| 157 | for (dev = bus->devices; dev != NULL; dev = dev->next) | ||
| 158 | if (dev->descriptor.idVendor == 0x05ac | ||
| 159 | && dev->descriptor.idProduct >= 0x1290 && dev->descriptor.idProduct <= 0x1293) | ||
| 160 | return iphone_get_specific_device(bus->location, dev->devnum, device); | ||
| 161 | |||
| 162 | return IPHONE_E_NO_DEVICE; | ||
| 163 | } | ||
| 164 | |||
| 165 | /** Cleans up an iPhone structure, then frees the structure itself. | ||
| 166 | * This is a library-level function; deals directly with the iPhone to tear | ||
| 167 | * down relations, but otherwise is mostly internal. | ||
| 168 | * | ||
| 169 | * @param phone A pointer to an iPhone structure. | ||
| 170 | */ | ||
| 171 | iphone_error_t iphone_free_device(iphone_device_t device) | ||
| 172 | { | ||
| 173 | if (!device) | ||
| 174 | return IPHONE_E_INVALID_ARG; | ||
| 175 | iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; | ||
| 176 | |||
| 177 | if (device->buffer) { | ||
| 178 | free(device->buffer); | ||
| 179 | } | ||
| 180 | if (device->device) { | ||
| 181 | usb_release_interface(device->device, 1); | ||
| 182 | usb_reset(device->device); | ||
| 183 | usb_close(device->device); | ||
| 184 | ret = IPHONE_E_SUCCESS; | ||
| 185 | } | ||
| 186 | free(device); | ||
| 187 | return ret; | ||
| 188 | } | ||
| 189 | |||
| 190 | /** Sends data to the phone | ||
| 191 | * This is a low-level (i.e. directly to phone) function. | ||
| 192 | * | ||
| 193 | * @param phone The iPhone to send data to | ||
| 194 | * @param data The data to send to the iPhone | ||
| 195 | * @param datalen The length of the data | ||
| 196 | * @return The number of bytes sent, or -1 on error or something. | ||
| 197 | */ | ||
| 198 | int send_to_phone(iphone_device_t phone, char *data, int datalen) | ||
| 199 | { | ||
| 200 | if (!phone) | ||
| 201 | return -1; | ||
| 202 | int bytes = 0; | ||
| 203 | |||
| 204 | if (!phone) | ||
| 205 | return -1; | ||
| 206 | log_debug_msg("send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data); | ||
| 207 | |||
| 208 | bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800); | ||
| 209 | if (bytes < datalen) { | ||
| 210 | if (bytes < 0) | ||
| 211 | log_debug_msg("send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), | ||
| 212 | strerror(-bytes)); | ||
| 213 | return -1; | ||
| 214 | } else { | ||
| 215 | return bytes; | ||
| 216 | } | ||
| 217 | |||
| 218 | return -1; | ||
| 219 | } | ||
| 220 | |||
| 221 | /** This function is a low-level (i.e. direct to iPhone) function. | ||
| 222 | * | ||
| 223 | * @param phone The iPhone to receive data from | ||
| 224 | * @param data Where to put data read | ||
| 225 | * @param datalen How much data to read in | ||
| 226 | * | ||
| 227 | * @return How many bytes were read in, or -1 on error. | ||
| 228 | */ | ||
| 229 | int recv_from_phone(iphone_device_t phone, char *data, int datalen) | ||
| 230 | { | ||
| 231 | if (!phone) | ||
| 232 | return -1; | ||
| 233 | int bytes = 0; | ||
| 234 | |||
| 235 | if (!phone) | ||
| 236 | return -1; | ||
| 237 | log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen); | ||
| 238 | |||
| 239 | bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500); | ||
| 240 | if (bytes < 0) { | ||
| 241 | log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), | ||
| 242 | strerror(-bytes)); | ||
| 243 | return -1; | ||
| 244 | } | ||
| 245 | |||
| 246 | return bytes; | ||
| 247 | } | ||
