3 * oFono - Open Source Telephony
5 * Copyright (C) 2011-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
35 #include <sys/signalfd.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
42 #define IFX_RESET_PATH "/sys/module/hsi_ffl_tty/parameters/reset_modem"
44 static gchar *option_device = NULL;
45 static gboolean option_ifx = FALSE;
47 static GMainLoop *main_loop;
48 static bool main_terminated;
50 static int device_fd = -1;
51 static int client_fd = -1;
53 static guint device_watch = 0;
54 static guint client_watch = 0;
56 static gboolean shutdown_timeout(gpointer user_data)
58 g_main_loop_quit(main_loop);
63 static void do_terminate(void)
68 main_terminated = true;
70 g_timeout_add_seconds(1, shutdown_timeout, NULL);
73 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
76 struct signalfd_siginfo si;
80 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
83 fd = g_io_channel_unix_get_fd(channel);
85 result = read(fd, &si, sizeof(si));
86 if (result != sizeof(si))
89 switch (si.ssi_signo) {
99 static guint create_watch(int fd, GIOFunc func)
104 channel = g_io_channel_unix_new(fd);
106 g_io_channel_set_close_on_unref(channel, TRUE);
107 g_io_channel_set_encoding(channel, NULL, NULL);
108 g_io_channel_set_buffered(channel, FALSE);
110 source = g_io_add_watch(channel,
111 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, func, NULL);
113 g_io_channel_unref(channel);
118 static guint setup_signalfd(void)
124 sigaddset(&mask, SIGINT);
125 sigaddset(&mask, SIGTERM);
127 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
128 perror("Failed to set signal mask");
132 fd = signalfd(-1, &mask, 0);
134 perror("Failed to create signal descriptor");
138 return create_watch(fd, signal_handler);
141 static int write_file(const char *path, const char *value)
146 fd = open(path, O_RDWR | O_NOCTTY | O_CLOEXEC);
148 perror("Failed to open file");
152 written = write(fd, value, strlen(value));
154 perror("Failed to write value");
161 static int open_device(const char *path)
166 /* Switch TTY to raw mode */
167 memset(&ti, 0, sizeof(ti));
170 fd = open(path, O_RDWR | O_NOCTTY | O_CLOEXEC);
172 perror("Failed to open device");
176 tcflush(fd, TCIOFLUSH);
177 tcsetattr(fd, TCSANOW, &ti);
182 static gboolean forward_data(GIOCondition cond, int input_fd, int output_fd)
184 unsigned char buf[1024];
185 ssize_t bytes_read, bytes_written;
187 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
190 bytes_read = read(input_fd, buf, sizeof(buf));
194 bytes_written = write(output_fd, buf, bytes_read);
195 if (bytes_written != bytes_read)
201 static gboolean device_handler(GIOChannel *channel, GIOCondition cond,
204 if (forward_data(cond, device_fd, client_fd) == FALSE) {
205 g_printerr("Closing device descriptor\n");
206 if (client_watch > 0) {
207 g_source_remove(client_watch);
218 static gboolean client_handler(GIOChannel *channel, GIOCondition cond,
221 if (forward_data(cond, client_fd, device_fd) == FALSE) {
222 g_printerr("Closing client connection\n");
223 if (device_watch > 0) {
224 g_source_remove(device_watch);
235 static gboolean accept_handler(GIOChannel *channel, GIOCondition cond,
238 struct sockaddr_in addr;
242 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
245 fd = g_io_channel_unix_get_fd(channel);
247 memset(&addr, 0, sizeof(addr));
248 addrlen = sizeof(addr);
250 nfd = accept4(fd, (struct sockaddr *) &addr, &addrlen, SOCK_CLOEXEC);
254 if (device_watch > 0) {
255 g_printerr("Closing previous descriptors\n");
256 g_source_remove(device_watch);
259 if (client_watch > 0) {
260 g_source_remove(client_watch);
265 if (option_ifx == TRUE) {
266 write_file(IFX_RESET_PATH, "1");
268 write_file(IFX_RESET_PATH, "0");
272 device_fd = open_device(option_device);
278 device_watch = create_watch(device_fd, device_handler);
279 if (device_watch == 0) {
284 client_watch = create_watch(nfd, client_handler);
285 if (client_watch == 0) {
286 g_source_remove(device_watch);
297 static guint setup_server(void)
299 struct sockaddr_in addr;
302 fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
304 perror("Failed to open server socket");
308 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
310 memset(&addr, 0, sizeof(addr));
311 addr.sin_family = AF_INET;
312 addr.sin_addr.s_addr = INADDR_ANY;
313 addr.sin_port = htons(12345);
315 if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
316 perror("Failed to bind server socket");
321 if (listen(fd, 1) < 0) {
322 perror("Failed to listen server socket");
327 return create_watch(fd, accept_handler);
330 static GOptionEntry options[] = {
331 { "device", 0, 0, G_OPTION_ARG_STRING, &option_device,
332 "Specify device to use", "DEVNODE" },
333 { "ifx", 0, 0, G_OPTION_ARG_NONE, &option_ifx,
334 "Use Infineon reset handling" },
338 int main(int argc, char **argv)
340 GOptionContext *context;
341 GError *error = NULL;
345 context = g_option_context_new(NULL);
346 g_option_context_add_main_entries(context, options, NULL);
348 if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
350 g_printerr("%s\n", error->message);
353 g_printerr("An unknown error occurred\n");
357 g_option_context_free(context);
359 if (option_device == NULL) {
360 if (option_ifx == TRUE) {
361 option_device = g_strdup("/dev/ttyIFX0");
363 g_printerr("No valid device specified\n");
368 main_loop = g_main_loop_new(NULL, FALSE);
369 signal_watch = setup_signalfd();
370 server_watch = setup_server();
372 g_main_loop_run(main_loop);
374 g_source_remove(server_watch);
375 g_source_remove(signal_watch);
376 g_main_loop_unref(main_loop);
378 g_free(option_device);