View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2008 Michael Lifshits
4    *  Copyright (C) 2008 Vlad Skarzhevskyy
5    *
6    *  Licensed to the Apache Software Foundation (ASF) under one
7    *  or more contributor license agreements.  See the NOTICE file
8    *  distributed with this work for additional information
9    *  regarding copyright ownership.  The ASF licenses this file
10   *  to you under the Apache License, Version 2.0 (the
11   *  "License"); you may not use this file except in compliance
12   *  with the License.  You may obtain a copy of the License at
13   *
14   *    http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing,
17   *  software distributed under the License is distributed on an
18   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   *  KIND, either express or implied.  See the License for the
20   *  specific language governing permissions and limitations
21   *  under the License.
22   *
23   *  @author vlads
24   *  @version $Id: DeviceSDP.java 2471 2008-12-01 03:44:20Z skarzhevskyy $
25   */
26  package com.intel.bluetooth.emu;
27  
28  import java.util.Enumeration;
29  import java.util.Hashtable;
30  import java.util.Vector;
31  
32  import com.intel.bluetooth.DebugLog;
33  import com.intel.bluetooth.RemoteDeviceHelper;
34  
35  /**
36   * 
37   */
38  class DeviceSDP {
39  
40  	private long address;
41  
42  	private Hashtable<Long, ServicesDescriptor> services = new Hashtable<Long, ServicesDescriptor>();
43  
44  	DeviceSDP(long address) {
45  		this.address = address;
46  	}
47  
48  	synchronized void updateServiceRecord(long handle, ServicesDescriptor sdpData) {
49  		Long key = new Long(handle);
50  		boolean update = (services.get(key) != null);
51  		services.put(key, sdpData);
52  
53  		String[] serviceUuidSet = sdpData.getUuidSet();
54  		for (int i = 0; i < serviceUuidSet.length; i++) {
55  			DebugLog.debug((update ? "Update" : "Create") + " Srv on "
56  					+ RemoteDeviceHelper.getBluetoothAddress(address) + " " + handle + " " + i + " "
57  					+ serviceUuidSet[i]);
58  		}
59  	}
60  
61  	synchronized void removeServiceRecord(long handle) {
62  		ServicesDescriptor srv = (ServicesDescriptor) services.remove(new Long(handle));
63  		if (srv != null) {
64  			DebugLog.debug("Remove Srv on " + RemoteDeviceHelper.getBluetoothAddress(address) + " " + handle);
65  		}
66  	}
67  
68  	ServicesDescriptor getServicesDescriptor(long handle) {
69  		return (ServicesDescriptor) services.get(new Long(handle));
70  	}
71  
72  	synchronized long[] searchServices(String[] uuidSet) {
73  		Vector<Long> handles = new Vector<Long>();
74  
75  		for (Enumeration<Long> iterator = services.keys(); iterator.hasMoreElements();) {
76  			Long key = (Long) iterator.nextElement();
77  			ServicesDescriptor service = (ServicesDescriptor) services.get(key);
78  			String[] serviceUuidSet = service.getUuidSet();
79  			// No duplicate values in any set!
80  			int match = 0;
81  			for (int i = 0; i < serviceUuidSet.length; i++) {
82  				for (int k = 0; k < uuidSet.length; k++) {
83  					if (uuidSet[k].equals(serviceUuidSet[i])) {
84  						match++;
85  						break;
86  					}
87  				}
88  			}
89  			if (match == uuidSet.length) {
90  				handles.addElement(key);
91  			}
92  		}
93  
94  		long[] h = new long[handles.size()];
95  		int i = 0;
96  		for (Enumeration<Long> e = handles.elements(); e.hasMoreElements();) {
97  			h[i++] = ((Long) e.nextElement()).intValue();
98  		}
99  		return h;
100 	}
101 }