0bd5ab0df7c235b816bcf7996133512046e08d03
[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 <stdint.h>
24 #include <glib.h>
25
26 #include <libsyscommon/ini-parser.h>
27 #include <system/syscommon-plugin-deviced-power-interface.h>
28
29 #include "shared/log-macro.h"
30 #include "shared/device-notifier.h"
31
32 struct trans_info {
33         uint64_t curr;
34         uint64_t next;
35         int reason;
36         const 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 DEVICED_POWER_STATE_START;
45         else if (MATCH(str, "sleep"))
46                 return DEVICED_POWER_STATE_SLEEP;
47         else if (MATCH(str, "normal"))
48                 return DEVICED_POWER_STATE_NORMAL;
49         else if (MATCH(str, "poweroff"))
50                 return DEVICED_POWER_STATE_POWEROFF;
51         else if (MATCH(str, "reboot"))
52                 return DEVICED_POWER_STATE_REBOOT;
53         else if (MATCH(str, "exit"))
54                 return DEVICED_POWER_STATE_EXIT;
55         else if (MATCH(str, "current"))
56                 return DEVICED_POWER_STATE_ALL;
57
58         _W("Invalid power state=%s", str);
59
60         return DEVICED_POWER_STATE_UNDEFINED;
61 }
62
63 static inline const char *state_name(uint64_t state)
64 {
65         if (state == DEVICED_POWER_STATE_START)
66                 return "POWER_STATE_START";
67         if (state == DEVICED_POWER_STATE_NORMAL)
68                 return "POWER_STATE_NORMAL";
69         if (state == DEVICED_POWER_STATE_SLEEP)
70                 return "POWER_STATE_SLEEP";
71         if (state == DEVICED_POWER_STATE_POWEROFF)
72                 return "POWER_STATE_POWEROFF";
73         if (state == DEVICED_POWER_STATE_REBOOT)
74                 return "POWER_STATE_REBOOT";
75         if (state == DEVICED_POWER_STATE_EXIT)
76                 return "POWER_STATE_EXIT";
77         if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_EARLY)
78                 return "POWER_TRANSIENT_STATE_SUSPENDING_EARLY";
79         if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING)
80                 return "POWER_TRANSIENT_STATE_SUSPENDING";
81         if (state == DEVICED_POWER_TRANSIENT_STATE_SUSPENDING_LATE)
82                 return "POWER_TRANSIENT_STATE_SUSPENDING_LATE";
83         if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING_EARLY)
84                 return "POWER_TRANSIENT_STATE_RESUMING_EARLY";
85         if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING)
86                 return "POWER_TRANSIENT_STATE_RESUMING";
87         if (state == DEVICED_POWER_TRANSIENT_STATE_RESUMING_LATE)
88                 return "POWER_TRANSIENT_STATE_RESUMING_LATE";
89         if (state == DEVICED_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 & (DEVICED_POWER_STATE_POWEROFF | DEVICED_POWER_STATE_REBOOT | DEVICED_POWER_STATE_EXIT));
98 }
99
100 static inline void power_request_change_state_strict(uint64_t curr, uint64_t next, int reason, const void *udata)
101 {
102         struct trans_info ti = { curr, next, reason, udata };
103         GList l = { &ti, NULL, NULL };
104
105         syscommon_notifier_emit_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(DEVICED_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 int is_there_pending_transition(void);
116 void remove_change_state_wait(pid_t pid, guint64 state);
117 int confirm_change_state_wait(pid_t pid, guint64 transition_id);
118 int cancel_change_state_wait(pid_t pid, guint64 transition_id);
119
120 #endif //__POWER_STATE_MANAGER_H__