summaryrefslogtreecommitdiff
path: root/include/power-internal.h
blob: 6647d8a048af3b2a0160cca99a1de6c25966b74f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __TIZEN_SYSTEM_POWER_INTERNAL_H__
#define __TIZEN_SYSTEM_POWER_INTERNAL_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <glib.h>
#include <gio/gio.h>

#include <hal/device/hal-board.h>

#include "device-error.h"

/**
 * @platform
 * @brief Power off the device.
 * @details It operates synchronously.
 * @since_tizen 6.5
 * @privlevel platform
 * @privilege %http://tizen.org/privilege/reboot
 * @return @c 0 on success,
 *         otherwise a negative error value
 * @retval #DEVICE_ERROR_NONE Successful
 * @retval #DEVICE_ERROR_INVALID_PARAMETER Invalid parameter
 * @retval #DEVICE_ERROR_PERMISSION_DENIED Permission denied
 * @retval #DEVICE_ERROR_OPERATION_FAILED Operation failed
 */
int device_power_poweroff(void);

enum {
	POWER_STATE_MIN_INDEX = 4,
	POWER_STATE_START_INDEX = POWER_STATE_MIN_INDEX,
	POWER_STATE_NORMAL_INDEX,
	POWER_STATE_SLEEP_INDEX,
	POWER_STATE_POWEROFF_INDEX,
	POWER_STATE_REBOOT_INDEX,
	POWER_STATE_EXIT_INDEX,
	POWER_STATE_MAX_INDEX,
};

#define POWER_STATE_START    (1ULL << POWER_STATE_START_INDEX)
#define	POWER_STATE_NORMAL   (1ULL << POWER_STATE_NORMAL_INDEX)
#define	POWER_STATE_SLEEP    (1ULL << POWER_STATE_SLEEP_INDEX)
#define	POWER_STATE_POWEROFF (1ULL << POWER_STATE_POWEROFF_INDEX)
#define	POWER_STATE_REBOOT   (1ULL << POWER_STATE_REBOOT_INDEX)
#define	POWER_STATE_EXIT     (1ULL << POWER_STATE_EXIT_INDEX)
#define	POWER_STATE_ALL      ((1ULL << POWER_STATE_MAX_INDEX) - (1ULL << POWER_STATE_MIN_INDEX))

#define SIGNAME_CHANGE_STATE_TO_START         "ChangeStateToStart"
#define SIGNAME_CHANGE_STATE_TO_NORMAL        "ChangeStateToNormal"
#define SIGNAME_CHANGE_STATE_TO_SLEEP         "ChangeStateToSleep"
#define SIGNAME_CHANGE_STATE_TO_POWEROFF      "ChangeStateToPowerOff"
#define SIGNAME_CHANGE_STATE_TO_REBOOT        "ChangeStateToReboot"
#define SIGNAME_CHANGE_STATE_TO_EXIT          "ChangeStateToExit"

struct device_change_state_info {
	uint64_t prev_state; /* previous state before the state transition, one of POWER_STATE_* */
	uint64_t next_state; /* current state that has been transitioned, one of POWER_STATE_* */
	uint64_t id;         /* unique id for each transition. it is used for wait-done */
	int reason;          /* integer that signifies what event has triggered the transition */
};

/**
 * Callback function that delivers information about a state transition.
 */
typedef void (*change_state_wait_callback) (const struct device_change_state_info *info, void *user_data);

/**
 * Add callback for states to be transitioned. The action caused by the transition,
 *   e.g., wake unlock for sleep or shutdown for reboot, would be defered until calling
 *   device_power_change_state_wait_done().
 *
 * uint64_t state_bits: bitwise-ORing of interesting state.
 * change_state_wait_callback cb: For every state transition to the state_bit, the cb will be invoked.
 * void *user_data: user data for the callback.
 */
int device_power_add_change_state_wait_callback(uint64_t state_bits, change_state_wait_callback cb, void *user_data);

/**
 * Notify the deviced that it is ready to transition state.
 *
 * uint64_t id: id of device_change_state_info that has received through callback.
 */
int device_power_change_state_wait_done(uint64_t id);

/**
 * Remove callback of state transition.
 */
void device_power_remove_change_state_wait_callback(uint64_t state_bits);

/**
 * Async callback of device_power_change_state()
 *
 * uint64_t state: which state it has transitioned.
 * int retval: result of the method call.
 * void *user_data: user data for the callback.
 */
typedef void (*change_state_callback) (uint64_t state, int retval, void *user_data);

/**
 * Send a request to the deviced for changing power state. It doesn't ensure the whole execution
 *   of a callback subroutine - think of a request for sleep/poweroff/reboot state. To ensure
 *   the whole execution of a callback, use device_power_add_change_state_wait_callback() additionally.
 *   If both change_state_wait_callback and change_state_callback has registered to a state,
 *   it is unpredictable which callback will be invoked first.
 *
 * uint64_t state: which state to transition to
 * int timeout_sec: maximum timeout of async method call. if it expires, then the second parameter of
 *   a callback function, int retval, gives -ETIMEDOUT error. If it is 0 or negative, it is set to
 *   the default, 10 seconds.
 * change_state_callback cb: async callback function, nullable
 * void *user_data: user data for the callback.
 */
int device_power_change_state(uint64_t state, int timeout_sec, change_state_callback cb, void *user_data);

/**
 * Return 0 if cloning partition is in progress.
 */
static inline int device_power_check_reboot_allowed(void)
{
	int retval, cloned;

	retval = hal_device_board_get_partition_ab_cloned(&cloned);

	return (retval != 0 || cloned != 0);
}

#ifdef __cplusplus
}
#endif

#endif