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
/**
* 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.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

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

/**
* Defines Emulator configuration properties.
*
* See properties defined in javax.bluetooth.LocalDevice.getProperty();
*/
public class EmulatorConfiguration implements Serializable {

private static final long serialVersionUID = 1L;

/**
* Name of configuration file or resource.
*
* Defaults to "bluecove.emulator.properties". Can be changed using system property "bluecove.emulator.properties"
*
*/
public static final String CONFIG_FILE_NAME = "bluecove.emulator.properties";

/**
* Allow to define device name. <br/>
* Example
*
* <pre>
* 0B1000000000.deviceName=My Device Server
* # Device is Computer
* 0B1000000000.deviceClass=0x0100
*
* 0B1000000001.deviceName=My Device Client
* # Device is Phone
* 0B1000000001.deviceClass=0x0200
*
* <pre>
*/
public static final String deviceName = "deviceName";

/**
* Allow to define device class.
*
* @see deviceName
*/
public static final String deviceClass = "deviceClass";

/**
* Defaults to '0B1000000000'.
*/
protected long firstDeviceAddress = 0x0B1000000000L;

/**
* Defaults to "EmuDevice".
*
*/
protected String deviceNamePrefix = "EmuDevice";

/**
* Created Devices are initially discoverable.
*/
protected boolean deviceDiscoverable = true;

/**
* Discoverable duration in seconds for LIAC. Defaults to <code>3</code>.
*/
protected int durationLIAC = 3;

/**
* deviceInquiryDuration in seconds, Defaults to <code>11</code>.
*/
protected int deviceInquiryDuration = 11;

/**
* Device Inquiry Duration is random. Defaults to <code>false</code>.
*/
protected boolean deviceInquiryRandomDelay = true;

/**
* Defaults to 8K.
*/
protected int connectionBufferSize = 8 * 1024;

/**
* Defaults to <code>true</code>.
*/
protected boolean linkEncryptionSupported = true;

/**
* stream.flush() will Block sender till client reads all data.
*/
protected boolean senderFlushBlock = false;

/**
* Monitor if client device is active.
*
* RMI timeout is up to 10 minutes. This property enables killing application and recovery faster.
*/
protected int keepAliveSeconds = 5;

private Map<String, String> propertiesMap;

public EmulatorConfiguration() {
propertiesMap = new Hashtable<String, String>();
final String TRUE = "true";
final String FALSE = "false";
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_DEVICES_MAX, "7");
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_SD_TRANS_MAX, "7");
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_INQUIRY_SCAN, TRUE);
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_PAGE_SCAN, TRUE);
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_INQUIRY, TRUE);
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_CONNECTED_PAGE, TRUE);
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_SD_ATTR_RETRIEVABLE_MAX, "255");
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_MASTER_SWITCH, FALSE);
propertiesMap.put(BluetoothConsts.PROPERTY_BLUETOOTH_L2CAP_RECEIVEMTU_MAX, "2048");
}

public void loadConfigFile() {
String configName = System.getProperty(CONFIG_FILE_NAME, CONFIG_FILE_NAME);
// Try file to find the file first
File file = new File(configName);
if (file.exists()) {
try {
load(new FileInputStream(file));
} catch (IOException e) {
DebugLog.error("Error loading properties from file " + file.getAbsolutePath(), e);
}
} else {
// find the resource in the classPath
if (!configName.startsWith("/")) {
configName = "/" + configName;
}
InputStream input = this.getClass().getResourceAsStream(configName);
if (input != null) {
try {
load(input);
} catch (IOException e) {
DebugLog.error("Error loading properties from resource " + configName, e);
}
}
}
}

private void load(InputStream input) throws IOException {
Properties values = new Properties();
try {
values.load(input);
} finally {
input.close();
}
for (Map.Entry<Object, Object> me : values.entrySet()) {
Object value = me.getValue();
if (value == null) {
continue;
}
String txt = value.toString().trim();
if (txt.length() == 0) {
continue;
}
propertiesMap.put(me.getKey().toString(), txt);
}
copyPertiesToFields();
}

private void copyPertiesToFields() {
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
String value = propertiesMap.get(field.getName());
if (value == null) {
continue;
}
Class<?> type = field.getType();
try {
if (String.class.isAssignableFrom(type)) {
field.set(this, value);
} else if (boolean.class.isAssignableFrom(type)) {
field.setBoolean(this, valueToBoolean(value));
} else if (int.class.isAssignableFrom(type)) {
field.setInt(this, valueToInt(value));
} else if (long.class.isAssignableFrom(type)) {
field.setLong(this, valueToLong(value));
}
} catch (Throwable e) {
DebugLog.error("Error setting property " + field.getName(), e);
}
}
}

private boolean valueToBoolean(String value) {
if (value.equalsIgnoreCase("true") || "1".equals(value)) {
return true;
} else {
return false;
}
}

public static int valueToInt(String value) {
if (value.startsWith("0x")) {
return Integer.parseInt(value.substring(2), 16);
} else {
return Integer.parseInt(value);
}
}

public static long valueToLong(String value) {
if (value.startsWith("0x")) {
return Long.parseLong(value.substring(2), 16);
} else {
return Long.parseLong(value);
}
}

public EmulatorConfiguration clone(long localAddress) {
String namePrefix = RemoteDeviceHelper.getBluetoothAddress(localAddress) + ".";
EmulatorConfiguration deviceConfig = new EmulatorConfiguration();
for (Map.Entry<String, String> me : this.propertiesMap.entrySet()) {
String key = me.getKey();
String value = me.getValue();
deviceConfig.propertiesMap.put(key, value);
if (key.startsWith(namePrefix)) {
deviceConfig.propertiesMap.put(key.substring(namePrefix.length()), value);
}
}
deviceConfig.copyPertiesToFields();
return deviceConfig;
}

public int getDurationLIAC() {
return durationLIAC;
}

public int getDeviceInquiryDuration() {
return deviceInquiryDuration;
}

public boolean isDeviceInquiryRandomDelay() {
return deviceInquiryRandomDelay;
}

public long getFirstDeviceAddress() {
return firstDeviceAddress;
}

/**
* Get specific device property.
*
* If specific not found, return global value.
*
* @param address
* device address
* @param property
* @return value or null
*/
public String getProperty(long address, String property) {
String addressString = RemoteDeviceHelper.getBluetoothAddress(address);
String v = getProperty(addressString + "." + property);
if (v != null) {
return v;
} else {
return getProperty(property);
}
}

public String getProperty(String property) {
return (String) propertiesMap.get(property);
}

public int getIntProperty(String property) {
return Integer.valueOf(getProperty(property)).intValue();
}

public String getDeviceNamePrefix() {
return deviceNamePrefix;
}

public boolean isDeviceDiscoverable() {
return this.deviceDiscoverable;
}

public int getConnectionBufferSize() {
return connectionBufferSize;
}

public boolean isLinkEncryptionSupported() {
return this.linkEncryptionSupported;
}

public int getKeepAliveSeconds() {
return keepAliveSeconds;
}

public boolean isSenderFlushBlock() {
return this.senderFlushBlock;
}
}
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

r2642 by skarzhevskyy on Dec 22, 2008   Diff
Fixed properties
r2599 by skarzhevskyy on Dec 16, 2008   Diff
Emulator configuration properties
r2525 by skarzhevskyy on Dec 08, 2008   Diff
Cleanup references to "bluetooth.*"
properties
All revisions of this file

File info

Size: 9122 bytes, 334 lines

File properties

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