summaryrefslogtreecommitdiff
path: root/src/battery.c
blob: 61cf8545c6e7160515b2b56fd421042fc56585b9 (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
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
/*
 * capi-system-device
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdio.h>
#include <string.h>
#include <devman.h>
#include <vconf.h>
#include "device.h"
#include "device_log.h"

#define CHECK_ERR(val)	\
	do {	\
		if (val == DEVMAN_ERROR_NOT_SUPPORTED)	\
			return DEVICE_ERROR_NOT_SUPPORTED;	\
		else if (val == DEVMAN_ERROR_OPERATION_FAILED)	\
			return DEVICE_ERROR_OPERATION_FAILED;	\
	} while(0)

int device_battery_get_percent(int* percent)
{
	if (percent == NULL)
		return DEVICE_ERROR_INVALID_PARAMETER;

	int pct = device_get_battery_pct();
	CHECK_ERR(pct);

	*percent = pct;
	return DEVICE_ERROR_NONE;
}

int device_battery_get_detail(int* percent)
{
	if (percent == NULL)
		return DEVICE_ERROR_INVALID_PARAMETER;

	int pct = device_get_battery_pct_raw();
	CHECK_ERR(pct);

	*percent = pct;
	return DEVICE_ERROR_NONE;
}

int device_battery_is_full(bool* full)
{
	if (full == NULL)
		return DEVICE_ERROR_INVALID_PARAMETER;

	int f = device_is_battery_full();
	CHECK_ERR(f);

	*full = (f == 1) ? true : false;
	return DEVICE_ERROR_NONE;
}

int device_battery_is_charging(bool *charging)
{
	// VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW
	int value, err;

	if (charging == NULL)
		return DEVICE_ERROR_INVALID_PARAMETER;

	err = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CHARGE_NOW, &value);
	if (err < 0)
		return DEVICE_ERROR_OPERATION_FAILED;

	if (value == 1) {
		*charging = true;
	} else if (value == 0) {
		*charging = false;
	} else {
		return DEVICE_ERROR_OPERATION_FAILED;
	}

	return DEVICE_ERROR_NONE;
}

static device_battery_cb changed_callback = NULL;
static void* changed_callback_user_data = NULL;

static void battery_changed_inside_cb(keynode_t* key, void* user_data)
{
	char* keyname = vconf_keynode_get_name(key);

	if (keyname != NULL && changed_callback != NULL && strcmp(keyname, VCONFKEY_SYSMAN_BATTERY_CAPACITY) == 0) {
		int percent = 0;
		if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY, &percent) == 0) {
			changed_callback(percent, changed_callback_user_data);
		}
	}
}

int device_battery_set_cb(device_battery_cb callback, void* user_data)
{
	// VCONFKEY_SYSMAN_BATTERY_CAPACITY
	int err;
	if (callback == NULL)
		return DEVICE_ERROR_INVALID_PARAMETER;

	changed_callback = callback;
	changed_callback_user_data = user_data;

	err = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY, battery_changed_inside_cb, NULL);
	if (err < 0)
		return DEVICE_ERROR_INVALID_PARAMETER;

	return DEVICE_ERROR_NONE;
}

int device_battery_unset_cb(void)
{
	int err = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_CAPACITY, battery_changed_inside_cb);
	if (err < 0)
		return DEVICE_ERROR_OPERATION_FAILED;

	changed_callback = NULL;
	changed_callback_user_data = NULL;

	return DEVICE_ERROR_NONE;
}

int device_battery_get_warning_status(device_battery_warn_e *status)
{
	if (status == NULL)
		return DEVICE_ERROR_INVALID_PARAMETER;

	int value, err;

	err = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &value);
	if (err < 0)
		return DEVICE_ERROR_OPERATION_FAILED;

	if (value == VCONFKEY_SYSMAN_BAT_POWER_OFF) {
		*status = DEVICE_BATTERY_WARN_EMPTY;
	} else if (value == VCONFKEY_SYSMAN_BAT_CRITICAL_LOW) {
		*status = DEVICE_BATTERY_WARN_CRITICAL;
	} else if (value == VCONFKEY_SYSMAN_BAT_WARNING_LOW) {
		*status = DEVICE_BATTERY_WARN_LOW;
	} else if (value == VCONFKEY_SYSMAN_BAT_NORMAL) {
		*status = DEVICE_BATTERY_WARN_NORMAL;
	} else if (value == VCONFKEY_SYSMAN_BAT_FULL) {
		*status = DEVICE_BATTERY_WARN_FULL;
	} else {
		return DEVICE_ERROR_OPERATION_FAILED;
	}

	return DEVICE_ERROR_NONE;
}

static device_battery_warn_cb warn_changed_callback = NULL;
static void* warn_changed_callback_user_data = NULL;

static void battery_warn_changed_inside_cb(keynode_t* key, void* user_data)
{
	char* keyname = vconf_keynode_get_name(key);

	if (keyname != NULL && warn_changed_callback != NULL && strcmp(keyname, VCONFKEY_SYSMAN_BATTERY_STATUS_LOW) == 0) {
		int bat_state = 0;
		if (vconf_get_int(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, &bat_state) == 0) {
			warn_changed_callback(bat_state-1, warn_changed_callback_user_data);
		}
	}
}

int device_battery_warning_set_cb(device_battery_warn_cb callback, void* user_data)
{
	// VCONFKEY_SYSMAN_BATTERY_STATUS_LOW
	int err;

	if (callback == NULL)
		return DEVICE_ERROR_INVALID_PARAMETER;

	warn_changed_callback = callback;
	warn_changed_callback_user_data = user_data;

	err = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, battery_warn_changed_inside_cb, NULL);
	if (err < 0)
		return DEVICE_ERROR_INVALID_PARAMETER;

	return DEVICE_ERROR_NONE;
}

int device_battery_warning_unset_cb(void)
{
	int err = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, battery_warn_changed_inside_cb);
	if (err < 0)
		return DEVICE_ERROR_OPERATION_FAILED;

	warn_changed_callback = NULL;
	warn_changed_callback_user_data = NULL;

	return DEVICE_ERROR_NONE;
}

static device_battery_remaining_time_changed_cb timetofull_changed_cb = NULL;
static device_battery_remaining_time_changed_cb timetoempty_changed_cb = NULL;
static void* timetofull_cb_user_data = NULL;
static void* timetoempty_cb_user_data = NULL;

static void device_battery_time_timetofull_inside_cb(keynode_t* key, void* user_data)
{
	char* keyname = vconf_keynode_get_name(key);
	int time = -1, err= -1;

	if (keyname == NULL)
		return;

	if (strcmp(keyname, VCONFKEY_PM_BATTERY_TIMETOFULL) == 0) {
		if (timetofull_changed_cb != NULL) {
			err = device_battery_get_remaining_time(
				DEVICE_BATTERY_REMAINING_TIME_TO_FULLY_CHARGED, &time);
			if ((err == DEVICE_ERROR_NONE) && (time >= 0))
				timetofull_changed_cb(time, timetofull_cb_user_data);
		}
	}
}

static void device_battery_time_timetoempty_inside_cb(keynode_t* key, void* user_data)
{
	char* keyname = vconf_keynode_get_name(key);
	int time = -1, err= -1;

	if (keyname == NULL)
		return;

	if (strcmp(keyname, VCONFKEY_PM_BATTERY_TIMETOEMPTY) == 0) {
		if (timetoempty_changed_cb != NULL) {
			err = device_battery_get_remaining_time(
				DEVICE_BATTERY_REMAINING_TIME_TO_DISCHARGED, &time);
			if ((err == DEVICE_ERROR_NONE) && (time >= 0))
				timetoempty_changed_cb(time, timetoempty_cb_user_data);
		}
	}
}

int device_battery_get_remaining_time(device_battery_remaining_time_type_e type, int* time)
{
	bool charging = false;
	int err = -1;

	err = device_battery_is_charging(&charging);
	if (err != DEVICE_ERROR_NONE)
		return DEVICE_ERROR_OPERATION_FAILED;

	switch(type) {
		case DEVICE_BATTERY_REMAINING_TIME_TO_FULLY_CHARGED:
			if (charging == false) {
				return DEVICE_ERROR_OPERATION_FAILED;
			} else {
				err = vconf_get_int(VCONFKEY_PM_BATTERY_TIMETOFULL, time);
				if (err < 0 || time < 0) {
					return DEVICE_ERROR_OPERATION_FAILED;
				}
			}
			break;
		case DEVICE_BATTERY_REMAINING_TIME_TO_DISCHARGED:
			if (charging == true) {
				return DEVICE_ERROR_OPERATION_FAILED;
			} else {
				err = vconf_get_int(VCONFKEY_PM_BATTERY_TIMETOEMPTY, time);
				if (err < 0 || time < 0) {
					return DEVICE_ERROR_OPERATION_FAILED;
				}
			}
			break;
		default:
			return DEVICE_ERROR_INVALID_PARAMETER;
	}
	return DEVICE_ERROR_NONE;
}

int device_battery_set_remaining_time_changed_cb(device_battery_remaining_time_type_e type,
	device_battery_remaining_time_changed_cb callback, void* user_data)
{
	int err;

	if (callback == NULL)
		return DEVICE_ERROR_INVALID_PARAMETER;

	switch(type) {
		case DEVICE_BATTERY_REMAINING_TIME_TO_FULLY_CHARGED:
			timetofull_changed_cb = callback;
			timetofull_cb_user_data = user_data;
			err = vconf_notify_key_changed(VCONFKEY_PM_BATTERY_TIMETOFULL,
				device_battery_time_timetofull_inside_cb, NULL);
			if (err < 0)
				return DEVICE_ERROR_OPERATION_FAILED;
			break;
		case DEVICE_BATTERY_REMAINING_TIME_TO_DISCHARGED:
			timetoempty_changed_cb = callback;
			timetoempty_cb_user_data = user_data;
			err = vconf_notify_key_changed(VCONFKEY_PM_BATTERY_TIMETOEMPTY,
				device_battery_time_timetoempty_inside_cb, NULL);
			if (err < 0)
				return DEVICE_ERROR_OPERATION_FAILED;
			break;
		default:
			return DEVICE_ERROR_INVALID_PARAMETER;
	}
	return DEVICE_ERROR_NONE;
}

int device_battery_unset_remaining_time_changed_cb(device_battery_remaining_time_type_e type)
{
	int err;

	switch(type) {
		case DEVICE_BATTERY_REMAINING_TIME_TO_FULLY_CHARGED:
			err = vconf_ignore_key_changed(VCONFKEY_PM_BATTERY_TIMETOFULL,
				device_battery_time_timetofull_inside_cb);
			if (err < 0)
				return DEVICE_ERROR_OPERATION_FAILED;
			timetofull_changed_cb = NULL;
			timetofull_cb_user_data = NULL;
			break;
		case DEVICE_BATTERY_REMAINING_TIME_TO_DISCHARGED:
			err = vconf_ignore_key_changed(VCONFKEY_PM_BATTERY_TIMETOEMPTY,
				device_battery_time_timetoempty_inside_cb);
			if (err < 0)
				return DEVICE_ERROR_OPERATION_FAILED;
			timetoempty_changed_cb = NULL;
			timetoempty_cb_user_data = NULL;
			break;
		default:
			return DEVICE_ERROR_INVALID_PARAMETER;
	}
	return DEVICE_ERROR_NONE;
}