File: libacpi.c

package info (click to toggle)
libacpi 0.2-5
  • links: PTS
  • area: main
  • in suites: buster
  • size: 1,024 kB
  • sloc: ansic: 867; makefile: 82; sh: 19
file content (740 lines) | stat: -rw-r--r-- 20,315 bytes parent folder | download | duplicates (3)
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
 * (C)opyright 2007 Nico Golde <nico@ngolde.de>
 * See LICENSE file for license details
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <stddef.h>

#include "libacpi.h"
#include "list.h"


static int read_acpi_battinfo(const int num, const int sysstyle);
static int read_acpi_battalarm(const int num, const int sysstyle);
static int read_acpi_battstate(const int num);
static void read_acpi_thermalzones(global_t *globals);

typedef struct {
	char * value;
	size_t offset;
} acpi_value_t;

static acpi_value_t
battinfo_values[] = {
	{ "last full capacity:", offsetof(battery_t, last_full_cap) },
	{ "design voltage:", offsetof(battery_t, design_voltage) },
	{ "design capacity warning:", offsetof(battery_t, design_warn) },
	{ "design capacity low:", offsetof(battery_t, design_low) },
	{ "capacity granularity 1:", offsetof(battery_t, design_level1) },
	{ "capacity granularity 2:", offsetof(battery_t, design_level2) },
	{ NULL, 0 }
};

static acpi_value_t
battstate_values[] = {
	{ "present rate:", offsetof(battery_t, present_rate) },
	{ "remaining capacity:", offsetof(battery_t, remaining_cap) },
	{ "present voltage:", offsetof(battery_t, present_voltage) },
	{ NULL, 0 }
};

/* given a buffer for example from a file, search for key
 * and return a pointer to the value of it. On error return NULL*/
static char *
scan_acpi_value(const char *buf, const char *key){
	char *ptr = NULL;
	char *tmpbuf = NULL;
	char *tmpkey = NULL;
	char *tmpval = NULL;

	if((tmpbuf = strdup(buf)) == NULL)
		return NULL;

	/* jump to the key in buffer */
	if((tmpkey = strstr(tmpbuf, key))) {
		/* jump behind the key, whitespaces and tabs */
		for(tmpkey += strlen(key); *tmpkey && (*tmpkey == ' ' || *tmpkey == '\t'); tmpkey++);
		for(tmpval = tmpkey; *tmpval && *tmpval != ' ' &&
				*tmpval != '\t' && *tmpval != '\n' &&
				*tmpval != '\r'; tmpval++);
		if(tmpval)
			*tmpval = '\0';

		if((ptr = strdup(tmpkey)) == NULL) {
			free(tmpbuf);
			return NULL;
		}
	}
	free(tmpbuf);
	return ptr;
}

/* reads a file into a buffer and returns a pointer to it, or NULL on error */
static char *
get_acpi_content(const char *file){
	FILE *input = NULL;
	char *buf = NULL;
	int read = 0;

	if((buf = malloc(MAX_BUF + 1)) == NULL)
		return NULL;
	if((input = fopen(file, "r")) == NULL)
		return NULL;

	read = fread(buf, 1, MAX_BUF, input);
	if(read > 0) buf[read - 1] = '\0';
	else buf[0] = '\0'; /* I would consider it a kernel bug if that happens */

	fclose(input);
	return buf;
}

/* returns the acpi version or NOT_SUPPORTED(negative value) on failure */
static int
get_acpi_version(void){
	long ret = -1;
	char *tmp = get_acpi_content(PROC_ACPI "info");
	char *version = NULL;
	
	if(!tmp) {
		tmp = get_acpi_content("/sys/module/acpi/parameters/acpica_version");
		if (tmp) {
			long ret = strtol(tmp, NULL, 10);
			free(tmp);
			return ret;
		} else {
			return NOT_SUPPORTED;
		}
	}
	if((version = scan_acpi_value(tmp, "version:")) == NULL){
		free(tmp);
		return NOT_SUPPORTED;
	}
	ret = strtol(version, NULL, 10);
	free(tmp);
	free(version);
	return ret;
}

/* check if acpi is supported on the system, return 0 on success
 * and -1 if not */
int
check_acpi_support(void){
	int version = get_acpi_version();

	/* we don't support 2.4 kernel versions TODO */
	if(version == NOT_SUPPORTED || version < 20020214)
		return NOT_SUPPORTED;
	return SUCCESS;
}

