summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2009-04-12 13:09:25 +0200
committerGravatar Nikias Bassen2009-04-12 13:09:25 +0200
commite1751b8c02822da3881b41a8e07a36665a163130 (patch)
tree76679eec9f375789c5e302f68ef27d7dc1643825
parent0915fa88ac895e2ccbe6e34669c8b1bd73a5034e (diff)
downloadusbmuxd-e1751b8c02822da3881b41a8e07a36665a163130.tar.gz
usbmuxd-e1751b8c02822da3881b41a8e07a36665a163130.tar.bz2
better error handling, some cleanup, and changed confusing
struct/variable naming.
-rw-r--r--iphone.c49
-rw-r--r--iphone.h3
-rw-r--r--main.c128
3 files changed, 86 insertions, 94 deletions
diff --git a/iphone.c b/iphone.c
index ad9dafd..5cceb95 100644
--- a/iphone.c
+++ b/iphone.c
@@ -114,7 +114,13 @@ static int clients = 0;
114 114
115/** 115/**
116 */ 116 */
117int toto_debug = 1; 117int toto_debug = 0;
118
119void iphone_set_debug(int e)
120{
121 toto_debug = e;
122}
123
118void log_debug_msg(const char *format, ...) 124void log_debug_msg(const char *format, ...)
119{ 125{
120#ifndef STRIP_DEBUG_CODE 126#ifndef STRIP_DEBUG_CODE
@@ -518,13 +524,11 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen)
518 */ 524 */
519int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int timeoutmillis) 525int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int timeoutmillis)
520{ 526{
521 if (!phone)
522 return -1;
523 int bytes = 0; 527 int bytes = 0;
524 528
525 if (!phone) 529 if (!phone)
526 return -1; 530 return -EINVAL;
527 log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen); 531 //log_debug_msg("recv_from_phone(): attempting to receive %i bytes\n", datalen);
528 532
529 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, timeoutmillis); 533 bytes = usb_bulk_read(phone->device, BULKIN, data, datalen, timeoutmillis);
530 if (bytes < 0) { 534 if (bytes < 0) {
@@ -534,6 +538,7 @@ int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int
534 if (bytes == -ETIMEDOUT) { 538 if (bytes == -ETIMEDOUT) {
535 // ignore this. it just means timeout reached before we 539 // ignore this. it just means timeout reached before we
536 // picked up any data. no problem. 540 // picked up any data. no problem.
541 return 0;
537 } 542 }
538 else { 543 else {
539 fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), 544 fprintf(stderr, "recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
@@ -541,7 +546,7 @@ int recv_from_phone_timeout(iphone_device_t phone, char *data, int datalen, int
541 log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(), 546 log_debug_msg("recv_from_phone(): libusb gave me the error %d: %s (%s)\n", bytes, usb_strerror(),
542 strerror(-bytes)); 547 strerror(-bytes));
543 } 548 }
544 return -1; 549 return bytes;
545 } 550 }
546 551
547#ifdef DEBUG 552#ifdef DEBUG
@@ -736,7 +741,7 @@ iphone_error_t iphone_mux_new_client(iphone_device_t device, uint16_t src_port,
736 add_connection(new_connection); 741 add_connection(new_connection);
737 new_connection->error = IPHONE_E_SUCCESS; 742 new_connection->error = IPHONE_E_SUCCESS;
738 hton_header(new_connection->header); 743 hton_header(new_connection->header);
739 printf("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(new_connection->header->sport), ntohs(new_connection->header->dport)); 744 log_debug_msg("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(new_connection->header->sport), ntohs(new_connection->header->dport));
740 if (send_to_phone(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)) >= 0) { 745 if (send_to_phone(device, (char *) new_connection->header, sizeof(usbmux_tcp_header)) >= 0) {
741 *client = new_connection; 746 *client = new_connection;
742 return IPHONE_E_SUCCESS; 747 return IPHONE_E_SUCCESS;
@@ -842,7 +847,7 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui
842 memcpy(buffer, client->header, sizeof(usbmux_tcp_header)); 847 memcpy(buffer, client->header, sizeof(usbmux_tcp_header));
843 memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen); 848 memcpy(buffer + sizeof(usbmux_tcp_header), data, datalen);
844 849
845 printf("%s: send_to_phone(%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport)); 850 log_debug_msg("%s: send_to_phone(%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport));
846 sendresult = send_to_phone(client->phone, buffer, blocksize); 851 sendresult = send_to_phone(client->phone, buffer, blocksize);
847 // Now that we've sent it off, we can clean up after our sloppy selves. 852 // Now that we've sent it off, we can clean up after our sloppy selves.
848 if (buffer) 853 if (buffer)
@@ -907,9 +912,9 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet)
907 // falls on our responsibility because we are the ones reading in 912 // falls on our responsibility because we are the ones reading in
908 // feedback. 913 // feedback.
909 if (client->header->scnt == 0 && client->header->ocnt == 0 ) { 914 if (client->header->scnt == 0 && client->header->ocnt == 0 ) {
910 fprintf(stdout, "client is still waiting for handshake.\n"); 915 log_debug_msg("client is still waiting for handshake.\n");
911 if (header->tcp_flags == (TCP_SYN | TCP_ACK)) { 916 if (header->tcp_flags == (TCP_SYN | TCP_ACK)) {
912 fprintf(stdout, "yes, got syn+ack ; replying with ack.\n"); 917 log_debug_msg("yes, got syn+ack ; replying with ack.\n");
913 client->header->tcp_flags = TCP_ACK; 918 client->header->tcp_flags = TCP_ACK;
914 client->header->length = sizeof(usbmux_tcp_header); 919 client->header->length = sizeof(usbmux_tcp_header);
915 client->header->length16 = sizeof(usbmux_tcp_header); 920 client->header->length16 = sizeof(usbmux_tcp_header);
@@ -918,9 +923,9 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet)
918 hton_header(client->header); 923 hton_header(client->header);
919 // push it to USB 924 // push it to USB
920 // TODO: need to check for error in the send here.... :( 925 // TODO: need to check for error in the send here.... :(
921 printf("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport)); 926 log_debug_msg("%s: send_to_phone (%d --> %d)\n", __func__, ntohs(client->header->sport), ntohs(client->header->dport));
922 if (send_to_phone(client->phone, (char *)client->header, sizeof(usbmux_tcp_header)) <= 0) { 927 if (send_to_phone(client->phone, (char *)client->header, sizeof(usbmux_tcp_header)) <= 0) {
923 fprintf(stdout, "%s: error when pushing to usb...\n", __func__); 928 log_debug_msg("%s: error when pushing to usb...\n", __func__);
924 } 929 }
925 // need to revert some of the fields back to host notation. 930 // need to revert some of the fields back to host notation.
926 ntoh_header(client->header); 931 ntoh_header(client->header);
@@ -929,7 +934,7 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet)
929 client->error = IPHONE_E_ECONNABORTED; 934 client->error = IPHONE_E_ECONNABORTED;
930 // woah... this connection failed us. 935 // woah... this connection failed us.
931 // TODO: somehow signal that this stream is a no-go. 936 // TODO: somehow signal that this stream is a no-go.
932 fprintf(stderr, "WOAH! client failed to get proper syn+ack.\n"); 937 log_debug_msg("WOAH! client failed to get proper syn+ack.\n");
933 } 938 }
934 } 939 }
935 940
@@ -1002,7 +1007,7 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet)
1002 1007
1003 // ensure there is enough space, either by first malloc or realloc 1008 // ensure there is enough space, either by first malloc or realloc
1004 if (datalen > 0) { 1009 if (datalen > 0) {
1005 fprintf(stderr, "%s: putting %d bytes into client's recv_buffer\n", __func__, datalen); 1010 log_debug_msg("%s: putting %d bytes into client's recv_buffer\n", __func__, datalen);
1006 if (client->r_len == 0) dobroadcast = 1; 1011 if (client->r_len == 0) dobroadcast = 1;
1007 1012
1008 if (client->recv_buffer == NULL) { 1013 if (client->recv_buffer == NULL) {
@@ -1061,13 +1066,14 @@ iphone_umux_client_t find_client(usbmux_tcp_header* recv_header)
1061 1066
1062/** pull in a big USB bulk packet and distribute it to queues appropriately. 1067/** pull in a big USB bulk packet and distribute it to queues appropriately.
1063 */ 1068 */
1064void iphone_mux_pullbulk(iphone_device_t phone) 1069int iphone_mux_pullbulk(iphone_device_t phone)
1065{ 1070{
1066 if (!phone) { 1071 if (!phone) {
1067 fprintf(stderr, "iphone_mux_pullbulk: invalid argument\n"); 1072 fprintf(stderr, "iphone_mux_pullbulk: invalid argument\n");
1068 return; 1073 return -EINVAL;
1069 } 1074 }
1070 1075
1076 int res = 0;
1071 static const int DEFAULT_CAPACITY = 128*1024; 1077 static const int DEFAULT_CAPACITY = 128*1024;
1072 if (phone->usbReceive.buffer == NULL) { 1078 if (phone->usbReceive.buffer == NULL) {
1073 phone->usbReceive.capacity = DEFAULT_CAPACITY; 1079 phone->usbReceive.capacity = DEFAULT_CAPACITY;
@@ -1080,6 +1086,7 @@ void iphone_mux_pullbulk(iphone_device_t phone)
1080 // pull in content, note that the amount we can pull is capacity minus leftover 1086 // pull in content, note that the amount we can pull is capacity minus leftover
1081 int readlen = recv_from_phone_timeout(phone, cursor, phone->usbReceive.capacity - phone->usbReceive.leftover, 5000); 1087 int readlen = recv_from_phone_timeout(phone, cursor, phone->usbReceive.capacity - phone->usbReceive.leftover, 5000);
1082 if (readlen < 0) { 1088 if (readlen < 0) {
1089 res = readlen;
1083 //fprintf(stderr, "recv_from_phone_timeout gave us an error.\n"); 1090 //fprintf(stderr, "recv_from_phone_timeout gave us an error.\n");
1084 readlen = 0; 1091 readlen = 0;
1085 } 1092 }
@@ -1099,24 +1106,24 @@ void iphone_mux_pullbulk(iphone_device_t phone)
1099 if (phone->usbReceive.leftover < HEADERLEN) break; 1106 if (phone->usbReceive.leftover < HEADERLEN) break;
1100 usbmux_tcp_header *header = (usbmux_tcp_header *) cursor; 1107 usbmux_tcp_header *header = (usbmux_tcp_header *) cursor;
1101 1108
1102 printf("%s: recv_from_phone_timeout (%d --> %d)\n", __func__, ntohs(header->sport), ntohs(header->dport)); 1109 log_debug_msg("%s: recv_from_phone_timeout (%d --> %d)\n", __func__, ntohs(header->sport), ntohs(header->dport));
1103 1110
1104 // now that we have a header, check if there is sufficient data 1111 // now that we have a header, check if there is sufficient data
1105 // to construct a full packet, including its data 1112 // to construct a full packet, including its data
1106 uint32 packetlen = ntohl(header->length); 1113 uint32 packetlen = ntohl(header->length);
1107 if (phone->usbReceive.leftover < packetlen) { 1114 if (phone->usbReceive.leftover < packetlen) {
1108 printf("%s: not enough data to construct a full packet\n", __func__); 1115 fprintf(stderr, "%s: not enough data to construct a full packet\n", __func__);
1109 break; 1116 break;
1110 } 1117 }
1111 1118
1112 // ok... find the client this packet will get stuffed to. 1119 // ok... find the client this packet will get stuffed to.
1113 iphone_umux_client_t client = find_client(header); 1120 iphone_umux_client_t client = find_client(header);
1114 if (client == NULL) { 1121 if (client == NULL) {
1115 fprintf(stderr, "WARNING: client for packet cannot be found. dropping packet.\n"); 1122 log_debug_msg("WARNING: client for packet cannot be found. dropping packet.\n");
1116 } 1123 }
1117 else { 1124 else {
1118 // stuff the data 1125 // stuff the data
1119 fprintf(stderr, "%s: found client, calling append_receive_buffer\n", __func__); 1126 log_debug_msg("%s: found client, calling append_receive_buffer\n", __func__);
1120 append_receive_buffer(client, cursor); 1127 append_receive_buffer(client, cursor);
1121 } 1128 }
1122 1129
@@ -1141,6 +1148,8 @@ void iphone_mux_pullbulk(iphone_device_t phone)
1141 phone->usbReceive.buffer = newbuff; 1148 phone->usbReceive.buffer = newbuff;
1142 phone->usbReceive.capacity = DEFAULT_CAPACITY; 1149 phone->usbReceive.capacity = DEFAULT_CAPACITY;
1143 } 1150 }
1151
1152 return res;
1144} 1153}
1145 1154
1146/** 1155/**
diff --git a/iphone.h b/iphone.h
index e132cd5..fcef32a 100644
--- a/iphone.h
+++ b/iphone.h
@@ -50,6 +50,7 @@
50#define IPHONE_E_ETIMEDOUT -ETIMEDOUT 50#define IPHONE_E_ETIMEDOUT -ETIMEDOUT
51#define IPHONE_E_ECONNREFUSED -ECONNREFUSED 51#define IPHONE_E_ECONNREFUSED -ECONNREFUSED
52 52
53void iphone_set_debug(int e);
53 54
54typedef int16_t iphone_error_t; 55typedef int16_t iphone_error_t;
55 56
@@ -72,7 +73,7 @@ iphone_error_t iphone_mux_send(iphone_umux_client_t client, const char *data, ui
72iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t * recv_bytes); 73iphone_error_t iphone_mux_recv(iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t * recv_bytes);
73iphone_error_t iphone_mux_recv_timeout(iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t * recv_bytes, int timeout); 74iphone_error_t iphone_mux_recv_timeout(iphone_umux_client_t client, char *data, uint32_t datalen, uint32_t * recv_bytes, int timeout);
74 75
75void iphone_mux_pullbulk(iphone_device_t phone); 76int iphone_mux_pullbulk(iphone_device_t phone);
76 77
77iphone_error_t iphone_mux_get_error(iphone_umux_client_t client); 78iphone_error_t iphone_mux_get_error(iphone_umux_client_t client);
78 79
diff --git a/main.c b/main.c
index c0bb340..6b1b6ce 100644
--- a/main.c
+++ b/main.c
@@ -55,7 +55,7 @@ static int verbose = DEBUG_LEVEL;
55static int foreground = 0; 55static int foreground = 0;
56static int exit_on_no_devices = 0; 56static int exit_on_no_devices = 0;
57 57
58struct device_use_info { 58struct device_info {
59 uint32_t device_id; 59 uint32_t device_id;
60 iphone_device_t phone; 60 iphone_device_t phone;
61 int use_count; 61 int use_count;
@@ -78,11 +78,11 @@ struct client_data {
78 int handler_dead; 78 int handler_dead;
79 int connected; 79 int connected;
80 iphone_umux_client_t muxclient; 80 iphone_umux_client_t muxclient;
81 struct device_use_info *duinfo; 81 struct device_info *dev;
82}; 82};
83 83
84static struct device_use_info **device_use_list = NULL; 84static struct device_info **devices = NULL;
85static int device_use_count = 0; 85static int device_count = 0;
86static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; 86static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER;
87static pthread_mutex_t usb_mutex = PTHREAD_MUTEX_INITIALIZER; 87static pthread_mutex_t usb_mutex = PTHREAD_MUTEX_INITIALIZER;
88 88
@@ -223,7 +223,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
223 223
224 cdata->reader_dead = 0; 224 cdata->reader_dead = 0;
225 225
226 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 226 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->dev->device_id, cdata->dev->use_count);
227 227
228 while (!quit_flag && !cdata->reader_quit) { 228 while (!quit_flag && !cdata->reader_quit) {
229 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT); 229 result = check_fd(cdata->socket, FD_WRITE, DEFAULT_TIMEOUT);
@@ -237,7 +237,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
237 rlen = 0; 237 rlen = 0;
238 err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT); 238 err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT);
239 if (err != 0) { 239 if (err != 0) {
240 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: encountered USB read error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err); 240 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: encountered USB read error: %d\n", __func__, cdata->dev->device_id, cdata->dev->use_count, err);
241 break; 241 break;
242 } 242 }
243 243
@@ -261,7 +261,7 @@ static void *usbmuxd_client_reader_thread(void *arg)
261 fsync(cdata->socket); 261 fsync(cdata->socket);
262 } 262 }
263 263
264 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 264 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->dev->device_id, cdata->dev->use_count);
265 265
266 cdata->reader_dead = 1; 266 cdata->reader_dead = 1;
267 267
@@ -358,7 +358,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
358 358
359 cdata = (struct client_data*)arg; 359 cdata = (struct client_data*)arg;
360 360
361 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id,cdata->duinfo->use_count); 361 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: started\n", __func__, cdata->dev->device_id,cdata->dev->use_count);
362 362
363 if (usbmuxd_handleConnectResult(cdata)) { 363 if (usbmuxd_handleConnectResult(cdata)) {
364 if (verbose >= 3) fprintf(stderr, "handleConnectResult: Error\n"); 364 if (verbose >= 3) fprintf(stderr, "handleConnectResult: Error\n");
@@ -391,20 +391,20 @@ static void *usbmuxd_client_handler_thread(void *arg)
391 break; 391 break;
392 } 392 }
393 if (len < 0) { 393 if (len < 0) {
394 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: Error: recv: %s\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, strerror(errno)); 394 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: Error: recv: %s\n", __func__, cdata->dev->device_id, cdata->dev->use_count, strerror(errno));
395 break; 395 break;
396 } 396 }
397 397
398 cursor = buffer; 398 cursor = buffer;
399 399
400 pthread_mutex_lock(&cdata->duinfo->writer_mutex); 400 pthread_mutex_lock(&cdata->dev->writer_mutex);
401 do { 401 do {
402 wlen = 0; 402 wlen = 0;
403 err = iphone_mux_send(cdata->muxclient, cursor, len, &wlen); 403 err = iphone_mux_send(cdata->muxclient, cursor, len, &wlen);
404 if (err == IPHONE_E_TIMEOUT) { 404 if (err == IPHONE_E_TIMEOUT) {
405 // some kind of timeout... just be patient and retry. 405 // some kind of timeout... just be patient and retry.
406 } else if (err != IPHONE_E_SUCCESS) { 406 } else if (err != IPHONE_E_SUCCESS) {
407 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: USB write error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err); 407 if (verbose >= 2) fprintf(stderr, "%s[%d:%d]: USB write error: %d\n", __func__, cdata->dev->device_id, cdata->dev->use_count, err);
408 len = -1; 408 len = -1;
409 break; 409 break;
410 } 410 }
@@ -414,7 +414,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
414 // advance cursor appropiately. 414 // advance cursor appropiately.
415 cursor += wlen; 415 cursor += wlen;
416 } while ((len > 0) && !quit_flag); 416 } while ((len > 0) && !quit_flag);
417 pthread_mutex_unlock(&cdata->duinfo->writer_mutex); 417 pthread_mutex_unlock(&cdata->dev->writer_mutex);
418 if (len < 0) { 418 if (len < 0) {
419 break; 419 break;
420 } 420 }
@@ -422,7 +422,7 @@ static void *usbmuxd_client_handler_thread(void *arg)
422 422
423leave: 423leave:
424 // cleanup 424 // cleanup
425 if (verbose >= 3) fprintf(stderr, "%s[%d:%d]: terminating\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 425 if (verbose >= 3) fprintf(stderr, "%s[%d:%d]: terminating\n", __func__, cdata->dev->device_id, cdata->dev->use_count);
426 if (cdata->reader != 0) { 426 if (cdata->reader != 0) {
427 cdata->reader_quit = 1; 427 cdata->reader_quit = 1;
428 pthread_join(cdata->reader, NULL); 428 pthread_join(cdata->reader, NULL);
@@ -430,7 +430,7 @@ leave:
430 430
431 cdata->handler_dead = 1; 431 cdata->handler_dead = 1;
432 432
433 if (verbose >= 3) fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); 433 if (verbose >= 3) fprintf(stderr, "%s[%d:%d]: terminated\n", __func__, cdata->dev->device_id, cdata->dev->use_count);
434 return NULL; 434 return NULL;
435} 435}
436 436
@@ -440,14 +440,15 @@ leave:
440 */ 440 */
441static void *usbmuxd_bulk_reader_thread(void *arg) 441static void *usbmuxd_bulk_reader_thread(void *arg)
442{ 442{
443 struct device_use_info *cur_dev; 443 struct device_info *cur_dev;
444 int err;
444 445
445 if (!arg) { 446 if (!arg) {
446 if (verbose >= 3) fprintf(stderr, "%s: Invalid client_data provided\n", __func__); 447 if (verbose >= 3) fprintf(stderr, "%s: Invalid client_data provided\n", __func__);
447 return NULL; 448 return NULL;
448 } 449 }
449 450
450 cur_dev = (struct device_use_info*)arg; 451 cur_dev = (struct device_info*)arg;
451 452
452 if (verbose >= 5) fprintf(stderr, "%s: started\n", __func__); 453 if (verbose >= 5) fprintf(stderr, "%s: started\n", __func__);
453 454
@@ -460,11 +461,10 @@ static void *usbmuxd_bulk_reader_thread(void *arg)
460 } 461 }
461 pthread_mutex_unlock(&cur_dev->mutex); 462 pthread_mutex_unlock(&cur_dev->mutex);
462 463
463 iphone_mux_pullbulk(cur_dev->phone); 464 if ((err = iphone_mux_pullbulk(cur_dev->phone)) < 0) {
464 //err = iphone_mux_get_error(cdata->muxclient); 465 if (verbose >= 1) fprintf(stderr, "%s: error %d when reading from device\n", __func__, err);
465 //if (err != IPHONE_E_SUCCESS) { 466 break;
466 // break; 467 }
467 //}
468 } 468 }
469 469
470 if (verbose >= 0) fprintf(stderr, "%s: terminated\n", __func__); 470 if (verbose >= 0) fprintf(stderr, "%s: terminated\n", __func__);
@@ -495,7 +495,7 @@ static void *usbmuxd_client_init_thread(void *arg)
495// iphone_error_t err; 495// iphone_error_t err;
496 496
497 iphone_device_t phone = NULL; 497 iphone_device_t phone = NULL;
498 struct device_use_info *cur_dev = NULL; 498 struct device_info *cur_dev = NULL;
499 499
500 if (!arg) { 500 if (!arg) {
501 if (verbose >= 1) fprintf(stderr, "%s: invalid client_data provided!\n", __func__); 501 if (verbose >= 1) fprintf(stderr, "%s: invalid client_data provided!\n", __func__);
@@ -608,13 +608,13 @@ connect:
608 phone = NULL; 608 phone = NULL;
609 cur_dev = NULL; 609 cur_dev = NULL;
610 // first check if we already have an open connection 610 // first check if we already have an open connection
611 if (device_use_list) { 611 if (devices) {
612 pthread_mutex_lock(&usbmux_mutex); 612 pthread_mutex_lock(&usbmux_mutex);
613 for (i = 0; i < device_use_count; i++) { 613 for (i = 0; i < device_count; i++) {
614 if (device_use_list[i]) { 614 if (devices[i]) {
615 if (device_use_list[i]->device_id == c_req->device_id) { 615 if (devices[i]->device_id == c_req->device_id) {
616 device_use_list[i]->use_count++; 616 devices[i]->use_count++;
617 cur_dev = device_use_list[i]; 617 cur_dev = devices[i];
618 phone = cur_dev->phone; 618 phone = cur_dev->phone;
619 break; 619 break;
620 } 620 }
@@ -635,10 +635,11 @@ connect:
635 } 635 }
636 pthread_mutex_unlock(&usb_mutex); 636 pthread_mutex_unlock(&usb_mutex);
637 637
638 // add to device list 638 // create device object
639 pthread_mutex_lock(&usbmux_mutex);
639 if (verbose >= 3) fprintf(stderr, "%s: add to device list\n", __func__); 640 if (verbose >= 3) fprintf(stderr, "%s: add to device list\n", __func__);
640 cur_dev = (struct device_use_info*)malloc(sizeof(struct device_use_info)); 641 cur_dev = (struct device_info*)malloc(sizeof(struct device_info));
641 memset(cur_dev, 0, sizeof(struct device_use_info)); 642 memset(cur_dev, 0, sizeof(struct device_info));
642 cur_dev->use_count = 1; 643 cur_dev->use_count = 1;
643 cur_dev->device_id = c_req->device_id; 644 cur_dev->device_id = c_req->device_id;
644 cur_dev->phone = phone; 645 cur_dev->phone = phone;
@@ -646,13 +647,13 @@ connect:
646 pthread_mutex_init(&cur_dev->mutex, NULL); 647 pthread_mutex_init(&cur_dev->mutex, NULL);
647 pthread_mutex_init(&cur_dev->writer_mutex, NULL); 648 pthread_mutex_init(&cur_dev->writer_mutex, NULL);
648 649
649 if (verbose >= 3) fprintf(stderr, "%s: device_use_count = %d\n", __func__, device_use_count); 650 if (verbose >= 3) fprintf(stderr, "%s: device_count = %d\n", __func__, device_count);
650 651
651 pthread_mutex_lock(&usbmux_mutex); 652 // add to list of devices
652 device_use_list = (struct device_use_info**)realloc(device_use_list, sizeof(struct device_use_info*) * (device_use_count+1)); 653 devices = (struct device_info**)realloc(devices, sizeof(struct device_info*) * (device_count+1));
653 if (device_use_list) { 654 if (devices) {
654 device_use_list[device_use_count] = cur_dev; 655 devices[device_count] = cur_dev;
655 device_use_count++; 656 devices++;
656 } 657 }
657 pthread_mutex_unlock(&usbmux_mutex); 658 pthread_mutex_unlock(&usbmux_mutex);
658 } else { 659 } else {
@@ -671,43 +672,22 @@ connect:
671 } 672 }
672 673
673 // start bulk reader thread (once per device) 674 // start bulk reader thread (once per device)
675 pthread_mutex_lock(&cur_dev->mutex);
674 if (cur_dev->bulk_reader == 0) { 676 if (cur_dev->bulk_reader == 0) {
675 pthread_create(&cur_dev->bulk_reader, NULL, usbmuxd_bulk_reader_thread, cur_dev); 677 pthread_create(&cur_dev->bulk_reader, NULL, usbmuxd_bulk_reader_thread, cur_dev);
676 } 678 }
679 pthread_mutex_unlock(&cur_dev->mutex);
677 680
678 // start connection handler thread 681 // start connection handler thread
679 cdata->handler_dead = 0; 682 cdata->handler_dead = 0;
680 cdata->tag = c_req->header.tag; 683 cdata->tag = c_req->header.tag;
681 cdata->duinfo = cur_dev; 684 cdata->dev = cur_dev;
682 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) { 685 if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) {
683 if (verbose >= 3) fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__); 686 if (verbose >= 3) fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__);
684 cdata->handler = 0; 687 cdata->handler = 0;
685 goto leave; 688 goto leave;
686 } 689 }
687 690
688 //sent_result = 0;
689
690 // TODO: wait for connect result?
691 // if connect failed, don't run this loop:
692
693 /*
694 // start reading data from the connected device
695 while (!quit_flag && !cdata->handler_dead) {
696 iphone_mux_pullbulk(cur_dev->phone);
697 err = iphone_mux_get_error(cdata->muxclient);
698 if (err != IPHONE_E_SUCCESS) {
699 break;
700 }
701 }
702
703 if (!sent_result) {
704 //fprintf(stderr, "Sending error message %d tag %d\n", err, c_req.header.tag);
705 err = iphone_mux_get_error(cdata->muxclient);
706 //usbmuxd_send_result(cdata->socket, c_req.header.tag, err);
707 }*/
708
709 //fprintf(stderr, "%s: terminating\n", __func__);
710
711 // wait for handler thread to finish its work 691 // wait for handler thread to finish its work
712 if (cdata->handler != 0) { 692 if (cdata->handler != 0) {
713 pthread_join(cdata->handler, NULL); 693 pthread_join(cdata->handler, NULL);
@@ -754,23 +734,23 @@ leave:
754 free(cur_dev); 734 free(cur_dev);
755 cur_dev = NULL; 735 cur_dev = NULL;
756 pthread_mutex_lock(&usbmux_mutex); 736 pthread_mutex_lock(&usbmux_mutex);
757 if (device_use_count > 1) { 737 if (device_count > 1) {
758 struct device_use_info **newlist; 738 struct device_info **newlist;
759 int j; 739 int j;
760 740
761 newlist = (struct device_use_info**)malloc(sizeof(struct device_use_info*) * device_use_count-1); 741 newlist = (struct device_info**)malloc(sizeof(struct device_info*) * device_count-1);
762 for (i = 0; i < device_use_count; i++) { 742 for (i = 0; i < device_count; i++) {
763 if (device_use_list[i] != NULL) { 743 if (devices[i] != NULL) {
764 newlist[j++] = device_use_list[i]; 744 newlist[j++] = devices[i];
765 } 745 }
766 } 746 }
767 free(device_use_list); 747 free(devices);
768 device_use_list = newlist; 748 devices = newlist;
769 device_use_count--; 749 device_count--;
770 } else { 750 } else {
771 free(device_use_list); 751 free(devices);
772 device_use_list = NULL; 752 devices = NULL;
773 device_use_count = 0; 753 device_count = 0;
774 } 754 }
775 pthread_mutex_unlock(&usbmux_mutex); 755 pthread_mutex_unlock(&usbmux_mutex);
776 } 756 }
@@ -1013,6 +993,8 @@ int main(int argc, char **argv)
1013 993
1014 chmod(USBMUXD_SOCKET_FILE, 0666); 994 chmod(USBMUXD_SOCKET_FILE, 0666);
1015 995
996 if (verbose >= 3) iphone_set_debug(1);
997
1016 if (!foreground) { 998 if (!foreground) {
1017 if (daemonize() < 0) { 999 if (daemonize() < 0) {
1018 fprintf(stderr, "usbmuxd: FATAL: Could not daemonize!\n"); 1000 fprintf(stderr, "usbmuxd: FATAL: Could not daemonize!\n");