| 1041 | |
| 1042 | |
| 1043 | int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds) |
| 1044 | { |
| 1045 | /* Set device error to none */ |
| 1046 | register_device_error(dev, NULL); |
| 1047 | |
| 1048 | int bytes_read; |
| 1049 | |
| 1050 | if (milliseconds >= 0) { |
| 1051 | /* Milliseconds is either 0 (non-blocking) or > 0 (contains |
| 1052 | a valid timeout). In both cases we want to call poll() |
| 1053 | and wait for data to arrive. Don't rely on non-blocking |
| 1054 | operation (O_NONBLOCK) since some kernels don't seem to |
| 1055 | properly report device disconnection through read() when |
| 1056 | in non-blocking mode. */ |
| 1057 | int ret; |
| 1058 | struct pollfd fds; |
| 1059 | |
| 1060 | fds.fd = dev->device_handle; |
| 1061 | fds.events = POLLIN; |
| 1062 | fds.revents = 0; |
| 1063 | ret = poll(&fds, 1, milliseconds); |
| 1064 | if (ret == 0) { |
| 1065 | /* Timeout */ |
| 1066 | return ret; |
| 1067 | } |
| 1068 | if (ret == -1) { |
| 1069 | /* Error */ |
| 1070 | register_device_error(dev, strerror(errno)); |
| 1071 | return ret; |
| 1072 | } |
| 1073 | else { |
| 1074 | /* Check for errors on the file descriptor. This will |
| 1075 | indicate a device disconnection. */ |
| 1076 | if (fds.revents & (POLLERR | POLLHUP | POLLNVAL)) { |
| 1077 | // We cannot use strerror() here as no -1 was returned from poll(). |
| 1078 | register_device_error(dev, "hid_read_timeout: unexpected poll error (device disconnected)"); |
| 1079 | return -1; |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | bytes_read = read(dev->device_handle, data, length); |
| 1085 | if (bytes_read < 0) { |
| 1086 | if (errno == EAGAIN || errno == EINPROGRESS) |
| 1087 | bytes_read = 0; |
| 1088 | else |
| 1089 | register_device_error(dev, strerror(errno)); |
| 1090 | } |
| 1091 | |
| 1092 | return bytes_read; |
| 1093 | } |
| 1094 | |
| 1095 | int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length) |
| 1096 | { |
no test coverage detected