@@ -142,6 +142,7 @@ struct hid_device_ {
142142 pthread_barrier_t shutdown_barrier ; /* Ensures correct shutdown sequence */
143143 int shutdown_thread ;
144144 wchar_t * last_error_str ;
145+ wchar_t * last_read_error_str ;
145146};
146147
147148static hid_device * new_hid_device (void )
@@ -163,6 +164,7 @@ static hid_device *new_hid_device(void)
163164 dev -> device_info = NULL ;
164165 dev -> shutdown_thread = 0 ;
165166 dev -> last_error_str = NULL ;
167+ dev -> last_read_error_str = NULL ;
166168
167169 /* Thread objects */
168170 pthread_mutex_init (& dev -> mutex , NULL );
@@ -196,6 +198,7 @@ static void free_hid_device(hid_device *dev)
196198 CFRelease (dev -> source );
197199 free (dev -> input_report_buf );
198200 free (dev -> last_error_str );
201+ free (dev -> last_read_error_str );
199202 hid_free_enumeration (dev -> device_info );
200203
201204 /* Clean up the thread objects */
@@ -1244,6 +1247,8 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12441247{
12451248 int bytes_read = -1 ;
12461249
1250+ register_error_str (& dev -> last_read_error_str , NULL );
1251+
12471252 /* Lock the access to the report list. */
12481253 pthread_mutex_lock (& dev -> mutex );
12491254
@@ -1257,7 +1262,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12571262 /* Return if the device has been disconnected. */
12581263 if (dev -> disconnected ) {
12591264 bytes_read = -1 ;
1260- register_device_error ( dev , "hid_read_timeout: device disconnected" );
1265+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: device disconnected" );
12611266 goto ret ;
12621267 }
12631268
@@ -1266,7 +1271,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12661271 has been an error. An error code of -1 should
12671272 be returned. */
12681273 bytes_read = -1 ;
1269- register_device_error ( dev , "hid_read_timeout: thread shutdown" );
1274+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: thread shutdown" );
12701275 goto ret ;
12711276 }
12721277
@@ -1280,7 +1285,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
12801285 bytes_read = return_data (dev , data , length );
12811286 else {
12821287 /* There was an error, or a device disconnection. */
1283- register_device_error ( dev , "hid_read_timeout: error waiting for more data" );
1288+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: error waiting for more data" );
12841289 bytes_read = -1 ;
12851290 }
12861291 }
@@ -1304,7 +1309,7 @@ int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t
13041309 } else if (res == ETIMEDOUT ) {
13051310 bytes_read = 0 ;
13061311 } else {
1307- register_device_error ( dev , "hid_read_timeout: error waiting for more data" );
1312+ register_error_str ( & dev -> last_read_error_str , "hid_read_timeout: error waiting for more data" );
13081313 bytes_read = -1 ;
13091314 }
13101315 }
@@ -1324,6 +1329,13 @@ int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
13241329 return hid_read_timeout (dev , data , length , (dev -> blocking )? -1 : 0 );
13251330}
13261331
1332+ HID_API_EXPORT const wchar_t * HID_API_CALL hid_read_error (hid_device * dev )
1333+ {
1334+ if (dev -> last_read_error_str == NULL )
1335+ return L"Success" ;
1336+ return dev -> last_read_error_str ;
1337+ }
1338+
13271339int HID_API_EXPORT hid_set_nonblocking (hid_device * dev , int nonblock )
13281340{
13291341 /* All Nonblocking operation is handled by the library. */
0 commit comments