static int rsct_usbdev_scan_serial(char **devices, int i_devices, rsct_usbdev_t **usbdev_list) { int i; for (i=0; i<i_devices; i++) { const char *udi=devices[i]; if (libhal_device_exists(global_hal_context->ctx, udi, &(global_hal_context->dbus_error))) { char *busType; busType=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.subsystem", NULL); if (busType==NULL) busType=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.bus", NULL); if (busType) { if (strcasecmp(busType, "tty")==0) { char *parent_udi; /* ttyUSB device, get USB info from parent */ parent_udi=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.parent", NULL); if (parent_udi) { rsct_usbdev_t *d; char *path; /* find device entry for parent */ d=rsct_usbdev_list_findByUDI(*usbdev_list, parent_udi); if (d) { int port; port=libhal_device_get_property_int(global_hal_context->ctx, udi, "serial.port", NULL); d->port=port; path=libhal_device_get_property_string(global_hal_context->ctx, udi, "serial.device", NULL); if (path) { strncpy(d->deviceNodePath, path, sizeof(d->deviceNodePath)-1); d->deviceNodePath[sizeof(d->deviceNodePath)-1]=0; libhal_free_string(path); } } libhal_free_string(parent_udi); } else { fprintf(stderr, "RSCT: Parent for serial device not found\n"); } } /* if tty */ libhal_free_string(busType); } /* if bus type */ } /* if device exists */ } /* for */ return 0; }
int add_network_printer(LibHalContext *ctx, char *base, char *hostaddr, char *device, char *community) { DBusError error; int rc = -1; char udi[128]; char *tmp_udi = NULL; static char *parent = NULL; char *manufacturer = NULL, *model = NULL, *description = NULL, *uri = NULL, *sn, *serial; sn = serial = pseudo_serialno_from_addr(hostaddr); if (parent == NULL) parent = getenv("UDI"); dbus_error_init(&error); network_device_name_to_udi(udi, sizeof (udi), base, serial, NULL); if (libhal_device_exists(ctx, udi, &error) == TRUE) goto out; if ((tmp_udi = libhal_new_device(ctx, &error)) == NULL) goto out; snmp_printer_info(hostaddr, community, &manufacturer, &model, &description, &serial, NULL, &uri); libhal_device_set_property_string(ctx, tmp_udi, "info.parent", parent, &error); libhal_device_set_property_string(ctx, tmp_udi, "info.category", "printer", &error); libhal_device_property_strlist_append(ctx, tmp_udi, "info.capabilities", "printer", &error); libhal_device_property_strlist_append(ctx, tmp_udi, "info.capabilities", "network_device", &error); libhal_device_set_property_string(ctx, tmp_udi, "network_device.address", hostaddr, &error); if ((community != NULL) && (strcasecmp(community, "public") != 0)) libhal_device_set_property_string(ctx, tmp_udi, "network_device.snmp_community", community, &error); if ((uri != NULL) || (device != NULL)) libhal_device_set_property_string(ctx, tmp_udi, "printer.device", (uri ? uri : device), &error); if (serial != NULL) libhal_device_set_property_string(ctx, tmp_udi, "printer.serial", serial, &error); if (manufacturer != NULL) libhal_device_set_property_string(ctx, tmp_udi, "printer.vendor", manufacturer, &error); if (model != NULL) libhal_device_set_property_string(ctx, tmp_udi, "printer.product", model, &error); if (description != NULL) libhal_device_set_property_string(ctx, tmp_udi, "printer.description", description, &error); /* commit the changes to the new UDI */ rc = libhal_device_commit_to_gdl(ctx, tmp_udi, udi, &error); out: HAL_DEBUG(("result: %s (%s): %s, %s, %s, %s, %s", hostaddr, udi, NP(manufacturer), NP(model), NP(description), NP(serial), NP(uri))); if (tmp_udi != NULL) free(tmp_udi); if (manufacturer != NULL) free(manufacturer); if (model != NULL) free(model); if (description != NULL) free(description); if (uri != NULL) free(uri); if (sn != NULL) free(sn); if (dbus_error_is_set(&error)) { HAL_WARNING(("%s: %s", error.name, error.message)); dbus_error_free(&error); } HAL_DEBUG(("add: %s (%s)", hostaddr, udi)); return (rc); }
static int rsct_usbdev_scan_nonserial(char **devices, int i_devices, rsct_usbdev_t **usbdev_list) { int i; for (i=0; i<i_devices; i++) { const char *udi=devices[i]; if (libhal_device_exists(global_hal_context->ctx, udi, &(global_hal_context->dbus_error))) { char *busType; busType=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.subsystem", NULL); if (busType && (strcasecmp(busType, "usb")!=0)) { libhal_free_string(busType); busType=NULL; /* non-USB devices are handled below */ } if (busType==NULL) busType=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.bus", NULL); if (busType) { if (strcasecmp(busType, "usb")==0) { /* USB device, look for LibUSB info */ if (libhal_device_property_exists(global_hal_context->ctx, udi, "usb.bus_number", NULL) && libhal_device_property_exists(global_hal_context->ctx, udi, "usb.linux.device_number", NULL)){ int busId; int busPos; int vendorId; int productId; char pbuff[256]; struct stat st; int havePath=0; busId=libhal_device_get_property_int(global_hal_context->ctx, udi, "usb.bus_number", NULL); busPos=libhal_device_get_property_int(global_hal_context->ctx, udi, "usb.linux.device_number", NULL); vendorId=libhal_device_get_property_int(global_hal_context->ctx, udi, "usb.vendor_id", NULL); productId=libhal_device_get_property_int(global_hal_context->ctx, udi, "usb.product_id", NULL); if (vendorId==0xc4b && rsct_usbdev_list_findByBus(*usbdev_list, busId, busPos)==NULL) { rsct_usbdev_t *d; char *serial=NULL; char *productName=NULL; d=rsct_usbdev_new(); d->busId=busId; d->busPos=busPos; d->vendorId=vendorId; d->productId=productId; snprintf(d->halPath, sizeof(d->halPath)-1, "usb:%04x/%04x:libhal:%s", d->vendorId, d->productId, udi); d->halPath[sizeof(d->halPath)-1]=0; /* set HAL UDI */ strncpy(d->halUDI, udi, sizeof(d->halUDI)-1); d->halUDI[sizeof(d->halUDI)-1]=0; /* determine path for LibUSB */ snprintf(pbuff, sizeof(pbuff)-1, "/dev/bus/usb/%03d/%03d", busId, busPos); pbuff[sizeof(pbuff)-1]=0; if (stat(pbuff, &st)==0) { havePath=1; } else { snprintf(pbuff, sizeof(pbuff)-1, "/proc/bus/usb/%03d/%03d", busId, busPos); pbuff[sizeof(pbuff)-1]=0; if (stat(pbuff, &st)==0) { havePath=1; } } if (havePath) { strncpy(d->usbPath, pbuff, sizeof(d->usbPath)-1); d->usbPath[sizeof(d->usbPath)-1]=0; strncpy(d->deviceNodePath, pbuff, sizeof(d->deviceNodePath)-1); d->deviceNodePath[sizeof(d->deviceNodePath)-1]=0; } /* generate path for CTAPI/IFD */ snprintf(d->path, sizeof(d->path)-1, "usb:%04x/%04x:libusb:%03d:%03d", d->vendorId, d->productId, d->busId, d->busPos); serial=libhal_device_get_property_string(global_hal_context->ctx, udi, "usb.serial", NULL); if (serial) { strncpy(d->serial, serial, sizeof(d->serial)-1); d->serial[sizeof(d->serial)-1]=0; libhal_free_string(serial); } /* get product name from parent. * Please note: This udi refers to the *_ifX HAL device, and it's product name * is the product name of the interface ("USB Vendor Specific Interface") rather than * that of the device itself. * Therefore we must access the parent in order to find the product name of the device. */ if (1) { char *parent_udi; /* ttyUSB device, get USB info from parent */ parent_udi=libhal_device_get_property_string(global_hal_context->ctx, udi, "info.parent", NULL); if (parent_udi) { productName=libhal_device_get_property_string(global_hal_context->ctx, parent_udi, "usb_device.product", NULL); libhal_free_string(parent_udi); } } if (productName) { strncpy(d->productName, productName, sizeof(d->productName)-1); d->productName[sizeof(d->productName)-1]=0; libhal_free_string(productName); } /* all set, add device */ rsct_usbdev_list_add(usbdev_list, d); } } } /* if USB */ libhal_free_string(busType); } /* if bus type */ } /* if device exists */ } /* for */ return 0; }
int add_udi(LibHalContext *hal_ctx, char *arg) { DBusError error; dbus_bool_t dev_exists = FALSE; char *udi = NULL, buf[1024]; lh_prop_t *prop; int err; if (!arg) return 21; if (*arg == '/') { udi = arg; } else { #ifdef HAVE_ASPRINTF asprintf(&udi, "/org/freedesktop/Hal/devices/%s", arg); #else udi = calloc(1, sizeof ("/org/freedesktop/Hal/devices/%s") + strlen(arg)); sprintf(udi, "/org/freedesktop/Hal/devices/%s", arg); #endif } if (udi) new_dev.udi = strdup(udi); dbus_error_init(&error); if (udi) dev_exists = libhal_device_exists(hal_ctx, udi, &error); if (dev_exists) { new_dev.real_udi = strdup(new_dev.udi); } else { new_dev.real_udi = libhal_new_device(hal_ctx, &error); if (!new_dev.real_udi) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); free(new_dev.real_udi); return 22; } printf("tmp udi: %s\n", new_dev.real_udi); } prop = NULL; while (fgets(buf, sizeof buf, stdin)) { process_property(hal_ctx, buf, &prop); } err = add_properties(hal_ctx, &new_dev, prop); prop = free_properties(prop); if (!dev_exists) { if (!libhal_device_commit_to_gdl(hal_ctx, new_dev.real_udi, new_dev.udi, &error)) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); free(new_dev.real_udi); err = err ? err : 23; } } printf("%s: %s\n", dev_exists ? "merged": "created", new_dev.udi); return err; }