summaryrefslogtreecommitdiff
path: root/src/power.c
blob: cae193b4f4edbd0ac63c165a0ab93cf0bc3e92fc (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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
 * Copyright (c) 2011 - 2015 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.
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <glib.h>
#include <limits.h>
#include <tracker.h>
#include <sys/time.h>
#include <libsyscommon/libgdbus.h>
#include <libsyscommon/ini-parser.h>

#include "power.h"
#include "power-internal.h"
#include "display.h"
#include "display-internal.h"
#include "common.h"

/**
 * Parameters for device_power_request_lock()
 */
#define STAY_CUR_STATE  0x1
#define GOTO_STATE_NOW  0x2
#define HOLD_KEY_BLOCK  0x4
#define STANDBY_MODE    0x8

/**
 * Parameters for device_power_request_unlock()
 */
#define PM_SLEEP_MARGIN 0x0 /**< keep guard time for unlock */
#define PM_RESET_TIMER  0x1 /**< reset timer for unlock */
#define PM_KEEP_TIMER   0x2 /**< keep timer for unlock */

#define METHOD_LOCK_STATE           "lockstate"
#define METHOD_UNLOCK_STATE         "unlockstate"
#define METHOD_CHANGE_STATE         "changestate"
#define METHOD_POWEROFF             "PowerOff"
#define METHOD_POWEROFF_WITH_OPTION "PowerOffWithOption"

#define TYPE_POWEROFF           "poweroff"
#define TYPE_REBOOT             "reboot"
#define REBOOT_REASON_NONE      ""

#define STR_STAYCURSTATE "staycurstate"
#define STR_GOTOSTATENOW "gotostatenow"

#define STR_HOLDKEYBLOCK "holdkeyblock"
#define STR_STANDBYMODE  "standbymode"
#define STR_NULL         "NULL"

#define STR_SLEEP_MARGIN "sleepmargin"
#define STR_RESET_TIMER  "resettimer"
#define STR_KEEP_TIMER   "keeptimer"

#define STR_LCD_OFF   "lcdoff"
#define STR_LCD_DIM   "lcddim"
#define STR_LCD_ON    "lcdon"

#define LOCK_CPU_TIMEOUT_MAX       (24*60*60*1000) /* milliseconds */
#define LOCK_CPU_PADDING_TIMEOUT   (20*1000) /* milliseconds */

#define POWER_CONF "/etc/deviced/device/power.conf"

static guint off_lock_timeout;
static guint padding_timeout;
static int prev_count;

static GList *request_id_list;

static struct _lock_timeout {
	unsigned int release;
	unsigned int padding;
} lock_timeout = {
	.release = LOCK_CPU_TIMEOUT_MAX,
	.padding = LOCK_CPU_PADDING_TIMEOUT,
};
static bool power_locked_CPU;
static bool power_locked_DISPLAY;
static bool power_locked_DISPLAY_DIM;

static char *get_state_str(display_state_e state)
{
	switch (state) {
	case DISPLAY_STATE_NORMAL:
		return STR_LCD_ON;
	case DISPLAY_STATE_SCREEN_DIM:
		return STR_LCD_DIM;
	case DISPLAY_STATE_SCREEN_OFF:
		return STR_LCD_OFF;
	default:
		break;
	}
	return NULL;
}

static void remove_off_lock_timeout(void)
{
	if (!TIZEN_FEATURE_TRACKER)
		return;
	if (off_lock_timeout) {
		_I("Power lock timeout handler removed"); //LCOV_EXCL_LINE Logs
		g_source_remove(off_lock_timeout);
		off_lock_timeout = 0;
	}
}

static void remove_padding_timeout(void)
{
	if (!TIZEN_FEATURE_TRACKER)
		return;
	if (padding_timeout) {
		_I("Padding timeout handler removed"); //LCOV_EXCL_LINE Logs
		g_source_remove(padding_timeout);
		padding_timeout = 0;
	}
}

//LCOV_EXCL_START Callback function
static void notice_lock_expired_done(GVariant *result, void *data, GError *err)
{
	int val, ret_val;
	GList *l, *l_next;
	char *id, *elem;
	size_t len;

	if (!result)
		return;

	g_variant_get(result, "(&si)", &id, &val);

	elem = NULL;
	len = strlen(id) + 1;
	for (l = request_id_list, l_next = g_list_next(l);
			l && (elem = l->data) != NULL;
			l = l_next, l_next = g_list_next(l), elem = NULL) {
		if (!strncmp(id, elem, len))
			break;
	}
	if (!elem)
		return;

	request_id_list = g_list_remove(request_id_list, elem);
	free(elem);

	if (val == 0) { /* Allow to lock cpu */
		_I("Continue Power Lock");
		return;
	} else if (val == 1) { /* Release cpu lock */
		ret_val = device_power_release_lock(POWER_LOCK_CPU);
		if (ret_val != DEVICE_ERROR_NONE)
			_E("Failed to release power(CPU) (%d)", ret_val);
	}
}
//LCOV_EXCL_STOP

//LCOV_EXCL_START Callback function
static char *get_new_request_id(pid_t pid)
{
	char id[64];
	snprintf(id, sizeof(id), "%d_%lu", pid, time(NULL));
	return strdup(id);
}
//LCOV_EXCL_STOP

//LCOV_EXCL_START Callback function
static int notice_power_lock_expired(void)
{
	int ret_dbus;
	pid_t pid;
	char *req_id;

	pid = getpid();

	req_id = get_new_request_id(pid);
	if (!req_id) {
		_E("Failed to get request id");
		return -ENOMEM;
	}

	ret_dbus = gdbus_call_async_with_reply(
			DEVICED_BUS_NAME,
			DEVICED_PATH_DISPLAY,
			DEVICED_INTERFACE_DISPLAY,
			"LockTimeoutExpired",
			g_variant_new("(s)", req_id),
			notice_lock_expired_done,
			-1,
			NULL);
	if (ret_dbus < 0) {
		_E("Failed to notice power lock expired (%d)", ret_dbus);
		free(req_id);
		return ret_dbus;
	}

	request_id_list = g_list_append(request_id_list, req_id);
	_I("request ID %s is added", req_id);

	return 0;
}
//LCOV_EXCL_STOP

//LCOV_EXCL_START Callback function
static gboolean padding_timeout_expired(gpointer data)
{
	int ret_val, ref;
	int count;

	_I("Padding timeout expired");

	remove_padding_timeout();

	ret_val = tracker_get_ref_counter(TRACKER_TYPE_POWER_LOCK, &ref);
	if (ret_val != TRACKER_ERROR_NONE) {
		_E("Failed to get reference count of power lock");
		goto out;
	}

	_I("reference count of power lock is (%d)", ref);
	if (ref > 0) {
		_I("Power Lock continue (Reference count > 0 !!)");
		return G_SOURCE_REMOVE;
	}

	ret_val = tracker_get_tick(TRACKER_TYPE_POWER_LOCK, &count);
	if (ret_val != TRACKER_ERROR_NONE) {
		_E("Failed to get total count of power lock(%d)", ret_val);
		goto out;
	}

	if (count != prev_count) {
		_I("Power Lock continue (Total reference count increased !!)");
		return G_SOURCE_REMOVE;
	}

out:
	ret_val = notice_power_lock_expired();
	if (ret_val < 0) {
		_E("Failed to launch power lock expired popup (%d)", ret_val);
		return G_SOURCE_CONTINUE;
	}

	remove_off_lock_timeout();

	return G_SOURCE_REMOVE;
}
//LCOV_EXCL_STOP

//LCOV_EXCL_START Callback function
static void add_padding_timeout(void)
{
	guint id;

	remove_padding_timeout();

	_I("Padding timeout handler added");

	id = g_timeout_add(lock_timeout.padding,
			padding_timeout_expired, NULL);
	if (id)
		padding_timeout = id;
	else
		_E("Failed to add timeout for padding time");
}
//LCOV_EXCL_STOP

//LCOV_EXCL_START Callback function
static gboolean off_lock_timeout_expired(gpointer data)
{
	int ret_val, ref;

	_I("Power lock timeout expired");

	ret_val = tracker_get_ref_counter(TRACKER_TYPE_POWER_LOCK, &ref);
	if (ret_val != TRACKER_ERROR_NONE) {
		_E("Failed to get reference count of power lock(%d)", ret_val);
		remove_off_lock_timeout();
		return G_SOURCE_REMOVE;
	}

	_I("reference count of power lock is (%d)", ref);
	if (ref > 0)
		goto out;

	add_padding_timeout();

	ret_val = tracker_get_tick(TRACKER_TYPE_POWER_LOCK, &prev_count);
	if (ret_val != TRACKER_ERROR_NONE)
		_E("Failed to get total count of power lock(%d)", ret_val);

out:
	return G_SOURCE_CONTINUE;
}
//LCOV_EXCL_STOP

static void add_off_lock_timeout(void)
{
	guint id;

	if (!TIZEN_FEATURE_TRACKER)
		return;

	remove_off_lock_timeout();
	remove_padding_timeout();

	_I("Power lock timeout handler added"); //LCOV_EXCL_LINE Logs

	id = g_timeout_add(lock_timeout.release,
			off_lock_timeout_expired, NULL);
	if (id)
		off_lock_timeout = id;
	else
		_E("Failed to add Power Lock timeout handler"); //LCOV_EXCL_LINE Logs
}

static void lock_cb(GVariant *result, void *data, GError *err)
{
	int ret_val;

	if (!result) {
//LCOV_EXCL_START System Error
		_E("no message : %s", err->message);
		return;
//LCOV_EXCL_STOP
	}

	g_variant_get(result, "(i)", &ret_val);
	_D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_LOCK_STATE, ret_val);

	if (ret_val < 0)
		remove_off_lock_timeout();
}

