Commit 641928be authored by Ivan Podogov's avatar Ivan Podogov Committed by Gerrit Code Review
Browse files

Merge "HID Device role API fixes"

parents ae635ea9 2769ac31
...@@ -398,7 +398,7 @@ ...@@ -398,7 +398,7 @@
android:name = ".hid.HidDevService" android:name = ".hid.HidDevService"
android:enabled="@bool/profile_supported_hidd"> android:enabled="@bool/profile_supported_hidd">
<intent-filter> <intent-filter>
<action android:name="android.bluetooth.IBluetoothHidDevice" /> <action android:name="android.bluetooth.IBluetoothInputHost" />
</intent-filter> </intent-filter>
</service> </service>
</application> </application>
......
...@@ -36,6 +36,8 @@ import com.android.bluetooth.btservice.ProfileService; ...@@ -36,6 +36,8 @@ import com.android.bluetooth.btservice.ProfileService;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
/** @hide */ /** @hide */
...@@ -80,6 +82,12 @@ public class HidDevService extends ProfileService { ...@@ -80,6 +82,12 @@ public class HidDevService extends ProfileService {
msg.obj != null ? getDevice((byte[])msg.obj) : null; msg.obj != null ? getDevice((byte[])msg.obj) : null;
boolean success = (msg.arg1 != 0); boolean success = (msg.arg1 != 0);
if (success) {
mHidDevice = device;
} else {
mHidDevice = null;
}
try { try {
if (mCallback != null) if (mCallback != null)
mCallback.onAppStatusChanged(device, mAppConfig, success); mCallback.onAppStatusChanged(device, mAppConfig, success);
...@@ -129,14 +137,18 @@ public class HidDevService extends ProfileService { ...@@ -129,14 +137,18 @@ public class HidDevService extends ProfileService {
int halState = msg.arg1; int halState = msg.arg1;
int state = convertHalState(halState); int state = convertHalState(halState);
if (state != BluetoothInputHost.STATE_DISCONNECTED) {
mHidDevice = device;
}
broadcastConnectionState(device, state);
try { try {
if (mCallback != null) if (mCallback != null)
mCallback.onConnectionStateChanged(device, state); mCallback.onConnectionStateChanged(device, state);
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
} }
broadcastConnectionState(device, state);
break; break;
} }
...@@ -146,8 +158,7 @@ public class HidDevService extends ProfileService { ...@@ -146,8 +158,7 @@ public class HidDevService extends ProfileService {
int bufferSize = msg.obj == null ? 0 : ((Integer)msg.obj).intValue(); int bufferSize = msg.obj == null ? 0 : ((Integer)msg.obj).intValue();
try { try {
if (mCallback != null) if (mCallback != null) mCallback.onGetReport(mHidDevice, type, id, bufferSize);
mCallback.onGetReport(type, id, bufferSize);
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -159,8 +170,7 @@ public class HidDevService extends ProfileService { ...@@ -159,8 +170,7 @@ public class HidDevService extends ProfileService {
byte[] data = ((ByteBuffer)msg.obj).array(); byte[] data = ((ByteBuffer)msg.obj).array();
try { try {
if (mCallback != null) if (mCallback != null) mCallback.onSetReport(mHidDevice, reportType, reportId, data);
mCallback.onSetReport(reportType, reportId, data);
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -171,8 +181,7 @@ public class HidDevService extends ProfileService { ...@@ -171,8 +181,7 @@ public class HidDevService extends ProfileService {
byte protocol = (byte)msg.arg1; byte protocol = (byte)msg.arg1;
try { try {
if (mCallback != null) if (mCallback != null) mCallback.onSetProtocol(mHidDevice, protocol);
mCallback.onSetProtocol(protocol);
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -183,8 +192,7 @@ public class HidDevService extends ProfileService { ...@@ -183,8 +192,7 @@ public class HidDevService extends ProfileService {
byte[] data = ((ByteBuffer)msg.obj).array(); byte[] data = ((ByteBuffer)msg.obj).array();
try { try {
if (mCallback != null) if (mCallback != null) mCallback.onIntrData(mHidDevice, reportId, data);
mCallback.onIntrData(reportId, data);
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -192,11 +200,11 @@ public class HidDevService extends ProfileService { ...@@ -192,11 +200,11 @@ public class HidDevService extends ProfileService {
case MESSAGE_VC_UNPLUG: case MESSAGE_VC_UNPLUG:
try { try {
if (mCallback != null) if (mCallback != null) mCallback.onVirtualCableUnplug(mHidDevice);
mCallback.onVirtualCableUnplug();
} catch (RemoteException e) { } catch (RemoteException e) {
e.printStackTrace(); e.printStackTrace();
} }
mHidDevice = null;
break; break;
} }
} }
...@@ -287,81 +295,107 @@ public class HidDevService extends ProfileService { ...@@ -287,81 +295,107 @@ public class HidDevService extends ProfileService {
} }
@Override @Override
public boolean sendReport(int id, byte[] data) { public boolean sendReport(BluetoothDevice device, int id, byte[] data) {
if (DBG) if (DBG) Log.v(TAG, "sendReport(): device=" + device + " id=" + id);
Log.v(TAG, "sendReport(): id=" + id);
HidDevService service = getService(); HidDevService service = getService();
if (service == null) { if (service == null) {
return false; return false;
} }
return service.sendReport(id, data); return service.sendReport(device, id, data);
} }
@Override @Override
public boolean replyReport(byte type, byte id, byte[] data) { public boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data) {
if (DBG) if (DBG) Log.v(TAG, "replyReport(): device=" + device + " type=" + type + " id=" + id);
Log.v(TAG, "replyReport(): type=" + type + " id=" + id);
HidDevService service = getService(); HidDevService service = getService();
if (service == null) { if (service == null) {
return false; return false;
} }
return service.replyReport(type, id, data); return service.replyReport(device, type, id, data);
} }
@Override @Override
public boolean unplug() { public boolean unplug(BluetoothDevice device) {
if (DBG) if (DBG) Log.v(TAG, "unplug(): device=" + device);
Log.v(TAG, "unplug()");
HidDevService service = getService(); HidDevService service = getService();
if (service == null) { if (service == null) {
return false; return false;
} }
return service.unplug(); return service.unplug(device);
} }
@Override @Override
public boolean connect() { public boolean connect(BluetoothDevice device) {
if (DBG) if (DBG) Log.v(TAG, "connect(): device=" + device);
Log.v(TAG, "connect()");
HidDevService service = getService(); HidDevService service = getService();
if (service == null) { if (service == null) {
return false; return false;
} }
return service.connect(); return service.connect(device);
} }
@Override @Override
public boolean disconnect() { public boolean disconnect(BluetoothDevice device) {
if (DBG) if (DBG) Log.v(TAG, "disconnect(): device=" + device);
Log.v(TAG, "disconnect()");
HidDevService service = getService(); HidDevService service = getService();
if (service == null) { if (service == null) {
return false; return false;
} }
return service.disconnect(); return service.disconnect(device);
} }
@Override @Override
public boolean reportError(byte error) { public boolean reportError(BluetoothDevice device, byte error) {
if (DBG) if (DBG) Log.v(TAG, "reportError(): device=" + device + " error=" + error);
Log.v(TAG, "reportError(), error = " + error);
HidDevService service = getService(); HidDevService service = getService();
if (service == null) { if (service == null) {
return false; return false;
} }
return service.reportError(device, error);
}
@Override
public int getConnectionState(BluetoothDevice device) {
if (DBG) Log.v(TAG, "getConnectionState(): device=" + device);
HidDevService service = getService();
if (service == null) {
return BluetoothInputHost.STATE_DISCONNECTED;
}
return service.getConnectionState(device);
}
@Override
public List<BluetoothDevice> getConnectedDevices() {
if (DBG) Log.v(TAG, "getConnectedDevices()");
return service.reportError(error); return getDevicesMatchingConnectionStates(new int[] {BluetoothProfile.STATE_CONNECTED});
}
@Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (DBG)
Log.v(TAG, "getDevicesMatchingConnectionStates(): states=" + Arrays.toString(states));
HidDevService service = getService();
if (service == null) {
return new ArrayList<BluetoothDevice>(0);
}
return service.getDevicesMatchingConnectionStates(states);
} }
} }
...@@ -370,6 +404,14 @@ public class HidDevService extends ProfileService { ...@@ -370,6 +404,14 @@ public class HidDevService extends ProfileService {
return new BluetoothHidDeviceBinder(this); return new BluetoothHidDeviceBinder(this);
} }
private boolean checkDevice(BluetoothDevice device) {
if (mHidDevice == null || !mHidDevice.equals(device)) {
Log.w(TAG, "Unknown device: " + device);
return false;
}
return true;
}
synchronized boolean registerApp(BluetoothHidDeviceAppConfiguration config, synchronized boolean registerApp(BluetoothHidDeviceAppConfiguration config,
BluetoothHidDeviceAppSdpSettings sdp, BluetoothHidDeviceAppSdpSettings sdp,
BluetoothHidDeviceAppQosSettings inQos, BluetoothHidDeviceAppQosSettings inQos,
...@@ -403,46 +445,60 @@ public class HidDevService extends ProfileService { ...@@ -403,46 +445,60 @@ public class HidDevService extends ProfileService {
return unregisterAppNative(); return unregisterAppNative();
} }
synchronized boolean sendReport(int id, byte[] data) { synchronized boolean sendReport(BluetoothDevice device, int id, byte[] data) {
if (DBG) if (DBG) Log.v(TAG, "sendReport(): device=" + device + " id=" + id);
Log.v(TAG, "sendReport(): id=" + id);
return sendReportNative(id, data); if (!checkDevice(device)) {
return false;
}
return sendReportNative(id, data);
} }
synchronized boolean replyReport(byte type, byte id, byte[] data) { synchronized boolean replyReport(BluetoothDevice device, byte type, byte id, byte[] data) {
if (DBG) if (DBG) Log.v(TAG, "replyReport(): device=" + device + " type=" + type + " id=" + id);
Log.v(TAG, "replyReport(): type=" + type + " id=" + id);
if (!checkDevice(device)) {
return false;
}
return replyReportNative(type, id, data); return replyReportNative(type, id, data);
} }
synchronized boolean unplug() { synchronized boolean unplug(BluetoothDevice device) {
if (DBG) if (DBG) Log.v(TAG, "unplug(): device=" + device);
Log.v(TAG, "unplug()");
if (!checkDevice(device)) {
return false;
}
return unplugNative(); return unplugNative();
} }
synchronized boolean connect() { synchronized boolean connect(BluetoothDevice device) {
if (DBG) if (DBG) Log.v(TAG, "connect(): device=" + device);
Log.v(TAG, "connect()");
return connectNative(); return connectNative();
} }
synchronized boolean disconnect() { synchronized boolean disconnect(BluetoothDevice device) {
if (DBG) if (DBG) Log.v(TAG, "disconnect(): device=" + device);
Log.v(TAG, "disconnect()");
return disconnectNative(); if (!checkDevice(device)) {
return false;
}
return disconnectNative();
} }
synchronized boolean reportError(byte error) { synchronized boolean reportError(BluetoothDevice device, byte error) {
if (DBG) if (DBG) Log.v(TAG, "reportError(): device=" + device + " error=" + error);
Log.v(TAG, "reportError(): error = " + error);
return reportErrorNative(error); if (!checkDevice(device)) {
return false;
}
return reportErrorNative(error);
} }
@Override @Override
...@@ -477,6 +533,28 @@ public class HidDevService extends ProfileService { ...@@ -477,6 +533,28 @@ public class HidDevService extends ProfileService {
return true; return true;
} }
int getConnectionState(BluetoothDevice device) {
if (mHidDevice != null && mHidDevice.equals(device)) {
return mHidDeviceState;
}
return BluetoothInputHost.STATE_DISCONNECTED;
}
List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
List<BluetoothDevice> inputDevices = new ArrayList<BluetoothDevice>();
if (mHidDevice != null) {
for (int state : states) {
if (state == mHidDeviceState) {
inputDevices.add(mHidDevice);
break;
}
}
}
return inputDevices;
}
private synchronized void onApplicationStateChanged(byte[] address, private synchronized void onApplicationStateChanged(byte[] address,
boolean registered) { boolean registered) {
if (DBG) if (DBG)
...@@ -560,9 +638,9 @@ public class HidDevService extends ProfileService { ...@@ -560,9 +638,9 @@ public class HidDevService extends ProfileService {
Log.v(TAG, "broadcastConnectionState(): device=" + device.getAddress() + Log.v(TAG, "broadcastConnectionState(): device=" + device.getAddress() +
" newState=" + newState); " newState=" + newState);
if (mHidDevice != null && mHidDevice != device) { if (mHidDevice != null && !mHidDevice.equals(device)) {
Log.w(TAG, "Connection state changed for unknown device, ignoring"); Log.w(TAG, "Connection state changed for unknown device, ignoring");
return; return;
} }
int prevState = mHidDeviceState; int prevState = mHidDeviceState;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment