diff options
author | Richard Hughes <richard@hughsie.com> | 2008-08-01 17:27:12 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2008-08-01 17:27:12 +0100 |
commit | 74683e3fa712d7f4f2b2474cc4c045020ad2bafb (patch) | |
tree | 2ab261d5b30e8ac8e0c9ae899221c7f0e66e85c7 /libdevkit-power | |
parent | d06a9707c5e2ab366325e5e8e77207dcc1885c56 (diff) |
of course, the right thing to do is make a static lib to avoid playing with ../src/ type paths
Diffstat (limited to 'libdevkit-power')
-rw-r--r-- | libdevkit-power/.gitignore | 11 | ||||
-rw-r--r-- | libdevkit-power/Makefile.am | 25 | ||||
-rw-r--r-- | libdevkit-power/dkp-debug.c | 240 | ||||
-rw-r--r-- | libdevkit-power/dkp-debug.h | 80 | ||||
-rw-r--r-- | libdevkit-power/dkp-enum.c | 229 | ||||
-rw-r--r-- | libdevkit-power/dkp-enum.h | 69 | ||||
-rw-r--r-- | libdevkit-power/dkp-object.c | 361 | ||||
-rw-r--r-- | libdevkit-power/dkp-object.h | 67 |
8 files changed, 1082 insertions, 0 deletions
diff --git a/libdevkit-power/.gitignore b/libdevkit-power/.gitignore new file mode 100644 index 0000000..e9199c5 --- /dev/null +++ b/libdevkit-power/.gitignore @@ -0,0 +1,11 @@ +.deps +.libs +*.o +*.la +*.lo +*-marshal.c +*-marshal.h +*.gcov +*.gcda +*.gcno + diff --git a/libdevkit-power/Makefile.am b/libdevkit-power/Makefile.am new file mode 100644 index 0000000..e3e0f8b --- /dev/null +++ b/libdevkit-power/Makefile.am @@ -0,0 +1,25 @@ +INCLUDES = \ + $(GLIB_CFLAGS) \ + $(DBUS_CFLAGS) \ + -I$(top_srcdir)/libdevkit-power \ + -DPACKAGE_DATA_DIR=\""$(datadir)"\" \ + -DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\" + +noinst_LTLIBRARIES = \ + libdevkit-power.la + +libdevkit_power_la_SOURCES = \ + dkp-debug.c \ + dkp-debug.h \ + dkp-enum.c \ + dkp-enum.h \ + dkp-object.c \ + dkp-object.h + +libdevkit_power_la_LIBADD = $(INTLLIBS) $(GLIB_LIBS) + +clean-local: + rm -f *~ + +CLEANFILES = $(BUILT_SOURCES) + diff --git a/libdevkit-power/dkp-debug.c b/libdevkit-power/dkp-debug.c new file mode 100644 index 0000000..bf3f12e --- /dev/null +++ b/libdevkit-power/dkp-debug.c @@ -0,0 +1,240 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/** + * SECTION:dkp-debug + * @short_description: Debugging functions + * + * This file contains functions that can be used for debugging. + */ + +#include <glib.h> +#include <glib/gi18n.h> +#include <glib/gprintf.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> +#include <stdlib.h> +#include <signal.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <time.h> +#include <execinfo.h> + +#include "dkp-debug.h" + +#define CONSOLE_RESET 0 +#define CONSOLE_BLACK 30 +#define CONSOLE_RED 31 +#define CONSOLE_GREEN 32 +#define CONSOLE_YELLOW 33 +#define CONSOLE_BLUE 34 +#define CONSOLE_MAGENTA 35 +#define CONSOLE_CYAN 36 +#define CONSOLE_WHITE 37 + +#define PK_LOG_FILE PK_LOG_DIR "/PackageKit" + +static gboolean do_verbose = FALSE; /* if we should print out debugging */ +static gboolean is_console = FALSE; + +/** + * dkp_set_console_mode: + **/ +static void +dkp_set_console_mode (guint console_code) +{ + gchar command[13]; + + /* don't put extra commands into logs */ + if (!is_console) { + return; + } + /* Command is the control command to the terminal */ + g_snprintf (command, 13, "%c[%dm", 0x1B, console_code); + printf ("%s", command); +} + +/** + * dkp_debug_backtrace: + **/ +void +dkp_debug_backtrace (void) +{ + void *call_stack[512]; + int call_stack_size; + char **symbols; + int i = 1; + + call_stack_size = backtrace (call_stack, G_N_ELEMENTS (call_stack)); + symbols = backtrace_symbols (call_stack, call_stack_size); + if (symbols != NULL) { + dkp_set_console_mode (CONSOLE_RED); + g_print ("Traceback:\n"); + while (i < call_stack_size) { + g_print ("\t%s\n", symbols[i]); + i++; + } + dkp_set_console_mode (CONSOLE_RESET); + free (symbols); + } +} + +/** + * dkp_print_line: + **/ +static void +dkp_print_line (const gchar *func, const gchar *file, const int line, const gchar *buffer, guint color) +{ + gchar *str_time; + gchar *header; + time_t the_time; + GThread *thread; + + time (&the_time); + str_time = g_new0 (gchar, 255); + strftime (str_time, 254, "%H:%M:%S", localtime (&the_time)); + thread = g_thread_self (); + + /* generate header text */ + header = g_strdup_printf ("TI:%s\tTH:%p\tFI:%s\tFN:%s,%d", str_time, thread, file, func, line); + g_free (str_time); + + /* always in light green */ + dkp_set_console_mode (CONSOLE_GREEN); + printf ("%s\n", header); + + /* different colours according to the severity */ + dkp_set_console_mode (color); + printf (" - %s\n", buffer); + dkp_set_console_mode (CONSOLE_RESET); + + /* flush this output, as we need to debug */ + fflush (stdout); + + g_free (header); +} + +/** + * dkp_debug_real: + **/ +void +dkp_debug_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...) +{ + va_list args; + gchar *buffer = NULL; + + if (do_verbose == FALSE) { + return; + } + + va_start (args, format); + g_vasprintf (&buffer, format, args); + va_end (args); + + dkp_print_line (func, file, line, buffer, CONSOLE_BLUE); + + g_free(buffer); +} + +/** + * dkp_warning_real: + **/ +void +dkp_warning_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...) +{ + va_list args; + gchar *buffer = NULL; + + if (do_verbose == FALSE) { + return; + } + + va_start (args, format); + g_vasprintf (&buffer, format, args); + va_end (args); + + /* do extra stuff for a warning */ + if (!is_console) { + printf ("*** WARNING ***\n"); + } + dkp_print_line (func, file, line, buffer, CONSOLE_RED); + + g_free(buffer); + + /* we want to fix this! */ + dkp_debug_backtrace (); +} + +/** + * dkp_error_real: + **/ +void +dkp_error_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...) +{ + va_list args; + gchar *buffer = NULL; + + va_start (args, format); + g_vasprintf (&buffer, format, args); + va_end (args); + + /* do extra stuff for a warning */ + if (!is_console) { + printf ("*** ERROR ***\n"); + } + dkp_print_line (func, file, line, buffer, CONSOLE_RED); + g_free(buffer); + + /* we want to fix this! */ + dkp_debug_backtrace (); + + exit (1); +} + +/** + * dkp_debug_enabled: + * + * Returns: TRUE if we have debugging enabled + **/ +gboolean +dkp_debug_enabled (void) +{ + return do_verbose; +} + +/** + * dkp_debug_init: + * @debug: If we should print out verbose debugging + **/ +void +dkp_debug_init (gboolean debug) +{ + do_verbose = debug; + /* check if we are on console */ + if (isatty (fileno (stdout)) == 1) { + is_console = TRUE; + } + dkp_debug ("Verbose debugging %i (on console %i)", do_verbose, is_console); +} + diff --git a/libdevkit-power/dkp-debug.h b/libdevkit-power/dkp-debug.h new file mode 100644 index 0000000..8f05c01 --- /dev/null +++ b/libdevkit-power/dkp-debug.h @@ -0,0 +1,80 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __PK_DEBUG_H +#define __PK_DEBUG_H + +#include <stdarg.h> +#include <glib.h> + +G_BEGIN_DECLS + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +/** + * dkp_debug: + * + * Non critical debugging + */ +#define dkp_debug(...) dkp_debug_real (__func__, __FILE__, __LINE__, __VA_ARGS__) + +/** + * dkp_warning: + * + * Important debugging + */ +#define dkp_warning(...) dkp_warning_real (__func__, __FILE__, __LINE__, __VA_ARGS__) + +/** + * dkp_error: + * + * Critical debugging, with exit + */ +#define dkp_error(...) dkp_error_real (__func__, __FILE__, __LINE__, __VA_ARGS__) + +#elif defined(__GNUC__) && __GNUC__ >= 3 +#define dkp_debug(...) dkp_debug_real (__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) +#define dkp_warning(...) dkp_warning_real (__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) +#define dkp_error(...) dkp_error_real (__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) +#else +#define dkp_debug(...) +#define dkp_warning(...) +#define dkp_error(...) +#endif + +void dkp_debug_init (gboolean debug); +gboolean dkp_debug_enabled (void); +void dkp_debug_backtrace (void); +void dkp_debug_real (const gchar *func, + const gchar *file, + int line, + const gchar *format, ...) __attribute__((format (printf,4,5))); +void dkp_warning_real (const gchar *func, + const gchar *file, + int line, + const gchar *format, ...) __attribute__((format (printf,4,5))); +void dkp_error_real (const gchar *func, + const gchar *file, + int line, + const gchar *format, ...) __attribute__((format (printf,4,5))); + +G_END_DECLS + +#endif /* __PK_DEBUG_H */ diff --git a/libdevkit-power/dkp-enum.c b/libdevkit-power/dkp-enum.c new file mode 100644 index 0000000..26fd788 --- /dev/null +++ b/libdevkit-power/dkp-enum.c @@ -0,0 +1,229 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 David Zeuthen <david@fubar.dk> + * Copyright (C) 2008 Richard Hughes <richard@hughsie.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include <glib.h> +#include <string.h> +#include "dkp-debug.h" +#include "dkp-enum.h" + +/** + * dkp_source_type_to_text: + **/ +const gchar * +dkp_source_type_to_text (DkpSourceType type_enum) +{ + const gchar *type = NULL; + switch (type_enum) { + case DKP_SOURCE_TYPE_LINE_POWER: + type = "line-power"; + break; + case DKP_SOURCE_TYPE_BATTERY: + type = "battery"; + break; + case DKP_SOURCE_TYPE_UPS: + type = "ups"; + break; + case DKP_SOURCE_TYPE_MOUSE: + type = "mouse"; + break; + case DKP_SOURCE_TYPE_KEYBOARD: + type = "keyboard"; + break; + case DKP_SOURCE_TYPE_PDA: + type = "pda"; + break; + case DKP_SOURCE_TYPE_PHONE: + type = "phone"; + break; + case DKP_SOURCE_TYPE_UNKNOWN: + type = "unknown"; + break; + default: + g_assert_not_reached (); + break; + } + return type; +} + +/** + * dkp_source_type_from_text: + **/ +DkpSourceType +dkp_source_type_from_text (const gchar *type) +{ + if (type == NULL) + return DKP_SOURCE_TYPE_UNKNOWN; + if (strcmp (type, "line-power") == 0) + return DKP_SOURCE_TYPE_LINE_POWER; + if (strcmp (type, "battery") == 0) + return DKP_SOURCE_TYPE_BATTERY; + if (strcmp (type, "ups") == 0) + return DKP_SOURCE_TYPE_UPS; + if (strcmp (type, "mouse") == 0) + return DKP_SOURCE_TYPE_MOUSE; + if (strcmp (type, "keyboard") == 0) + return DKP_SOURCE_TYPE_KEYBOARD; + if (strcmp (type, "pda") == 0) + return DKP_SOURCE_TYPE_PDA; + if (strcmp (type, "phone") == 0) + return DKP_SOURCE_TYPE_PHONE; + return DKP_SOURCE_TYPE_UNKNOWN; +} + +/** + * dkp_source_state_to_text: + **/ +const gchar * +dkp_source_state_to_text (DkpSourceState state_enum) +{ + const gchar *state = NULL; + switch (state_enum) { + case DKP_SOURCE_STATE_CHARGING: + state = "charging"; + break; + case DKP_SOURCE_STATE_DISCHARGING: + state = "discharging"; + break; + case DKP_SOURCE_STATE_EMPTY: + state = "empty"; + break; + case DKP_SOURCE_STATE_FULLY_CHARGED: + state = "fully-charged"; + break; + case DKP_SOURCE_STATE_UNKNOWN: + state = "unknown"; + break; + default: + g_assert_not_reached (); + break; + } + return state; +} + +/** + * dkp_source_state_from_text: + **/ +DkpSourceState +dkp_source_state_from_text (const gchar *state) +{ + if (state == NULL) + return DKP_SOURCE_STATE_UNKNOWN; + if (strcmp (state, "charging") == 0) + return DKP_SOURCE_STATE_CHARGING; + if (strcmp (state, "discharging") == 0) + return DKP_SOURCE_STATE_DISCHARGING; + if (strcmp (state, "empty") == 0) + return DKP_SOURCE_STATE_EMPTY; + if (strcmp (state, "fully-charged") == 0) + return DKP_SOURCE_STATE_FULLY_CHARGED; + return DKP_SOURCE_STATE_UNKNOWN; +} + +/** + * dkp_source_technology_to_text: + **/ +const gchar * +dkp_source_technology_to_text (DkpSourceTechnology technology_enum) +{ + const gchar *technology = NULL; + switch (technology_enum) { + case DKP_SOURCE_TECHNOLGY_LITHIUM_ION: + technology = "lithium-ion"; + break; + case DKP_SOURCE_TECHNOLGY_LITHIUM_POLYMER: + technology = "lithium-polymer"; + break; + case DKP_SOURCE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE: + technology = "lithium-iron-phosphate"; + break; + case DKP_SOURCE_TECHNOLGY_LEAD_ACID: + technology = "lead-acid"; + break; + case DKP_SOURCE_TECHNOLGY_NICKEL_CADMIUM: + technology = "nickel-cadmium"; + break; + case DKP_SOURCE_TECHNOLGY_NICKEL_METAL_HYDRIDE: + technology = "nickel-metal-hydride"; + break; + case DKP_SOURCE_TECHNOLGY_UNKNOWN: + technology = "unknown"; + break; + default: + g_assert_not_reached (); + break; + } + return technology; +} + +/** + * dkp_source_technology_from_text: + **/ +DkpSourceTechnology +dkp_source_technology_from_text (const gchar *technology) +{ + if (technology == NULL) + return DKP_SOURCE_TECHNOLGY_UNKNOWN; + if (strcmp (technology, "lithium-ion") == 0) + return DKP_SOURCE_TECHNOLGY_LITHIUM_ION; + if (strcmp (technology, "lithium-polymer") == 0) + return DKP_SOURCE_TECHNOLGY_LITHIUM_POLYMER; + if (strcmp (technology, "lithium-iron-phosphate") == 0) + return DKP_SOURCE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE; + if (strcmp (technology, "lead-acid") == 0) + return DKP_SOURCE_TECHNOLGY_LEAD_ACID; + if (strcmp (technology, "nickel-cadmium") == 0) + return DKP_SOURCE_TECHNOLGY_NICKEL_CADMIUM; + if (strcmp (technology, "nickel-metal-hydride") == 0) + return DKP_SOURCE_TECHNOLGY_NICKEL_METAL_HYDRIDE; + return DKP_SOURCE_TECHNOLGY_UNKNOWN; +} + +/** + * dkp_acpi_to_source_technology: + **/ +DkpSourceTechnology +dkp_acpi_to_source_technology (const gchar *type) +{ + if (type == NULL) { + return DKP_SOURCE_TECHNOLGY_UNKNOWN; + } + /* every case combination of Li-Ion is commonly used.. */ + if (strcasecmp (type, "li-ion") == 0 || + strcasecmp (type, "lion") == 0) { + return DKP_SOURCE_TECHNOLGY_LITHIUM_ION; + } + if (strcasecmp (type, "pb") == 0 || + strcasecmp (type, "pbac") == 0) { + return DKP_SOURCE_TECHNOLGY_LEAD_ACID; + } + if (strcasecmp (type, "lip") == 0 || + strcasecmp (type, "lipo") == 0) { + return DKP_SOURCE_TECHNOLGY_LITHIUM_POLYMER; + } + if (strcasecmp (type, "nimh") == 0) { + return DKP_SOURCE_TECHNOLGY_NICKEL_METAL_HYDRIDE; + } + if (strcasecmp (type, "lifo") == 0) { + return DKP_SOURCE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE; + } + return DKP_SOURCE_TECHNOLGY_UNKNOWN; +} + diff --git a/libdevkit-power/dkp-enum.h b/libdevkit-power/dkp-enum.h new file mode 100644 index 0000000..6565566 --- /dev/null +++ b/libdevkit-power/dkp-enum.h @@ -0,0 +1,69 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 David Zeuthen <david@fubar.dk> + * Copyright (C) 2008 Richard Hughes <richard@hughsie.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __DKP_ENUM_H__ +#define __DKP_ENUM_H__ + +#include <glib.h> + +G_BEGIN_DECLS + +typedef enum { + DKP_SOURCE_TYPE_LINE_POWER, + DKP_SOURCE_TYPE_BATTERY, + DKP_SOURCE_TYPE_UPS, + DKP_SOURCE_TYPE_MOUSE, + DKP_SOURCE_TYPE_KEYBOARD, + DKP_SOURCE_TYPE_PDA, + DKP_SOURCE_TYPE_PHONE, + DKP_SOURCE_TYPE_UNKNOWN +} DkpSourceType; + +typedef enum { + DKP_SOURCE_STATE_CHARGING, + DKP_SOURCE_STATE_DISCHARGING, + DKP_SOURCE_STATE_EMPTY, + DKP_SOURCE_STATE_FULLY_CHARGED, + DKP_SOURCE_STATE_UNKNOWN +} DkpSourceState; + +typedef enum { + DKP_SOURCE_TECHNOLGY_LITHIUM_ION, + DKP_SOURCE_TECHNOLGY_LITHIUM_POLYMER, + DKP_SOURCE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE, + DKP_SOURCE_TECHNOLGY_LEAD_ACID, + DKP_SOURCE_TECHNOLGY_NICKEL_CADMIUM, + DKP_SOURCE_TECHNOLGY_NICKEL_METAL_HYDRIDE, + DKP_SOURCE_TECHNOLGY_UNKNOWN +} DkpSourceTechnology; + +const gchar *dkp_source_type_to_text (DkpSourceType type_enum); +const gchar *dkp_source_state_to_text (DkpSourceState state_enum); +const gchar *dkp_source_technology_to_text (DkpSourceTechnology technology_enum); +DkpSourceType dkp_source_type_from_text (const gchar *type); +DkpSourceState dkp_source_state_from_text (const gchar *state); +DkpSourceTechnology dkp_source_technology_from_text (const gchar *technology); +DkpSourceTechnology dkp_acpi_to_source_technology (const char *type); + +G_END_DECLS + +#endif /* __DKP_ENUM_H__ */ + diff --git a/libdevkit-power/dkp-object.c b/libdevkit-power/dkp-object.c new file mode 100644 index 0000000..c76e0f6 --- /dev/null +++ b/libdevkit-power/dkp-object.c @@ -0,0 +1,361 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Richard Hughes <richard@hughsie.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include <glib.h> +#include <glib-object.h> +#include <string.h> + +#include "dkp-debug.h" +#include "dkp-enum.h" +#include "dkp-object.h" + +/** + * dkp_object_clear_internal: + **/ +static void +dkp_object_clear_internal (DkpObject *obj) +{ + obj->type = DKP_SOURCE_TYPE_UNKNOWN; + obj->update_time = 0; + obj->battery_energy = -1; + obj->battery_energy_full = -1; + obj->battery_energy_full_design = -1; + obj->battery_energy_rate = -1; + obj->battery_percentage = -1; + obj->battery_capacity = -1; + obj->battery_time_to_empty = -1; + obj->battery_time_to_full = -1; + obj->battery_state = DKP_SOURCE_STATE_UNKNOWN; + obj->battery_technology = DKP_SOURCE_TECHNOLGY_UNKNOWN; + obj->vendor = NULL; + obj->model = NULL; + obj->serial = NULL; + obj->native_path = NULL; + obj->line_power_online = FALSE; + obj->battery_is_present = FALSE; + obj->power_supply = FALSE; + obj->battery_is_rechargeable = FALSE; +} + +/** + * dkp_object_collect_props: + **/ +static void +dkp_object_collect_props (const char *key, const GValue *value, DkpObject *obj) +{ + gboolean handled = TRUE; + + if (strcmp (key, "native-path") == 0) + obj->native_path = g_strdup (g_value_get_string (value)); + else if (strcmp (key, "vendor") == 0) + obj->vendor = g_strdup (g_value_get_string (value)); + else if (strcmp (key, "model") == 0) + obj->model = g_strdup (g_value_get_string (value)); + else if (strcmp (key, "serial") == 0) + obj->serial = g_strdup (g_value_get_string (value)); + else if (strcmp (key, "update-time") == 0) + obj->update_time = g_value_get_uint64 (value); + else if (strcmp (key, "type") == 0) + obj->type = dkp_source_type_from_text (g_value_get_string (value)); + else if (strcmp (key, "line-power-online") == 0) + obj->line_power_online = g_value_get_boolean (value); + else if (strcmp (key, "battery-energy") == 0) + obj->battery_energy = g_value_get_double (value); + else if (strcmp (key, "battery-energy-empty") == 0) + obj->battery_energy_empty = g_value_get_double (value); + else if (strcmp (key, "battery-energy-full") == 0) + obj->battery_energy_full = g_value_get_double (value); + else if (strcmp (key, "battery-energy-full-design") == 0) + obj->battery_energy_full_design = g_value_get_double (value); + else if (strcmp (key, "battery-energy-rate") == 0) + obj->battery_energy_rate = g_value_get_double (value); + else if (strcmp (key, "battery-time-to-full") == 0) + obj->battery_time_to_full = g_value_get_int64 (value); + else if (strcmp (key, "battery-time-to-empty") == 0) + obj->battery_time_to_empty = g_value_get_int64 (value); + else if (strcmp (key, "battery-percentage") == 0) + obj->battery_percentage = g_value_get_double (value); + else if (strcmp (key, "battery-technology") == 0) + obj->battery_technology = dkp_source_technology_from_text (g_value_get_string (value)); + else if (strcmp (key, "battery-is-present") == 0) + obj->battery_is_present = g_value_get_boolean (value); + else if (strcmp (key, "battery-is-rechargeable") == 0) + obj->battery_is_rechargeable = g_value_get_boolean (value); + else if (strcmp (key, "power-supply") == 0) + obj->power_supply = g_value_get_boolean (value); + else if (strcmp (key, "battery-capacity") == 0) + obj->battery_capacity = g_value_get_double (value); + else if (strcmp (key, "battery-state") == 0) + obj->battery_state = dkp_source_state_from_text (g_value_get_string (value)); + else + handled = FALSE; + + if (!handled) + dkp_warning ("unhandled property '%s'", key); +} + +/** + * dkp_object_set_from_map: + **/ +gboolean +dkp_object_set_from_map (DkpObject *obj, GHashTable *hash_table) +{ + g_hash_table_foreach (hash_table, (GHFunc) dkp_object_collect_props, obj); + return TRUE; +} + +/** + * dkp_object_copy: + **/ +DkpObject * +dkp_object_copy (const DkpObject *cobj) +{ + DkpObject *obj; + obj = g_new0 (DkpObject, 1); + + obj->type = cobj->type; + obj->update_time = cobj->update_time; + obj->battery_energy = cobj->battery_energy; + obj->battery_energy_full = cobj->battery_energy_full; + obj->battery_energy_full_design = cobj->battery_energy_full_design; + obj->battery_energy_rate = cobj->battery_energy_rate; + obj->battery_percentage = cobj->battery_percentage; + obj->battery_capacity = cobj->battery_capacity; + obj->battery_time_to_empty = cobj->battery_time_to_empty; + obj->battery_time_to_full = cobj->battery_time_to_full; + obj->battery_state = cobj->battery_state; + obj->battery_technology = cobj->battery_technology; + obj->vendor = g_strdup (cobj->vendor); + obj->model = g_strdup (cobj->model); + obj->serial = g_strdup (cobj->serial); + obj->native_path = g_strdup (cobj->native_path); + obj->line_power_online = cobj->line_power_online; + obj->battery_is_present = cobj->battery_is_present; + obj->power_supply = cobj->power_supply; + obj->battery_is_rechargeable = cobj->battery_is_rechargeable; + + return obj; +} + +/** + * dkp_object_equal: + **/ +gboolean +dkp_object_equal (const DkpObject *obj1, const DkpObject *obj2) +{ + if (obj1->type == obj2->type && + obj1->update_time == obj2->update_time && + obj1->battery_energy == obj2->battery_energy && + obj1->battery_energy_full == obj2->battery_energy_full && + obj1->battery_energy_full_design == obj2->battery_energy_full_design && + obj1->battery_energy_rate == obj2->battery_energy_rate && + obj1->battery_percentage == obj2->battery_percentage && + obj1->battery_capacity == obj2->battery_capacity && + obj1->battery_time_to_empty == obj2->battery_time_to_empty && + obj1->battery_time_to_full == obj2->battery_time_to_full && + obj1->battery_state == obj2->battery_state && + obj1->battery_technology == obj2->battery_technology && + strcmp (obj1->vendor, obj2->vendor) == 0 && + strcmp (obj1->model, obj2->model) == 0 && + strcmp (obj1->serial, obj2->serial) == 0 && + strcmp (obj1->native_path, obj2->native_path) == 0 && + obj1->line_power_online == obj2->line_power_online && + obj1->battery_is_present == obj2->battery_is_present && + obj1->power_supply == obj2->power_supply && + obj1->battery_is_rechargeable == obj2->battery_is_rechargeable) + return TRUE; + return FALSE; +} + +/** + * dkp_strzero: + * @text: The text to check + * + * This function is a much safer way of doing "if (strlen (text) == 0))" + * as it does not rely on text being NULL terminated. It's also much + * quicker as it only checks the first byte rather than scanning the whole + * string just to verify it's not zero length. + * + * Return value: %TRUE if the string was converted correctly + **/ +static gboolean +dkp_strzero (const gchar *text) +{ + if (text == NULL) { + return TRUE; + } + if (text[0] == '\0') { + return TRUE; + } + return FALSE; +} + +/** + * dkp_object_print: + **/ +gboolean +dkp_object_print (const DkpObject *obj) +{ + gboolean ret = TRUE; + struct tm *time_tm; + time_t t; + gchar time_buf[256]; + + /* get a human readable time */ + t = (time_t) obj->update_time; + time_tm = localtime (&t); + strftime (time_buf, sizeof time_buf, "%c", time_tm); + + g_print (" native-path: %s\n", obj->native_path); + if (!dkp_strzero (obj->vendor)) + g_print (" vendor: %s\n", obj->vendor); + if (!dkp_strzero (obj->model)) + g_print (" model: %s\n", obj->model); + if (!dkp_strzero (obj->serial)) + g_print (" serial: %s\n", obj->serial); + g_print (" power supply: %s\n", obj->power_supply ? "yes" : "no"); + g_print (" updated: %s (%d seconds ago)\n", time_buf, (int) (time (NULL) - obj->update_time)); + if (obj->type == DKP_SOURCE_TYPE_BATTERY) { + g_print (" battery\n"); + g_print (" present: %s\n", obj->battery_is_present ? "yes" : "no"); + g_print (" rechargeable: %s\n", obj->battery_is_rechargeable ? "yes" : "no"); + g_print (" state: %s\n", dkp_source_state_to_text (obj->battery_state)); + g_print (" energy: %g Wh\n", obj->battery_energy); + g_print (" energy-empty: %g Wh\n", obj->battery_energy_empty); + g_print (" energy-full: %g Wh\n", obj->battery_energy_full); + g_print (" energy-full-design: %g Wh\n", obj->battery_energy_full_design); + g_print (" energy-rate: %g W\n", obj->battery_energy_rate); + g_print (" time to full: "); + if (obj->battery_time_to_full >= 0) + g_print ("%d seconds\n", (int) obj->battery_time_to_full); + else + g_print ("unknown\n"); + g_print (" time to empty: "); + if (obj->battery_time_to_empty >= 0) + g_print ("%d seconds\n", (int) obj->battery_time_to_empty); + else + g_print ("unknown\n"); + g_print (" percentage: %g%%\n", obj->battery_percentage); + g_print (" capacity: %g%%\n", obj->battery_capacity); + g_print (" technology: %s\n", dkp_source_technology_to_text (obj->battery_technology)); + } else if (obj->type == DKP_SOURCE_TYPE_LINE_POWER) { + g_print (" line-power\n"); + g_print (" online: %s\n", obj->line_power_online ? "yes" : "no"); + } else { + g_print (" unknown power source type '%s'\n", dkp_source_type_to_text (obj->type)); + ret = FALSE; + } + return ret; +} + +/** + * dkp_object_new: + **/ +DkpObject * +dkp_object_new (void) +{ + DkpObject *obj; + obj = g_new0 (DkpObject, 1); + dkp_object_clear_internal (obj); + return obj; +} + +/** + * dkp_object_clear: + **/ +gboolean +dkp_object_clear (DkpObject *obj) +{ + if (obj == NULL) + return FALSE; + dkp_object_free (obj); + dkp_object_clear_internal (obj); + return TRUE; +} + +/** + * dkp_object_free: + **/ +gboolean +dkp_object_free (DkpObject *obj) +{ + if (obj == NULL) + return FALSE; + g_free (obj->vendor); + g_free (obj->model); + g_free (obj->serial); + g_free (obj->native_path); + g_free (obj); + return TRUE; +} + +/** + * dkp_object_get_id: + **/ +gchar * +dkp_object_get_id (DkpObject *obj) +{ + GString *string; + gchar *id = NULL; + + /* only valid for devices supplying the system */ + if (!obj->power_supply) + return id; + + /* only valid for batteries */ + if (obj->type != DKP_SOURCE_TYPE_BATTERY) + return id; + + /* we don't have an ID if we are not present */ + if (!obj->battery_is_present) + return id; + + string = g_string_new (""); + + /* in an ideal world, model-capacity-serial */ + if (obj->model != NULL && strlen (obj->model) > 2) { + g_string_append (string, obj->model); + g_string_append_c (string, '-'); + } + if (obj->battery_energy_full_design > 0) { + g_string_append_printf (string, "%i", (guint) obj->battery_energy_full_design); + g_string_append_c (string, '-'); + } + if (obj->serial != NULL && strlen (obj->serial) > 2) { + g_string_append (string, obj->serial); + g_string_append_c (string, '-'); + } + + /* make sure we are sane */ + if (string->len == 0) { + /* just use something generic */ + g_string_append (string, "generic_id"); + } else { + /* remove trailing '-' */ + g_string_set_size (string, string->len - 1); + } + + /* the id may have invalid chars that need to be replaced */ + id = g_string_free (string, FALSE); + g_strdelimit (id, "\\\t\"' /", '_'); + + return id; +} + diff --git a/libdevkit-power/dkp-object.h b/libdevkit-power/dkp-object.h new file mode 100644 index 0000000..07fb3dd --- /dev/null +++ b/libdevkit-power/dkp-object.h @@ -0,0 +1,67 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2008 Richard Hughes <richard@hughsie.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef __DKP_OBJECT_H__ +#define __DKP_OBJECT_H__ + +#include <glib.h> +#include "dkp-enum.h" + +G_BEGIN_DECLS + +typedef struct { + guint64 update_time; + gchar *vendor; + gchar *model; + gchar *serial; + gchar *native_path; + gboolean power_supply; + gboolean line_power_online; + gboolean battery_is_present; + gboolean battery_is_rechargeable; + DkpSourceType type; + DkpSourceState battery_state; + DkpSourceTechnology battery_technology; + gdouble battery_capacity; + gdouble battery_energy; + gdouble battery_energy_empty; + gdouble battery_energy_full; + gdouble battery_energy_full_design; + gdouble battery_energy_rate; + gint64 battery_time_to_empty; + gint64 battery_time_to_full; + gdouble battery_percentage; +} DkpObject; + +DkpObject *dkp_object_new (void); +gboolean dkp_object_clear (DkpObject *obj); +gboolean dkp_object_free (DkpObject *obj); +gchar *dkp_object_get_id (DkpObject *obj); +DkpObject *dkp_object_copy (const DkpObject *cobj); +gboolean dkp_object_print (const DkpObject *obj); +gboolean dkp_object_equal (const DkpObject *obj1, + const DkpObject *obj2); +gboolean dkp_object_set_from_map (DkpObject *obj, + GHashTable *hash_table); + +G_END_DECLS + +#endif /* __DKP_OBJECT_H__ */ + |