/* reads existent battery directories and starts to fill the battery
 * structure. Returns 0 on success, negative values on error */
int
init_acpi_batt(global_t *globals){
	char *names[MAX_ITEMS];
	battery_t *binfo;
	list_t *lst = NULL;
	node_t *node = NULL;
	int i = 0;

	globals->batt_count = 0;
	globals->sysstyle = 0;
	if((lst = dir_list(PROC_ACPI "battery")) == NULL || !lst->top)
	{
		/* check for new Linux 2.6.24+ layout */
		if((lst = dir_list(SYS_POWER)) == NULL || !lst->top)
			return NOT_SUPPORTED;
		else
			globals->sysstyle = 1;
	}
	for(node = lst->top; node; node=node->next){
		/* Skip non-battery AC power */
		if (0 == strcmp("AC", node->name))
			continue;
		if((names[globals->batt_count] = strdup(node->name)) == NULL){
			delete_list(lst);
			return ALLOC_ERR;
		}
		globals->batt_count++;
	}

	if(globals->batt_count > MAX_ITEMS) return ITEM_EXCEED;

	/* A quick insertion sort, to sort battery names */
	{
		char *tmp1, *tmp2;
		int x,y;
		for (x = 1; x < globals->batt_count; x++) {
			tmp1 = names[x];
			y = x - 1;
			while ((y >= 0) && ((strcmp (tmp1, names[y])) < 0)) {
				tmp2 = names[y + 1];
				names[y + 1] = names[y];
				names[y] = tmp2;
			}
		}
	}

	for (i=0; i < globals->batt_count && i < MAX_ITEMS; i++){
		binfo = &batteries[i];
		snprintf(binfo->name, MAX_NAME, "%s", names[i]);
		if(globals->sysstyle)
		{
			snprintf(binfo->state_file, MAX_NAME, "/%s/present", names[i]);
			snprintf(binfo->info_file, MAX_NAME, SYS_POWER "/%s", names[i]);
			snprintf(binfo->alarm_file, MAX_NAME, SYS_POWER "/%s/alarm", names[i]);
		}
		else
		{
			snprintf(binfo->state_file, MAX_NAME, PROC_ACPI "battery/%s/state", names[i]);
			snprintf(binfo->info_file, MAX_NAME, PROC_ACPI "battery/%s/info", names[i]);
			snprintf(binfo->alarm_file, MAX_NAME, PROC_ACPI "battery/%s/alarm", names[i]);
		}
		read_acpi_battinfo(i, globals->sysstyle);
		read_acpi_battalarm(i, globals->sysstyle);
		free(names[i]);
	}
	delete_list(lst);
	return SUCCESS;
}

/* reads the acpi state and writes it into the globals structure, void */
void
read_acpi_acstate(global_t *globals){
	adapter_t *ac = &globals->adapt;
	char *buf = NULL;
	char *tmp = NULL;

	if(ac->state_file && (buf = get_acpi_content(ac->state_file)) == NULL){
		ac->ac_state = P_ERR;
		return;
	}
	if(globals->sysstyle)
	{
		if(!strcmp(buf, "1"))
			ac->ac_state = P_AC;
		else if(!strcmp(buf, "0"))
			ac->ac_state = P_BATT;
		else ac->ac_state = P_ERR;
	}
	else
	{
		if((tmp = scan_acpi_value(buf, "state:")) && !strncmp(tmp, "on-line", 7))
			ac->ac_state = P_AC;
		else if(tmp && !strncmp(tmp, "off-line", 8))
			ac->ac_state = P_BATT;
		else ac->ac_state = P_ERR;
	}
	free(buf);
	free(tmp);
}

/* reads the name of the ac-adapter directory and fills the adapter_t
 * structure with the name and the state-file. Return 0 on success, negative values on errors */
