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