@@ -47,57 +47,59 @@ typedef struct DeviceListNode {
47
47
48
48
static int find_devices (DeviceList * * device_list , int test_device )
49
49
{
50
+ DeviceListNode * devices_found = malloc (sizeof (DeviceListNode ));
51
+ devices_found -> element = malloc (sizeof (struct device ));
52
+ DeviceListNode * curr_node = devices_found ;
53
+
54
+ int found = 0 ;
55
+
56
+ // Adding test device
50
57
if (test_device ) {
51
- DeviceList * device_element = malloc (sizeof (DeviceList ));
52
- device_element -> device = malloc (sizeof (struct device ));
53
- if (!get_device (device_element -> device , VENDOR_TESTDEVICE , PRODUCT_TESTDEVICE )) {
54
- * device_list = device_element ;
55
- return 1 ;
56
- } else {
57
- free (device_element -> device );
58
- free (device_element );
59
- return 0 ;
58
+ if (!get_device (devices_found -> element , VENDOR_TESTDEVICE , PRODUCT_TESTDEVICE )) {
59
+ curr_node -> next = malloc (sizeof (struct DeviceListNode ));
60
+ curr_node -> next -> element = malloc (sizeof (struct device ));
61
+ curr_node = devices_found -> next ;
62
+ found ++ ;
60
63
}
61
64
}
65
+
62
66
struct hid_device_info * devs ;
63
67
struct hid_device_info * cur_dev ;
64
- int found = 0 ;
65
- devs = hid_enumerate (0x0 , 0x0 );
66
- cur_dev = devs ;
68
+ struct device * last_device = NULL ;
69
+ devs = hid_enumerate (0x0 , 0x0 );
70
+ cur_dev = devs ;
67
71
68
- DeviceListNode * head = malloc (sizeof (DeviceListNode ));
69
- DeviceListNode * devices_found = head ;
70
- devices_found -> element = malloc (sizeof (struct device ));
71
- struct device * last_device = NULL ;
72
+ // Iterate through all devices and the compatible to device_found list
72
73
while (cur_dev ) {
73
- if (last_device != NULL && last_device -> idVendor == cur_dev -> vendor_id && last_device -> idProduct == cur_dev -> product_id ) {
74
+ if (false && last_device != NULL && last_device -> idVendor == cur_dev -> vendor_id && last_device -> idProduct == cur_dev -> product_id ) {
74
75
cur_dev = cur_dev -> next ;
75
76
continue ;
76
77
}
77
- if (!get_device (devices_found -> element , cur_dev -> vendor_id , cur_dev -> product_id )) {
78
+ if (!get_device (curr_node -> element , cur_dev -> vendor_id , cur_dev -> product_id )) {
78
79
found ++ ;
79
- last_device = devices_found -> element ;
80
- devices_found -> next = malloc (sizeof (struct DeviceListNode ));
81
- devices_found = devices_found -> next ;
82
- devices_found -> element = malloc ( sizeof ( struct device )) ;
80
+ last_device = curr_node -> element ;
81
+ curr_node -> next = malloc (sizeof (struct DeviceListNode ));
82
+ curr_node -> next -> element = malloc ( sizeof ( struct device )) ;
83
+ curr_node = curr_node -> next ;
83
84
}
84
85
cur_dev = cur_dev -> next ;
85
86
}
86
- free (devices_found -> element );
87
- free (devices_found );
87
+ free (curr_node -> element );
88
+ free (curr_node );
88
89
89
- * device_list = malloc (sizeof (DeviceList ) * found );
90
- devices_found = head ;
90
+ // Copy by address the found devices to the device_list
91
+ * device_list = malloc (sizeof (DeviceList ) * found );
92
+ curr_node = devices_found ;
91
93
for (int i = 0 ; i < found ; i ++ ) {
92
94
DeviceList * device_element = * device_list + i ;
93
- device_element -> device = devices_found -> element ;
95
+ device_element -> device = curr_node -> element ;
94
96
device_element -> num_devices = found - i ;
95
97
device_element -> featureRequests = NULL ;
96
98
device_element -> size = 0 ;
97
99
98
- devices_found = devices_found -> next ;
99
- free (head );
100
- head = devices_found ;
100
+ curr_node = curr_node -> next ;
101
+ free (devices_found );
102
+ devices_found = curr_node ;
101
103
}
102
104
hid_free_enumeration (devs );
103
105
0 commit comments