Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1c8e09ea authored by Arman Uguray's avatar Arman Uguray
Browse files

service: Introduce IBluetoothCallback.h

This CL introduces the native bindings for the IBluetoothCallback
Binder interface and implements the IBluetooth registerCallback and
unregisterCallback methods.

Bug: 23328384
Change-Id: I6fbf72cff7e3f037a412be8678ea97f3a7ada0c8
parent d6fb7761
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -34,8 +34,9 @@ btserviceCommonSrc := \
	uuid.cpp

btserviceBinderSrc := \
	ipc/binder/IBluetooth.cpp \
	ipc/binder/bluetooth_binder_server.cpp \
	ipc/binder/IBluetooth.cpp \
	ipc/binder/IBluetoothCallback.cpp \
	ipc/binder/ipc_handler_binder.cpp

btserviceCommonIncludes := $(LOCAL_PATH)/../
+28 −0
Original line number Diff line number Diff line
@@ -111,6 +111,16 @@ android::status_t BnBluetooth::onTransact(
      reply->writeCString(name.c_str());
      return android::NO_ERROR;
    }
    case REGISTER_CALLBACK_TRANSACTION: {
      sp<IBinder> callback = data.readStrongBinder();
      RegisterCallback(interface_cast<IBluetoothCallback>(callback));
      return android::NO_ERROR;
    }
    case UNREGISTER_CALLBACK_TRANSACTION: {
      sp<IBinder> callback = data.readStrongBinder();
      UnregisterCallback(interface_cast<IBluetoothCallback>(callback));
      return android::NO_ERROR;
    }
    default:
      return BBinder::onTransact(code, data, reply, flags);
  }
@@ -203,6 +213,24 @@ std::string BpBluetooth::GetName() {
  return reply.readCString();
}

void BpBluetooth::RegisterCallback(const sp<IBluetoothCallback>& callback) {
  Parcel data, reply;

  data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
  data.writeStrongBinder(IInterface::asBinder(callback.get()));

  remote()->transact(IBluetooth::REGISTER_CALLBACK_TRANSACTION, data, &reply);
}

void BpBluetooth::UnregisterCallback(const sp<IBluetoothCallback>& callback) {
  Parcel data, reply;

  data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
  data.writeStrongBinder(IInterface::asBinder(callback.get()));

  remote()->transact(IBluetooth::UNREGISTER_CALLBACK_TRANSACTION, data, &reply);
}

IMPLEMENT_META_INTERFACE(Bluetooth, IBluetooth::kBluetoothServiceName);

}  // namespace binder
+10 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <binder/IBinder.h>
#include <binder/IInterface.h>

#include "service/ipc/binder/IBluetoothCallback.h"
#include "service/uuid.h"

