@@ -75,6 +75,7 @@ struct hid_device_ {
7575 int device_handle ;
7676 int blocking ;
7777 wchar_t * last_error_str ;
78+ wchar_t * last_read_error_str ;
7879 struct hid_device_info * device_info ;
7980};
8081
@@ -97,6 +98,7 @@ static hid_device *new_hid_device(void)
9798 dev -> device_handle = -1 ;
9899 dev -> blocking = 1 ;
99100 dev -> last_error_str = NULL ;
101+ dev -> last_read_error_str = NULL ;
100102 dev -> device_info = NULL ;
101103
102104 return dev ;
@@ -1108,7 +1110,7 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
11081110int HID_API_EXPORT hid_read_timeout (hid_device * dev , unsigned char * data , size_t length , int milliseconds )
11091111{
11101112 /* Set device error to none */
1111- register_device_error ( dev , NULL );
1113+ register_error_str ( & dev -> last_read_error_str , NULL );
11121114
11131115 int bytes_read ;
11141116
@@ -1132,15 +1134,15 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
11321134 }
11331135 if (ret == -1 ) {
11341136 /* Error */
1135- register_device_error ( dev , strerror (errno ));
1137+ register_error_str ( & dev -> last_read_error_str , strerror (errno ));
11361138 return ret ;
11371139 }
11381140 else {
11391141 /* Check for errors on the file descriptor. This will
11401142 indicate a device disconnection. */
11411143 if (fds .revents & (POLLERR | POLLHUP | POLLNVAL )) {
11421144 // We cannot use strerror() here as no -1 was returned from poll().
1143- register_device_error ( dev , "hid_read_timeout: unexpected poll error (device disconnected)" );
1145+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: unexpected poll error (device disconnected)" );
11441146 return -1 ;
11451147 }
11461148 }
@@ -1151,7 +1153,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
11511153 if (errno == EAGAIN || errno == EINPROGRESS )
11521154 bytes_read = 0 ;
11531155 else
1154- register_device_error ( dev , strerror (errno ));
1156+ register_error_str ( & dev -> last_read_error_str , strerror (errno ));
11551157 }
11561158
11571159 return bytes_read ;
@@ -1162,6 +1164,13 @@ int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
11621164 return hid_read_timeout (dev , data , length , (dev -> blocking )? -1 : 0 );
11631165}
11641166
1167+ HID_API_EXPORT const wchar_t * HID_API_CALL hid_read_error (hid_device * dev )
1168+ {
1169+ if (dev -> last_read_error_str == NULL )
1170+ return L"Success" ;
1171+ return dev -> last_read_error_str ;
1172+ }
1173+
11651174int HID_API_EXPORT hid_set_nonblocking (hid_device * dev , int nonblock )
11661175{
11671176 /* Do all non-blocking in userspace using poll(), since it looks
@@ -1232,8 +1241,8 @@ void HID_API_EXPORT hid_close(hid_device *dev)
12321241
12331242 close (dev -> device_handle );
12341243
1235- /* Free the device error message */
1236- register_device_error (dev , NULL );
1244+ free ( dev -> last_error_str );
1245+ free (dev -> last_read_error_str );
12371246
12381247 hid_free_enumeration (dev -> device_info );
12391248
0 commit comments