summaryrefslogtreecommitdiffstats
path: root/backend/battery_n810.c
blob: d75de420ade60053c811cecec781e5653a10dc70 (plain)
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
/*
 *   Copyright (C) 2010 Michael Buesch <m@bues.ch>
 *
 *   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.
 */

#include "battery_n810.h"
#include "fileaccess.h"
#include "log.h"
#include "util.h"

#include <stdio.h>
#include <errno.h>
#include <string.h>


static int battery_n810_update(struct battery *b)
{
	struct battery_n810 *bn = container_of(b, struct battery_n810, battery);
	int err, value;
	int value_changed = 0;

	err = file_read_int(bn->level_file, &value, 0);
	if (err) {
		logerr("WARNING: Failed to read battery charge status file\n");
		return -1;
	}
	if (value != bn->charge)
		value_changed = 1;
	bn->charge = value;

	if (value_changed)
		battery_notify_state_change(b);

	return 0;
}

static int battery_n810_charge_level(struct battery *b)
{
	struct battery_n810 *bn = container_of(b, struct battery_n810, battery);

	return bn->charge;
}

static void battery_n810_destroy(struct battery *b)
{
	struct battery_n810 *bn = container_of(b, struct battery_n810, battery);

	file_close(bn->level_file);
	free(bn);
}

static struct fileaccess * battery_n810_attr_open(int flags, const char *name)
{
	struct fileaccess *fd;

	fd = sysfs_file_open(flags, "/devices/platform/n810bm/%s", name);
	if (fd)
		return fd;
	fd = sysfs_file_open(flags, "/devices/platform/tahvo/tahvo-n810bm/%s", name);
	if (fd)
		return fd;
	fd = sysfs_file_open(flags, "/devices/platform/retu/retu-n810bm/%s", name);
	if (fd)
		return fd;

	return NULL;
}

static struct battery * battery_n810_probe(void)
{
	struct fileaccess *level_file;
	struct battery_n810 *bn;

	level_file = battery_n810_attr_open(O_RDONLY, "battery_level");
	if (!level_file)
		goto error;

	bn = zalloc(sizeof(*bn));
	if (!bn)
		goto err_close;

	bn->level_file = level_file;

	battery_init(&bn->battery, "n810");
	bn->battery.destroy = battery_n810_destroy;
	bn->battery.update = battery_n810_update;
	bn->battery.charge_level = battery_n810_charge_level;
	bn->battery.poll_interval = 10000;

	return &bn->battery;

err_close:
	file_close(level_file);
error:
	return NULL;
}

BATTERY_PROBE(n810, battery_n810_probe);
bues.ch cgit interface