summaryrefslogtreecommitdiff
path: root/test/battery.c
blob: 16408d2a196322a687089e3d51a8a418356cbcce (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
/*
 * 
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 * PROPRIETARY/CONFIDENTIAL
 * 
 * This software is the confidential and proprietary information of SAMSUNG 
 * ELECTRONICS ("Confidential Information"). You agree and acknowledge that 
 * this software is owned by Samsung and you shall not disclose such 
 * Confidential Information and shall use it only in accordance with the terms 
 * of the license agreement you entered into with SAMSUNG ELECTRONICS. SAMSUNG 
 * make no representations or warranties about the suitability of the software, 
 * either express or implied, including but not limited to the implied 
 * warranties of merchantability, fitness for a particular purpose, or 
 * non-infringement. SAMSUNG shall not be liable for any damages suffered by 
 * licensee arising out of or related to this software.
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <stdbool.h>
#include <device.h>

static GMainLoop *mainloop;

void battery_cb(int percent, void* ud)
{
    char* txt = (char*)ud;
    printf("battery capacity [%d] -- %s\n", percent, txt); 
}

static void sig_quit(int signo)
{
	if(mainloop)
	{
		g_main_loop_quit(mainloop);
	}
}

static void errp(device_error_e e){
    switch(e){
        case DEVICE_ERROR_INVALID_PARAMETER:
            printf("invalid parameter!\n");
            break;
        case DEVICE_ERROR_OPERATION_FAILED:
            printf("operation failed!\n");
            break;
        case DEVICE_ERROR_NONE:
            printf("success!\n");
            break;
        default:
            printf("unknown!\n");
    }
}

int main(int argc, char *argv[])
{
    int err;

	signal(SIGINT, sig_quit);
	signal(SIGTERM, sig_quit);
	signal(SIGQUIT, sig_quit);

	mainloop = g_main_loop_new(NULL, FALSE);

    bool charging;
    if( (err = device_battery_is_charging(&charging)) < 0){
        printf("is charging return ");
        errp(err);
    }
    printf("charging state -> %s\n", charging?"charging...":"not charging");

    if( (err=device_battery_set_cb(battery_cb, "PIUS!!")) < 0){
        printf("set cb return ");
        errp(err);
    }

	g_main_loop_run(mainloop);
	g_main_loop_unref(mainloop);

    if( (err=device_battery_unset_cb()) < 0){
        printf("unset cb return ");
        errp(err);
    }

	return 0;
}