static int lock_state(display_state_e state, unsigned int flag, int timeout_ms)
{
	char *arr[4];
	int ret_dbus;
	static int privilege = -1;

	if (flag & GOTO_STATE_NOW)
		arr[1] = STR_GOTOSTATENOW;
	else
		arr[1] = STR_STAYCURSTATE;

	if (flag & HOLD_KEY_BLOCK)
		arr[2] = STR_HOLDKEYBLOCK;
	else if (flag & STANDBY_MODE)
		arr[2] = STR_STANDBYMODE;
	else
		arr[2] = STR_NULL;

	if (privilege < 0) {
		arr[0] = "privilege check";

		ret_dbus = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
				DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
				METHOD_LOCK_STATE, g_variant_new("(sssi)", arr[0], arr[1], arr[2], timeout_ms),
				NULL);
//LCOV_EXCL_START System Error
		if (ret_dbus < 0)
			return ret_dbus;
//LCOV_EXCL_STOP
		else
			privilege = 1;
	}

	arr[0] = get_state_str(state);
	if (!arr[0])
		return -EINVAL;

	return gdbus_call_async_with_reply(DEVICED_BUS_NAME,
			DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
			METHOD_LOCK_STATE, g_variant_new("(sssi)", arr[0], arr[1], arr[2], timeout_ms), lock_cb, -1, NULL);
}

