summaryrefslogtreecommitdiff
path: root/pm_poll.c
blob: 271f866b3b41e6470a0fa509effae7140fbc6c11 (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
/*
 * power-manager
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *
 * 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.
*/


/**
 * @file	pm_poll.c
 * @version	0.2
 * @brief	 Power Manager poll implementation (input devices & a domain socket file)
 *
 * This file includes the input device poll implementation.
 * Default input devices are /dev/event0 and /dev/event1
 * User can use "PM_INPUT" for setting another input device poll in an environment file (/etc/profile). 
 * (ex: PM_INPUT=/dev/event0:/dev/event1:/dev/event5 )
 */

#include <glib.h>
#include <stdio.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "util.h"
#include "pm_core.h"
#include "pm_poll.h"

#define DEV_PATH_DLM	":"

PMMsg recv_data;
int (*g_pm_callback) (int, PMMsg *);

#ifdef ENABLE_KEY_FILTER
extern int check_key_filter(int length, char buf[]);
#  define CHECK_KEY_FILTER(a, b) do {\
							if (check_key_filter(a, b) != 0)\
								return TRUE;\
							} while (0);

#else
#  define CHECK_KEY_FILTER(a, b)
#endif

#define DEFAULT_DEV_PATH "/dev/event1:/dev/event0"

static GSource *src;
static GSourceFuncs *funcs;
static int sockfd;

static gboolean pm_check(GSource *src)
{
	GSList *fd_list;
	GPollFD *tmp;

	fd_list = src->poll_fds;
	do {
		tmp = (GPollFD *) fd_list->data;
		if ((tmp->revents & (POLLIN | POLLPRI)))
			return TRUE;
		fd_list = fd_list->next;
	} while (fd_list);

	return FALSE;
}

static gboolean pm_dispatch(GSource *src, GSourceFunc callback, gpointer data)
{
	callback(data);
	return TRUE;
}

static gboolean pm_prepare(GSource *src, gint *timeout)
{
	return FALSE;
}

gboolean pm_handler(gpointer data)
{
	char buf[1024];
	struct sockaddr_un clientaddr;

	GPollFD *gpollfd = (GPollFD *) data;
	int ret;
	int clilen = sizeof(clientaddr);

	if (g_pm_callback == NULL) {
		return FALSE;
	}
	if (gpollfd->fd == sockfd) {
		ret =
		    recvfrom(gpollfd->fd, &recv_data, sizeof(recv_data), 0,
			     (struct sockaddr *)&clientaddr,
			     (socklen_t *)&clilen);
		(*g_pm_callback) (PM_CONTROL_EVENT, &recv_data);
	} else {
		ret = read(gpollfd->fd, buf, sizeof(buf));
		CHECK_KEY_FILTER(ret, buf);
		(*g_pm_callback) (INPUT_POLL_EVENT, NULL);
	}

	return TRUE;
}

static GList *find_indev_list(char *path)
{
	int i;
	guint total = 0;
	indev *tmp;

	total = g_list_length(indev_list);

	for (i = 0; i < total; i++) {
		tmp = (indev*) (g_list_nth(indev_list, i)->data);

		if (!strcmp(tmp->dev_path, path)) {
			LOGINFO("%s is already in dev list! gfd(%d)", path, tmp->dev_fd);
			return g_list_nth(indev_list, i);
		}
	}

	return NULL;
}

