tizen 2.3 release
[framework/system/deviced.git] / src / sleep / sleep.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 <time.h>
21
22 #include "core/log.h"
23 #include "core/device-notifier.h"
24 #include "core/devices.h"
25 #include "core/edbus-handler.h"
26 #include "core/common.h"
27 #include "display/core.h"
28 #include "sleep.h"
29
30 #define SLEEP_CHECK_DURATION 3600 /* 1hour */
31
32 enum sleep_enum {
33         DEVICE_AWAKE = 0,
34         DEVICE_SLEEP = 1,
35 };
36
37 struct sleep_type {
38         int sleep_status;
39 };
40
41 static struct sleep_type monitor = {
42         .sleep_status = -1,
43 };
44 static time_t old = 0;
45 static time_t diff = 0;
46
47 static void sleep_changed(int status)
48 {
49         if (monitor.sleep_status == status)
50                 return;
51         monitor.sleep_status = status;
52         _I("sleep status %d", status);
53 }
54
55 static void check_sleep(void)
56 {
57         time_t now;
58
59         if (monitor.sleep_status == DEVICE_SLEEP)
60                 return;
61
62         time(&now);
63         diff = now - old;
64
65         if (diff > SLEEP_CHECK_DURATION)
66                 sleep_changed(DEVICE_SLEEP);
67 }
68
69 static int display_changed(void *data)
70 {
71         enum state_t state = (enum state_t)data;
72
73         if (state == S_LCDOFF || state == S_SLEEP)
74                 goto out;
75
76         time(&old);
77         diff = 0;
78
79         sleep_changed(DEVICE_AWAKE);
80 out:
81         return 0;
82 }
83
84 static int booting_done(void *data)
85 {
86         time(&old);
87         register_notifier(DEVICE_NOTIFIER_LCD, display_changed);
88         return 0;
89 }
90
91 static void sleep_init(void *data)
92 {
93         register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
94 }
95
96 static void sleep_exit(void *data)
97 {
98         unregister_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done);
99         unregister_notifier(DEVICE_NOTIFIER_LCD, display_changed);
100 }
101
102 static int sleep_execute(void *data)
103 {
104         check_sleep();
105         return 0;
106 }
107
108 static const struct device_ops sleep_device_ops = {
109         .priority = DEVICE_PRIORITY_NORMAL,
110         .name     = DEVICE_SLEEP_OPS,
111         .init     = sleep_init,
112         .exit     = sleep_exit,
113         .execute  = sleep_execute,
114 };
115
116 DEVICE_OPS_REGISTER(&sleep_device_ops)
117