OSDN Git Service

service: Expose Connect and Disconnect through IBluetothLowEnergy
authorJakub Pawlowski <jpawlowski@google.com>
Wed, 20 Jan 2016 01:00:16 +0000 (17:00 -0800)
committerJakub Pawlowski <jpawlowski@google.com>
Wed, 20 Jan 2016 21:55:10 +0000 (21:55 +0000)
Change-Id: Idcfd3fac263e61bc1bb2be97017554d552511765

service/client/main.cpp
service/common/bluetooth/binder/IBluetoothLowEnergy.cpp
service/common/bluetooth/binder/IBluetoothLowEnergy.h
service/common/bluetooth/binder/IBluetoothLowEnergyCallback.cpp
service/common/bluetooth/binder/IBluetoothLowEnergyCallback.h
service/doc/IBluetoothLowEnergy.txt
service/doc/IBluetoothLowEnergyCallback.txt
service/ipc/binder/bluetooth_low_energy_binder_server.cpp
service/ipc/binder/bluetooth_low_energy_binder_server.h

index 294275b..4ba1b20 100644 (file)
@@ -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;
index b6f3c21..48d9956 100644 (file)
@@ -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,
index 1560208..78d7213 100644 (file)
@@ -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<IBluetoothLowEnergy> {
       const android::sp<IBluetoothLowEnergyCallback>& 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,
index 98c92ef..e2b7292 100644 (file)
@@ -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;
index 6a88243..908e78b 100644 (file)
@@ -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,
index 59cd949..c05620e 100644 (file)
@@ -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
index 57cdf42..4fa60bf 100644 (file)
@@ -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
index 04c8450..f49d909 100644 (file)
@@ -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<std::mutex> 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<std::mutex> 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(
index ed4a476..48a4f71 100644 (file)
@@ -47,6 +47,8 @@ class BluetoothLowEnergyBinderServer
       const android::sp<IBluetoothLowEnergyCallback>& 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,