~phablet-team/ofono/pro5-support-and-more

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
/*
 *
 *  oFono - Open Source Telephony - Android based wakelocks
 *
 *  Copyright (C) 2016 Canonical Ltd.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include <dlfcn.h>

#include <glib.h>

#define OFONO_API_SUBJECT_TO_CHANGE

#include "plugin.h"
#include "log.h"
#include "wakelock.h"

#define ANDROID_WAKELOCK_LOCK_PATH		"/sys/power/wake_lock"
#define ANDROID_WAKELOCK_UNLOCK_PATH	"/sys/power/wake_unlock"

struct wakelock {
	char *name;
	unsigned int acquisitions;
};

GSList *locks = NULL;

static int file_exists(char const *filename)
{
	struct stat st;

	return stat(filename, &st) == 0;
}

static int write_file(const char *file, const char *content)
{
	int fd;
	unsigned int r = 0;

	fd = open(file, O_WRONLY);

	if (fd == -1)
		return -EIO;

	r = write(fd, content, strlen(content));

	close(fd);

	if (r != strlen(content))
		return -EIO;

	return 0;
}

static int wakelock_lock(const char *name)
{
	return write_file(ANDROID_WAKELOCK_LOCK_PATH, name);
}

static int wakelock_unlock(const char *name)
{
	return write_file(ANDROID_WAKELOCK_UNLOCK_PATH, name);
}

static int android_wakelock_acquire(struct wakelock *lock)
{
	if (lock == NULL)
		return -EINVAL;

	if (lock->acquisitions > 0) {
		lock->acquisitions++;
		return 0;
	}

	if (wakelock_lock(lock->name) < 0)
		return -EIO;

	lock->acquisitions++;

	return 0;
}

static int android_wakelock_release(struct wakelock *lock)
{
	if (lock == NULL)
		return -EINVAL;

	DBG("lock %p name %s acquisitions %d",
		lock, lock->name, lock->acquisitions);

	if (!lock->acquisitions) {
		ofono_warn("Attempted to release already released lock %s", lock->name);
		return -EINVAL;
	}

	if (lock->acquisitions > 1) {
		lock->acquisitions--;
		DBG("lock %s released acquisitions %d", lock->name, lock->acquisitions);
		return 0;
	}

	if (wakelock_unlock(lock->name) < 0)
		return -EIO;

	lock->acquisitions = 0;

	DBG("lock %s was released", lock->name);

	return 0;
}

static int android_wakelock_create(const char *name, struct wakelock **lock)
{
	if (lock == NULL)
		return -EINVAL;

	*lock = g_new0(struct wakelock, 1);
	(*lock)->name = g_strdup(name);
	(*lock)->acquisitions = 0;

	locks = g_slist_prepend(locks, *lock);

	DBG("wakelock %s create", name);

	return 0;
}

static int android_wakelock_free(struct wakelock *lock)
{
	if (lock == NULL)
		return -EINVAL;

	if (lock->acquisitions) {
		/* Need to force releasing the lock here */
		lock->acquisitions = 1;
		android_wakelock_release(lock);
	}

	locks = g_slist_remove(locks, lock);

	DBG("Freeing lock %s", lock->name);

	g_free(lock->name);
	g_free(lock);

	return 0;
}

static ofono_bool_t android_wakelock_is_locked(struct wakelock *lock)
{
	if (lock == NULL)
		return FALSE;

	return lock->acquisitions > 0;
}

struct wakelock_table driver = {
	.create = android_wakelock_create,
	.free = android_wakelock_free,
	.acquire = android_wakelock_acquire,
	.release = android_wakelock_release,
	.is_locked = android_wakelock_is_locked
};

static int android_wakelock_init(void)
{
	if (!file_exists(ANDROID_WAKELOCK_LOCK_PATH)) {
		ofono_warn("System does not support Android wakelocks.");
		return 0;
	}

	if (wakelock_plugin_register("android-wakelock", &driver) < 0) {
		ofono_error("Failed to register wakelock driver");
		return -EIO;
	}

	return 0;
}

static void android_wakelock_exit(void)
{
	wakelock_plugin_unregister();
}

OFONO_PLUGIN_DEFINE(android_wakelock, "Android Wakelock driver", VERSION,
			OFONO_PLUGIN_PRIORITY_DEFAULT, android_wakelock_init, android_wakelock_exit)