@@ -137,6 +137,7 @@ struct hid_device_ {
137137#endif
138138
139139 hidapi_error_ctx error ;
140+ wchar_t * last_read_error_str ;
140141};
141142
142143static struct hid_api_version api_version = {
@@ -177,6 +178,7 @@ static void free_hid_device(hid_device *dev)
177178
178179 hid_free_enumeration (dev -> device_info );
179180 free_hidapi_error (& dev -> error );
181+ free (dev -> last_read_error_str );
180182
181183 /* Free the device itself */
182184 free (dev );
@@ -349,6 +351,13 @@ static void register_string_error(hidapi_error_ctx *err, const char *error)
349351}
350352
351353
354+ static void register_read_error (hid_device * dev , const char * error )
355+ {
356+ free (dev -> last_read_error_str );
357+ dev -> last_read_error_str = error ? ctowcdup (error , strlen (error )) : NULL ;
358+ }
359+
360+
352361/* we don't use iconv on Android, or when it is explicitly disabled */
353362#if !defined(__ANDROID__ ) && !defined(NO_ICONV )
354363
@@ -1641,15 +1650,15 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
16411650 return transferred ;
16421651#endif
16431652 /* by initialising this variable right here, GCC gives a compilation warning/error: */
1644- /* error: variable ‘ bytes_read’ might be clobbered by ‘ longjmp’ or ‘ vfork’ [-Werror=clobbered] */
1653+ /* error: variable ' bytes_read' might be clobbered by ' longjmp' or ' vfork' [-Werror=clobbered] */
16451654 int bytes_read ; /* = -1; */
16461655
16471656 if (!data || !length ) {
1648- register_string_error ( & dev -> error , "Zero buffer/length" );
1657+ register_read_error ( dev , "Zero buffer/length" );
16491658 return -1 ;
16501659 }
16511660
1652- register_libusb_error ( & dev -> error , LIBUSB_SUCCESS , NULL );
1661+ register_read_error ( dev , NULL );
16531662
16541663 hidapi_thread_mutex_lock (& dev -> thread_state );
16551664 hidapi_thread_cleanup_push (cleanup_mutex , dev );
@@ -1667,7 +1676,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
16671676 /* This means the device has been disconnected.
16681677 An error code of -1 should be returned. */
16691678 bytes_read = -1 ;
1670- register_string_error ( & dev -> error , "hid_read(_timeout): device is closing" );
1679+ register_read_error ( dev , "hid_read(_timeout): device is closing" );
16711680 goto ret ;
16721681 }
16731682
@@ -1707,7 +1716,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
17071716 else {
17081717 /* Error. */
17091718 bytes_read = -1 ;
1710- register_string_error ( & dev -> error , "hid_read(_timeout): error waiting for data" );
1719+ register_read_error ( dev , "hid_read(_timeout): error waiting for data" );
17111720 break ;
17121721 }
17131722 }
@@ -1733,8 +1742,9 @@ int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
17331742
17341743HID_API_EXPORT const wchar_t * HID_API_CALL hid_read_error (hid_device * dev )
17351744{
1736- (void )dev ;
1737- return L"hid_read_error is not implemented yet" ;
1745+ if (dev -> last_read_error_str == NULL )
1746+ return L"Success" ;
1747+ return dev -> last_read_error_str ;
17381748}
17391749
17401750
0 commit comments