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)
downloadlibimobiledevice-89050631439f71ad652e68b59020f8801e100e45.tar.gz
libimobiledevice-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" {
35#define IPHONE_E_NO_DEVICE -2 35#define IPHONE_E_NO_DEVICE -2
36#define IPHONE_E_TIMEOUT -3 36#define IPHONE_E_TIMEOUT -3
37#define IPHONE_E_NOT_ENOUGH_DATA -4 37#define IPHONE_E_NOT_ENOUGH_DATA -4
38#define IPHONE_E_BAD_HEADER -5
38 39
39//lockdownd specific error 40//lockdownd specific error
40#define IPHONE_E_INVALID_CONF -5 41#define IPHONE_E_INVALID_CONF -6
41#define IPHONE_E_PAIRING_FAILED -6 42#define IPHONE_E_PAIRING_FAILED -7
42#define IPHONE_E_SSL_ERROR -7 43#define IPHONE_E_SSL_ERROR -8
43 44
44//afc specific error 45//afc specific error
45#define IPHONE_E_NO_SUCH_FILE -8 46#define IPHONE_E_NO_SUCH_FILE -9
46 47
47struct iphone_device_int; 48struct iphone_device_int;
48typedef struct iphone_device_int *iphone_device_t; 49typedef 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;
34 * @return A structure with data on the first iPhone it finds. (Or NULL, on 34 * @return A structure with data on the first iPhone it finds. (Or NULL, on
35 * error) 35 * error)
36 */ 36 */
37iPhone_t get_iPhone() { 37int iphone_get_device ( iphone_device_t *device ){
38 iPhone *phone = (iPhone*)malloc(sizeof(iPhone)); 38 //check we can actually write in device
39 if (!device || (device && *device))
40 return IPHONE_E_INVALID_ARG;
41
39 struct usb_bus *bus, *busses; 42 struct usb_bus *bus, *busses;
40 struct usb_device *dev; 43 struct usb_device *dev;
44 iphone_device_t phone = (iphone_device_t)malloc(sizeof(struct iphone_device_int));
45 usbmux_version_header *version = version_header();
41 46
42 // Initialize the struct 47 // Initialize the struct
43 phone->device = NULL; 48 phone->device = NULL;
@@ -74,7 +79,7 @@ iPhone_t get_iPhone() {
74 if (!phone->device || !phone->__device) { 79 if (!phone->device || !phone->__device) {
75 free_iPhone(phone); 80 free_iPhone(phone);
76 if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n"); 81 if (debug) fprintf(stderr, "get_iPhone(): iPhone not found\n");
77 return NULL; 82 return IPHONE_E_NO_DEVICE;
78 } 83 }
79 84
80 // Send the version command to the phone 85 // Send the version command to the phone
@@ -99,7 +104,7 @@ iPhone_t get_iPhone() {
99 if (debug) fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n"); 104 if (debug) fprintf(stderr, "get_iPhone(): Invalid version message -- header too short.\n");
100 if (debug && bytes < 0) fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n", 105 if (debug && bytes < 0) fprintf(stderr, "get_iPhone(): libusb error message %d: %s (%s)\n",
101 bytes, usb_strerror(), strerror(-bytes)); 106 bytes, usb_strerror(), strerror(-bytes));
102 return NULL; 107 return IPHONE_E_NOT_ENOUGH_DATA;
103 } 108 }
104 109
105 // Check for correct version 110 // Check for correct version
@@ -107,20 +112,21 @@ iPhone_t get_iPhone() {
107 // We're all ready to roll. 112 // We're all ready to roll.
108 fprintf(stderr, "get_iPhone() success\n"); 113 fprintf(stderr, "get_iPhone() success\n");
109 free(version); 114 free(version);
110 return phone; 115 *device = phone;
116 return IPHONE_E_SUCCESS;
111 } else { 117 } else {
112 // Bad header 118 // Bad header
113 free_iPhone(phone); 119 free_iPhone(phone);
114 free(version); 120 free(version);
115 if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number."); 121 if (debug) fprintf(stderr, "get_iPhone(): Received a bad header/invalid version number.");
116 return NULL; 122 return IPHONE_E_BAD_HEADER;
117 } 123 }
118 124
119 // If it got to this point it's gotta be bad 125 // If it got to this point it's gotta be bad
120 if (debug) fprintf(stderr, "get_iPhone(): Unknown error.\n"); 126 if (debug) fprintf(stderr, "get_iPhone(): Unknown error.\n");
121 free_iPhone(phone); 127 free_iPhone(phone);
122 free(version); 128 free(version);
123 return NULL; 129 return IPHONE_E_NO_DEVICE; // if it got to this point it's gotta be bad
124} 130}
125 131
126/** Cleans up an iPhone structure, then frees the structure itself. 132/** Cleans up an iPhone structure, then frees the structure itself.
@@ -129,14 +135,14 @@ iPhone_t get_iPhone() {
129 * 135 *
130 * @param phone A pointer to an iPhone structure. 136 * @param phone A pointer to an iPhone structure.
131 */ 137 */
132void free_iPhone(iPhone_t phone) { 138void iphone_free_device ( iphone_device_t device ) {
133 if (phone->buffer) free(phone->buffer); 139 if (device->buffer) free(device->buffer);
134 if (phone->device) { 140 if (device->device) {
135 usb_release_interface(phone->device, 1); 141 usb_release_interface(device->device, 1);
136 usb_reset(phone->device); 142 usb_reset(device->device);
137 usb_close(phone->device); 143 usb_close(device->device);
138 } 144 }
139 free(phone); 145 free(device);
140} 146}
141 147
142/** Sends data to the phone 148/** Sends data to the phone
@@ -147,7 +153,8 @@ void free_iPhone(iPhone_t phone) {
147 * @param datalen The length of the data 153 * @param datalen The length of the data
148 * @return The number of bytes sent, or -1 on error or something. 154 * @return The number of bytes sent, or -1 on error or something.
149 */ 155 */
150int send_to_phone(iPhone *phone, char *data, int datalen) { 156int send_to_phone(iphone_device_t phone, char *data, int datalen) {
157 if (!phone) return -1;
151 int bytes = 0; 158 int bytes = 0;
152 159
153 if (!phone) return -1; 160 if (!phone) return -1;
@@ -173,7 +180,8 @@ int send_to_phone(iPhone *phone, char *data, int datalen) {
173 * 180 *
174 * @return How many bytes were read in, or -1 on error. 181 * @return How many bytes were read in, or -1 on error.
175 */ 182 */
176int recv_from_phone(iPhone *phone, char *data, int datalen) { 183int recv_from_phone(iphone_device_t phone, char *data, int datalen) {
184 if (!phone) return -1;
177 int bytes = 0; 185 int bytes = 0;
178 186
179 if (!phone) return -1; 187 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 @@
33#define BULKIN 0x85 33#define BULKIN 0x85
34#define BULKOUT 0x04 34#define BULKOUT 0x04
35 35
36typedef struct iPhone_s { 36struct iphone_device_int {
37 char *buffer; 37 char *buffer;
38 struct usb_dev_handle *device; 38 struct usb_dev_handle *device;
39 struct usb_device *__device; 39 struct usb_device *__device;
40} iPhone; 40};
41 41
42// Function definitions 42// Function definitions
43int send_to_phone(iPhone *phone, char *data, int datalen); 43int send_to_phone(iphone_device_t phone, char *data, int datalen);
44int recv_from_phone(iPhone *phone, char *data, int datalen); 44int recv_from_phone(iphone_device_t phone, char *data, int datalen);
45#endif 45#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) {
116 * 116 *
117 * @return A mux TCP header for the connection which is used for tracking and data transfer. 117 * @return A mux TCP header for the connection which is used for tracking and data transfer.
118 */ 118 */
119usbmux_connection *mux_connect(iPhone *phone, uint16 s_port, uint16 d_port) { 119usbmux_connection *mux_connect(iphone_device_t phone, uint16 s_port, uint16 d_port) {
120 if (!phone || !s_port || !d_port) return NULL; 120 if (!phone || !s_port || !d_port) return NULL;
121 int bytes = 0; 121 int bytes = 0;
122 // Initialize connection stuff 122 // 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 {
45 45
46typedef struct { 46typedef struct {
47 usbmux_tcp_header *header; 47 usbmux_tcp_header *header;
48 iPhone *phone; 48 iphone_device_t phone;
49 char *recv_buffer; 49 char *recv_buffer;
50 int r_len; 50 int r_len;
51} usbmux_connection; 51} usbmux_connection;
@@ -58,7 +58,7 @@ typedef struct {
58 58
59usbmux_version_header *version_header(); 59usbmux_version_header *version_header();
60 60
61usbmux_connection *mux_connect(iPhone *phone, uint16 s_port, uint16 d_port); 61usbmux_connection *mux_connect(iphone_device_t phone, uint16 s_port, uint16 d_port);
62void mux_close_connection(usbmux_connection *connection); 62void mux_close_connection(usbmux_connection *connection);
63int mux_send(usbmux_connection *connection, const char *data, uint32 datalen); 63int mux_send(usbmux_connection *connection, const char *data, uint32 datalen);
64int mux_recv(usbmux_connection *connection, char *data, uint32 datalen); 64int mux_recv(usbmux_connection *connection, char *data, uint32 datalen);