diff --git a/Makefile b/Makefile index c59a675..a611e3f 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ build: target/mpfm target/mpfm.service target/mpfm: mpfm.c mkdir -p target - $(CC) -std=gnu99 -O2 mpfm.c -lusb-1.0 -o target/mpfm + $(CC) -std=gnu99 -O2 mpfm.c -lusb-1.0 -ludev -o target/mpfm target/mpfm.service: mpfm.service mkdir -p target diff --git a/mpfm.c b/mpfm.c index 361d88d..187b568 100644 --- a/mpfm.c +++ b/mpfm.c @@ -4,6 +4,59 @@ #include static int count = 0; +#include +#include + +void get_partition(uint16_t idVendor, uint16_t idProduct) { + struct udev *udev = udev_new(); + if (!udev) { + printf("Error: Failed to initialize udev.\n"); + return; + } + + struct udev_enumerate *enumerate = udev_enumerate_new(udev); + udev_enumerate_add_match_subsystem(enumerate, "block"); + udev_enumerate_scan_devices(enumerate); + + struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate); + struct udev_list_entry *entry; + + udev_list_entry_foreach(entry, devices) { + const char *path = udev_list_entry_get_name(entry); + struct udev_device *dev = udev_device_new_from_syspath(udev, path); + + // Obtén el idVendor e idProduct del dispositivo USB actual + const char *vendor_id_str = udev_device_get_sysattr_value(dev, "idVendor"); + const char *product_id_str = udev_device_get_sysattr_value(dev, "idProduct"); + printf(vendor_id_str); + + if (vendor_id_str && product_id_str) { + uint16_t vendor_id = strtol(vendor_id_str, NULL, 16); + uint16_t product_id = strtol(product_id_str, NULL, 16); + + if (vendor_id == idVendor && product_id == idProduct) { + const char *partition = udev_device_get_devnode(dev); + if (partition) { + // Extraer el nombre de la partición + const char *partition_name = strrchr(partition, '/'); + if (partition_name) { + printf("Partition: %s\n", partition_name + 1); + } else { + printf("Partition: %s\n", partition); + } + } else { + printf("Error: Failed to get partition.\n"); + } + break; + } + } + + udev_device_unref(dev); + } + + udev_enumerate_unref(enumerate); + udev_unref(udev); +} int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, libusb_hotplug_event event, void *user_data) { @@ -22,6 +75,10 @@ int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, if (LIBUSB_SUCCESS != rc) { printf("Could not open USB device\n"); } + + // Obtén la partición del dispositivo + get_partition(desc.idVendor, desc.idProduct); + } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) { if (dev_handle) { libusb_close(dev_handle);