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 <string>
18 
19 #include <hidl/HidlLazyUtils.h>
20 #include <hidl/HidlTransportSupport.h>
21 #include <sys/wait.h>
22 #include <utils/Errors.h>
23 #include <utils/Log.h>
24 #include <utils/StrongPointer.h>
25 
26 #pragma once
27 
28 namespace android {
29 namespace hardware {
30 namespace details {
31 
32 using RegisterServiceCb =
33         std::function<status_t(const sp<::android::hidl::base::V1_0::IBase>&, const std::string&)>;
34 
35 __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
36         const std::string& interfaceName, const std::string& expectInterfaceName,
37         RegisterServiceCb registerServiceCb, const std::string& serviceName = "default");
38 
39 }  // namespace details
40 
41 /**
42  * Registers passthrough service implementation.
43  */
44 __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
45         const std::string& interfaceName, const std::string& expectInterfaceName,
46         const std::string& serviceName);
47 
48 inline __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
49         const std::string& interfaceName, const std::string& serviceName = "default") {
50     return registerPassthroughServiceImplementation(interfaceName, interfaceName, serviceName);
51 }
52 
53 template <class Interface, class ExpectInterface = Interface>
54 __attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
55         const std::string& name = "default") {
56     return registerPassthroughServiceImplementation(Interface::descriptor,
57                                                     ExpectInterface::descriptor, name);
58 }
59 
60 /**
61  * Creates default passthrough service implementation. This method never returns.
62  *
63  * Return value is exit status.
64  */
65 template <class Interface, class ExpectInterface = Interface>
66 __attribute__((warn_unused_result)) status_t defaultPassthroughServiceImplementation(
67         const std::string& name, size_t maxThreads = 1) {
68     configureRpcThreadpool(maxThreads, true);
69     status_t result = registerPassthroughServiceImplementation<Interface, ExpectInterface>(name);
70 
71     if (result != OK) {
72         return result;
73     }
74 
75     joinRpcThreadpool();
76     return UNKNOWN_ERROR;
77 }
78 template <class Interface, class ExpectInterface = Interface>
79 __attribute__((warn_unused_result)) status_t defaultPassthroughServiceImplementation(
80         size_t maxThreads = 1) {
81     return defaultPassthroughServiceImplementation<Interface, ExpectInterface>("default",
82                                                                                maxThreads);
83 }
84 
85 /**
86  * Registers a passthrough service implementation that exits when there are 0 clients.
87  *
88  * If this function is called multiple times to register different services, then this process will
89  * only exit once all services have 0 clients. This function does not know about clients registered
90  * through registerPassthroughServiceImplementation, so if that function is used in conjunction with
91  * this one, the process may exit while a client is still using the HAL.
92  */
93 template <class Interface, class ExpectInterface = Interface>
94 __attribute__((warn_unused_result)) status_t registerLazyPassthroughServiceImplementation(
95         const std::string& name = "default") {
96     return details::registerPassthroughServiceImplementation(
97             Interface::descriptor, ExpectInterface::descriptor,
98             [](const sp<::android::hidl::base::V1_0::IBase>& service, const std::string& name) {
99                 using android::hardware::LazyServiceRegistrar;
100                 return LazyServiceRegistrar::getInstance().registerService(service, name);
101             },
102             name);
103 }
104 
105 /**
106  * Creates default passthrough service implementation that exits when there are 0 clients. This
107  * method never returns.
108  *
109  * Return value is exit status.
110  */
111 template <class Interface, class ExpectInterface = Interface>
112 __attribute__((warn_unused_result)) status_t defaultLazyPassthroughServiceImplementation(
113         const std::string& name, size_t maxThreads = 1) {
114     configureRpcThreadpool(maxThreads, true);
115     status_t result =
116             registerLazyPassthroughServiceImplementation<Interface, ExpectInterface>(name);
117 
118     if (result != OK) {
119         return result;
120     }
121 
122     joinRpcThreadpool();
123     return UNKNOWN_ERROR;
124 }
125 template <class Interface, class ExpectInterface = Interface>
126 __attribute__((warn_unused_result)) status_t defaultLazyPassthroughServiceImplementation(
127         size_t maxThreads = 1) {
128     return defaultLazyPassthroughServiceImplementation<Interface, ExpectInterface>("default",
129                                                                                    maxThreads);
130 }
131 
132 }  // namespace hardware
133 }  // namespace android
134