upgrade obexd to 0.47
[profile/ivi/obexd.git] / plugins / usb.c
1 /*
2  *
3  *  OBEX Server
4  *
5  *  Copyright (C) 2007-2010  Nokia Corporation
6  *  Copyright (C) 2007-2010  Marcel Holtmann <marcel@holtmann.org>
7  *
8  *
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.
13  *
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.
18  *
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
22  *
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <signal.h>
35 #include <fcntl.h>
36 #include <termios.h>
37 #include <inttypes.h>
38
39 #include <glib.h>
40 #include <gdbus.h>
41
42 #include "obexd.h"
43 #include "plugin.h"
44 #include "server.h"
45 #include "obex.h"
46 #include "transport.h"
47 #include "service.h"
48 #include "log.h"
49
50 static GIOChannel *usb_io = NULL;
51 static guint usb_reconnecting = 0;
52 static guint usb_watch = 0;
53 static DBusConnection *connection = NULL;
54
55 #define USB_RX_MTU 65535
56 #define USB_TX_MTU 65535
57 #define USB_DEVNODE "/dev/ttyGS0"
58
59 static int usb_connect(struct obex_server *server);
60
61 static void usb_disconnect(struct obex_server *server)
62 {
63         if (usb_reconnecting > 0) {
64                 g_source_remove(usb_reconnecting);
65                 usb_reconnecting = 0;
66         }
67
68         if (usb_watch > 0) {
69                 g_source_remove(usb_watch);
70                 usb_watch = 0;
71         }
72
73         /* already disconnected */
74         if (usb_io == NULL)
75                 return;
76
77         g_io_channel_shutdown(usb_io, TRUE, NULL);
78         g_io_channel_unref(usb_io);
79         usb_io = NULL;
80         DBG("disconnected");
81 }
82
83 static gboolean usb_reconnect(void *data)
84 {
85         struct obex_server *server = data;
86
87         DBG("reconnecting");
88         usb_reconnecting = 0;
89         usb_connect(server);
90
91         return FALSE;
92 }
93
94 static gboolean usb_watchdog(GIOChannel *io, GIOCondition cond,
95                                 void *user_data)
96 {
97         struct obex_server *server = user_data;
98
99         usb_watch = 0;
100         usb_disconnect(server);
101
102         if ((cond & G_IO_NVAL) == FALSE)
103                 usb_reconnecting = g_idle_add(usb_reconnect, server);
104
105         return FALSE;
106 }
107
108 static int usb_connect(struct obex_server *server)
109 {
110         struct termios options;
111         int fd, err, arg;
112         glong flags;
113
114         if (usb_reconnecting > 0) {
115                 g_source_remove(usb_reconnecting);
116                 usb_reconnecting = 0;
117         }
118
119         /* already connected */
120         if (usb_io != NULL)
121                 return 0;
122
123         fd = open(USB_DEVNODE, O_RDWR | O_NOCTTY);
124         if (fd < 0)
125                 return fd;
126
127         flags = fcntl(fd, F_GETFL);
128         fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
129
130         tcgetattr(fd, &options);
131         cfmakeraw(&options);
132         options.c_oflag &= ~ONLCR;
133         tcsetattr(fd, TCSANOW, &options);
134
135         arg = fcntl(fd, F_GETFL);
136         if (arg < 0) {
137                 err = -errno;
138                 goto failed;
139         }
140
141         arg |= O_NONBLOCK;
142         if (fcntl(fd, F_SETFL, arg) < 0) {
143                 err = -errno;
144                 goto failed;
145         }
146
147         usb_io = g_io_channel_unix_new(fd);
148         g_io_channel_set_close_on_unref(usb_io, TRUE);
149
150         err = obex_server_new_connection(server, usb_io, USB_TX_MTU,
151                                                         USB_RX_MTU, TRUE);
152         if (err < 0)
153                 goto failed;
154
155         usb_watch = g_io_add_watch(usb_io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
156                                         usb_watchdog, server);
157
158         DBG("Successfully opened %s", USB_DEVNODE);
159
160         return 0;
161
162 failed:
163         error("usb: %s (%d)", strerror(-err), -err);
164         if (usb_io == NULL)
165                 close(fd);
166         else
167                 usb_disconnect(server);
168         return err;
169 }
170
171 static void sig_usb(int sig)
172 {
173 }
174
175 static void usb_set_mode(struct obex_server *server, const char *mode)
176 {
177         DBG("%s", mode);
178
179         if (g_str_equal(mode, "ovi_suite") == TRUE)
180                 usb_connect(server);
181         else if (g_str_equal(mode, "USB disconnected") == TRUE)
182                 usb_disconnect(server);
183 }
184
185 static gboolean handle_signal(DBusConnection *connection,
186                                 DBusMessage *message, void *user_data)
187 {
188         struct obex_server *server = user_data;
189         const char *mode;
190
191         dbus_message_get_args(message, NULL,
192                                 DBUS_TYPE_STRING, &mode,
193                                 DBUS_TYPE_INVALID);
194
195         usb_set_mode(server, mode);
196
197         return TRUE;
198 }
199
200 static void usb_stop(void *data)
201 {
202         guint id = GPOINTER_TO_UINT(data);
203         g_dbus_remove_watch(connection, id);
204 }
205
206 static void mode_request_reply(DBusPendingCall *call, void *user_data)
207 {
208         struct obex_server *server = user_data;
209         DBusMessage *reply = dbus_pending_call_steal_reply(call);
210         DBusError derr;
211
212         dbus_error_init(&derr);
213         if (dbus_set_error_from_message(&derr, reply)) {
214                 error("usb: Replied with an error: %s, %s",
215                                 derr.name, derr.message);
216                 dbus_error_free(&derr);
217         } else {
218                 const char *mode;
219                 dbus_message_get_args(reply, NULL,
220                                 DBUS_TYPE_STRING, &mode,
221                                 DBUS_TYPE_INVALID);
222
223                 usb_set_mode(server, mode);
224         }
225
226         dbus_message_unref(reply);
227 }
228
229 static void *usb_start(struct obex_server *server, int *err)
230 {
231         guint id;
232         DBusMessage *msg;
233         DBusPendingCall *call;
234
235         msg = dbus_message_new_method_call("com.meego.usb_moded",
236                                                 "/com/meego/usb_moded",
237                                                 "com.meego.usb_moded",
238                                                 "mode_request");
239
240         if (dbus_connection_send_with_reply(connection,
241                                         msg, &call, -1) == FALSE) {
242                 error("usb: unable to send mode_request");
243                 dbus_message_unref(msg);
244                 goto fail;
245         }
246
247         dbus_pending_call_set_notify(call, mode_request_reply, server, NULL);
248         dbus_pending_call_unref(call);
249         dbus_message_unref(msg);
250
251         id = g_dbus_add_signal_watch(connection, NULL, NULL,
252                                         "com.meego.usb_moded",
253                                         "sig_usb_state_ind",
254                                         handle_signal, server, NULL);
255
256         if (err != NULL)
257                 *err = 0;
258
259         return GUINT_TO_POINTER(id);
260
261 fail:
262         if (err != NULL)
263                 *err = -1;
264
265         return NULL;
266 }
267
268 static struct obex_transport_driver driver = {
269         .name = "usb",
270         .service = OBEX_PCSUITE,
271         .start = usb_start,
272         .stop = usb_stop
273 };
274
275 static int usb_init(void)
276 {
277         struct sigaction sa;
278
279         memset(&sa, 0, sizeof(sa));
280         sa.sa_handler = sig_usb;
281         sigaction(SIGUSR1, &sa, NULL);
282         sigaction(SIGHUP, &sa, NULL);
283
284         connection = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
285         if (connection == NULL)
286                 return -EPERM;
287
288         return obex_transport_driver_register(&driver);
289 }
290
291 static void usb_exit(void)
292 {
293         if (connection)
294                 dbus_connection_unref(connection);
295
296         obex_transport_driver_unregister(&driver);
297 }
298
299 OBEX_PLUGIN_DEFINE(usb, usb_init, usb_exit)