Merge "GATT Server refactoring (2/4)"

am: d13954e8c6

Change-Id: I10383d95f79409282d96ae60b294c72754df5641
This commit is contained in:
Jakub Pawlowski
2016-08-03 12:59:24 +00:00
committed by android-build-merger
7 changed files with 126 additions and 155 deletions

View File

@@ -250,9 +250,6 @@ public final class BluetoothGatt implements BluetoothProfile {
if (VDBG) Log.d(TAG, "onCharacteristicRead() - Device=" + address if (VDBG) Log.d(TAG, "onCharacteristicRead() - Device=" + address
+ " handle=" + handle + " Status=" + status); + " handle=" + handle + " Status=" + status);
Log.w(TAG, "onCharacteristicRead() - Device=" + address
+ " handle=" + handle + " Status=" + status);
if (!address.equals(mDevice.getAddress())) { if (!address.equals(mDevice.getAddress())) {
return; return;
} }

View File

@@ -321,10 +321,10 @@ public class BluetoothGattCharacteristic implements Parcelable {
} }
/** /**
* Returns the deisred key size. * Returns the desired key size.
* @hide * @hide
*/ */
/*package*/ int getKeySize() { public int getKeySize() {
return mKeySize; return mKeySize;
} }
@@ -392,6 +392,14 @@ public class BluetoothGattCharacteristic implements Parcelable {
return mInstance; return mInstance;
} }
/**
* Force the instance ID.
* @hide
*/
public void setInstanceId(int instanceId) {
mInstance = instanceId;
}
/** /**
* Returns the properties of this characteristic. * Returns the properties of this characteristic.
* *

View File

@@ -226,6 +226,14 @@ public class BluetoothGattDescriptor implements Parcelable {
return mInstance; return mInstance;
} }
/**
* Force the instance ID.
* @hide
*/
public void setInstanceId(int instanceId) {
mInstance = instanceId;
}
/** /**
* Returns the permissions for this descriptor. * Returns the permissions for this descriptor.
* *

View File

@@ -52,6 +52,7 @@ public final class BluetoothGattServer implements BluetoothProfile {
private Object mServerIfLock = new Object(); private Object mServerIfLock = new Object();
private int mServerIf; private int mServerIf;
private int mTransport; private int mTransport;
private BluetoothGattService mPendingService;
private List<BluetoothGattService> mServices; private List<BluetoothGattService> mServices;
private static final int CALLBACK_REG_TIMEOUT = 10000; private static final int CALLBACK_REG_TIMEOUT = 10000;
@@ -109,17 +110,37 @@ public final class BluetoothGattServer implements BluetoothProfile {
* Service has been added * Service has been added
* @hide * @hide
*/ */
public void onServiceAdded(int status, int srvcType, public void onServiceAdded(int status, BluetoothGattService service) {
int srvcInstId, ParcelUuid srvcId) { if (DBG) Log.d(TAG, "onServiceAdded() - handle=" + service.getInstanceId()
UUID srvcUuid = srvcId.getUuid(); + " uuid=" + service.getUuid() + " status=" + status);
if (DBG) Log.d(TAG, "onServiceAdded() - service=" + srvcUuid
+ "status=" + status);
BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); if (mPendingService == null)
if (service == null) return; return;
BluetoothGattService tmp = mPendingService;
mPendingService = null;
// Rewrite newly assigned handles to existing service.
tmp.setInstanceId(service.getInstanceId());
List<BluetoothGattCharacteristic> temp_chars = tmp.getCharacteristics();
List<BluetoothGattCharacteristic> svc_chars = service.getCharacteristics();
for (int i=0; i<svc_chars.size(); i++) {
BluetoothGattCharacteristic temp_char = temp_chars.get(i);
BluetoothGattCharacteristic svc_char = svc_chars.get(i);
temp_char.setInstanceId(svc_char.getInstanceId());
List<BluetoothGattDescriptor> temp_descs = temp_char.getDescriptors();
List<BluetoothGattDescriptor> svc_descs = svc_char.getDescriptors();
for (int j=0; j<svc_descs.size(); j++) {
temp_descs.get(i).setInstanceId(svc_descs.get(i).getInstanceId());
}
}
mServices.add(tmp);
try { try {
mCallback.onServiceAdded((int)status, service); mCallback.onServiceAdded((int)status, tmp);
} catch (Exception ex) { } catch (Exception ex) {
Log.w(TAG, "Unhandled exception in callback", ex); Log.w(TAG, "Unhandled exception in callback", ex);
} }
@@ -130,19 +151,15 @@ public final class BluetoothGattServer implements BluetoothProfile {
* @hide * @hide
*/ */
public void onCharacteristicReadRequest(String address, int transId, public void onCharacteristicReadRequest(String address, int transId,
int offset, boolean isLong, int srvcType, int srvcInstId, int offset, boolean isLong, int handle) {
ParcelUuid srvcId, int charInstId, ParcelUuid charId) { if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - handle=" + handle);
UUID srvcUuid = srvcId.getUuid();
UUID charUuid = charId.getUuid();
if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - "
+ "service=" + srvcUuid + ", characteristic=" + charUuid);
BluetoothDevice device = mAdapter.getRemoteDevice(address); BluetoothDevice device = mAdapter.getRemoteDevice(address);
BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); BluetoothGattCharacteristic characteristic = getCharacteristicByHandle(handle);
if (service == null) return; if (characteristic == null) {
Log.w(TAG, "onCharacteristicReadRequest() no char for handle " + handle);
BluetoothGattCharacteristic characteristic = service.getCharacteristic(charUuid); return;
if (characteristic == null) return; }
try { try {
mCallback.onCharacteristicReadRequest(device, transId, offset, characteristic); mCallback.onCharacteristicReadRequest(device, transId, offset, characteristic);
@@ -156,25 +173,15 @@ public final class BluetoothGattServer implements BluetoothProfile {
* @hide * @hide
*/ */
public void onDescriptorReadRequest(String address, int transId, public void onDescriptorReadRequest(String address, int transId,
int offset, boolean isLong, int srvcType, int srvcInstId, int offset, boolean isLong, int handle) {
ParcelUuid srvcId, int charInstId, ParcelUuid charId, if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - handle=" + handle);
ParcelUuid descrId) {
UUID srvcUuid = srvcId.getUuid();
UUID charUuid = charId.getUuid();
UUID descrUuid = descrId.getUuid();
if (VDBG) Log.d(TAG, "onCharacteristicReadRequest() - "
+ "service=" + srvcUuid + ", characteristic=" + charUuid
+ "descriptor=" + descrUuid);
BluetoothDevice device = mAdapter.getRemoteDevice(address); BluetoothDevice device = mAdapter.getRemoteDevice(address);
BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); BluetoothGattDescriptor descriptor = getDescriptorByHandle(handle);
if (service == null) return; if (descriptor == null) {
Log.w(TAG, "onDescriptorReadRequest() no desc for handle " + handle);
BluetoothGattCharacteristic characteristic = service.getCharacteristic(charUuid); return;
if (characteristic == null) return; }
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descrUuid);
if (descriptor == null) return;
try { try {
mCallback.onDescriptorReadRequest(device, transId, offset, descriptor); mCallback.onDescriptorReadRequest(device, transId, offset, descriptor);
@@ -189,19 +196,15 @@ public final class BluetoothGattServer implements BluetoothProfile {
*/ */
public void onCharacteristicWriteRequest(String address, int transId, public void onCharacteristicWriteRequest(String address, int transId,
int offset, int length, boolean isPrep, boolean needRsp, int offset, int length, boolean isPrep, boolean needRsp,
int srvcType, int srvcInstId, ParcelUuid srvcId, int handle, byte[] value) {
int charInstId, ParcelUuid charId, byte[] value) { if (VDBG) Log.d(TAG, "onCharacteristicWriteRequest() - handle=" + handle);
UUID srvcUuid = srvcId.getUuid();
UUID charUuid = charId.getUuid();
if (VDBG) Log.d(TAG, "onCharacteristicWriteRequest() - "
+ "service=" + srvcUuid + ", characteristic=" + charUuid);
BluetoothDevice device = mAdapter.getRemoteDevice(address); BluetoothDevice device = mAdapter.getRemoteDevice(address);
BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); BluetoothGattCharacteristic characteristic = getCharacteristicByHandle(handle);
if (service == null) return; if (characteristic == null) {
Log.w(TAG, "onCharacteristicWriteRequest() no char for handle " + handle);
BluetoothGattCharacteristic characteristic = service.getCharacteristic(charUuid); return;
if (characteristic == null) return; }
try { try {
mCallback.onCharacteristicWriteRequest(device, transId, characteristic, mCallback.onCharacteristicWriteRequest(device, transId, characteristic,
@@ -216,28 +219,16 @@ public final class BluetoothGattServer implements BluetoothProfile {
* Remote client descriptor write request. * Remote client descriptor write request.
* @hide * @hide
*/ */
public void onDescriptorWriteRequest(String address, int transId, public void onDescriptorWriteRequest(String address, int transId, int offset,
int offset, int length, boolean isPrep, boolean needRsp, int length, boolean isPrep, boolean needRsp, int handle, byte[] value) {
int srvcType, int srvcInstId, ParcelUuid srvcId, if (VDBG) Log.d(TAG, "onDescriptorWriteRequest() - handle=" + handle);
int charInstId, ParcelUuid charId, ParcelUuid descrId,
byte[] value) {
UUID srvcUuid = srvcId.getUuid();
UUID charUuid = charId.getUuid();
UUID descrUuid = descrId.getUuid();
if (VDBG) Log.d(TAG, "onDescriptorWriteRequest() - "
+ "service=" + srvcUuid + ", characteristic=" + charUuid
+ "descriptor=" + descrUuid);
BluetoothDevice device = mAdapter.getRemoteDevice(address); BluetoothDevice device = mAdapter.getRemoteDevice(address);
BluetoothGattDescriptor descriptor = getDescriptorByHandle(handle);
BluetoothGattService service = getService(srvcUuid, srvcInstId, srvcType); if (descriptor == null) {
if (service == null) return; Log.w(TAG, "onDescriptorWriteRequest() no desc for handle " + handle);
return;
BluetoothGattCharacteristic characteristic = service.getCharacteristic(charUuid); }
if (characteristic == null) return;
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(descrUuid);
if (descriptor == null) return;
try { try {
mCallback.onDescriptorWriteRequest(device, transId, descriptor, mCallback.onDescriptorWriteRequest(device, transId, descriptor,
@@ -317,6 +308,36 @@ public final class BluetoothGattServer implements BluetoothProfile {
mServices = new ArrayList<BluetoothGattService>(); mServices = new ArrayList<BluetoothGattService>();
} }
/**
* Returns a characteristic with given handle.
* @hide
*/
/*package*/ BluetoothGattCharacteristic getCharacteristicByHandle(int handle) {
for(BluetoothGattService svc : mServices) {
for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) {
if (charac.getInstanceId() == handle)
return charac;
}
}
return null;
}
/**
* Returns a descriptor with given handle.
* @hide
*/
/*package*/ BluetoothGattDescriptor getDescriptorByHandle(int handle) {
for(BluetoothGattService svc : mServices) {
for(BluetoothGattCharacteristic charac : svc.getCharacteristics()) {
for(BluetoothGattDescriptor desc : charac.getDescriptors()) {
if (desc.getInstanceId() == handle)
return desc;
}
}
}
return null;
}
/** /**
* Close this GATT server instance. * Close this GATT server instance.
* *
@@ -537,9 +558,7 @@ public final class BluetoothGattServer implements BluetoothProfile {
try { try {
mService.sendNotification(mServerIf, device.getAddress(), mService.sendNotification(mServerIf, device.getAddress(),
service.getType(), service.getInstanceId(), characteristic.getInstanceId(), confirm,
new ParcelUuid(service.getUuid()), characteristic.getInstanceId(),
new ParcelUuid(characteristic.getUuid()), confirm,
characteristic.getValue()); characteristic.getValue());
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG,"",e); Log.e(TAG,"",e);
@@ -568,39 +587,10 @@ public final class BluetoothGattServer implements BluetoothProfile {
if (DBG) Log.d(TAG, "addService() - service: " + service.getUuid()); if (DBG) Log.d(TAG, "addService() - service: " + service.getUuid());
if (mService == null || mServerIf == 0) return false; if (mService == null || mServerIf == 0) return false;
mServices.add(service); mPendingService = service;
try { try {
mService.beginServiceDeclaration(mServerIf, service.getType(), mService.addService(mServerIf, service);
service.getInstanceId(), service.getHandles(),
new ParcelUuid(service.getUuid()), service.isAdvertisePreferred());
List<BluetoothGattService> includedServices = service.getIncludedServices();
for (BluetoothGattService includedService : includedServices) {
mService.addIncludedService(mServerIf,
includedService.getType(),
includedService.getInstanceId(),
new ParcelUuid(includedService.getUuid()));
}
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
for (BluetoothGattCharacteristic characteristic : characteristics) {
int permission = ((characteristic.getKeySize() - 7) << 12)
+ characteristic.getPermissions();
mService.addCharacteristic(mServerIf,
new ParcelUuid(characteristic.getUuid()),
characteristic.getProperties(), permission);
List<BluetoothGattDescriptor> descriptors = characteristic.getDescriptors();
for (BluetoothGattDescriptor descriptor: descriptors) {
permission = ((characteristic.getKeySize() - 7) << 12)
+ descriptor.getPermissions();
mService.addDescriptor(mServerIf,
new ParcelUuid(descriptor.getUuid()), permission);
}
}
mService.endServiceDeclaration(mServerIf);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG,"",e); Log.e(TAG,"",e);
return false; return false;
@@ -626,8 +616,7 @@ public final class BluetoothGattServer implements BluetoothProfile {
if (intService == null) return false; if (intService == null) return false;
try { try {
mService.removeService(mServerIf, service.getType(), mService.removeService(mServerIf, service.getInstanceId());
service.getInstanceId(), new ParcelUuid(service.getUuid()));
mServices.remove(intService); mServices.remove(intService);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG,"",e); Log.e(TAG,"",e);

View File

@@ -250,7 +250,6 @@ public class BluetoothGattService implements Parcelable {
/** /**
* Force the instance ID. * Force the instance ID.
* This is needed for conformance testing only.
* @hide * @hide
*/ */
public void setInstanceId(int instanceId) { public void setInstanceId(int instanceId) {

View File

@@ -17,6 +17,7 @@
package android.bluetooth; package android.bluetooth;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.le.AdvertiseSettings; import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.AdvertiseData; import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.ScanFilter; import android.bluetooth.le.ScanFilter;
@@ -66,26 +67,14 @@ interface IBluetoothGatt {
void registerServer(in ParcelUuid appId, in IBluetoothGattServerCallback callback); void registerServer(in ParcelUuid appId, in IBluetoothGattServerCallback callback);
void unregisterServer(in int serverIf); void unregisterServer(in int serverIf);
void serverConnect(in int servertIf, in String address, in boolean isDirect, in int transport); void serverConnect(in int serverIf, in String address, in boolean isDirect, in int transport);
void serverDisconnect(in int serverIf, in String address); void serverDisconnect(in int serverIf, in String address);
void beginServiceDeclaration(in int serverIf, in int srvcType, void addService(in int serverIf, in BluetoothGattService service);
in int srvcInstanceId, in int minHandles, void removeService(in int serverIf, in int handle);
in ParcelUuid srvcId, boolean advertisePreferred);
void addIncludedService(in int serverIf, in int srvcType,
in int srvcInstanceId, in ParcelUuid srvcId);
void addCharacteristic(in int serverIf, in ParcelUuid charId,
in int properties, in int permissions);
void addDescriptor(in int serverIf, in ParcelUuid descId,
in int permissions);
void endServiceDeclaration(in int serverIf);
void removeService(in int serverIf, in int srvcType,
in int srvcInstanceId, in ParcelUuid srvcId);
void clearServices(in int serverIf); void clearServices(in int serverIf);
void sendResponse(in int serverIf, in String address, in int requestId, void sendResponse(in int serverIf, in String address, in int requestId,
in int status, in int offset, in byte[] value); in int status, in int offset, in byte[] value);
void sendNotification(in int serverIf, in String address, in int srvcType, void sendNotification(in int serverIf, in String address, in int handle,
in int srvcInstanceId, in ParcelUuid srvcId,
in int charInstanceId, in ParcelUuid charId,
in boolean confirm, in byte[] value); in boolean confirm, in byte[] value);
void disconnectAll(); void disconnectAll();
void unregAll(); void unregAll();

View File

@@ -15,8 +15,7 @@
*/ */
package android.bluetooth; package android.bluetooth;
import android.os.ParcelUuid; import android.bluetooth.BluetoothGattService;
/** /**
* Callback definitions for interacting with BLE / GATT * Callback definitions for interacting with BLE / GATT
@@ -27,36 +26,18 @@ oneway interface IBluetoothGattServerCallback {
void onScanResult(in String address, in int rssi, in byte[] advData); void onScanResult(in String address, in int rssi, in byte[] advData);
void onServerConnectionState(in int status, in int serverIf, void onServerConnectionState(in int status, in int serverIf,
in boolean connected, in String address); in boolean connected, in String address);
void onServiceAdded(in int status, in int srvcType, void onServiceAdded(in int status, in BluetoothGattService service);
in int srvcInstId, in ParcelUuid srvcId); void onCharacteristicReadRequest(in String address, in int transId, in int offset,
void onCharacteristicReadRequest(in String address, in int transId, in boolean isLong, in int handle);
in int offset, in boolean isLong,
in int srvcType,
in int srvcInstId, in ParcelUuid srvcId,
in int charInstId, in ParcelUuid charId);
void onDescriptorReadRequest(in String address, in int transId, void onDescriptorReadRequest(in String address, in int transId,
in int offset, in boolean isLong, in int offset, in boolean isLong,
in int srvcType, in int handle);
in int srvcInstId, in ParcelUuid srvcId, void onCharacteristicWriteRequest(in String address, in int transId, in int offset,
in int charInstId, in ParcelUuid charId, in int length, in boolean isPrep, in boolean needRsp,
in ParcelUuid descrId); in int handle, in byte[] value);
void onCharacteristicWriteRequest(in String address, in int transId, void onDescriptorWriteRequest(in String address, in int transId, in int offset,
in int offset, in int length, in int length, in boolean isPrep, in boolean needRsp,
in boolean isPrep, in int handle, in byte[] value);
in boolean needRsp,
in int srvcType,
in int srvcInstId, in ParcelUuid srvcId,
in int charInstId, in ParcelUuid charId,
in byte[] value);
void onDescriptorWriteRequest(in String address, in int transId,
in int offset, in int length,
in boolean isPrep,
in boolean needRsp,
in int srvcType,
in int srvcInstId, in ParcelUuid srvcId,
in int charInstId, in ParcelUuid charId,
in ParcelUuid descrId,
in byte[] value);
void onExecuteWrite(in String address, in int transId, in boolean execWrite); void onExecuteWrite(in String address, in int transId, in boolean execWrite);
void onNotificationSent(in String address, in int status); void onNotificationSent(in String address, in int status);
void onMtuChanged(in String address, in int mtu); void onMtuChanged(in String address, in int mtu);