--include $(DEPS)
diff --git a/tools/xenpmd/acpi-events.c b/tools/xenpmd/acpi-events.c
new file mode 100644
-index 0000000..8baf720
+index 0000000..d991eb0
--- /dev/null
+++ b/tools/xenpmd/acpi-events.c
-@@ -0,0 +1,149 @@
+@@ -0,0 +1,164 @@
+/*
+ * acpi-events.c
+ *
+ xs_write(xs, XBT_NULL, xenstore_path, default_value, strlen(default_value));
+ file = fopen(file_path, "r");
+ if ( file == NULL )
++ {
++ xenpmd_log(LOG_NOTICE, "File %s failed to open with error %d\n",
++ file_path, errno);
+ return;
++ }
+
+ memset(file_data, 0, 1024);
+ fgets(file_data, 1024, file);
+
+void handle_ac_adapter_state_change(void)
+{
++ xenpmd_log(LOG_INFO, "AC adapter state change event\n");
+ write_state_info_in_xenstore(AC_ADAPTER_STATE_FILE_PATH,
+ XS_AC_ADAPTER_STATE_PATH, "off-line", "1", "0");
+ xs_write(xs, XBT_NULL, XS_AC_ADAPTER_EVENT_PATH, "1", 1);
+
+void handle_lid_state_change(void)
+{
++ xenpmd_log(LOG_INFO, "LID state change event\n");
+ write_state_info_in_xenstore(LID_STATE_FILE_PATH, XS_LID_STATE_PATH,
+ "closed", "1", "0");
+ xs_write(xs, XBT_NULL, XS_LID_EVENT_PATH, "1", 1);
+
+void handle_pbtn_pressed_event(void)
+{
++ xenpmd_log(LOG_INFO, "Power button pressed event\n");
+ xs_write(xs, XBT_NULL, XS_PBTN_EVENT_PATH, "1", 1);
+}
+
+void handle_sbtn_pressed_event(void)
+{
++ xenpmd_log(LOG_INFO, "Sleep button pressed event\n");
+ xs_write(xs, XBT_NULL, XS_SBTN_EVENT_PATH, "1", 1);
+}
+
+
+ socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
+ if ( socket_fd == -1)
++ {
++ xenpmd_log(LOG_ERR, "socket function failed with error - %d\n", errno);
+ return (void *)socket_fd;
++ }
+
+ addr.sun_family = AF_UNIX;
+ strncpy(addr.sun_path, ACPID_SOCKET_PATH, strlen(ACPID_SOCKET_PATH));
+ addr.sun_path[strlen(ACPID_SOCKET_PATH)] = '\0';
+ ret = connect(socket_fd, (struct sockaddr *)&addr, sizeof(addr));
+ if ( ret == -1 )
++ {
++ xenpmd_log(LOG_ERR, "Socket connection function failed with error - %d\n", errno);
+ return (void *)ret;
++ }
+
+ while( 1 )
+ {
+
+void acpi_events_cleanup(void)
+{
++ xenpmd_log(LOG_INFO, "In acpi events cleanup\n");
+ if ( socket_fd != -1 )
+ close(socket_fd);
+
+}
+
diff --git a/tools/xenpmd/xenpmd.c b/tools/xenpmd/xenpmd.c
-index 28de744..50583b8 100644
+index 28de744..ff6d39d 100644
--- a/tools/xenpmd/xenpmd.c
+++ b/tools/xenpmd/xenpmd.c
@@ -33,68 +33,10 @@
if (type == BIF)
memset(info_or_status, 0, sizeof(struct battery_info));
else
-@@ -306,8 +257,11 @@ int get_next_battery_info_or_status(DIR *battery_dir,
+@@ -306,31 +257,34 @@ int get_next_battery_info_or_status(DIR *battery_dir,
if ( !file )
return 0;
fclose(file);
return 1;
-@@ -317,14 +271,14 @@ int get_next_battery_info_or_status(DIR *battery_dir,
+ }
+
+-#ifdef RUN_STANDALONE
++#ifdef XENPMD_DEBUG
void print_battery_info(struct battery_info *info)
{
- printf("present: %d\n", info->present);
+- printf("present: %d\n", info->present);
- printf("design capacity: %d\n", info->design_capacity);
- printf("last full capacity: %d\n", info->last_full_capacity);
-+ printf("design capacity: %d\n", (int) info->design_capacity);
-+ printf("last full capacity: %d\n", (int) info->last_full_capacity);
- printf("battery technology: %d\n", info->battery_technology);
+- printf("battery technology: %d\n", info->battery_technology);
- printf("design voltage: %d\n", info->design_voltage);
- printf("design capacity warning:%d\n", info->design_capacity_warning);
- printf("design capacity low: %d\n", info->design_capacity_low);
- printf("capacity granularity 1: %d\n", info->capacity_granularity_1);
- printf("capacity granularity 2: %d\n", info->capacity_granularity_2);
-+ printf("design voltage: %d\n", (int) info->design_voltage);
-+ printf("design capacity warning:%d\n", (int) info->design_capacity_warning);
-+ printf("design capacity low: %d\n", (int) info->design_capacity_low);
-+ printf("capacity granularity 1: %d\n", (int) info->capacity_granularity_1);
-+ printf("capacity granularity 2: %d\n", (int) info->capacity_granularity_2);
- printf("model number: %s\n", info->model_number);
- printf("serial number: %s\n", info->serial_number);
- printf("battery type: %s\n", info->battery_type);
-@@ -406,10 +360,11 @@ int write_one_time_battery_info(void)
+- printf("model number: %s\n", info->model_number);
+- printf("serial number: %s\n", info->serial_number);
+- printf("battery type: %s\n", info->battery_type);
+- printf("OEM info: %s\n", info->oem_info);
++ xenpmd_log(LOG_DEBUG, "present: %d\n", info->present);
++ xenpmd_log(LOG_DEBUG, "design capacity: %d\n", (int) info->design_capacity);
++ xenpmd_log(LOG_DEBUG, "last full capacity: %d\n", (int) info->last_full_capacity);
++ xenpmd_log(LOG_DEBUG, "battery technology: %d\n", info->battery_technology);
++ xenpmd_log(LOG_DEBUG, "design voltage: %d\n", (int) info->design_voltage);
++ xenpmd_log(LOG_DEBUG, "design capacity warning:%d\n", (int) info->design_capacity_warning);
++ xenpmd_log(LOG_DEBUG, "design capacity low: %d\n", (int) info->design_capacity_low);
++ xenpmd_log(LOG_DEBUG, "capacity granularity 1: %d\n", (int) info->capacity_granularity_1);
++ xenpmd_log(LOG_DEBUG, "capacity granularity 2: %d\n", (int) info->capacity_granularity_2);
++ xenpmd_log(LOG_DEBUG, "model number: %s\n", info->model_number);
++ xenpmd_log(LOG_DEBUG, "serial number: %s\n", info->serial_number);
++ xenpmd_log(LOG_DEBUG, "battery type: %s\n", info->battery_type);
++ xenpmd_log(LOG_DEBUG, "OEM info: %s\n", info->oem_info);
+ }
+-#endif /*RUN_STANDALONE*/
++#endif /*XENPMD_DEBUG*/
+
+ void write_ulong_lsb_first(char *temp_val, unsigned long val)
+ {
+@@ -383,16 +337,21 @@ int write_one_time_battery_info(void)
+
+ dir = opendir(BATTERY_DIR_PATH);
+ if ( !dir )
++ {
++ xenpmd_log(LOG_ERR, "Failed to open dir %s with error - %d\n",
++ BATTERY_DIR_PATH, errno);
+ return 0;
++ }
+
+ while ( get_next_battery_info_or_status(dir, BIF, (void *)&info) )
+ {
+-#ifdef RUN_STANDALONE
++#ifdef XENPMD_DEBUG
+ print_battery_info(&info);
+ #endif
+ if ( info.present == YES )
+ {
+ write_battery_info_to_xenstore(&info);
++ xenpmd_log(LOG_INFO, "One time battery information written to xenstore\n");
+ ret = 1;
+ break; /* rethink this... */
+ }
+@@ -402,16 +361,17 @@ int write_one_time_battery_info(void)
+ return ret;
+ }
+
+-#ifdef RUN_STANDALONE
++#ifdef XENPMD_DEBUG
void print_battery_status(struct battery_status *status)
{
- printf("present: %d\n", status->present);
+- printf("present: %d\n", status->present);
- printf("Battery state %d\n", status->state);
- printf("Battery present rate %d\n", status->present_rate);
- printf("Battery remining capacity %d\n", status->remaining_capacity);
- printf("Battery present voltage %d\n", status->present_voltage);
-+ printf("Battery state %d\n", (int) status->state);
-+ printf("Battery present rate %d\n", (int) status->present_rate);
-+ printf("Battery remining capacity %d\n",
++ xenpmd_log(LOG_DEBUG, "present: %d\n", status->present);
++ xenpmd_log(LOG_DEBUG, "Battery state %d\n", (int) status->state);
++ xenpmd_log(LOG_DEBUG, "Battery present rate %d\n", (int) status->present_rate);
++ xenpmd_log(LOG_DEBUG, "Battery remining capacity %d\n",
+ (int) status->remaining_capacity);
-+ printf("Battery present voltage %d\n", (int) status->present_voltage);
++ xenpmd_log(LOG_DEBUG, "Battery present voltage %d\n", (int) status->present_voltage);
}
- #endif /*RUN_STANDALONE*/
+-#endif /*RUN_STANDALONE*/
++#endif /*XENPMD_DEBUG*/
+
+ void write_battery_status_to_xenstore(struct battery_status *status)
+ {
+@@ -427,55 +387,78 @@ void write_battery_status_to_xenstore(struct battery_status *status)
+ write_ulong_lsb_first(val+26, status->present_voltage);
-@@ -429,46 +384,59 @@ void write_battery_status_to_xenstore(struct battery_status *status)
xs_write(xs, XBT_NULL, "/pm/bst", val, 35);
++#ifdef XENPMD_DEBUG
++ xenpmd_log(LOG_DEBUG, "Updated battery information in xenstore\n");
++#endif
}
-int wait_for_and_update_battery_status_request(void)
struct battery_status status;
- while ( true )
-- {
++ /* KN:@TODO - It is rather inefficient to not cache the file handle.
++ * Switch to caching file handle.
++ */
++ dir = opendir(BATTERY_DIR_PATH);
++ if ( !dir )
+ {
- /* KN:@TODO - It is rather inefficient to not cache the file handle.
- * Switch to caching file handle.
- */
- dir = opendir(BATTERY_DIR_PATH);
- if ( !dir )
- return 0;
-+ /* KN:@TODO - It is rather inefficient to not cache the file handle.
-+ * Switch to caching file handle.
-+ */
-+ dir = opendir(BATTERY_DIR_PATH);
-+ if ( !dir )
++ xenpmd_log(LOG_ERR, "opendir failed for directory %s with error - %d\n",
++ BATTERY_DIR_PATH, errno);
+ return;
++ }
- while ( get_next_battery_info_or_status(dir, BST, (void *)&status) )
- {
+-#ifdef RUN_STANDALONE
+- print_battery_status(&status);
+ while ( get_next_battery_info_or_status(dir, BST, (void *)&status) )
+ {
- #ifdef RUN_STANDALONE
-- print_battery_status(&status);
++#ifdef XENPMD_DEBUG
+ print_battery_status(&status);
#endif
- if ( status.present == YES )
- return ret;
+ closedir(dir);
+ return;
-+}
-+
+ }
+
+void wait_for_and_update_power_mgmt_info(void)
+{
+ char **buffer;
+ }
+ free(buffer);
+ }
- }
-
++}
++
+#ifndef RUN_STANDALONE
/* Borrowed daemonize from xenstored - Initially written by Stevens. */
static void daemonize(void)
{
-@@ -493,24 +461,51 @@ static void daemonize(void)
+ pid_t pid;
+
+ if ( (pid = fork()) < 0 )
++ {
++ xenpmd_log(LOG_ERR, "Failed to fork - %d\n", errno);
+ exit(1);
++ }
+
+ if ( pid != 0 )
+ exit(0);
+@@ -483,34 +466,73 @@ static void daemonize(void)
+ setsid();
+
+ if ( (pid = fork()) < 0 )
++ {
++ xenpmd_log(LOG_ERR, "Failed to fork - %d\n", errno);
+ exit(1);
++ }
+
+ if ( pid != 0 )
+ exit(0);
+
+ if ( chdir("/") == -1 )
++ {
++ xenpmd_log(LOG_ERR, "chdir failed with error - %d\n", errno);
+ exit(1);
++ }
umask(0);
}
- if ( xs == NULL )
- return -1;
+ if ( xs == NULL )
++ {
++ xenpmd_log(LOG_ERR, "Unable to get a handle to XS daemon\n");
+ return NULL;
++ }
- if ( write_one_time_battery_info() == 0 )
+ initialize_system_state_info();
{
xs_daemon_close(xs);
- return -1;
++ xenpmd_log(LOG_ERR, "Unable to write one time battery information\n");
+ return NULL;
}
+ acpi_events_cleanup();
xs_daemon_close(xs);
- return 0;
++ closelog();
+ return NULL;
}
+int main(int argc, char *argv[])
+{
+#ifndef RUN_STANDALONE
-+ daemonize();
++ openlog("xenpmd", 0, LOG_DAEMON);
++ daemonize();
+#endif
+
+ /* Refer to worker_thread_routine for why we create additional thread */
+}
diff --git a/tools/xenpmd/xenpmd.h b/tools/xenpmd/xenpmd.h
new file mode 100644
-index 0000000..778ba38
+index 0000000..211e847
--- /dev/null
+++ b/tools/xenpmd/xenpmd.h
-@@ -0,0 +1,107 @@
+@@ -0,0 +1,115 @@
+/*
+ * xenpmd.h
+ *
+#include <syslog.h>
+#include <sys/socket.h>
+#include <sys/un.h>
++#include <errno.h>
+#include <xs.h>
+
+/* #define RUN_STANDALONE */
+/* #define RUN_IN_SIMULATE_MODE */
++/* #define XENPMD_DEBUG */
+
+enum BATTERY_INFO_TYPE {
+ BIF,
+#define XS_PBTN_EVENT_PATH "/pm/events/powerbuttonpressed"
+#define XS_SBTN_EVENT_PATH "/pm/events/sleepbuttonpressed"
+
++#ifndef RUN_STANDALONE
++ #define xenpmd_log(priority, format, p...) syslog(priority, format, ##p)
++#else
++ #define xenpmd_log(priority, format, p...) printf(format, ##p)
++#endif
++