From cb44c822d2877d8b915ca1c486c969338533097c Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Thu, 20 Jun 2019 15:14:53 +0100 Subject: [PATCH] st/prober: Refactor hidraw enumeration code in udev file --- src/xrt/state_trackers/prober/p_prober.c | 2 + src/xrt/state_trackers/prober/p_prober.h | 4 +- src/xrt/state_trackers/prober/p_udev.c | 95 +++++++++++++----------- 3 files changed, 57 insertions(+), 44 deletions(-) diff --git a/src/xrt/state_trackers/prober/p_prober.c b/src/xrt/state_trackers/prober/p_prober.c index bc01aef1c..5cd8d5d3b 100644 --- a/src/xrt/state_trackers/prober/p_prober.c +++ b/src/xrt/state_trackers/prober/p_prober.c @@ -267,6 +267,7 @@ teardown_devices(struct prober* p) for (size_t i = 0; i < p->num_devices; i++) { struct prober_device* pdev = &p->devices[i]; +#ifdef XRT_OS_LINUX for (size_t j = 0; j < pdev->num_hidraws; j++) { struct prober_hidraw* hidraw = &pdev->hidraws[j]; free((char*)hidraw->path); @@ -278,6 +279,7 @@ teardown_devices(struct prober* p) pdev->hidraws = NULL; pdev->num_hidraws = 0; } +#endif } if (p->devices != NULL) { diff --git a/src/xrt/state_trackers/prober/p_prober.h b/src/xrt/state_trackers/prober/p_prober.h index 32dde56ef..b19e287b7 100644 --- a/src/xrt/state_trackers/prober/p_prober.h +++ b/src/xrt/state_trackers/prober/p_prober.h @@ -55,6 +55,7 @@ #define MAX_AUTO_PROBERS 8 +#ifdef XRT_OS_LINUX /*! * A hidraw interface that a @ref prober_device exposes. */ @@ -63,6 +64,7 @@ struct prober_hidraw ssize_t interface; const char* path; }; +#endif /*! * A prober device. @@ -101,10 +103,10 @@ struct prober_device { const char** paths; } v4l; -#endif size_t num_hidraws; struct prober_hidraw* hidraws; +#endif }; struct prober diff --git a/src/xrt/state_trackers/prober/p_udev.c b/src/xrt/state_trackers/prober/p_udev.c index 2f2c211aa..7df970798 100644 --- a/src/xrt/state_trackers/prober/p_udev.c +++ b/src/xrt/state_trackers/prober/p_udev.c @@ -34,9 +34,12 @@ */ static void -p_udev_add_interface(struct prober_device* pdev, - uint32_t interface, - const char* path); +p_udev_enumerate_hidraw(struct prober* p, struct udev* udev); + +static void +p_udev_add_hidraw(struct prober_device* pdev, + uint32_t interface, + const char* path); static int p_udev_get_interface_number(struct udev_device* raw_dev, @@ -50,10 +53,10 @@ p_udev_get_and_parse_uevent(struct udev_device* raw_dev, uint64_t* out_bluetooth_serial); static int -p_udev_get_usb_address(struct udev_device* raw_dev, - uint32_t bus_type, - uint16_t* usb_bus, - uint16_t* usb_addr); +p_udev_get_usb_hid_address(struct udev_device* raw_dev, + uint32_t bus_type, + uint16_t* usb_bus, + uint16_t* usb_addr); /* @@ -65,27 +68,32 @@ p_udev_get_usb_address(struct udev_device* raw_dev, int p_udev_probe(struct prober* p) { - struct prober_device* pdev; - struct udev* udev; - struct udev_enumerate* enumerate; - struct udev_list_entry *devices, *dev_list_entry; - struct udev_device* raw_dev = NULL; - uint16_t vendor_id, product_id, interface; - uint16_t usb_bus = 0; - uint16_t usb_addr = 0; - uint32_t bus_type; - uint64_t bluetooth_id; - int ret; - - const char* sysfs_path; - const char* dev_path; - - udev = udev_new(); + struct udev* udev = udev_new(); if (!udev) { P_ERROR(p, "Can't create udev\n"); return -1; } + p_udev_enumerate_hidraw(p, udev); + + udev = udev_unref(udev); + + return 0; +} + + +/* + * + * Internal functions. + * + */ + +static void +p_udev_enumerate_hidraw(struct prober* p, struct udev* udev) +{ + struct udev_enumerate* enumerate; + struct udev_list_entry *devices, *dev_list_entry; + enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "hidraw"); udev_enumerate_scan_devices(enumerate); @@ -94,6 +102,17 @@ p_udev_probe(struct prober* p) udev_list_entry_foreach(dev_list_entry, devices) { + struct prober_device* pdev = NULL; + struct udev_device* raw_dev = NULL; + uint16_t vendor_id, product_id, interface; + uint16_t usb_bus = 0; + uint16_t usb_addr = 0; + uint32_t bus_type = 0; + uint64_t bluetooth_id = 0; + const char* sysfs_path; + const char* dev_path; + int ret; + // Where in the sysfs is. sysfs_path = udev_list_entry_get_name(dev_list_entry); // Raw sysfs node. @@ -117,8 +136,8 @@ p_udev_probe(struct prober* p) } // Get USB bus and address to de-dublicate devices. - ret = p_udev_get_usb_address(raw_dev, bus_type, &usb_bus, - &usb_addr); + ret = p_udev_get_usb_hid_address(raw_dev, bus_type, &usb_bus, + &usb_addr); if (ret != 0) { P_ERROR(p, "Failed to get USB bus and addr."); goto next; @@ -161,29 +180,19 @@ p_udev_probe(struct prober* p) } // Add this interface to the usb device. - p_udev_add_interface(pdev, interface, dev_path); + p_udev_add_hidraw(pdev, interface, dev_path); next: udev_device_unref(raw_dev); } enumerate = udev_enumerate_unref(enumerate); - udev = udev_unref(udev); - - return 0; } - -/* - * - * Internal functions. - * - */ - static void -p_udev_add_interface(struct prober_device* pdev, - uint32_t interface, - const char* path) +p_udev_add_hidraw(struct prober_device* pdev, + uint32_t interface, + const char* path) { size_t new_size = (pdev->num_hidraws + 1) * sizeof(struct prober_hidraw); @@ -197,10 +206,10 @@ p_udev_add_interface(struct prober_device* pdev, } static int -p_udev_get_usb_address(struct udev_device* raw_dev, - uint32_t bus_type, - uint16_t* usb_bus, - uint16_t* usb_addr) +p_udev_get_usb_hid_address(struct udev_device* raw_dev, + uint32_t bus_type, + uint16_t* usb_bus, + uint16_t* usb_addr) { struct udev_device* usb_dev; const char* bus_str;