#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libudev.h>

#define OAU_VENDOR_ID	"1556"	/* CERN USB Vendor ID */
#define OAU_DEVICE_ID	"0443"	/* oau USB Device ID */

#define ID_VENDOR_ID	OAU_VENDOR_ID
#define ID_DEVICE_ID	OAU_DEVICE_ID

#define	MAX_OUA		16

static struct oua_usb_dev {
	char	path[PATH_MAX];
	char 	busnum[6];
	char	devnum[6];
	char	portnum[6];
	char	devno[10];
	char	serial[256];
} usb_dev[MAX_OUA];

static int oua_enumerate(char *vendor_id, char *device_id);

int main(int argc, char *argv[])
{
	oua_enumerate(ID_VENDOR_ID, ID_DEVICE_ID);
	return 0;
}

static int oua_enumerate(char *vendor_id, char *device_id)
{
    struct udev *udev = udev_new();
    struct udev_enumerate *enumerate = udev_enumerate_new(udev);
    struct udev_list_entry *devs, *ptr, *ptr2;
    int found_devs = 0;

    //udev_enumerate_add_match_subsystem(enumerate, "tty");
    udev_enumerate_add_match_property(enumerate, "ID_VENDOR_ID", vendor_id);
    udev_enumerate_add_match_property(enumerate, "ID_MODEL_ID", device_id);
    udev_enumerate_scan_devices(enumerate);

    devs = udev_enumerate_get_list_entry(enumerate);
    udev_list_entry_foreach(ptr, devs) {
	if (found_devs >= MAX_OUA)
		goto out;
        const char *path = udev_list_entry_get_name(ptr);
        struct udev_device *dev = udev_device_new_from_syspath(udev, path);
    	struct oua_usb_dev *entry = &usb_dev[found_devs];
	strcpy(entry->path, path);
	strcpy(entry->busnum, udev_device_get_sysattr_value(dev, "busnum"));
	strcpy(entry->devnum, udev_device_get_sysattr_value(dev, "devnum"));
	strcpy(entry->devno, udev_device_get_sysattr_value(dev, "dev"));
	strcpy(entry->serial, udev_device_get_sysattr_value(dev, "serial"));

	printf("bus:dev %03ld:%03ld, devno: %s, serial: %s, devpath=[%s]\n",
		strtoul(entry->busnum, NULL, 0), strtoul(entry->devnum, NULL, 0),
		entry->devno, entry->serial, entry->path);

	found_devs++;
	udev_device_unref(dev);
    }
out:
    udev_enumerate_unref(enumerate);
    return found_devs;
}
