View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
4    *
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: Configuration.java 2607 2008-12-17 23:51:33Z skarzhevskyy $
24   */
25  package net.sf.bluecove;
26  
27  import java.io.IOException;
28  import java.io.InputStream;
29  import java.util.Hashtable;
30  
31  import javax.bluetooth.ServiceRecord;
32  import javax.bluetooth.UUID;
33  
34  import org.bluecove.tester.util.IOUtils;
35  
36  import net.sf.bluecove.util.BooleanVar;
37  import net.sf.bluecove.util.IntVar;
38  import net.sf.bluecove.util.J2MEStringTokenizer;
39  import net.sf.bluecove.util.Storage;
40  
41  /**
42   * 
43   * This define different client and server work patterns to identify problem in native code.
44   * 
45   */
46  public class Configuration {
47  
48  	public static BooleanVar deviceClassFilter = new BooleanVar(true);
49  
50  	public static BooleanVar discoverDevicesComputers = new BooleanVar(true);
51  
52  	public static BooleanVar discoverDevicesPhones = new BooleanVar(true);
53  
54  	public static boolean searchOnlyBluecoveUuid = true;
55  
56  	public static boolean discoverySearchOnlyBluecoveUuid = false;
57  
58  	/**
59  	 * Limit connections to precompiled list of test devices.
60  	 */
61  	public static BooleanVar listedDevicesOnly = new BooleanVar(false);
62  
63  	/**
64  	 * This may hung forever on some Nokia devices.
65  	 */
66  	public static BooleanVar discoveryGetDeviceFriendlyName = new BooleanVar(false);
67  
68  	public static UUID discoveryUUID = new UUID(0x0100); // L2CAP
69  
70  	public static BooleanVar useShortUUID = new BooleanVar(false);
71  
72  	public static BooleanVar useServiceClassExtUUID = new BooleanVar(false);
73  
74  	public static Hashtable testDeviceNames = null;
75  
76  	public static Hashtable ignoreDevices = null;
77  
78  	public static Hashtable useDevices = null;
79  
80  	public static BooleanVar serverAcceptWhileConnected = new BooleanVar(false);
81  
82  	public static boolean serverAcceptWhileConnectedOnJavaSE = true;
83  
84  	public static boolean serverContinuous = true;
85  
86  	public static BooleanVar clientContinuous = new BooleanVar(true);
87  
88  	public static BooleanVar clientContinuousDiscovery = new BooleanVar(true);;
89  
90  	public static boolean clientContinuousDiscoveryDevices = true;
91  
92  	public static boolean clientContinuousServicesSearch = true;
93  
94  	public static boolean clientTestConnections = true;
95  
96  	// This test concurrent connections if you have Multiple servers running.
97  	public static boolean clientTestConnectionsMultipleThreads = true;
98  
99  	public static BooleanVar authenticate = new BooleanVar(false);
100 
101 	public static BooleanVar encrypt = new BooleanVar(false);
102 
103 	public static boolean authorize = false;
104 
105 	public static BooleanVar testRFCOMM = new BooleanVar(true);
106 
107 	public static IntVar TEST_CASE_FIRST = new IntVar(1);
108 
109 	public static IntVar TEST_CASE_LAST = new IntVar(Consts.TEST_LAST_WORKING);
110 
111 	public static IntVar STERSS_TEST_CASE = new IntVar(Consts.TEST_BYTE);
112 
113 	public static BooleanVar testL2CAP = new BooleanVar(true);
114 
115 	public static IntVar TEST_CASE_L2CAP_FIRST = new IntVar(1);
116 
117 	public static IntVar TEST_CASE_L2CAP_LAST = new IntVar(Consts.TEST_L2CAP_LAST_WORKING);
118 
119 	public static String bluecovepsm;
120 
121 	public static BooleanVar testServerOBEX_TCP = new BooleanVar(false);
122 
123 	public static boolean testServerOBEXObjectPush = false;
124 
125 	public static IntVar authenticateOBEX = new IntVar(0);
126 
127 	public static BooleanVar testServiceAttributes = new BooleanVar(true);
128 
129 	public static BooleanVar testAllServiceAttributes = new BooleanVar(false);
130 
131 	public static int tgSleep = 2;
132 
133 	public static int tgSize = 90;
134 
135 	public static int tgDurationMin = 2;
136 
137 	/**
138 	 * Apparently Motorola Service Attribute STRING is not working. INT_4 not working on some Nokia and breakers its
139 	 * discovery by Motorola. INT_16 are truncated in discovery by WIDCOMM Service attributes are not supported on
140 	 * BlueSoleil
141 	 */
142 	public static BooleanVar testIgnoreNotWorkingServiceAttributes = new BooleanVar(true);
143 
144 	public static BooleanVar testServerForceDiscoverable = new BooleanVar(false);
145 
146 	public static boolean initializeLocalDevice = true;
147 
148 	public static int clientSleepBetweenConnections = 4100;
149 
150 	public static int serverSleepB4ClosingConnection = 1000;
151 
152 	public static int clientTestTimeOutSec = 60;
153 
154 	public static int clientTestStopOnErrorCount = 100;
155 
156 	public static int serverTestTimeOutSec = 60;
157 
158 	public static int serverMAXTimeSec = 80;
159 
160 	public static int clientSleepOnConnectionRetry = 500;
161 
162 	public static int clientSleepOnDeviceInquiryError = 10000;
163 
164 	public static Storage storage;
165 
166 	private static String lastServerURL = null;
167 
168 	/**
169 	 * We can't add Motorola TCKAgent to this MIDlet.
170 	 */
171 	public static final boolean likedTCKAgent = true;
172 
173 	/**
174 	 * Apparently on Motorola iDEN serverConnection.acceptAndOpen() never returns.
175 	 */
176 	public static boolean canCloseServer = true;
177 
178 	public static boolean isConfigured = false;
179 
180 	public static boolean windows = false;
181 
182 	public static boolean windowsXP = false;
183 
184 	public static boolean windowsCE = false;
185 
186 	public static boolean linux = false;
187 
188 	public static boolean macOSx = false;
189 
190 	public static boolean stackWIDCOMM = false;
191 
192 	public static boolean hasManyDevices = false;
193 
194 	public static boolean supportL2CAP = true;
195 
196 	public static boolean logTimeStamp = false;
197 
198 	public static boolean screenSizeSmall = false;
199 
200 	public static Object threadLocalBluetoothStack;
201 
202 	static {
203 		testDeviceNames = new Hashtable();
204 		ignoreDevices = new Hashtable();
205 		useDevices = new Hashtable();
206 		loadNames(testDeviceNames, "bluecove.device.names.txt");
207 		loadNames(ignoreDevices, "bluecove.device.ignore.txt");
208 		loadNames(useDevices, "bluecove.device.use.txt");
209 
210 		if ((ignoreDevices.size() != 0) || (useDevices.size() != 0)) {
211 			listedDevicesOnly.setValue(true);
212 		}
213 
214 		String sysName = System.getProperty("os.name");
215 		if (sysName != null) {
216 			sysName = sysName.toLowerCase();
217 			if (sysName.indexOf("windows") != -1) {
218 				windows = true;
219 				if (sysName.indexOf("ce") != -1) {
220 					windowsCE = true;
221 				} else {
222 					windowsXP = true;
223 				}
224 			} else if (sysName.indexOf("mac os x") != -1) {
225 				macOSx = true;
226 			} else if (sysName.indexOf("linux") != -1) {
227 				linux = true;
228 			}
229 		}
230 	}
231 
232 	public static boolean isWhiteDevice(String bluetoothAddress) {
233 		String addr = bluetoothAddress.toUpperCase();
234 		if (useDevices.get(addr) != null) {
235 			return true;
236 		} else if (useDevices.size() > 0) {
237 			return false;
238 		} else if (ignoreDevices.get(addr) != null) {
239 			return false;
240 		}
241 		return (testDeviceNames.get(addr) != null);
242 	}
243 
244 	private static void loadNames(Hashtable deviceNames, String resourceName) {
245 		InputStream inputstream = Configuration.class.getResourceAsStream("/" + resourceName);
246 		if (inputstream == null) {
247 			return;
248 		}
249 		StringBuffer b = new StringBuffer();
250 		try {
251 			byte[] buf = new byte[1024];
252 			int i = 0;
253 			while ((i = inputstream.read(buf)) != -1) {
254 				b.append(new String(buf, 0, i));
255 			}
256 		} catch (IOException e) {
257 			return;
258 		} finally {
259 			IOUtils.closeQuietly(inputstream);
260 		}
261 		J2MEStringTokenizer st = new J2MEStringTokenizer(b.toString(), "\n");
262 		while (st.hasMoreTokens()) {
263 			String s = st.nextToken().trim();
264 			if (s.startsWith("#") || s.length() == 0) {
265 				continue;
266 			}
267 			int idx = s.indexOf(',');
268 			if ((idx == -1) || (idx == s.length())) {
269 				deviceNames.put(s, s);
270 			} else {
271 				deviceNames.put(s.substring(0, idx).trim(), s.substring(idx + 1).trim());
272 			}
273 		}
274 	}
275 
276 	public static boolean useMajorDeviceClass(int majorDeviceClass) {
277 		if (!Configuration.deviceClassFilter.booleanValue()) {
278 			return true;
279 		}
280 		switch (majorDeviceClass) {
281 		case Consts.DEVICE_COMPUTER:
282 			return Configuration.discoverDevicesComputers.booleanValue();
283 		case Consts.DEVICE_PHONE:
284 			return Configuration.discoverDevicesPhones.booleanValue();
285 		default:
286 			return (!Configuration.discoverDevicesPhones.booleanValue())
287 					&& (!Configuration.discoverDevicesComputers.booleanValue());
288 		}
289 	}
290 
291 	public static UUID blueCoveUUID() {
292 		if (useShortUUID.booleanValue()) {
293 			return Consts.uuidShort;
294 		} else {
295 			return Consts.uuidLong;
296 		}
297 	}
298 
299 	public static UUID blueCoveL2CAPUUID() {
300 		if (useShortUUID.booleanValue()) {
301 			return Consts.uuidL2CAPShort;
302 		} else {
303 			return Consts.uuidL2CAPLong;
304 		}
305 	}
306 
307 	public static UUID blueCoveOBEXUUID() {
308 		return Consts.uuidOBEX;
309 	}
310 
311 	public static int getRequiredSecurity() {
312 		int requiredSecurity = ServiceRecord.NOAUTHENTICATE_NOENCRYPT;
313 		if (Configuration.authenticate.booleanValue()) {
314 			if (Configuration.encrypt.booleanValue()) {
315 				requiredSecurity = ServiceRecord.AUTHENTICATE_ENCRYPT;
316 			} else {
317 				requiredSecurity = ServiceRecord.AUTHENTICATE_NOENCRYPT;
318 			}
319 		} else if (Configuration.encrypt.booleanValue()) {
320 			throw new IllegalArgumentException("Illegal encrypt configuration");
321 		}
322 		return requiredSecurity;
323 	}
324 
325 	public static String serverURLParams() {
326 		StringBuffer buf = new StringBuffer();
327 		buf.append(";authenticate=").append(authenticate.booleanValue() ? "true" : "false");
328 		buf.append(";encrypt=").append(encrypt.booleanValue() ? "true" : "false");
329 		buf.append(";authorize=").append(authorize ? "true" : "false");
330 		return buf.toString();
331 	}
332 
333 	public static String getLastServerURL() {
334 		if (lastServerURL == null) {
335 			lastServerURL = Configuration.storage.retriveData(Storage.configLastServiceURL);
336 		}
337 		return lastServerURL;
338 	}
339 
340 	public static void setLastServerURL(String lastServerURL) {
341 		Configuration.lastServerURL = lastServerURL;
342 		if (Configuration.storage != null) {
343 			Configuration.storage.storeData(Storage.configLastServiceURL, lastServerURL);
344 		}
345 	}
346 
347 	public static String getStorageData(String name, String defaultValue) {
348 		String val = null;
349 		if (Configuration.storage != null) {
350 			val = Configuration.storage.retriveData(name);
351 		}
352 		return (val == null) ? defaultValue : val;
353 	}
354 
355 	public static void storeData(String name, String value) {
356 		if (Configuration.storage != null) {
357 			Configuration.storage.storeData(name, value);
358 		}
359 	}
360 }