@@ -763,38 +763,64 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
763
763
764
764
static int set_report (hid_device * dev , IOHIDReportType type , const unsigned char * data , size_t length )
765
765
{
766
- const unsigned char * data_to_send ;
767
- size_t length_to_send ;
766
+ const unsigned char * data_to_send = data ;
767
+ CFIndex length_to_send = length ;
768
768
IOReturn res ;
769
+ const unsigned char report_id = data [0 ];
769
770
770
- /* Return if the device has been disconnected. */
771
- if (dev -> disconnected )
772
- return -1 ;
773
-
774
- if (data [0 ] == 0x0 ) {
771
+ if (report_id == 0x0 ) {
775
772
/* Not using numbered Reports.
776
773
Don't send the report number. */
777
774
data_to_send = data + 1 ;
778
775
length_to_send = length - 1 ;
779
776
}
780
- else {
781
- /* Using numbered Reports.
782
- Send the Report Number */
783
- data_to_send = data ;
784
- length_to_send = length ;
777
+
778
+ /* Avoid crash if the device has been unplugged. */
779
+ if (dev -> disconnected ) {
780
+ return -1 ;
785
781
}
786
782
787
- if (!dev -> disconnected ) {
788
- res = IOHIDDeviceSetReport (dev -> device_handle ,
789
- type ,
790
- data [0 ], /* Report ID*/
791
- data_to_send , length_to_send );
783
+ res = IOHIDDeviceSetReport (dev -> device_handle ,
784
+ type ,
785
+ report_id ,
786
+ data_to_send , length_to_send );
787
+
788
+ if (res == kIOReturnSuccess ) {
789
+ return length ;
790
+ }
792
791
793
- if (res == kIOReturnSuccess ) {
794
- return length ;
792
+ return -1 ;
793
+ }
794
+
795
+ static int get_report (hid_device * dev , IOHIDReportType type , unsigned char * data , size_t length )
796
+ {
797
+ unsigned char * report = data ;
798
+ CFIndex report_length = length ;
799
+ IOReturn res = kIOReturnSuccess ;
800
+ const unsigned char report_id = data [0 ];
801
+
802
+ if (report_id == 0x0 ) {
803
+ /* Not using numbered Reports.
804
+ Don't send the report number. */
805
+ report = data + 1 ;
806
+ report_length = length - 1 ;
807
+ }
808
+
809
+ /* Avoid crash if the device has been unplugged. */
810
+ if (dev -> disconnected ) {
811
+ return -1 ;
812
+ }
813
+
814
+ res = IOHIDDeviceGetReport (dev -> device_handle ,
815
+ type ,
816
+ report_id ,
817
+ report , & report_length );
818
+
819
+ if (res == kIOReturnSuccess ) {
820
+ if (report_id == 0x0 ) { // 0 report number still present at the beginning
821
+ report_length ++ ;
795
822
}
796
- else
797
- return -1 ;
823
+ return report_length ;
798
824
}
799
825
800
826
return -1 ;
@@ -954,24 +980,9 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
954
980
955
981
int HID_API_EXPORT hid_get_feature_report (hid_device * dev , unsigned char * data , size_t length )
956
982
{
957
- CFIndex len = length - 1 ;
958
- IOReturn res ;
959
-
960
- /* Return if the device has been unplugged. */
961
- if (dev -> disconnected )
962
- return -1 ;
963
-
964
- res = IOHIDDeviceGetReport (dev -> device_handle ,
965
- kIOHIDReportTypeFeature ,
966
- data [0 ], /* Report ID */
967
- data + 1 , & len );
968
- if (res == kIOReturnSuccess )
969
- return len + 1 ;
970
- else
971
- return -1 ;
983
+ return get_report (dev , kIOHIDReportTypeFeature , data , length );
972
984
}
973
985
974
-
975
986
void HID_API_EXPORT hid_close (hid_device * dev )
976
987
{
977
988
if (!dev )
0 commit comments