817c1d467cf68c589ce3a2ef280c3e7a97037434
[platform/core/api/device.git] / include / power-internal.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __TIZEN_SYSTEM_POWER_INTERNAL_H__
18 #define __TIZEN_SYSTEM_POWER_INTERNAL_H__
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #include <stdint.h>
25 #include <glib.h>
26 #include <gio/gio.h>
27
28 #include "device-error.h"
29
30 /**
31  * @platform
32  * @brief Power off the device.
33  * @details It operates synchronously.
34  * @since_tizen 6.5
35  * @privlevel platform
36  * @privilege %http://tizen.org/privilege/reboot
37  * @return @c 0 on success,
38  *         otherwise a negative error value
39  * @retval #DEVICE_ERROR_NONE Successful
40  * @retval #DEVICE_ERROR_INVALID_PARAMETER Invalid parameter
41  * @retval #DEVICE_ERROR_PERMISSION_DENIED Permission denied
42  * @retval #DEVICE_ERROR_OPERATION_FAILED Operation failed
43  */
44 int device_power_poweroff(void);
45
46 enum {
47         POWER_STATE_MIN_INDEX = 4,
48         POWER_STATE_START_INDEX = POWER_STATE_MIN_INDEX,
49         POWER_STATE_NORMAL_INDEX,
50         POWER_STATE_SLEEP_INDEX,
51         POWER_STATE_POWEROFF_INDEX,
52         POWER_STATE_REBOOT_INDEX,
53         POWER_STATE_EXIT_INDEX,
54         POWER_STATE_MAX_INDEX,
55 };
56
57 #define POWER_STATE_START    (1ULL << POWER_STATE_START_INDEX)
58 #define POWER_STATE_NORMAL   (1ULL << POWER_STATE_NORMAL_INDEX)
59 #define POWER_STATE_SLEEP    (1ULL << POWER_STATE_SLEEP_INDEX)
60 #define POWER_STATE_POWEROFF (1ULL << POWER_STATE_POWEROFF_INDEX)
61 #define POWER_STATE_REBOOT   (1ULL << POWER_STATE_REBOOT_INDEX)
62 #define POWER_STATE_EXIT     (1ULL << POWER_STATE_EXIT_INDEX)
63 #define POWER_STATE_ALL      ((1ULL << POWER_STATE_MAX_INDEX) - (1ULL << POWER_STATE_MIN_INDEX))
64
65 #define SIGNAME_CHANGE_STATE_TO_START         "ChangeStateToStart"
66 #define SIGNAME_CHANGE_STATE_TO_NORMAL        "ChangeStateToNormal"
67 #define SIGNAME_CHANGE_STATE_TO_SLEEP         "ChangeStateToSleep"
68 #define SIGNAME_CHANGE_STATE_TO_POWEROFF      "ChangeStateToPowerOff"
69 #define SIGNAME_CHANGE_STATE_TO_REBOOT        "ChangeStateToReboot"
70 #define SIGNAME_CHANGE_STATE_TO_EXIT          "ChangeStateToExit"
71
72 struct device_change_state_info {
73         uint64_t prev_state; /* previous state before the state transition, one of POWER_STATE_* */
74         uint64_t next_state; /* current state that has been transitioned, one of POWER_STATE_* */
75         uint64_t id;         /* unique id for each transition. it is used for wait-done */
76         int reason;          /* integer that signifies what event has triggered the transition */
77 };
78
79 /**
80  * Callback function that delivers information about a state transition.
81  */
82 typedef void (*change_state_wait_callback) (const struct device_change_state_info *info, void *user_data);
83
84 /**
85  * Add callback for states to be transitioned. The action caused by the transition,
86  *   e.g., wake unlock for sleep or shutdown for reboot, would be defered until calling
87  *   device_power_change_state_wait_done().
88  *
89  * uint64_t state_bits: bitwise-ORing of interesting state.
90  * change_state_wait_callback cb: For every state transition to the state_bit, the cb will be invoked.
91  * void *user_data: user data for the callback.
92  */
93 int device_power_add_change_state_wait_callback(uint64_t state_bits, change_state_wait_callback cb, void *user_data);
94
95 /**
96  * Notify the deviced that it is ready to transition state.
97  *
98  * uint64_t id: id of device_change_state_info that has received through callback.
99  */
100 int device_power_change_state_wait_done(uint64_t id);
101
102 /**
103  * Remove callback of state transition.
104  */
105 void device_power_remove_change_state_wait_callback(uint64_t state_bits);
106
107 /**
108  * Async callback of device_power_change_state()
109  *
110  * uint64_t state: which state it has transitioned.
111  * int retval: result of the method call.
112  * void *user_data: user data for the callback.
113  */
114 typedef void (*change_state_callback) (uint64_t state, int retval, void *user_data);
115
116 /**
117  * Send a request to the deviced for changing power state. It doesn't ensure the whole execution
118  *   of a callback subroutine - think of a request for sleep/poweroff/reboot state. To ensure
119  *   the whole execution of a callback, use device_power_add_change_state_wait_callback() additionally.
120  *   If both change_state_wait_callback and change_state_callback has registered to a state,
121  *   it is unpredictable which callback will be invoked first.
122  *
123  * uint64_t state: which state to transition to
124  * int timeout_sec: maximum timeout of async method call. if it expires, then the second parameter of
125  *   a callback function, int retval, gives -ETIMEDOUT error. If it is 0 or negative, it is set to
126  *   the default, 10 seconds.
127  * change_state_callback cb: async callback function, nullable
128  * void *user_data: user data for the callback.
129  */
130 int device_power_change_state(uint64_t state, int timeout_sec, change_state_callback cb, void *user_data);
131
132 /**
133  * Return 0 if cloning partition is in progress.
134  */
135 int device_power_check_reboot_allowed(void);
136
137 int device_power_get_state(uint64_t *state);
138
139 #ifdef __cplusplus
140 }
141 #endif
142
143 #endif