My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2008 Vlad Skarzhevskyy
* Copyright (C) 2008-2010 Mina Shokry
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @version $Id$
*/
#define CPP__FILE "BlueCoveBlueZ_L2CAP.c"

#include "BlueCoveBlueZ.h"

#include <sys/poll.h>
#include <bluetooth/l2cap.h>

//#define BLUECOVE_L2CAP_USE_MSG
// TODO Is this necessary to truncate data before calling socket functions? sockets preserve message boundaries.
//#define BLUECOVE_L2CAP_MTU_TRUNCATE

bool l2Get_options(JNIEnv* env, jlong handle, struct l2cap_options* opt);

JNIEXPORT jlong JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2OpenClientConnectionImpl
(JNIEnv* env, jobject peer, jlong localDeviceBTAddress, jlong address, jint channel, jboolean authenticate, jboolean encrypt, jint receiveMTU, jint transmitMTU, jint timeout) {
debug("CONNECT connect, psm %d", channel);

// allocate socket
int handle = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
if (handle < 0) {
throwIOException(env, "Failed to create socket. [%d] %s", errno, strerror(errno));
return 0;
}

struct sockaddr_l2 localAddr;
//bind local address
memset(&localAddr, 0, sizeof(localAddr));
localAddr.l2_family = AF_BLUETOOTH;
localAddr.l2_psm = 0;
//bacpy(&localAddr.l2_bdaddr, BDADDR_ANY);
longToDeviceAddr(localDeviceBTAddress, &localAddr.l2_bdaddr);

if (bind(handle, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0) {
throwIOException(env, "Failed to bind socket. [%d] %s", errno, strerror(errno));
close(handle);
return 0;
}

// Set link mtu and security options
struct l2cap_options opt;
socklen_t opt_len = sizeof(opt);
memset(&opt, 0, opt_len);
opt.imtu = receiveMTU;
opt.omtu = (transmitMTU > 0)?transmitMTU:L2CAP_DEFAULT_MTU;
opt.flush_to = L2CAP_DEFAULT_FLUSH_TO;
Edebug("L2CAP set imtu %i, omtu %i", opt.imtu, opt.omtu);

if (setsockopt(handle, SOL_L2CAP, L2CAP_OPTIONS, &opt, opt_len) < 0) {
throwIOException(env, "Failed to set L2CAP mtu options. [%d] %s", errno, strerror(errno));
close(handle);
return 0;
}

if (encrypt || authenticate) {
int socket_opt = 0;
socklen_t len = sizeof(socket_opt);
if (getsockopt(handle, SOL_L2CAP, L2CAP_LM, &socket_opt, &len) < 0) {
throwIOException(env, "Failed to read L2CAP link mode. [%d] %s", errno, strerror(errno));
close(handle);
return 0;
}
//if (master) {
// socket_opt |= L2CAP_LM_MASTER;
//}
if (authenticate) {
socket_opt |= L2CAP_LM_AUTH;
Edebug("L2CAP set authenticate");
}
if (encrypt) {
socket_opt |= L2CAP_LM_ENCRYPT;
}

if ((socket_opt != 0) && setsockopt(handle, SOL_L2CAP, L2CAP_LM, &socket_opt, sizeof(socket_opt)) < 0) {
throwIOException(env, "Failed to set L2CAP link mode. [%d] %s", errno, strerror(errno));
close(handle);
return 0;
}
}


struct sockaddr_l2 remoteAddr;
memset(&remoteAddr, 0, sizeof(remoteAddr));
remoteAddr.l2_family = AF_BLUETOOTH;
longToDeviceAddr(address, &remoteAddr.l2_bdaddr);
remoteAddr.l2_psm = channel;

// connect to server
if (connect(handle, (struct sockaddr*)&remoteAddr, sizeof(remoteAddr)) != 0) {
throwIOException(env, "Failed to connect. [%d] %s", errno, strerror(errno));
close(handle);
return 0;
}
debug("L2CAP connected, handle %li", handle);

struct l2cap_options copt;
if (!l2Get_options(env, handle, &copt)) {
close(handle);
return 0;
}
debug("L2CAP imtu %i, omtu %i", copt.imtu, copt.omtu);
return handle;
}


JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2CloseClientConnection
(JNIEnv* env, jobject peer, jlong handle) {
debug("L2CAP disconnect, handle %li", handle);
// Closing channel, further sends and receives will be disallowed.
if (shutdown(handle, SHUT_RDWR) < 0) {
debug("shutdown failed. [%d] %s", errno, strerror(errno));
}
if (close(handle) < 0) {
throwIOException(env, "Failed to close socket. [%d] %s", errno, strerror(errno));
}
}

bool l2Get_options(JNIEnv* env, jlong handle, struct l2cap_options* opt) {
socklen_t opt_len = sizeof(*opt);
if (getsockopt(handle, SOL_L2CAP, L2CAP_OPTIONS, opt, &opt_len) < 0) {
throwIOException(env, "Failed to get L2CAP link mtu. [%d] %s", errno, strerror(errno));
return false;
}
return true;
}


JNIEXPORT jboolean JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2Ready
(JNIEnv* env, jobject peer, jlong handle) {
struct pollfd fds;
int timeout = 10; // milliseconds
memset(&fds, 0, sizeof(fds));
fds.fd = handle;
fds.events = POLLIN | POLLHUP | POLLERR;// | POLLRDHUP;
fds.revents = 0;
int poll_rc = poll(&fds, 1, timeout);
if (poll_rc > 0) {
if (fds.revents & (POLLHUP | POLLERR /*| POLLRDHUP*/)) {
throwIOException(env, "Peer closed connection");
return JNI_FALSE;
} else if (fds.revents & POLLNVAL) {
// this connection has been closed by invoking the close() method.
throwIOException(env, "Connection closed");
return JNI_FALSE;
} else if (fds.revents & POLLIN) {
return JNI_TRUE;
}
} else if (poll_rc == -1) {
throwIOException(env, "Failed to read. [%d] %s", errno, strerror(errno));
return JNI_FALSE;
} else {
//Edebug("poll: call timed out");
}
return JNI_FALSE;
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2Receive
(JNIEnv* env, jobject peer, jlong handle, jbyteArray inBuf) {
if (inBuf == NULL) {
throwRuntimeException(env, "Invalid argument");
return 0;
}
#ifdef BLUECOVE_L2CAP_MTU_TRUNCATE
struct l2cap_options opt;
if (!l2Get_options(env, handle, &opt)) {
return 0;
}
#endif //BLUECOVE_L2CAP_MTU_TRUNCATE

bool dataReady = false;
while(!dataReady) {
struct pollfd fds;
int timeout = 10; // milliseconds
memset(&fds, 0, sizeof(fds));
fds.fd = handle;
fds.events = POLLIN | POLLHUP | POLLERR;// | POLLRDHUP;
fds.revents = 0;
int poll_rc = poll(&fds, 1, timeout);
if (poll_rc > 0) {
if (fds.revents & (POLLHUP | POLLERR /*| POLLRDHUP*/)) {
throwIOException(env, "Peer closed connection");
return 0;
} else if (fds.revents & POLLNVAL) {
// this connection has been closed by invoking the close() method.
throwIOException(env, "Connection closed");
return 0;
} else if (fds.revents & POLLIN) {
dataReady = true;
}
} else if (poll_rc == -1) {
throwIOException(env, "Failed to read. [%d] %s", errno, strerror(errno));
return 0;
} else {
//Edebug("poll: call timed out");
}
if(isCurrentThreadInterrupted(env, peer)) {
return 0;
}
}

jbyte *bytes = (*env)->GetByteArrayElements(env, inBuf, 0);
if (bytes == NULL) {
throwRuntimeException(env, "Invalid argument");
return 0;
}
size_t inBufLen = (size_t)(*env)->GetArrayLength(env, inBuf);
int readLen = inBufLen;

#ifdef BLUECOVE_L2CAP_MTU_TRUNCATE
if (readLen > opt.imtu) {
readLen = opt.imtu;
}
#endif //BLUECOVE_L2CAP_MTU_TRUNCATE

#ifdef BLUECOVE_L2CAP_USE_MSG
int flags = 0;
iovec iov;
msghdr msg;
memset((void*)&iov, 0, sizeof(iov));
memset((void*)&msg, 0, sizeof(msg));
iov.iov_base = bytes;
iov.iov_len = readLen;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;

int count = recvmsg(handle, &msg, flags);
#else
int count = recv(handle, (char *)bytes, readLen, 0);
#endif //BLUECOVE_L2CAP_USE_MSG
if (count < 0) {
throwIOException(env, "Failed to read. [%d] %s", errno, strerror(errno));
count = 0;
}

(*env)->ReleaseByteArrayElements(env, inBuf, bytes, 0);
debug("receive[] returns %i", count);
return count;
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2Send
(JNIEnv* env, jobject peer, jlong handle, jbyteArray data, jint transmitMTU) {
#ifdef BLUECOVE_L2CAP_MTU_TRUNCATE
struct l2cap_options opt;
if (!l2Get_options(env, handle, &opt)) {
return;
}
#endif //BLUECOVE_L2CAP_MTU_TRUNCATE

if (data == NULL) {
throwRuntimeException(env, "Invalid argument");
return;
}
jbyte *bytes = (*env)->GetByteArrayElements(env, data, 0);
if (bytes == NULL) {
throwRuntimeException(env, "Invalid argument");
return;
}
int len = (int)(*env)->GetArrayLength(env, data);
if (len > transmitMTU) {
len = transmitMTU;
}

#ifdef BLUECOVE_L2CAP_MTU_TRUNCATE
if (len > opt.omtu) {
len = opt.omtu;
}
#endif //BLUECOVE_L2CAP_MTU_TRUNCATE

int count = send(handle, (char *)bytes, len, 0);
if (count < 0) {
throwIOException(env, "Failed to write. [%d] %s", errno, strerror(errno));
}
(*env)->ReleaseByteArrayElements(env, data, bytes, 0);
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2GetReceiveMTU
(JNIEnv* env, jobject peer, jlong handle) {
struct l2cap_options opt;
if (l2Get_options(env, handle, &opt)) {
return opt.imtu;
} else {
return 0;
}
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2GetTransmitMTU
(JNIEnv* env, jobject peer, jlong handle) {
struct l2cap_options opt;
if (l2Get_options(env, handle, &opt)) {
return opt.omtu;
} else {
return 0;
}
}

JNIEXPORT jlong JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2RemoteAddress
(JNIEnv* env, jobject peer, jlong handle) {
struct sockaddr_l2 remoteAddr;
memset(&remoteAddr, 0, sizeof(remoteAddr));
socklen_t len = sizeof(remoteAddr);
if (getpeername(handle, (struct sockaddr*)&remoteAddr, &len) < 0) {
throwIOException(env, "Failed to get L2CAP (%i) peer name. [%d] %s", (int)handle, errno, strerror(errno));
return -1;
}
return deviceAddrToLong(&remoteAddr.l2_bdaddr);
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_l2GetSecurityOpt
(JNIEnv* env, jobject peer, jlong handle, jint expected) {
int socket_opt = 0;
socklen_t len = sizeof(socket_opt);
if (getsockopt(handle, SOL_L2CAP, L2CAP_LM, &socket_opt, &len) < 0) {
throwIOException(env, "Failed to get L2CAP (%i) link mode. [%d] %s", (int)handle, errno, strerror(errno));
return 0;
}
bool encrypted = socket_opt & (L2CAP_LM_ENCRYPT | L2CAP_LM_SECURE);
bool authenticated = socket_opt & L2CAP_LM_AUTH;
if (authenticated) {
return NOAUTHENTICATE_NOENCRYPT;
}
if (encrypted) {
return AUTHENTICATE_ENCRYPT;
} else {
return AUTHENTICATE_NOENCRYPT;
}
}

Change log

r3044 by skarzhevskyy on May 24, 2010   Diff
Initialize struct instances (Johannes
Schindelin)
Go to: 
Project members, sign in to write a code review

Older revisions

r3041 by minashokry on Apr 18, 2010   Diff
eliminate warnings even when using
strict compiler flags
r2945 by skarzhevskyy on Mar 21, 2009   Diff
honor requested TransmitMTU
r2842 by skarzhevskyy on Mar 5, 2009   Diff
Work on bluecove-bluez
All revisions of this file

File info

Size: 11871 bytes, 346 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting