blob: 0a9feee4ba61d8477f51b41c42bc966039fb08fb [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
Shibin George7860da32019-05-16 17:08:43 +053094// Make LazyServiceRegistrar static so that multiple calls to
95// registerLazyPassthroughServiceImplementation work as expected: each HAL is registered and the
96// process only exits once all HALs have 0 clients.
97static inline std::shared_ptr<LazyServiceRegistrar> getOrCreateLazyServiceRegistrar() {
98 using android::hardware::LazyServiceRegistrar;
99 static auto serviceCounter(std::make_shared<LazyServiceRegistrar>());
100 return serviceCounter;
101}
102
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -0700103/**
104 * Registers a passthrough service implementation that exits when there are 0 clients.
105 *
106 * If this function is called multiple times to register different services, then this process will
107 * only exit once all services have 0 clients. This function does not know about clients registered
Peter Kalauskas76900d52018-11-27 12:18:55 -0800108 * through registerPassthroughServiceImplementation, so if that function is used in conjunction with
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -0700109 * this one, the process may exit while a client is still using the HAL.
110 */
111template <class Interface>
112__attribute__((warn_unused_result)) status_t registerLazyPassthroughServiceImplementation(
113 const std::string& name = "default") {
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -0700114 return details::registerPassthroughServiceImplementation<Interface>(
115 [](const sp<Interface>& service, const std::string& name) {
Shibin George7860da32019-05-16 17:08:43 +0530116 return getOrCreateLazyServiceRegistrar()->registerService(service, name);
Peter Kalauskasdb0ab7e2018-10-30 14:40:10 -0700117 },
118 name);
119}
120
121/**
122 * Creates default passthrough service implementation that exits when there are 0 clients. This
123 * method never returns.
124 *
125 * Return value is exit status.
126 */
127template <class Interface>
128__attribute__((warn_unused_result)) status_t defaultLazyPassthroughServiceImplementation(
129 const std::string& name, size_t maxThreads = 1) {
130 configureRpcThreadpool(maxThreads, true);
131 status_t result = registerLazyPassthroughServiceImplementation<Interface>(name);
132
133 if (result != OK) {
134 return result;
135 }
136
137 joinRpcThreadpool();
138 return UNKNOWN_ERROR;
139}
140template <class Interface>
141__attribute__((warn_unused_result)) status_t defaultLazyPassthroughServiceImplementation(
142 size_t maxThreads = 1) {
143 return defaultLazyPassthroughServiceImplementation<Interface>("default", maxThreads);
144}
145
Steven Moreland4f6935c2016-10-19 12:45:06 -0700146} // namespace hardware
147} // namespace android