1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "PowerTestService"
18 
19 #include <signal.h>
20 #include <utils/Log.h>
21 
22 #include <binder/IPCThreadState.h>
23 #include <binder/ProcessState.h>
24 #include <binder/IServiceManager.h>
25 #include <binder/IBinder.h>
26 #include <binder/IInterface.h>
27 
28 #include "android/car/ICar.h"
29 #include "android/car/hardware/power/ICarPower.h"
30 #include "CarPowerManager.h"
31 
32 using namespace android;
33 using namespace android::binder;
34 using namespace android::car;
35 using namespace android::car::hardware::power;
36 
37 static std::atomic_bool run(true);
38 
onStateChanged(CarPowerManager::State state)39 void onStateChanged(CarPowerManager::State state) {
40     ALOGI(LOG_TAG "onStateChanged callback = %d", state);
41     if (state == CarPowerManager::State::kShutdownPrepare) {
42         // Stop the loop
43         run = false;
44     }
45 }
46 
main(int,char **)47 int main(int, char**)
48 {
49     int retVal;
50 
51     sp<ProcessState> processSelf(ProcessState::self());
52     processSelf->startThreadPool();
53     ALOGI(LOG_TAG "started");
54 
55     std::unique_ptr<CarPowerManager> carPowerManager(new CarPowerManager());
56 
57     do {
58         retVal = carPowerManager->setListener(onStateChanged);
59     } while (retVal != 0);
60 
61     do {
62         ALOGI(LOG_TAG "Waiting for CarPowerManager listener to initiate SHUTDOWN_PREPARE...");
63         sleep(5);
64     } while (run);
65 
66     ALOGI(LOG_TAG "Exited loop, shutting down");
67 
68     // Unregister the listener
69     carPowerManager->clearListener();
70 
71     // Wait for threads to finish, and then exit.
72     IPCThreadState::self()->joinThreadPool();
73     return 0;
74 }
75 
76