File: dstate-hal.c

package info (click to toggle)
nut 2.4.3-1.1squeeze2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,816 kB
  • ctags: 5,658
  • sloc: ansic: 45,735; sh: 11,290; makefile: 668; python: 448; perl: 179
file content (700 lines) | stat: -rw-r--r-- 19,965 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/* dstate-hal.c - Network UPS Tools driver-side state management
   This is a compatibility interface that encapsulate the HAL bridge
   into the NUT dstate API for NUT drivers

   Copyright (C) 2006-2007  Arnaud Quette <aquette.dev@gmail.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <stdio.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "common.h"
#include "config.h"
#include "dstate-hal.h"
#include "extstate.h"
/* #include "state.h"
#include "parseconf.h" */

#include <hal/libhal.h>

/* FIXME: export command and RW variables (using the HAL example: addon-cpufreq and macbook addon) */ 
/* beeper.enable, beeper.disable => SetBeeper(bool)
   beeper.toggle => ToggleBeeper(void)

org.freedesktop.Hal.Device.UPS.SetSounder (bool)


Shutdown() or ShutOff() 
	shutdown.return
	shutdown.stayoff
	shutdown.reboot
	shutdown.reboot.graceful
	
#define UPS_ERROR_GENERAL                   "GeneralError"	
#define UPS_ERROR_UNSUPPORTED_FEATURE       "FeatureNotSupported"	
#define UPS_ERROR_PERMISSION_DENIED         "PermissionDenied"
  
****** implementation *******

#define DBUS_INTERFACE "org.freedesktop.Hal.Device.UPS"

if (!libhal_device_claim_interface(halctx, udi, DBUS_INTERFACE, 

	"    <method name=\"Shutdown\">\n"
	"      <arg name=\"shutdown_type\" direction=\"in\" type=\"s\"/>\n"
	"      <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
	"    </method>\n"

	&dbus_dbus_error)) {
		fprintf(stderr, "Cannot claim interface: %s", dbus_dbus_error.message);
		goto Error;
	}	


*/

/*
 *	static	int	sockfd = -1, stale = 1, alarm_active = 0;
 *	static	struct	st_tree_t	*dtree_root = NULL;
 *	static	struct	conn_t	*connhead = NULL;
 *	static	struct	cmdlist_t *cmdhead = NULL;
 *	static	char	*sockfn = NULL;
 *	static	char	status_buf[ST_MAX_VALUE_LEN], 
 *			alarm_buf[ST_MAX_VALUE_LEN];
 */
 
struct	ups_handler	upsh;

LibHalContext *halctx = NULL;
char *udi;
int ac_present = 0; /* 0 = false ; 1 = true */
extern char *dbus_methods_introspection;

static void* runtime_handler(LibHalChangeSet *cs, char* runtime);
static void* level_handler(LibHalChangeSet *cs, char* critical_level);
static void* battery_type_handler(LibHalChangeSet *cs, char* battery_type);

/* Structure to lookup between NUT and HAL data */
typedef struct {
	char	*nut_name;				/* NUT variable name */
	char	*hal_name;				/* HAL variable name */
	int		hal_type;				/* HAL variable type */
	void    *(*fun)(LibHalChangeSet *cs, 
					char *value);	/* conversion function. */
} info_lkp_t;

enum hal_type_t
{
	NONE = 0,
	HAL_TYPE_INT,
	HAL_TYPE_BOOL,
	HAL_TYPE_STRING
};

/* Structure to lookup between NUT commands and HAL/DBus methods */
typedef struct {
	char	*nut_name;				/* NUT command name */
	char	*hal_name;				/* HAL/DBus method name */
	char	*xml_introspection;		/* HAL/DBus method introspection */
/* FIXME: how to lookup param values between HAL and NUT?? */
/*	void    *(*fun)(LibHalChangeSet *cs, 
					char *value);*/	/* NUT function */
} method_lkp_t;

#if 0
/* Data to lookup between NUT commands and HAL/DBus methods
 * for dstate_addcmd() */
static method_lkp_t nut2hal_cmd[] =
{
	/* ups.status is handled by status_set() calls */
	{
		"beeper.enable",
		"SetBeeper",
		"    <method name=\"SetBeeper\">\n"
		"      <arg name=\"beeper_mode\" direction=\"in\" type=\"b\"/>\n"
		"      <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
		"    </method>\n"
 	},
 
	/* Terminating element */
	{ NULL, NULL, NULL }
};
#endif

