File: sonypi.c

package info (click to toggle)
wmbattery 2.51-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 828 kB
  • sloc: ansic: 1,557; sh: 152; makefile: 42
file content (76 lines) | stat: -rw-r--r-- 1,730 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
#include <stdio.h>
#include <sys/ioctl.h>
#include "apm.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>

#include "sonypi.h"

signed int spicfd = -1;

int sonypi_supported(void)
{
	spicfd = open("/dev/sonypi", O_RDWR);
	if (spicfd == -1)
		return 0;
	else
		return 1;
}

inline int sonypi_ioctl(int ioctlno, void *param)
{
	if (ioctl(spicfd, ioctlno, param) < 0)
		return 0;
	else
		return 1;
}

/* Read battery info from sonypi device and shove it into an apm_info
 * struct. */
int sonypi_read(apm_info *info)
{
	uint8_t batflags;
	uint16_t cap, rem;
	int havebatt = 0;

	info->using_minutes = info->battery_flags = 0;

	if (!sonypi_ioctl(SONYPI_IOCGBATFLAGS, &batflags))
		return 1;

	info->ac_line_status = (batflags & SONYPI_BFLAGS_AC) != 0;
	if (batflags & SONYPI_BFLAGS_B1) {
		if (!sonypi_ioctl(SONYPI_IOCGBAT1CAP, &cap))
			return 1;
		if (!sonypi_ioctl(SONYPI_IOCGBAT1REM, &rem))
			return 1;
		havebatt = 1;
	} else if (batflags & SONYPI_BFLAGS_B2) {
		/* Not quite right, if there is a second battery I should
		 * probably merge the two somehow.. */
		if (!sonypi_ioctl(SONYPI_IOCGBAT2CAP, &cap))
			return 1;
		if (!sonypi_ioctl(SONYPI_IOCGBAT2REM, &rem))
			return 1;
		havebatt = 1;
	} else {
		info->battery_percentage = 0;
		info->battery_status = BATTERY_STATUS_ABSENT;
	}

	if (havebatt) {
		info->battery_percentage = 100 * rem / cap;
		/* Guess at whether the battery is charging. */
		if (info->battery_percentage < 99 && info->ac_line_status == 1) {
			info->battery_flags = info->battery_flags | BATTERY_FLAGS_CHARGING;
			info->battery_status = BATTERY_STATUS_CHARGING;
		}
	}

	/* Sadly, there is no way to estimate this. */
	info->battery_time = 0;

	return 0;
}