3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012 Nordic Semiconductor Inc.
6 * Copyright (C) 2012 Instituto Nokia de Tecnologia - INdT
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <sys/types.h>
43 #define HOG_SUSPEND_FIFO "/tmp/hogsuspend"
45 static suspend_event suspend_cb = NULL;
46 static resume_event resume_cb = NULL;
47 static guint watch = 0;
49 static int fifo_open(void);
51 static gboolean read_fifo(GIOChannel *io, GIOCondition cond, gpointer user_data)
54 gsize offset, left, bread;
57 if (cond & (G_IO_ERR | G_IO_HUP)) {
59 * Both ends needs to be open simultaneously before proceeding
60 * any input or output operation. When the remote closes the
61 * channel, hup signal is received on this end.
68 left = sizeof(buffer) - 1;
69 memset(buffer, 0, sizeof(buffer));
72 iostatus = g_io_channel_read_chars(io, &buffer[offset], left,
79 } while (iostatus == G_IO_STATUS_NORMAL);
81 if (g_ascii_strncasecmp("suspend", buffer, 7) == 0)
83 else if (g_ascii_strncasecmp("resume", buffer, 6) == 0)
89 static int fifo_open(void)
91 GIOCondition condition = G_IO_IN | G_IO_ERR | G_IO_HUP;
95 fd = open(HOG_SUSPEND_FIFO, O_RDONLY | O_NONBLOCK);
98 error("Can't open FIFO (%s): %s(%d)", HOG_SUSPEND_FIFO,
99 strerror(-err), -err);
103 fifoio = g_io_channel_unix_new(fd);
104 g_io_channel_set_close_on_unref(fifoio, TRUE);
106 watch = g_io_add_watch(fifoio, condition, read_fifo, NULL);
108 g_io_channel_unref(fifoio);
113 int suspend_init(suspend_event suspend, resume_event resume)
120 suspend_cb = suspend;
123 if (stat(HOG_SUSPEND_FIFO, &st) == 0) {
124 if (!S_ISFIFO(st.st_mode)) {
125 error("Unexpected non-FIFO %s file", HOG_SUSPEND_FIFO);
129 if (unlink(HOG_SUSPEND_FIFO) < 0) {
131 error("Failed to remove FIFO (%s): %s (%d)",
132 HOG_SUSPEND_FIFO, strerror(-err), -err);
137 if (mkfifo(HOG_SUSPEND_FIFO, S_IRUSR | S_IWUSR) < 0) {
140 error("Can't create FIFO (%s): %s (%d)", HOG_SUSPEND_FIFO,
141 strerror(-err), -err);
145 DBG("Created suspend-dummy FIFO on %s", HOG_SUSPEND_FIFO);
149 unlink(HOG_SUSPEND_FIFO);
154 void suspend_exit(void)
157 g_source_remove(watch);
161 unlink(HOG_SUSPEND_FIFO);