static int init_sock(char *sock_path)
{
	struct sockaddr_un serveraddr;
	int fd;

	LOGINFO("initialize pm_socket for pm_control library");

	if (sock_path == NULL || strcmp(sock_path, SOCK_PATH)) {
		LOGERR("invalid sock_path= %s");
		return -1;
	}

	fd = socket(AF_UNIX, SOCK_DGRAM, 0);
	if (fd < 0) {
		LOGERR("socket error");
		return -1;
	}

	unlink(sock_path);

	bzero(&serveraddr, sizeof(serveraddr));
	serveraddr.sun_family = AF_UNIX;
	strncpy(serveraddr.sun_path, sock_path, sizeof(serveraddr.sun_path) - 1);

	if (bind(fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) {
		LOGERR("bind error");
		close(fd);
		return -1;
	}

	if (chmod(sock_path, (S_IRWXU | S_IRWXG | S_IRWXO)) < 0)	/* 0777 */
		LOGERR("failed to change the socket permission");

	if (!strcmp(sock_path, SOCK_PATH))
		sockfd = fd;

	LOGINFO("init sock() sueccess!");
	return fd;
}

int init_pm_poll(int (*pm_callback) (int, PMMsg *))
{

	guint ret;
	char *dev_paths, *path_tok, *pm_input_env, *save_ptr;
	int dev_paths_size;

	GPollFD *gpollfd;

	g_pm_callback = pm_callback;

	LOGINFO
	    ("initialize pm poll - input devices and domain socket(libpmapi)");

	pm_input_env = getenv("PM_INPUT");
	if ((pm_input_env != NULL) && (strlen(pm_input_env) < 1024)) {
		LOGINFO("Getting input device path from environment: %s",
		       pm_input_env);
		/* Add 2 bytes for following strncat() */
		dev_paths_size =  strlen(pm_input_env) + strlen(SOCK_PATH) + strlen(DEV_PATH_DLM) + 1;
		dev_paths = (char *)malloc(dev_paths_size);
		snprintf(dev_paths, dev_paths_size, "%s", pm_input_env);
	} else {
		/* Add 2 bytes for following strncat() */
		dev_paths_size = strlen(DEFAULT_DEV_PATH) + strlen(SOCK_PATH) + strlen(DEV_PATH_DLM) + 1;
		dev_paths = (char *)malloc(dev_paths_size);
		snprintf(dev_paths, dev_paths_size, "%s", DEFAULT_DEV_PATH);
	}

	/* add the UNIX domain socket file path */
	strncat(dev_paths, DEV_PATH_DLM, strlen(DEV_PATH_DLM));
	strncat(dev_paths, SOCK_PATH, strlen(SOCK_PATH));
	dev_paths[dev_paths_size - 1] = '\0';

	path_tok = strtok_r(dev_paths, DEV_PATH_DLM, &save_ptr);
	if (path_tok == NULL) {
		LOGERR("Device Path Tokeninzing Failed");
		free(dev_paths);
		return -1;
	}

	funcs = (GSourceFuncs *) g_malloc(sizeof(GSourceFuncs));
	funcs->prepare = pm_prepare;
	funcs->check = pm_check;
	funcs->dispatch = pm_dispatch;
	funcs->finalize = NULL;

	do {
		indev *adddev = NULL;
		char *path, *new_path;
		int len;

		src = g_source_new(funcs, sizeof(GSource));

		gpollfd = (GPollFD *) g_malloc(sizeof(GPollFD));
		gpollfd->events = POLLIN;

		if (strcmp(path_tok, SOCK_PATH) == 0) {
			gpollfd->fd = init_sock(SOCK_PATH);
			path = SOCK_PATH;
			LOGINFO("pm_poll domain socket file: %s, fd: %d",
			       path_tok, gpollfd->fd);
		} else {
			gpollfd->fd = open(path_tok, O_RDONLY);
			path = path_tok;
			LOGINFO("pm_poll input device file: %s, fd: %d",
			       path_tok, gpollfd->fd);
		}

		if (gpollfd->fd == -1) {
			LOGERR("Cannot open the file: %s", path_tok);
			free(dev_paths);
			return -1;
		}

		len = strlen(path) + 1;
		new_path = (char *) malloc(len);
		if (!new_path) {
			LOGERR("Fail to alloc new path: %s", path_tok);
			free(dev_paths);
			return -1;
		}
		strncpy(new_path, path, len);

		adddev = (indev *) malloc(sizeof(indev));
		if (!adddev) {
			LOGERR("Fail to alloc indev: %s", path_tok);
			free(new_path);
			free(dev_paths);
			return -1;
		}
		adddev->dev_path = new_path;
		adddev->dev_src = src;
		adddev->dev_fd = gpollfd;

		indev_list = g_list_append(indev_list, adddev);
		LOGINFO("pm_poll for input dev file(path:%s, gsource:%d, gpollfd:%d",
			    adddev->dev_path, adddev->dev_src, adddev->dev_fd);

		g_source_add_poll(src, gpollfd);
		g_source_set_callback(src, (GSourceFunc) pm_handler,
				      (gpointer) gpollfd, NULL);

		g_source_set_priority(src, G_PRIORITY_LOW);
		ret = g_source_attach(src, NULL);
		if (ret == 0) {
			LOGERR("Failed g_source_attach() in init_pm_poll()");
			free(dev_paths);
			return -1;
		}
		g_source_unref(src);

	} while ((path_tok = strtok_r(NULL, DEV_PATH_DLM, &save_ptr)));

	free(dev_paths);
	return 0;
}

int exit_pm_poll()
{
	g_free(funcs);
	close(sockfd);
	unlink(SOCK_PATH);
	LOGINFO("pm_poll is finished");
	return 0;
}

int init_pm_poll_input(int (*pm_callback)(int , PMMsg * ), const char *path)
{
	guint ret;
	indev *adddev = NULL;

	g_pm_callback = pm_callback;

	GPollFD *gpollfd;
	const char *devpath;
	GSource *devsrc;

	LOGINFO("initialize pm poll for bt %s",path);

	if(find_indev_list(path)) {
		LOGERR("%s is already in dev list!", path);
		return -1;
	}

	adddev=(indev *)malloc(sizeof(indev));
	adddev->dev_fd = (GPollFD *)g_malloc(sizeof(GPollFD));
	(adddev->dev_fd)->events = POLLIN;
	(adddev->dev_fd)->fd = open(path, O_RDONLY);
	if((adddev->dev_fd)->fd == -1) {
		LOGERR("Cannot open the file for BT: %s",path);
		g_free(adddev->dev_fd);
		free(adddev);
		return -1;
	}
	adddev->dev_path = (char *)malloc(strlen(path) + 1);
	strncpy(adddev->dev_path, path, strlen(path) + 1);
	adddev->dev_src = g_source_new(funcs, sizeof(GSource));
	LOGINFO("pm_poll for BT input device file(path: %s, gsource: %d, gpollfd: %d", adddev->dev_path, adddev->dev_src, adddev->dev_fd);
	indev_list=g_list_append(indev_list, adddev);
	
	g_source_add_poll(adddev->dev_src, adddev->dev_fd);
	
	g_source_set_callback(adddev->dev_src, (GSourceFunc) pm_handler, (gpointer)adddev->dev_fd, NULL);
	

	g_source_set_priority(adddev->dev_src, G_PRIORITY_LOW );
	
	ret = g_source_attach(adddev->dev_src, NULL);
	if(ret == 0)
	{
		LOGERR("Failed g_source_attach() in init_pm_poll()");
		return -1;
	}
	g_source_unref(adddev->dev_src);
	return 0;
}