3
* oFono - Open Source Telephony
5
* Copyright (C) 2008-2009 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
31
#include <sys/socket.h>
33
#include <netinet/in.h>
34
#include <netinet/tcp.h>
35
#include <arpa/inet.h>
41
#include <ofono/log.h>
45
struct modem_session_callback {
46
modem_session_callback_t callback;
48
GDestroyNotify notify;
49
guint timeout_watcher;
53
static void connect_destroy(gpointer user)
55
struct modem_session_callback *callback = user;
58
callback->notify(callback->user_data);
60
if (callback->timeout_watcher != 0)
61
g_source_remove(callback->timeout_watcher);
66
static gboolean connect_cb(GIOChannel *io, GIOCondition cond, gpointer user)
68
struct modem_session_callback *callback = user;
75
if (cond & G_IO_OUT) {
76
int sock = g_io_channel_unix_get_fd(io);
77
socklen_t len = sizeof(err);
79
if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
80
err = errno == ENOTSOCK ? 0 : errno;
81
} else if (cond & (G_IO_HUP | G_IO_ERR))
86
callback->callback(io, success, callback->user_data);
91
static gboolean connect_timeout(gpointer user)
93
struct modem_session_callback *callback = user;
95
callback->callback(callback->io, FALSE, callback->user_data);
97
callback->timeout_watcher = 0;
99
g_io_channel_unref(callback->io);
104
static GIOChannel *tty_connect(const char *tty)
108
struct termios newtio;
110
sk = open(tty, O_RDWR | O_NOCTTY);
113
ofono_error("Can't open TTY %s: %s(%d)",
114
tty, strerror(errno), errno);
118
newtio.c_cflag = B115200 | CRTSCTS | CLOCAL | CREAD;
119
newtio.c_iflag = IGNPAR;
123
newtio.c_cc[VTIME] = 1;
124
newtio.c_cc[VMIN] = 5;
126
tcflush(sk, TCIFLUSH);
127
if (tcsetattr(sk, TCSANOW, &newtio) < 0) {
128
ofono_error("Can't change serial settings: %s(%d)",
129
strerror(errno), errno);
134
io = g_io_channel_unix_new(sk);
135
g_io_channel_set_close_on_unref(io, TRUE);
137
if (g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK,
138
NULL) != G_IO_STATUS_NORMAL) {
139
g_io_channel_unref(io);
146
static GIOChannel *socket_common(int sk, struct sockaddr *addr,
149
GIOChannel *io = g_io_channel_unix_new(sk);
156
g_io_channel_set_close_on_unref(io, TRUE);
158
if (g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK,
159
NULL) != G_IO_STATUS_NORMAL) {
160
g_io_channel_unref(io);
164
if (connect(sk, addr, addrlen) < 0) {
165
if (errno != EAGAIN && errno != EINPROGRESS) {
166
g_io_channel_unref(io);
174
static GIOChannel *unix_connect(const char *address)
176
struct sockaddr_un addr;
179
memset(&addr, 0, sizeof(addr));
180
addr.sun_family = PF_UNIX;
182
if (strncmp("x00", address, 3) == 0)
183
strcpy(addr.sun_path + 1, address + 3);
185
strcpy(addr.sun_path, address);
187
sk = socket(AF_UNIX, SOCK_STREAM, 0);
192
return socket_common(sk, (struct sockaddr *)&addr, sizeof(addr));
195
static GIOChannel *tcp_connect(const char *address)
197
struct sockaddr_in addr;
204
memset(&addr, 0, sizeof(addr));
206
portstr = strchr(address, ':');
208
if (!portstr || (unsigned int)(portstr-address) > (sizeof(addrstr) - 1))
211
strncpy(addrstr, address, portstr-address);
212
addrstr[portstr-address] = '\0';
216
port = atoi(portstr);
221
inetaddr = inet_addr(addrstr);
223
if (inetaddr == INADDR_NONE)
226
sk = socket(PF_INET, SOCK_STREAM, 0);
231
addr.sin_family = AF_INET;
232
addr.sin_addr.s_addr = inetaddr;
233
addr.sin_port = htons(port);
235
return socket_common(sk, (struct sockaddr *) &addr, sizeof(addr));
238
GIOChannel *modem_session_create(const char *target,
239
modem_session_callback_t func,
241
GDestroyNotify notify)
243
struct modem_session_callback *callback;
244
GIOChannel *io = NULL;
247
if (target == NULL || func == NULL)
250
if (!strncasecmp(target, "tcp:", 4))
251
io = tcp_connect(target+4);
252
else if (!strncasecmp(target, "unix:", 5))
253
io = unix_connect(target+5);
254
else if (!strncasecmp(target, "dev:", 4))
255
io = tty_connect(target+4);
260
callback = g_new0(struct modem_session_callback, 1);
262
callback->callback = func;
263
callback->user_data = user_data;
264
callback->notify = notify;
266
callback->timeout_watcher = g_timeout_add_seconds(20, connect_timeout,
269
cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
270
g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, connect_cb,
271
callback, connect_destroy);
273
g_io_channel_unref(io);