/* Data to lookup between NUT and HAL for dstate_setinfo() */
static info_lkp_t nut2hal_info[] =
{
	/* ups.status is handled by status_set() calls */
	{ "battery.charge.low", "battery.charge_level.low", HAL_TYPE_INT, *level_handler },
	/* { "battery.charge.low", "battery.reporting.low", HAL_TYPE_INT, NULL }, */
	{ "battery.charge.low", "battery.alarm.design", HAL_TYPE_INT, NULL },
	
	{ "battery.charge", "battery.charge_level.current", HAL_TYPE_INT, NULL },
	{ "battery.charge", "battery.charge_level.percentage", HAL_TYPE_INT, NULL },
	{ "battery.charge", "battery.reporting.current", HAL_TYPE_INT, NULL },
	{ "battery.charge", "battery.reporting.percentage", HAL_TYPE_INT, NULL },
	{ "battery.runtime", "battery.remaining_time", HAL_TYPE_INT, *runtime_handler },
	/* raw version (PbAc) */
	{ "battery.type", "battery.reporting.technology", HAL_TYPE_STRING, NULL },
	/* Human readable version */
	{ "battery.type", "battery.technology", HAL_TYPE_STRING, *battery_type_handler },
	
	/* AQ note: Not sure it fits!	*/
	/* HAL marked as mandatory! */
	{ "battery.voltage", "battery.voltage.current", HAL_TYPE_INT, NULL },
	{ "battery.voltage.nominal", "battery.voltage.design", HAL_TYPE_INT, NULL },

	{ "ups.mfr", "battery.vendor", HAL_TYPE_STRING, NULL },
	{ "ups.model", "battery.model", HAL_TYPE_STRING, NULL },
	{ "ups.serial", "battery.serial", HAL_TYPE_STRING, NULL },

	/* Terminating element */
	{ NULL, NULL, NONE, NULL }
};

/* Functions to lookup between NUT and HAL */
static info_lkp_t *find_nut_info(const char *nut_varname, info_lkp_t *prev_info_item);

/* HAL accessors wrappers */
void hal_set_string(LibHalChangeSet *cs, const char *key, const char *value);
void hal_set_int(LibHalChangeSet *cs, const char *key, const int value);
void hal_set_bool(LibHalChangeSet *cs, const char *key, const dbus_bool_t value);
int hal_get_int(const char *key);
char *hal_get_string(const char *key);

/* Handle warning charge level according to the critical level */
static void* level_handler(LibHalChangeSet *cs, char* critical_level)
{
	/* Magic formula to generate the warning level */
	int int_critical_level = atoi(critical_level);
	/* warning level = critical + 1/3 of (100 % - critical level), approx at the leat mod 10 */
	int int_warning_level = int_critical_level + ((100 - int_critical_level) / 3);
	int_warning_level -= (int_warning_level % 10);

	/* Set the critical level value */
	hal_set_int (cs, "battery.charge_level.low", int_critical_level);
	hal_set_int (cs, "battery.reporting.low", int_critical_level);
	
	/* Set the warning level value (FIXME: set to 50 % for now) */
	hal_set_int (cs, "battery.charge_level.warning", int_warning_level);
	hal_set_int (cs, "battery.reporting.warning", int_warning_level);

	return NULL; /* Nothing to return */
}

/* Handle runtime exposition according to the AC status */
static void* runtime_handler(LibHalChangeSet *cs, char* runtime)
{
	if (ac_present == 0) {
		/* unSet the runtime auto computation and rely upon NUT.battery.runtime*/
		hal_set_bool (cs, "battery.remaining_time.calculate_per_time", FALSE);

		/* Set the runtime value */
		hal_set_int (cs, "battery.remaining_time", atoi(runtime));
	}
	else {
		/* Set the runtime auto computation */
		hal_set_bool (cs, "battery.remaining_time.calculate_per_time", TRUE);

		/* Set the runtime value */
		hal_set_int (cs, "battery.remaining_time", 0);
	}
	return NULL; /* Nothing to return */
}

/* Handle the battery technology reporting */
static void* battery_type_handler(LibHalChangeSet *cs, char* battery_type)
{
	if (!strncmp (battery_type, "PbAc", 4)) {
		hal_set_string(cs, "battery.technology", "lead-acid");
	}
	/* FIXME: manage other types (lithium-ion, lithium-polymer,
	 *  nickel-metal-hydride, unknown */

	return NULL; /* Nothing to return */
}

/********************************************************************
 * dstate compatibility interface
 *******************************************************************/
