service: Add IBluetooth interface definition

This CL adds the initial IBluetooth interface definition. Client and server
proxy classes have been added, which would normally be autogenerated in Android
Framework Land. This currently only includes the first five methods from
IBluetooth.aidl but will be extended in the future.

Bug: 22743677
Change-Id: I32be9801e91877c2961e24232cdfaecd80ee2368
diff --git a/service/Android.mk b/service/Android.mk
index 4934c47..de963a8 100644
--- a/service/Android.mk
+++ b/service/Android.mk
@@ -31,12 +31,16 @@
 	settings.cpp \
 	uuid.cpp
 
+btserviceBinderSrc := \
+	ipc/binder/IBluetooth.cpp
+
 btserviceCommonIncludes := $(LOCAL_PATH)/../
 
 # Native system service for target
 # ========================================================
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := \
+	$(btserviceBinderSrc) \
 	$(btserviceCommonSrc) \
 	main.cpp
 LOCAL_C_INCLUDES += $(btserviceCommonIncludes)
@@ -46,10 +50,12 @@
 LOCAL_REQUIRED_MODULES = bluetooth.default
 LOCAL_STATIC_LIBRARIES += libbtcore
 LOCAL_SHARED_LIBRARIES += \
+	libbinder \
 	libchrome \
 	libcutils \
 	libhardware \
-	liblog
+	liblog \
+	libutils
 include $(BUILD_EXECUTABLE)
 
 # Native system service unittests for host
