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
|
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd
*
* Licensed under the Flora License, Version 1.1 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://floralicense.org/license/
*
* 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 "battery-data.h"
#include <device/battery.h>
#include "app-log.h"
battery_changed_cb battery_cb;
/*
* @brief provide the battery level
* return battery level
*/
int battery_get_status()
{
int battery_level = 0;
int ret = device_battery_get_percent(&battery_level);
if (ret != DEVICE_ERROR_NONE) {
__E("device_battery_get_percentage error : %d", ret);
}
__D("%s", __func__);
return battery_level;
}
/*
* @brief Called when a device status is changed.
* @param Enumeration for the device state callback.
* @param value of the changed value
* @param user data
*/
static void _battery_state_changed_cb(device_callback_e type, void *value, void *user_data)
{
int battery_level = 0;
if (DEVICE_CALLBACK_BATTERY_CAPACITY == type) {
battery_level = (int)value;
}
if (battery_cb) {
__D("battery_cb called");
battery_cb(battery_level, user_data);
}
}
/*
* @brief Adds a callback to the observing device state.
* @param callback The callback function to add
* @param user data
*/
void battery_add_status_changed_cb(battery_changed_cb callback,
void *user_data){
battery_cb = callback;
__D("%s", __func__);
device_add_callback(DEVICE_CALLBACK_BATTERY_CAPACITY,
_battery_state_changed_cb, user_data);
}
/*
* @brief removes the callback registered
* @param function pointer to callback function
*/
void battery_remove_status_changed_cb(battery_changed_cb callback)
{
battery_cb = NULL;
device_remove_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, _battery_state_changed_cb);
}
|