void dstate_init(const char *prog, const char *port)
{
	DBusError dbus_error;
	LibHalChangeSet *cs;

	dbus_error_init (&dbus_error);

	cs = libhal_device_new_changeset (udi);
	if (cs == NULL) {
		fatalx (EXIT_FAILURE, "Cannot initialize changeset");
	}

	/* UPS always report charge as percent */
	hal_set_string (cs, "battery.charge_level.unit", "percent");
	hal_set_string (cs, "battery.reporting.unit", "percent");
	hal_set_string (cs, "battery.alarm.unit", "percent");

	/* Various UPSs assumptions */
	/****************************/
	/* UPS are always rechargeable! */
	/* FIXME: Check for NUT extension however: ie HID->UPS.PowerSummary.Rechargeable
	 * into battery.rechargeable
	 * or always expose it?
	 */
	hal_set_bool (cs, "battery.is_rechargeable", TRUE);

	/* UPS always has a max battery charge of 100 % */
	hal_set_int (cs, "battery.charge_level.design", 100);
	hal_set_int (cs, "battery.charge_level.last_full", 100);
	hal_set_int (cs, "battery.reporting.design", 100);
	hal_set_int (cs, "battery.reporting.last_full", 100);

	/* NUT always express Voltage in Volts "V" */
	/* But not all UPSs provide the data
	 * battery.voltage.{design,current} */	
	hal_set_string (cs, "battery.voltage.unit", "V");

	/* UPS always have a battery! */
	/* Note(AQU): wrong with some solar panel usage, where the UPS */
	/* is just an energy source switch! */
	/* FIXME: to be processed (need possible NUT extension) */
	hal_set_bool (cs, "battery.present", TRUE);

	/* FIXME: can be improved?! (implies "info.recall.vendor") */
	hal_set_bool (cs, "info.is_recalled", FALSE);

	/* Set generic properties */
	hal_set_string (cs, "battery.type", "ups");
	libhal_device_add_capability (halctx, udi, "battery", &dbus_error);
	libhal_device_add_capability (halctx, udi, "ac_adaptor", &dbus_error);
	
	/* FIXME: can be improved?! Set granularity (1 %)*/
	hal_set_int (cs, "battery.charge_level.granularity_1", 1);
	hal_set_int (cs, "battery.charge_level.granularity_2", 1);
	hal_set_int (cs, "battery.reporting.granularity_1", 1);
	hal_set_int (cs, "battery.reporting.granularity_2", 1);
	
	dbus_error_init (&dbus_error);
	/* NOTE: commit_changeset won't do IPC if set is empty */
	libhal_device_commit_changeset (halctx, cs, &dbus_error);
	libhal_device_free_changeset (cs);
	
	if (dbus_error_is_set (&dbus_error))
		dbus_error_free (&dbus_error);
}


const char *dstate_getinfo(const char *var)
{
	info_lkp_t *nut2hal_info = find_nut_info(var, NULL);

/* FIXME: use the data exposed by NUT on the DBus when available */
	
	if (nut2hal_info != NULL) {

		switch (nut2hal_info->hal_type)
		{
/*			case HAL_TYPE_INT:
				static char	value[8];
				snprintf(value, sizeof(value), "%i", hal_get_int(nut2hal_info->hal_name));
				return value;
			case HAL_TYPE_BOOL:
				hal_set_bool(cs, nut2hal_info->hal_name, TRUE);
				break;
*/
			case HAL_TYPE_STRING:
				return hal_get_string(nut2hal_info->hal_name);
		}
	}	
	return NULL;
}

int dstate_setinfo(const char *var, const char *fmt, ...)
{
	va_list	ap;
	int	ret = 1;
	LibHalChangeSet *cs;
	DBusError dbus_error;
	char	value[ST_MAX_VALUE_LEN];
	info_lkp_t *nut2hal_info = NULL, *prev_nut2hal_info = NULL;

	va_start(ap, fmt);
	vsnprintf(value, sizeof(value), fmt, ap);
	va_end(ap);

	cs = libhal_device_new_changeset (udi);
	if (cs == NULL) {
		fatalx (EXIT_FAILURE, "Cannot initialize changeset");
	}

	/* Loop on getting HAL variable(s) matching this NUT variable */
	while ( (nut2hal_info = find_nut_info(var, prev_nut2hal_info)) != NULL)
	{
		upsdebugx(2, "dstate_setinfo: %s => %s (%s)\n", var, nut2hal_info->hal_name, value);

		if (nut2hal_info->fun != NULL)
			nut2hal_info->fun(cs, value);
		else {
			switch (nut2hal_info->hal_type)
			{
				case HAL_TYPE_INT:
					hal_set_int(cs, nut2hal_info->hal_name, atoi(value));
					break;
				case HAL_TYPE_BOOL:
					/* FIXME: howto lookup TRUE/FALSE? */
					hal_set_bool(cs, nut2hal_info->hal_name, TRUE);
					break;
				case HAL_TYPE_STRING:
					hal_set_string(cs, nut2hal_info->hal_name, value);
					break;
			}
		}
		prev_nut2hal_info = nut2hal_info;
	}

	dbus_error_init (&dbus_error);
	/* NOTE: commit_changeset won't do IPC if set is empty */
	libhal_device_commit_changeset(halctx,cs,&dbus_error);
	libhal_device_free_changeset (cs);

	if (dbus_error_is_set (&dbus_error))
		dbus_error_free (&dbus_error);

	return ret;
}


