non consigo chegar ó 'block'

This commit is contained in:
serxoz 2023-07-12 18:44:30 +02:00
parent 4662c0985b
commit f7e2fcddd7

140
mpfm.c
View File

@ -7,15 +7,13 @@
static int count = 0; static int count = 0;
void get_partition(uint16_t idVendor, uint16_t idProduct) { static struct udev_device* get_child(struct udev* udev, struct udev_device* parent, const char* subsystem)
struct udev *udev = udev_new(); {
if (!udev) { struct udev_device* child = NULL;
printf("Error: Failed to initialize udev.\n");
return;
}
struct udev_enumerate *enumerate = udev_enumerate_new(udev); struct udev_enumerate *enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "block");
udev_enumerate_add_match_parent(enumerate, parent);
udev_enumerate_add_match_subsystem(enumerate, subsystem);
udev_enumerate_scan_devices(enumerate); udev_enumerate_scan_devices(enumerate);
struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate); struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
@ -23,43 +21,99 @@ void get_partition(uint16_t idVendor, uint16_t idProduct) {
udev_list_entry_foreach(entry, devices) { udev_list_entry_foreach(entry, devices) {
const char *path = udev_list_entry_get_name(entry); const char *path = udev_list_entry_get_name(entry);
struct udev_device *dev = udev_device_new_from_syspath(udev, path); printf("%s\n", path); // DEBUG
child = udev_device_new_from_syspath(udev, path);
break;
}
// Obtén el idVendor e idProduct del dispositivo USB actual udev_enumerate_unref(enumerate);
const char *vendor_id_str = udev_device_get_sysattr_value(dev, "idVendor"); return child;
const char *product_id_str = udev_device_get_sysattr_value(dev, "idProduct"); }
printf(vendor_id_str);
if (vendor_id_str && product_id_str) { void get_partition(uint16_t idVendor, uint16_t idProduct)
uint16_t vendor_id = strtol(vendor_id_str, NULL, 16); {
uint16_t product_id = strtol(product_id_str, NULL, 16); struct udev *udev = udev_new();
if (!udev) {
printf("Error: Failed to initialize udev.\n");
return;
}
if (vendor_id == idVendor && product_id == idProduct) { struct udev_enumerate *enumerate = udev_enumerate_new(udev);
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_add_match_subsystem(enumerate, "scsi");
/* udev_enumerate_add_match_property(enumerate, "DEVTYPE", "scsi_target"); */
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);
printf("%s\n", path); // DEBUG
struct udev_device* scsi = udev_device_new_from_syspath(udev, path);
struct udev_device* block = get_child(udev, scsi, "block");
struct udev_device* scsi_disk = get_child(udev, scsi, "scsi_disk");
struct udev_device* usb
= udev_device_get_parent_with_subsystem_devtype(
scsi, "usb", "usb_device");
/* if (block && scsi_disk && usb) { */
printf("block = %s, usb = %s:%s, scsi = %s\n",
udev_device_get_devnode(block),
udev_device_get_sysattr_value(usb, "idVendor"),
udev_device_get_sysattr_value(usb, "idProduct"),
udev_device_get_sysattr_value(scsi, "vendor"));
printf(" %s\n %s\n",
udev_device_get_sysattr_value(usb,"manufacturer"),
udev_device_get_sysattr_value(usb,"product"));
printf(" serial: %s\n",
udev_device_get_sysattr_value(usb, "serial"));
/* } */
if (block)
udev_device_unref(block);
if (scsi_disk)
udev_device_unref(scsi_disk);
/* // 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"); */
/* 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(scsi);
} }
udev_enumerate_unref(enumerate); udev_enumerate_unref(enumerate);
udev_unref(udev); udev_unref(udev);
} }
int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, int
libusb_hotplug_event event, void *user_data) { hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
libusb_hotplug_event event, void *user_data)
{
static libusb_device_handle *dev_handle = NULL; static libusb_device_handle *dev_handle = NULL;
struct libusb_device_descriptor desc; struct libusb_device_descriptor desc;
int rc; int rc;
@ -67,14 +121,14 @@ int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
(void)libusb_get_device_descriptor(dev, &desc); (void)libusb_get_device_descriptor(dev, &desc);
printf("%04x:%04x (bus %d, device %d)\n", printf("%04x:%04x (bus %d, device %d)\n",
desc.idVendor, desc.idProduct, desc.idVendor, desc.idProduct,
libusb_get_bus_number(dev), libusb_get_device_address(dev)); libusb_get_bus_number(dev), libusb_get_device_address(dev));
if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) { if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
rc = libusb_open(dev, &dev_handle); /* rc = libusb_open(dev, &dev_handle); */
if (LIBUSB_SUCCESS != rc) { /* if (LIBUSB_SUCCESS != rc) { */
printf("Could not open USB device\n"); /* printf("Could not open USB device\n"); */
} /* } */
// Obtén la partición del dispositivo // Obtén la partición del dispositivo
get_partition(desc.idVendor, desc.idProduct); get_partition(desc.idVendor, desc.idProduct);
@ -92,7 +146,9 @@ int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev,
return 0; return 0;
} }
int main (void) { int
main (void)
{
libusb_hotplug_callback_handle callback_handle; libusb_hotplug_callback_handle callback_handle;
int rc; int rc;