Skip to content

Commit 6472fa2

Browse files
committed
Adding test_device to connected device list
1 parent 3d8cd31 commit 6472fa2

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

src/main.c

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,57 +47,59 @@ typedef struct DeviceListNode {
4747

4848
static int find_devices(DeviceList** device_list, int test_device)
4949
{
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
5057
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++;
6063
}
6164
}
65+
6266
struct hid_device_info* devs;
6367
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;
6771

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
7273
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) {
7475
cur_dev = cur_dev->next;
7576
continue;
7677
}
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)) {
7879
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;
8384
}
8485
cur_dev = cur_dev->next;
8586
}
86-
free(devices_found->element);
87-
free(devices_found);
87+
free(curr_node->element);
88+
free(curr_node);
8889

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;
9193
for (int i = 0; i < found; i++) {
9294
DeviceList* device_element = *device_list + i;
93-
device_element->device = devices_found->element;
95+
device_element->device = curr_node->element;
9496
device_element->num_devices = found - i;
9597
device_element->featureRequests = NULL;
9698
device_element->size = 0;
9799

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;
101103
}
102104
hid_free_enumeration(devs);
103105

0 commit comments

Comments
 (0)