int
init_acpi_acadapt(global_t *globals){
	list_t *lst = NULL;
	adapter_t *ac = &globals->adapt;

	globals->sysstyle = 0;
	if((lst = dir_list(PROC_ACPI "ac_adapter")) == NULL || !lst->top)
	{
		if((lst = dir_list(SYS_POWER "/AC")) == NULL || !lst->top)
			return NOT_SUPPORTED;
		else
			globals->sysstyle = 1;
	}
	if((!lst->top->name || ((ac->name = strdup(lst->top->name)) == NULL))){
		delete_list(lst);
		return ALLOC_ERR;
	}
	if(globals->sysstyle)
		snprintf(ac->state_file, MAX_NAME, SYS_POWER "/AC/online");
	else
		snprintf(ac->state_file, MAX_NAME, PROC_ACPI "ac_adapter/%s/state", ac->name);
	delete_list(lst);
	read_acpi_acstate(globals);
	return SUCCESS;
}

/* read acpi information for fan num, returns 0 on success and negative values on errors */
int
read_acpi_fan(const int num){
	char *buf = NULL;
	char *tmp = NULL;
	fan_t *info = &fans[num];

	if(num > MAX_ITEMS) return ITEM_EXCEED;

	/* scan state file */
	if((buf = get_acpi_content(info->state_file)) == NULL)
		info->fan_state = F_ERR;

	if(!buf || (tmp = scan_acpi_value(buf, "status:")) == NULL){
		info->fan_state = F_ERR;
		return NOT_SUPPORTED;
	}
	if (tmp[0] == 'o' && tmp[1] == 'n') info->fan_state = F_ON;
	else if(tmp[0] == 'o' && tmp[1] == 'f') info->fan_state = F_OFF;
	else info->fan_state = F_ERR;
	free(buf);
	free(tmp);
	return SUCCESS;
}

/* read all fans, fill the fan structures */
static void
read_acpi_fans(global_t *globals){
	int i;
	for(i = 0; i < globals->fan_count; i++)
		read_acpi_fan(i);
}

/* reads the names of the fan directories, fills fan_t,
 * return 0 on success, negative values on errors */
int
init_acpi_fan(global_t *globals){
	char *names[MAX_ITEMS];
	list_t *lst = NULL;
	node_t *node = NULL;
	int i = 0;
	fan_t *finfo = NULL;
	globals->fan_count = 0;

	if((lst = dir_list(PROC_ACPI "fan")) == NULL || !lst->top)
		return NOT_SUPPORTED;
	for(node = lst->top; node; node = node->next){
		if((names[globals->fan_count] = strdup(node->name)) == NULL){
			delete_list(lst);
			return ALLOC_ERR;
		}
		globals->fan_count++;
	}

	if(globals->fan_count > MAX_ITEMS) return ITEM_EXCEED;

	for (; i < globals->fan_count && i < MAX_ITEMS; i++){
		finfo = &fans[i];
		snprintf(finfo->name, MAX_NAME, "%s", names[i]);
		snprintf(finfo->state_file, MAX_NAME, PROC_ACPI "fan/%s/state", names[i]);
		free(names[i]);
	}
	delete_list(lst);
	read_acpi_fans(globals);
	return SUCCESS;
}

/* reads the name of the thermal-zone directory and fills the adapter_t
 * structure with the name and the state-file. Return 0 on success, negative values on errors */
