patch tizen_2.0_build
[framework/api/device.git] / test / battery.c
1 /*
2  * 
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4  * PROPRIETARY/CONFIDENTIAL
5  * 
6  * This software is the confidential and proprietary information of SAMSUNG 
7  * ELECTRONICS ("Confidential Information"). You agree and acknowledge that 
8  * this software is owned by Samsung and you shall not disclose such 
9  * Confidential Information and shall use it only in accordance with the terms 
10  * of the license agreement you entered into with SAMSUNG ELECTRONICS. SAMSUNG 
11  * make no representations or warranties about the suitability of the software, 
12  * either express or implied, including but not limited to the implied 
13  * warranties of merchantability, fitness for a particular purpose, or 
14  * non-infringement. SAMSUNG shall not be liable for any damages suffered by 
15  * licensee arising out of or related to this software.
16  * 
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <stdbool.h>
23 #include <device.h>
24
25 static GMainLoop *mainloop;
26
27 void battery_cb(int percent, void* ud)
28 {
29     char* txt = (char*)ud;
30     printf("battery capacity [%d] -- %s\n", percent, txt); 
31 }
32
33 static void sig_quit(int signo)
34 {
35         if(mainloop)
36         {
37                 g_main_loop_quit(mainloop);
38         }
39 }
40
41 static void errp(device_error_e e){
42     switch(e){
43         case DEVICE_ERROR_INVALID_PARAMETER:
44             printf("invalid parameter!\n");
45             break;
46         case DEVICE_ERROR_OPERATION_FAILED:
47             printf("operation failed!\n");
48             break;
49         case DEVICE_ERROR_NONE:
50             printf("success!\n");
51             break;
52         default:
53             printf("unknown!\n");
54     }
55 }
56
57 int main(int argc, char *argv[])
58 {
59     int err;
60
61         signal(SIGINT, sig_quit);
62         signal(SIGTERM, sig_quit);
63         signal(SIGQUIT, sig_quit);
64
65         mainloop = g_main_loop_new(NULL, FALSE);
66
67     bool charging;
68     if( (err = device_battery_is_charging(&charging)) < 0){
69         printf("is charging return ");
70         errp(err);
71     }
72     printf("charging state -> %s\n", charging?"charging...":"not charging");
73
74     if( (err=device_battery_set_cb(battery_cb, "PIUS!!")) < 0){
75         printf("set cb return ");
76         errp(err);
77     }
78
79         g_main_loop_run(mainloop);
80         g_main_loop_unref(mainloop);
81
82     if( (err=device_battery_unset_cb()) < 0){
83         printf("unset cb return ");
84         errp(err);
85     }
86
87         return 0;
88 }