1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2006-2008 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#include <glib.h>
#include <dbus/dbus.h>
#include "logging.h"
#include "process.h"
#include "session.h"
static DBusHandlerResult cancel_message(DBusConnection *conn,
DBusMessage *msg, void *data)
{
DBusMessage *reply;
struct session_data *session = data;
reply = dbus_message_new_method_return(msg);
if (!reply)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
debug("Cancel of session at %s", session->identifier);
dbus_message_append_args(reply, DBUS_TYPE_INVALID);
dbus_connection_send(conn, reply, NULL);
dbus_message_unref(reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult session_handler(DBusConnection *conn,
DBusMessage *msg, void *data)
{
if (dbus_message_is_method_call(msg, "org.bluez.transfer.Session", "Cancel"))
return cancel_message(conn, msg, data);
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static DBusObjectPathVTable session_table = {
.message_function = session_handler,
};
struct session_data *session_create(DBusConnection *conn, DBusMessage *msg)
{
struct session_data *session;
session = malloc(sizeof(*session));
if (!session)
return NULL;
memset(session, 0, sizeof(*session));
session->conn = dbus_connection_ref(conn);
session->msg = dbus_message_ref(msg);
return session;
}
void session_destroy(struct session_data *session)
{
if (!session)
return;
if (session->identifier) {
dbus_connection_unregister_object_path(session->conn,
session->identifier);
free(session->identifier);
}
dbus_message_unref(session->msg);
dbus_connection_unref(session->conn);
free(session);
}
static gboolean connect_callback(GIOChannel *chan,
GIOCondition cond, gpointer data)
{
struct session_data *session = data;
int sk;
debug("Connection for session %s established", session->identifier);
sk = g_io_channel_unix_get_fd(session->rfcomm_io);
close(sk);
return FALSE;
}
static int rfcomm_connect(struct session_data *session)
{
struct sockaddr_rc addr;
long arg;
int sk;
sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (sk < 0)
return -1;
arg = fcntl(sk, F_GETFL);
if (arg < 0) {
close(sk);
return -1;
}
arg |= O_NONBLOCK;
if (fcntl(sk, F_SETFL, arg) < 0) {
close(sk);
return -1;
}
session->rfcomm_io = g_io_channel_unix_new(sk);
if (!session->rfcomm_io) {
close(sk);
return -1;
}
g_io_channel_set_close_on_unref(session->rfcomm_io, TRUE);
memset(&addr, 0, sizeof(addr));
addr.rc_family = AF_BLUETOOTH;
bacpy(&addr.rc_bdaddr, &session->bdaddr);
addr.rc_channel = session->channel;
if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
if (errno != EAGAIN && errno != EINPROGRESS) {
close(sk);
return -1;
}
}
g_io_add_watch(session->rfcomm_io, G_IO_OUT, connect_callback, session);
return 0;
}
static gboolean data_event(GIOChannel *chan, GIOCondition cond, gpointer data)
{
unsigned char buf[64];
gsize len;
GIOError err;
int i;
debug("Data event");
if (cond & (G_IO_HUP | G_IO_ERR))
return FALSE;
memset(buf, 0, sizeof(buf));
err = g_io_channel_read(chan, (gchar *) buf, sizeof(buf) - 1, &len);
if (err == G_IO_ERROR_AGAIN)
return TRUE;
for (i = 0; i < len; i++)
if (!isprint(buf[i]))
buf[i] = '.';
debug("%s", buf);
return TRUE;
}
const char *session_connect(struct session_data *session,
const char *address, const char *pathname)
{
const char *sender;
char path[128];
sender = dbus_message_get_sender(session->msg);
session->uid = dbus_bus_get_unix_user(session->conn, sender, NULL);
debug("Request by user %d", session->uid);
create_reader(session->uid, pathname, data_event, NULL);
str2ba(address, &session->bdaddr);
session->channel = 1;
snprintf(path, sizeof(path), "/org/bluez/transfer/%d%d",
session->uid, rand());
session->identifier = strdup(path);
if (!session->identifier)
return NULL;
if (dbus_connection_register_object_path(session->conn, path,
&session_table, session) == FALSE) {
free(session->identifier);
session->identifier = NULL;
return NULL;
}
if (rfcomm_connect(session) < 0)
return NULL;
return session->identifier;
}
|