diff --git a/service/ipc/binder/IBluetooth.cpp b/service/ipc/binder/IBluetooth.cpp
new file mode 100644
index 0000000..c496eff
--- /dev/null
+++ b/service/ipc/binder/IBluetooth.cpp
@@ -0,0 +1,140 @@
+//
+//  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/IBluetooth.h"
+
+#include <base/logging.h>
+#include <binder/IServiceManager.h>
+#include <binder/Parcel.h>
+
+using android::defaultServiceManager;
+using android::IBinder;
+using android::interface_cast;
+using android::IServiceManager;
+using android::Parcel;
+using android::sp;
+using android::String16;
+
+namespace ipc {
+namespace binder {
+
+// static
+const char IBluetooth::kBluetoothServiceName[] = "bluetoothservice";
+
+// static
+android::sp<IBluetooth> IBluetooth::getClientInterface() {
+  sp<IServiceManager> sm = defaultServiceManager();
+  if (!sm.get()) {
+    LOG(ERROR) << "Failed to obtain a handle to the default Service Manager";
+    return nullptr;
+  }
+
+  sp<IBinder> binder = sm->getService(String16(kBluetoothServiceName));
+  if (!binder.get()) {
+    LOG(ERROR) << "Failed to obtain a handle to the Bluetooth service";
+    return nullptr;
+  }
+
+  sp<IBluetooth> bt_iface = interface_cast<IBluetooth>(binder);
+  if (!bt_iface.get()) {
+    LOG(ERROR) << "Obtained invalid IBinder handle";
+    return nullptr;
+  }
+
+  return bt_iface;
+}
+
+// BnBluetooth (server) implementation
+// ========================================================
+
+android::status_t BnBluetooth::onTransact(
+    uint32_t code,
+    const Parcel& data,
+    Parcel* reply,
+    uint32_t flags) {
+  VLOG(2) << "IBluetooth transaction: " << code;
+  if (!data.checkInterface(this))
+    return android::PERMISSION_DENIED;
+
+  switch (code) {
+    case IS_ENABLED_TRANSACTION: {
+      bool is_enabled = IsEnabled();
+      reply->writeInt32(is_enabled);
+      return android::NO_ERROR;
+    }
+    // TODO(armansito): Implement other functions here.
+    default:
+      return BBinder::onTransact(code, data, reply, flags);
+  }
+}
+
+// BpBluetooth (client) implementation
+// ========================================================
+
+BpBluetooth::BpBluetooth(const sp<IBinder>& impl)
+    : BpInterface<IBluetooth>(impl) {
+}
+
+bool BpBluetooth::IsEnabled() {
+  Parcel data, reply;
+
+  data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
+  remote()->transact(IBluetooth::IS_ENABLED_TRANSACTION, data, &reply);
+
+  return reply.readInt32();
+}
+
+int BpBluetooth::GetState() {
+  Parcel data, reply;
+
+  data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
+  remote()->transact(IBluetooth::GET_STATE_TRANSACTION, data, &reply);
+
+  return reply.readInt32();
+}
+
+bool BpBluetooth::Enable() {
+  Parcel data, reply;
+
+  data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
+  remote()->transact(IBluetooth::ENABLE_TRANSACTION, data, &reply);
+
+  return reply.readInt32();
+}
+
+bool BpBluetooth::EnableNoAutoConnect() {
+  Parcel data, reply;
+
+  data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
+  remote()->transact(IBluetooth::ENABLE_NO_AUTO_CONNECT_TRANSACTION,
+                     data, &reply);
+
+  return reply.readInt32();
+}
+
+bool BpBluetooth::Disable() {
+  Parcel data, reply;
+
+  data.writeInterfaceToken(IBluetooth::getInterfaceDescriptor());
+  remote()->transact(IBluetooth::DISABLE_TRANSACTION, data, &reply);
+
+  return reply.readInt32();
+}
+
+IMPLEMENT_META_INTERFACE(Bluetooth, IBluetooth::kBluetoothServiceName);
+
+}  // namespace binder
+}  // namespace ipc
diff --git a/service/ipc/binder/IBluetooth.h b/service/ipc/binder/IBluetooth.h
new file mode 100644
index 0000000..937f05d
--- /dev/null
+++ b/service/ipc/binder/IBluetooth.h
@@ -0,0 +1,92 @@
+//
+//  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>
+
+namespace ipc {
+namespace binder {
+
+// This class defines the Binder IPC interface for accessing the Bluetooth
+// service. This class was written based on the corresponding AIDL file at
+// /frameworks/base/core/java/android/bluetooth/IBluetooth.aidl.
+class IBluetooth : public android::IInterface {
+ public:
+  DECLARE_META_INTERFACE(Bluetooth);
+
+  static const char kBluetoothServiceName[];
+
+  // Transaction codes for interface methods.
+  enum {
+    IS_ENABLED_TRANSACTION = android::IBinder::FIRST_CALL_TRANSACTION,
+    GET_STATE_TRANSACTION,
+    ENABLE_TRANSACTION,
+    ENABLE_NO_AUTO_CONNECT_TRANSACTION,
+    DISABLE_TRANSACTION,
+  };
+
+  // Returns a handle to the IBluetooth Binder from the Android ServiceManager.
+  // Binder client code can use this to make calls to the service.
+  static android::sp<IBluetooth> getClientInterface();
+
+  virtual bool IsEnabled() = 0;
+  virtual int GetState() = 0;
+  virtual bool Enable() = 0;
+  virtual bool EnableNoAutoConnect() = 0;
+  virtual bool Disable() = 0;
+
+  // TODO(armansito): Complete the API definition.
+ private:
+  DISALLOW_COPY_AND_ASSIGN(IBluetooth);
+};
+
+// The Binder server interface to IBluetooth. A class that implements IBluetooth
+// must inherit from this class.
+class BnBluetooth : public android::BnInterface<IBluetooth> {
+ public:
+  BnBluetooth() = default;
+  virtual ~BnBluetooth() = 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 IBluetooth.
+class BpBluetooth : public android::BpInterface<IBluetooth> {
+ public:
+  BpBluetooth(const android::sp<android::IBinder>& impl);
+  virtual ~BpBluetooth() = default;
+
+  // IBluetooth overrides:
+  bool IsEnabled() override;
+  int GetState() override;
+  bool Enable() override;
+  bool EnableNoAutoConnect() override;
+  bool Disable() override;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(BpBluetooth);
+};
+
+}  // namespace binder
+}  // namespace ipc