1 /*
2  * Copyright (C) 2016 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 #include <unistd.h>
18 
19 #include <android-base/logging.h>
20 #include <hidl/HidlTransportSupport.h>
21 #include <utils/Errors.h>
22 #include <utils/StrongPointer.h>
23 #include <utils/Log.h>
24 
25 #include "ServiceNames.h"
26 #include "Enumerator.h"
27 
28 
29 // libhidl:
30 using android::hardware::configureRpcThreadpool;
31 using android::hardware::joinRpcThreadpool;
32 
33 // Generated HIDL files
34 using android::hardware::automotive::evs::V1_1::IEvsEnumerator;
35 using android::hardware::automotive::evs::V1_0::IEvsDisplay;
36 
37 // The namespace in which all our implementation code lives
38 using namespace android::automotive::evs::V1_1::implementation;
39 using namespace android;
40 
41 
startService(const char * hardwareServiceName,const char * managerServiceName)42 static void startService(const char *hardwareServiceName, const char * managerServiceName) {
43     LOG(INFO) << "EVS managed service connecting to hardware service at " << hardwareServiceName;
44     android::sp<Enumerator> service = new Enumerator();
45     if (!service->init(hardwareServiceName)) {
46         LOG(ERROR) << "Failed to connect to hardware service - quitting from registrationThread";
47         exit(1);
48     }
49 
50     // Register our service -- if somebody is already registered by our name,
51     // they will be killed (their thread pool will throw an exception).
52     LOG(INFO) << "EVS managed service is starting as " << managerServiceName;
53     status_t status = service->registerAsService(managerServiceName);
54     if (status != OK) {
55         LOG(ERROR) << "Could not register service " << managerServiceName
56                    << " status = " << status << " - quitting from registrationThread";
57         exit(2);
58     }
59 
60     LOG(INFO) << "Registration complete";
61 }
62 
63 
main(int argc,char ** argv)64 int main(int argc, char** argv) {
65     LOG(INFO) << "EVS manager starting";
66 
67 #ifdef EVS_DEBUG
68     SetMinimumLogSeverity(android::base::DEBUG);
69 #endif
70 
71     // Set up default behavior, then check for command line options
72     bool printHelp = false;
73     const char* evsHardwareServiceName = kHardwareEnumeratorName;
74     for (int i=1; i< argc; i++) {
75         if (strcmp(argv[i], "--mock") == 0) {
76             evsHardwareServiceName = kMockEnumeratorName;
77         } else if (strcmp(argv[i], "--target") == 0) {
78             i++;
79             if (i >= argc) {
80                 LOG(ERROR) << "--target <service> was not provided with a service name";
81             } else {
82                 evsHardwareServiceName = argv[i];
83             }
84         } else if (strcmp(argv[i], "--help") == 0) {
85             printHelp = true;
86         } else {
87             printf("Ignoring unrecognized command line arg '%s'\n", argv[i]);
88             printHelp = true;
89         }
90     }
91     if (printHelp) {
92         printf("Options include:\n");
93         printf("  --mock                   Connect to the mock driver at EvsEnumeratorHw-Mock\n");
94         printf("  --target <service_name>  Connect to the named IEvsEnumerator service");
95     }
96 
97 
98     // Prepare the RPC serving thread pool.  We're configuring it with no additional
99     // threads beyond the main thread which will "join" the pool below.
100     configureRpcThreadpool(1, true /* callerWillJoin */);
101 
102     // The connection to the underlying hardware service must happen on a dedicated thread to ensure
103     // that the hwbinder response can be processed by the thread pool without blocking.
104     std::thread registrationThread(startService, evsHardwareServiceName, kManagedEnumeratorName);
105 
106     // Send this main thread to become a permanent part of the thread pool.
107     // This is not expected to return.
108     LOG(INFO) << "Main thread entering thread pool";
109     joinRpcThreadpool();
110 
111     // In normal operation, we don't expect the thread pool to exit
112     LOG(ERROR) << "EVS Hardware Enumerator is shutting down";
113     return 1;
114 }
115