Add wakeupApplicationProcessor function.

Add the logic to call wakeupAP when required.
Also update target in README.

Test: None, trivial logic.
Bug: 254547153
Change-Id: I0ad59f9f004d0cb5c01d664090fab984ac5420db
diff --git a/automotive/remoteaccess/test_grpc_server/README.md b/automotive/remoteaccess/test_grpc_server/README.md
index 42e2c94..090a8fd 100644
--- a/automotive/remoteaccess/test_grpc_server/README.md
+++ b/automotive/remoteaccess/test_grpc_server/README.md
@@ -41,7 +41,7 @@
   pending tasks through the 'ServerWriter'. If no task is pending, then it must
   block and wait for a new task to arrive.
 
-  If one task data is failed to be sent through the channel, it likely means
+  If one task data fails to be sent through the channel, it likely means
   the other side (Application processor) is shutting down or has closed the
   channel. The wakeup client must put the task back to the pending queue and
   wait for a new 'GetRemoteTasks' request to retry sending the task.
@@ -89,7 +89,7 @@
 
   `adb remount`
 
-* Under android root: `cd out/target/product/[product_name]`
+* Under android root: `cd $ANDROID_PRODUCT_OUT`
 
 * `adb push vendor/bin/TestWakeupClientServer /vendor/bin`
 
@@ -99,24 +99,24 @@
 
 * `/vendor/bin/TestWakeupClientServer`
 
-## How to build and test the test wakeup client using one gcar emulator.
+## How to build and test the test wakeup client using one car emulator.
 
 In this test setup we will use one google car emulator
-(gcar_emu_x86_64-userdebug). We assume both the TCU and the remote access HAL
+(sdk_car_x86_64-userdebug). We assume both the TCU and the remote access HAL
 runs on the same Android system, and they communicate through local loopback
 interface.
 
 * Under android root, `source build/envsetup.sh`
 
-* `lunch gcar_emu_x86_64-userdebug`
+* `lunch sdk_car_x86_64-userdebug`
 
 * `m -j`
 
 * Run the emulator, the '-read-only' flag is required to run multiple instances:
 
-  `aae emulator run -read-only`
+  `emulator -writable-system -read-only`
 
-* The android lunch target: gcar_emu_x86_64-userdebug and
+* The android lunch target: sdk_car_x86_64-userdebug and
   cf_x86_64_auto-userdebug already contains the default remote access HAL. For
   other lunch target, you can add the default remote access HAL by adding
   'android.hardware.automotive.remoteaccess@V1-default-service' to
@@ -142,9 +142,7 @@
 
 * `make -j TestWakeupClientServer`
 
-* `cd out/target/product/emulator_car64_x86_64/`
-
-* `adb push vendor/bin/TestWakeupClientServer /vendor/bin`
+* `adb push $ANDROID_PRODUCT_OUT/vendor/bin/TestWakeupClientServer /vendor/bin`
 
 * `adb shell`
 
@@ -152,7 +150,7 @@
 
 * `/vendor/bin/TestWakeupClientServer`
 
-* Remote access HAL should start by default when the gcar emulator starts. Now
+* Remote access HAL should start by default when the car emulator starts. Now
   the test wake up client should also be running and generating fake tasks.
 
   Start a new adb shell session by
@@ -210,9 +208,9 @@
 * Now you can issue `ctrl c` on the first adb shell to stop the test wakeup
   client.
 
-## How to build and test the test wakeup client using two gcar emulators.
+## How to build and test the test wakeup client using two car emulators.
 
-In this test case, we are going to use two gcar emulators, one as the
+In this test case, we are going to use two car emulators, one as the
 Application Processor, one as the TCU.
 
 * Change the IP address to allow IP communication between different emulator
@@ -227,13 +225,13 @@
 
 * Under android root: `source build/envsetup.sh`
 
-* `lunch gcar_emu_x86_64-userdebug`
+* `lunch sdk_car_x86_64-userdebug`
 
 *  `m -j`
 
-* Start one gcar emulator as TCU
+* Start one car emulator as TCU
 
