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
/**
* 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_RFCOMM.c"

#include "BlueCoveBlueZ.h"

#include <poll.h>
#include <bluetooth/rfcomm.h>

JNIEXPORT jlong JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_connectionRfOpenClientConnectionImpl
(JNIEnv* env, jobject peer, jlong localDeviceBTAddress, jlong address, jint channel, jboolean authenticate, jboolean encrypt, jint timeout) {
debug("RFCOMM connect, channel %d", channel);

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

struct sockaddr_rc localAddr;
//bind local address
memset(&localAddr, 0, sizeof(localAddr));
localAddr.rc_family = AF_BLUETOOTH;
localAddr.rc_channel = 0;
//bacpy(&localAddr.rc_bdaddr, BDADDR_ANY);
longToDeviceAddr(localDeviceBTAddress, &localAddr.rc_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;
}

// TODO verify how this works, I think device needs to paird before this can be setup.
// Set link security options
if (encrypt || authenticate) {
int socket_opt = 0;
socklen_t len = sizeof(socket_opt);
if (getsockopt(handle, SOL_RFCOMM, RFCOMM_LM, &socket_opt, &len) < 0) {
throwIOException(env, "Failed to read RFCOMM link mode. [%d] %s", errno, strerror(errno));
close(handle);
return 0;
}
//if (master) {
// socket_opt |= RFCOMM_LM_MASTER;
//}
if (authenticate) {
socket_opt |= RFCOMM_LM_AUTH;
debug("RFCOMM set authenticate");
}
if (encrypt) {
socket_opt |= RFCOMM_LM_ENCRYPT;
}
//if (socket_opt != 0) {
// socket_opt |= RFCOMM_LM_SECURE;
//}

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

struct sockaddr_rc remoteAddr;
memset(&remoteAddr, 0, sizeof(remoteAddr));
remoteAddr.rc_family = AF_BLUETOOTH;
longToDeviceAddr(address, &remoteAddr.rc_bdaddr);
remoteAddr.rc_channel = 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("RFCOMM connected, handle %li", handle);
return handle;
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_connectionRfCloseClientConnection
(JNIEnv* env, jobject peer, jlong handle) {
debug("RFCOMM 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));
}
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_rfGetSecurityOptImpl
(JNIEnv *env, jobject peer, jlong handle) {
int socket_opt = 0;
socklen_t len = sizeof(socket_opt);
if (getsockopt(handle, SOL_RFCOMM, RFCOMM_LM, &socket_opt, &len) < 0) {
throwIOException(env, "Failed to get RFCOMM link mode. [%d] %s", errno, strerror(errno));
return 0;
}
bool encrypted = socket_opt & (RFCOMM_LM_ENCRYPT | RFCOMM_LM_SECURE);
bool authenticated = socket_opt & RFCOMM_LM_AUTH;
if (authenticated) {
return NOAUTHENTICATE_NOENCRYPT;
}
if (encrypted) {
return AUTHENTICATE_ENCRYPT;
} else {
return AUTHENTICATE_NOENCRYPT;
}
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_connectionRfRead
(JNIEnv* env, jobject peer, jlong handle, jbyteArray b, jint off, jint len ) {
if (b == NULL) {
throwRuntimeException(env, "Invalid argument");
return 0;
}
jbyte *bytes = (*env)->GetByteArrayElements(env, b, 0);
if (bytes == NULL) {
throwRuntimeException(env, "Invalid argument");
return 0;
}
int done = 0;
while (done == 0) {
int flags = MSG_DONTWAIT;
int count = recv(handle, (char *)(bytes + off + done), len - done, flags);
if (count < 0) {
if (errno == EAGAIN) { // Try again for non-blocking operation
count = 0;
Edebug("no data available for read");
} else if (errno == ECONNRESET) { //104 Connection reset by peer
debug("Connection closed, Connection reset by peer");
// See InputStream.read();
done = -1;
goto rfReadEnd;
} else {
throwIOException(env, "Failed to read. [%d] %s", errno, strerror(errno));
done = 0;
goto rfReadEnd;
}
} else if (count == 0) {
debug("Connection closed");
if (done == 0) {
// See InputStream.read();
done = -1;
}
goto rfReadEnd;
}
done += count;
if (isCurrentThreadInterrupted(env, peer)) {
done = 0;
goto rfReadEnd;
}
if (done == 0) {
// Sleep while not avalable
bool available = false;
do {
struct pollfd fds;
int timeout = 500; // milliseconds
memset(&fds, 0, sizeof(fds));
fds.fd = handle;
fds.events = POLLIN | POLLHUP | POLLERR;// | POLLRDHUP;
fds.revents = 0;
//Edebug("poll: wait");
int poll_rc = poll(&fds, 1, timeout);
if (poll_rc > 0) {
if (fds.revents & (POLLHUP | POLLERR /* | POLLRDHUP */)) {
debug("Stream socket peer closed connection");
done = -1;
goto rfReadEnd;
} else if (fds.revents & POLLNVAL) {
// socket closed...
done = -1;
goto rfReadEnd;
} else if (fds.revents & POLLIN) {
//Edebug("poll: data to read available");
available = true;
} else {
Edebug("poll: revents %i", fds.revents);
}
} else if (poll_rc == -1) {
//Edebug("poll: call error %i", errno);
throwIOException(env, "Failed to poll. [%d] %s", errno, strerror(errno));
done = 0;
goto rfReadEnd;
} else {
//Edebug("poll: call timed out");
}
if (isCurrentThreadInterrupted(env, peer)) {
done = -1;
goto rfReadEnd;
}
} while (!available);
}
}
rfReadEnd:
(*env)->ReleaseByteArrayElements(env, b, bytes, 0);
return done;
}

JNIEXPORT jint JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_connectionRfReadAvailable
(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, "Stream socket peer closed connection");
} else if (fds.revents & POLLIN) {
return 1;
}
// POLLNVAL - this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.
// We do not
} else if (poll_rc == -1) {
throwIOException(env, "Failed to read available. [%d] %s", errno, strerror(errno));
}
return 0;
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_connectionRfWrite__JI
(JNIEnv* env, jobject peer, jlong handle, jint b) {
char c = (char)b;
if (send(handle, &c, 1, 0) != 1) {
throwIOException(env, "Failed to write. [%d] %s", errno, strerror(errno));
}
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_connectionRfWrite__J_3BII
(JNIEnv* env, jobject peer, jlong handle, jbyteArray b, jint off, jint len) {
if (b == NULL) {
throwRuntimeException(env, "Invalid argument");
return;
}
jbyte *bytes = (*env)->GetByteArrayElements(env, b, 0);
if (bytes == NULL) {
throwRuntimeException(env, "Invalid argument");
return;
}
int done = 0;
while(done < len) {
int count = send(handle, (char *)(bytes + off + done), len - done, 0);
if (count < 0) {
throwIOException(env, "Failed to write. [%d] %s", errno, strerror(errno));
break;
}
if (isCurrentThreadInterrupted(env, peer)) {
break;
}
done += count;
}
(*env)->ReleaseByteArrayElements(env, b, bytes, 0);
}

JNIEXPORT void JNICALL Java_com_intel_bluetooth_BluetoothStackBlueZ_connectionRfFlush
(JNIEnv* env, jobject peer, jlong handle) {
}

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

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
r2842 by skarzhevskyy on Mar 5, 2009   Diff
Work on bluecove-bluez
r2511 by minashokry on Dec 6, 2008   Diff
fix:
checking for closed connections before
checking for available data
All revisions of this file

File info

Size: 11092 bytes, 294 lines

File properties

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