int dstate_addenum(const char *var, const char *fmt, ...)
{
	return 0;
}
	
int dstate_delinfo(const char *var)
{
	return 0;
}

int dstate_delcmd(const char *var)
{
	return 0;
}

void dstate_setflags(const char *var, int flags)
{
	return;
}

void dstate_setaux(const char *var, int aux)
{
	return;
}

void dstate_dataok(void)
{
	return;
}

void dstate_datastale(void)
{
	return;
}

void dstate_free(void)
{
	return;
}

/* extrafd: provided for waking up based on the driver's UPS fd */
int dstate_poll_fds(struct timeval timeout, int extrafd)
{
	/* drivers expect us to limit the polling rate here */
	sleep(timeout.tv_sec);

	/* the timeout expired */
	return 1;
}

/* clean out the temp space for a new pass */
void status_init(void)
{
	/* Nothing to do */
	return;
}

/* ups.status element conversion */
void status_set(const char *buf)
{
	upsdebugx(2, "status_set: %s", buf);

	/* Note: only usbhid-ups supported devices expose [DIS]CHRG status */
	/* along with the standard OL (online) / OB (on battery) status! */
	if ( (strcmp(buf, "DISCHRG") == 0) || (strcmp(buf, "OB") == 0) )
	{
		ac_present = 0;
	}
	else if ( (strcmp(buf, "CHRG") == 0) || (strcmp(buf, "OL") == 0) )
	{
		ac_present = 1;
	}
	else 
		upsdebugx(2, "status_set: dropping status %s (not managed)\n", buf);

	return;
}

/* write the status_buf into the externally visible dstate storage */
void status_commit(void)
{
	LibHalChangeSet *cs;
	DBusError dbus_error;
	int curlevel, warnlevel, lowlevel;

	upsdebugx(2, "status_commit");

	cs = libhal_device_new_changeset (udi);
	if (cs == NULL) {
		fatalx (EXIT_FAILURE, "Cannot initialize changeset");
	}

	/* Retrieve the levels */
	curlevel = hal_get_int ("battery.charge_level.current");
	warnlevel = hal_get_int ("battery.charge_level.warning");
	lowlevel = hal_get_int ("battery.charge_level.low");

	/* Set AC present status */
	/* Note: UPSs are also AC adaptors! */
	hal_set_bool (cs, "ac_adaptor.present", (ac_present == 0)?FALSE:TRUE);

	/* Set discharging status */
	hal_set_bool (cs, "battery.rechargeable.is_discharging", (ac_present == 0)?TRUE:FALSE);

	/* Set charging status */
	if (curlevel != 100)
		hal_set_bool (cs, "battery.rechargeable.is_charging", (ac_present == 0)?FALSE:TRUE);

	/* Set the battery status (FIXME: are these values valid?) */
	if (curlevel <= lowlevel)
		hal_set_string(cs, "battery.charge_level.capacity_state", "critical");
	else if (curlevel <= warnlevel)
		hal_set_string(cs, "battery.charge_level.capacity_state", "warning"); /*low?*/
	else
		hal_set_string(cs, "battery.charge_level.capacity_state", "ok");

	dbus_error_init (&dbus_error);
	/* NOTE: commit_changeset won't do IPC if set is empty */
	libhal_device_commit_changeset (halctx, cs, &dbus_error);
	libhal_device_free_changeset (cs);
	
	if (dbus_error_is_set (&dbus_error))
		dbus_error_free (&dbus_error);

	return;
}

/* similar functions for ups.alarm */
void alarm_init(void)
{
	return;
}

void alarm_set(const char *buf)
{
	return;
}

void alarm_commit(void)
{
	return;
}

