static HFPCDROM * hfp_cdrom_new_real (boolean has_fd, int fd, const char *path, const char *parent) { HFPCDROM *cdrom = NULL; struct cam_device *cam; assert(path != NULL); assert(parent != NULL); /* cam_open_device() fails unless we use O_RDWR */ cam = cam_open_device(path, O_RDWR); if (cam) { cdrom = hfp_new0(HFPCDROM, 1); cdrom->cam = cam; cdrom->fd = -1; } else { #ifndef IOCATAREQUEST fd = open("/dev/ata", O_RDONLY); #else if (! has_fd) fd = open(path, O_RDONLY); #endif if (fd >= 0) { cdrom = hfp_new0(HFPCDROM, 1); cdrom->fd = fd; #ifndef IOCATAREQUEST cdrom->fd_owned = TRUE; cdrom->channel = libhal_device_get_property_int(hfp_ctx, parent, "ide.host", &hfp_error); dbus_error_free(&hfp_error); cdrom->device = libhal_device_get_property_int(hfp_ctx, parent, "ide.channel", &hfp_error); dbus_error_free(&hfp_error); #else cdrom->fd_owned = ! has_fd; cdrom->channel = -1; cdrom->device = -1; #endif } } return cdrom; }
static gboolean hal_get_ipod_usb_position (LibHalContext *ctx, const char *udi, int *usb_bus_number, int *usb_device_number) { char *parent_udi; char *subsystem; gboolean found_ids; DBusError error; parent_udi = NULL; subsystem = NULL; found_ids = FALSE; dbus_error_init (&error); while (TRUE) { parent_udi = libhal_device_get_property_string (ctx, udi, "info.parent", &error); if (parent_udi == NULL || dbus_error_is_set (&error)) goto end; udi = parent_udi; subsystem = libhal_device_get_property_string (ctx, udi, "linux.subsystem", &error); if (subsystem == NULL || dbus_error_is_set (&error)) { dbus_error_free (&error); dbus_error_init (&error); continue; } if (strcmp (subsystem, "usb") == 0) { *usb_bus_number = libhal_device_get_property_int (ctx, udi, "usb.bus_number", &error); if (dbus_error_is_set (&error)) { goto end; } *usb_device_number = libhal_device_get_property_int (ctx, udi, "usb.linux.device_number", &error); if (dbus_error_is_set (&error)) { goto end; } found_ids = TRUE; goto end; } } end: libhal_free_string (parent_udi); libhal_free_string (subsystem); if (dbus_error_is_set (&error)) { g_print ("Error: %s\n", error.message); dbus_error_free (&error); } return found_ids; }
static PropertyCacheItem* property_cache_item_get (const char *hal_device_udi) { PropertyCacheItem * pci = g_new0 (PropertyCacheItem,1); DBusError err; dbus_error_init (&err); pci->bus_no_present = libhal_device_property_exists (halctx, hal_device_udi, "usb_device.bus_number", &err); if (dbus_error_is_set (&err)) { HAL_ERROR (("Error: [%s]/[%s]", err.name, err.message)); dbus_error_free (&err); } if (pci->bus_no_present) pci->bus_no = libhal_device_get_property_int (halctx, hal_device_udi, "usb_device.bus_number", &err); LIBHAL_FREE_DBUS_ERROR (&err); pci->port_no_present = libhal_device_property_exists (halctx, hal_device_udi, "usb_device.linux.device_number", &err); LIBHAL_FREE_DBUS_ERROR (&err); if (pci->port_no_present) pci->port_no = libhal_device_get_property_int (halctx, hal_device_udi, "usb_device.linux.device_number", &err); LIBHAL_FREE_DBUS_ERROR (&err); pci->csr_is_dual_present = libhal_device_property_exists (halctx, hal_device_udi, "battery.csr.is_dual", &err); LIBHAL_FREE_DBUS_ERROR (&err); if (pci->csr_is_dual_present) pci->csr_is_dual = libhal_device_get_property_bool (halctx, hal_device_udi, "battery.csr.is_dual", &err); LIBHAL_FREE_DBUS_ERROR (&err); pci->current_charge_present = libhal_device_property_exists (halctx, hal_device_udi, "battery.charge_level.current", &err); LIBHAL_FREE_DBUS_ERROR (&err); if (pci->current_charge_present) pci->current_charge = libhal_device_get_property_int (halctx, hal_device_udi, "battery.charge_level.current", &err); LIBHAL_FREE_DBUS_ERROR (&err); return pci; }
static gboolean battery_update_property( LibHalContext *ctx, struct battery_info *battery, const char *key ) { DBusError error; gboolean ret; ret = TRUE; dbus_error_init( &error ); if( !strcmp( key, "battery.present" ) ) battery->status.present = libhal_device_get_property_bool( ctx, battery->udi, key, &error ); if( !strcmp( key, "battery.charge_level.current" ) ) battery->status.current_level = libhal_device_get_property_int( ctx, battery->udi, key, &error ); if( !strcmp( key, "battery.charge_level.rate" ) ) battery->status.rate = libhal_device_get_property_int( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.charge_level.design" ) ) battery->status.design_level = libhal_device_get_property_int( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.charge_level.last_full" ) ) battery->status.full_level = libhal_device_get_property_int( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.remaining_time" ) ) battery->status.remaining_time = libhal_device_get_property_int( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.rechargeable.is_charging" ) ) battery->status.charging = libhal_device_get_property_bool( ctx, battery->udi, key, &error ); else if( !strcmp( key, "battery.rechargeable.is_discharging" ) ) battery->status.discharging = libhal_device_get_property_bool( ctx, battery->udi, key, &error ); else ret = FALSE; dbus_error_free( &error ); return ret; }
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; }
static int get_int_prop(LibHalContext *ctxt, const char *udi, const char *prop, int *val_p) { DBusError err; int val; int rv; dbus_error_init(&err); val = libhal_device_get_property_int(ctxt, udi, prop, &err); rv = dbus_error_is_set(&err); dbus_error_free(&err); if (rv == 0) *val_p = val; return rv; }
/* Setting current brightness of the led */ static int set_leds_brightness (const char *udi, int level) { int fd, l, ret; char path[256]; char buf[5]; /* Assume we don't need more */ char *sysfs_path; DBusError error; ret = -1; if (!g_hash_table_lookup_extended (leds, udi, NULL, (gpointer) &sysfs_path)) { return -1; } dbus_error_init (&error); if (level > libhal_device_get_property_int (ctx, udi, "leds.num_levels", &error) -1) { HAL_DEBUG (("Requested level is higher than max level (check leds.num_levels).")); return -1; } snprintf (path, sizeof (path), "%s/brightness", sysfs_path); fd = open (path, O_WRONLY); if (fd < 0) { HAL_WARNING(("Could not open '%s' for '%s', error='%s'", sysfs_path, udi, strerror(errno))); goto out; } if ((l = snprintf (buf, 4, "%d", level)) < 4) { if (write (fd, buf, l) < 0) { HAL_WARNING(("Could not write '%s' to '%s'", buf , sysfs_path)); } else { /* everything okay */ ret = level; } } out: if (fd >= 0) close (fd); return ret; }
static void rb_mtp_plugin_maybe_add_source (RBMtpPlugin *plugin, const char *udi, LIBMTP_raw_device_t *raw_devices, int num_raw_devices) { int i; int device_num = 0; DBusError error; rb_debug ("checking if UDI %s matches an MTP device", udi); /* get device number */ dbus_error_init (&error); device_num = libhal_device_get_property_int (plugin->hal_context, udi, "usb.linux.device_number", &error); if (dbus_error_is_set (&error)) { rb_debug ("unable to get USB device number: %s", error.message); dbus_error_free (&error); return; } rb_debug ("USB device number: %d", device_num); for (i = 0; i < num_raw_devices; i++) { rb_debug ("detected MTP device: device number %d (bus location %u)", raw_devices[i].devnum, raw_devices[i].bus_location); if (raw_devices[i].devnum == device_num) { RBSource *source; RBShell *shell; rb_debug ("device matched, creating a source"); g_object_get (plugin, "object", &shell, NULL); source = RB_SOURCE (rb_mtp_source_new (shell, G_OBJECT (plugin), udi, &raw_devices[i])); rb_shell_append_display_page (shell, RB_DISPLAY_PAGE (source), RB_DISPLAY_PAGE_GROUP_DEVICES); plugin->mtp_sources = g_list_prepend (plugin->mtp_sources, source); g_signal_connect_object (source, "deleted", G_CALLBACK (source_deleted_cb), plugin, 0); g_object_unref (shell); } } }
signed int get_hal_int(const char *udi, const char *key, int optional) { int ret; DBusError error; if (!hal_ready()) return -1; dbus_error_init(&error); ret = libhal_device_get_property_int (hal_ctx, udi, key, &error); if (!dbus_error_is_set(&error)) { return ret; } else { if (!optional) { fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n", error.name, error.message); } dbus_error_free(&error); return -1; } }
std::map<int, std::pair<std::string, std::string> > SerialPortEnumerator::getPorts() { std::map<int, std::pair<std::string, std::string> > ports; #ifdef MACOSX // use IOKit to enumerates devices // get a matching dictionary to specify which IOService class we're interested in CFMutableDictionaryRef classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); if (classesToMatch == NULL) throw DashelException(DashelException::EnumerationError, 0, "IOServiceMatching returned a NULL dictionary"); // specify all types of serial devices CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes)); // get an iterator to serial port services io_iterator_t matchingServices; kern_return_t kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, &matchingServices); if (KERN_SUCCESS != kernResult) throw DashelException(DashelException::EnumerationError, kernResult, "IOServiceGetMatchingServices failed"); // iterate over services io_object_t modemService; int index = 0; while((modemService = IOIteratorNext(matchingServices))) { // get path for device CFTypeRef bsdPathAsCFString = IORegistryEntryCreateCFProperty(modemService, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0); if (bsdPathAsCFString) { std::string path; char cStr[255]; std::string name; bool res = CFStringGetCString((CFStringRef) bsdPathAsCFString, cStr, 255, kCFStringEncodingUTF8); if(res) path = cStr; else throw DashelException(DashelException::EnumerationError, 0, "CFStringGetCString failed"); CFRelease(bsdPathAsCFString); CFTypeRef fn = IORegistryEntrySearchCFProperty(modemService, kIOServicePlane, CFSTR("USB Product Name"), kCFAllocatorDefault, kIORegistryIterateRecursively | kIORegistryIterateParents); if(fn) { res = CFStringGetCString((CFStringRef) fn, cStr, 255, kCFStringEncodingUTF8); if(res) name = cStr; else throw DashelException(DashelException::EnumerationError, 0, "CFStringGetString failed"); CFRelease(fn); } else name = "Serial Port"; name = name + " (" + path + ")"; ports[index++] = std::make_pair<std::string, std::string>(path, name); } else throw DashelException(DashelException::EnumerationError, 0, "IORegistryEntryCreateCFProperty returned a NULL path"); // release service IOObjectRelease(modemService); } IOObjectRelease(matchingServices); #elif defined(USE_LIBUDEV) struct udev *udev; struct udev_enumerate *enumerate; struct udev_list_entry *devices, *dev_list_entry; struct udev_device *dev; int index = 0; udev = udev_new(); if(!udev) throw DashelException(DashelException::EnumerationError, 0, "Cannot create udev context"); enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "tty"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); udev_list_entry_foreach(dev_list_entry, devices) { const char *sysfs_path; struct udev_device *usb_dev; const char * path; struct stat st; unsigned int maj,min; /* Get sysfs path and create the udev device */ sysfs_path = udev_list_entry_get_name(dev_list_entry); dev = udev_device_new_from_syspath(udev, sysfs_path); // Some sanity check path = udev_device_get_devnode(dev); if(stat(path, &st)) throw DashelException(DashelException::EnumerationError, 0, "Cannot stat serial port"); if(!S_ISCHR(st.st_mode)) throw DashelException(DashelException::EnumerationError, 0, "Serial port is not character device"); // Get the major/minor number maj = major(st.st_rdev); min = minor(st.st_rdev); // Ignore all the non physical ports if(!(maj == 2 || (maj == 4 && min < 64) || maj == 3 || maj == 5)) { ostringstream oss; // Check if usb, if yes get the device name usb_dev = udev_device_get_parent_with_subsystem_devtype(dev,"usb","usb_device"); if(usb_dev) oss << udev_device_get_sysattr_value(usb_dev,"product"); else oss << "Serial Port"; oss << " (" << path << ")"; ports[index++] = std::make_pair<std::string, std::string>(path,oss.str()); } udev_device_unref(dev); } udev_enumerate_unref(enumerate); udev_unref(udev); #elif defined(USE_HAL) // use HAL to enumerates devices DBusConnection* dbusConnection = dbus_bus_get(DBUS_BUS_SYSTEM, 0); if (!dbusConnection) throw DashelException(DashelException::EnumerationError, 0, "cannot connect to D-BUS."); LibHalContext* halContext = libhal_ctx_new(); if (!halContext) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot create context"); if (!libhal_ctx_set_dbus_connection(halContext, dbusConnection)) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot connect to D-BUS"); if (!libhal_ctx_init(halContext, 0)) throw DashelException(DashelException::EnumerationError, 0, "cannot create HAL context: cannot init context"); int devicesCount; char** devices = libhal_find_device_by_capability(halContext, "serial", &devicesCount, 0); for (int i = 0; i < devicesCount; i++) { char* devFileName = libhal_device_get_property_string(halContext, devices[i], "serial.device", 0); char* info = libhal_device_get_property_string(halContext, devices[i], "info.product", 0); int port = libhal_device_get_property_int(halContext, devices[i], "serial.port", 0); ostringstream oss; oss << info << " " << port; ports[devicesCount - i] = std::make_pair<std::string, std::string>(devFileName, oss.str()); libhal_free_string(info); libhal_free_string(devFileName); } libhal_free_string_array(devices); libhal_ctx_shutdown(halContext, 0); libhal_ctx_free(halContext); #endif return ports; };
static void get_video_devices (ofGstCamData & cam_data) { int i, fd, ok; int num_udis = 0; char **udis; DBusError error; LibHalContext *hal_ctx; cam_data.num_webcam_devices = 0; g_print ("Probing devices with HAL...\n"); dbus_error_init (&error); hal_ctx = libhal_ctx_new (); if (hal_ctx == NULL) { g_warning ("Could not create libhal context"); dbus_error_free (&error); goto fallback; } if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { g_warning ("libhal_ctx_set_dbus_connection: %s: %s", error.name, error.message); dbus_error_free (&error); goto fallback; } if (!libhal_ctx_init (hal_ctx, &error)) { if (dbus_error_is_set (&error)) { g_warning ("libhal_ctx_init: %s: %s", error.name, error.message); dbus_error_free (&error); } g_warning ("Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready"); goto fallback; } udis = libhal_find_device_by_capability (hal_ctx, "video4linux", &num_udis, &error); if (dbus_error_is_set (&error)) { g_warning ("libhal_find_device_by_capability: %s: %s", error.name, error.message); dbus_error_free (&error); goto fallback; } /* Initialize webcam structures */ cam_data.webcam_devices = new ofGstDevice[num_udis]; for (i = 0; i < num_udis; i++) { char *device; char *parent_udi = NULL; char *subsystem = NULL; char *gstreamer_src, *product_name; struct v4l2_capability v2cap; struct video_capability v1cap; gint vendor_id = 0; gint product_id = 0; gchar *property_name = NULL; parent_udi = libhal_device_get_property_string (hal_ctx, udis[i], "info.parent", &error); if (dbus_error_is_set (&error)) { g_warning ("error getting parent for %s: %s: %s", udis[i], error.name, error.message); dbus_error_free (&error); } if (parent_udi != NULL) { subsystem = libhal_device_get_property_string (hal_ctx, parent_udi, "info.subsystem", NULL); if (subsystem == NULL) continue; property_name = g_strjoin (".", subsystem, "vendor_id", NULL); vendor_id = libhal_device_get_property_int (hal_ctx, parent_udi, property_name , &error); if (dbus_error_is_set (&error)) { g_warning ("error getting vendor id: %s: %s", error.name, error.message); dbus_error_free (&error); } g_free (property_name); property_name = g_strjoin (".", subsystem, "product_id", NULL); product_id = libhal_device_get_property_int (hal_ctx, parent_udi, property_name, &error); if (dbus_error_is_set (&error)) { g_warning ("error getting product id: %s: %s", error.name, error.message); dbus_error_free (&error); } g_free (property_name); libhal_free_string (subsystem); libhal_free_string (parent_udi); } g_print ("Found device %04x:%04x, getting capabilities...\n", vendor_id, product_id); device = libhal_device_get_property_string (hal_ctx, udis[i], "video4linux.device", &error); if (dbus_error_is_set (&error)) { g_warning ("error getting V4L device for %s: %s: %s", udis[i], error.name, error.message); dbus_error_free (&error); continue; } /* vbi devices support capture capability too, but cannot be used, * so detect them by device name */ if (strstr (device, "vbi")) { g_print ("Skipping vbi device: %s\n", device); libhal_free_string (device); continue; } if ((fd = open (device, O_RDONLY | O_NONBLOCK)) < 0) { g_warning ("Failed to open %s: %s", device, strerror (errno)); libhal_free_string (device); continue; } ok = ioctl (fd, VIDIOC_QUERYCAP, &v2cap); if (ok < 0) { ok = ioctl (fd, VIDIOCGCAP, &v1cap); if (ok < 0) { g_warning ("Error while probing v4l capabilities for %s: %s", device, strerror (errno)); libhal_free_string (device); close (fd); continue; } g_print ("Detected v4l device: %s\n", v1cap.name); g_print ("Device type: %d\n", v1cap.type); gstreamer_src = "v4lsrc"; product_name = v1cap.name; } else { guint cap = v2cap.capabilities; g_print ("Detected v4l2 device: %s\n", v2cap.card); g_print ("Driver: %s, version: %d\n", v2cap.driver, v2cap.version); /* g_print ("Bus info: %s\n", v2cap.bus_info); */ /* Doesn't seem anything useful */ g_print ("Capabilities: 0x%08X\n", v2cap.capabilities); if (!(cap & V4L2_CAP_VIDEO_CAPTURE)) { g_print ("Device %s seems to not have the capture capability, (radio tuner?)\n" "Removing it from device list.\n", device); libhal_free_string (device); close (fd); continue; } gstreamer_src = "v4l2src"; product_name = (char *) v2cap.card; } g_print ("\n"); cam_data.webcam_devices[cam_data.num_webcam_devices].hal_udi = g_strdup (udis[i]); cam_data.webcam_devices[cam_data.num_webcam_devices].video_device = g_strdup (device); cam_data.webcam_devices[cam_data.num_webcam_devices].gstreamer_src = g_strdup (gstreamer_src); cam_data.webcam_devices[cam_data.num_webcam_devices].product_name = g_strdup (product_name); cam_data.webcam_devices[cam_data.num_webcam_devices].num_video_formats = 0; cam_data.webcam_devices[cam_data.num_webcam_devices].supported_resolutions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); cam_data.num_webcam_devices++; libhal_free_string (device); close (fd); } libhal_free_string_array (udis); if (cam_data.num_webcam_devices == 0) { /* Create a fake device so that resolution changing stil works even if the * computer doesn't have a webcam. */ fallback: if (num_udis == 0) { cam_data.webcam_devices = new ofGstDevice; } cam_data.webcam_devices[0].num_video_formats = 0; cam_data.webcam_devices[0].hal_udi = g_strdup ("oF_fake_videodevice"); } cam_data.bInited=true; }
static void battery_dynamic_update(LibHalContext *ctx, const char *udi, int fd) { int reporting_rate; int reporting_current; int reporting_lastfull; int design_voltage; int present_voltage; char *reporting_unit; int remaining_time; int remaining_percentage; gboolean charging; gboolean discharging; acpi_bst_t bst; LibHalChangeSet *cs; DBusError error; static int counter = 0; HAL_DEBUG(("battery_dynamic_update() enter")); bzero(&bst, sizeof (bst)); if (ioctl(fd, BATT_IOC_STATUS, &bst) < 0) { return; } charging = bst.bst_state & BATT_BST_CHARGING ? TRUE : FALSE; discharging = bst.bst_state & BATT_BST_DISCHARGING ? TRUE : FALSE; /* No need to continue if battery is essentially idle. */ if (counter && !charging && !discharging) { return; } dbus_error_init(&error); libhal_device_set_property_bool(ctx, udi, "battery.is_rechargeable", TRUE, &error); my_dbus_error_free(&error); if (libhal_device_property_exists(ctx, udi, "battery.charge_level.percentage", &error)) { remaining_percentage = libhal_device_get_property_int(ctx, udi, "battery.charge_level.percentage", &error); if ((remaining_percentage == 100) && charging) { charging = FALSE; } } libhal_device_set_property_bool(ctx, udi, "battery.rechargeable.is_charging", charging, &error); my_dbus_error_free(&error); libhal_device_set_property_bool(ctx, udi, "battery.rechargeable.is_discharging", discharging, &error); my_dbus_error_free(&error); reporting_current = bst.bst_rem_cap; libhal_device_set_property_int(ctx, udi, "battery.reporting.current", bst.bst_rem_cap, &error); my_dbus_error_free(&error); reporting_rate = bst.bst_rate; libhal_device_set_property_int(ctx, udi, "battery.reporting.rate", bst.bst_rate, &error); my_dbus_error_free(&error); present_voltage = bst.bst_voltage; libhal_device_set_property_int(ctx, udi, "battery.voltage.present", bst.bst_voltage, &error); /* get all the data we know */ my_dbus_error_free(&error); reporting_unit = libhal_device_get_property_string(ctx, udi, "battery.reporting.unit", &error); my_dbus_error_free(&error); reporting_lastfull = libhal_device_get_property_int(ctx, udi, "battery.reporting.last_full", &error); /* * Convert mAh to mWh since util_compute_time_remaining() works * for mWh. */ if (reporting_unit && strcmp(reporting_unit, "mAh") == 0) { my_dbus_error_free(&error); design_voltage = libhal_device_get_property_int(ctx, udi, "battery.voltage.design", &error); /* * If the present_voltage is inaccurate, set it to the * design_voltage. */ if (((present_voltage * 10) < design_voltage) || (present_voltage <= 0) || (present_voltage > design_voltage)) { present_voltage = design_voltage; } reporting_rate = (reporting_rate * present_voltage) / 1000; reporting_lastfull = (reporting_lastfull * present_voltage) / 1000; reporting_current = (reporting_current * present_voltage) / 1000; } /* Make sure the current charge does not exceed the full charge */ if (reporting_current > reporting_lastfull) { reporting_current = reporting_lastfull; } if (!charging && !discharging) { counter++; reporting_rate = 0; } if ((cs = libhal_device_new_changeset(udi)) == NULL) { HAL_DEBUG(("Cannot allocate changeset")); libhal_free_string(reporting_unit); my_dbus_error_free(&error); return; } libhal_changeset_set_property_int(cs, "battery.charge_level.rate", reporting_rate); libhal_changeset_set_property_int(cs, "battery.charge_level.last_full", reporting_lastfull); libhal_changeset_set_property_int(cs, "battery.charge_level.current", reporting_current); remaining_percentage = util_compute_percentage_charge(udi, reporting_current, reporting_lastfull); remaining_time = util_compute_time_remaining(udi, reporting_rate, reporting_current, reporting_lastfull, discharging, charging, 0); /* * Some batteries give bad remaining_time estimates relative to * the charge level. */ if (charging && ((remaining_time < 30) || ((remaining_time < 300) && (remaining_percentage < 95)) || (remaining_percentage > 97))) { remaining_time = util_compute_time_remaining(udi, reporting_rate, reporting_current, reporting_lastfull, discharging, charging, 1); } if (remaining_percentage > 0) { libhal_changeset_set_property_int(cs, "battery.charge_level.percentage", remaining_percentage); } else { my_dbus_error_free(&error); libhal_device_remove_property(ctx, udi, "battery.charge_level.percentage", &error); } if ((remaining_percentage == 100) && charging) { battery_last_full(cs, fd); } /* * remaining_percentage is more accurate so we handle cases * where the remaining_time cannot be correct. */ if ((!charging && !discharging) || ((remaining_percentage == 100) && !discharging)) { remaining_time = 0; } if (remaining_time < 0) { my_dbus_error_free(&error); libhal_device_remove_property(ctx, udi, "battery.remaining_time", &error); } else if (remaining_time >= 0) { libhal_changeset_set_property_int(cs, "battery.remaining_time", remaining_time); } my_dbus_error_free(&error); libhal_device_commit_changeset(ctx, cs, &error); libhal_device_free_changeset(cs); libhal_free_string(reporting_unit); my_dbus_error_free(&error); HAL_DEBUG(("battery_dynamic_update() exit")); }
int main (int argc, char *argv[]) { DBusConnection *dbconn; DBusError dberr; LibHalContext *hal_ctx; LibHalPropertyType property_type; char *property_key; dbus_bool_t bool_value; int int_value; char **udis; int num_udis; int i; dbus_error_init (&dberr); dbconn = dbus_bus_get (DBUS_BUS_SYSTEM, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "Can't get D-Bus system bus!"); return EXIT_FAILURE; } hal_ctx = libhal_ctx_new (); if (hal_ctx == NULL) { fprintf (stderr, "Can't create a LibHalContext!"); return EXIT_FAILURE; } /* Associate HAL with the D-Bus connection we established */ libhal_ctx_set_dbus_connection (hal_ctx, dbconn); dbus_error_init (&dberr); libhal_ctx_init (hal_ctx, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "libhal_ctx_init() failed: '%s'. Is hald running?", dberr.message); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Looking for optical drives: storage.cdrom is the capability */ udis = libhal_find_device_by_capability (hal_ctx, "storage.cdrom", &num_udis, &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "libhal_find_device_by_capability error: '%s'\n", dberr.message); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } printf ("Found %d Optical Device(s)\n", num_udis); for (i = 0; i < num_udis; i++) { /* Ensure our properties are the expected type */ property_type = libhal_device_get_property_type (hal_ctx, udis[i], "storage.cdrom.dvd", &dberr); if (dbus_error_is_set (&dberr) || property_type != LIBHAL_PROPERTY_TYPE_BOOLEAN) { fprintf (stderr, "error checking storage.cdrom.dvd type"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } property_type = libhal_device_get_property_type (hal_ctx, udis[i], "storage.cdrom.read_speed", &dberr); if (dbus_error_is_set (&dberr) || property_type != LIBHAL_PROPERTY_TYPE_INT32) { fprintf (stderr, "error checking storage.cdrom.read_speed type"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Okay, now simply get property values */ bool_value = libhal_device_get_property_bool (hal_ctx, udis[i], "storage.cdrom.dvd", &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "error getting storage.cdrom.dvd"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } int_value = libhal_device_get_property_int (hal_ctx, udis[i], "storage.cdrom.read_speed", &dberr); if (dbus_error_is_set (&dberr)) { fprintf (stderr, "error getting storage.cdrom.dvd"); dbus_error_free (&dberr); libhal_ctx_free (hal_ctx); return EXIT_FAILURE; } /* Display the info we just got */ printf ("Device %s has a maximum read spead of %d kb/s and %s read DVDs.\n", udis[i], int_value, bool_value ? "can" : "cannot"); } return EXIT_SUCCESS; }
/** Entry point * * @param argc Number of arguments given to program * @param argv Arguments given to program * @return Return code */ int main (int argc, char *argv[]) { char *udi = NULL; char *key = NULL; int type; dbus_bool_t is_hex = FALSE; dbus_bool_t is_verbose = FALSE; dbus_bool_t is_version = FALSE; char *str; DBusError error; if (argc <= 1) { usage (argc, argv); return 1; } while (1) { int c; int option_index = 0; const char *opt; static struct option long_options[] = { {"udi", 1, NULL, 0}, {"key", 1, NULL, 0}, {"hex", 0, NULL, 0}, {"verbose", 0, NULL, 0}, {"version", 0, NULL, 0}, {"help", 0, NULL, 0}, {NULL, 0, NULL, 0} }; c = getopt_long (argc, argv, "", long_options, &option_index); if (c == -1) break; switch (c) { case 0: opt = long_options[option_index].name; if (strcmp (opt, "help") == 0) { usage (argc, argv); return 0; } else if (strcmp (opt, "hex") == 0) { is_hex = TRUE; } else if (strcmp (opt, "verbose") == 0) { is_verbose = TRUE; } else if (strcmp (opt, "version") == 0) { is_version = TRUE; } else if (strcmp (opt, "key") == 0) { key = strdup (optarg); } else if (strcmp (opt, "udi") == 0) { udi = strdup (optarg); } break; default: usage (argc, argv); return 1; break; } } if (is_version) { printf ("hal-get-property " PACKAGE_VERSION "\n"); return 0; } if (udi == NULL || key == NULL) { usage (argc, argv); return 1; } dbus_error_init (&error); if ((hal_ctx = libhal_ctx_new ()) == NULL) { fprintf (stderr, "error: libhal_ctx_new\n"); return 1; } if (!libhal_ctx_set_dbus_connection (hal_ctx, dbus_bus_get (DBUS_BUS_SYSTEM, &error))) { fprintf (stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } if (!libhal_ctx_init (hal_ctx, &error)) { if (dbus_error_is_set(&error)) { fprintf (stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); } fprintf (stderr, "Could not initialise connection to hald.\n" "Normally this means the HAL daemon (hald) is not running or not ready.\n"); return 1; } type = libhal_device_get_property_type (hal_ctx, udi, key, &error); if (type == LIBHAL_PROPERTY_TYPE_INVALID) { fprintf (stderr, "error: libhal_device_get_property_type: %s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 1; } /* emit the value to stdout */ switch (type) { case LIBHAL_PROPERTY_TYPE_STRING: str = libhal_device_get_property_string (hal_ctx, udi, key, &error); if (is_verbose) printf ("Type is string\n"); printf ("%s\n", str); libhal_free_string (str); break; case LIBHAL_PROPERTY_TYPE_INT32: if (is_verbose) printf ("Type is integer (shown in %s)\n", (is_hex ? "hexadecimal" : "decimal")); printf ((is_hex ? "%x\n" : "%d\n"), libhal_device_get_property_int (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_UINT64: if (is_verbose) printf ("Type is uint64 (shown in %s)\n", (is_hex ? "hexadecimal" : "decimal")); printf ((is_hex ? "%llx\n" : "%llu\n"), (long long unsigned int) libhal_device_get_property_uint64 (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_DOUBLE: if (is_verbose) printf ("Type is double\n"); printf ("%f\n", libhal_device_get_property_double (hal_ctx, udi, key, &error)); break; case LIBHAL_PROPERTY_TYPE_BOOLEAN: if (is_verbose) printf ("Type is boolean\n"); printf ("%s\n", libhal_device_get_property_bool (hal_ctx, udi, key, &error) ? "true" : "false"); break; case LIBHAL_PROPERTY_TYPE_STRLIST: { unsigned int i; char **strlist; if ((strlist = libhal_device_get_property_strlist (hal_ctx, udi, key, &error)) != NULL) { for (i = 0; strlist[i] != 0; i++) { printf ("%s", strlist[i]); if (strlist[i+1] != NULL) printf (" "); } } break; } default: printf ("Unknown type %d='%c'\n", type, type); return 1; break; } if (dbus_error_is_set (&error)) { fprintf (stderr, "error: %s: %s\n", error.name, error.message); dbus_error_free (&error); return 1; } return 0; }
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; }
static void get_device_info (RBGenericPlayerSource *source) { RBGenericPlayerSourcePrivate *priv = GENERIC_PLAYER_SOURCE_GET_PRIVATE (source); GMount *mount; GFile *is_audio_player; GError *error = NULL; #ifdef HAVE_HAL LibHalContext *ctx; #endif g_object_get (source, "mount", &mount, NULL); #ifdef HAVE_HAL ctx = get_hal_context (); if (ctx != NULL) { char *udi; udi = get_hal_udi_for_player (ctx, mount); if (udi != NULL) { DBusError error; char *prop; char **proplist; int value; /* get audio folders */ dbus_error_init (&error); proplist = libhal_device_get_property_strlist (ctx, udi, "portable_audio_player.audio_folders", &error); if (proplist) { if (!dbus_error_is_set (&error)) { priv->audio_folders = g_strdupv (proplist); } libhal_free_string_array (proplist); } free_dbus_error ("getting audio folder list", &error); /* get supported mime-types */ dbus_error_init (&error); proplist = libhal_device_get_property_strlist (ctx, udi, "portable_audio_player.output_formats", &error); if (proplist) { if (!dbus_error_is_set (&error)) { priv->output_mime_types = g_strdupv (proplist); } libhal_free_string_array (proplist); } free_dbus_error ("getting supported mime-type list", &error); /* get playlist format */ dbus_error_init (&error); proplist = libhal_device_get_property_strlist (ctx, udi, "portable_audio_player.playlist_format", &error); if (proplist) { if (!dbus_error_is_set (&error)) { set_playlist_formats (source, proplist); } libhal_free_string_array (proplist); } free_dbus_error ("getting playlist format", &error); /* get playlist path - in current hal-info packages, this is sometimes a string * and sometimes a strlist, so try both. * http://bugs.freedesktop.org/show_bug.cgi?id=19961 */ dbus_error_init (&error); proplist = libhal_device_get_property_strlist (ctx, udi, "portable_audio_player.playlist_path", &error); if (proplist && !dbus_error_is_set (&error)) { set_playlist_path (source, proplist[0]); libhal_free_string_array (proplist); } free_dbus_error ("getting playlist path (strlist)", &error); dbus_error_init (&error); prop = libhal_device_get_property_string (ctx, udi, "portable_audio_player.playlist_path", &error); if (prop && !dbus_error_is_set (&error)) { set_playlist_path (source, prop); libhal_free_string (prop); } free_dbus_error ("getting playlist path (string)", &error); /* get max folder depth */ dbus_error_init (&error); value = libhal_device_get_property_int (ctx, udi, "portable_audio_player.folder_depth", &error); if (!dbus_error_is_set (&error)) { priv->folder_depth = value; } free_dbus_error ("getting max folder depth", &error); debug_device_info (source, mount, "HAL"); } else { rb_debug ("no player info available (HAL doesn't recognise it as a player"); } g_free (udi); } cleanup_hal_context (ctx); #endif /* allow HAL info to be overridden with .is_audio_player file */ is_audio_player = get_is_audio_player_file (mount); if (is_audio_player != NULL) { char *data = NULL; gsize data_size = 0; rb_debug ("reading .is_audio_player file"); g_file_load_contents (is_audio_player, NULL, &data, &data_size, NULL, &error); if (error != NULL) { /* can we sensibly report this anywhere? */ rb_debug ("error reading .is_audio_player file: %s", error->message); g_clear_error (&error); } else { GKeyFile *keyfile; GError *error = NULL; char *munged; gsize munged_size; const char *fake_group = "[x-rb-data]\n"; char *group; /* prepend a group name to the file contents */ munged_size = data_size + strlen (fake_group); munged = g_malloc0 (munged_size + 1); strcpy (munged, fake_group); memcpy (munged + strlen (fake_group), data, data_size); keyfile = g_key_file_new (); g_key_file_set_list_separator (keyfile, ','); if (g_key_file_load_from_data (keyfile, munged, munged_size, G_KEY_FILE_NONE, &error) == FALSE) { /* and this */ rb_debug ("error loading .is_audio_player file: %s", error->message); g_error_free (error); } else { char *value; char **list; group = g_key_file_get_start_group (keyfile); list = g_key_file_get_string_list (keyfile, group, "audio_folders", NULL, NULL); if (list != NULL) { g_strfreev (priv->audio_folders); priv->audio_folders = list; } list = g_key_file_get_string_list (keyfile, group, "output_formats", NULL, NULL); if (list != NULL) { g_strfreev (priv->output_mime_types); priv->output_mime_types = list; } list = g_key_file_get_string_list (keyfile, group, "playlist_format", NULL, NULL); if (list != NULL) { set_playlist_formats (source, list); g_strfreev (list); } value = g_key_file_get_string (keyfile, group, "playlist_path", NULL); if (value != NULL) { set_playlist_path (source, value); g_free (value); } if (g_key_file_has_key (keyfile, group, "folder_depth", NULL)) { priv->folder_depth = g_key_file_get_integer (keyfile, group, "folder_depth", NULL); } g_free (group); } g_key_file_free (keyfile); g_free (munged); debug_device_info (source, mount, ".is_audio_player file"); } g_free (data); g_object_unref (is_audio_player); } else { rb_debug ("no .is_audio_player file found on this device"); } g_object_unref (mount); }
/* * gst_hal_get_alsa_element: * @ctx: a #LibHalContext which should be used for querying HAL. * @udi: a #gchar corresponding to the UDI you want to get. * @device_type: a #GstHalDeviceType specifying the wanted device type. * * Get Hal UDI @udi's string value. * * Returns: a newly allocated #gchar string containing the appropriate pipeline * for UDI @udi, or NULL in the case of an error.. */ static gchar * gst_hal_get_alsa_element (LibHalContext * ctx, const gchar * udi, GstHalDeviceType device_type) { char *type, *element = NULL, *string = NULL; DBusError error; dbus_error_init (&error); if (!libhal_device_query_capability (ctx, udi, "alsa", &error)) { if (dbus_error_is_set (&error)) { GST_DEBUG ("Failed querying %s for alsa capability: %s: %s", udi, error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); } else { GST_DEBUG ("UDI %s has no alsa capability", udi); } return NULL; } type = libhal_device_get_property_string (ctx, udi, "alsa.type", &error); if (dbus_error_is_set (&error)) { GST_DEBUG ("UDI %s has alsa capabilities but no alsa.type property: %s, %s", udi, error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return NULL; } else if (!type) { GST_DEBUG ("UDI %s has empty alsa.type property", udi); return NULL; } if (strcmp (type, "playback") == 0 && device_type == GST_HAL_AUDIOSINK) element = "alsasink"; else if (strcmp (type, "capture") == 0 && device_type == GST_HAL_AUDIOSRC) element = "alsasrc"; libhal_free_string (type); if (element) { int card, device; card = libhal_device_get_property_int (ctx, udi, "alsa.card", &error); if (dbus_error_is_set (&error)) { GST_DEBUG ("UDI %s has no alsa.card property: %s: %s", udi, error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return NULL; } else if (card == -1) { GST_DEBUG ("UDI %s has no alsa.card property", udi); return NULL; } device = libhal_device_get_property_int (ctx, udi, "alsa.device", &error); if (dbus_error_is_set (&error)) { GST_DEBUG ("UDI %s has no alsa.device property: %s: %s", udi, error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return NULL; } else if (device == -1) { GST_DEBUG ("UDI %s has no alsa.device property", udi); return NULL; } /* This is a bit dodgy, since it makes lots of assumptions about the way * alsa is set up. In any case, only munge the device string for playback */ if (strcmp (element, "alsasink") == 0 && device == 0) { /* handle default device specially to use * dmix, dsnoop, and softvol if appropriate */ string = g_strdup_printf ("%s device=default:%d", element, card); } else { string = g_strdup_printf ("%s device=plughw:%d,%d", element, card, device); } } return string; }
gboolean check_libhal (const char *server_addr) { pid_t child_pid; child_pid = fork (); if (child_pid == -1) { printf ("Cannot fork\n"); exit (1); } else if (child_pid == 0) { DBusError error; DBusConnection *conn; LibHalContext *ctx; dbus_bool_t passed; printf ("server address='%s'\n", server_addr); dbus_error_init (&error); if ((conn = dbus_connection_open (server_addr, &error)) == NULL) { printf ("Error connecting to server: %s\n", error.message); goto fail; } dbus_connection_setup_with_g_main (conn, NULL); if ((ctx = libhal_ctx_new ()) == NULL) { printf ("Error getting libhal context\n"); goto fail; } libhal_ctx_set_dbus_connection (ctx, conn); libhal_ctx_init (ctx, &error); if (dbus_error_is_set (&error)) { printf ("FAILED98: %s\n", error.message); goto fail; } printf ("SUCCESS98\n"); libhal_device_print (ctx, "/org/freedesktop/Hal/devices/testobj1", &error); if (dbus_error_is_set (&error)) { printf ("FAILED99: %s\n", error.message); goto fail; } printf ("SUCCESS99\n"); passed = FALSE; { char *val; val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string", &error); if (val == NULL || strcmp (val, "fooooobar22") != 0 || dbus_error_is_set (&error)) { libhal_free_string (val); printf ("FAILED100\n"); goto fail; } printf ("SUCCESS100\n"); libhal_free_string (val); } { char *val; val = libhal_device_get_property_string (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.string2", &error); if (val == NULL || strcmp (val, "fooøةמ") != 0 || dbus_error_is_set (&error)) { libhal_free_string (val); printf ("FAILED101: %s\n", error.message); goto fail; } libhal_free_string (val); printf ("SUCCESS101\n"); } { dbus_bool_t b; b = libhal_device_get_property_bool ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.bool", &error); if (!b || dbus_error_is_set (&error)) { printf ("FAILED102: %s, %i\n", error.message, b); goto fail; } printf ("SUCCESS102\n"); } { double val; double expected_val = 0.53434343; val = libhal_device_get_property_double (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.double", &error); if ( memcmp (&val, &expected_val, sizeof (double)) != 0 || dbus_error_is_set (&error)) { printf ("FAILED103\n"); goto fail; } printf ("SUCCESS103\n"); } if (libhal_device_get_property_uint64 ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.uint64", &error) != ((((dbus_uint64_t)1)<<35) + 5) || dbus_error_is_set (&error)) { printf ("FAILED104: %s\n", error.message); goto fail; } printf ("SUCCESS104\n"); { char **val; val = libhal_device_get_property_strlist (ctx, "/org/freedesktop/Hal/devices/testobj1", "test.strlist", &error); if (val == NULL || dbus_error_is_set (&error)) { libhal_free_string_array (val); printf ("FAILED105: %s\n", error.message); goto fail; } printf ("SUCCESS105\n"); if (libhal_string_array_length (val) != 2) { libhal_free_string_array (val); printf ("FAILED106\n"); goto fail; } printf ("SUCCESS106\n"); if (strcmp (val[0], "foostrlist2") != 0 || strcmp (val[1], "foostrlist3") != 0) { libhal_free_string_array (val); printf ("FAILED107\n"); goto fail; } printf ("SUCCESS107\n"); libhal_free_string_array (val); } if (libhal_device_get_property_int ( ctx, "/org/freedesktop/Hal/devices/testobj1", "test.int", &error) != 42 || dbus_error_is_set (&error)) { printf ("FAILED108\n"); goto fail; } printf ("SUCCESS108\n"); /* tests for libhal_psi */ { int type; char *key; LibHalPropertySet *pset; LibHalPropertySetIterator it; unsigned int psi_num_passed; unsigned int psi_num_elems; if ((pset = libhal_device_get_all_properties (ctx, "/org/freedesktop/Hal/devices/testobj1", &error)) == NULL) return FALSE; printf ("SUCCESS110\n"); psi_num_passed = 0; psi_num_elems = libhal_property_set_get_num_elems (pset); for (libhal_psi_init (&it, pset); libhal_psi_has_more (&it); libhal_psi_next (&it)) { type = libhal_psi_get_type (&it); key = libhal_psi_get_key (&it); switch (type) { case LIBHAL_PROPERTY_TYPE_STRING: if (strcmp (key, "info.udi") == 0) { if (strcmp (libhal_psi_get_string (&it), "/org/freedesktop/Hal/devices/testobj1") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } else if (strcmp (key, "test.string") == 0) { if (strcmp (libhal_psi_get_string (&it), "fooooobar22") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } else if (strcmp (key, "test.string2") == 0) { if (strcmp (libhal_psi_get_string (&it), "fooøةמ") == 0) { psi_num_passed++; } else { printf ("fail on %s\n", key); } } break; case LIBHAL_PROPERTY_TYPE_INT32: if (strcmp (key, "test.int") == 0) { if (libhal_psi_get_int (&it) == 42) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_UINT64: if (strcmp (key, "test.uint64") == 0) { if (libhal_psi_get_uint64 (&it) == ((((dbus_uint64_t)1)<<35) + 5)) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_BOOLEAN: if (strcmp (key, "test.bool") == 0) { if (libhal_psi_get_bool (&it) == TRUE) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_DOUBLE: if (strcmp (key, "test.double") == 0) { double val = 0.53434343; double val2; val2 = libhal_psi_get_double (&it); if (memcmp (&val, &val2, sizeof (double)) == 0) psi_num_passed++; else printf ("fail on %s\n", key); } break; case LIBHAL_PROPERTY_TYPE_STRLIST: if (strcmp (key, "test.strlist") == 0) { char **val; val = libhal_psi_get_strlist (&it); if (libhal_string_array_length (val) == 2 && strcmp (val[0], "foostrlist2") == 0 && strcmp (val[1], "foostrlist3") == 0 && val[2] == NULL) psi_num_passed++; else printf ("fail on %s\n", key); } break; default: printf (" *** unknown type for key %s\n", key); break; } } libhal_free_property_set (pset); if (psi_num_passed != psi_num_elems) { printf ("FAILED111\n"); goto fail; } printf ("SUCCESS111\n"); } /* end libhal_test_psi */ printf ("Passed all libhal tests\n"); passed = TRUE; fail: if (dbus_error_is_set (&error)) dbus_error_free (&error); send_tests_done (conn, passed); exit (1); } else { printf ("child pid=%d\n", child_pid); } return TRUE; }