7771556a17bc63c1ab8157ca95a287d68f9afca7
[platform/core/system/deviced.git] / src / power / power.h
1 /*
2  * deviced
3  *
4  * Copyright (c) 2022 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 #ifndef __POWER_STATE_MANAGER_H__
20 #define __POWER_STATE_MANAGER_H__
21
22 #include <stdio.h>
23 #include <glib.h>
24 #include <libsyscommon/ini-parser.h>
25 #include <device/power-internal.h>
26
27 #include "shared/log-macro.h"
28 #include "shared/device-notifier.h"
29
30 #define POWER_STATE_NONE    0
31
32 struct trans_info {
33         uint64_t curr;
34         uint64_t next;
35         int reason;
36         void *data;
37 };
38
39 void power_state_manager_init(void *data);
40
41 static inline uint64_t convert_action_string_to_power_state(const char *str)
42 {
43         if (MATCH(str, "start"))
44                 return POWER_STATE_START;
45         else if (MATCH(str, "sleep"))
46                 return POWER_STATE_SLEEP;
47         else if (MATCH(str, "normal"))
48                 return POWER_STATE_NORMAL;
49         else if (MATCH(str, "poweroff"))
50                 return POWER_STATE_POWEROFF;
51         else if (MATCH(str, "reboot"))
52                 return POWER_STATE_REBOOT;
53         else if (MATCH(str, "exit"))
54                 return POWER_STATE_EXIT;
55         else if (MATCH(str, "current"))
56                 return POWER_STATE_ALL;
57
58         _W("Invalid power state=%s", str);
59
60         return POWER_STATE_NONE;
61 }
62
63 static inline const char *state_name(uint64_t state)
64 {
65         if (state == POWER_STATE_START)
66                 return "POWER_STATE_START";
67         if (state == POWER_STATE_NORMAL)
68                 return "POWER_STATE_NORMAL";
69         if (state == POWER_STATE_SLEEP)
70                 return "POWER_STATE_SLEEP";
71         if (state == POWER_STATE_POWEROFF)
72                 return "POWER_STATE_POWEROFF";
73         if (state == POWER_STATE_REBOOT)
74                 return "POWER_STATE_REBOOT";
75         if (state == POWER_STATE_EXIT)
76                 return "POWER_STATE_EXIT";
77         if (state == POWER_STATE_TRANSIENT_SUSPENDING_EARLY)
78                 return "POWER_STATE_TRANSIENT_SUSPENDING_EARLY";
79         if (state == POWER_STATE_TRANSIENT_SUSPENDING)
80                 return "POWER_STATE_TRANSIENT_SUSPENDING";
81         if (state == POWER_STATE_TRANSIENT_SUSPENDING_LATE)
82                 return "POWER_STATE_TRANSIENT_SUSPENDING_LATE";
83         if (state == POWER_STATE_TRANSIENT_RESUMING_EARLY)
84                 return "POWER_STATE_TRANSIENT_RESUMING_EARLY";
85         if (state == POWER_STATE_TRANSIENT_RESUMING)
86                 return "POWER_STATE_TRANSIENT_RESUMING";
87         if (state == POWER_STATE_TRANSIENT_RESUMING_LATE)
88                 return "POWER_STATE_TRANSIENT_RESUMING_LATE";
89         if (state == POWER_STATE_ALL)
90                 return "POWER_STATE_ALL";
91
92         return "POWER_STATE_INVALID";
93 }
94
95 static inline int is_poweroff_state(uint64_t state)
96 {
97         return !!(state & (POWER_STATE_POWEROFF | POWER_STATE_REBOOT | POWER_STATE_EXIT));
98 }
99
100 static inline void power_request_change_state_strict(uint64_t curr, uint64_t next, int reason, void *udata)
101 {
102         struct trans_info ti = { curr, next, reason, udata };
103         GList l = { &ti, NULL, NULL };
104
105         device_notify(DEVICE_NOTIFIER_REQUEST_TRANSITION_STATE, &l);
106 }
107
108 static inline void power_request_change_state(uint64_t next, int reason)
109 {
110         power_request_change_state_strict(POWER_STATE_ALL, next, reason, NULL);
111 }
112
113 uint64_t power_get_state(void);
114 int add_change_state_wait(pid_t pid, guint64 state);
115 void remove_change_state_wait(pid_t pid, guint64 state);
116 int confirm_change_state_wait(pid_t pid, guint64 id);
117
118 #endif //__POWER_STATE_MANAGER_H__