Convert rpc_service_test to use a singleton manager

The singleton manager will make it easier to organize the code when
adding logic to use pw_rpc in the test.

Bug: 210138227
Test: compile
Change-Id: I8d7557eefa0499d0e05807031131b3b244169c8c
diff --git a/apps/test/common/rpc_service_test/Makefile b/apps/test/common/rpc_service_test/Makefile
index 6196bd0..8e9ac1c 100644
--- a/apps/test/common/rpc_service_test/Makefile
+++ b/apps/test/common/rpc_service_test/Makefile
@@ -24,6 +24,7 @@
 
 # Source Code ##################################################################
 
+COMMON_SRCS += $(NANOAPP_PATH)/src/rpc_service_manager.cc
 COMMON_SRCS += $(NANOAPP_PATH)/src/rpc_service_test.cc
 
 # Compiler Flags ###############################################################
@@ -33,6 +34,7 @@
 
 # Includes
 COMMON_CFLAGS += -I$(TEST_SHARED_PATH)/inc
+COMMON_CFLAGS += -I$(NANOAPP_PATH)/inc
 
 # Makefile Includes ############################################################
 
diff --git a/apps/test/common/rpc_service_test/inc/rpc_service_manager.h b/apps/test/common/rpc_service_test/inc/rpc_service_manager.h
new file mode 100644
index 0000000..c3f26ca
--- /dev/null
+++ b/apps/test/common/rpc_service_test/inc/rpc_service_manager.h
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2022 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 CHRE_RPC_SERVICE_MANAGER_H_
+#define CHRE_RPC_SERVICE_MANAGER_H_
+
+#include <cinttypes>
+#include <cstdint>
+
+#include <chre.h>
+
+#include "chre/util/singleton.h"
+
+namespace chre {
+namespace rpc_service_test {
+
+/**
+ * Class to manage the CHRE rpc service nanoapp.
+ */
+class RpcServiceManager {
+ public:
+  /**
+   * Allows the manager to do any init necessary as part of nanoappStart.
+   */
+  bool start();
+
+  /**
+   * Handle a CHRE event.
+   *
+   * @param senderInstanceId The instand ID that sent the event.
+   * @param eventType The type of the event.
+   * @param eventData The data for the event.
+   */
+  void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
+                   const void *eventData);
+};
+
+typedef chre::Singleton<RpcServiceManager> RpcServiceManagerSingleton;
+
+}  // namespace rpc_service_test
+}  // namespace chre
+
+#endif  // CHRE_RPC_SERVICE_MANAGER_H_
diff --git a/apps/test/common/rpc_service_test/src/rpc_service_manager.cc b/apps/test/common/rpc_service_test/src/rpc_service_manager.cc
new file mode 100644
index 0000000..3dd18af
--- /dev/null
+++ b/apps/test/common/rpc_service_test/src/rpc_service_manager.cc
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2022 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 "rpc_service_manager.h"
+
+#include "chre/util/macros.h"
+#include "chre/util/nanoapp/log.h"
+
+#define LOG_TAG "[RpcServiceTest]"
+
+namespace chre {
+namespace rpc_service_test {
+
+bool RpcServiceManager::start() {
+  static chreNanoappRpcService sRpcService = {
+      .id = 0xca8f7150a3f05847,
+      .version = 0x01020034,
+  };
+
+  return chrePublishRpcServices(&sRpcService, 1 /* numServices */);
+}
+
+void RpcServiceManager::handleEvent(uint32_t senderInstanceId,
+                                    uint16_t eventType, const void *eventData) {
+  UNUSED_VAR(eventData);
+
+  LOGW("Got unknown event type from senderInstanceId %" PRIu32
+       " and with eventType %" PRIu16,
+       senderInstanceId, eventType);
+}
+
+}  // namespace rpc_service_test
+}  // namespace chre
diff --git a/apps/test/common/rpc_service_test/src/rpc_service_test.cc b/apps/test/common/rpc_service_test/src/rpc_service_test.cc
index effe663..da709ff 100644
--- a/apps/test/common/rpc_service_test/src/rpc_service_test.cc
+++ b/apps/test/common/rpc_service_test/src/rpc_service_test.cc
@@ -14,34 +14,25 @@
  * limitations under the License.
  */
 
-#include <chre.h>
-#include <cinttypes>
-
-#include "chre/util/macros.h"
-#include "chre/util/nanoapp/log.h"
-
-#define LOG_TAG "[RpcServiceTest]"
+#include "rpc_service_manager.h"
 
 namespace chre {
+namespace rpc_service_test {
 
 extern "C" void nanoappHandleEvent(uint32_t senderInstanceId,
                                    uint16_t eventType, const void *eventData) {
-  UNUSED_VAR(eventData);
-
-  LOGW("Got unknown event type from senderInstanceId %" PRIu32
-       " and with eventType %" PRIu16,
-       senderInstanceId, eventType);
+  RpcServiceManagerSingleton::get()->handleEvent(senderInstanceId, eventType,
+                                                 eventData);
 }
 
 extern "C" bool nanoappStart(void) {
-  static chreNanoappRpcService sRpcService = {
-      .id = 0xca8f7150a3f05847,
-      .version = 0x01020034,
-  };
-
-  return chrePublishRpcServices(&sRpcService, 1 /* numServices */);
+  RpcServiceManagerSingleton::init();
+  return RpcServiceManagerSingleton::get()->start();
 }
 
-extern "C" void nanoappEnd(void) {}
+extern "C" void nanoappEnd(void) {
+  RpcServiceManagerSingleton::deinit();
+}
 
+}  // namespace rpc_service_test
 }  // namespace chre