4 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef __POWER_STATE_MANAGER_H__
20 #define __POWER_STATE_MANAGER_H__
26 #include <libsyscommon/ini-parser.h>
27 #include <system/syscommon-plugin-deviced-power-interface.h>
29 #include "shared/log-macro.h"
30 #include "shared/device-notifier.h"
39 void power_state_manager_init(void *data);
41 static inline uint64_t convert_action_string_to_power_state(const char *str)
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;
58 _W("Invalid power state=%s", str);
60 return DEVICED_POWER_STATE_UNDEFINED;
63 static inline const char *state_name(uint64_t state)
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";
92 return "POWER_STATE_INVALID";
95 static inline int is_poweroff_state(uint64_t state)
97 return !!(state & (DEVICED_POWER_STATE_POWEROFF | DEVICED_POWER_STATE_REBOOT | DEVICED_POWER_STATE_EXIT));
100 static inline void power_request_change_state_strict(uint64_t curr, uint64_t next, int reason, const void *udata)
102 struct trans_info ti = { curr, next, reason, udata };
103 GList l = { &ti, NULL, NULL };
105 syscommon_notifier_emit_notify(DEVICE_NOTIFIER_REQUEST_TRANSITION_STATE, &l);
108 static inline void power_request_change_state(uint64_t next, int reason)
110 power_request_change_state_strict(DEVICED_POWER_STATE_ALL, next, reason, NULL);
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);
120 #endif //__POWER_STATE_MANAGER_H__