Found a C macro called `udev_list_entry_foreach` w...
# kotlin-native
n
Found a C macro called
udev_list_entry_foreach
which is used to access each entry in the list. Would manually iterating over the list (via
udev_list_entry_get_next
) be the way to workaround the macro? I am using this C example as a reference: https://github.com/gavv/snippets/blob/master/udev/udev_monitor_usb.c
Tried the iteration method however it doesn't seem to iterate over the list, and the same USB device appears twice.
Definition file for the udev library.
Below is the program's output:
Copy code
> Task :runSerial_reading_demoReleaseExecutableLinux
Starting USB monitor example...
Device Path: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-0:1.0
* Device: Action - exists, Vendor - 0000, Product - 0000
Device Path: /sys/devices/pci0000:00/0000:00:14.0/usb1/1-0:1.0
* Device: Action - exists, Vendor - 0000, Product - 0000
Below is the output from the
lsusb
command:
Copy code
Bus 002 Device 002: ID 0bda:0316 Realtek Semiconductor Corp. 
Bus 002 Device 008: ID 2109:0813 VIA Labs, Inc. 
Bus 002 Device 007: ID 2109:0813 VIA Labs, Inc. 
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 04f2:b604 Chicony Electronics Co., Ltd 
Bus 001 Device 013: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light
Bus 001 Device 012: ID 2109:2813 VIA Labs, Inc. 
Bus 001 Device 011: ID 2109:2813 VIA Labs, Inc. 
Bus 001 Device 002: ID 046d:c52f Logitech, Inc. Unifying Receiver
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
m
Maybe you use wrong definition of list_entry In systemd it defined as
Copy code
struct udev_list_entry {
        struct udev_list *list;
        char *name;
        char *value;

        LIST_FIELDS(struct udev_list_entry, entries);
};
n
Was close but forgot to assign the output of
udev_enumerate_get_list_entry
to a property 🤦‍♂️. Iterating through the registered devices now looks like this:
Copy code
val devices = udev_enumerate_get_list_entry(enumerate)
        val usbDevices = mutableListOf<UsbDevice>()
        if (devices != null) {
            val tmp = processListEntry(udev, devices)
            if (tmp.vendor.isNotEmpty()) usbDevices += tmp
        }
        var entry = udev_list_entry_get_next(devices)
        while (entry != null) {
            val tmp = processListEntry(udev, entry)
            if (tmp.vendor.isNotEmpty()) usbDevices += tmp
            entry = udev_list_entry_get_next(entry)
        }
        usbDevices.forEach { println("* $it") }
        println("Total USB Devices: ${usbDevices.size}")
d
Have a look at
generateSequence
, you might find it useful here.
1