Merge "Modify service example to use aidl."
diff --git a/service_example/Android.mk b/service_example/Android.mk
index 01f6b9c..007133c 100644
--- a/service_example/Android.mk
+++ b/service_example/Android.mk
@@ -11,29 +11,40 @@
 # 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.
+#
 
 LOCAL_PATH := $(call my-dir)
 
 include $(CLEAR_VARS)
-LOCAL_MODULE := testservice
-LOCAL_REQUIRED_MODULES := testservice.rc
-LOCAL_SRC_FILES := testservice.cpp
-LOCAL_SHARED_LIBRARIES := libc libbase
-LOCAL_CFLAGS := -Werror
+LOCAL_MODULE := brillo_example_service
+LOCAL_SRC_FILES := \
+    brillo_example_service.cpp \
+    android/brillo/example/IAlertCallback.aidl \
+    android/brillo/example/IExampleService.aidl
+LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)
+LOCAL_CFLAGS := -Wall
+LOCAL_SHARED_LIBRARIES := \
+    libbinder \
+    libbrillo \
+    libbrillo-binder \
+    libchrome \
+    libutils
+LOCAL_INIT_RC := example_service.rc
 include $(BUILD_EXECUTABLE)
 
-ifdef INITRC_TEMPLATE
 include $(CLEAR_VARS)
-LOCAL_MODULE := testservice.rc
-LOCAL_MODULE_CLASS := ETC
-LOCAL_MODULE_PATH := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_INITRCD)
-
-include $(BUILD_SYSTEM)/base_rules.mk
-
-.PHONY: $(LOCAL_BUILT_MODULE)
-$(LOCAL_BUILT_MODULE): my_args := arg1 arg2
-$(LOCAL_BUILT_MODULE): my_groups := inet
-$(LOCAL_BUILT_MODULE): $(INITRC_TEMPLATE)
-	$(call generate-initrc-file,testservice,$(my_args),\
-		$(my_groups))
-endif
+LOCAL_MODULE := brillo_example_client
+LOCAL_SRC_FILES := \
+    brillo_example_client.cpp \
+    android/brillo/example/IAlertCallback.aidl \
+    android/brillo/example/IExampleService.aidl
+LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)
+LOCAL_CFLAGS := -Wall
+LOCAL_SHARED_LIBRARIES := \
+    libbinder \
+    libbrillo \
+    libbrillo-binder \
+    libchrome \
+    libcutils \
+    libutils
+include $(BUILD_EXECUTABLE)
diff --git a/service_example/testservice.cpp b/service_example/android/brillo/example/IAlertCallback.aidl
similarity index 67%
rename from service_example/testservice.cpp
rename to service_example/android/brillo/example/IAlertCallback.aidl
index 0317b31..a3eb9ee 100644
--- a/service_example/testservice.cpp
+++ b/service_example/android/brillo/example/IAlertCallback.aidl
@@ -14,18 +14,12 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "testservice"
+package android.brillo.example;
 