static void unlock_cb(GVariant *result, void *data, GError *err)
{
	int ret_val;

	if (!result) {
//LCOV_EXCL_START System Error
		_E("no message : %s", err->message);
		return;
//LCOV_EXCL_STOP
	}

	g_variant_get(result, "(i)", &ret_val);
	_D("%s-%s : %d", DEVICED_INTERFACE_DISPLAY, METHOD_UNLOCK_STATE, ret_val);
}

static int unlock_state(display_state_e state, unsigned int flag)
{
	char *arr[2];
	int ret_dbus;
	static int privilege = -1;

	if (flag == PM_SLEEP_MARGIN)
		arr[1] = STR_SLEEP_MARGIN;
	else if (flag == PM_RESET_TIMER)
		arr[1] = STR_RESET_TIMER;
	else if (flag == PM_KEEP_TIMER)
		arr[1] = STR_KEEP_TIMER;
	else
		return -EINVAL;

	if (privilege < 0) {
		arr[0] = "privilege check";

		ret_dbus = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
				DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
				METHOD_UNLOCK_STATE, g_variant_new("(ss)", arr[0], arr[1]),
				NULL);
//LCOV_EXCL_START System Error
		if (ret_dbus < 0)
			return ret_dbus;
//LCOV_EXCL_STOP
		else
			privilege = 1;
	}

	arr[0] = get_state_str(state);
	if (!arr[0])
		return -EINVAL;

	return gdbus_call_async_with_reply(DEVICED_BUS_NAME,
			DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
			METHOD_UNLOCK_STATE, g_variant_new("(ss)", arr[0], arr[1]), unlock_cb, -1, NULL);
}

