Upgrade ofono to 1.2
[profile/ivi/ofono.git] / tools / tty-redirector.c
1 /*
2  *
3  *  oFono - Open Source Telephony
4  *
5  *  Copyright (C) 2011-2012  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define _GNU_SOURCE
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdbool.h>
33 #include <termios.h>
34 #include <signal.h>
35 #include <sys/signalfd.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39
40 #include <glib.h>
41
42 #define IFX_RESET_PATH "/sys/module/hsi_ffl_tty/parameters/reset_modem"
43
44 static gchar *option_device = NULL;
45 static gboolean option_ifx = FALSE;
46
47 static GMainLoop *main_loop;
48 static bool main_terminated;
49
50 static int device_fd = -1;
51 static int client_fd = -1;
52
53 static guint device_watch = 0;
54 static guint client_watch = 0;
55
56 static gboolean shutdown_timeout(gpointer user_data)
57 {
58         g_main_loop_quit(main_loop);
59
60         return FALSE;
61 }
62
63 static void do_terminate(void)
64 {
65         if (main_terminated)
66                 return;
67
68         main_terminated = true;
69
70         g_timeout_add_seconds(1, shutdown_timeout, NULL);
71 }
72
73 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
74                                                         gpointer user_data)
75 {
76         struct signalfd_siginfo si;
77         ssize_t result;
78         int fd;
79
80         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
81                 return FALSE;
82
83         fd = g_io_channel_unix_get_fd(channel);
84
85         result = read(fd, &si, sizeof(si));
86         if (result != sizeof(si))
87                 return FALSE;
88
89         switch (si.ssi_signo) {
90         case SIGINT:
91         case SIGTERM:
92                 do_terminate();
93                 break;
94         }
95
96         return TRUE;
97 }
98
99 static guint create_watch(int fd, GIOFunc func)
100 {
101         GIOChannel *channel;
102         guint source;
103
104         channel = g_io_channel_unix_new(fd);
105
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);
109
110         source = g_io_add_watch(channel,
111                         G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, func, NULL);
112
113         g_io_channel_unref(channel);
114
115         return source;
116 }
117
118 static guint setup_signalfd(void)
119 {
120         sigset_t mask;
121         int fd;
122
123         sigemptyset(&mask);
124         sigaddset(&mask, SIGINT);
125         sigaddset(&mask, SIGTERM);
126
127         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
128                 perror("Failed to set signal mask");
129                 return 0;
130         }
131
132         fd = signalfd(-1, &mask, 0);
133         if (fd < 0) {
134                 perror("Failed to create signal descriptor");
135                 return 0;
136         }
137
138         return create_watch(fd, signal_handler);
139 }
140
141 static int write_file(const char *path, const char *value)
142 {
143         ssize_t written;
144         int fd;
145
146         fd = open(path, O_RDWR | O_NOCTTY | O_CLOEXEC);
147         if (fd < 0) {
148                 perror("Failed to open file");
149                 return -1;
150         }
151
152         written = write(fd, value, strlen(value));
153         if (written < 0) {
154                 perror("Failed to write value");
155                 return -1;
156         }
157
158         return 0;
159 }
160
161 static int open_device(const char *path)
162 {
163         struct termios ti;
164         int fd;
165
166         /* Switch TTY to raw mode */
167         memset(&ti, 0, sizeof(ti));
168         cfmakeraw(&ti);
169
170         fd = open(path, O_RDWR | O_NOCTTY | O_CLOEXEC);
171         if (fd < 0) {
172                 perror("Failed to open device");
173                 return -1;
174         }
175
176         tcflush(fd, TCIOFLUSH);
177         tcsetattr(fd, TCSANOW, &ti);
178
179         return fd;
180 }
181
182 static gboolean forward_data(GIOCondition cond, int input_fd, int output_fd)
183 {
184         unsigned char buf[1024];
185         ssize_t bytes_read, bytes_written;
186
187         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
188                 return FALSE;
189
190         bytes_read = read(input_fd, buf, sizeof(buf));
191         if (bytes_read < 0)
192                 return FALSE;
193
194         bytes_written = write(output_fd, buf, bytes_read);
195         if (bytes_written != bytes_read)
196                 return FALSE;
197
198         return TRUE;
199 }
200
201 static gboolean device_handler(GIOChannel *channel, GIOCondition cond,
202                                                         gpointer user_data)
203 {
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);
208                         client_watch = 0;
209                 }
210
211                 device_watch = 0;
212                 return FALSE;
213         }
214
215         return TRUE;
216 }
217
218 static gboolean client_handler(GIOChannel *channel, GIOCondition cond,
219                                                         gpointer user_data)
220 {
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);
225                         device_watch = 0;
226                 }
227
228                 client_watch = 0;
229                 return FALSE;
230         }
231
232         return TRUE;
233 }
234
235 static gboolean accept_handler(GIOChannel *channel, GIOCondition cond,
236                                                         gpointer user_data)
237 {
238         struct sockaddr_in addr;
239         socklen_t addrlen;
240         int fd, nfd;
241
242         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
243                 return FALSE;
244
245         fd = g_io_channel_unix_get_fd(channel);
246
247         memset(&addr, 0, sizeof(addr));
248         addrlen = sizeof(addr);
249
250         nfd = accept4(fd, (struct sockaddr *) &addr, &addrlen, SOCK_CLOEXEC);
251         if (nfd < 0)
252                 return TRUE;
253
254         if (device_watch > 0) {
255                 g_printerr("Closing previous descriptors\n");
256                 g_source_remove(device_watch);
257                 device_watch = 0;
258
259                 if (client_watch > 0) {
260                         g_source_remove(client_watch);
261                         client_watch = 0;
262                 }
263         }
264
265         if (option_ifx == TRUE) {
266                 write_file(IFX_RESET_PATH, "1");
267                 sleep(1);
268                 write_file(IFX_RESET_PATH, "0");
269                 sleep(1);
270         }
271
272         device_fd = open_device(option_device);
273         if (device_fd < 0) {
274                 close(nfd);
275                 return TRUE;
276         }
277
278         device_watch = create_watch(device_fd, device_handler);
279         if (device_watch == 0) {
280                 close(nfd);
281                 return TRUE;
282         }
283
284         client_watch = create_watch(nfd, client_handler);
285         if (client_watch == 0) {
286                 g_source_remove(device_watch);
287                 device_watch = 0;
288                 close(nfd);
289                 return TRUE;
290         }
291
292         client_fd = nfd;
293
294         return TRUE;
295 }
296
297 static guint setup_server(void)
298 {
299         struct sockaddr_in addr;
300         int fd, opt = 1;
301
302         fd = socket(PF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
303         if (fd < 0) {
304                 perror("Failed to open server socket");
305                 return 0;
306         }
307
308         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
309
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);
314
315         if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
316                 perror("Failed to bind server socket");
317                 close(fd);
318                 return 0;
319         }
320
321         if (listen(fd, 1) < 0) {
322                 perror("Failed to listen server socket");
323                 close(fd);
324                 return 0;
325         }
326
327         return create_watch(fd, accept_handler);
328 }
329
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" },
335         { NULL },
336 };
337
338 int main(int argc, char **argv)
339 {
340         GOptionContext *context;
341         GError *error = NULL;
342         guint signal_watch;
343         guint server_watch;
344
345         context = g_option_context_new(NULL);
346         g_option_context_add_main_entries(context, options, NULL);
347
348         if (g_option_context_parse(context, &argc, &argv, &error) == FALSE) {
349                 if (error != NULL) {
350                         g_printerr("%s\n", error->message);
351                         g_error_free(error);
352                 } else
353                         g_printerr("An unknown error occurred\n");
354                 return EXIT_FAILURE;
355         }
356
357         g_option_context_free(context);
358
359         if (option_device == NULL) {
360                 if (option_ifx == TRUE) {
361                         option_device = g_strdup("/dev/ttyIFX0");
362                 } else {
363                         g_printerr("No valid device specified\n");
364                         return EXIT_FAILURE;
365                 }
366         }
367
368         main_loop = g_main_loop_new(NULL, FALSE);
369         signal_watch = setup_signalfd();
370         server_watch = setup_server();
371
372         g_main_loop_run(main_loop);
373
374         g_source_remove(server_watch);
375         g_source_remove(signal_watch);
376         g_main_loop_unref(main_loop);
377
378         g_free(option_device);
379
380         return EXIT_SUCCESS;
381 }