int
init_acpi_thermal(global_t *globals){
	char *names[MAX_ITEMS];
	list_t *lst = NULL;
	node_t *node = NULL;
	thermal_t *tinfo = NULL;
	int i = 0;
	globals->thermal_count = 0;

	if((lst = dir_list(PROC_ACPI "thermal_zone")) == NULL)
		return NOT_SUPPORTED;
	for(node = lst->top; node; node = node->next){
		if((names[globals->thermal_count] = strdup(node->name)) == NULL){
			delete_list(lst);
			return ALLOC_ERR;
		}
		globals->thermal_count++;
	}

	if(globals->thermal_count > MAX_ITEMS) return ITEM_EXCEED;

	for (; i < globals->thermal_count && i < MAX_ITEMS; i++){
		tinfo = &thermals[i];
		snprintf(tinfo->name, MAX_NAME, "%s", names[i]);
		snprintf(tinfo->state_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/state", names[i]);
		snprintf(tinfo->temp_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/temperature", names[i]);
		snprintf(tinfo->cooling_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/cooling_mode", names[i]);
		snprintf(tinfo->freq_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/polling_frequency", names[i]);
		snprintf(tinfo->trips_file, MAX_NAME, PROC_ACPI "thermal_zone/%s/trip_points", names[i]);
		free(names[i]);
	}
	delete_list(lst);
	read_acpi_thermalzones(globals);
	return SUCCESS;
}

/* checks the string state and sets the thermal state, returns void */
static void
thermal_state(const char *state, thermal_t *info){
	if(state[0] == 'o')
		info->therm_state = T_OK;
	else if(!strncmp (state, "crit", 4))
		info->therm_state = T_CRIT;
	else if (!strncmp (state, "hot", 3))
		info->therm_state = T_HOT; else if (!strncmp (state, "pas", 3))
		info->therm_state = T_PASS;
	else
		info->therm_state = T_ACT;
}

/* checks the string tmp and sets the cooling mode */
static void
fill_cooling_mode(const char *tmp, thermal_t *info){
	if(tmp[0] == 'a')
		info->therm_mode = CO_ACT;
	else if(tmp[0]  == 'p')
		info->therm_mode = CO_PASS;
	else info->therm_mode = CO_CRIT;
}

/* reads values for thermal_zone num, return 0 on success, negative values on error */
int
read_acpi_zone(const int num, global_t *globals){
	char *buf = NULL;
	char *tmp = NULL;
	thermal_t *info = &thermals[num];

	if(num > MAX_ITEMS) return ITEM_EXCEED;

	/* scan state file */
	if((buf = get_acpi_content(info->state_file)) == NULL)
		info->therm_state = T_ERR;

	if(buf && (tmp = scan_acpi_value(buf, "state:")))
			thermal_state(tmp, info);
	free(tmp);
	free(buf);

	/* scan temperature file */
	if((buf = get_acpi_content(info->temp_file)) == NULL)
		info->temperature = NOT_SUPPORTED;

	if(buf && (tmp = scan_acpi_value(buf, "temperature:"))){
		info->temperature = strtol(tmp, NULL, 10);
		/* if we just have one big thermal zone, this will be the global temperature */
		if(globals->thermal_count == 1)
			globals->temperature = info->temperature;
	}
	free(tmp);
	free(buf);

	/* scan cooling mode file */
	if((buf = get_acpi_content(info->cooling_file)) == NULL)
		info->therm_mode = CO_ERR;
	if(buf && (tmp = scan_acpi_value(buf, "cooling mode:")))
		fill_cooling_mode(tmp, info);
	else info->therm_mode = CO_ERR;
	free(tmp);
	free(buf);

	/* scan polling_frequencies file */
	if((buf = get_acpi_content(info->freq_file)) == NULL)
		info->frequency = DISABLED;
	if(buf && (tmp = scan_acpi_value(buf, "polling frequency:")))
		info->frequency = strtol(tmp, NULL, 10);
	else info->frequency = DISABLED;
	free(tmp);
	free(buf);

	/* TODO: IMPLEMENT TRIP POINTS FILE */

	return SUCCESS;
}

/* read all thermal zones, fill the thermal structures */
static void
read_acpi_thermalzones(global_t *globals){
	int i;
	for(i = 0; i < globals->thermal_count; i++)
		read_acpi_zone(i, globals);
}

/* fill battery_state for given battery, return 0 on success or negative values on error */
static void
batt_charge_state(battery_t *info){
	int high = info->last_full_cap / 2;
	int med = high / 2;

	if(info->remaining_cap > high)
		info->batt_state = B_HIGH;
	else if(info->remaining_cap <= high && info->remaining_cap > med)
		info->batt_state = B_MED;
	else if(info->remaining_cap <= med && info->remaining_cap > info->design_warn)
		info->batt_state = B_LOW;
	else if(info->remaining_cap <= info->design_warn && info->remaining_cap > info->design_low)
		info->batt_state = B_CRIT;
	else info->batt_state = B_HARD_CRIT;
}

/* fill charge_state of a given battery num, return 0 on success or negative values on error */
static void
fill_charge_state(const char *state, battery_t *info){
	if(state[0] == 'u')
		info->charge_state = C_ERR;
	else if(!strncmp (state, "disch", 5))
		info->charge_state = C_DISCHARGE;
	else if (!strncmp (state, "charge", 6))
		info->charge_state = C_CHARGED;
	else if (!strncmp (state, "chargi", 6))
		info->charge_state = C_CHARGE;
	else
		info->charge_state = C_NOINFO;
}

/* read alarm capacity, return 0 on success, negative values on error */
static int
read_acpi_battalarm(const int num, const int sysstyle){
	char *buf = NULL;
	char *tmp = NULL;
	battery_t *info = &batteries[num];

	if((buf = get_acpi_content(info->alarm_file)) == NULL)
		return NOT_SUPPORTED;

	if(sysstyle)
	{
		if(!strcmp(buf, "0"))
			info->alarm = 0;
		else if(!strcmp(buf, "1"))
			info->alarm = 1;
		else
			info->alarm = NOT_SUPPORTED;
	}
	else
	{
		if((tmp = scan_acpi_value(buf, "alarm:")) && tmp[0] != 'u')
			info->alarm = strtol(tmp, NULL, 10);
		else
			info->alarm = NOT_SUPPORTED;
	}
	free(buf);
	free(tmp);
	return SUCCESS;
}

/* reads static values for a battery (info file), returns SUCCESS */
static int
read_acpi_battinfo(const int num, const int sysstyle){
	char *buf = NULL;
	char *tmp = NULL;
	battery_t *info = &batteries[num];
	int i = 0;
	char sysfile[MAX_NAME];

	if(sysstyle)
	{
		snprintf(sysfile, MAX_NAME, "%s/present", info->info_file);
		if((buf = get_acpi_content(sysfile)) == NULL)
			return NOT_SUPPORTED;
		if(!strcmp(buf, "1")) {
			info->present = 1;
		} else {
			info->present = 0;
			return NOT_PRESENT;
		}

		snprintf(sysfile, MAX_NAME, "%s/charge_full_design", info->info_file);
		if((buf = get_acpi_content(sysfile)) == NULL) {
			snprintf(sysfile, MAX_NAME, "%s/energy_full_design",
				 info->info_file);
			if((buf = get_acpi_content(sysfile)) == NULL)
				return NOT_SUPPORTED;
		}
		info->design_cap = strtol(buf, NULL, 10);

		snprintf(sysfile, MAX_NAME, "%s/charge_full", info->info_file);
		if((buf = get_acpi_content(sysfile)) == NULL) {
			snprintf(sysfile, MAX_NAME, "%s/energy_full",
				 info->info_file);
			if((buf = get_acpi_content(sysfile)) == NULL)
				return NOT_SUPPORTED;
		}
		info->last_full_cap = strtol(buf, NULL, 10);

		snprintf(sysfile, MAX_NAME, "%s/charge_now", info->info_file);
		if((buf = get_acpi_content(sysfile)) == NULL) {
			snprintf(sysfile, MAX_NAME, "%s/energy_now",
				 info->info_file);
			if((buf = get_acpi_content(sysfile)) == NULL)
				return NOT_SUPPORTED;
		}
		info->remaining_cap = strtol(buf, NULL, 10);

		snprintf(sysfile, MAX_NAME, "%s/voltage_min_design", info->info_file);
		if((buf = get_acpi_content(sysfile)) == NULL)
			return NOT_SUPPORTED;
		info->design_voltage = strtol(buf, NULL, 10);

		snprintf(sysfile, MAX_NAME, "%s/voltage_now", info->info_file);
		if((buf = get_acpi_content(sysfile)) == NULL)
			return NOT_SUPPORTED;
		info->present_voltage = strtol(buf, NULL, 10);

		/* FIXME: is rate == current here? */
		snprintf(sysfile, MAX_NAME, "%s/current_now", info->info_file);
		if((buf = get_acpi_content(sysfile)) == NULL)
			return NOT_SUPPORTED;
		info->present_rate = strtol(buf, NULL, 10);

		return SUCCESS;
	}

	if((buf = get_acpi_content(info->info_file)) == NULL)
		return NOT_SUPPORTED;

	/* you have to read the present value always since a battery can be taken away while
	 * refreshing the data */
	if((tmp = scan_acpi_value(buf, "present:")) && !strncmp(tmp, "yes", 3)) {
		free(tmp);
		info->present = 1;
	} else {
		info->present = 0;
		free(buf);
		return NOT_PRESENT;
	}

	if((tmp = scan_acpi_value(buf, "design capacity:")) && tmp[0] != 'u'){
		info->design_cap = strtol(tmp, NULL, 10);
		/* workaround ACPI's broken way of reporting no battery */
		if(info->design_cap == 655350) info->design_cap = NOT_SUPPORTED;
		free(tmp);
	}
	else info->design_cap = NOT_SUPPORTED;

	for (;battinfo_values[i].value; i++) {
		if ((tmp = scan_acpi_value(buf, battinfo_values[i].value)) && tmp[0] != 'u') {
			*((int *)(((char *)info) + battinfo_values[i].offset)) = strtol(tmp, NULL, 10);
			free(tmp);
		} else {
			*((int *)(((char *)info) + battinfo_values[i].offset)) = NOT_SUPPORTED;
		}
	}

	/* TODO remove debug */
	/* printf("%s\n", buf); */
	free(buf);

	return SUCCESS;
}

/* read information for battery num, return 0 on success or negative values on error */
static int
read_acpi_battstate(const int num){
	char *buf = NULL;
	char *tmp = NULL;
	battery_t *info = &batteries[num];
	unsigned int i = 0;
	char sysfile[MAX_NAME];

	if((buf = get_acpi_content(info->state_file)) == NULL) {
		snprintf(sysfile, MAX_NAME, "%s/status", info->info_file);
		if((buf = get_acpi_content(sysfile)) == NULL)
			return NOT_SUPPORTED;
		if(!strcmp(buf, "Discharging"))
			info->charge_state = C_DISCHARGE;
		else if(!strcmp(buf, "Charging"))
			info->charge_state = C_CHARGE;
		else if(!strcmp(buf, "Full"))
			info->charge_state = C_CHARGED;
		else
			info->charge_state = C_NOINFO;

		return SUCCESS;
	}
	
	if((tmp = scan_acpi_value(buf, "present:")) && !strncmp(tmp, "yes", 3)) {
		info->present = 1;
		free(tmp);
	} else {
		info->present = 0;
		free(buf);
		return NOT_PRESENT;
	}

	/* TODO REMOVE DEBUG */
	/* printf("%s\n\n", buf); */

	if((tmp = scan_acpi_value(buf, "charging state:")) && tmp[0] != 'u') {
		fill_charge_state(tmp, info);
		free(tmp);
	} else {
		info->charge_state = C_NOINFO;
	}

	for (;battstate_values[i].value; i++) {
		if ((tmp = scan_acpi_value(buf, battstate_values[i].value)) && tmp[0] != 'u') {
			*((int *)(((char *)info) + battstate_values[i].offset)) = strtol(tmp, NULL, 10);
			free(tmp);
		} else {
			*((int *)(((char *)info) + battstate_values[i].offset)) = NOT_SUPPORTED;
		}
	}

	/* get information from the info file */
	batt_charge_state(info);
	
	free(buf);
	return SUCCESS;
}

/* calculate percentage of battery capacity num */
static void
calc_remain_perc(const int num){
	float lfcap;
	battery_t *info = &batteries[num];
	int perc;

	if(info->remaining_cap < 0){
		info->percentage = NOT_SUPPORTED;
		return;
	}
	else{
		lfcap = info->last_full_cap;
		if(lfcap <= 0) lfcap = 1;
		perc = (int) ((info->remaining_cap / lfcap) * 100.0);
	}
	info->percentage = perc > 100 ? 100 : perc;
}

/* calculate remaining charge time for battery num */
static void
calc_remain_chargetime(const int num){
	battery_t *info = &batteries[num];

	if(info->present_rate < 0 || info->charge_state != C_CHARGE){
		info->charge_time = 0;
		return;
	}
	info->charge_time = (int) ((((float)info->last_full_cap - (float)info->remaining_cap) / info->present_rate) * 60.0);
}

/* calculate remaining time for battery num */
static void
calc_remain_time(const int num){
	battery_t *info = &batteries[num];

	if(info->present_rate < 0 || info->charge_state != C_DISCHARGE){
		info->remaining_time = 0;
		return;
	}
	info->remaining_time = (int) (((float)info->remaining_cap / (float)info->present_rate) * 60.0);
}

/* read/refresh information about a given battery num
 * returns 0 on SUCCESS, negative values on errors */
int
read_acpi_batt(const int num){
	if(num > MAX_ITEMS) return ITEM_EXCEED;
	read_acpi_battstate(num);
	read_acpi_battalarm(num, 0);
	calc_remain_perc(num);
	calc_remain_chargetime(num);
	calc_remain_time(num);
	return SUCCESS;
}