diff options
Diffstat (limited to 'src/device.c')
| -rw-r--r-- | src/device.c | 69 |
1 files changed, 36 insertions, 33 deletions
diff --git a/src/device.c b/src/device.c index aac40b1..0928021 100644 --- a/src/device.c +++ b/src/device.c | |||
| @@ -32,8 +32,11 @@ | |||
| 32 | #include <string.h> | 32 | #include <string.h> |
| 33 | #include <stdint.h> | 33 | #include <stdint.h> |
| 34 | #include <inttypes.h> | 34 | #include <inttypes.h> |
| 35 | #include <pthread.h> | ||
| 36 | #include <unistd.h> | 35 | #include <unistd.h> |
| 36 | |||
| 37 | #include <libimobiledevice-glue/collection.h> | ||
| 38 | #include <libimobiledevice-glue/thread.h> | ||
| 39 | |||
| 37 | #include "device.h" | 40 | #include "device.h" |
| 38 | #include "client.h" | 41 | #include "client.h" |
| 39 | #include "preflight.h" | 42 | #include "preflight.h" |
| @@ -127,19 +130,19 @@ struct mux_device | |||
| 127 | }; | 130 | }; |
| 128 | 131 | ||
| 129 | static struct collection device_list; | 132 | static struct collection device_list; |
| 130 | pthread_mutex_t device_list_mutex; | 133 | mutex_t device_list_mutex; |
| 131 | 134 | ||
| 132 | static struct mux_device* get_mux_device_for_id(int device_id) | 135 | static struct mux_device* get_mux_device_for_id(int device_id) |
| 133 | { | 136 | { |
| 134 | struct mux_device *dev = NULL; | 137 | struct mux_device *dev = NULL; |
| 135 | pthread_mutex_lock(&device_list_mutex); | 138 | mutex_lock(&device_list_mutex); |
| 136 | FOREACH(struct mux_device *cdev, &device_list) { | 139 | FOREACH(struct mux_device *cdev, &device_list) { |
| 137 | if(cdev->id == device_id) { | 140 | if(cdev->id == device_id) { |
| 138 | dev = cdev; | 141 | dev = cdev; |
| 139 | break; | 142 | break; |
| 140 | } | 143 | } |
| 141 | } ENDFOREACH | 144 | } ENDFOREACH |
| 142 | pthread_mutex_unlock(&device_list_mutex); | 145 | mutex_unlock(&device_list_mutex); |
| 143 | 146 | ||
| 144 | return dev; | 147 | return dev; |
| 145 | } | 148 | } |
| @@ -166,7 +169,7 @@ static int get_next_device_id(void) | |||
| 166 | { | 169 | { |
| 167 | while(1) { | 170 | while(1) { |
| 168 | int ok = 1; | 171 | int ok = 1; |
| 169 | pthread_mutex_lock(&device_list_mutex); | 172 | mutex_lock(&device_list_mutex); |
| 170 | FOREACH(struct mux_device *dev, &device_list) { | 173 | FOREACH(struct mux_device *dev, &device_list) { |
| 171 | if(dev->id == next_device_id) { | 174 | if(dev->id == next_device_id) { |
| 172 | next_device_id++; | 175 | next_device_id++; |
| @@ -174,7 +177,7 @@ static int get_next_device_id(void) | |||
| 174 | break; | 177 | break; |
| 175 | } | 178 | } |
| 176 | } ENDFOREACH | 179 | } ENDFOREACH |
| 177 | pthread_mutex_unlock(&device_list_mutex); | 180 | mutex_unlock(&device_list_mutex); |
| 178 | if(ok) | 181 | if(ok) |
| 179 | return next_device_id++; | 182 | return next_device_id++; |
| 180 | } | 183 | } |
| @@ -464,9 +467,9 @@ static int send_tcp_ack(struct mux_connection *conn) | |||
| 464 | */ | 467 | */ |
| 465 | void device_client_process(int device_id, struct mux_client *client, short events) | 468 | void device_client_process(int device_id, struct mux_client *client, short events) |
| 466 | { | 469 | { |
| 467 | pthread_mutex_lock(&device_list_mutex); | 470 | mutex_lock(&device_list_mutex); |
| 468 | struct mux_connection *conn = get_mux_connection(device_id, client); | 471 | struct mux_connection *conn = get_mux_connection(device_id, client); |
| 469 | pthread_mutex_unlock(&device_list_mutex); | 472 | mutex_unlock(&device_list_mutex); |
| 470 | if(!conn) { | 473 | if(!conn) { |
| 471 | usbmuxd_log(LL_WARNING, "Could not find connection for device %d client %p", device_id, client); | 474 | usbmuxd_log(LL_WARNING, "Could not find connection for device %d client %p", device_id, client); |
| 472 | return; | 475 | return; |
| @@ -566,9 +569,9 @@ static void device_version_input(struct mux_device *dev, struct version_header * | |||
| 566 | vh->minor = ntohl(vh->minor); | 569 | vh->minor = ntohl(vh->minor); |
| 567 | if(vh->major != 2 && vh->major != 1) { | 570 | if(vh->major != 2 && vh->major != 1) { |
| 568 | usbmuxd_log(LL_ERROR, "Device %d has unknown version %d.%d", dev->id, vh->major, vh->minor); | 571 | usbmuxd_log(LL_ERROR, "Device %d has unknown version %d.%d", dev->id, vh->major, vh->minor); |
| 569 | pthread_mutex_lock(&device_list_mutex); | 572 | mutex_lock(&device_list_mutex); |
| 570 | collection_remove(&device_list, dev); | 573 | collection_remove(&device_list, dev); |
| 571 | pthread_mutex_unlock(&device_list_mutex); | 574 | mutex_unlock(&device_list_mutex); |
| 572 | free(dev); | 575 | free(dev); |
| 573 | return; | 576 | return; |
| 574 | } | 577 | } |
| @@ -726,14 +729,14 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned | |||
| 726 | void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length) | 729 | void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length) |
| 727 | { | 730 | { |
| 728 | struct mux_device *dev = NULL; | 731 | struct mux_device *dev = NULL; |
| 729 | pthread_mutex_lock(&device_list_mutex); | 732 | mutex_lock(&device_list_mutex); |
| 730 | FOREACH(struct mux_device *tdev, &device_list) { | 733 | FOREACH(struct mux_device *tdev, &device_list) { |
| 731 | if(tdev->usbdev == usbdev) { | 734 | if(tdev->usbdev == usbdev) { |
| 732 | dev = tdev; | 735 | dev = tdev; |
| 733 | break; | 736 | break; |
| 734 | } | 737 | } |
| 735 | } ENDFOREACH | 738 | } ENDFOREACH |
| 736 | pthread_mutex_unlock(&device_list_mutex); | 739 | mutex_unlock(&device_list_mutex); |
| 737 | if(!dev) { | 740 | if(!dev) { |
| 738 | usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); | 741 | usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); |
| 739 | return; | 742 | return; |
| @@ -850,15 +853,15 @@ int device_add(struct usb_device *usbdev) | |||
| 850 | free(dev); | 853 | free(dev); |
| 851 | return res; | 854 | return res; |
| 852 | } | 855 | } |
| 853 | pthread_mutex_lock(&device_list_mutex); | 856 | mutex_lock(&device_list_mutex); |
| 854 | collection_add(&device_list, dev); | 857 | collection_add(&device_list, dev); |
| 855 | pthread_mutex_unlock(&device_list_mutex); | 858 | mutex_unlock(&device_list_mutex); |
| 856 | return 0; | 859 | return 0; |
| 857 | } | 860 | } |
| 858 | 861 | ||
| 859 | void device_remove(struct usb_device *usbdev) | 862 | void device_remove(struct usb_device *usbdev) |
| 860 | { | 863 | { |
| 861 | pthread_mutex_lock(&device_list_mutex); | 864 | mutex_lock(&device_list_mutex); |
| 862 | FOREACH(struct mux_device *dev, &device_list) { | 865 | FOREACH(struct mux_device *dev, &device_list) { |
| 863 | if(dev->usbdev == usbdev) { | 866 | if(dev->usbdev == usbdev) { |
| 864 | usbmuxd_log(LL_NOTICE, "Removed device %d on location 0x%x", dev->id, usb_get_location(usbdev)); | 867 | usbmuxd_log(LL_NOTICE, "Removed device %d on location 0x%x", dev->id, usb_get_location(usbdev)); |
| @@ -874,48 +877,48 @@ void device_remove(struct usb_device *usbdev) | |||
| 874 | preflight_device_remove_cb(dev->preflight_cb_data); | 877 | preflight_device_remove_cb(dev->preflight_cb_data); |
| 875 | } | 878 | } |
| 876 | collection_remove(&device_list, dev); | 879 | collection_remove(&device_list, dev); |
| 877 | pthread_mutex_unlock(&device_list_mutex); | 880 | mutex_unlock(&device_list_mutex); |
| 878 | free(dev->pktbuf); | 881 | free(dev->pktbuf); |
| 879 | free(dev); | 882 | free(dev); |
| 880 | return; | 883 | return; |
| 881 | } | 884 | } |
| 882 | } ENDFOREACH | 885 | } ENDFOREACH |
| 883 | pthread_mutex_unlock(&device_list_mutex); | 886 | mutex_unlock(&device_list_mutex); |
| 884 | 887 | ||
| 885 | usbmuxd_log(LL_WARNING, "Cannot find device entry while removing USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); | 888 | usbmuxd_log(LL_WARNING, "Cannot find device entry while removing USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); |
| 886 | } | 889 | } |
| 887 | 890 | ||
| 888 | void device_set_visible(int device_id) | 891 | void device_set_visible(int device_id) |
| 889 | { | 892 | { |
| 890 | pthread_mutex_lock(&device_list_mutex); | 893 | mutex_lock(&device_list_mutex); |
| 891 | FOREACH(struct mux_device *dev, &device_list) { | 894 | FOREACH(struct mux_device *dev, &device_list) { |
| 892 | if(dev->id == device_id) { | 895 | if(dev->id == device_id) { |
| 893 | dev->visible = 1; | 896 | dev->visible = 1; |
| 894 | break; | 897 | break; |
| 895 | } | 898 | } |
| 896 | } ENDFOREACH | 899 | } ENDFOREACH |
| 897 | pthread_mutex_unlock(&device_list_mutex); | 900 | mutex_unlock(&device_list_mutex); |
| 898 | } | 901 | } |
| 899 | 902 | ||
| 900 | void device_set_preflight_cb_data(int device_id, void* data) | 903 | void device_set_preflight_cb_data(int device_id, void* data) |
| 901 | { | 904 | { |
| 902 | pthread_mutex_lock(&device_list_mutex); | 905 | mutex_lock(&device_list_mutex); |
| 903 | FOREACH(struct mux_device *dev, &device_list) { | 906 | FOREACH(struct mux_device *dev, &device_list) { |
| 904 | if(dev->id == device_id) { | 907 | if(dev->id == device_id) { |
| 905 | dev->preflight_cb_data = data; | 908 | dev->preflight_cb_data = data; |
| 906 | break; | 909 | break; |
| 907 | } | 910 | } |
| 908 | } ENDFOREACH | 911 | } ENDFOREACH |
| 909 | pthread_mutex_unlock(&device_list_mutex); | 912 | mutex_unlock(&device_list_mutex); |
| 910 | } | 913 | } |
| 911 | 914 | ||
| 912 | int device_get_count(int include_hidden) | 915 | int device_get_count(int include_hidden) |
| 913 | { | 916 | { |
| 914 | int count = 0; | 917 | int count = 0; |
| 915 | struct collection dev_list = {NULL, 0}; | 918 | struct collection dev_list = {NULL, 0}; |
| 916 | pthread_mutex_lock(&device_list_mutex); | 919 | mutex_lock(&device_list_mutex); |
| 917 | collection_copy(&dev_list, &device_list); | 920 | collection_copy(&dev_list, &device_list); |
| 918 | pthread_mutex_unlock(&device_list_mutex); | 921 | mutex_unlock(&device_list_mutex); |
| 919 | 922 | ||
| 920 | FOREACH(struct mux_device *dev, &dev_list) { | 923 | FOREACH(struct mux_device *dev, &dev_list) { |
| 921 | if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible)) | 924 | if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible)) |
| @@ -930,9 +933,9 @@ int device_get_list(int include_hidden, struct device_info **devices) | |||
| 930 | { | 933 | { |
| 931 | int count = 0; | 934 | int count = 0; |
| 932 | struct collection dev_list = {NULL, 0}; | 935 | struct collection dev_list = {NULL, 0}; |
| 933 | pthread_mutex_lock(&device_list_mutex); | 936 | mutex_lock(&device_list_mutex); |
| 934 | collection_copy(&dev_list, &device_list); | 937 | collection_copy(&dev_list, &device_list); |
| 935 | pthread_mutex_unlock(&device_list_mutex); | 938 | mutex_unlock(&device_list_mutex); |
| 936 | 939 | ||
| 937 | *devices = malloc(sizeof(struct device_info) * dev_list.capacity); | 940 | *devices = malloc(sizeof(struct device_info) * dev_list.capacity); |
| 938 | struct device_info *p = *devices; | 941 | struct device_info *p = *devices; |
| @@ -957,7 +960,7 @@ int device_get_list(int include_hidden, struct device_info **devices) | |||
| 957 | int device_get_timeout(void) | 960 | int device_get_timeout(void) |
| 958 | { | 961 | { |
| 959 | uint64_t oldest = (uint64_t)-1LL; | 962 | uint64_t oldest = (uint64_t)-1LL; |
| 960 | pthread_mutex_lock(&device_list_mutex); | 963 | mutex_lock(&device_list_mutex); |
| 961 | FOREACH(struct mux_device *dev, &device_list) { | 964 | FOREACH(struct mux_device *dev, &device_list) { |
| 962 | if(dev->state == MUXDEV_ACTIVE) { | 965 | if(dev->state == MUXDEV_ACTIVE) { |
| 963 | FOREACH(struct mux_connection *conn, &dev->connections) { | 966 | FOREACH(struct mux_connection *conn, &dev->connections) { |
| @@ -966,7 +969,7 @@ int device_get_timeout(void) | |||
| 966 | } ENDFOREACH | 969 | } ENDFOREACH |
| 967 | } | 970 | } |
| 968 | } ENDFOREACH | 971 | } ENDFOREACH |
| 969 | pthread_mutex_unlock(&device_list_mutex); | 972 | mutex_unlock(&device_list_mutex); |
| 970 | uint64_t ct = mstime64(); | 973 | uint64_t ct = mstime64(); |
| 971 | if((int64_t)oldest == -1LL) | 974 | if((int64_t)oldest == -1LL) |
| 972 | return 100000; //meh | 975 | return 100000; //meh |
| @@ -978,7 +981,7 @@ int device_get_timeout(void) | |||
| 978 | void device_check_timeouts(void) | 981 | void device_check_timeouts(void) |
| 979 | { | 982 | { |
| 980 | uint64_t ct = mstime64(); | 983 | uint64_t ct = mstime64(); |
| 981 | pthread_mutex_lock(&device_list_mutex); | 984 | mutex_lock(&device_list_mutex); |
| 982 | FOREACH(struct mux_device *dev, &device_list) { | 985 | FOREACH(struct mux_device *dev, &device_list) { |
| 983 | if(dev->state == MUXDEV_ACTIVE) { | 986 | if(dev->state == MUXDEV_ACTIVE) { |
| 984 | FOREACH(struct mux_connection *conn, &dev->connections) { | 987 | FOREACH(struct mux_connection *conn, &dev->connections) { |
| @@ -991,14 +994,14 @@ void device_check_timeouts(void) | |||
| 991 | } ENDFOREACH | 994 | } ENDFOREACH |
| 992 | } | 995 | } |
| 993 | } ENDFOREACH | 996 | } ENDFOREACH |
| 994 | pthread_mutex_unlock(&device_list_mutex); | 997 | mutex_unlock(&device_list_mutex); |
| 995 | } | 998 | } |
| 996 | 999 | ||
| 997 | void device_init(void) | 1000 | void device_init(void) |
| 998 | { | 1001 | { |
| 999 | usbmuxd_log(LL_DEBUG, "device_init"); | 1002 | usbmuxd_log(LL_DEBUG, "device_init"); |
| 1000 | collection_init(&device_list); | 1003 | collection_init(&device_list); |
| 1001 | pthread_mutex_init(&device_list_mutex, NULL); | 1004 | mutex_init(&device_list_mutex); |
| 1002 | next_device_id = 1; | 1005 | next_device_id = 1; |
| 1003 | } | 1006 | } |
| 1004 | 1007 | ||
| @@ -1019,7 +1022,7 @@ void device_kill_connections(void) | |||
| 1019 | void device_shutdown(void) | 1022 | void device_shutdown(void) |
| 1020 | { | 1023 | { |
| 1021 | usbmuxd_log(LL_DEBUG, "device_shutdown"); | 1024 | usbmuxd_log(LL_DEBUG, "device_shutdown"); |
| 1022 | pthread_mutex_lock(&device_list_mutex); | 1025 | mutex_lock(&device_list_mutex); |
| 1023 | FOREACH(struct mux_device *dev, &device_list) { | 1026 | FOREACH(struct mux_device *dev, &device_list) { |
| 1024 | FOREACH(struct mux_connection *conn, &dev->connections) { | 1027 | FOREACH(struct mux_connection *conn, &dev->connections) { |
| 1025 | connection_teardown(conn); | 1028 | connection_teardown(conn); |
| @@ -1028,7 +1031,7 @@ void device_shutdown(void) | |||
| 1028 | collection_remove(&device_list, dev); | 1031 | collection_remove(&device_list, dev); |
| 1029 | free(dev); | 1032 | free(dev); |
| 1030 | } ENDFOREACH | 1033 | } ENDFOREACH |
| 1031 | pthread_mutex_unlock(&device_list_mutex); | 1034 | mutex_unlock(&device_list_mutex); |
| 1032 | pthread_mutex_destroy(&device_list_mutex); | 1035 | mutex_destroy(&device_list_mutex); |
| 1033 | collection_free(&device_list); | 1036 | collection_free(&device_list); |
| 1034 | } | 1037 | } |
