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
29/**
30 * Registers passthrough service implementation.
31 */
32template<class Interface>
33__attribute__((warn_unused_result))
34status_t registerPassthroughServiceImplementation(
35        std::string name = "default") {
36    sp<Interface> service = Interface::getService(name, true /* getStub */);
37
38    if (service == nullptr) {
39        ALOGE("Could not get passthrough implementation for %s/%s.",
40            Interface::descriptor, name.c_str());
41        return EXIT_FAILURE;
42    }
43
44    LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!",
45            Interface::descriptor, name.c_str());
46
47    status_t status = service->registerAsService(name);
48
49    if (status == OK) {
50        ALOGI("Registration complete for %s/%s.",
51            Interface::descriptor, name.c_str());
52    } else {
53        ALOGE("Could not register service %s/%s (%d).",
54            Interface::descriptor, name.c_str(), status);
55    }
56
57    return status;
58}
59
60/**
61 * Creates default passthrough service implementation. This method never returns.
62 *
63 * Return value is exit status.
64 */
65template<class Interface>
66__attribute__((warn_unused_result))
67status_t defaultPassthroughServiceImplementation(std::string name,
68                                            size_t maxThreads = 1) {
69    configureRpcThreadpool(maxThreads, true);
70    status_t result = registerPassthroughServiceImplementation<Interface>(name);
71
72    if (result != OK) {
73        return result;
74    }
75
76    joinRpcThreadpool();
77    return 0;
78}
79template<class Interface>
80__attribute__((warn_unused_result))
81status_t defaultPassthroughServiceImplementation(size_t maxThreads = 1) {
82    return defaultPassthroughServiceImplementation<Interface>("default", maxThreads);
83}
84
85}  // namespace hardware
86}  // namespace android
87
88#endif  // ANDROID_HIDL_LEGACY_SUPPORT_H
89