blob: 2f0c3f3af47b63456d429bffc3dae70e63652f90 [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
29/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080030 * Registers passthrough service implementation.
Steven Moreland4f6935c2016-10-19 12:45:06 -070031 */
32template<class Interface>
Steven Morelandd51a3e12017-03-19 14:20:47 -070033__attribute__((warn_unused_result))
Chris Phoenixd403ab32017-01-11 18:00:19 -080034status_t registerPassthroughServiceImplementation(
Steven Moreland3903f462017-01-13 12:57:00 -080035 std::string name = "default") {
Steven Moreland4f6935c2016-10-19 12:45:06 -070036 sp<Interface> service = Interface::getService(name, true /* getStub */);
37
38 if (service == nullptr) {
Steven Morelandf1b70282017-03-22 13:57:34 -070039 ALOGE("Could not get passthrough implementation for %s/%s.",
40 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070041 return EXIT_FAILURE;
42 }
43
Steven Morelandf1b70282017-03-22 13:57:34 -070044 LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!",
45 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070046
47 status_t status = service->registerAsService(name);
48
49 if (status == OK) {
Steven Morelandf1b70282017-03-22 13:57:34 -070050 ALOGI("Registration complete for %s/%s.",
51 Interface::descriptor, name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070052 } else {
Steven Morelandf1b70282017-03-22 13:57:34 -070053 ALOGE("Could not register service %s/%s (%d).",
54 Interface::descriptor, name.c_str(), status);
Steven Moreland4f6935c2016-10-19 12:45:06 -070055 }
56
Mikhail Naganov965aa982016-11-11 10:03:21 -080057 return status;
58}
59
60/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080061 * Creates default passthrough service implementation. This method never returns.
62 *
63 * Return value is exit status.
64 */
65template<class Interface>
Steven Morelandd51a3e12017-03-19 14:20:47 -070066__attribute__((warn_unused_result))
67status_t defaultPassthroughServiceImplementation(std::string name,
Chris Phoenixd403ab32017-01-11 18:00:19 -080068 size_t maxThreads = 1) {
Martijn Coenene76c7a22016-11-22 14:53:47 +010069 configureRpcThreadpool(maxThreads, true);
Steven Morelandfd9311c2017-01-23 20:37:51 -080070 status_t result = registerPassthroughServiceImplementation<Interface>(name);
71
72 if (result != OK) {
73 return result;
74 }
75
Martijn Coenene76c7a22016-11-22 14:53:47 +010076 joinRpcThreadpool();
77 return 0;
Mikhail Naganov965aa982016-11-11 10:03:21 -080078}
Steven Moreland05865142017-02-07 10:15:50 -080079template<class Interface>
Steven Morelandd51a3e12017-03-19 14:20:47 -070080__attribute__((warn_unused_result))
81status_t defaultPassthroughServiceImplementation(size_t maxThreads = 1) {
Steven Moreland05865142017-02-07 10:15:50 -080082 return defaultPassthroughServiceImplementation<Interface>("default", maxThreads);
83}
Mikhail Naganov965aa982016-11-11 10:03:21 -080084
Steven Moreland4f6935c2016-10-19 12:45:06 -070085} // namespace hardware
86} // namespace android
87
Mikhail Naganov965aa982016-11-11 10:03:21 -080088#endif // ANDROID_HIDL_LEGACY_SUPPORT_H