1 /*
2  * Copyright (C) 2016-2019 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 #include <atomic>
19 
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 "EvsEnumerator.h"
27 #include "EvsGlDisplay.h"
28 
29 
30 // libhidl:
31 using android::hardware::configureRpcThreadpool;
32 using android::hardware::joinRpcThreadpool;
33 
34 // Generated HIDL files
35 using android::hardware::automotive::evs::V1_1::IEvsEnumerator;
36 using android::hardware::automotive::evs::V1_1::IEvsDisplay;
37 using android::frameworks::automotive::display::V1_0::IAutomotiveDisplayProxyService;
38 
39 // The namespace in which all our implementation code lives
40 using namespace android::hardware::automotive::evs::V1_1::implementation;
41 using namespace android;
42 
43 
main()44 int main() {
45     LOG(INFO) << "EVS Hardware Enumerator service is starting";
46 
47     android::sp<IAutomotiveDisplayProxyService> carWindowService =
48         IAutomotiveDisplayProxyService::getService("default");
49     if (carWindowService == nullptr) {
50         LOG(ERROR) << "Cannot use AutomotiveDisplayProxyService.  Exiting.";
51         return 1;
52     }
53 
54 #ifdef EVS_DEBUG
55     SetMinimumLogSeverity(android::base::DEBUG);
56 #endif
57 
58     // Start a thread to listen video device addition events.
59     std::atomic<bool> running { true };
60     std::thread ueventHandler(EvsEnumerator::EvsUeventThread, std::ref(running));
61 
62     android::sp<IEvsEnumerator> service = new EvsEnumerator(carWindowService);
63 
64     configureRpcThreadpool(1, true /* callerWillJoin */);
65 
66     // Register our service -- if somebody is already registered by our name,
67     // they will be killed (their thread pool will throw an exception).
68     status_t status = service->registerAsService(kEnumeratorServiceName);
69     if (status == OK) {
70         LOG(DEBUG) << kEnumeratorServiceName << " is ready.";
71         joinRpcThreadpool();
72     } else {
73         LOG(ERROR) << "Could not register service " << kEnumeratorServiceName
74                    << " (" << status << ").";
75     }
76 
77     // Exit a uevent handler thread.
78     running = false;
79     if (ueventHandler.joinable()) {
80         ueventHandler.join();
81     }
82 
83     // In normal operation, we don't expect the thread pool to exit
84     LOG(ERROR) << "EVS Hardware Enumerator is shutting down";
85     return 1;
86 }
87