gboolean ac_adapter_update(LibHalContext *ctx, const char *udi, int fd) { LibHalChangeSet *cs; DBusError error; HAL_DEBUG(("ac_adapter_update() enter")); dbus_error_init(&error); if (!libhal_device_query_capability(ctx, udi, "ac_adapter", &error)) { my_dbus_error_free(&error); libhal_device_add_capability(ctx, udi, "ac_adapter", &error); if ((cs = libhal_device_new_changeset(udi)) == NULL) { my_dbus_error_free(&error); return (FALSE); } libhal_changeset_set_property_string(cs, "info.product", "AC Adapter"); libhal_changeset_set_property_string(cs, "info.category", "ac_adapter"); my_dbus_error_free(&error); libhal_device_commit_changeset(ctx, cs, &error); libhal_device_free_changeset(cs); } ac_adapter_present(ctx, udi, fd); battery_update_all(ctx); my_dbus_error_free(&error); HAL_DEBUG(("ac_adapter_update() exit")); return (TRUE); }
static void ac_adapter_present(LibHalContext *ctx, const char *udi, int fd) { int pow; LibHalChangeSet *cs; DBusError error; HAL_DEBUG(("ac_adapter_present() enter")); if (ioctl(fd, BATT_IOC_POWER_STATUS, &pow) < 0) { return; } if ((cs = libhal_device_new_changeset(udi)) == NULL) { return; } if (pow > 0) { libhal_changeset_set_property_bool(cs, "ac_adapter.present", TRUE); } else { libhal_changeset_set_property_bool(cs, "ac_adapter.present", FALSE); } dbus_error_init(&error); libhal_device_commit_changeset(ctx, cs, &error); libhal_device_free_changeset(cs); my_dbus_error_free(&error); HAL_DEBUG(("ac_adapter_present() exit")); }
int main (int argc, char *argv[]) { int ret; char *udi; DBusError error; LibHalContext *hal_ctx = NULL; LibHalChangeSet *cs; gboolean can_suspend = FALSE; gboolean can_suspend_hybrid = FALSE; gboolean can_hibernate = FALSE; int exit_status; ret = 1; udi = getenv ("HAL_PROP_INFO_UDI"); if (udi == NULL) goto out; dbus_error_init (&error); if ((hal_ctx = libhal_ctx_init_direct (&error)) == NULL) { printf ("Cannot connect to hald\n"); if (dbus_error_is_set (&error)) { dbus_error_free (&error); } goto out; } cs = libhal_device_new_changeset (udi); if (cs == NULL) { printf ("Cannot initialize changeset\n"); goto out; } g_spawn_command_line_sync ("/usr/bin/pm-is-supported --suspend", NULL, NULL, &exit_status, NULL); can_suspend = (exit_status == 0); g_spawn_command_line_sync ("/usr/bin/pm-is-supported --suspend-hybrid", NULL, NULL, &exit_status, NULL); can_suspend_hybrid = (exit_status == 0); g_spawn_command_line_sync ("/usr/bin/pm-is-supported --hibernate", NULL, NULL, &exit_status, NULL); can_hibernate = (exit_status == 0); libhal_changeset_set_property_bool (cs, "power_management.can_suspend", can_suspend); libhal_changeset_set_property_bool (cs, "power_management.can_suspend_hybrid", can_suspend_hybrid); libhal_changeset_set_property_bool (cs, "power_management.can_hibernate", can_hibernate); libhal_device_commit_changeset (hal_ctx, cs, &error); libhal_device_free_changeset (cs); ret = 0; out: return ret; }
static void set_fstyp_properties (LibHalContext *ctx, const char *udi, const char *fstype, nvlist_t *fsattr) { char buf[256]; DBusError error; char *uuid = NULL; char *label_orig = NULL; char *label = NULL; int err; LibHalChangeSet *cs; dbus_error_init (&error); if ((cs = libhal_device_new_changeset (udi)) == NULL) { return; } libhal_changeset_set_property_string (cs, "volume.fsusage", "filesystem"); libhal_changeset_set_property_string (cs, "volume.fstype", fstype); /* label */ (void) nvlist_lookup_string(fsattr, "gen_volume_label", &label_orig); if (label_orig != NULL) { label = rtrim_copy(label_orig, 0); } /* Check if label is utf8 format */ if ((label != NULL) && (label[0] != '\0') && (u8_validate(label, strlen(label), (char **)NULL, U8_VALIDATE_ENTIRE, &err) != -1)) { libhal_changeset_set_property_string (cs, "volume.label", label); libhal_changeset_set_property_string (cs, "info.product", label); } else { libhal_changeset_set_property_string (cs, "volume.label", ""); snprintf (buf, sizeof (buf), "Volume (%s)", fstype); libhal_changeset_set_property_string (cs, "info.product", buf); } free(label); /* uuid */ if (nvlist_lookup_string(fsattr, "gen_uuid", &uuid) == 0) { libhal_changeset_set_property_string (cs, "volume.uuid", uuid); } else { libhal_changeset_set_property_string (cs, "volume.uuid", ""); } libhal_device_commit_changeset (ctx, cs, &error); libhal_device_free_changeset (cs); my_dbus_error_free (&error); }
static dbus_bool_t probe_disc (int fd, LibHalContext *ctx, const char *udi, dbus_bool_t *has_data, dbus_bool_t *has_audio) { DBusError error; disc_info_t di; int profile; dbus_bool_t is_blank, is_appendable, is_rewritable; char *disc_type = "cd_rom"; uint64_t capacity = 0; int i; LibHalChangeSet *cs; dbus_error_init (&error); if (get_disc_info (fd, &di)) { is_blank = (di.disc_status == 0); is_appendable = (di.disc_status == 1); is_rewritable = (di.erasable != 0); } else { is_blank = is_appendable = is_rewritable = FALSE; } if (get_current_profile (fd, &profile)) { switch (profile) { case 0x08: /* CD-ROM */ disc_type = "cd_rom"; break; case 0x09: /* CD-R */ disc_type = "cd_r"; break; case 0x0A: /* CD-RW */ disc_type = "cd_rw"; is_rewritable = TRUE; break; case 0x10: /* DVD-ROM */ disc_type = "dvd_rom"; break; case 0x11: /* DVD-R Sequential */ disc_type = "dvd_r"; break; case 0x12: /* DVD-RAM */ disc_type = "dvd_ram"; is_rewritable = TRUE; break; case 0x13: /* DVD-RW Restricted Overwrite */ disc_type = "dvd_rw"; is_rewritable = TRUE; break; case 0x14: /* DVD-RW Sequential */ disc_type = "dvd_rw"; is_rewritable = TRUE; break; case 0x1A: /* DVD+RW */ disc_type = "dvd_plus_rw"; is_rewritable = TRUE; break; case 0x1B: /* DVD+R */ disc_type = "dvd_plus_r"; break; case 0x2B: /* DVD+R Double Layer */ disc_type = "dvd_plus_r_dl"; break; case 0x40: /* BD-ROM */ disc_type = "bd_rom"; break; case 0x41: /* BD-R Sequential */ disc_type = "bd_r"; break; case 0x42: /* BD-R Random */ disc_type = "bd_r"; break; case 0x43: /* BD-RE */ disc_type = "bd_re"; is_rewritable = TRUE; break; case 0x50: /* HD DVD-ROM */ disc_type = "hddvd_rom"; break; case 0x51: /* HD DVD-R */ disc_type = "hddvd_r"; break; case 0x52: /* HD DVD-Rewritable */ disc_type = "hddvd_rw"; is_rewritable = TRUE; break; } (void) get_disc_capacity_for_profile(fd, profile, &capacity); } *has_audio = *has_data = FALSE; if (!is_blank) { uchar_t smalltoc[12]; size_t toc_size; uchar_t *toc, *p; /* * XXX for some reason CDROMREADTOCENTRY fails on video DVDs, * but extracting the toc directly works okay. */ if (!read_toc(fd, 0, 1, 4, smalltoc)) { HAL_DEBUG(("read_toc failed")); *has_data = B_TRUE; /* probe for fs anyway */ } else { toc_size = smalltoc[0] * 256 + smalltoc[1] + 2; toc = (uchar_t *)calloc(1, toc_size); if (toc == NULL || !read_toc(fd, 0, 1, toc_size, toc)) { HAL_DEBUG (("read_toc again failed")); } else { for (p = &toc[4]; p < (toc + toc_size); p += 8) { /* skip leadout */ if (p[2] == 0xAA) { continue; } if (p[1] & 4) { *has_data = B_TRUE; } else { *has_audio = B_TRUE; } } } free(toc); } } if ((cs = libhal_device_new_changeset (udi)) == NULL) { return (FALSE); } libhal_changeset_set_property_string (cs, "volume.disc.type", disc_type); libhal_changeset_set_property_bool (cs, "volume.disc.is_blank", is_blank); libhal_changeset_set_property_bool (cs, "volume.disc.has_audio", *has_audio); libhal_changeset_set_property_bool (cs, "volume.disc.has_data", *has_data); libhal_changeset_set_property_bool (cs, "volume.disc.is_appendable", is_appendable); libhal_changeset_set_property_bool (cs, "volume.disc.is_rewritable", is_rewritable); libhal_changeset_set_property_uint64 (cs, "volume.disc.capacity", capacity); libhal_changeset_set_property_bool (cs, "volume.disc.is_videodvd", FALSE); libhal_changeset_set_property_bool (cs, "volume.disc.is_vcd", FALSE); libhal_changeset_set_property_bool (cs, "volume.disc.is_svcd", FALSE); libhal_device_commit_changeset (ctx, cs, &error); libhal_device_free_changeset (cs); out: my_dbus_error_free (&error); return (TRUE); }
int main(int argc, char *argv[]) { int ret = 1; char *udi; char *printer_address, *community; DBusError error; LibHalContext *ctx = NULL; LibHalChangeSet *cs = NULL; char *manufacturer = NULL, *model = NULL, *serial_number = NULL, *description = NULL, **command_set = NULL, *device_uri = NULL; extern int snmp_printer_info(char *hostname, char *community, char **manufacturer, char **model, char **description, char **serial_number, char ***command_set, char **device_uri); dbus_error_init(&error); if ((udi = getenv("UDI")) == NULL) goto out; printer_address = getenv("HAL_PROP_NETWORK_DEVICE_ADDRESS"); if (printer_address == NULL) goto out; community = getenv("HAL_PROP_NETWORK_DEVICE_SNMP_COMMUNITY"); if (community == NULL) community = "public"; setup_logger(); dbus_error_init(&error); if ((ctx = libhal_ctx_init_direct(&error)) == NULL) goto out; if ((cs = libhal_device_new_changeset(udi)) == NULL) { HAL_DEBUG(("Cannot allocate changeset")); goto out; } /* Probe the printer for characteristics via SNMP */ ret = snmp_printer_info(printer_address, community, &manufacturer, &model, &description, &serial_number, &command_set, &device_uri); if (ret < 0) { HAL_DEBUG(("Cannot get snmp data for %s: %s", printer_address, strerror(errno))); goto out; } /* Add printer characteristics to the HAL device tree */ ret = add_printer_info(cs, udi, manufacturer, model, description, serial_number, command_set, device_uri); if (ret < 0) { HAL_DEBUG(("Cannot add printer data for %s to %s: %s", printer_address, udi, strerror(errno))); goto out; } libhal_device_commit_changeset(ctx, cs, &error); ret = 0; out: if (cs != NULL) { libhal_device_free_changeset(cs); } if (ctx != NULL) { if (dbus_error_is_set(&error)) { dbus_error_free(&error); } libhal_ctx_shutdown(ctx, &error); libhal_ctx_free(ctx); } return (ret); }
static gboolean battery_static_update(LibHalContext *ctx, const char *udi, int fd) { const char *technology; int reporting_design; int reporting_warning; int reporting_low; int reporting_gran1; int reporting_gran2; int voltage_design; char reporting_unit[10]; acpi_bif_t bif; LibHalChangeSet *cs; DBusError error; HAL_DEBUG(("battery_static_update() enter")); bzero(&bif, sizeof (bif)); if (ioctl(fd, BATT_IOC_INFO, &bif) < 0) { return (FALSE); } if ((cs = libhal_device_new_changeset(udi)) == NULL) { HAL_DEBUG(("Cannot allocate changeset")); return (FALSE); } libhal_changeset_set_property_string(cs, "battery.vendor", bif.bif_oem_info); technology = bif.bif_type; if (technology != NULL) { libhal_changeset_set_property_string(cs, "battery.reporting.technology", technology); libhal_changeset_set_property_string(cs, "battery.technology", util_get_battery_technology(technology)); } libhal_changeset_set_property_string(cs, "battery.serial", bif.bif_serial); libhal_changeset_set_property_string(cs, "battery.model", bif.bif_model); if (bif.bif_unit) { libhal_changeset_set_property_string(cs, "battery.reporting.unit", "mAh"); strlcpy(reporting_unit, "mAh", sizeof (reporting_unit)); } else { libhal_changeset_set_property_string(cs, "battery.reporting.unit", "mWh"); strlcpy(reporting_unit, "mWh", sizeof (reporting_unit)); } libhal_changeset_set_property_int(cs, "battery.reporting.last_full", bif.bif_last_cap); libhal_changeset_set_property_int(cs, "battery.reporting.design", bif.bif_design_cap); reporting_design = bif.bif_design_cap; libhal_changeset_set_property_int(cs, "battery.reporting.warning", bif.bif_warn_cap); reporting_warning = bif.bif_warn_cap; libhal_changeset_set_property_int(cs, "battery.reporting.low", bif.bif_low_cap); reporting_low = bif.bif_low_cap; libhal_changeset_set_property_int(cs, "battery.reporting.granularity_1", bif.bif_gran1_cap); reporting_gran1 = bif.bif_gran1_cap; libhal_changeset_set_property_int(cs, "battery.reporting.granularity_2", bif.bif_gran2_cap); reporting_gran2 = bif.bif_gran2_cap; libhal_changeset_set_property_int(cs, "battery.voltage.design", bif.bif_voltage); voltage_design = bif.bif_voltage; if (reporting_unit && strcmp(reporting_unit, "mAh") == 0) { /* convert to mWh */ libhal_changeset_set_property_string(cs, "battery.charge_level.unit", "mWh"); libhal_changeset_set_property_int(cs, "battery.charge_level.design", (reporting_design * voltage_design) / 1000); libhal_changeset_set_property_int(cs, "battery.charge_level.warning", (reporting_warning * voltage_design) / 1000); libhal_changeset_set_property_int(cs, "battery.charge_level.low", (reporting_low * voltage_design) / 1000); libhal_changeset_set_property_int(cs, "battery.charge_level.granularity_1", (reporting_gran1 * voltage_design) / 1000); libhal_changeset_set_property_int(cs, "battery.charge_level.granularity_2", (reporting_gran2 * voltage_design) / 1000); } else { if (reporting_unit && strcmp(reporting_unit, "mWh") == 0) { libhal_changeset_set_property_string(cs, "battery.charge_level.unit", "mWh"); } libhal_changeset_set_property_int(cs, "battery.charge_level.design", reporting_design); libhal_changeset_set_property_int(cs, "battery.charge_level.warning", reporting_warning); libhal_changeset_set_property_int(cs, "battery.charge_level.low", reporting_low); libhal_changeset_set_property_int(cs, "battery.charge_level.granularity_1", reporting_gran1); libhal_changeset_set_property_int(cs, "battery.charge_level.granularity_2", reporting_gran2); } dbus_error_init(&error); libhal_device_commit_changeset(ctx, cs, &error); libhal_device_free_changeset(cs); my_dbus_error_free(&error); HAL_DEBUG(("battery_static_update() exit")); return (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")); }