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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2008-2009 Michael Lifshits
* Copyright (C) 2008-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth;

import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRegistrationException;
import javax.bluetooth.UUID;

class BluetoothEmulator implements BluetoothStack {

static final int NATIVE_LIBRARY_VERSION = BlueCoveImpl.nativeLibraryVersionExpected;

private EmulatorLocalDevice localDevice;

private EmulatorDeviceInquiry deviceInquiry;

BluetoothEmulator() {
}

// --- Library initialization

public String getStackID() {
return BlueCoveImpl.STACK_EMULATOR;
}

public String toString() {
if (localDevice != null) {
return getStackID() + ":" + RemoteDeviceHelper.getBluetoothAddress(localDevice.getAddress());
} else {
return getStackID();
}
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#isNativeCodeLoaded()
*/
public boolean isNativeCodeLoaded() {
return true;
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#requireNativeLibraries()
*/
public LibraryInformation[] requireNativeLibraries() {
return null;
}

public int getLibraryVersion() throws BluetoothStateException {
return NATIVE_LIBRARY_VERSION;
}

public int detectBluetoothStack() {
return BlueCoveImpl.BLUECOVE_STACK_DETECT_EMULATOR;
}

public void initialize() throws BluetoothStateException {
localDevice = EmulatorHelper.createNewLocalDevice();
}

public void destroy() {
EmulatorHelper.releaseDevice(localDevice);
localDevice = null;
}

public void enableNativeDebug(Class nativeDebugCallback, boolean on) {
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#isCurrentThreadInterruptedCallback()
*/
public boolean isCurrentThreadInterruptedCallback() {
return Thread.interrupted();
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#getFeatureSet()
*/
public int getFeatureSet() {
return FEATURE_SET_DEVICE_SERVICE_CLASSES | FEATURE_SERVICE_ATTRIBUTES | FEATURE_L2CAP | FEATURE_ASSIGN_SERVER_PSM;
}

// --- LocalDevice

private void assertClosed() {
if (localDevice == null) {
throw new RuntimeException("Device Closed");
}
}

public String getLocalDeviceBluetoothAddress() throws BluetoothStateException {
assertClosed();
return RemoteDeviceHelper.getBluetoothAddress(localDevice.getAddress());
}

public DeviceClass getLocalDeviceClass() {
assertClosed();
return new DeviceClass(localDevice.getDeviceClass());
}

public String getLocalDeviceName() {
assertClosed();
return localDevice.getName();
}

public boolean isLocalDevicePowerOn() {
assertClosed();
return localDevice.isLocalDevicePowerOn();
}

public String getLocalDeviceProperty(String property) {
assertClosed();
return localDevice.getLocalDeviceProperty(property);
}

public int getLocalDeviceDiscoverable() {
assertClosed();
return localDevice.getLocalDeviceDiscoverable();
}

public boolean setLocalDeviceDiscoverable(int mode) throws BluetoothStateException {
assertClosed();
return localDevice.setLocalDeviceDiscoverable(mode);
}

private EmulatorLocalDevice activeLocalDevice() throws BluetoothStateException {
assertClosed();
if (!localDevice.isActive()) {
throw new BluetoothStateException("Bluetooth system is off");
}
return localDevice;
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#setLocalDeviceServiceClasses(int)
*/
public void setLocalDeviceServiceClasses(int classOfDevice) {
assertClosed();
localDevice.setLocalDeviceServiceClasses(classOfDevice);
}

// --- Remote Device authentication

public boolean authenticateRemoteDevice(long address) throws IOException {
return false;
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#authenticateRemoteDevice(long, java.lang.String)
*/
public boolean authenticateRemoteDevice(long address, String passkey) throws IOException {
return false;
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#removeAuthenticationWithRemoteDevice (long)
*/
public void removeAuthenticationWithRemoteDevice(long address) throws IOException {
throw new NotSupportedIOException(getStackID());
}

// --- Device Inquiry

public boolean startInquiry(int accessCode, DiscoveryListener listener) throws BluetoothStateException {
if (deviceInquiry != null) {
throw new BluetoothStateException("Another inquiry already running");
}
deviceInquiry = new EmulatorDeviceInquiry(activeLocalDevice(), this, listener);
return DeviceInquiryThread.startInquiry(this, deviceInquiry, accessCode, listener);
}

public boolean cancelInquiry(DiscoveryListener listener) {
assertClosed();
if (deviceInquiry == null) {
return false;
}
if (deviceInquiry.cancelInquiry(listener)) {
deviceInquiry = null;
return true;
} else {
return false;
}
}

public String getRemoteDeviceFriendlyName(long address) throws IOException {
return activeLocalDevice().getDeviceManagerService().getRemoteDeviceFriendlyName(address);
}

public RemoteDevice[] retrieveDevices(int option) {
return null;
}

public Boolean isRemoteDeviceTrusted(long address) {
return null;
}

public Boolean isRemoteDeviceAuthenticated(long address) {
return null;
}

// --- Service search

public int searchServices(int[] attrSet, UUID[] uuidSet, RemoteDevice device, DiscoveryListener listener)
throws BluetoothStateException {
return SearchServicesThread.startSearchServices(this, new EmulatorSearchServices(activeLocalDevice(), this),
attrSet, uuidSet, device, listener);
}

public boolean cancelServiceSearch(int transID) {
assertClosed();
SearchServicesThread sst = SearchServicesThread.getServiceSearchThread(transID);
if (sst != null) {
synchronized (sst) {
if (!sst.isTerminated()) {
sst.setTerminated();
return true;
}
}
}
return false;
}

public boolean populateServicesRecordAttributeValues(ServiceRecordImpl serviceRecord, int[] attrIDs)
throws IOException {
if (attrIDs.length > localDevice.getBluetooth_sd_attr_retrievable_max()) {
throw new IllegalArgumentException();
}
return EmulatorSearchServices.populateServicesRecordAttributeValues(activeLocalDevice(), serviceRecord,
attrIDs, RemoteDeviceHelper.getAddress(serviceRecord.getHostDevice()), serviceRecord.getHandle());
}

// --- Client RFCOMM connections

public long connectionRfOpenClientConnection(BluetoothConnectionParams params) throws IOException {
EmulatorRFCOMMClient c = activeLocalDevice().createRFCOMMClient(params.address);
boolean success = false;
try {
c.connect(params);
success = true;
} finally {
if (!success) {
localDevice.removeConnection(c);
}
}
return c.getHandle();
}

public void connectionRfCloseClientConnection(long handle) throws IOException {
assertClosed();
if (localDevice == null) {
return;
}
EmulatorRFCOMMClient c = ((EmulatorRFCOMMClient) localDevice.getConnection(handle));
try {
c.close();
} finally {
if (localDevice != null) {
localDevice.removeConnection(c);
}
}
}

public int rfGetSecurityOpt(long handle, int expected) throws IOException {
assertClosed();
return ((EmulatorLinkedConnection) localDevice.getConnection(handle)).getSecurityOpt(expected);
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2Encrypt(long,long,boolean)
*/
public boolean rfEncrypt(long address, long handle, boolean on) throws IOException {
assertClosed();
return ((EmulatorLinkedConnection) localDevice.getConnection(handle)).encrypt(address, on);
}

// --- Server RFCOMM connections

public long rfServerOpen(BluetoothConnectionNotifierParams params, ServiceRecordImpl serviceRecord)
throws IOException {
EmulatorRFCOMMService s = activeLocalDevice().createRFCOMMService();
boolean success = false;
try {
s.open(params);
serviceRecord.setHandle(s.getHandle());
serviceRecord
.populateRFCOMMAttributes(s.getHandle(), s.getChannel(), params.uuid, params.name, params.obex);
s.updateServiceRecord(serviceRecord);
success = true;
} finally {
if (!success) {
localDevice.removeConnection(s);
}
}
return s.getHandle();
}

public void rfServerClose(long handle, ServiceRecordImpl serviceRecord) throws IOException {
assertClosed();
if (localDevice == null) {
return;
}
EmulatorRFCOMMService s = ((EmulatorRFCOMMService) localDevice.getConnection(handle));
try {
s.close(serviceRecord);
} finally {
if (localDevice != null) {
localDevice.removeConnection(s);
}
}
}

private void serverUpdateServiceRecord(long handle, ServiceRecordImpl serviceRecord, boolean acceptAndOpen)
throws ServiceRegistrationException {
EmulatorServiceConnection s;
try {
s = ((EmulatorServiceConnection) activeLocalDevice().getConnection(handle));
} catch (IOException e) {
throw new ServiceRegistrationException(e.getMessage());
}
s.updateServiceRecord(serviceRecord);
}

public void rfServerUpdateServiceRecord(long handle, ServiceRecordImpl serviceRecord, boolean acceptAndOpen)
throws ServiceRegistrationException {
serverUpdateServiceRecord(handle, serviceRecord, acceptAndOpen);
}

public long rfServerAcceptAndOpenRfServerConnection(long handle) throws IOException {
EmulatorRFCOMMService s = ((EmulatorRFCOMMService) activeLocalDevice().getConnection(handle));
if (!localDevice.isConnectable()) {
throw new BluetoothStateException("Local device is not connectable");
}
long clientHandle = 0;
boolean success = false;
while (!success) {
long connectionHandle = s.accept();
try {
long remoteAddress = localDevice.getDeviceManagerService().getRemoteAddress(localDevice.getAddress(),
connectionHandle);
EmulatorRFCOMMClient c = localDevice.createRFCOMMClient(remoteAddress);
c.connect(remoteAddress, connectionHandle);
localDevice.getDeviceManagerService().connectionAccepted(localDevice.getAddress(), connectionHandle);
success = true;
clientHandle = c.getHandle();
} catch (IOException e) {
DebugLog.debug("fail to accept connection", e);
continue;
} finally {
if (!success) {
localDevice.getDeviceManagerService().closeConnection(localDevice.getAddress(), connectionHandle);
}
}
}
return clientHandle;
}

public void connectionRfCloseServerConnection(long handle) throws IOException {
connectionRfCloseClientConnection(handle);
}

// --- Shared Client and Server RFCOMM connections

public int connectionRfRead(long handle) throws IOException {
return ((EmulatorRFCOMMClient) activeLocalDevice().getConnection(handle)).read();
}

public int connectionRfRead(long handle, byte[] b, int off, int len) throws IOException {
return ((EmulatorRFCOMMClient) activeLocalDevice().getConnection(handle)).read(b, off, len);
}

public int connectionRfReadAvailable(long handle) throws IOException {
return ((EmulatorRFCOMMClient) activeLocalDevice().getConnection(handle)).available();
}

public void connectionRfWrite(long handle, int b) throws IOException {
((EmulatorRFCOMMClient) activeLocalDevice().getConnection(handle)).write(b);
}

public void connectionRfWrite(long handle, byte[] b, int off, int len) throws IOException {
((EmulatorRFCOMMClient) activeLocalDevice().getConnection(handle)).write(b, off, len);
}

public void connectionRfFlush(long handle) throws IOException {
((EmulatorRFCOMMClient) activeLocalDevice().getConnection(handle)).flush();
}

public long getConnectionRfRemoteAddress(long handle) throws IOException {
return ((EmulatorRFCOMMClient) activeLocalDevice().getConnection(handle)).getRemoteAddress();
}

// --- Client and Server L2CAP connections

private void validateMTU(int receiveMTU, int transmitMTU) {
if (receiveMTU > localDevice.getBluetooth_l2cap_receiveMTU_max()) {
throw new IllegalArgumentException("invalid ReceiveMTU value " + receiveMTU);
}
}

public long l2OpenClientConnection(BluetoothConnectionParams params, int receiveMTU, int transmitMTU)
throws IOException {
validateMTU(receiveMTU, transmitMTU);
EmulatorL2CAPClient c = activeLocalDevice().createL2CAPClient(params.address);
boolean success = false;
try {
c.connect(params, receiveMTU, transmitMTU);
success = true;
} finally {
if (!success) {
localDevice.removeConnection(c);
}
}
return c.getHandle();
}

public void l2CloseClientConnection(long handle) throws IOException {
assertClosed();
EmulatorL2CAPClient c = ((EmulatorL2CAPClient) localDevice.getConnection(handle));
try {
c.close();
} finally {
localDevice.removeConnection(c);
}
}

public long l2ServerOpen(BluetoothConnectionNotifierParams params, int receiveMTU, int transmitMTU,
ServiceRecordImpl serviceRecord) throws IOException {
validateMTU(receiveMTU, transmitMTU);
EmulatorL2CAPService s = activeLocalDevice().createL2CAPService(params.bluecove_ext_psm);
boolean success = false;
try {
s.open(params, receiveMTU, transmitMTU);
serviceRecord.setHandle(s.getHandle());
serviceRecord.populateL2CAPAttributes((int) s.getHandle(), s.getPcm(), params.uuid, params.name);
s.updateServiceRecord(serviceRecord);
success = true;
} finally {
if (!success) {
localDevice.removeConnection(s);
}
}
return s.getHandle();
}

public void l2ServerUpdateServiceRecord(long handle, ServiceRecordImpl serviceRecord, boolean acceptAndOpen)
throws ServiceRegistrationException {
serverUpdateServiceRecord(handle, serviceRecord, acceptAndOpen);
}

public long l2ServerAcceptAndOpenServerConnection(long handle) throws IOException {
EmulatorL2CAPService s = ((EmulatorL2CAPService) activeLocalDevice().getConnection(handle));
if (!localDevice.isConnectable()) {
throw new BluetoothStateException("Local device is not connectable");
}
long clientHandle = 0;
boolean success = false;
while (!success) {
long connectionHandle = s.accept();
try {
long remoteAddress = localDevice.getDeviceManagerService().getRemoteAddress(localDevice.getAddress(),
connectionHandle);
EmulatorL2CAPClient c = localDevice.createL2CAPClient(remoteAddress);
c.connect(remoteAddress, connectionHandle, s.getReceiveMTU(), s.getTransmitMTU());
localDevice.getDeviceManagerService().connectionAccepted(localDevice.getAddress(), connectionHandle);
success = true;
clientHandle = c.getHandle();
} catch (IOException e) {
DebugLog.debug("fail to accept connection", e);
continue;
} finally {
if (!success) {
localDevice.getDeviceManagerService().closeConnection(localDevice.getAddress(), connectionHandle);
}
}
}
return clientHandle;
}

public void l2CloseServerConnection(long handle) throws IOException {
l2CloseClientConnection(handle);
}

public void l2ServerClose(long handle, ServiceRecordImpl serviceRecord) throws IOException {
assertClosed();
EmulatorL2CAPService s = ((EmulatorL2CAPService) localDevice.getConnection(handle));
try {
s.close(serviceRecord);
} finally {
localDevice.removeConnection(s);
}
}

public int l2GetSecurityOpt(long handle, int expected) throws IOException {
assertClosed();
return ((EmulatorLinkedConnection) localDevice.getConnection(handle)).getSecurityOpt(expected);
}

public boolean l2Ready(long handle) throws IOException {
return ((EmulatorL2CAPClient) activeLocalDevice().getConnection(handle)).ready();
}

public int l2Receive(long handle, byte[] inBuf) throws IOException {
return ((EmulatorL2CAPClient) activeLocalDevice().getConnection(handle)).receive(inBuf);
}

public void l2Send(long handle, byte[] data, int transmitMTU) throws IOException {
((EmulatorL2CAPClient) activeLocalDevice().getConnection(handle)).send(data);
}

public int l2GetReceiveMTU(long handle) throws IOException {
return ((EmulatorL2CAPClient) activeLocalDevice().getConnection(handle)).getReceiveMTU();
}

public int l2GetTransmitMTU(long handle) throws IOException {
return ((EmulatorL2CAPClient) activeLocalDevice().getConnection(handle)).getTransmitMTU();
}

public long l2RemoteAddress(long handle) throws IOException {
return ((EmulatorL2CAPClient) activeLocalDevice().getConnection(handle)).getRemoteAddress();
}

/*
* (non-Javadoc)
*
* @see com.intel.bluetooth.BluetoothStack#l2Encrypt(long,long,boolean)
*/
public boolean l2Encrypt(long address, long handle, boolean on) throws IOException {
assertClosed();
return ((EmulatorLinkedConnection) localDevice.getConnection(handle)).encrypt(address, on);
}
}

Change log

r3045 by skarzhevskyy on Jul 6, 2010   Diff
verify of bluecovepsm is supported
Go to: 
Project members, sign in to write a code review

Older revisions

r2945 by skarzhevskyy on Mar 21, 2009   Diff
honor requested TransmitMTU
r2917 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
r2471 by skarzhevskyy on Nov 30, 2008   Diff
Change license to Apache License,
Version 2.0, Update headers
All revisions of this file

File info

Size: 17660 bytes, 573 lines

File properties

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