LegacySupport.h revision d51a3e18d63e722973e8784265a7e6f3ac77350a
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 <hidl/HidlTransportSupport.h>
18#include <sys/wait.h>
19#include <utils/Log.h>
20#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
29namespace details {
30bool blockingHalBinderizationEnabled();
31void blockIfBinderizationDisabled(const std::string& interface,
32                                  const std::string& instance);
33} // namespace details
34
35/**
36 * Registers passthrough service implementation.
37 */
38template<class Interface>
39__attribute__((warn_unused_result))
40status_t registerPassthroughServiceImplementation(
41        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
49    sp<Interface> service = Interface::getService(name, true /* getStub */);
50
51    if (service == nullptr) {
52        ALOGE("Could not get passthrough implementation for %s.", name.c_str());
53        return EXIT_FAILURE;
54    }
55
56    LOG_FATAL_IF(service->isRemote(), "Implementation of %s is remote!", name.c_str());
57
58    status_t status = service->registerAsService(name);
59
60    if (status == OK) {
61        ALOGI("Registration complete for %s.", name.c_str());
62    } else {
63        ALOGE("Could not register service %s (%d).", name.c_str(), status);
64    }
65
66    return status;
67}
68
69/**
70 * Creates default passthrough service implementation. This method never returns.
71 *
72 * Return value is exit status.
73 */
74template<class Interface>
75__attribute__((warn_unused_result))
76status_t defaultPassthroughServiceImplementation(std::string name,
77                                            size_t maxThreads = 1) {
78    configureRpcThreadpool(maxThreads, true);
79    status_t result = registerPassthroughServiceImplementation<Interface>(name);
80
81    if (result != OK) {
82        return result;
83    }
84
85    joinRpcThreadpool();
86    return 0;
87}
88template<class Interface>
89__attribute__((warn_unused_result))
90status_t defaultPassthroughServiceImplementation(size_t maxThreads = 1) {
91    return defaultPassthroughServiceImplementation<Interface>("default", maxThreads);
92}
93
94}  // namespace hardware
95}  // namespace android
96
97#endif  // ANDROID_HIDL_LEGACY_SUPPORT_H
98