-  `aae emulator run -read-only`
+  `emulator -writable-system -read-only`
 
 * Start a new shell session. Connect to the emulator's console,
   see [Start and stop a console session](https://developer.android.com/studio/run/emulator-console#console-session)
@@ -262,9 +260,7 @@
 
 * `make -j TestWakeupClientServer`
 
-* `cd out/target/product/emulator_car64_x86_64/`
-
-* `adb push vendor/bin/TestWakeupClientServer /vendor/bin`
+* `adb push $ANDROID_PRODUCT_OUT/vendor/bin/TestWakeupClientServer /vendor/bin`
 
 * `adb shell`
 
@@ -272,9 +268,9 @@
 
 * `/vendor/bin/TestWakeupClientServer`
 
-* Start a new shell, start another gcar emulator as the Application Processor:
+* Start a new shell, start another car emulator as the Application Processor:
 
-  `aae emulator run -read-only`
+  `emulator -writable-system -read-only`
 
 * Connect to adb shell for the application processor:
 
@@ -282,5 +278,5 @@
 
   `su`
 
-* Follow the test instructions for one gcar emulator using the 'dumpsys'
+* Follow the test instructions for one car emulator using the 'dumpsys'
   commands.
diff --git a/automotive/remoteaccess/test_grpc_server/impl/include/TestWakeupClientServiceImpl.h b/automotive/remoteaccess/test_grpc_server/impl/include/TestWakeupClientServiceImpl.h
index 61c1ad3..12bd93b 100644
--- a/automotive/remoteaccess/test_grpc_server/impl/include/TestWakeupClientServiceImpl.h
+++ b/automotive/remoteaccess/test_grpc_server/impl/include/TestWakeupClientServiceImpl.h
@@ -81,6 +81,7 @@
     void waitForTask();
     void stopWait();
     void handleTaskTimeout();
+    bool isEmpty();
 
   private:
     std::thread mCheckTaskTimeoutThread;
@@ -117,6 +118,8 @@
     std::thread mThread;
     // A variable to notify server is stopping.
     std::condition_variable mServerStoppedCv;
+    // Whether wakeup AP is required for executing tasks.
+    std::atomic<bool> mWakeupRequired = false;
     std::mutex mLock;
     bool mServerStopped GUARDED_BY(mLock);
 
@@ -126,6 +129,8 @@
     TaskQueue mTaskQueue;
 
     void fakeTaskGenerateLoop();
+
+    void wakeupApplicationProcessor();
 };
 
 }  // namespace remoteaccess
diff --git a/automotive/remoteaccess/test_grpc_server/impl/src/TestWakeupClientServiceImpl.cpp b/automotive/remoteaccess/test_grpc_server/impl/src/TestWakeupClientServiceImpl.cpp
index f64ac10..795265f 100644
--- a/automotive/remoteaccess/test_grpc_server/impl/src/TestWakeupClientServiceImpl.cpp
+++ b/automotive/remoteaccess/test_grpc_server/impl/src/TestWakeupClientServiceImpl.cpp
@@ -125,6 +125,11 @@
     mTasksNotEmptyCv.notify_all();
 }
 
+bool TaskQueue::isEmpty() {
+    std::lock_guard<std::mutex> lockGuard(mLock);
+    return mTasks.size() == 0 || mStopped;
+}
+
 void TaskQueue::checkForTestTimeoutLoop() {
     Looper::setForThread(mLooper);
 
@@ -179,6 +184,11 @@
     // from it. Here we simulate receiving one remote task every {kTaskIntervalInMs}ms.
     while (true) {
         mTaskQueue.add(mFakeTaskGenerator.generateTask());
+        printf("Received a new task\n");
+        if (mWakeupRequired) {
+            wakeupApplicationProcessor();
+        }
+
         printf("Sleeping for %d seconds until next task\n", kTaskIntervalInMs);
 
         std::unique_lock lk(mLock);
@@ -224,9 +234,21 @@
 Status TestWakeupClientServiceImpl::NotifyWakeupRequired(ServerContext* context,
                                                          const NotifyWakeupRequiredRequest* request,
                                                          NotifyWakeupRequiredResponse* response) {
+    if (request->iswakeuprequired() && !mWakeupRequired && !mTaskQueue.isEmpty()) {
+        // If wakeup is now required and previously not required, this means we have finished
+        // shutting down the device. If there are still pending tasks, try waking up AP again
+        // to finish executing those tasks.
+        wakeupApplicationProcessor();
+    }
+    mWakeupRequired = request->iswakeuprequired();
     return Status::OK;
 }
 
+void TestWakeupClientServiceImpl::wakeupApplicationProcessor() {
+    printf("Waking up application processor...\n");
+    // TODO(b/254547153): Send can bus message using socket CAN once we know what the message is.
+}
+
 }  // namespace remoteaccess
 }  // namespace automotive
 }  // namespace hardware