blob: 573a793880f674bffac7ffea517751cb2d11f5d9 [file] [log] [blame]
Steven Moreland4f6935c2016-10-19 12:45:06 -07001/*
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
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -070017#include <hidl/HidlLazyUtils.h>
Martijn Coenene76c7a22016-11-22 14:53:47 +010018#include <hidl/HidlTransportSupport.h>
19#include <sys/wait.h>
Steven Moreland4f6935c2016-10-19 12:45:06 -070020#include <utils/Errors.h>
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -070021#include <utils/Log.h>
Steven Moreland4f6935c2016-10-19 12:45:06 -070022#include <utils/StrongPointer.h>
23
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -070024#pragma once
Steven Moreland4f6935c2016-10-19 12:45:06 -070025
26namespace android {
27namespace hardware {
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -070028namespace details {
29template <class Interface, typename Func>
30__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
31 Func registerServiceCb, const std::string& name = "default") {
Steven Moreland4f6935c2016-10-19 12:45:06 -070032 sp<Interface> service = Interface::getService(name, true /* getStub */);
33
34 if (service == nullptr) {
Steven Morelandf1b70282017-03-22 13:57:34 -070035 ALOGE("Could not get passthrough implementation for %s/%s.",
36 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070037 return EXIT_FAILURE;
38 }
39
Steven Morelandf1b70282017-03-22 13:57:34 -070040 LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!",
41 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070042
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -070043 status_t status = registerServiceCb(service, name);
Steven Moreland4f6935c2016-10-19 12:45:06 -070044
45 if (status == OK) {
Steven Morelandf1b70282017-03-22 13:57:34 -070046 ALOGI("Registration complete for %s/%s.",
47 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070048 } else {
Steven Morelandf1b70282017-03-22 13:57:34 -070049 ALOGE("Could not register service %s/%s (%d).",
50 Interface::descriptor, name.c_str(), status);
Steven Moreland4f6935c2016-10-19 12:45:06 -070051 }
52
Mikhail Naganov965aa982016-11-11 10:03:21 -080053 return status;
54}
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -070055} // namespace details
56
57/**
58 * Registers passthrough service implementation.
59 */
60template <class Interface>
61__attribute__((warn_unused_result)) status_t registerPassthroughServiceImplementation(
62 const std::string& name = "default") {
63 return details::registerPassthroughServiceImplementation<Interface>(
64 [](const sp<Interface>& service, const std::string& name) {
65 return service->registerAsService(name);
66 },
67 name);
68}
Mikhail Naganov965aa982016-11-11 10:03:21 -080069
70/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080071 * Creates default passthrough service implementation. This method never returns.
72 *
73 * Return value is exit status.
74 */
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -070075template <class Interface>
76__attribute__((warn_unused_result)) status_t defaultPassthroughServiceImplementation(
77 const std::string& name, size_t maxThreads = 1) {
Martijn Coenene76c7a22016-11-22 14:53:47 +010078 configureRpcThreadpool(maxThreads, true);
Steven Morelandfd9311c2017-01-23 20:37:51 -080079 status_t result = registerPassthroughServiceImplementation<Interface>(name);
80
81 if (result != OK) {
82 return result;
83 }
84
Martijn Coenene76c7a22016-11-22 14:53:47 +010085 joinRpcThreadpool();
Steven Moreland03416d92017-08-15 09:16:16 -070086 return UNKNOWN_ERROR;
Mikhail Naganov965aa982016-11-11 10:03:21 -080087}
Steven Moreland05865142017-02-07 10:15:50 -080088template<class Interface>
Steven Morelandd51a3e12017-03-19 14:20:47 -070089__attribute__((warn_unused_result))
90status_t defaultPassthroughServiceImplementation(size_t maxThreads = 1) {
Steven Moreland05865142017-02-07 10:15:50 -080091 return defaultPassthroughServiceImplementation<Interface>("default", maxThreads);
92}
Mikhail Naganov965aa982016-11-11 10:03:21 -080093
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -070094/**
95 * Registers a passthrough service implementation that exits when there are 0 clients.
96 *
97 * If this function is called multiple times to register different services, then this process will
98 * only exit once all services have 0 clients. This function does not know about clients registered
99 * through registerPassthroughServiceImplementation, so if that function is used in conjuction with
100 * this one, the process may exit while a client is still using the HAL.
101 */
102template <class Interface>
103__attribute__((warn_unused_result)) status_t registerLazyPassthroughServiceImplementation(
104 const std::string& name = "default") {
105 // Make LazyServiceRegistrar static so that multiple calls to
106 // registerLazyPassthroughServiceImplementation work as expected: each HAL is registered and the
107 // process only exits once all HALs have 0 clients.
108 using android::hardware::LazyServiceRegistrar;
109 static auto serviceCounter(std::make_shared<LazyServiceRegistrar>());
110
111 return details::registerPassthroughServiceImplementation<Interface>(
112 [](const sp<Interface>& service, const std::string& name) {
113 return serviceCounter->registerServiceWithCallback(service, name);
114 },
115 name);
116}
117
118/**
119 * Creates default passthrough service implementation that exits when there are 0 clients. This
120 * method never returns.
121 *
122 * Return value is exit status.
123 */
124template <class Interface>
125__attribute__((warn_unused_result)) status_t defaultLazyPassthroughServiceImplementation(
126 const std::string& name, size_t maxThreads = 1) {
127 configureRpcThreadpool(maxThreads, true);
128 status_t result = registerLazyPassthroughServiceImplementation<Interface>(name);
129
130 if (result != OK) {
131 return result;
132 }
133
134 joinRpcThreadpool();
135 return UNKNOWN_ERROR;
136}
137template <class Interface>
138__attribute__((warn_unused_result)) status_t defaultLazyPassthroughServiceImplementation(
139 size_t maxThreads = 1) {
140 return defaultLazyPassthroughServiceImplementation<Interface>("default", maxThreads);
141}
142
Steven Moreland4f6935c2016-10-19 12:45:06 -0700143} // namespace hardware
144} // namespace android