-#include <unistd.h>
-
-#include <android-base/logging.h>
-
-int main(int argc __unused, char **argv __unused) {
-    LOG(INFO) << "starting";
-    while (1) {
-        LOG(INFO) << "loop iteration";
-        sleep(5);
-    }
-    LOG(INFO) << "exiting";
-    return 0;
+// Interface for a callback object that is to be registered with
+// IExampleService.
+interface IAlertCallback {
+  // This should be a oneway call since we don't want services to be blocked on
+  // clients.
+  oneway void OnNLogEventsReceivedCallback(int n);
 }
diff --git a/service_example/android/brillo/example/IExampleService.aidl b/service_example/android/brillo/example/IExampleService.aidl
new file mode 100644
index 0000000..d478abd
--- /dev/null
+++ b/service_example/android/brillo/example/IExampleService.aidl
@@ -0,0 +1,31 @@
+/*
+ * Copyright 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.brillo.example;
+
+import android.brillo.example.IAlertCallback;
+
+// Interface for an example service that accepts a string and logs it using
+// logcat. It also provides a method to register a callback.
+interface IExampleService {
+  // Registers a callback object of type IAlertCallback with the service. Once
+  // the service has recieved n calls to Log, then the method
+  // OnNEventsReceivedCallback on the callback object will be called.
+  void RegisterCallback(IAlertCallback callback, int n);
+
+  // Logs a string.
+  void Log(String s);
+}
diff --git a/service_example/brillo_example_client.cpp b/service_example/brillo_example_client.cpp
new file mode 100644
index 0000000..ec63569
--- /dev/null
+++ b/service_example/brillo_example_client.cpp
@@ -0,0 +1,86 @@
+/*
+ * Copyright 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 <iostream>
+#include <string>
+
+#include <base/at_exit.h>
+#include <base/bind.h>
+#include <base/logging.h>
+#include <base/message_loop/message_loop.h>
+#include <binder/IInterface.h>
+#include <binder/IServiceManager.h>
+#include <brillo/binder_watcher.h>
+#include <brillo/message_loops/base_message_loop.h>
+#include <brillo/syslog_logging.h>
+#include <utils/String16.h>
+
+#include "android/brillo/example/BnAlertCallback.h"
+#include "android/brillo/example/IExampleService.h"
+
+namespace android {
+
+class AlertCallback : public android::brillo::example::BnAlertCallback {
+ public:
+  binder::Status OnNLogEventsReceivedCallback(int n) {
+    LOG(INFO) << "Received Callback from service: Received " << n << " log "
+              << "messages.";
+    return binder::Status::ok();
+  }
+};
+
+}  // namespace android
+
+// A function to read user input on the console and pass it along to the example
+// service for logging.
+void StdinCallback(
+    android::sp<android::brillo::example::IExampleService> service) {
+  std::string line;
+  std::cin >> line;
+  service->Log(android::String16(line.c_str()));
+}
+
+int main() {
+  // TODO(ralphnathan): Hide this in brillo::BaseMessageLoop.
+  base::AtExitManager at_exit_manager;
+  brillo::InitLog(brillo::kLogToSyslog);
+  // Get handle for the example service.
+  android::sp<android::brillo::example::IExampleService> service;
+  android::status_t status = android::getService(
+      android::String16("android.brillo.example.ExampleService"), &service);
+  CHECK(status == android::OK) <<
+      "Failed to get IExampleService binder from service manager!";
+
+  // Register a callback object with the service.
+  android::sp<android::AlertCallback> cbo = new android::AlertCallback();
+  service->RegisterCallback(cbo, 3);
+
+  // Create a message loop.
+  // TODO(ralphnathan): Change this to brillo::BaseMessageLoop.
+  base::MessageLoopForIO message_loop_for_io;
+
+  // Initialize a binder watcher.
+  brillo::BinderWatcher watcher;
+  watcher.Init();
+
+  // Poll stdin.
+  brillo::BaseMessageLoop message_loop(&message_loop_for_io);
+  base::Closure stdin_callback = base::Bind(&StdinCallback, service);
+  message_loop.WatchFileDescriptor(
+      STDIN_FILENO, brillo::BaseMessageLoop::kWatchRead, true, stdin_callback);
+  // Start the message loop.
+  message_loop.Run();
+}
diff --git a/service_example/brillo_example_service.cpp b/service_example/brillo_example_service.cpp
new file mode 100644
index 0000000..4c4af15
--- /dev/null
+++ b/service_example/brillo_example_service.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright 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 <base/at_exit.h>
+#include <base/logging.h>
+#include <base/message_loop/message_loop.h>
+#include <binder/IServiceManager.h>
+#include <binder/Status.h>
+#include <brillo/binder_watcher.h>
+#include <brillo/syslog_logging.h>
+#include <utils/String16.h>
+
+#include "android/brillo/example/BnExampleService.h"
+#include "android/brillo/example/IAlertCallback.h"
+
+namespace android {
+
+class ExampleService : public android::brillo::example::BnExampleService {
+ public:
+  binder::Status Log(const String16& s) {
+    LOG(INFO) << String8(s).string();
+    count_++;
+    if (count_ == max_events_) {
+      cbo_->OnNLogEventsReceivedCallback(count_);
+      count_ = 0;
+    }
+    return binder::Status::ok();
+  }
+
+  binder::Status RegisterCallback(
+      const sp<android::brillo::example::IAlertCallback>& callback, int n) {
+    cbo_ = callback;
+    max_events_ = n;
+    count_ = 0;
+    return binder::Status::ok();
+  }
+
+ private:
+  sp<android::brillo::example::IAlertCallback> cbo_;
+  int count_ = 0;
+  int max_events_ = 0;
+};
+
+}  // namespace android
+
+int main() {
+  // TODO(ralphnathan): Hide this in brillo::BaseMessageLoop.
+  base::AtExitManager at_exit_manager;
+  brillo::InitLog(brillo::kLogToSyslog);
+  // Register service with the service manager.
+  android::status_t status = android::defaultServiceManager()->addService(
+      android::String16("android.brillo.example.ExampleService"),
+      new android::ExampleService());
+  CHECK(status == android::OK) <<
+      "Failed to get IExampleService binder from service manager!";
+
+  // Create a message loop.
+  // TODO(ralphnathan): Change this to brillo::BaseMessageLoop.
+  base::MessageLoopForIO message_loop_for_io;
+
+  // Initialize a binder watcher.
+  brillo::BinderWatcher watcher;
+  watcher.Init();
+
+  // Run the message loop.
+  message_loop_for_io.Run();
+}
diff --git a/service_example/example_service.rc b/service_example/example_service.rc
new file mode 100644
index 0000000..79200e8
--- /dev/null
+++ b/service_example/example_service.rc
@@ -0,0 +1,4 @@
+service example_service /system/bin/brillo_example_service
+    class main
+    user system
+    group system
diff --git a/service_example/init.testservice.rc.example b/service_example/init.testservice.rc.example
deleted file mode 100644
index 572d5f3..0000000
--- a/service_example/init.testservice.rc.example
+++ /dev/null
@@ -1,4 +0,0 @@
-service testservice /system/bin/testservice
-    class main
-    user system
-    group system