namespace ipc {
@@ -136,6 +137,11 @@ class IBluetooth : public android::IInterface {
  virtual bool SetName(const std::string& name) = 0;
  virtual std::string GetName() = 0;

  virtual void RegisterCallback(
      const android::sp<IBluetoothCallback>& callback) = 0;
  virtual void UnregisterCallback(
      const android::sp<IBluetoothCallback>& callback) = 0;

  // TODO(armansito): Complete the API definition.
 private:
  DISALLOW_COPY_AND_ASSIGN(IBluetooth);
@@ -174,6 +180,10 @@ class BpBluetooth : public android::BpInterface<IBluetooth> {
  std::vector<bluetooth::UUID> GetUUIDs() override;
  bool SetName(const std::string& name) override;
  std::string GetName() override;
  void RegisterCallback(
      const android::sp<IBluetoothCallback>& callback) override;
  void UnregisterCallback(
      const android::sp<IBluetoothCallback>& callback) override;

 private:
  DISALLOW_COPY_AND_ASSIGN(BpBluetooth);
+86 −0
Original line number Diff line number Diff line
//
//  Copyright (C) 2015 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#include "service/ipc/binder/IBluetoothCallback.h"

#include <base/logging.h>
#include <binder/Parcel.h>

using android::IBinder;
using android::Parcel;
using android::sp;

namespace ipc {
namespace binder {

// static
const char IBluetoothCallback::kBluetoothCallbackServiceName[] =
    "bluetooth-callback-service";

// BnBluetoothCallback (server) implementation
// ========================================================

android::status_t BnBluetoothCallback::onTransact(
    uint32_t code,
    const Parcel& data,
    Parcel* reply,
    uint32_t flags) {
  VLOG(2) << "IBluetoothCallback transaction: " << code;
  if (!data.checkInterface(this))
    return android::PERMISSION_DENIED;

  switch (code) {
    case ON_BLUETOOTH_STATE_CHANGE_TRANSACTION: {
      int prev_state, new_state;
      if (data.readInt32(&prev_state) != android::NO_ERROR ||
          data.readInt32(&new_state) != android::NO_ERROR)
        return android::NOT_ENOUGH_DATA;

      OnBluetoothStateChange(
          static_cast<bluetooth::AdapterState>(prev_state),
          static_cast<bluetooth::AdapterState>(new_state));
      return android::NO_ERROR;
    }
    default:
      return BBinder::onTransact(code, data, reply, flags);
  }
}

// BpBluetoothCallback (client) implementation
// ========================================================

BpBluetoothCallback::BpBluetoothCallback(const sp<IBinder>& impl)
  : BpInterface<IBluetoothCallback>(impl) {
}

void BpBluetoothCallback::OnBluetoothStateChange(
    bluetooth::AdapterState prev_state,
    bluetooth::AdapterState new_state) {
  Parcel data, reply;

  data.writeInterfaceToken(IBluetoothCallback::getInterfaceDescriptor());
  data.writeInt32(prev_state);
  data.writeInt32(new_state);

  remote()->transact(IBluetoothCallback::ON_BLUETOOTH_STATE_CHANGE_TRANSACTION,
                     data, &reply);
}

IMPLEMENT_META_INTERFACE(BluetoothCallback,
                         IBluetoothCallback::kBluetoothCallbackServiceName);

}  // namespace binder
}  // namespace ipc
+87 −0
Original line number Diff line number Diff line
//
//  Copyright (C) 2015 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#pragma once

#include <base/macros.h>
#include <binder/IBinder.h>
#include <binder/IInterface.h>

#include "service/adapter_state.h"

namespace ipc {
namespace binder {

// This class defines the Binder IPC interface for receiving adapter state
// updates from the Bluetooth service. This class was written based on the
// corresponding AIDL file at
// /frameworks/base/core/java/android/bluetooth/IBluetoothCallback.aidl.
//
// NOTE: KEEP THIS FILE UP-TO-DATE with the corresponding AIDL, otherwise this
// won't be compatible with the Android framework.
class IBluetoothCallback : public android::IInterface {
 public:
  DECLARE_META_INTERFACE(BluetoothCallback);

  static const char kBluetoothCallbackServiceName[];

  // Transaction codes for interface methods.
  enum {
    ON_BLUETOOTH_STATE_CHANGE_TRANSACTION =
        android::IBinder::FIRST_CALL_TRANSACTION,
  };

  virtual void OnBluetoothStateChange(
      bluetooth::AdapterState prev_state,
      bluetooth::AdapterState new_state) = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(IBluetoothCallback);
};

// TODO(armansito): Implement notification for when the process dies.

// The Binder server interface to IBluetoothCallback. A class that implements
// IBluetoothCallback must inherit from this class.
class BnBluetoothCallback : public android::BnInterface<IBluetoothCallback> {
 public:
  BnBluetoothCallback() = default;
  virtual ~BnBluetoothCallback() = default;

 private:
  virtual android::status_t onTransact(
      uint32_t code,
      const android::Parcel& data,
      android::Parcel* reply,
      uint32_t flags = 0);
};

// The Binder client interface to IBluetoothCallback.
class BpBluetoothCallback : public android::BpInterface<IBluetoothCallback> {
 public:
  BpBluetoothCallback(const android::sp<android::IBinder>& impl);
  virtual ~BpBluetoothCallback() = default;

  // IBluetoothCallback override:
  void OnBluetoothStateChange(bluetooth::AdapterState prev_state,
                              bluetooth::AdapterState new_state) override;

 private:
  DISALLOW_COPY_AND_ASSIGN(BpBluetoothCallback);
};

}  // namespace binder
}  // namespace ipc
Loading