From 455dc8f535a719af6a65a7512d90f9db878f5a58 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Tue, 19 Jan 2016 17:00:16 -0800 Subject: [PATCH] service: Expose Connect and Disconnect through IBluetothLowEnergy Change-Id: Idcfd3fac263e61bc1bb2be97017554d552511765 --- service/client/main.cpp | 5 +++ .../bluetooth/binder/IBluetoothLowEnergy.cpp | 47 ++++++++++++++++++++++ .../common/bluetooth/binder/IBluetoothLowEnergy.h | 7 ++++ .../binder/IBluetoothLowEnergyCallback.cpp | 27 +++++++++++++ .../bluetooth/binder/IBluetoothLowEnergyCallback.h | 4 ++ service/doc/IBluetoothLowEnergy.txt | 12 ++++++ service/doc/IBluetoothLowEnergyCallback.txt | 5 +++ .../binder/bluetooth_low_energy_binder_server.cpp | 41 +++++++++++++++++++ .../binder/bluetooth_low_energy_binder_server.h | 2 + 9 files changed, 150 insertions(+) diff --git a/service/client/main.cpp b/service/client/main.cpp index 294275b75..4ba1b20f8 100644 --- a/service/client/main.cpp +++ b/service/client/main.cpp @@ -158,6 +158,11 @@ class CLIBluetoothLowEnergyCallback ble_registering = false; } + void OnConnectionState(int status, int client_id, const char* address, + bool connected) override { + //TODO(jpawlowski): implement + } + void OnScanResult(const bluetooth::ScanResult& scan_result) override { if (showing_prompt.load()) cout << endl; diff --git a/service/common/bluetooth/binder/IBluetoothLowEnergy.cpp b/service/common/bluetooth/binder/IBluetoothLowEnergy.cpp index b6f3c21cf..48d99560a 100644 --- a/service/common/bluetooth/binder/IBluetoothLowEnergy.cpp +++ b/service/common/bluetooth/binder/IBluetoothLowEnergy.cpp @@ -65,6 +65,25 @@ status_t BnBluetoothLowEnergy::onTransact( UnregisterAll(); return android::NO_ERROR; } + case CONNECT_TRANSACTION: { + int client_id = data.readInt32(); + const char* address = data.readCString(); + bool is_direct = data.readBool(); + + bool result = Connect(client_id, address, is_direct); + reply->writeInt32(result); + + return android::NO_ERROR; + } + case DISCONNECT_TRANSACTION: { + int client_id = data.readInt32(); + const char* address = data.readCString(); + + bool result = Disconnect(client_id, address); + reply->writeInt32(result); + + return android::NO_ERROR; + } case START_SCAN_TRANSACTION: { int client_id = data.readInt32(); auto settings = CreateScanSettingsFromParcel(data); @@ -162,6 +181,34 @@ void BpBluetoothLowEnergy::UnregisterAll() { data, &reply); } +bool BpBluetoothLowEnergy::Connect(int client_id, const char* address, + bool is_direct) { + Parcel data, reply; + + data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor()); + data.writeInt32(client_id); + data.writeCString(address); + data.writeBool(is_direct); + + remote()->transact(IBluetoothLowEnergy::CONNECT_TRANSACTION, + data, &reply); + + return reply.readInt32(); +} + +bool BpBluetoothLowEnergy::Disconnect(int client_id, const char* address) { + Parcel data, reply; + + data.writeInterfaceToken(IBluetoothLowEnergy::getInterfaceDescriptor()); + data.writeInt32(client_id); + data.writeCString(address); + + remote()->transact(IBluetoothLowEnergy::DISCONNECT_TRANSACTION, + data, &reply); + + return reply.readInt32(); +} + bool BpBluetoothLowEnergy::StartScan( int client_id, const bluetooth::ScanSettings& settings, diff --git a/service/common/bluetooth/binder/IBluetoothLowEnergy.h b/service/common/bluetooth/binder/IBluetoothLowEnergy.h index 1560208d4..78d7213c2 100644 --- a/service/common/bluetooth/binder/IBluetoothLowEnergy.h +++ b/service/common/bluetooth/binder/IBluetoothLowEnergy.h @@ -72,6 +72,9 @@ class IBluetoothLowEnergy : public android::IInterface { virtual void UnregisterClient(int client_if) = 0; virtual void UnregisterAll() = 0; + virtual bool Connect(int client_id, const char* address, bool is_direct) = 0; + virtual bool Disconnect(int client_id, const char* address) = 0; + virtual bool StartScan( int client_id, const bluetooth::ScanSettings& settings, @@ -119,6 +122,10 @@ class BpBluetoothLowEnergy : public android::BpInterface { const android::sp& callback) override; void UnregisterClient(int client_if) override; void UnregisterAll() override; + + bool Connect(int client_id, const char* address, bool is_direct) override; + bool Disconnect(int client_id, const char* address) override; + bool StartScan( int client_id, const bluetooth::ScanSettings& settings, diff --git a/service/common/bluetooth/binder/IBluetoothLowEnergyCallback.cpp b/service/common/bluetooth/binder/IBluetoothLowEnergyCallback.cpp index 98c92efd0..e2b729299 100644 --- a/service/common/bluetooth/binder/IBluetoothLowEnergyCallback.cpp +++ b/service/common/bluetooth/binder/IBluetoothLowEnergyCallback.cpp @@ -55,6 +55,15 @@ status_t BnBluetoothLowEnergyCallback::onTransact( OnClientRegistered(status, client_if); return android::NO_ERROR; } + case ON_CONNECTION_STATE_TRANSACTION: { + int status = data.readInt32(); + int client_id = data.readInt32(); + const char* address = data.readCString(); + bool connected = data.readBool(); + + OnConnectionState(status, client_id, address, connected); + return android::NO_ERROR; + } case ON_SCAN_RESULT_TRANSACTION: { auto scan_result = CreateScanResultFromParcel(data); CHECK(scan_result.get()); @@ -97,6 +106,24 @@ void BpBluetoothLowEnergyCallback::OnClientRegistered( IBinder::FLAG_ONEWAY); } +void BpBluetoothLowEnergyCallback::OnConnectionState( + int status, int client_id, const char* address, bool connected) { + + Parcel data; + + data.writeInterfaceToken( + IBluetoothLowEnergyCallback::getInterfaceDescriptor()); + data.writeInt32(status); + data.writeInt32(client_id); + data.writeCString(address); + data.writeBool(connected); + + remote()->transact( + IBluetoothLowEnergyCallback::ON_CONNECTION_STATE_TRANSACTION, + data, NULL, + IBinder::FLAG_ONEWAY); +} + void BpBluetoothLowEnergyCallback::OnScanResult( const bluetooth::ScanResult& scan_result) { Parcel data, reply; diff --git a/service/common/bluetooth/binder/IBluetoothLowEnergyCallback.h b/service/common/bluetooth/binder/IBluetoothLowEnergyCallback.h index 6a88243a2..908e78b2e 100644 --- a/service/common/bluetooth/binder/IBluetoothLowEnergyCallback.h +++ b/service/common/bluetooth/binder/IBluetoothLowEnergyCallback.h @@ -55,6 +55,8 @@ namespace binder { }; virtual void OnClientRegistered(int status, int client_if) = 0; + virtual void OnConnectionState(int status, int client_id, const char* address, + bool connected) = 0; virtual void OnScanResult(const bluetooth::ScanResult& scan_result) = 0; virtual void OnMultiAdvertiseCallback( int status, bool is_start, @@ -93,6 +95,8 @@ class BpBluetoothLowEnergyCallback // IBluetoothLowEnergyCallback overrides: void OnClientRegistered(int status, int client_if) override; + void OnConnectionState(int status, int client_id, const char* address, + bool connected) override; void OnScanResult(const bluetooth::ScanResult& scan_result) override; void OnMultiAdvertiseCallback( int status, bool is_start, diff --git a/service/doc/IBluetoothLowEnergy.txt b/service/doc/IBluetoothLowEnergy.txt index 59cd9495c..c05620e41 100644 --- a/service/doc/IBluetoothLowEnergy.txt +++ b/service/doc/IBluetoothLowEnergy.txt @@ -37,6 +37,18 @@ interface IBluetoothLowEnergy { */ void unregisterAll(); + /* Initiates a BLE connection do device with address |address|. If + * |is_direct| is set, use direct connect procedure. Return true on success, + * false otherwise. + */ + boolean Connect(in int client_id, in const char* address, + in boolean is_direct); + + /* Disconnect from previously connected BLE device with address |address|. + * Return true on success, false otherwise. + */ + boolean Disconnect(in int client_id, in const char* address); + /** * Initiates a BLE device scan for the scan client with ID |client_id|, using * the parameters defined in |settings|. Scan results that are reported to the diff --git a/service/doc/IBluetoothLowEnergyCallback.txt b/service/doc/IBluetoothLowEnergyCallback.txt index 57cdf4293..4fa60bfac 100644 --- a/service/doc/IBluetoothLowEnergyCallback.txt +++ b/service/doc/IBluetoothLowEnergyCallback.txt @@ -28,6 +28,11 @@ oneway interface IBluetoothLowEnergyCallback { */ void onClientRegistered(in int status, in int client_if); + /* Called asynchronously to notify the delegate of connection state change. + */ + void OnConnectionState(in int status, in int client_id, in const char* address, + in bool connected); + /** * Called to report BLE device scan results once a scan session is started for * this client using IBluetoothLowEnergy.startScan. |scan_result| contains all diff --git a/service/ipc/binder/bluetooth_low_energy_binder_server.cpp b/service/ipc/binder/bluetooth_low_energy_binder_server.cpp index 04c845059..f49d909d9 100644 --- a/service/ipc/binder/bluetooth_low_energy_binder_server.cpp +++ b/service/ipc/binder/bluetooth_low_energy_binder_server.cpp @@ -54,6 +54,38 @@ void BluetoothLowEnergyBinderServer::UnregisterAll() { UnregisterAllBase(); } +bool BluetoothLowEnergyBinderServer::Connect(int client_id, + const char* address, + bool is_direct) { + VLOG(2) << __func__ << " client_id: " << client_id + << " address: " << address + << " is_direct: " << is_direct; + std::lock_guard lock(*maps_lock()); + + auto client = GetLEClient(client_id); + if (!client) { + LOG(ERROR) << "Unknown client_id: " << client_id; + return false; + } + + return client->Connect(std::string(address), is_direct); +} + +bool BluetoothLowEnergyBinderServer::Disconnect(int client_id, + const char* address) { + VLOG(2) << __func__ << " client_id: " << client_id + << " address: " << address; + std::lock_guard lock(*maps_lock()); + + auto client = GetLEClient(client_id); + if (!client) { + LOG(ERROR) << "Unknown client_id: " << client_id; + return false; + } + + return client->Disconnect(std::string(address)); +} + bool BluetoothLowEnergyBinderServer::StartScan( int client_id, const bluetooth::ScanSettings& settings, @@ -172,6 +204,15 @@ void BluetoothLowEnergyBinderServer::OnConnectionState( bluetooth::LowEnergyClient* client, int status, const char* address, bool connected) { VLOG(2) << __func__ << " address: " << address << " connected: " << connected; + + int client_id = client->GetInstanceId(); + auto cb = GetLECallback(client->GetInstanceId()); + if (!cb.get()) { + VLOG(2) << "Client was unregistered - client_id: " << client_id; + return; + } + + cb->OnConnectionState(status, client_id, address, connected); } void BluetoothLowEnergyBinderServer::OnScanResult( diff --git a/service/ipc/binder/bluetooth_low_energy_binder_server.h b/service/ipc/binder/bluetooth_low_energy_binder_server.h index ed4a476dd..48a4f7197 100644 --- a/service/ipc/binder/bluetooth_low_energy_binder_server.h +++ b/service/ipc/binder/bluetooth_low_energy_binder_server.h @@ -47,6 +47,8 @@ class BluetoothLowEnergyBinderServer const android::sp& callback) override; void UnregisterClient(int client_id) override; void UnregisterAll() override; + bool Connect(int client_id, const char* address, bool is_direct) override; + bool Disconnect(int client_id, const char* address) override; bool StartScan( int client_id, const bluetooth::ScanSettings& settings, -- 2.11.0