1 /**
2  * Copyright (c) 2020, 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 "carpowerpolicyd"
18 
19 #include "CarPowerPolicyServer.h"
20 
21 #include <android-base/result.h>
22 #include <binder/IPCThreadState.h>
23 #include <binder/ProcessState.h>
24 #include <log/log.h>
25 #include <utils/Looper.h>
26 #include <utils/StrongPointer.h>
27 
28 #include <signal.h>
29 
30 using android::IPCThreadState;
31 using android::Looper;
32 using android::ProcessState;
33 using android::sp;
34 using android::frameworks::automotive::powerpolicy::CarPowerPolicyServer;
35 
36 namespace {
37 
38 const size_t kMaxBinderThreadCount = 2;
39 
40 }  // namespace
41 
main(int,char **)42 int main(int /*argc*/, char** /*argv*/) {
43     // Set up the binder
44     sp<ProcessState> ps(ProcessState::self());
45     ps->setThreadPoolMaxThreadCount(kMaxBinderThreadCount);
46     ps->startThreadPool();
47     ps->giveThreadPoolName();
48     IPCThreadState::self()->disableBackgroundScheduling(true);
49 
50     // Set up the looper
51     sp<Looper> looper(Looper::prepare(/*opts=*/0));
52 
53     // Start the services
54     auto result = CarPowerPolicyServer::startService(looper);
55     if (!result.ok()) {
56         ALOGE("Failed to start service: %s", result.error().message().c_str());
57         CarPowerPolicyServer::terminateService();
58         exit(result.error().code());
59     }
60 
61     while (true) {
62         looper->pollAll(/*timeoutMillis=*/-1);
63     }
64     ALOGW("Car power policy server escaped from its loop.");
65 
66     return 0;
67 }
68