summaryrefslogtreecommitdiffstats
path: root/src/iphone.c
diff options
context:
space:
mode:
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 @@
#include <stdlib.h>
#include <string.h>
-extern int debug;
+extern int debug;
/** Gets a handle to an iPhone
*
* @return A structure with data on the first iPhone it finds. (Or NULL, on
* error)
*/
-iphone_error_t iphone_get_device ( iphone_device_t *device ){
+iphone_error_t iphone_get_device(iphone_device_t * device)
+{
//check we can actually write in device
if (!device || (device && *device))
return IPHONE_E_INVALID_ARG;
struct usb_bus *bus, *busses;
struct usb_device *dev;
- iphone_device_t phone = (iphone_device_t)malloc(sizeof(struct iphone_device_int));
-
+ iphone_device_t phone = (iphone_device_t) malloc(sizeof(struct iphone_device_int));
+
// Initialize the struct
phone->device = NULL;
phone->__device = NULL;
- phone->buffer = NULL;
-
+ phone->buffer = NULL;
+
// Initialize libusb
usb_init();
usb_find_busses();
usb_find_devices();
busses = usb_get_busses();
-
+
// Set the device configuration
- for (bus = busses; bus; bus = bus->next) {
+ for (bus = busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
- if (dev->descriptor.idVendor == 0x05ac &&
+ if (dev->descriptor.idVendor == 0x05ac &&
(dev->descriptor.idProduct == 0x1290 ||
- dev->descriptor.idProduct == 0x1291 ||
- dev->descriptor.idProduct == 0x1292
- )
- ) {
+ dev->descriptor.idProduct == 0x1291 || dev->descriptor.idProduct == 0x1292)
+ ) {
phone->__device = dev;
phone->device = usb_open(phone->__device);
usb_set_configuration(phone->device, 3);
@@ -71,20 +70,21 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
break;
}
}
- if (phone->__device && phone->device) break;
+ if (phone->__device && phone->device)
+ break;
}
-
+
// Check to see if we are connected
if (!phone->device || !phone->__device) {
iphone_free_device(phone);
- if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n");
+ if (debug)
+ fprintf(stderr, "get_iPhone(): iPhone not found\n");
return IPHONE_E_NO_DEVICE;
}
-
// Send the version command to the phone
int bytes = 0;
usbmux_version_header *version = version_header();
- bytes = usb_bulk_write(phone->device, BULKOUT, (char*)version, sizeof(*version), 800);
+ bytes = usb_bulk_write(phone->device, BULKOUT, (char *) version, sizeof(*version), 800);
if (bytes < 20 && debug) {
fprintf(stderr, "get_iPhone(): libusb did NOT send enough!\n");
if (bytes < 0) {
@@ -92,20 +92,20 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
bytes, usb_strerror(), strerror(-bytes));
}
}
-
// Read the phone's response
- bytes = usb_bulk_read(phone->device, BULKIN, (char*)version, sizeof(*version), 800);
-
+ bytes = usb_bulk_read(phone->device, BULKIN, (char *) version, sizeof(*version), 800);
+
// Check for bad response
if (bytes < 20) {
free(version);
iphone_free_device(phone);
- if (debug) fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n");
- if (debug && bytes < 0) fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
- bytes, usb_strerror(), strerror(-bytes));
+ if (debug)
+ fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n");
+ if (debug && bytes < 0)
+ fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
+ bytes, usb_strerror(), strerror(-bytes));
return IPHONE_E_NOT_ENOUGH_DATA;
}
-
// Check for correct version
if (ntohl(version->major) == 1 && ntohl(version->minor) == 0) {
// We're all ready to roll.
@@ -117,15 +117,17 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
// Bad header
iphone_free_device(phone);
free(version);
- if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
+ if (debug)
+ fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
return IPHONE_E_BAD_HEADER;
}
// If it got to this point it's gotta be bad
- if (debug) fprintf(stderr, "get_iPhone(): Unknown error.\n");
+ if (debug)
+ fprintf(stderr, "get_iPhone(): Unknown error.\n");
iphone_free_device(phone);
free(version);
- return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
+ return IPHONE_E_UNKNOWN_ERROR; // if it got to this point it's gotta be bad
}
/** Cleans up an iPhone structure, then frees the structure itself.
@@ -134,8 +136,10 @@ iphone_error_t iphone_get_device ( iphone_device_t *device ){
*
* @param phone A pointer to an iPhone structure.
*/
-iphone_error_t iphone_free_device ( iphone_device_t device ) {
- if (!device) return IPHONE_E_INVALID_ARG;
+iphone_error_t iphone_free_device(iphone_device_t device)
+{
+ if (!device)
+ return IPHONE_E_INVALID_ARG;
iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
if (device->buffer) {
@@ -150,7 +154,7 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) {
free(device);
return ret;
}
-
+
/** Sends data to the phone
* This is a low-level (i.e. directly to phone) function.
*
@@ -159,22 +163,27 @@ iphone_error_t iphone_free_device ( iphone_device_t device ) {
* @param datalen The length of the data
* @return The number of bytes sent, or -1 on error or something.
*/
-int send_to_phone(iphone_device_t phone, char *data, int datalen) {
- if (!phone) return -1;
+int send_to_phone(iphone_device_t phone, char *data, int datalen)
+{
+ if (!phone)
+ return -1;
int bytes = 0;
-
- if (!phone) return -1;
- if (debug) fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
+
+ if (!phone)
+ return -1;
+ if (debug)
+ fprintf(stderr, "send_to_phone: Attempting to send datalen = %i data = %p\n", datalen, data);
bytes = usb_bulk_write(phone->device, BULKOUT, data, datalen, 800);
if (bytes < datalen) {
- if(debug && bytes < 0)
- fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(), strerror(-bytes));
+ if (debug && bytes < 0)
+ fprintf(stderr, "send_to_iphone(): libusb gave me the error %d: %s - %s\n", bytes, usb_strerror(),
+ strerror(-bytes));
return -1;
} else {
return bytes;
}
-
+
return -1;
}
@@ -186,18 +195,24 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) {
*
* @return How many bytes were read in, or -1 on error.
*/
-int recv_from_phone(iphone_device_t phone, char *data, int datalen) {
- if (!phone) return -1;
+int recv_from_phone(iphone_device_t phone, char *data, int datalen)
+{
+ if (!phone)
+ return -1;
int bytes = 0;
-
- if (!phone) return -1;
- if (debug) fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
-
+
+ if (!phone)
+ return -1;
+ if (debug)
+ fprintf(stderr, "recv_from_phone(): attempting to receive %i bytes\n", datalen);
+
bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, 3500);
if (bytes < 0) {
- if(debug) fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), strerror(-bytes));
+ if (debug)
+ fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
+ strerror(-bytes));
return -1;
}
-
+
return bytes;
}