diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | iphone.c | 6 | ||||
| -rw-r--r-- | main.c | 30 |
3 files changed, 33 insertions, 5 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | TARGETS=usbmuxd iproxy testclient | 1 | TARGETS=usbmuxd iproxy testclient |
| 2 | CFLAGS=-Wall -g | 2 | CFLAGS=-Wall -g -DDEBUG |
| 3 | LIBS=-lpthread -lusb -lrt | 3 | LIBS=-lpthread -lusb -lrt |
| 4 | LDFLAGS= | 4 | LDFLAGS= |
| 5 | 5 | ||
| @@ -449,9 +449,11 @@ int send_to_phone(iphone_device_t phone, char *data, int datalen) | |||
| 449 | int bytes = 0; | 449 | int bytes = 0; |
| 450 | 450 | ||
| 451 | #ifdef DEBUG | 451 | #ifdef DEBUG |
| 452 | #ifdef DEBUG_MORE | ||
| 452 | printf("===============================\n%s: trying to send\n", __func__); | 453 | printf("===============================\n%s: trying to send\n", __func__); |
| 453 | print_buffer(data, datalen); | 454 | print_buffer(data, datalen); |
| 454 | printf("===============================\n"); | 455 | printf("===============================\n"); |
| 456 | #endif | ||
| 455 | #endif | 457 | #endif |
| 456 | do { | 458 | do { |
| 457 | if (retrycount > 3) { | 459 | if (retrycount > 3) { |
| @@ -980,6 +982,7 @@ uint32 append_receive_buffer(iphone_umux_client_t client, char* packet) | |||
| 980 | 982 | ||
| 981 | // ensure there is enough space, either by first malloc or realloc | 983 | // ensure there is enough space, either by first malloc or realloc |
| 982 | if (datalen > 0) { | 984 | if (datalen > 0) { |
| 985 | fprintf(stderr, "%s: putting %d bytes into client's recv_buffer\n", __func__, datalen); | ||
| 983 | if (client->r_len == 0) dobroadcast = 1; | 986 | if (client->r_len == 0) dobroadcast = 1; |
| 984 | 987 | ||
| 985 | if (client->recv_buffer == NULL) { | 988 | if (client->recv_buffer == NULL) { |
| @@ -1077,6 +1080,7 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1077 | // to construct a full packet, including its data | 1080 | // to construct a full packet, including its data |
| 1078 | uint32 packetlen = ntohl(header->length); | 1081 | uint32 packetlen = ntohl(header->length); |
| 1079 | if (usbReceive.leftover < packetlen) { | 1082 | if (usbReceive.leftover < packetlen) { |
| 1083 | printf("%s: not enough data to construct a full packet\n", __func__); | ||
| 1080 | break; | 1084 | break; |
| 1081 | } | 1085 | } |
| 1082 | 1086 | ||
| @@ -1087,6 +1091,7 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1087 | } | 1091 | } |
| 1088 | else { | 1092 | else { |
| 1089 | // stuff the data | 1093 | // stuff the data |
| 1094 | fprintf(stderr, "%s: found client, calling append_receive_buffer\n", __func__); | ||
| 1090 | append_receive_buffer(client, cursor); | 1095 | append_receive_buffer(client, cursor); |
| 1091 | } | 1096 | } |
| 1092 | 1097 | ||
| @@ -1104,6 +1109,7 @@ void iphone_mux_pullbulk(iphone_device_t phone) | |||
| 1104 | // if there are no leftovers, we just leave the datastructure as is, | 1109 | // if there are no leftovers, we just leave the datastructure as is, |
| 1105 | // and re-use the block next time. | 1110 | // and re-use the block next time. |
| 1106 | if (usbReceive.leftover > 0 && cursor != usbReceive.buffer) { | 1111 | if (usbReceive.leftover > 0 && cursor != usbReceive.buffer) { |
| 1112 | fprintf(stderr, "%s: we got a leftover, so handle it\n", __func__); | ||
| 1107 | char* newbuff = malloc(DEFAULT_CAPACITY); | 1113 | char* newbuff = malloc(DEFAULT_CAPACITY); |
| 1108 | memcpy(newbuff, cursor, usbReceive.leftover); | 1114 | memcpy(newbuff, cursor, usbReceive.leftover); |
| 1109 | free(usbReceive.buffer); | 1115 | free(usbReceive.buffer); |
| @@ -74,6 +74,7 @@ struct client_data { | |||
| 74 | static struct device_use_info **device_use_list = NULL; | 74 | static struct device_use_info **device_use_list = NULL; |
| 75 | static int device_use_count = 0; | 75 | static int device_use_count = 0; |
| 76 | static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; | 76 | static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 77 | static 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) | |||
| 167 | static int usbmuxd_send_result(int fd, uint32_t tag, uint32_t result_code) | 168 | static 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); |
