blob: 3cc23f34f6fcca4648f7cc7f4bf2bdebc269e313 [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>
Chris Phoenixd403ab32017-01-11 18:00:19 -080039status_t registerPassthroughServiceImplementation(
Steven Moreland3903f462017-01-13 12:57:00 -080040 std::string name = "default") {
41 // TODO(b/34274385)
42 // If binderization is enabled, we should start up. Otherwise, wait around.
43 // If we return/kill ourselves, we will just be restarted by init. This
44 // function is only called from thin wrapping services, so blocking won't
45 // stop anything important from happening.
46 details::blockIfBinderizationDisabled(Interface::descriptor, name);
47
Steven Moreland4f6935c2016-10-19 12:45:06 -070048 sp<Interface> service = Interface::getService(name, true /* getStub */);
49
50 if (service == nullptr) {
Mikhail Naganov965aa982016-11-11 10:03:21 -080051 ALOGE("Could not get passthrough implementation for %s.", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070052 return EXIT_FAILURE;
53 }
54
Mikhail Naganov965aa982016-11-11 10:03:21 -080055 LOG_FATAL_IF(service->isRemote(), "Implementation of %s is remote!", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070056
57 status_t status = service->registerAsService(name);
58
59 if (status == OK) {
Mikhail Naganov965aa982016-11-11 10:03:21 -080060 ALOGI("Registration complete for %s.", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070061 } else {
Mikhail Naganov965aa982016-11-11 10:03:21 -080062 ALOGE("Could not register service %s (%d).", name.c_str(), status);
Steven Moreland4f6935c2016-10-19 12:45:06 -070063 }
64
Mikhail Naganov965aa982016-11-11 10:03:21 -080065 return status;
66}
67
68/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080069 * Creates default passthrough service implementation. This method never returns.
70 *
71 * Return value is exit status.
72 */
73template<class Interface>
Steven Moreland05865142017-02-07 10:15:50 -080074int defaultPassthroughServiceImplementation(std::string name,
Chris Phoenixd403ab32017-01-11 18:00:19 -080075 size_t maxThreads = 1) {
Martijn Coenene76c7a22016-11-22 14:53:47 +010076 configureRpcThreadpool(maxThreads, true);
Steven Morelandfd9311c2017-01-23 20:37:51 -080077 status_t result = registerPassthroughServiceImplementation<Interface>(name);
78
79 if (result != OK) {
80 return result;
81 }
82
Martijn Coenene76c7a22016-11-22 14:53:47 +010083 joinRpcThreadpool();
84 return 0;
Mikhail Naganov965aa982016-11-11 10:03:21 -080085}
Steven Moreland05865142017-02-07 10:15:50 -080086template<class Interface>
87int defaultPassthroughServiceImplementation(size_t maxThreads = 1) {
88 return defaultPassthroughServiceImplementation<Interface>("default", maxThreads);
89}
Mikhail Naganov965aa982016-11-11 10:03:21 -080090
Steven Moreland4f6935c2016-10-19 12:45:06 -070091} // namespace hardware
92} // namespace android
93
Mikhail Naganov965aa982016-11-11 10:03:21 -080094#endif // ANDROID_HIDL_LEGACY_SUPPORT_H