My favorites | Sign in
Logo
                
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
/**
* 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.emu;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.bluetooth.BluetoothConnectionException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.ServiceRegistrationException;

import com.intel.bluetooth.DebugLog;
import com.intel.bluetooth.RemoteDeviceHelper;

public class DeviceManagerServiceImpl implements DeviceManagerService {

public static final int MAJOR_COMPUTER = 0x0100;

static final EmulatorConfiguration configuration;

private static Map<Long, Device> devices = new Hashtable<Long, Device>();

static {
configuration = new EmulatorConfiguration();
configuration.loadConfigFile();
}

public DeviceManagerServiceImpl() {
}

public void shutdown() {
synchronized (devices) {
for (Iterator<Device> iterator = devices.values().iterator(); iterator.hasNext();) {
Device device = iterator.next();
device.release();
}
devices.clear();
}
}

public DeviceDescriptor createNewDevice(String deviceID, String deviceAddress) throws BluetoothStateException {
synchronized (devices) {
long address = getNextAvailableBTAddress(deviceID, deviceAddress);

String name = configuration.getProperty(address, EmulatorConfiguration.deviceName);
if (name == null) {
name = configuration.getDeviceNamePrefix() + RemoteDeviceHelper.getBluetoothAddress(address);
}
int deviceClass = MAJOR_COMPUTER;
String cod = configuration.getProperty(address, EmulatorConfiguration.deviceClass);
if (cod != null) {
deviceClass = EmulatorConfiguration.valueToInt(cod);
}
DeviceDescriptor descriptor = new DeviceDescriptor(address, name, deviceClass);

if (!configuration.isDeviceDiscoverable()) {
descriptor.setDiscoverableMode(DiscoveryAgent.NOT_DISCOVERABLE);
}

devices.put(new Long(address), new Device(descriptor));
return descriptor;
}
}

public EmulatorConfiguration getEmulatorConfiguration(long localAddress) {
return configuration.clone(localAddress);
}

public void releaseDevice(long address) {
Device device;
synchronized (devices) {
device = (Device) devices.remove(new Long(address));
}
if (device != null) {
device.release();
}
}

public DeviceCommand pollCommand(long address) {
Device device = getDevice(address);
if (device == null) {
throw new RuntimeException("No such device " + RemoteDeviceHelper.getBluetoothAddress(address));
}
return device.pollCommand();
}

static Device getDevice(long address) {
Device d = ((Device) devices.get(new Long(address)));
if ((d != null) && (!d.isAlive())) {
synchronized (devices) {
devices.remove(new Long(address));
}
d.died();
return null;
}
return d;
}

private Device getActiveDevice(long address) {
Device d = getDevice(address);
if ((d != null) && (!d.getDescriptor().isPoweredOn())) {
return null;
} else {
return d;
}
}

public DeviceDescriptor getDeviceDescriptor(long address) {
Device device = getDevice(address);
if (device == null) {
throw new RuntimeException("No such device " + RemoteDeviceHelper.getBluetoothAddress(address));
}
return device.getDescriptor();
}

private DeviceSDP getDeviceSDP(long address) {
Device device = getActiveDevice(address);
if (device == null) {
return null;
}
return device.getDeviceSDP(false);
}

public static List<MonitorDevice> getMonitorDevices() {
Vector<MonitorDevice> monitorDevices = new Vector<MonitorDevice>();
synchronized (devices) {
for (Iterator<Device> iterator = devices.values().iterator(); iterator.hasNext();) {
Device device = iterator.next();
if (!device.isAlive()) {
iterator.remove();
device.died();
continue;
}
isDiscoverable(device.getDescriptor());
monitorDevices.add(new MonitorDevice(device));
}
}
return monitorDevices;
}

public DeviceDescriptor[] getDiscoveredDevices(long address) {
Vector<DeviceDescriptor> discoveredDevice = new Vector<DeviceDescriptor>();
synchronized (devices) {
for (Iterator<Device> iterator = devices.values().iterator(); iterator.hasNext();) {
Device device = iterator.next();
if (!device.isAlive()) {
iterator.remove();
device.died();
continue;
}
if (device.getDescriptor().getAddress() == address) {
continue;
}
if (isDiscoverable(device.getDescriptor())) {
discoveredDevice.addElement(device.getDescriptor());
}
}
}
return (DeviceDescriptor[]) discoveredDevice.toArray(new DeviceDescriptor[discoveredDevice.size()]);
}

private static boolean isDiscoverable(DeviceDescriptor device) {
if (!device.isPoweredOn()) {
return false;
}
int discoverableMode = device.getDiscoverableMode();
switch (discoverableMode) {
case DiscoveryAgent.NOT_DISCOVERABLE:
return false;
case DiscoveryAgent.GIAC:
return true;
case DiscoveryAgent.LIAC:
if (device.getLimitedDiscoverableStart() + configuration.getDurationLIAC() * 1000 * 60 < System
.currentTimeMillis()) {
DebugLog.debug(RemoteDeviceHelper.getBluetoothAddress(device.getAddress())
+ " LIAC -> NOT_DISCOVERABLE");
device.setDiscoverableMode(DiscoveryAgent.NOT_DISCOVERABLE);
return false;
} else {
return true;
}
default:
// No idea what are other modes are.
return true;
}
}

public boolean isLocalDevicePowerOn(long localAddress) {
return getDeviceDescriptor(localAddress).isPoweredOn();
}

public void setLocalDevicePower(long localAddress, boolean on) {
Device device = getDevice(localAddress);
if (device == null) {
throw new RuntimeException("No such device " + RemoteDeviceHelper.getBluetoothAddress(localAddress));
}
device.setDevicePower(on);
}

public int getLocalDeviceDiscoverable(long localAddress) {
DeviceDescriptor dd = getDeviceDescriptor(localAddress);
if (!dd.isPoweredOn()) {
return DiscoveryAgent.NOT_DISCOVERABLE;
}
// Update mode if it was LIAC
isDiscoverable(dd);
return dd.getDiscoverableMode();
}

public boolean setLocalDeviceDiscoverable(long localAddress, int mode) throws BluetoothStateException {
DeviceDescriptor dd = getDeviceDescriptor(localAddress);
if (!dd.isPoweredOn()) {
throw new BluetoothStateException("Device power is off");
}
DebugLog.debug(RemoteDeviceHelper.getBluetoothAddress(localAddress) + " setDiscoverableMode", EmulatorUtils
.discoverableModeString(mode));
dd.setDiscoverableMode(mode);
return true;
}

public void setLocalDeviceServiceClasses(long localAddress, int classOfDevice) {
DebugLog.debug(RemoteDeviceHelper.getBluetoothAddress(localAddress) + " setServiceClasses ", classOfDevice);
getDeviceDescriptor(localAddress).setDeviceClass(classOfDevice);
}

public String getRemoteDeviceFriendlyName(long address) throws IOException {
DeviceDescriptor dd = getDeviceDescriptor(address);
if (!dd.isPoweredOn()) {
throw new IOException("Remote device power is off");
}
return dd.getName();
}

private long getNextAvailableBTAddress(String deviceID, String deviceAddress) throws BluetoothStateException {
if (deviceID != null) {
long id = configuration.getFirstDeviceAddress() + Long.parseLong(deviceID);
if (getDevice(id) != null) {
throw new BluetoothStateException("Device already reserved "
+ RemoteDeviceHelper.getBluetoothAddress(id));
}
return id;
} else if (deviceAddress != null) {
long address = RemoteDeviceHelper.getAddress(deviceAddress);
if (getDevice(address) != null) {
throw new BluetoothStateException("Device already reserved "
+ RemoteDeviceHelper.getBluetoothAddress(address));
}
return address;
} else {
return EmulatorUtils.getNextAvailable(devices.keySet(), configuration.getFirstDeviceAddress(), 1);
}

}

public void updateServiceRecord(long address, long handle, ServicesDescriptor sdpData)
throws ServiceRegistrationException {
Device device = getActiveDevice(address);
if (device == null) {
throw new ServiceRegistrationException("No such device " + RemoteDeviceHelper.getBluetoothAddress(address));
}
DeviceSDP ds = device.getDeviceSDP(true);
ds.updateServiceRecord(handle, sdpData);
}

public void removeServiceRecord(long address, long handle) throws IOException {
DeviceSDP ds = getDeviceSDP(address);
if (ds != null) {
ds.removeServiceRecord(handle);
}
}

public long[] searchServices(long address, String[] uuidSet) {
if (getActiveDevice(address) == null) {
return null;
}
DeviceSDP ds = getDeviceSDP(address);
if (ds == null) {
return new long[0];
}
return ds.searchServices(uuidSet);
}

public byte[] getServicesRecordBinary(long address, long handle) throws IOException {
DeviceSDP ds = getDeviceSDP(address);
if (ds == null) {
throw new IOException("No such device " + RemoteDeviceHelper.getBluetoothAddress(address));
}
ServicesDescriptor sd = ds.getServicesDescriptor(handle);
if (sd == null) {
throw new IOException("No such service");
}
return sd.getSdpBinary();
}

public void rfOpenService(long localAddress, int channel) throws IOException {
openService(localAddress, ServiceListener.rfPrefix(channel));
}

public long rfAccept(long localAddress, int channel, boolean authenticate, boolean encrypt) throws IOException {
return accept(localAddress, ServiceListener.rfPrefix(channel), authenticate, encrypt, 0);
}

public long rfConnect(long localAddress, long remoteAddress, int channel, boolean authenticate, boolean encrypt,
int timeout) throws IOException {
return connect(localAddress, remoteAddress, ServiceListener.rfPrefix(channel), authenticate, encrypt, 0,
timeout);
}

public void rfCloseService(long address, int pcm) {
closeService(address, ServiceListener.rfPrefix(pcm));
}

public void l2OpenService(long localAddress, int pcm) throws IOException {
openService(localAddress, ServiceListener.l2Prefix(pcm));
}

public long l2Accept(long localAddress, int channel, boolean authenticate, boolean encrypt, int receiveMTU)
throws IOException {
return accept(localAddress, ServiceListener.l2Prefix(channel), authenticate, encrypt, receiveMTU);
}

public long l2Connect(long localAddress, long remoteAddress, int channel, boolean authenticate, boolean encrypt,
int receiveMTU, int timeout) throws IOException {
return connect(localAddress, remoteAddress, ServiceListener.l2Prefix(channel), authenticate, encrypt,
receiveMTU, timeout);
}

public void l2CloseService(long address, int channel) {
closeService(address, ServiceListener.l2Prefix(channel));
}

private long accept(long localAddress, String channelID, boolean authenticate, boolean encrypt, int receiveMTU)
throws IOException {
Device device;
if ((device = getActiveDevice(localAddress)) == null) {
throw new IOException("No such device " + RemoteDeviceHelper.getBluetoothAddress(localAddress));
}
ServiceListener sl = device.createServiceListener(channelID);
return sl.accept(device, authenticate, encrypt, receiveMTU);
}

private long connect(long localAddress, long remoteAddress, String portID, boolean authenticate, boolean encrypt,
int receiveMTU, int timeout) throws IOException {
Device remoteDevice = getActiveDevice(remoteAddress);
if (remoteDevice == null) {
throw new BluetoothConnectionException(BluetoothConnectionException.FAILED_NOINFO, "No such device "
+ RemoteDeviceHelper.getBluetoothAddress(remoteAddress));
}
Device localDevice = getActiveDevice(localAddress);
if (localDevice == null) {
throw new BluetoothConnectionException(BluetoothConnectionException.FAILED_NOINFO, "No such device "
+ RemoteDeviceHelper.getBluetoothAddress(localAddress));
}
if (localDevice.getConnectionBuffer(remoteAddress, portID) != null) {
throw new BluetoothConnectionException(BluetoothConnectionException.FAILED_NOINFO,
"Already connected to the same port " + portID + " on "
+ RemoteDeviceHelper.getBluetoothAddress(remoteAddress));
}
ServiceListener sl = remoteDevice.connectService(portID, timeout);
if (sl == null) {
throw new BluetoothConnectionException(BluetoothConnectionException.UNKNOWN_PSM, "No such service "
+ portID);
}
return sl.connect(localDevice, authenticate, encrypt, receiveMTU, timeout);
}

public void connectionAccepted(long localAddress, long connectionId) throws IOException {
Device device;
if ((device = getActiveDevice(localAddress)) == null) {
throw new IOException("No such device " + RemoteDeviceHelper.getBluetoothAddress(localAddress));
}
ConnectionBuffer c = device.getConnectionBuffer(connectionId);
if (c == null) {
throw new IOException("No such connection " + connectionId);
}
c.accepted();
}

private void openService(long address, String channelID) throws IOException {
Device device = getActiveDevice(address);
if (device == null) {
throw new IOException("No such device " + RemoteDeviceHelper.getBluetoothAddress(address));
}
device.openService(channelID);
}

private void closeService(long address, String channelID) {
Device device = getDevice(address);
if (device == null) {
return;
}
device.closeService(channelID);
}

private ConnectionBuffer getConnectionBuffer(long localAddress, long connectionId) throws IOException {
Device localDevice = getActiveDevice(localAddress);
if (localDevice == null) {
throw new IOException("No such device " + RemoteDeviceHelper.getBluetoothAddress(localAddress));
}
ConnectionBuffer c = localDevice.getConnectionBuffer(connectionId);
if (c == null) {
throw new IOException("No such connection " + connectionId);
}
return c;
}

public long getRemoteAddress(long localAddress, long connectionId) throws IOException {
return getConnectionBuffer(localAddress, connectionId).getRemoteAddress();
}

public void rfWrite(long localAddress, long connectionId, byte[] b) throws IOException {
((ConnectionBufferRFCOMM) getConnectionBuffer(localAddress, connectionId)).rfWrite(b);
}

public void rfFlush(long localAddress, long connectionId) throws IOException {
((ConnectionBufferRFCOMM) getConnectionBuffer(localAddress, connectionId)).rfFlush();
}

public int rfAvailable(long localAddress, long connectionId) throws IOException {
return ((ConnectionBufferRFCOMM) getConnectionBuffer(localAddress, connectionId)).rfAvailable();
}

public byte[] rfRead(long localAddress, long connectionId, int len) throws IOException {
return ((ConnectionBufferRFCOMM) getConnectionBuffer(localAddress, connectionId)).rfRead(len);
}

public void closeConnection(long localAddress, long connectionId) throws IOException {
Device localDevice = getDevice(localAddress);
if (localDevice == null) {
throw new IOException("No such device " + RemoteDeviceHelper.getBluetoothAddress(localAddress));
}
localDevice.closeConnection(connectionId);
}

public int getSecurityOpt(long localAddress, long connectionId, int expected) throws IOException {
return ((ConnectionBuffer) getConnectionBuffer(localAddress, connectionId)).getSecurityOpt(expected);
}

public boolean encrypt(long localAddress, long connectionId, long remoteAddress, boolean on) throws IOException {
return ((ConnectionBuffer) getConnectionBuffer(localAddress, connectionId)).encrypt(remoteAddress, on);
}

public int l2RemoteDeviceReceiveMTU(long localAddress, long connectionId) throws IOException {
return ((ConnectionBufferL2CAP) getConnectionBuffer(localAddress, connectionId)).getRemoteReceiveMTU();
}

public boolean l2Ready(long localAddress, long connectionId) throws IOException {
return ((ConnectionBufferL2CAP) getConnectionBuffer(localAddress, connectionId)).ready();
}

public byte[] l2Receive(long localAddress, long connectionId, int len) throws IOException {
return ((ConnectionBufferL2CAP) getConnectionBuffer(localAddress, connectionId)).receive(len);
}

public void l2Send(long localAddress, long connectionId, byte[] data) throws IOException {
((ConnectionBufferL2CAP) getConnectionBuffer(localAddress, connectionId)).send(data);
}
}
Show details Hide details

Change log

r2917 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2600 by skarzhevskyy on Dec 16, 2008   Diff
Impl should be public
r2599 by skarzhevskyy on Dec 16, 2008   Diff
Emulator configuration properties
r2578 by skarzhevskyy on Dec 13, 2008   Diff
RFCOMM flush(); Block till client
reads all data.
All revisions of this file

File info

Size: 17171 bytes, 486 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Hosted by Google Code