int device_power_request_lock(power_lock_e type, int timeout_ms)
{
	int ret;
	static long num_calls = 0;

	_I("power_lock_e = %d", type);

	if (!is_feature_display_supported() || !is_feature_display_state_supported()) {
		if ((type == POWER_LOCK_DISPLAY) || (type == POWER_LOCK_DISPLAY_DIM)) {
			return DEVICE_ERROR_NOT_SUPPORTED;
		} else if (type == POWER_LOCK_CPU) {
			/* in case of headless, request for cpu lock is handled
			 * in power module of deviced, not in display module */

			/* In current policy, headless doesn't support cpulock. Disable it.
			ret = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
				DEVICED_PATH_POWER, DEVICED_INTERFACE_POWER,
				"LockCpu", g_variant_new("(i)", timeout_ms), NULL);
			return errno_to_device_error(ret);
			*/
			return DEVICE_ERROR_NONE;
		}
	}

	if (check_async_call_rate(&num_calls) < 0) {
//LCOV_EXCL_START System Error
		_E("Rejected by too frequent calls; %d (calls per sec.) limit is violated."
				, CHECK_RATE_THRESHOLD);
		return DEVICE_ERROR_OPERATION_FAILED;
//LCOV_EXCL_STOP
	}

	if (timeout_ms < 0)
		return DEVICE_ERROR_INVALID_PARAMETER;

	if (type == POWER_LOCK_CPU) {
		ret = lock_state(DISPLAY_STATE_SCREEN_OFF, STAY_CUR_STATE, timeout_ms);
		if (ret == 0 &&
			(timeout_ms == 0 || timeout_ms > lock_timeout.release))
			add_off_lock_timeout();
		power_locked_CPU = true;
	} else if (type == POWER_LOCK_DISPLAY) {
		ret = lock_state(DISPLAY_STATE_NORMAL, STAY_CUR_STATE, timeout_ms);
		power_locked_DISPLAY = true;
	}
	else if (type == POWER_LOCK_DISPLAY_DIM) {
		ret = lock_state(DISPLAY_STATE_SCREEN_DIM, STAY_CUR_STATE, timeout_ms);
		power_locked_DISPLAY_DIM = true;
	}
	else
		return DEVICE_ERROR_INVALID_PARAMETER;

	return errno_to_device_error(ret);
}

