blob: efe4d920eaac0c5082a98e385ede17860b5fd65c [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
Martijn Coenene76c7a22016-11-22 14:53:47 +010017#include <hidl/HidlTransportSupport.h>
18#include <sys/wait.h>
Steven Moreland4f6935c2016-10-19 12:45:06 -070019#include <utils/Log.h>
Steven Moreland4f6935c2016-10-19 12:45:06 -070020#include <utils/Errors.h>
21#include <utils/StrongPointer.h>
22
23#ifndef ANDROID_HIDL_LEGACY_SUPPORT_H
24#define ANDROID_HIDL_LEGACY_SUPPORT_H
25
26namespace android {
27namespace hardware {
28
Steven Moreland3903f462017-01-13 12:57:00 -080029namespace details {
30bool blockingHalBinderizationEnabled();
31void blockIfBinderizationDisabled(const std::string& interface,
32 const std::string& instance);
33} // namespace details
34
Steven Moreland4f6935c2016-10-19 12:45:06 -070035/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080036 * Registers passthrough service implementation.
Steven Moreland4f6935c2016-10-19 12:45:06 -070037 */
38template<class Interface>
Steven Morelandd51a3e12017-03-19 14:20:47 -070039__attribute__((warn_unused_result))
Chris Phoenixd403ab32017-01-11 18:00:19 -080040status_t registerPassthroughServiceImplementation(
Steven Moreland3903f462017-01-13 12:57:00 -080041 std::string name = "default") {
42 // TODO(b/34274385)
43 // If binderization is enabled, we should start up. Otherwise, wait around.
44 // If we return/kill ourselves, we will just be restarted by init. This
45 // function is only called from thin wrapping services, so blocking won't
46 // stop anything important from happening.
47 details::blockIfBinderizationDisabled(Interface::descriptor, name);
48
Steven Moreland4f6935c2016-10-19 12:45:06 -070049 sp<Interface> service = Interface::getService(name, true /* getStub */);
50
51 if (service == nullptr) {
Steven Morelandf1b70282017-03-22 13:57:34 -070052 ALOGE("Could not get passthrough implementation for %s/%s.",
53 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070054 return EXIT_FAILURE;
55 }
56
Steven Morelandf1b70282017-03-22 13:57:34 -070057 LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!",
58 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070059
60 status_t status = service->registerAsService(name);
61
62 if (status == OK) {
Steven Morelandf1b70282017-03-22 13:57:34 -070063 ALOGI("Registration complete for %s/%s.",
64 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070065 } else {
Steven Morelandf1b70282017-03-22 13:57:34 -070066 ALOGE("Could not register service %s/%s (%d).",
67 Interface::descriptor, name.c_str(), status);
Steven Moreland4f6935c2016-10-19 12:45:06 -070068 }
69
Mikhail Naganov965aa982016-11-11 10:03:21 -080070 return status;
71}
72
73/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080074 * Creates default passthrough service implementation. This method never returns.
75 *
76 * Return value is exit status.
77 */
78template<class Interface>
Steven Morelandd51a3e12017-03-19 14:20:47 -070079__attribute__((warn_unused_result))
80status_t defaultPassthroughServiceImplementation(std::string name,
Chris Phoenixd403ab32017-01-11 18:00:19 -080081 size_t maxThreads = 1) {
Martijn Coenene76c7a22016-11-22 14:53:47 +010082 configureRpcThreadpool(maxThreads, true);
Steven Morelandfd9311c2017-01-23 20:37:51 -080083 status_t result = registerPassthroughServiceImplementation<Interface>(name);
84
85 if (result != OK) {
86 return result;
87 }
88
Martijn Coenene76c7a22016-11-22 14:53:47 +010089 joinRpcThreadpool();
90 return 0;
Mikhail Naganov965aa982016-11-11 10:03:21 -080091}
Steven Moreland05865142017-02-07 10:15:50 -080092template<class Interface>
Steven Morelandd51a3e12017-03-19 14:20:47 -070093__attribute__((warn_unused_result))
94status_t defaultPassthroughServiceImplementation(size_t maxThreads = 1) {
Steven Moreland05865142017-02-07 10:15:50 -080095 return defaultPassthroughServiceImplementation<Interface>("default", maxThreads);
96}
Mikhail Naganov965aa982016-11-11 10:03:21 -080097
Steven Moreland4f6935c2016-10-19 12:45:06 -070098} // namespace hardware
99} // namespace android
100
Mikhail Naganov965aa982016-11-11 10:03:21 -0800101#endif // ANDROID_HIDL_LEGACY_SUPPORT_H