5 * Copyright (C) 2007-2010 Nokia Corporation
6 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
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
46 #include "transport.h"
50 static GIOChannel *usb_io = NULL;
51 static guint usb_reconnecting = 0;
52 static guint usb_watch = 0;
53 static DBusConnection *connection = NULL;
55 #define USB_RX_MTU 65535
56 #define USB_TX_MTU 65535
57 #define USB_DEVNODE "/dev/ttyGS0"
59 static int usb_connect(struct obex_server *server);
61 static void usb_disconnect(struct obex_server *server)
63 if (usb_reconnecting > 0) {
64 g_source_remove(usb_reconnecting);
69 g_source_remove(usb_watch);
73 /* already disconnected */
77 g_io_channel_shutdown(usb_io, TRUE, NULL);
78 g_io_channel_unref(usb_io);
83 static gboolean usb_reconnect(void *data)
85 struct obex_server *server = data;
94 static gboolean usb_watchdog(GIOChannel *io, GIOCondition cond,
97 struct obex_server *server = user_data;
100 usb_disconnect(server);
102 if ((cond & G_IO_NVAL) == FALSE)
103 usb_reconnecting = g_idle_add(usb_reconnect, server);
108 static int usb_connect(struct obex_server *server)
110 struct termios options;
114 if (usb_reconnecting > 0) {
115 g_source_remove(usb_reconnecting);
116 usb_reconnecting = 0;
119 /* already connected */
123 fd = open(USB_DEVNODE, O_RDWR | O_NOCTTY);
127 flags = fcntl(fd, F_GETFL);
128 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
130 tcgetattr(fd, &options);
132 options.c_oflag &= ~ONLCR;
133 tcsetattr(fd, TCSANOW, &options);
135 arg = fcntl(fd, F_GETFL);
142 if (fcntl(fd, F_SETFL, arg) < 0) {
147 usb_io = g_io_channel_unix_new(fd);
148 g_io_channel_set_close_on_unref(usb_io, TRUE);
150 err = obex_server_new_connection(server, usb_io, USB_TX_MTU,
155 usb_watch = g_io_add_watch(usb_io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
156 usb_watchdog, server);
158 DBG("Successfully opened %s", USB_DEVNODE);
163 error("usb: %s (%d)", strerror(-err), -err);
167 usb_disconnect(server);
171 static void sig_usb(int sig)
175 static void usb_set_mode(struct obex_server *server, const char *mode)
179 if (g_str_equal(mode, "ovi_suite") == TRUE)
181 else if (g_str_equal(mode, "USB disconnected") == TRUE)
182 usb_disconnect(server);
185 static gboolean handle_signal(DBusConnection *connection,
186 DBusMessage *message, void *user_data)
188 struct obex_server *server = user_data;
191 dbus_message_get_args(message, NULL,
192 DBUS_TYPE_STRING, &mode,
195 usb_set_mode(server, mode);
200 static void usb_stop(void *data)
202 guint id = GPOINTER_TO_UINT(data);
203 g_dbus_remove_watch(connection, id);
206 static void mode_request_reply(DBusPendingCall *call, void *user_data)
208 struct obex_server *server = user_data;
209 DBusMessage *reply = dbus_pending_call_steal_reply(call);
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);
219 dbus_message_get_args(reply, NULL,
220 DBUS_TYPE_STRING, &mode,
223 usb_set_mode(server, mode);
226 dbus_message_unref(reply);
229 static void *usb_start(struct obex_server *server, int *err)
233 DBusPendingCall *call;
235 msg = dbus_message_new_method_call("com.meego.usb_moded",
236 "/com/meego/usb_moded",
237 "com.meego.usb_moded",
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);
247 dbus_pending_call_set_notify(call, mode_request_reply, server, NULL);
248 dbus_pending_call_unref(call);
249 dbus_message_unref(msg);
251 id = g_dbus_add_signal_watch(connection, NULL, NULL,
252 "com.meego.usb_moded",
254 handle_signal, server, NULL);
259 return GUINT_TO_POINTER(id);
268 static struct obex_transport_driver driver = {
270 .service = OBEX_PCSUITE,
275 static int usb_init(void)
279 memset(&sa, 0, sizeof(sa));
280 sa.sa_handler = sig_usb;
281 sigaction(SIGUSR1, &sa, NULL);
282 sigaction(SIGHUP, &sa, NULL);
284 connection = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
285 if (connection == NULL)
288 return obex_transport_driver_register(&driver);
291 static void usb_exit(void)
294 dbus_connection_unref(connection);
296 obex_transport_driver_unregister(&driver);
299 OBEX_PLUGIN_DEFINE(usb, usb_init, usb_exit)