summaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c128
1 files changed, 55 insertions, 73 deletions
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");