ContextMap.java revision 9d2ae23701114aedad1c8ea9ef17f93cc07adfd0
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.android.bluetooth.gatt; 17 18import android.os.Binder; 19import android.os.IBinder; 20import android.os.IBinder.DeathRecipient; 21import android.os.IInterface; 22import android.os.RemoteException; 23import android.util.Log; 24import java.util.ArrayList; 25import java.util.HashSet; 26import java.util.Iterator; 27import java.util.List; 28import java.util.NoSuchElementException; 29import java.util.Set; 30import java.util.UUID; 31import java.util.HashMap; 32import java.util.Map; 33 34import com.android.bluetooth.btservice.BluetoothProto; 35/** 36 * Helper class that keeps track of registered GATT applications. 37 * This class manages application callbacks and keeps track of GATT connections. 38 * @hide 39 */ 40/*package*/ class ContextMap<T> { 41 private static final String TAG = GattServiceConfig.TAG_PREFIX + "ContextMap"; 42 43 /** 44 * Connection class helps map connection IDs to device addresses. 45 */ 46 class Connection { 47 int connId; 48 String address; 49 int appId; 50 long startTime; 51 52 Connection(int connId, String address,int appId) { 53 this.connId = connId; 54 this.address = address; 55 this.appId = appId; 56 this.startTime = System.currentTimeMillis(); 57 } 58 } 59 60 /** 61 * Application entry mapping UUIDs to appIDs and callbacks. 62 */ 63 class App { 64 /** The UUID of the application */ 65 UUID uuid; 66 67 /** The id of the application */ 68 int id; 69 70 /** The package name of the application */ 71 String name; 72 73 /** Statistics for this app */ 74 AppScanStats appScanStats; 75 76 /** Application callbacks */ 77 T callback; 78 79 /** Death receipient */ 80 private IBinder.DeathRecipient mDeathRecipient; 81 82 /** Flag to signal that transport is congested */ 83 Boolean isCongested = false; 84 85 /** Internal callback info queue, waiting to be send on congestion clear */ 86 private List<CallbackInfo> congestionQueue = new ArrayList<CallbackInfo>(); 87 88 /** 89 * Creates a new app context. 90 */ 91 App(UUID uuid, T callback, String name, AppScanStats appScanStats) { 92 this.uuid = uuid; 93 this.callback = callback; 94 this.name = name; 95 this.appScanStats = appScanStats; 96 } 97 98 /** 99 * Link death recipient 100 */ 101 void linkToDeath(IBinder.DeathRecipient deathRecipient) { 102 try { 103 IBinder binder = ((IInterface)callback).asBinder(); 104 binder.linkToDeath(deathRecipient, 0); 105 mDeathRecipient = deathRecipient; 106 } catch (RemoteException e) { 107 Log.e(TAG, "Unable to link deathRecipient for app id " + id); 108 } 109 } 110 111 /** 112 * Unlink death recipient 113 */ 114 void unlinkToDeath() { 115 if (mDeathRecipient != null) { 116 try { 117 IBinder binder = ((IInterface)callback).asBinder(); 118 binder.unlinkToDeath(mDeathRecipient,0); 119 } catch (NoSuchElementException e) { 120 Log.e(TAG, "Unable to unlink deathRecipient for app id " + id); 121 } 122 } 123 } 124 125 void queueCallback(CallbackInfo callbackInfo) { 126 congestionQueue.add(callbackInfo); 127 } 128 129 CallbackInfo popQueuedCallback() { 130 if (congestionQueue.size() == 0) return null; 131 return congestionQueue.remove(0); 132 } 133 } 134 135 /** Our internal application list */ 136 List<App> mApps = new ArrayList<App>(); 137 138 /** Internal map to keep track of logging information by app name */ 139 HashMap<String, AppScanStats> mAppScanStats = new HashMap<String, AppScanStats>(); 140 141 /** Internal list of connected devices **/ 142 Set<Connection> mConnections = new HashSet<Connection>(); 143 144 /** 145 * Add an entry to the application context list. 146 */ 147 void add(UUID uuid, T callback, GattService service) { 148 String appName = service.getPackageManager().getNameForUid( 149 Binder.getCallingUid()); 150 if (appName == null) { 151 // Assign an app name if one isn't found 152 appName = "Unknown App (UID: " + Binder.getCallingUid() + ")"; 153 } 154 synchronized (mApps) { 155 AppScanStats appScanStats = mAppScanStats.get(appName); 156 if (appScanStats == null) { 157 appScanStats = new AppScanStats(appName, this, service); 158 mAppScanStats.put(appName, appScanStats); 159 } 160 mApps.add(new App(uuid, callback, appName, appScanStats)); 161 appScanStats.isRegistered = true; 162 } 163 } 164 165 /** 166 * Remove the context for a given UUID 167 */ 168 void remove(UUID uuid) { 169 synchronized (mApps) { 170 Iterator<App> i = mApps.iterator(); 171 while (i.hasNext()) { 172 App entry = i.next(); 173 if (entry.uuid.equals(uuid)) { 174 entry.unlinkToDeath(); 175 entry.appScanStats.isRegistered = false; 176 i.remove(); 177 break; 178 } 179 } 180 } 181 } 182 183 /** 184 * Remove the context for a given application ID. 185 */ 186 void remove(int id) { 187 synchronized (mApps) { 188 Iterator<App> i = mApps.iterator(); 189 while (i.hasNext()) { 190 App entry = i.next(); 191 if (entry.id == id) { 192 removeConnectionsByAppId(id); 193 entry.unlinkToDeath(); 194 entry.appScanStats.isRegistered = false; 195 i.remove(); 196 break; 197 } 198 } 199 } 200 } 201 202 /** 203 * Add a new connection for a given application ID. 204 */ 205 void addConnection(int id, int connId, String address) { 206 synchronized (mConnections) { 207 App entry = getById(id); 208 if (entry != null) { 209 mConnections.add(new Connection(connId, address, id)); 210 } 211 } 212 } 213 214 /** 215 * Remove a connection with the given ID. 216 */ 217 void removeConnection(int id, int connId) { 218 synchronized (mConnections) { 219 Iterator<Connection> i = mConnections.iterator(); 220 while (i.hasNext()) { 221 Connection connection = i.next(); 222 if (connection.connId == connId) { 223 i.remove(); 224 break; 225 } 226 } 227 } 228 } 229 230 /** 231 * Remove all connections for a given application ID. 232 */ 233 void removeConnectionsByAppId(int appId) { 234 Iterator<Connection> i = mConnections.iterator(); 235 while (i.hasNext()) { 236 Connection connection = i.next(); 237 if (connection.appId == appId) { 238 i.remove(); 239 } 240 } 241 } 242 243 /** 244 * Get an application context by ID. 245 */ 246 App getById(int id) { 247 Iterator<App> i = mApps.iterator(); 248 while (i.hasNext()) { 249 App entry = i.next(); 250 if (entry.id == id) return entry; 251 } 252 Log.e(TAG, "Context not found for ID " + id); 253 return null; 254 } 255 256 /** 257 * Get an application context by UUID. 258 */ 259 App getByUuid(UUID uuid) { 260 Iterator<App> i = mApps.iterator(); 261 while (i.hasNext()) { 262 App entry = i.next(); 263 if (entry.uuid.equals(uuid)) return entry; 264 } 265 Log.e(TAG, "Context not found for UUID " + uuid); 266 return null; 267 } 268 269 /** 270 * Get an application context by the calling Apps name. 271 */ 272 App getByName(String name) { 273 Iterator<App> i = mApps.iterator(); 274 while (i.hasNext()) { 275 App entry = i.next(); 276 if (entry.name.equals(name)) return entry; 277 } 278 Log.e(TAG, "Context not found for name " + name); 279 return null; 280 } 281 282 /** 283 * Get Logging info by ID 284 */ 285 AppScanStats getAppScanStatsById(int id) { 286 App temp = getById(id); 287 if (temp != null) { 288 return temp.appScanStats; 289 } 290 return null; 291 } 292 293 /** 294 * Get Logging info by application name 295 */ 296 AppScanStats getAppScanStatsByName(String name) { 297 return mAppScanStats.get(name); 298 } 299 300 /** 301 * Get the device addresses for all connected devices 302 */ 303 Set<String> getConnectedDevices() { 304 Set<String> addresses = new HashSet<String>(); 305 Iterator<Connection> i = mConnections.iterator(); 306 while (i.hasNext()) { 307 Connection connection = i.next(); 308 addresses.add(connection.address); 309 } 310 return addresses; 311 } 312 313 /** 314 * Get an application context by a connection ID. 315 */ 316 App getByConnId(int connId) { 317 Iterator<Connection> ii = mConnections.iterator(); 318 while (ii.hasNext()) { 319 Connection connection = ii.next(); 320 if (connection.connId == connId){ 321 return getById(connection.appId); 322 } 323 } 324 return null; 325 } 326 327 /** 328 * Returns a connection ID for a given device address. 329 */ 330 Integer connIdByAddress(int id, String address) { 331 App entry = getById(id); 332 if (entry == null) return null; 333 334 Iterator<Connection> i = mConnections.iterator(); 335 while (i.hasNext()) { 336 Connection connection = i.next(); 337 if (connection.address.equalsIgnoreCase(address) && connection.appId == id) 338 return connection.connId; 339 } 340 return null; 341 } 342 343 /** 344 * Returns the device address for a given connection ID. 345 */ 346 String addressByConnId(int connId) { 347 Iterator<Connection> i = mConnections.iterator(); 348 while (i.hasNext()) { 349 Connection connection = i.next(); 350 if (connection.connId == connId) return connection.address; 351 } 352 return null; 353 } 354 355 List<Connection> getConnectionByApp(int appId) { 356 List<Connection> currentConnections = new ArrayList<Connection>(); 357 Iterator<Connection> i = mConnections.iterator(); 358 while (i.hasNext()) { 359 Connection connection = i.next(); 360 if (connection.appId == appId) 361 currentConnections.add(connection); 362 } 363 return currentConnections; 364 } 365 366 /** 367 * Erases all application context entries. 368 */ 369 void clear() { 370 synchronized (mApps) { 371 Iterator<App> i = mApps.iterator(); 372 while (i.hasNext()) { 373 App entry = i.next(); 374 entry.unlinkToDeath(); 375 entry.appScanStats.isRegistered = false; 376 i.remove(); 377 } 378 } 379 380 synchronized (mConnections) { 381 mConnections.clear(); 382 } 383 } 384 385 /** 386 * Returns connect device map with addr and appid 387 */ 388 Map<Integer, String> getConnectedMap(){ 389 Map<Integer, String> connectedmap = new HashMap<Integer, String>(); 390 for(Connection conn: mConnections){ 391 connectedmap.put(conn.appId, conn.address); 392 } 393 return connectedmap; 394 } 395 396 /** 397 * Logs debug information. 398 */ 399 void dump(StringBuilder sb) { 400 sb.append(" Entries: " + mAppScanStats.size() + "\n\n"); 401 402 Iterator<Map.Entry<String, AppScanStats>> it = mAppScanStats.entrySet().iterator(); 403 while (it.hasNext()) { 404 Map.Entry<String, AppScanStats> entry = it.next(); 405 406 String name = entry.getKey(); 407 AppScanStats appScanStats = entry.getValue(); 408 appScanStats.dumpToString(sb); 409 } 410 } 411} 412