summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Jonathan Beck2008-08-19 22:55:46 +0200
committerGravatar Jonathan Beck2008-08-31 19:27:20 +0200
commit89050631439f71ad652e68b59020f8801e100e45 (patch)
treed1b64235991578d6b6bec70a2a0c371c2171a2f7
parent318f4bd51a25d5572f2dfd6f26c89fce52f43076 (diff)
downloadlibplist-89050631439f71ad652e68b59020f8801e100e45.tar.gz
libplist-89050631439f71ad652e68b59020f8801e100e45.tar.bz2
migrate iphone.c
-rw-r--r--include/libiphone/libiphone.h9
-rw-r--r--src/iphone.c40
-rw-r--r--src/iphone.h8
-rw-r--r--src/usbmux.c2
-rw-r--r--src/usbmux.h4
5 files changed, 36 insertions, 27 deletions
diff --git a/include/libiphone/libiphone.h b/include/libiphone/libiphone.h
index 230343e..241c6fa 100644
--- a/include/libiphone/libiphone.h
+++ b/include/libiphone/libiphone.h
@@ -35,14 +35,15 @@ extern "C" {
#define IPHONE_E_NO_DEVICE -2
#define IPHONE_E_TIMEOUT -3
#define IPHONE_E_NOT_ENOUGH_DATA -4
+#define IPHONE_E_BAD_HEADER -5
//lockdownd specific error
-#define IPHONE_E_INVALID_CONF -5
-#define IPHONE_E_PAIRING_FAILED -6
-#define IPHONE_E_SSL_ERROR -7
+#define IPHONE_E_INVALID_CONF -6
+#define IPHONE_E_PAIRING_FAILED -7
+#define IPHONE_E_SSL_ERROR -8
//afc specific error
-#define IPHONE_E_NO_SUCH_FILE -8
+#define IPHONE_E_NO_SUCH_FILE -9
struct iphone_device_int;
typedef struct iphone_device_int *iphone_device_t;
diff --git a/src/iphone.c b/src/iphone.c
index e0e150f..f3b7202 100644
--- a/src/iphone.c
+++ b/src/iphone.c
@@ -34,10 +34,15 @@ extern int debug;
* @return A structure with data on the first iPhone it finds. (Or NULL, on
* error)
*/
-iPhone_t get_iPhone() {
- iPhone *phone = (iPhone*)malloc(sizeof(iPhone));
+int 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));
+ usbmux_version_header *version = version_header();
// Initialize the struct
phone->device = NULL;
@@ -74,7 +79,7 @@ iPhone_t get_iPhone() {
if (!phone->device || !phone->__device) {
free_iPhone(phone);
if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n");
- return NULL;
+ return IPHONE_E_NO_DEVICE;
}
// Send the version command to the phone
@@ -99,7 +104,7 @@ iPhone_t get_iPhone() {
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 NULL;
+ return IPHONE_E_NOT_ENOUGH_DATA;
}
// Check for correct version
@@ -107,20 +112,21 @@ iPhone_t get_iPhone() {
// We're all ready to roll.
fprintf(stderr, "get_iPhone() success\n");
free(version);
- return phone;
+ *device = phone;
+ return IPHONE_E_SUCCESS;
} else {
// Bad header
free_iPhone(phone);
free(version);
if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
- return NULL;
+ 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");
free_iPhone(phone);
free(version);
- return NULL;
+ return IPHONE_E_NO_DEVICE; // if it got to this point it's gotta be bad
}
/** Cleans up an iPhone structure, then frees the structure itself.
@@ -129,14 +135,14 @@ iPhone_t get_iPhone() {
*
* @param phone A pointer to an iPhone structure.
*/
-void free_iPhone(iPhone_t phone) {
- if (phone->buffer) free(phone->buffer);
- if (phone->device) {
- usb_release_interface(phone->device, 1);
- usb_reset(phone->device);
- usb_close(phone->device);
+void iphone_free_device ( iphone_device_t device ) {
+ if (device->buffer) free(device->buffer);
+ if (device->device) {
+ usb_release_interface(device->device, 1);
+ usb_reset(device->device);
+ usb_close(device->device);
}
- free(phone);
+ free(device);
}
/** Sends data to the phone
@@ -147,7 +153,8 @@ void free_iPhone(iPhone_t phone) {
* @param datalen The length of the data
* @return The number of bytes sent, or -1 on error or something.
*/
-int send_to_phone(iPhone *phone, char *data, int datalen) {
+int send_to_phone(iphone_device_t phone, char *data, int datalen) {
+ if (!phone) return -1;
int bytes = 0;
if (!phone) return -1;
@@ -173,7 +180,8 @@ int send_to_phone(iPhone *phone, char *data, int datalen) {
*
* @return How many bytes were read in, or -1 on error.
*/
-int recv_from_phone(iPhone *phone, char *data, int datalen) {
+int recv_from_phone(iphone_device_t phone, char *data, int datalen) {
+ if (!phone) return -1;
int bytes = 0;
if (!phone) return -1;
diff --git a/src/iphone.h b/src/iphone.h
index f12d0eb..556a93a 100644
--- a/src/iphone.h
+++ b/src/iphone.h
@@ -33,13 +33,13 @@
#define BULKIN 0x85
#define BULKOUT 0x04
-typedef struct iPhone_s {
+struct iphone_device_int {
char *buffer;
struct usb_dev_handle *device;
struct usb_device *__device;
-} iPhone;
+};
// Function definitions
-int send_to_phone(iPhone *phone, char *data, int datalen);
-int recv_from_phone(iPhone *phone, char *data, int datalen);
+int send_to_phone(iphone_device_t phone, char *data, int datalen);
+int recv_from_phone(iphone_device_t phone, char *data, int datalen);
#endif
diff --git a/src/usbmux.c b/src/usbmux.c
index a71439d..a4a859a 100644
--- a/src/usbmux.c
+++ b/src/usbmux.c
@@ -116,7 +116,7 @@ void add_connection(usbmux_connection *connection) {
*
* @return A mux TCP header for the connection which is used for tracking and data transfer.
*/
-usbmux_connection *mux_connect(iPhone *phone, uint16 s_port, uint16 d_port) {
+usbmux_connection *mux_connect(iphone_device_t phone, uint16 s_port, uint16 d_port) {
if (!phone || !s_port || !d_port) return NULL;
int bytes = 0;
// Initialize connection stuff
diff --git a/src/usbmux.h b/src/usbmux.h
index dd3ee07..831f0fd 100644
--- a/src/usbmux.h
+++ b/src/usbmux.h
@@ -45,7 +45,7 @@ typedef struct {
typedef struct {
usbmux_tcp_header *header;
- iPhone *phone;
+ iphone_device_t phone;
char *recv_buffer;
int r_len;
} usbmux_connection;
@@ -58,7 +58,7 @@ typedef struct {
usbmux_version_header *version_header();
-usbmux_connection *mux_connect(iPhone *phone, uint16 s_port, uint16 d_port);
+usbmux_connection *mux_connect(iphone_device_t phone, uint16 s_port, uint16 d_port);
void mux_close_connection(usbmux_connection *connection);
int mux_send(usbmux_connection *connection, const char *data, uint32 datalen);
int mux_recv(usbmux_connection *connection, char *data, uint32 datalen);