| 943 | |
| 944 | |
| 945 | int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds) |
| 946 | { |
| 947 | int bytes_read = -1; |
| 948 | |
| 949 | #if 1 |
| 950 | int transferred; |
| 951 | int res = libusb_interrupt_transfer(dev->device_handle, dev->input_endpoint, data, length, &transferred, 5000); |
| 952 | LOG("transferred: %d\n", transferred); |
| 953 | return transferred; |
| 954 | #endif |
| 955 | |
| 956 | //pthread_mutex_lock(&dev->mutex); |
| 957 | //pthread_cleanup_push(&cleanup_mutex, dev); |
| 958 | |
| 959 | /* There's an input report queued up. Return it. */ |
| 960 | if (dev->input_reports) { |
| 961 | /* Return the first one */ |
| 962 | bytes_read = return_data(dev, data, length); |
| 963 | goto ret; |
| 964 | } |
| 965 | |
| 966 | if (dev->shutdown_thread) { |
| 967 | /* This means the device has been disconnected. |
| 968 | An error code of -1 should be returned. */ |
| 969 | bytes_read = -1; |
| 970 | goto ret; |
| 971 | } |
| 972 | |
| 973 | if (milliseconds == -1) { |
| 974 | /* Blocking */ |
| 975 | while (!dev->input_reports && !dev->shutdown_thread) { |
| 976 | //pthread_cond_wait(&dev->condition, &dev->mutex); |
| 977 | } |
| 978 | if (dev->input_reports) { |
| 979 | bytes_read = return_data(dev, data, length); |
| 980 | } |
| 981 | } |
| 982 | else if (milliseconds > 0) { |
| 983 | /* Non-blocking, but called with timeout. */ |
| 984 | int res; |
| 985 | struct timespec ts; |
| 986 | clock_gettime(CLOCK_REALTIME, &ts); |
| 987 | ts.tv_sec += milliseconds / 1000; |
| 988 | ts.tv_nsec += (milliseconds % 1000) * 1000000; |
| 989 | if (ts.tv_nsec >= 1000000000L) { |
| 990 | ts.tv_sec++; |
| 991 | ts.tv_nsec -= 1000000000L; |
| 992 | } |
| 993 | |
| 994 | while (!dev->input_reports && !dev->shutdown_thread) { |
| 995 | //res = pthread_cond_timedwait(&dev->condition, &dev->mutex, &ts); |
| 996 | if (res == 0) { |
| 997 | if (dev->input_reports) { |
| 998 | bytes_read = return_data(dev, data, length); |
| 999 | break; |
| 1000 | } |
| 1001 | |
| 1002 | /* If we're here, there was a spurious wake up |
no test coverage detected