Add Binder service for update_engine

Bug: 25632204
Test: Compiles, registers with the service manager on aosp_arm-eng

Change-Id: Iaf67a37193958f0a11d9f3d320cd6e93cf6afbef
diff --git a/Android.mk b/Android.mk
index 29afd3c..8c1388a 100644
--- a/Android.mk
+++ b/Android.mk
@@ -290,7 +290,6 @@
 LOCAL_MODULE := update_engine
 LOCAL_MODULE_CLASS := EXECUTABLES
 LOCAL_CPP_EXTENSION := .cc
-LOCAL_RTTI_FLAG := -frtti
 LOCAL_CLANG := true
 LOCAL_CFLAGS := $(ue_common_cflags)
 LOCAL_CPPFLAGS := $(ue_common_cppflags)
@@ -301,11 +300,31 @@
 LOCAL_STATIC_LIBRARIES := \
     libupdate_engine \
     $(ue_libupdate_engine_exported_static_libraries)
+
+ifdef BRILLO
+
+LOCAL_RTTI_FLAG := -frtti
 LOCAL_SHARED_LIBRARIES := \
     $(ue_common_shared_libraries) \
     $(ue_libupdate_engine_exported_shared_libraries)
 LOCAL_SRC_FILES := \
     main.cc
+
+else  # !defined(BRILLO)
+
+LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/binder_bindings
+LOCAL_SHARED_LIBRARIES := \
+    libbinder \
+    liblog \
+    libutils
+LOCAL_SRC_FILES := \
+    binder_bindings/android/os/IUpdateEngine.aidl \
+    binder_bindings/android/os/IUpdateEnginePayloadApplicationCallback.aidl \
+    binder_main.cc \
+    binder_service.cc
+
+endif  # defined(BRILLO)
+
 LOCAL_INIT_RC := update_engine.rc
 include $(BUILD_EXECUTABLE)
 
