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
|
/*
* Copyright 2009, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "BluetoothSocket.cpp"
#include "android_bluetooth_common.h"
#include "android_runtime/AndroidRuntime.h"
#include "JNIHelp.h"
#include "utils/Log.h"
#include "cutils/abort_socket.h"
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#ifdef HAVE_BLUETOOTH
#include <bluetooth/bluetooth.h>
#include <bluetooth/rfcomm.h>
#endif
namespace android {
static jfieldID field_mAuth; /* read-only */
static jfieldID field_mEncrypt; /* read-only */
static jfieldID field_mSocketData;
static jmethodID method_BluetoothSocket_ctor;
static jclass class_BluetoothSocket;
static struct asocket *get_socketData(JNIEnv *env, jobject obj) {
struct asocket *s =
(struct asocket *) env->GetIntField(obj, field_mSocketData);
if (!s)
jniThrowException(env, "java/io/IOException", "null socketData");
return s;
}
static void initSocketFromFdNative(JNIEnv *env, jobject obj, jint fd) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
struct asocket *s = asocket_init(fd);
if (!s) {
LOGV("asocket_init() failed, throwing");
jniThrowIOException(env, errno);
return;
}
env->SetIntField(obj, field_mSocketData, (jint)s);
return;
#endif
jniThrowIOException(env, ENOSYS);
}
static void initSocketNative(JNIEnv *env, jobject obj) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
int fd;
int lm = 0;
jboolean auth;
jboolean encrypt;
/*TODO: do not hardcode to rfcomm */
fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (fd < 0) {
LOGV("socket() failed, throwing");
jniThrowIOException(env, errno);
return;
}
auth = env->GetBooleanField(obj, field_mAuth);
encrypt = env->GetBooleanField(obj, field_mEncrypt);
lm |= auth ? RFCOMM_LM_AUTH : 0;
lm |= encrypt? RFCOMM_LM_ENCRYPT : 0;
if (lm) {
if (setsockopt(fd, SOL_RFCOMM, RFCOMM_LM, &lm, sizeof(lm))) {
LOGV("setsockopt() failed, throwing");
jniThrowIOException(env, errno);
return;
}
}
initSocketFromFdNative(env, obj, fd);
return;
#endif
jniThrowIOException(env, ENOSYS);
}
static void connectNative(JNIEnv *env, jobject obj, jstring address,
jint port, jint timeout) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
int ret;
struct sockaddr_rc addr;
const char *c_address;
struct asocket *s = get_socketData(env, obj);
if (!s)
return;
addr.rc_family = AF_BLUETOOTH;
addr.rc_channel = port;
c_address = env->GetStringUTFChars(address, NULL);
if (get_bdaddr((const char *)c_address, &addr.rc_bdaddr)) {
env->ReleaseStringUTFChars(address, c_address);
jniThrowIOException(env, EINVAL);
return;
}
env->ReleaseStringUTFChars(address, c_address);
ret = asocket_connect(s, (struct sockaddr *)&addr, sizeof(addr), timeout);
if (ret)
jniThrowIOException(env, errno);
return;
#endif
jniThrowIOException(env, ENOSYS);
}
static void bindListenNative(JNIEnv *env, jobject obj, jint port) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
struct sockaddr_rc addr;
struct asocket *s = get_socketData(env, obj);
if (!s)
return;
memset(&addr, 0, sizeof(struct sockaddr_rc));
addr.rc_family = AF_BLUETOOTH;
addr.rc_bdaddr = *BDADDR_ANY;
addr.rc_channel = port;
if (bind(s->fd, (struct sockaddr *)&addr, sizeof(addr))) {
jniThrowIOException(env, errno);
return;
}
if (listen(s->fd, 1)) {
jniThrowIOException(env, errno);
return;
}
return;
#endif
jniThrowIOException(env, ENOSYS);
}
static jobject acceptNative(JNIEnv *env, jobject obj, int timeout) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
int fd;
struct sockaddr_rc addr;
int addrlen = sizeof(addr);
jstring addr_jstr;
char addr_cstr[BTADDR_SIZE];
jboolean auth;
jboolean encrypt;
struct asocket *s = get_socketData(env, obj);
if (!s)
return NULL;
fd = asocket_accept(s, (struct sockaddr *)&addr, &addrlen, timeout);
if (fd < 0) {
jniThrowIOException(env, errno);
return NULL;
}
/* Connected - return new BluetoothSocket */
auth = env->GetBooleanField(obj, field_mAuth);
encrypt = env->GetBooleanField(obj, field_mEncrypt);
get_bdaddr_as_string(&addr.rc_bdaddr, addr_cstr);
addr_jstr = env->NewStringUTF(addr_cstr);
return env->NewObject(class_BluetoothSocket, method_BluetoothSocket_ctor, fd,
auth, encrypt, addr_jstr, -1);
#endif
jniThrowIOException(env, ENOSYS);
return NULL;
}
static jint availableNative(JNIEnv *env, jobject obj) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
int available;
struct asocket *s = get_socketData(env, obj);
if (!s)
return -1;
if (ioctl(s->fd, FIONREAD, &available) < 0) {
jniThrowIOException(env, errno);
return -1;
}
return available;
#endif
jniThrowIOException(env, ENOSYS);
return -1;
}
static jint readNative(JNIEnv *env, jobject obj) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
char buf;
struct asocket *s = get_socketData(env, obj);
if (!s)
return -1;
if (asocket_read(s, &buf, 1, -1) < 0) {
jniThrowIOException(env, errno);
return -1;
}
return (jint)buf;
#endif
jniThrowIOException(env, ENOSYS);
return -1;
}
static void writeNative(JNIEnv *env, jobject obj, jint data) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
const char buf = (char)data;
struct asocket *s = get_socketData(env, obj);
if (!s)
return;
if (asocket_write(s, &buf, 1, -1) < 0)
jniThrowIOException(env, errno);
return;
#endif
jniThrowIOException(env, ENOSYS);
}
static void closeNative(JNIEnv *env, jobject obj) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
struct asocket *s = get_socketData(env, obj);
if (!s)
return;
asocket_abort(s);
return;
#endif
jniThrowIOException(env, ENOSYS);
}
static void destroyNative(JNIEnv *env, jobject obj) {
#ifdef HAVE_BLUETOOTH
LOGV(__FUNCTION__);
struct asocket *s = get_socketData(env, obj);
if (!s)
return;
asocket_destroy(s);
return;
#endif
jniThrowIOException(env, ENOSYS);
}
static JNINativeMethod sMethods[] = {
{"initSocketNative", "()V", (void*) initSocketNative},
{"initSocketFromFdNative", "(I)V", (void*) initSocketFromFdNative},
{"connectNative", "(Ljava/lang/String;II)V", (void *) connectNative},
{"bindListenNative", "(I)V", (void *) bindListenNative},
{"acceptNative", "(I)Landroid/bluetooth/BluetoothSocket;", (void *) acceptNative},
{"availableNative", "()I", (void *) availableNative},
{"readNative", "()I", (void *) readNative},
{"writeNative", "(I)V", (void *) writeNative},
{"closeNative", "()V", (void *) closeNative},
{"destroyNative", "()V", (void *) destroyNative},
};
int register_android_bluetooth_BluetoothSocket(JNIEnv *env) {
jclass clazz = env->FindClass("android/bluetooth/BluetoothSocket");
if (clazz == NULL)
return -1;
class_BluetoothSocket = (jclass) env->NewGlobalRef(clazz);
field_mAuth = env->GetFieldID(clazz, "mAuth", "Z");
field_mEncrypt = env->GetFieldID(clazz, "mEncrypt", "Z");
field_mSocketData = env->GetFieldID(clazz, "mSocketData", "I");
method_BluetoothSocket_ctor = env->GetMethodID(clazz, "<init>", "(IZZLjava/lang/String;I)V");
return AndroidRuntime::registerNativeMethods(env,
"android/bluetooth/BluetoothSocket", sMethods, NELEM(sMethods));
}
} /* namespace android */
|