gboolean battery_update(LibHalContext *ctx, const char *udi, int fd) { acpi_bst_t bst; DBusError error; HAL_DEBUG(("battery_update() enter")); dbus_error_init(&error); libhal_device_set_property_string(ctx, udi, "info.product", "Battery Bay", &error); my_dbus_error_free(&error); libhal_device_set_property_string(ctx, udi, "info.category", "battery", &error); bzero(&bst, sizeof (bst)); if (ioctl(fd, BATT_IOC_STATUS, &bst) < 0) { if (errno == ENXIO) { my_dbus_error_free(&error); libhal_device_set_property_bool(ctx, udi, "battery.present", FALSE, &error); } else { my_dbus_error_free(&error); return (FALSE); } } else { my_dbus_error_free(&error); libhal_device_set_property_bool(ctx, udi, "battery.present", TRUE, &error); } my_dbus_error_free(&error); if (!libhal_device_get_property_bool(ctx, udi, "battery.present", &error)) { HAL_DEBUG(("battery_update(): battery is NOT present")); battery_remove(ctx, udi); } else { HAL_DEBUG(("battery_update(): battery is present")); my_dbus_error_free(&error); libhal_device_set_property_string(ctx, udi, "battery.type", "primary", &error); my_dbus_error_free(&error); libhal_device_add_capability(ctx, udi, "battery", &error); my_dbus_error_free(&error); if (libhal_device_get_property_type(ctx, udi, "battery.vendor", &error) == LIBHAL_PROPERTY_TYPE_INVALID) { battery_static_update(ctx, udi, fd); } battery_dynamic_update(ctx, udi, fd); } my_dbus_error_free(&error); HAL_DEBUG(("battery_update() exit")); return (TRUE); }
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; }
int add_properties(LibHalContext *hal_ctx, new_dev_t *nd, lh_prop_t *prop) { DBusError error; lh_prop_t *p; char *udi2 = NULL, *udi3 = NULL, **s; LibHalPropertyType old_type; dbus_error_init(&error); for(p = prop; p; p = p->next) { if (!strcmp(p->key, "udi") && p->type == LIBHAL_PROPERTY_TYPE_STRING) { udi2 = p->v.str_value; continue; } old_type = libhal_device_get_property_type(hal_ctx, nd->real_udi, p->key, &error); dbus_error_init(&error); if (old_type != LIBHAL_PROPERTY_TYPE_INVALID && ( p->type != old_type || p->type == LIBHAL_PROPERTY_TYPE_STRLIST)) { if (!libhal_device_remove_property(hal_ctx, nd->real_udi, p->key, &error)) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 41; } } switch (p->type) { case LIBHAL_PROPERTY_TYPE_INVALID: break; case LIBHAL_PROPERTY_TYPE_BOOLEAN: if (!libhal_device_set_property_bool(hal_ctx, nd->real_udi, p->key, p->v.bool_value, &error)) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 42; } break; case LIBHAL_PROPERTY_TYPE_INT32: if (!libhal_device_set_property_int(hal_ctx, nd->real_udi, p->key, p->v.int_value, &error)) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 42; } break; case LIBHAL_PROPERTY_TYPE_UINT64: if (!libhal_device_set_property_uint64(hal_ctx, nd->real_udi, p->key, p->v.uint64_value, &error)) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 42; } break; case LIBHAL_PROPERTY_TYPE_DOUBLE: if (!libhal_device_set_property_double(hal_ctx, nd->real_udi, p->key, p->v.double_value, &error)) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 42; } break; case LIBHAL_PROPERTY_TYPE_STRING: if (!strcmp(p->key, "info.udi")) udi3 = p->v.str_value; if (!libhal_device_set_property_string(hal_ctx, nd->real_udi, p->key, p->v.str_value, &error)) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 42; } break; case LIBHAL_PROPERTY_TYPE_STRLIST: for(s = p->v.strlist_value; *s; s++) { if (!libhal_device_property_strlist_append(hal_ctx, nd->real_udi, p->key, *s, &error)) { fprintf(stderr, "%s: %s\n", error.name, error.message); LIBHAL_FREE_DBUS_ERROR (&error); return 42; } } break; } } if (udi2) udi3 = NULL; if (udi3) udi2 = udi3; if (udi2 && !nd->udi) nd->udi = strdup(udi2); return 0; }