diff options
| author | 2009-02-25 18:09:19 +0100 | |
|---|---|---|
| committer | 2009-02-25 18:09:19 +0100 | |
| commit | 6e2946da89ce8c44ac2b78e49c8fc934974d021d (patch) | |
| tree | 33c63b8b7a008695df606190195b9baf4a59f350 /usbmuxd.c | |
| parent | 97e6c8b7bb89c7f08ad46050598c15a55ea3f436 (diff) | |
| download | usbmuxd-6e2946da89ce8c44ac2b78e49c8fc934974d021d.tar.gz usbmuxd-6e2946da89ce8c44ac2b78e49c8fc934974d021d.tar.bz2 | |
big endian fix and some improvements, now multi-client capable
Diffstat (limited to 'usbmuxd.c')
| -rw-r--r-- | usbmuxd.c | 61 |
1 files changed, 42 insertions, 19 deletions
| @@ -47,6 +47,12 @@ | |||
| 47 | static int quit_flag = 0; | 47 | static int quit_flag = 0; |
| 48 | static int fsock = -1; | 48 | static int fsock = -1; |
| 49 | 49 | ||
| 50 | struct device_use_info { | ||
| 51 | uint32_t device_id; | ||
| 52 | iphone_device_t phone; | ||
| 53 | int use_count; | ||
| 54 | }; | ||
| 55 | |||
| 50 | struct client_data { | 56 | struct client_data { |
| 51 | volatile int dead; | 57 | volatile int dead; |
| 52 | int socket; | 58 | int socket; |
| @@ -58,12 +64,7 @@ struct client_data { | |||
| 58 | int reader_dead; | 64 | int reader_dead; |
| 59 | int handler_dead; | 65 | int handler_dead; |
| 60 | iphone_umux_client_t muxclient; | 66 | iphone_umux_client_t muxclient; |
| 61 | }; | 67 | struct device_use_info *duinfo; |
| 62 | |||
| 63 | struct device_use_info { | ||
| 64 | uint32_t device_id; | ||
| 65 | iphone_device_t phone; | ||
| 66 | int use_count; | ||
| 67 | }; | 68 | }; |
| 68 | 69 | ||
| 69 | static struct device_use_info **device_use_list = NULL; | 70 | static struct device_use_info **device_use_list = NULL; |
| @@ -71,6 +72,18 @@ static int device_use_count = 0; | |||
| 71 | static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; | 72 | static pthread_mutex_t usbmux_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 72 | 73 | ||
| 73 | /** | 74 | /** |
| 75 | * mutex for mutual exclusion of calling the iphone_mux_send function | ||
| 76 | * TODO: I don't know if we really need this? | ||
| 77 | */ | ||
| 78 | static pthread_mutex_t writer_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 79 | |||
| 80 | /** | ||
| 81 | * mutex to keep the reader threads from reading partial packages | ||
| 82 | */ | ||
| 83 | static pthread_mutex_t reader_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
| 84 | |||
| 85 | #ifdef DEBUG | ||
| 86 | /** | ||
| 74 | * for debugging purposes. | 87 | * for debugging purposes. |
| 75 | */ | 88 | */ |
| 76 | static void print_buffer(const char *data, const int length) | 89 | static void print_buffer(const char *data, const int length) |
| @@ -103,6 +116,7 @@ static void print_buffer(const char *data, const int length) | |||
| 103 | } | 116 | } |
| 104 | printf("\n"); | 117 | printf("\n"); |
| 105 | } | 118 | } |
| 119 | #endif | ||
| 106 | 120 | ||
| 107 | /** | 121 | /** |
| 108 | * Read incoming usbmuxd packet. If the packet is larger than | 122 | * Read incoming usbmuxd packet. If the packet is larger than |
| @@ -192,7 +206,7 @@ static void *usbmuxd_client_reader_thread(void *arg) | |||
| 192 | 206 | ||
| 193 | cdata->reader_dead = 0; | 207 | cdata->reader_dead = 0; |
| 194 | 208 | ||
| 195 | fprintf(stdout, "%s: started\n", __func__); | 209 | fprintf(stdout, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); |
| 196 | 210 | ||
| 197 | while (!quit_flag && !cdata->reader_quit) { | 211 | while (!quit_flag && !cdata->reader_quit) { |
| 198 | result = check_fd(cdata->socket, fdwrite, DEFAULT_TIMEOUT); | 212 | result = check_fd(cdata->socket, fdwrite, DEFAULT_TIMEOUT); |
| @@ -204,9 +218,11 @@ static void *usbmuxd_client_reader_thread(void *arg) | |||
| 204 | } | 218 | } |
| 205 | 219 | ||
| 206 | rlen = 0; | 220 | rlen = 0; |
| 221 | //pthread_mutex_lock(&usbmux_mutex); | ||
| 207 | err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT); | 222 | err = iphone_mux_recv_timeout(cdata->muxclient, rbuffer, rbuffersize, &rlen, DEFAULT_TIMEOUT); |
| 223 | //pthread_mutex_unlock(&usbmux_mutex); | ||
| 208 | if (err != 0) { | 224 | if (err != 0) { |
| 209 | fprintf(stderr, "%s: encountered USB read error: %d\n", __func__, err); | 225 | fprintf(stderr, "%s[%d:%d]: encountered USB read error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err); |
| 210 | break; | 226 | break; |
| 211 | } | 227 | } |
| 212 | 228 | ||
| @@ -221,7 +237,7 @@ static void *usbmuxd_client_reader_thread(void *arg) | |||
| 221 | fsync(cdata->socket); | 237 | fsync(cdata->socket); |
| 222 | } | 238 | } |
| 223 | 239 | ||
| 224 | fprintf(stdout, "%s: terminated\n", __func__); | 240 | fprintf(stdout, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); |
| 225 | 241 | ||
| 226 | cdata->reader_dead = 1; | 242 | cdata->reader_dead = 1; |
| 227 | 243 | ||
| @@ -268,7 +284,6 @@ static int usbmuxd_handleConnectResult(struct client_data *cdata) | |||
| 268 | return err; | 284 | return err; |
| 269 | } else { | 285 | } else { |
| 270 | if (rlen > 0) { | 286 | if (rlen > 0) { |
| 271 | //print_buffer(buffer, rlen); | ||
| 272 | if ((buffer[0] == 1) && (rlen > 20) && !memcmp(buffer+1, "handleConnectResult:", 20)) { | 287 | if ((buffer[0] == 1) && (rlen > 20) && !memcmp(buffer+1, "handleConnectResult:", 20)) { |
| 273 | // hm... we got an error message! | 288 | // hm... we got an error message! |
| 274 | buffer[rlen] = 0; | 289 | buffer[rlen] = 0; |
| @@ -319,7 +334,7 @@ static void *usbmuxd_client_handler_thread(void *arg) | |||
| 319 | 334 | ||
| 320 | cdata = (struct client_data*)arg; | 335 | cdata = (struct client_data*)arg; |
| 321 | 336 | ||
| 322 | fprintf(stdout, "%s: started\n", __func__); | 337 | fprintf(stdout, "%s[%d:%d]: started\n", __func__, cdata->duinfo->device_id,cdata->duinfo->use_count); |
| 323 | 338 | ||
| 324 | if (usbmuxd_handleConnectResult(cdata)) { | 339 | if (usbmuxd_handleConnectResult(cdata)) { |
| 325 | goto leave; | 340 | goto leave; |
| @@ -349,18 +364,20 @@ static void *usbmuxd_client_handler_thread(void *arg) | |||
| 349 | break; | 364 | break; |
| 350 | } | 365 | } |
| 351 | if (len < 0) { | 366 | if (len < 0) { |
| 352 | fprintf(stderr, "%s: Error: recv: %s\n", __func__, strerror(errno)); | 367 | fprintf(stderr, "%s[%d:%d]: Error: recv: %s\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, strerror(errno)); |
| 353 | break; | 368 | break; |
| 354 | } | 369 | } |
| 355 | 370 | ||
| 356 | cursor = buffer; | 371 | cursor = buffer; |
| 372 | |||
| 373 | pthread_mutex_lock(&writer_mutex); | ||
| 357 | do { | 374 | do { |
| 358 | wlen = 0; | 375 | wlen = 0; |
| 359 | err = iphone_mux_send(cdata->muxclient, cursor, len, &wlen); | 376 | err = iphone_mux_send(cdata->muxclient, cursor, len, &wlen); |
| 360 | if (err == IPHONE_E_TIMEOUT) { | 377 | if (err == IPHONE_E_TIMEOUT) { |
| 361 | // some kind of timeout... just be patient and retry. | 378 | // some kind of timeout... just be patient and retry. |
| 362 | } else if (err != IPHONE_E_SUCCESS) { | 379 | } else if (err != IPHONE_E_SUCCESS) { |
| 363 | fprintf(stderr, "%s: USB write error: %d\n", __func__, err); | 380 | fprintf(stderr, "%s[%d:%d]: USB write error: %d\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count, err); |
| 364 | len = -1; | 381 | len = -1; |
| 365 | break; | 382 | break; |
| 366 | } | 383 | } |
| @@ -370,6 +387,7 @@ static void *usbmuxd_client_handler_thread(void *arg) | |||
| 370 | // advance cursor appropiately. | 387 | // advance cursor appropiately. |
| 371 | cursor += wlen; | 388 | cursor += wlen; |
| 372 | } while ((len > 0) && !quit_flag); | 389 | } while ((len > 0) && !quit_flag); |
| 390 | pthread_mutex_unlock(&writer_mutex); | ||
| 373 | if (len < 0) { | 391 | if (len < 0) { |
| 374 | break; | 392 | break; |
| 375 | } | 393 | } |
| @@ -377,7 +395,7 @@ static void *usbmuxd_client_handler_thread(void *arg) | |||
| 377 | 395 | ||
| 378 | leave: | 396 | leave: |
| 379 | // cleanup | 397 | // cleanup |
| 380 | fprintf(stdout, "%s: terminating\n", __func__); | 398 | fprintf(stdout, "%s[%d:%d]: terminating\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); |
| 381 | if (cdata->reader != 0) { | 399 | if (cdata->reader != 0) { |
| 382 | cdata->reader_quit = 1; | 400 | cdata->reader_quit = 1; |
| 383 | pthread_join(cdata->reader, NULL); | 401 | pthread_join(cdata->reader, NULL); |
| @@ -385,7 +403,7 @@ leave: | |||
| 385 | 403 | ||
| 386 | cdata->handler_dead = 1; | 404 | cdata->handler_dead = 1; |
| 387 | 405 | ||
| 388 | fprintf(stdout, "%s: terminated\n", __func__); | 406 | fprintf(stdout, "%s[%d:%d]: terminated\n", __func__, cdata->duinfo->device_id, cdata->duinfo->use_count); |
| 389 | return NULL; | 407 | return NULL; |
| 390 | } | 408 | } |
| 391 | 409 | ||
| @@ -472,7 +490,9 @@ static void *usbmuxd_client_init_thread(void *arg) | |||
| 472 | //pthread_mutex_unlock(&usbmux_mutex); | 490 | //pthread_mutex_unlock(&usbmux_mutex); |
| 473 | } | 491 | } |
| 474 | 492 | ||
| 493 | #ifdef DEBUG | ||
| 475 | print_buffer((char*)&dev_info_req, sizeof(dev_info_req)); | 494 | print_buffer((char*)&dev_info_req, sizeof(dev_info_req)); |
| 495 | #endif | ||
| 476 | 496 | ||
| 477 | // send it | 497 | // send it |
| 478 | if (send_buf(cdata->socket, &dev_info_req, sizeof(dev_info_req)) <= 0) { | 498 | if (send_buf(cdata->socket, &dev_info_req, sizeof(dev_info_req)) <= 0) { |
| @@ -534,6 +554,8 @@ static void *usbmuxd_client_init_thread(void *arg) | |||
| 534 | cur_dev->device_id = c_req.device_id; | 554 | cur_dev->device_id = c_req.device_id; |
| 535 | cur_dev->phone = phone; | 555 | cur_dev->phone = phone; |
| 536 | 556 | ||
| 557 | fprintf(stdout, "%s: device_use_count = %d\n", __func__, device_use_count); | ||
| 558 | |||
| 537 | pthread_mutex_lock(&usbmux_mutex); | 559 | pthread_mutex_lock(&usbmux_mutex); |
| 538 | device_use_list = (struct device_use_info**)realloc(device_use_list, sizeof(struct device_use_info*) * (device_use_count+1)); | 560 | device_use_list = (struct device_use_info**)realloc(device_use_list, sizeof(struct device_use_info*) * (device_use_count+1)); |
| 539 | if (device_use_list) { | 561 | if (device_use_list) { |
| @@ -559,6 +581,7 @@ static void *usbmuxd_client_init_thread(void *arg) | |||
| 559 | // start connection handler thread | 581 | // start connection handler thread |
| 560 | cdata->handler_dead = 0; | 582 | cdata->handler_dead = 0; |
| 561 | cdata->tag = c_req.header.tag; | 583 | cdata->tag = c_req.header.tag; |
| 584 | cdata->duinfo = cur_dev; | ||
| 562 | if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) { | 585 | if (pthread_create(&cdata->handler, NULL, usbmuxd_client_handler_thread, cdata) != 0) { |
| 563 | fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__); | 586 | fprintf(stderr, "%s: could not create usbmuxd_client_handler_thread!\n", __func__); |
| 564 | cdata->handler = 0; | 587 | cdata->handler = 0; |
| @@ -569,13 +592,12 @@ static void *usbmuxd_client_init_thread(void *arg) | |||
| 569 | 592 | ||
| 570 | // start reading data from the connected device | 593 | // start reading data from the connected device |
| 571 | while (!quit_flag && !cdata->handler_dead) { | 594 | while (!quit_flag && !cdata->handler_dead) { |
| 595 | pthread_mutex_lock(&reader_mutex); | ||
| 572 | iphone_mux_pullbulk(cur_dev->phone); | 596 | iphone_mux_pullbulk(cur_dev->phone); |
| 573 | err = iphone_mux_get_error(cdata->muxclient); | 597 | err = iphone_mux_get_error(cdata->muxclient); |
| 598 | pthread_mutex_unlock(&reader_mutex); | ||
| 574 | if (err != IPHONE_E_SUCCESS) { | 599 | if (err != IPHONE_E_SUCCESS) { |
| 575 | break; | 600 | break; |
| 576 | /*} else if (!sent_result) { | ||
| 577 | usbmuxd_send_result(cdata->socket, c_req.header.tag, 0); | ||
| 578 | sent_result = 1;*/ | ||
| 579 | } | 601 | } |
| 580 | } | 602 | } |
| 581 | 603 | ||
| @@ -630,6 +652,7 @@ leave: | |||
| 630 | } | 652 | } |
| 631 | 653 | ||
| 632 | cdata->dead = 1; | 654 | cdata->dead = 1; |
| 655 | close(cdata->socket); | ||
| 633 | 656 | ||
| 634 | fprintf(stdout, "%s: terminated\n", __func__); | 657 | fprintf(stdout, "%s: terminated\n", __func__); |
| 635 | 658 | ||
| @@ -684,7 +707,7 @@ static void *usbmuxd_accept_thread(void *arg) | |||
| 684 | while (!quit_flag) { | 707 | while (!quit_flag) { |
| 685 | // Check the file descriptor before accepting a connection. | 708 | // Check the file descriptor before accepting a connection. |
| 686 | // If no connection attempt is made, just repeat... | 709 | // If no connection attempt is made, just repeat... |
| 687 | result = check_fd(fsock, fdread, DEFAULT_TIMEOUT); | 710 | result = check_fd(fsock, fdread, 1000); |
| 688 | if (result <= 0) { | 711 | if (result <= 0) { |
| 689 | if (result == 0) { | 712 | if (result == 0) { |
| 690 | // cleanup | 713 | // cleanup |