int device_power_release_lock(power_lock_e type)
{
	int ret_val;

	_I("power_lock_e = %d", type);

	if (!is_feature_display_supported() || !is_feature_display_state_supported()) {
		if ((type == POWER_LOCK_DISPLAY) || (type == POWER_LOCK_DISPLAY_DIM)) {
			return DEVICE_ERROR_NOT_SUPPORTED;
		} else if (type == POWER_LOCK_CPU) {
			/* in case of headless, request for cpu unlock is handled
			 * in power module of deviced, not in display module */

			/* In current policy, headless doesn't support cpulock. Disable it.
			ret_val = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
				DEVICED_PATH_POWER, DEVICED_INTERFACE_POWER,
				"UnlockCpu", NULL, NULL);
			return errno_to_device_error(ret_val);
			*/
			return DEVICE_ERROR_NONE;
		}
	}

	if (type == POWER_LOCK_CPU && power_locked_CPU) {
		ret_val = unlock_state(DISPLAY_STATE_SCREEN_OFF, PM_SLEEP_MARGIN);
		if (ret_val == 0) {
			remove_off_lock_timeout();
			remove_padding_timeout();
			power_locked_CPU = false;
		}
	} else if (type == POWER_LOCK_DISPLAY && power_locked_DISPLAY) {
		ret_val = unlock_state(DISPLAY_STATE_NORMAL, PM_KEEP_TIMER);
		if (ret_val == 0) power_locked_DISPLAY = false;
	}
	else if (type == POWER_LOCK_DISPLAY_DIM && power_locked_DISPLAY_DIM) {
		ret_val = unlock_state(DISPLAY_STATE_SCREEN_DIM, PM_KEEP_TIMER);
		if (ret_val == 0) power_locked_DISPLAY_DIM = false;
	}
	else
		return DEVICE_ERROR_INVALID_PARAMETER;

	return errno_to_device_error(ret_val);
}

int device_power_wakeup(bool dim)
{
	int ret_val;

	ret_val = is_feature_display_state_supported();
	if (!ret_val)
		return DEVICE_ERROR_NOT_SUPPORTED;

	if (dim)
		return device_display_change_state(DISPLAY_STATE_SCREEN_DIM);

	return device_display_change_state(DISPLAY_STATE_NORMAL);
}

//LCOV_EXCL_START Not available to test (Reboot during TCT)
int device_power_reboot(const char *reason)
{
	const char *method;
	GVariant *param;
	int ret_dbus;

	if (reason) {
		method = METHOD_POWEROFF_WITH_OPTION;
		param = g_variant_new("(ss)", TYPE_REBOOT, reason);
	} else {
		method = METHOD_POWEROFF;
		param = g_variant_new("(s)", TYPE_REBOOT);
	}

	ret_dbus = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
			DEVICED_PATH_POWEROFF,
			DEVICED_INTERFACE_POWEROFF,
			method, param, NULL);
	return errno_to_device_error(ret_dbus);
}
//LCOV_EXCL_STOP

//LCOV_EXCL_START Not available to test (Reboot during TCT)
int device_power_poweroff(void)
{
	int ret_dbus;

	ret_dbus = gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME,
			DEVICED_PATH_POWEROFF, DEVICED_INTERFACE_POWEROFF,
			METHOD_POWEROFF, g_variant_new("(s)", TYPE_POWEROFF),
			NULL);

	return errno_to_device_error(ret_dbus);
}
//LCOV_EXCL_STOP

static int power_load_config(struct parse_result *result, void *data)
{
	unsigned int value;

	if (!result)
		return 0;

	if (strncmp(result->section, "Power", 6))
		return 0;

	value = strtoul(result->value, NULL, 10);
	if (value == 0)
		return 0;

	if (!strncmp(result->name, "LockTimeout", 12)) {
		_I("Power Lock Auto Release Timeout: %u msec", value);
		lock_timeout.release = value;
	} else if (!strncmp(result->name, "LockPaddingTimeout", 12)) {
		_I("Power Lock Auto Release Padding Timeout: %u msec", value);
		lock_timeout.padding = value;
	}

	return 0;
}

static void __CONSTRUCTOR__ power_init(void)
{
	int ret_val;

	ret_val = config_parse(POWER_CONF, power_load_config, NULL);
	if (ret_val < 0)
		_E("Failed to load config file (%d)", ret_val); //LCOV_EXCL_LINE Logs
}