diff --git a/binder_bindings/android/os/IUpdateEngine.aidl b/binder_bindings/android/os/IUpdateEngine.aidl
new file mode 100644
index 0000000..ebc3ffb
--- /dev/null
+++ b/binder_bindings/android/os/IUpdateEngine.aidl
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * 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.
+ */
+
+package android.os;
+
+import android.os.IUpdateEnginePayloadApplicationCallback;
+
+interface IUpdateEngine {
+  int applyPayload(String url,
+                   in String[] headerKeyValuePairs,
+                   IUpdateEnginePayloadApplicationCallback callback);
+  void suspend();
+  void resume();
+  void cancel();
+}
diff --git a/binder_bindings/android/os/IUpdateEnginePayloadApplicationCallback.aidl b/binder_bindings/android/os/IUpdateEnginePayloadApplicationCallback.aidl
new file mode 100644
index 0000000..871ef1d
--- /dev/null
+++ b/binder_bindings/android/os/IUpdateEnginePayloadApplicationCallback.aidl
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * 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.
+ */
+
+package android.os;
+
+oneway interface IUpdateEnginePayloadApplicationCallback {
+  void onStatusUpdate(int status_code, float percentage);
+  void onPayloadApplicationComplete(int error_code);
+}
diff --git a/binder_main.cc b/binder_main.cc
new file mode 100644
index 0000000..7d5c975
--- /dev/null
+++ b/binder_main.cc
@@ -0,0 +1,91 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// 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 <binder/IPCThreadState.h>
+#include <binder/IServiceManager.h>
+#include <binder/ProcessState.h>
+#include <utils/Errors.h>
+#include <utils/Log.h>
+#include <utils/Looper.h>
+#include <utils/StrongPointer.h>
+
+#include "update_engine/binder_service.h"
+
+// Log to logcat as update_engine.
+#undef LOG_TAG
+#define LOG_TAG "update_engine"
+
+namespace android {
+namespace {
+
+class BinderCallback : public LooperCallback {
+ public:
+  BinderCallback() {}
+  ~BinderCallback() override {}
+
+  int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
+    IPCThreadState::self()->handlePolledCommands();
+    return 1;  // Continue receiving callbacks.
+  }
+};
+
+bool run(const sp<IBinder>& service) {
+  sp<Looper> looper(Looper::prepare(0 /* opts */));
+
+  ALOGD("Connecting to binder driver");
+  int binder_fd = -1;
+  ProcessState::self()->setThreadPoolMaxThreadCount(0);
+  IPCThreadState::self()->disableBackgroundScheduling(true);
+  IPCThreadState::self()->setupPolling(&binder_fd);
+  if (binder_fd < 0) {
+    return false;
+  }
+
+  sp<BinderCallback> cb(new BinderCallback);
+  if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
+                    nullptr) != 1) {
+    ALOGE("Failed to add binder FD to Looper");
+    return false;
+  }
+
+  ALOGD("Registering update_engine with the service manager");
+  status_t status = defaultServiceManager()->addService(
+      service->getInterfaceDescriptor(), service);
+  if (status != android::OK) {
+    ALOGE("Failed to register update_engine with the service manager.");
+    return false;
+  }
+
+  ALOGD("Entering update_engine mainloop");
+  while (true) {
+    const int result = looper->pollAll(-1 /* timeoutMillis */);
+    ALOGD("Looper returned %d", result);
+  }
+  // We should never get here.
+  return false;
+}
+
+}  // namespace
+}  // namespace android
+
+int main(int argc, char** argv) {
+  android::sp<android::IBinder> service(
+      new chromeos_update_engine::BinderService);
+  if (!android::run(service)) {
+    return 1;
+  }
+  return 0;
+}
diff --git a/binder_service.cc b/binder_service.cc
new file mode 100644
index 0000000..b8c62ce
--- /dev/null
+++ b/binder_service.cc
@@ -0,0 +1,49 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// 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 "update_engine/binder_service.h"
+
+using android::OK;
+using android::String16;
+using android::os::IUpdateEnginePayloadApplicationCallback;
+using android::sp;
+using android::status_t;
+using std::vector;
+
+namespace chromeos_update_engine {
+
+status_t BinderService::applyPayload(
+    const String16& url,
+    const vector<String16>& header_kv_pairs,
+    const sp<IUpdateEnginePayloadApplicationCallback>& callback,
+    int32_t* return_value) {
+  *return_value = 0;
+  return OK;
+}
+
+status_t BinderService::suspend() {
+  return OK;
+}
+
+status_t BinderService::resume() {
+  return OK;
+}
+
+status_t BinderService::cancel() {
+  return OK;
+}
+
+}  // namespace chromeos_update_engine
diff --git a/binder_service.h b/binder_service.h
new file mode 100644
index 0000000..ac98169
--- /dev/null
+++ b/binder_service.h
@@ -0,0 +1,52 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// 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.
+//
+
+#ifndef UPDATE_ENGINE_BINDER_SERVICE_H_
+#define UPDATE_ENGINE_BINDER_SERVICE_H_
+
+#include <vector>
+
+#include <utils/Errors.h>
+#include <utils/String16.h>
+#include <utils/StrongPointer.h>
+
+#include "android/os/BnUpdateEngine.h"
+#include "android/os/IUpdateEnginePayloadApplicationCallback.h"
+
+namespace chromeos_update_engine {
+
+class BinderService : public android::os::BnUpdateEngine {
+ public:
+  BinderService() = default;
+  virtual ~BinderService() = default;
+
+  android::status_t applyPayload(
+      const android::String16& url,
+      const std::vector<android::String16>& header_kv_pairs,
+      const android::sp<android::os::IUpdateEnginePayloadApplicationCallback>&
+          callback,
+      int32_t* return_value) override;
+
+  android::status_t suspend() override;
+
+  android::status_t resume() override;
+
+  android::status_t cancel() override;
+};  // class BinderService
+
+}  // namespace chromeos_update_engine
+
+#endif  // UPDATE_ENGINE_BINDER_SERVICE_H_