2.0 beta code
[framework/api/power.git] / test / observing.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 <power.h>
24
25 static GMainLoop *mainloop;
26
27 void power_cb(power_state_e state, void *ud)
28 {
29     char* txt = (char*)ud;
30     printf("power state [%d] -- %s\n", state, 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(power_error_e e){
42     switch(e){
43         case POWER_ERROR_INVALID_PARAMETER:
44             printf("invalid parameter!\n");
45             break;
46         case POWER_ERROR_IO_ERROR:
47             printf("operation failed!\n");
48             break;
49         case POWER_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     power_state_e state;
68     if( (state = power_get_state()) < 0){
69         printf("power state return ");
70         errp(err);
71     }
72     printf("current state -> %d\n", state);
73
74     if( (err=power_set_changed_cb(power_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=power_unset_changed_cb()) < 0){
83         printf("unset cb return ");
84         errp(err);
85     }
86
87         return 0;
88 }