/* FIXME: complete and use nut2hal_cmd */
/* Register DBus methods, by feeling the methods buffer */
void dstate_addcmd(const char *cmdname)
{
	DBusError dbus_error;
	dbus_error_init (&dbus_error);

	upsdebugx(2, "dstate_addcmd: %s\n", cmdname);

	/* beeper.{on,off} */
	/* FIXME: how to deal with beeper.toggle */
	if (!strncasecmp(cmdname, "beeper.disable", 14))
	{
		strcat(dbus_methods_introspection,
		"    <method name=\"SetBeeper\">\n"
		"      <arg name=\"beeper_mode\" direction=\"in\" type=\"b\"/>\n"
		"      <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
		"    </method>\n");
	}
}



/*******************************************************************
 * internal functions
 *******************************************************************/

/****************
 * HAL wrappers *
 ****************/
/* Only update HAL string values if there are real changes */
void hal_set_string(LibHalChangeSet *cs, const char *key, const char *value)
{
	DBusError dbus_error;
	char *new_value = NULL;

	upsdebugx(2, "hal_set_string: %s => %s", key, value);

	dbus_error_init(&dbus_error);

	/* Check if the property already exists */
	if (libhal_device_property_exists (halctx, udi, key, &dbus_error) == TRUE) {
		
		new_value = libhal_device_get_property_string (halctx, udi,
				key, &dbus_error);

		/* Check if the value has really changed */
		if (strcmp(value, new_value))
			libhal_changeset_set_property_string (cs, key, value);
			
		/* Free the new_value string */
		if (new_value != NULL)
			libhal_free_string (new_value);
	}
	else {
		libhal_changeset_set_property_string (cs, key, value);
	}	
}

/* Only update HAL int values if there are real changes */
void hal_set_int(LibHalChangeSet *cs, const char *key, const int value)
{
	DBusError dbus_error;
	int new_value;

	upsdebugx(2, "hal_set_int: %s => %i", key, value);

	dbus_error_init(&dbus_error);

	/* Check if the property already exists */
	if (libhal_device_property_exists (halctx, udi, key, &dbus_error) == TRUE) {
		
		new_value = libhal_device_get_property_int (halctx, udi,
				key, &dbus_error);

		/* Check if the value has really changed */
		if (value != new_value)
			libhal_changeset_set_property_int (cs, key, value);
	}
	else {
		libhal_changeset_set_property_int (cs, key, value);
	}	
}

char *hal_get_string(const char *key)
{
	DBusError dbus_error;
	char *value = NULL;

	upsdebugx(2, "hal_get_string: %s", key);

	dbus_error_init(&dbus_error);

	/* Check if the property already exists */
	if (libhal_device_property_exists (halctx, udi, key, &dbus_error) == TRUE) {
		
		value = libhal_device_get_property_string (halctx, udi,
				key, &dbus_error);
	}
	return value;
}

int hal_get_int(const char *key)
{
	DBusError dbus_error;
	int value = -1;

	upsdebugx(2, "hal_get_int: %s", key);

	dbus_error_init(&dbus_error);

	/* Check if the property already exists */
	if (libhal_device_property_exists (halctx, udi, key, &dbus_error) == TRUE) {
		
		value = libhal_device_get_property_int (halctx, udi,
				key, &dbus_error);
	}
	return value;
}

/* Only update HAL int values if there are real changes */
void hal_set_bool(LibHalChangeSet *cs, const char *key, const dbus_bool_t value)
{
	DBusError dbus_error;
	dbus_bool_t new_value;

	upsdebugx(2, "hal_set_bool: %s => %s", key, (value==TRUE)?"true":"false");
	
	dbus_error_init(&dbus_error);

	/* Check if the property already exists */
	if (libhal_device_property_exists (halctx, udi, key, &dbus_error) == TRUE) {
		
		new_value = libhal_device_get_property_bool (halctx, udi,
				key, &dbus_error);

		/* Check if the value has really changed */
		if (value != new_value)
			libhal_changeset_set_property_bool (cs, key, value);
	}
	else {
		libhal_changeset_set_property_bool (cs, key, value);
	}	
}

/* find the next info element definition in info array
 * that matches nut_varname, and that is after prev_info_item
 * if specified.
 * Note that 1 nut item can matches several HAL items
 */
static info_lkp_t *find_nut_info(const char *nut_varname, info_lkp_t *prev_info_item)
{
	info_lkp_t *info_item;

	upsdebugx(2, "find_nut_info: looking up => %s\n", nut_varname);
	
	if (prev_info_item != NULL) {
		/* Start from the item following prev_info_item */
		info_item = ++prev_info_item;
	}
	else {
		info_item = nut2hal_info;
	}

	for ( ; info_item != NULL && info_item->nut_name != NULL ; info_item++) {

		if (!strcasecmp(info_item->nut_name, nut_varname))
			return info_item;
	}

	return NULL;
}