17b840e52Sphitran /***************************************************************************
27b840e52Sphitran  *
3d2ec54f7Sphitran  * probe-acpi.c : Probe for ACPI device information
47b840e52Sphitran  *
5*2d6b5ea7SGuoli Shu  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
67b840e52Sphitran  * Use is subject to license terms.
77b840e52Sphitran  *
87b840e52Sphitran  * Licensed under the Academic Free License version 2.1
97b840e52Sphitran  *
107b840e52Sphitran  **************************************************************************/
117b840e52Sphitran 
127b840e52Sphitran #ifdef HAVE_CONFIG_H
137b840e52Sphitran #include <config.h>
147b840e52Sphitran #endif
157b840e52Sphitran 
167b840e52Sphitran #include <errno.h>
177b840e52Sphitran #include <string.h>
187b840e52Sphitran #include <strings.h>
197b840e52Sphitran #include <ctype.h>
207b840e52Sphitran #include <stdlib.h>
217b840e52Sphitran #include <stdio.h>
227b840e52Sphitran #include <sys/ioctl.h>
237b840e52Sphitran #include <fcntl.h>
247b840e52Sphitran #include <unistd.h>
257b840e52Sphitran #include <glib.h>
267b840e52Sphitran 
277b840e52Sphitran #include <libhal.h>
287b840e52Sphitran #include <logger.h>
29d2ec54f7Sphitran #include "../utils/acpi.h"
307b840e52Sphitran 
317b840e52Sphitran int
main(int argc,char * argv[])327b840e52Sphitran main(int argc, char *argv[])
337b840e52Sphitran {
347b840e52Sphitran 	int ret = 1;
357b840e52Sphitran 	int fd = -1;
367b840e52Sphitran 	char *udi;
377b840e52Sphitran 	char device_file[HAL_PATH_MAX] = "/devices";
387b840e52Sphitran 	char *devfs_path;
397b840e52Sphitran 	LibHalContext *ctx = NULL;
407b840e52Sphitran 	DBusError error;
417b840e52Sphitran 
427b840e52Sphitran 	if ((udi = getenv("UDI")) == NULL)
437b840e52Sphitran 		goto out;
447b840e52Sphitran 	if ((devfs_path = getenv("HAL_PROP_SOLARIS_DEVFS_PATH")) == NULL)
457b840e52Sphitran 		goto out;
467b840e52Sphitran 	strlcat(device_file, devfs_path, HAL_PATH_MAX);
477b840e52Sphitran 
487b840e52Sphitran 	setup_logger();
497b840e52Sphitran 
507b840e52Sphitran 	dbus_error_init(&error);
517b840e52Sphitran 	if ((ctx = libhal_ctx_init_direct(&error)) == NULL)
527b840e52Sphitran 		goto out;
537b840e52Sphitran 
54d2ec54f7Sphitran 	HAL_DEBUG(("Doing probe-acpi for %s (udi=%s)",
557b840e52Sphitran 	    device_file, udi));
567b840e52Sphitran 
577b840e52Sphitran 	if ((fd = open(device_file, O_RDONLY | O_NONBLOCK)) < 0) {
587b840e52Sphitran 		HAL_DEBUG(("Cannot open %s: %s", device_file, strerror(errno)));
597b840e52Sphitran 		goto out;
607b840e52Sphitran 	}
61d2ec54f7Sphitran 	if (strstr(udi, "_ac")) {
627b840e52Sphitran 		ac_adapter_update(ctx, udi, fd);
63d2ec54f7Sphitran 	} else if (strstr(udi, "_battery")) {
647b840e52Sphitran 		battery_update(ctx, udi, fd);
65d2ec54f7Sphitran 	} else if (strstr(udi, "_lid")) {
66d2ec54f7Sphitran 		lid_update(ctx, udi, fd);
67*2d6b5ea7SGuoli Shu 	} else if (strstr(udi, "_hotkey")) {
68d2ec54f7Sphitran 		laptop_panel_update(ctx, udi, fd);
697b840e52Sphitran 	}
707b840e52Sphitran 
717b840e52Sphitran 	ret = 0;
727b840e52Sphitran 
737b840e52Sphitran out:
747b840e52Sphitran 	if (fd >= 0) {
757b840e52Sphitran 		close(fd);
767b840e52Sphitran 	}
777b840e52Sphitran 
787b840e52Sphitran 	if (ctx != NULL) {
797b840e52Sphitran 		libhal_ctx_shutdown(ctx, &error);
807b840e52Sphitran 		libhal_ctx_free(ctx);
817b840e52Sphitran 		if (dbus_error_is_set(&error)) {
827b840e52Sphitran 			dbus_error_free(&error);
837b840e52Sphitran 		}
847b840e52Sphitran 	}
857b840e52Sphitran 
867b840e52Sphitran 	return (ret);
877b840e52Sphitran }
88