1N/A/***************************************************************************
1N/A * CVSID: $Id$
1N/A *
1N/A * utili_pm.c - Various Powermanagement related utilities
1N/A *
1N/A * Copyright (C) 2005 Richard Hughes <richard@hughsie.com>
1N/A * Copyright (C) 2005 Danny Kukawka <danny.kukawka@web.de>
1N/A *
1N/A * Licensed under the Academic Free License version 2.1
1N/A *
1N/A * This program is free software; you can redistribute it and/or modify
1N/A * it under the terms of the GNU General Public License as published by
1N/A * the Free Software Foundation; either version 2 of the License, or
1N/A * (at your option) any later version.
1N/A *
1N/A * This program is distributed in the hope that it will be useful,
1N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
1N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1N/A * GNU General Public License for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program; if not, write to the Free Software
1N/A * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1N/A *
1N/A **************************************************************************/
1N/A
1N/A#include <stdio.h>
1N/A#include <string.h>
1N/A#include <time.h>
1N/A#include <ctype.h>
1N/A#include <stdint.h>
1N/A
1N/A#include <glib.h>
1N/A
1N/A#include "logger.h"
1N/A
1N/A#include "util_pm.h"
1N/A
1N/Atypedef struct {
1N/A int last_level;
1N/A int last_chargeRate;
1N/A time_t last_time;
1N/A} batteryInfo;
1N/A
1N/AGHashTable *saved_battery_info = NULL;
1N/A
1N/A/** Convert the hardware reported value into a few sane choices
1N/A *
1N/A * This is needed as ACPI does not specify the description text for a
1N/A * battery, and so we have to calculate it from the hardware output
1N/A *
1N/A * @param type The battery type recieved from the hardware
1N/A * @return The battery technology which is one of:
1N/A * unknown, lithium-ion or lead-acid
1N/A */
1N/Aconst char *
1N/Autil_get_battery_technology (const char *type)
1N/A{
1N/A if (type == NULL) {
1N/A return "unknown";
1N/A }
1N/A /* every case combination of Li-Ion is commonly used.. */
1N/A if (strcasecmp (type, "li-ion") == 0 ||
1N/A strcasecmp (type, "lion") == 0) {
1N/A return "lithium-ion";
1N/A }
1N/A if (strcasecmp (type, "pb") == 0 ||
1N/A strcasecmp (type, "pbac") == 0) {
1N/A return "lead-acid";
1N/A }
1N/A if (strcasecmp (type, "lip") == 0) {
1N/A return "lithium-polymer";
1N/A }
1N/A if (strcasecmp (type, "nimh") == 0) {
1N/A return "nickel-metal-hydride";
1N/A }
1N/A return "unknown";
1N/A}
1N/A
1N/A/** Given all the required parameters, this function will return the percentage
1N/A * charge remaining. There are lots of checks here as ACPI is often broken.
1N/A *
1N/A * @param id Optional ID given to this battery. Unused at present.
1N/A * @param chargeLevel The current charge level of the battery (typically mWh)
1N/A * @param chargeLastFull The last "full" charge of the battery (typically mWh)
1N/A * @return Percentage, -1 if invalid
1N/A */
1N/Aint
1N/Autil_compute_percentage_charge (const char *id,
1N/A int chargeLevel,
1N/A int chargeLastFull)
1N/A{
1N/A int percentage;
1N/A /* make sure we have chargelevel */
1N/A if (chargeLevel <= 0) {
1N/A HAL_WARNING (("chargeLevel %i, returning -1!", chargeLevel));
1N/A return -1;
1N/A }
1N/A /* make sure not division by zero */
1N/A if (chargeLastFull > 0)
1N/A percentage = ((double) chargeLevel / (double) chargeLastFull) * 100;
1N/A else {
1N/A HAL_WARNING (("chargeLastFull %i, percentage returning -1!", chargeLastFull));
1N/A return -1;
1N/A }
1N/A /* Some bios's will report this higher than 100, limit it here */
1N/A if (percentage > 100) {
1N/A HAL_WARNING (("Percentage %i, returning 100!", percentage));
1N/A return 100;
1N/A }
1N/A /* Something really isn't right if we get a negative... */
1N/A if (percentage < 0) {
1N/A HAL_WARNING (("Percentage %i, returning -1!", percentage));
1N/A return -1;
1N/A }
1N/A return percentage;
1N/A}
1N/A
1N/A/** Given all the required parameters, this function will return the number
1N/A * of seconds until the battery is charged (if charging) or the number
1N/A * of seconds until empty (if discharging)
1N/A *
1N/A * @param id Optional ID given to this battery. Unused at present.
1N/A * @param chargeRate The "rate" (typically mW)
1N/A * @param chargeLevel The current charge level of the battery (typically mWh)
1N/A * @param chargeLastFull The last "full" charge of the battery (typically mWh)
1N/A * @param isDischarging If battery is discharging
1N/A * @param isCharging If battery is charging
1N/A * @param guessChargeRate If ignore chargeRate and guess them.
1N/A * @return Number of seconds, or -1 if invalid
1N/A */
1N/Aint
1N/Autil_compute_time_remaining (const char *id,
1N/A int chargeRate,
1N/A int chargeLevel,
1N/A int chargeLastFull,
1N/A gboolean isDischarging,
1N/A gboolean isCharging,
1N/A gboolean guessChargeRate)
1N/A{
1N/A int remaining_time = 0;
1N/A
1N/A /* should not get negative values */
1N/A if (chargeRate < 0 || chargeLevel < 0 || chargeLastFull < 0) {
1N/A HAL_WARNING (("chargeRate, chargeLevel or chargeLastFull < 0, returning -1"));
1N/A return -1;
1N/A }
1N/A /* batteries cannot charge and discharge at the same time */
1N/A if (isDischarging && isCharging) {
1N/A HAL_WARNING (("isDischarging & isCharging TRUE, returning -1"));
1N/A return -1;
1N/A }
1N/A /*
1N/A * Some laptops don't supply any rate info, but that's no reason for HAL not
1N/A * to. We use the current and previous chargeLevel to estimate the rate.
1N/A * The info is stored in a GHashTable because there could be more than one battery.
1N/A */
1N/A if (chargeRate == 0 || guessChargeRate) {
1N/A batteryInfo *battery_info;
1N/A time_t cur_time = time(NULL);
1N/A
1N/A /* Initialize the save_battery_info GHashTable */
1N/A if (!saved_battery_info)
1N/A saved_battery_info = g_hash_table_new(g_str_hash, g_str_equal);
1N/A
1N/A if ((battery_info = g_hash_table_lookup(saved_battery_info, id))) {
1N/A /* check this to prevent division by zero */
1N/A if ((cur_time == battery_info->last_time) || (chargeLevel == battery_info->last_level)) {
1N/A /* if we can't calculate because nothing changed, use last
1N/A * chargeRate to prevent removing battery.remaining_time.
1N/A */
1N/A chargeRate = battery_info->last_chargeRate;
1N/A } else {
1N/A chargeRate = ((chargeLevel - battery_info->last_level) * 60 * 60) / (cur_time - battery_info->last_time);
1N/A /*
1N/A * During discharging chargeRate would be negative, which would
1N/A * mess up the the calculation below, so we make sure it's always
1N/A * positive.
1N/A */
1N/A chargeRate = (chargeRate > 0) ? chargeRate : -chargeRate;
1N/A
1N/A battery_info->last_level = chargeLevel;
1N/A battery_info->last_time = cur_time;
1N/A battery_info->last_chargeRate = chargeRate;
1N/A }
1N/A } else {
1N/A battery_info = g_new0(batteryInfo, 1);
1N/A g_hash_table_insert(saved_battery_info, (char*) id, battery_info);
1N/A
1N/A battery_info->last_level = chargeLevel;
1N/A battery_info->last_time = cur_time;
1N/A battery_info->last_chargeRate = 0;
1N/A return -1;
1N/A }
1N/A }
1N/A
1N/A if (chargeRate == 0)
1N/A return -1;
1N/A
1N/A if (isDischarging) {
1N/A remaining_time = ((double) chargeLevel / (double) chargeRate) * 60 * 60;
1N/A } else if (isCharging) {
1N/A /*
1N/A * Some ACPI BIOS's don't update chargeLastFull,
1N/A * so return 0 as we don't know how much more there is left
1N/A */
1N/A if (chargeLevel > chargeLastFull ) {
1N/A HAL_WARNING (("chargeLevel > chargeLastFull, returning -1"));
1N/A return -1;
1N/A }
1N/A remaining_time = ((double) (chargeLastFull - chargeLevel) / (double) chargeRate) * 60 * 60;
1N/A }
1N/A
1N/A /* This shouldn't happen, but check for completeness */
1N/A if (remaining_time < 0) {
1N/A HAL_WARNING (("remaining_time %i, returning -1", remaining_time));
1N/A remaining_time = -1;
1N/A }
1N/A /* Battery life cannot be above 60 hours */
1N/A else if (remaining_time > 60*60*60) {
1N/A HAL_WARNING (("remaining_time *very* high, returning -1"));
1N/A remaining_time = -1;
1N/A }
1N/A
1N/A return remaining_time;
1N/A}
1N/A