tizen 2.3 release
[framework/system/deviced.git] / src / powersaver / powersaver.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <vconf.h>
21
22 #include "core/common.h"
23 #include "core/devices.h"
24 #include "core/device-notifier.h"
25 #include "core/log.h"
26
27 static int power_saving_custom_cpu_cb(keynode_t *key_nodes, void *data)
28 {
29         int val = 0;
30
31         val = vconf_keynode_get_bool(key_nodes);
32         device_notify(DEVICE_NOTIFIER_PMQOS_POWERSAVING, (void*)val);
33         return 0;
34 }
35
36 static int emergency_cpu_cb(keynode_t *key_nodes, void *data)
37 {
38         int val;
39
40         val = vconf_keynode_get_int(key_nodes);
41         if (val == SETTING_PSMODE_EMERGENCY)
42                 val = 1;
43         else
44                 val = 0;
45         device_notify(DEVICE_NOTIFIER_PMQOS_EMERGENCY, (void*)val);
46         return 0;
47 }
48
49 static void set_freq_limit(void)
50 {
51         int ret = 0;
52         int val = 0;
53
54         ret = vconf_get_bool(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU,
55                         &val);
56         if (ret < 0) {
57                 _E("failed to get vconf key");
58                 return;
59         }
60         if (val == 0)
61                 return;
62         _I("init");
63         device_notify(DEVICE_NOTIFIER_PMQOS_POWERSAVING, (void*)val);
64 }
65
66 static void set_emergency_limit(void)
67 {
68         int ret, val;
69
70         ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE, &val);
71         if (ret < 0) {
72                 _E("failed to get vconf key");
73                 return;
74         }
75         if (val != SETTING_PSMODE_EMERGENCY)
76                 return;
77
78         val = 1;
79         device_notify(DEVICE_NOTIFIER_PMQOS_EMERGENCY, (void*)val);
80
81 }
82
83 static int booting_done(void *data)
84 {
85         set_freq_limit();
86         set_emergency_limit();
87
88         return 0;
89 }
90
91 static void powersaver_init(void *data)
92 {
93         int ret;
94
95         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PWRSV_CUSTMODE_CPU,
96                 (void *)power_saving_custom_cpu_cb, NULL);
97         if (ret != 0)
98                 _E("Failed to vconf_notify_key_changed!");
99         ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
100                 (void *)emergency_cpu_cb, NULL);
101         if (ret != 0)
102                 _E("Failed to vconf_notify_key_changed!");
103         register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
104 }
105
106 static void powersaver_exit(void *data)
107 {
108         unregister_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
109 }
110
111 static const struct device_ops powersaver_device_ops = {
112         .priority = DEVICE_PRIORITY_NORMAL,
113         .name     = "powersaver",
114         .init     = powersaver_init,
115         .exit     = powersaver_exit,
116 };
117
118 DEVICE_OPS_REGISTER(&powersaver_device_ops)