summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-02-20 14:31:45 +0100
committerGravatar Nikias Bassen2009-02-20 14:31:45 +0100
commit2ae80d9dd4855b0dd3e0c74e2f6d03b2b6ecd7c0 (patch)
tree3cc82db4bc424ec4681eec0b6389e76761f52c27
parent71d8775f865b431135cd3c178763d0a294b8ff9e (diff)
downloadusbmuxd-2ae80d9dd4855b0dd3e0c74e2f6d03b2b6ecd7c0.tar.gz
usbmuxd-2ae80d9dd4855b0dd3e0c74e2f6d03b2b6ecd7c0.tar.bz2
Added comments and did some small code cleanup.
-rw-r--r--usbmuxd.c71
1 files changed, 54 insertions, 17 deletions
diff --git a/usbmuxd.c b/usbmuxd.c
index 37a7f9e..8f2a6e9 100644
--- a/usbmuxd.c
+++ b/usbmuxd.c
@@ -70,6 +70,9 @@ static struct device_use_info **device_use_list = NULL;
70static int device_use_count = 0; 70static int device_use_count = 0;
71static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; 71static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER;
72 72
73/**
74 * for debugging purposes.
75 */
73static void print_buffer(const char *data, const int length) 76static void print_buffer(const char *data, const int length)
74{ 77{
75 int i; 78 int i;
@@ -101,6 +104,17 @@ static void print_buffer(const char *data, const int length)
101 printf("\n"); 104 printf("\n");
102} 105}
103 106
107/**
108 * Read incoming usbmuxd packet. If the packet is larger than
109 * the size specified by len, the data will be truncated.
110 *
111 * @param fd the file descriptor to read from.
112 * @param data pointer to a buffer to store the read data to.
113 * @param len the length of the data to be read. The buffer
114 * pointed to by data should be at least len bytes in size.
115 *
116 * @return
117 */
104static int usbmuxd_get_request(int fd, void *data, size_t len) 118static int usbmuxd_get_request(int fd, void *data, size_t len)
105{ 119{
106 uint32_t pktlen; 120 uint32_t pktlen;
@@ -117,13 +131,22 @@ static int usbmuxd_get_request(int fd, void *data, size_t len)
117 } 131 }
118 132
119 recv_len = recv_buf(fd, data, pktlen); 133 recv_len = recv_buf(fd, data, pktlen);
120 if (recv_len < pktlen) { 134 if ((recv_len > 0) && (recv_len < pktlen)) {
121 fprintf(stderr, "%s: Uh-oh, we got less than the packet's size, %d instead of %d...\n", __func__, recv_len, pktlen); 135 fprintf(stderr, "%s: Uh-oh, we got less than the packet's size, %d instead of %d...\n", __func__, recv_len, pktlen);
122 } 136 }
123 137
124 return recv_len; 138 return recv_len;
125} 139}
126 140
141/**
142 * Send a usbmuxd result packet with given tag and result_code.
143 *
144 * @param fd the file descriptor to write to.
145 * @param tag the tag value that identifies where this message belongs to.
146 * @param result_code the error value (0 = Success, most likely errno values otherwise)
147 *
148 * @return the return value returned by send_buf (normally the number of bytes sent)
149 */
127static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code) 150static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code)
128{ 151{
129 struct usbmux_result res; 152 struct usbmux_result res;
@@ -140,7 +163,12 @@ static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code)
140} 163}
141 164
142/** 165/**
166 * this thread reads from the usb connection and writes the
167 * data to the connected client.
143 * 168 *
169 * @param arg pointer to a client_data structure.
170 *
171 * @return NULL in any case
144 */ 172 */
145static void *usbmuxd_client_reader_thread(void *arg) 173static void *usbmuxd_client_reader_thread(void *arg)
146{ 174{
@@ -184,16 +212,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
184 212
185 cursor = rbuffer; 213 cursor = rbuffer;
186 while (rlen > 0) { 214 while (rlen > 0) {
187 //printf("%s: \n", __func__); 215 len = send_buf(cdata->socket, cursor, rlen);
188 //print_buffer(cursor, rlen);
189 //if ((rlen > 4) && !cursor[3]) {
190 len = send_buf(cdata->socket, cursor, rlen);
191 /*} else if (cursor[0] == 1) {
192 fprintf(stderr, "%s: Error message received: %s\n", __func__, cursor+1);
193 // we got an error message and no data. don't send it.
194 // TODO parse the error code and put it in the right place!
195 len = rlen;
196 }*/
197 // calculate remainder 216 // calculate remainder
198 rlen -= len; 217 rlen -= len;
199 // advance cursor 218 // advance cursor
@@ -209,6 +228,16 @@ static void *usbmuxd_client_reader_thread(void *arg)
209 return NULL; 228 return NULL;
210} 229}
211 230
231/**
232 * This function handles the connecting procedure to a previously
233 * set up usbmux client.
234 * Sends a usbmuxd result packet denoting success or failure.
235 * A successful result is mandatory for later communication.
236 *
237 * @param cdata pointer to a previously initialized client_data structure
238 *
239 * @return
240 */
212static int usbmuxd_handleConnectResult(struct client_data *cdata) 241static int usbmuxd_handleConnectResult(struct client_data *cdata)
213{ 242{
214 int result; 243 int result;
@@ -219,7 +248,11 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
219 uint32_t rlen; 248 uint32_t rlen;
220 iphone_error_t err; 249 iphone_error_t err;
221 250
222 // trigger connection attempt if ready to write to client 251 if (!cdata) {
252 fprintf(stderr, "%s: Invalid client_data provided!\n", __func__);
253 return -EINVAL;
254 }
255
223 result = check_fd(cdata->socket, fdwrite, DEFAULT_TIMEOUT); 256 result = check_fd(cdata->socket, fdwrite, DEFAULT_TIMEOUT);
224 if (result <= 0) { 257 if (result <= 0) {
225 if (result < 0) { 258 if (result < 0) {
@@ -231,7 +264,8 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
231 err = iphone_mux_recv_timeout(cdata->muxclient, buffer, maxlen, &rlen, DEFAULT_TIMEOUT); 264 err = iphone_mux_recv_timeout(cdata->muxclient, buffer, maxlen, &rlen, DEFAULT_TIMEOUT);
232 if (err != 0) { 265 if (err != 0) {
233 fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err); 266 fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err);
234 usbmuxd_send_result(cdata->socket, cdata->tag, err); 267 usbmuxd_send_result(cdata->socket, cdata->tag, -err);
268 return err;
235 } else { 269 } else {
236 if (rlen > 0) { 270 if (rlen > 0) {
237 //print_buffer(buffer, rlen); 271 //print_buffer(buffer, rlen);
@@ -242,10 +276,11 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
242 276
243 if (sscanf(buffer+22, "%s - %d\n", err_type, &err_code) == 2) { 277 if (sscanf(buffer+22, "%s - %d\n", err_type, &err_code) == 2) {
244 usbmuxd_send_result(cdata->socket, cdata->tag, err_code); 278 usbmuxd_send_result(cdata->socket, cdata->tag, err_code);
279 return -err_code;
245 } else { 280 } else {
246 usbmuxd_send_result(cdata->socket, cdata->tag, ENODATA); 281 usbmuxd_send_result(cdata->socket, cdata->tag, ENODATA);
282 return -ENODATA;
247 } 283 }
248 return -2;
249 } else { 284 } else {
250 // send success result 285 // send success result
251 usbmuxd_send_result(cdata->socket, cdata->tag, 0); 286 usbmuxd_send_result(cdata->socket, cdata->tag, 0);
@@ -255,12 +290,11 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
255 } else { 290 } else {
256 // no server greeting? this seems to be ok. send success. 291 // no server greeting? this seems to be ok. send success.
257 usbmuxd_send_result(cdata->socket, cdata->tag, 0); 292 usbmuxd_send_result(cdata->socket, cdata->tag, 0);
258 return 0;
259 } 293 }
260 } 294 }
261 //fsync(cdata->socket); 295 //fsync(cdata->socket);
262 } 296 }
263 return 0; 297 return result;
264} 298}
265 299
266/** 300/**
@@ -612,7 +646,7 @@ static int daemonize()
612} 646}
613 647
614/** 648/**
615 * signal handler function for cleaning up stuff 649 * signal handler function for cleaning up properly
616 */ 650 */
617static void clean_exit(int sig) 651static void clean_exit(int sig)
618{ 652{
@@ -745,6 +779,9 @@ static void *usbmuxd_accept_thread(void *arg)
745 return NULL; 779 return NULL;
746} 780}
747 781
782/**
783 * main function.
784 */
748int main(int argc, char **argv) 785int main(int argc, char **argv)
749{ 786{
750 int foreground = 1; 787 int foreground = 1;