forked from nemomobile/mce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibwakelock.c
262 lines (223 loc) · 6.06 KB
/
libwakelock.c
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* ------------------------------------------------------------------------- *
* Copyright (C) 2012 Jolla Ltd.
* Contact: Simo Piiroinen <[email protected]>
* License: LGPLv2
* ------------------------------------------------------------------------- */
/* NOTE: Only async-signal-safe functions can be called from this
* module since we might need to use the functionality while
* handling non-recoverable signals!
*/
#include "libwakelock.h"
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <errno.h>
/** Whether to write debug logging to stderr
*
* If not enabled, no diagnostics of any kind gets written.
*/
#ifndef LWL_ENABLE_LOGGING
# define LWL_ENABLE_LOGGING 01
#endif
/** Prefix used for log messages
*/
#define LWL_LOG_PFIX "LWL: "
/** Number to string helper
*
* @param buf work space
* @param len size of work space
* @param num number to convert
* @return pointer to ascii representation of num
*/
static char *lwl_number(char *buf, size_t len, long long num)
{
char *pos = buf + len;
int sgn = (num < 0);
unsigned long long val = sgn ? -num : num;
auto void emit(int c) {
if( pos > buf ) *--pos = c;
}
/* terminate at end of work space */
emit(0);
/* work backwards from least signigicant digits */
do { emit('0' + val % 10); } while( val /= 10 );
/* apply minus sign if needed */
if( sgn ) emit('-');
/* return pointer to 1st digit (not start of buffer) */
return pos;
}
/** String concatenation helper
*
* @param buf work space
* @param len size of work space
* @param str first string to copy
* @param ... NULL terminated list of more strings to copy
* @return pointer to the start of the buffer
*/
static char *lwl_concat(char *buf, size_t len, const char *str, ...)
{
char *pos = buf;
char *end = buf + len - 1;
va_list va;
auto void emit(const char *s) {
while( pos < end && *s ) *pos++ = *s++;
}
va_start(va, str);
while( str )
emit(str), str = va_arg(va, const char *);
va_end(va);
*pos = 0;
return buf;
}
#if LWL_ENABLE_LOGGING
/** Flag for enabling wakelock debug logging */
static int lwl_debug_enabled = 0;
/** Logging functionality that can be configured out at compile time
*/
static void lwl_debug_(const char *m, ...)
{
char buf[256];
char *pos = buf;
char *end = buf + sizeof buf - 1;
va_list va;
auto void emit(const char *s)
{
while( pos < end && *s ) *pos++ = *s++;
}
emit("LWL: ");
va_start(va, m);
while( m )
{
emit(m), m = va_arg(va, const char *);
}
va_end(va);
if( write(STDERR_FILENO, buf, pos - buf) < 0 )
{
// do not care
}
}
# define lwl_debug(MSG, MORE...) \
do {\
if( lwl_debug_enabled ) lwl_debug_(MSG, ##MORE); \
} while( 0 )
#else
# define lwl_debug(MSG, MORE...) do { } while( 0 )
#endif
/** Flag that gets set once the process is about to exit */
static int lwl_shutting_down = 0;
/** Sysfs entry for acquiring wakelocks */
static const char lwl_lock_path[] = "/sys/power/wake_lock";
/** Sysfs entry for releasing wakelocks */
static const char lwl_unlock_path[] = "/sys/power/wake_unlock";
/** Sysfs entry for allow/block suspend */
static const char lwl_state_path[] = "/sys/power/state";
/** Helper for writing to sysfs files
*/
static void lwl_write_file(const char *path, const char *data)
{
int file;
lwl_debug(path, " << ", data, NULL);
if( (file = TEMP_FAILURE_RETRY(open(path, O_WRONLY))) == -1 ) {
lwl_debug(path, ": open: ", strerror(errno), "\n", NULL);
} else {
int size = strlen(data);
errno = 0;
if( TEMP_FAILURE_RETRY(write(file, data, size)) != size ) {
lwl_debug(path, ": write: ", strerror(errno),
"\n", NULL);
}
TEMP_FAILURE_RETRY(close(file));
}
}
/** Helper for checking if wakelock interface is supported
*/
static int lwl_enabled(void)
{
static int checked = 0, enabled = 0;
if( !checked ) {
checked = 1, enabled = !access(lwl_lock_path, F_OK);
lwl_debug(enabled ? "enabled" : "disabled", "\n", NULL);
}
return enabled;
}
/** Use sysfs interface to create and enable a wakelock.
*
* @param name The name of the wakelock to obtain
* @param ns Time in nanoseconds before the wakelock gets released
* automatically, or negative value for no timeout.
*/
void wakelock_lock(const char *name, long long ns)
{
if( lwl_enabled() && !lwl_shutting_down ) {
char tmp[64];
char num[64];
if( ns < 0 ) {
lwl_concat(tmp, sizeof tmp, name, "\n", NULL);
} else {
lwl_concat(tmp, sizeof tmp, name,
lwl_number(num, sizeof num, ns),
"\n", NULL);
}
lwl_write_file(lwl_lock_path, tmp);
}
}
/** Use sysfs interface to disable a wakelock.
*
* @param name The name of the wakelock to release
*
* Note: This will not delete the wakelocks.
*/
void wakelock_unlock(const char *name)
{
if( lwl_enabled() ) {
char tmp[64];
lwl_concat(tmp, sizeof tmp, name, "\n", NULL);
lwl_write_file(lwl_unlock_path, tmp);
}
}
/** Use sysfs interface to allow automatic entry to suspend
*
* After this call the device will enter suspend mode once all
* the wakelocks have been released.
*
* Android kernels will enter early suspend (i.e. display is
* turned off etc) even if there still are active wakelocks.
*/
void wakelock_allow_suspend(void)
{
if( lwl_enabled() && !lwl_shutting_down ) {
lwl_write_file(lwl_state_path, "mem\n");
}
}
/** Use sysfs interface to block automatic entry to suspend
*
* The device will not enter suspend mode with or without
* active wakelocks.
*/
void wakelock_block_suspend(void)
{
if( lwl_enabled() ) {
lwl_write_file(lwl_state_path, "on\n");
}
}
/** Block automatic suspend without possibility to unblock it again
*
* For use on exit path. We want to do clean exit from mainloop and
* that might that code that re-enables autosuspend gets triggered
* while we're on exit path.
*
* By calling this function when initiating daemon shutdown we are
* protected against this.
*/
void wakelock_block_suspend_until_exit(void)
{
lwl_shutting_down = 1;
wakelock_block_suspend();
}
/** Enable wakelock debug logging (if support compiled in)
*/
void lwl_enable_logging(void)
{
lwl_debug_enabled = 1;
}