diff options
| author | 2012-03-31 19:54:55 +0200 | |
|---|---|---|
| committer | 2012-03-31 19:54:55 +0200 | |
| commit | b63c3355bd194d1df83b2f5aa0affab411397dd4 (patch) | |
| tree | 7ac0e2cc9eeffe2b8f537b1f59c38e65df743013 /libusbmuxd/libusbmuxd.c | |
| parent | 30e6b293c60564c2016ef42830fadc58492998fa (diff) | |
| download | usbmuxd-b63c3355bd194d1df83b2f5aa0affab411397dd4.tar.gz usbmuxd-b63c3355bd194d1df83b2f5aa0affab411397dd4.tar.bz2 | |
libusbmuxd: handle remove messages in usbmuxd_get_device_list
Diffstat (limited to 'libusbmuxd/libusbmuxd.c')
| -rw-r--r-- | libusbmuxd/libusbmuxd.c | 75 |
1 files changed, 49 insertions, 26 deletions
diff --git a/libusbmuxd/libusbmuxd.c b/libusbmuxd/libusbmuxd.c index 861eefc..172da80 100644 --- a/libusbmuxd/libusbmuxd.c +++ b/libusbmuxd/libusbmuxd.c | |||
| @@ -657,12 +657,15 @@ int usbmuxd_get_device_list(usbmuxd_device_info_t **device_list) | |||
| 657 | int sfd; | 657 | int sfd; |
| 658 | int listen_success = 0; | 658 | int listen_success = 0; |
| 659 | uint32_t res; | 659 | uint32_t res; |
| 660 | struct collection tmpdevs; | ||
| 660 | usbmuxd_device_info_t *newlist = NULL; | 661 | usbmuxd_device_info_t *newlist = NULL; |
| 661 | struct usbmuxd_header hdr; | 662 | struct usbmuxd_header hdr; |
| 662 | struct usbmuxd_device_record *dev_info; | 663 | struct usbmuxd_device_record *dev; |
| 663 | int dev_cnt = 0; | 664 | int dev_cnt = 0; |
| 664 | void *payload = NULL; | 665 | void *payload = NULL; |
| 665 | 666 | ||
| 667 | *device_list = NULL; | ||
| 668 | |||
| 666 | #ifdef HAVE_PLIST | 669 | #ifdef HAVE_PLIST |
| 667 | retry: | 670 | retry: |
| 668 | #endif | 671 | #endif |
| @@ -698,32 +701,42 @@ retry: | |||
| 698 | return -1; | 701 | return -1; |
| 699 | } | 702 | } |
| 700 | 703 | ||
| 701 | *device_list = NULL; | 704 | collection_init(&tmpdevs); |
| 705 | |||
| 702 | // receive device list | 706 | // receive device list |
| 703 | while (1) { | 707 | while (1) { |
| 704 | if (receive_packet(sfd, &hdr, &payload, 1000) > 0) { | 708 | if (receive_packet(sfd, &hdr, &payload, 1000) > 0) { |
| 705 | if (hdr.message == MESSAGE_DEVICE_ADD) { | 709 | if (hdr.message == MESSAGE_DEVICE_ADD) { |
| 706 | dev_info = payload; | 710 | dev = payload; |
| 707 | newlist = (usbmuxd_device_info_t *) realloc(*device_list, sizeof(usbmuxd_device_info_t) * (dev_cnt + 1)); | 711 | usbmuxd_device_info_t *devinfo = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t)); |
| 708 | if (newlist) { | 712 | if (!devinfo) { |
| 709 | newlist[dev_cnt].handle = | 713 | fprintf(stderr, "%s: Out of memory!\n", __func__); |
| 710 | (int) dev_info->device_id; | 714 | free(payload); |
| 711 | newlist[dev_cnt].product_id = | 715 | return -1; |
| 712 | dev_info->product_id; | 716 | } |
| 713 | memset(newlist[dev_cnt].uuid, '\0', | 717 | |
| 714 | sizeof(newlist[dev_cnt].uuid)); | 718 | devinfo->handle = dev->device_id; |
| 715 | memcpy(newlist[dev_cnt].uuid, | 719 | devinfo->product_id = dev->product_id; |
| 716 | dev_info->serial_number, | 720 | memset(devinfo->uuid, '\0', sizeof(devinfo->uuid)); |
| 717 | sizeof(newlist[dev_cnt].uuid)); | 721 | memcpy(devinfo->uuid, dev->serial_number, sizeof(devinfo->uuid)); |
| 718 | *device_list = newlist; | 722 | |
| 719 | dev_cnt++; | 723 | collection_add(&tmpdevs, devinfo); |
| 720 | } else { | 724 | |
| 721 | fprintf(stderr, | 725 | } else if (hdr.message == MESSAGE_DEVICE_REMOVE) { |
| 722 | "%s: ERROR: out of memory when trying to realloc!\n", | 726 | uint32_t handle; |
| 723 | __func__); | 727 | usbmuxd_device_info_t *devinfo = NULL; |
| 724 | if (payload) | 728 | |
| 725 | free(payload); | 729 | memcpy(&handle, payload, sizeof(uint32_t)); |
| 726 | break; | 730 | |
| 731 | FOREACH(usbmuxd_device_info_t *di, &tmpdevs) { | ||
| 732 | if (di && di->handle == handle) { | ||
| 733 | devinfo = di; | ||
| 734 | break; | ||
| 735 | } | ||
| 736 | } ENDFOREACH | ||
| 737 | if (devinfo) { | ||
| 738 | collection_remove(&tmpdevs, devinfo); | ||
| 739 | free(devinfo); | ||
| 727 | } | 740 | } |
| 728 | } else { | 741 | } else { |
| 729 | fprintf(stderr, "%s: Unexpected message %d\n", __func__, hdr.message); | 742 | fprintf(stderr, "%s: Unexpected message %d\n", __func__, hdr.message); |
| @@ -740,9 +753,19 @@ retry: | |||
| 740 | // explicitly close connection | 753 | // explicitly close connection |
| 741 | close_socket(sfd); | 754 | close_socket(sfd); |
| 742 | 755 | ||
| 743 | // terminating zero record | 756 | // create copy of device info entries from collection |
| 744 | newlist = (usbmuxd_device_info_t*) realloc(*device_list, sizeof(usbmuxd_device_info_t) * (dev_cnt + 1)); | 757 | newlist = (usbmuxd_device_info_t*)malloc(sizeof(usbmuxd_device_info_t) * (collection_count(&tmpdevs) + 1)); |
| 745 | memset(newlist + dev_cnt, 0, sizeof(usbmuxd_device_info_t)); | 758 | dev_cnt = 0; |
| 759 | FOREACH(usbmuxd_device_info_t *di, &tmpdevs) { | ||
| 760 | if (di) { | ||
| 761 | memcpy(&newlist[dev_cnt], di, sizeof(usbmuxd_device_info_t)); | ||
| 762 | free(di); | ||
| 763 | dev_cnt++; | ||
| 764 | } | ||
| 765 | } ENDFOREACH | ||
| 766 | collection_free(&tmpdevs); | ||
| 767 | |||
| 768 | memset(&newlist[dev_cnt], 0, sizeof(usbmuxd_device_info_t)); | ||
| 746 | *device_list = newlist; | 769 | *device_list = newlist; |
| 747 | 770 | ||
| 748 | return dev_cnt; | 771 | return dev_cnt; |
