summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/main.c b/main.c
index ec9b007..8fa2b81 100644
--- a/main.c
+++ b/main.c
@@ -74,6 +74,7 @@ struct client_data {
74static struct device_use_info **device_use_list = NULL; 74static struct device_use_info **device_use_list = NULL;
75static int device_use_count = 0; 75static int device_use_count = 0;
76static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; 76static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER;
77static pthread_mutex_t usb_mutex = PTHREAD_MUTEX_INITIALIZER;
77 78
78#ifdef DEBUG 79#ifdef DEBUG
79/** 80/**
@@ -167,6 +168,7 @@ static int usbmuxd_get_request(int fd, void **data, size_t len)
167static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code) 168static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code)
168{ 169{
169 struct usbmuxd_result res; 170 struct usbmuxd_result res;
171 int ret;
170 172
171 res.header.length = sizeof(res); 173 res.header.length = sizeof(res);
172 res.header.reserved = 0; 174 res.header.reserved = 0;
@@ -176,7 +178,9 @@ static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code)
176 178
177 fprintf(stderr, "%s: tag=%d result=%d\n", __func__, res.header.tag, res.result); 179 fprintf(stderr, "%s: tag=%d result=%d\n", __func__, res.header.tag, res.result);
178 180
179 return send_buf(fd, &res, sizeof(res)); 181 ret = send_buf(fd, &res, sizeof(res));
182 fsync(fd); // let's get it sent
183 return ret;
180} 184}
181 185
182/** 186/**
@@ -278,7 +282,7 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata)
278 } 282 }
279 } else { 283 } else {
280 result = 0; 284 result = 0;
281 err = iphone_mux_recv_timeout(cdata->muxclient, buffer, maxlen, &rlen, DEFAULT_TIMEOUT); 285 err = iphone_mux_recv_timeout(cdata->muxclient, buffer, maxlen, &rlen, 1000);
282 if (err != 0) { 286 if (err != 0) {
283 fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err); 287 fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err);
284 usbmuxd_send_result(cdata->socket, cdata->tag, -err); 288 usbmuxd_send_result(cdata->socket, cdata->tag, -err);
@@ -505,11 +509,17 @@ static void *usbmuxd_client_init_thread(void *arg)
505 goto leave; 509 goto leave;
506 } 510 }
507 511
512 pthread_mutex_lock(&usb_mutex);
513 fprintf(stderr, "%s: usb init\n", __func__);
508 // gather data about all iPhones/iPods attached 514 // gather data about all iPhones/iPods attached
509 usb_init(); 515 usb_init();
516 fprintf(stderr, "%s: usb find busses\n", __func__);
510 usb_find_busses(); 517 usb_find_busses();
518 fprintf(stderr, "%s: usb find devices\n", __func__);
511 usb_find_devices(); 519 usb_find_devices();
512 520
521 fprintf(stderr, "%s: Looking for attached devices...\n", __func__);
522
513 for (bus = usb_get_busses(); bus; bus = bus->next) { 523 for (bus = usb_get_busses(); bus; bus = bus->next) {
514 for (dev = bus->devices; dev; dev = dev->next) { 524 for (dev = bus->devices; dev; dev = dev->next) {
515 if (dev->descriptor.idVendor == 0x05ac 525 if (dev->descriptor.idVendor == 0x05ac
@@ -548,13 +558,16 @@ static void *usbmuxd_client_init_thread(void *arg)
548 } 558 }
549 } 559 }
550 } 560 }
561 pthread_mutex_unlock(&usb_mutex);
551 562
552 // now wait for connect request
553 if (found <= 0) { 563 if (found <= 0) {
554 fprintf(stderr, "%s: No attached iPhone/iPod devices found.\n", __func__); 564 fprintf(stderr, "%s: No attached iPhone/iPod devices found.\n", __func__);
555 goto leave; 565 goto leave;
556 } 566 }
557 567
568 fprintf(stderr, "%s: Waiting for connect request\n", __func__);
569
570 // now wait for connect request
558 //memset(&c_req, 0, sizeof(c_req)); 571 //memset(&c_req, 0, sizeof(c_req));
559 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&c_req, 0)) <= 0) { 572 if ((recv_len = usbmuxd_get_request(cdata->socket, (void**)&c_req, 0)) <= 0) {
560 fprintf(stderr, "%s: Did not receive any connect request.\n", __func__); 573 fprintf(stderr, "%s: Did not receive any connect request.\n", __func__);
@@ -590,12 +603,19 @@ connect:
590 } 603 }
591 if (!phone) { 604 if (!phone) {
592 // if not found, make a new connection 605 // if not found, make a new connection
606 fprintf(stderr, "%s: creating new usb connection, device_id=%d\n", __func__, c_req->device_id);
607
608 pthread_mutex_lock(&usb_mutex);
593 if (iphone_get_specific_device(0, c_req->device_id, &phone) != IPHONE_E_SUCCESS) { 609 if (iphone_get_specific_device(0, c_req->device_id, &phone) != IPHONE_E_SUCCESS) {
610 pthread_mutex_unlock(&usb_mutex);
594 fprintf(stderr, "%s: device_id %d could not be opened\n", __func__, c_req->device_id); 611 fprintf(stderr, "%s: device_id %d could not be opened\n", __func__, c_req->device_id);
595 usbmuxd_send_result(cdata->socket, c_req->header.tag, ENODEV); 612 usbmuxd_send_result(cdata->socket, c_req->header.tag, ENODEV);
596 goto leave; 613 goto leave;
597 } 614 }
615 pthread_mutex_unlock(&usb_mutex);
616
598 // add to device list 617 // add to device list
618 fprintf(stderr, "%s: add to device list\n", __func__);
599 cur_dev = (struct device_use_info*)malloc(sizeof(struct device_use_info)); 619 cur_dev = (struct device_use_info*)malloc(sizeof(struct device_use_info));
600 memset(cur_dev, 0, sizeof(struct device_use_info)); 620 memset(cur_dev, 0, sizeof(struct device_use_info));
601 cur_dev->use_count = 1; 621 cur_dev->use_count = 1;
@@ -615,7 +635,7 @@ connect:
615 } 635 }
616 pthread_mutex_unlock(&usbmux_mutex); 636 pthread_mutex_unlock(&usbmux_mutex);
617 } else { 637 } else {
618 fprintf(stderr, "%s: reusing usb connection device_id %d\n", __func__, c_req->device_id); 638 fprintf(stderr, "%s: reusing usb connection, device_id=%d\n", __func__, c_req->device_id);
619 } 639 }
620 640
621 // setup connection to iPhone/iPod 641 // setup connection to iPhone/iPod
@@ -697,7 +717,9 @@ leave:
697 cur_dev->use_count = 0; 717 cur_dev->use_count = 0;
698 pthread_mutex_unlock(&cur_dev->mutex); 718 pthread_mutex_unlock(&cur_dev->mutex);
699 pthread_join(cur_dev->bulk_reader, NULL); 719 pthread_join(cur_dev->bulk_reader, NULL);
720 pthread_mutex_lock(&usb_mutex);
700 iphone_free_device(cur_dev->phone); 721 iphone_free_device(cur_dev->phone);
722 pthread_mutex_unlock(&usb_mutex);
701 pthread_mutex_destroy(&cur_dev->writer_mutex); 723 pthread_mutex_destroy(&cur_dev->writer_mutex);
702 pthread_mutex_destroy(&cur_dev->mutex); 724 pthread_mutex_destroy(&cur_dev->mutex);
703 free(cur_dev); 725 free(cur_dev);