blob: fab8a1dc0c9bd4e0f2ecd306d7f72d4ef44353b7 [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>
Chris Phoenixd403ab32017-01-11 18:00:19 -080033status_t registerPassthroughServiceImplementation(
34 std::string name = "default") {
Steven Moreland4f6935c2016-10-19 12:45:06 -070035 sp<Interface> service = Interface::getService(name, true /* getStub */);
36
37 if (service == nullptr) {
Mikhail Naganov965aa982016-11-11 10:03:21 -080038 ALOGE("Could not get passthrough implementation for %s.", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070039 return EXIT_FAILURE;
40 }
41
Mikhail Naganov965aa982016-11-11 10:03:21 -080042 LOG_FATAL_IF(service->isRemote(), "Implementation of %s is remote!", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070043
44 status_t status = service->registerAsService(name);
45
46 if (status == OK) {
Mikhail Naganov965aa982016-11-11 10:03:21 -080047 ALOGI("Registration complete for %s.", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070048 } else {
Mikhail Naganov965aa982016-11-11 10:03:21 -080049 ALOGE("Could not register service %s (%d).", name.c_str(), status);
Steven Moreland4f6935c2016-10-19 12:45:06 -070050 }
51
Mikhail Naganov965aa982016-11-11 10:03:21 -080052 return status;
53}
54
55/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080056 * Creates default passthrough service implementation. This method never returns.
57 *
58 * Return value is exit status.
59 */
60template<class Interface>
Chris Phoenixd403ab32017-01-11 18:00:19 -080061int defaultPassthroughServiceImplementation(std::string name = "default",
62 size_t maxThreads = 1) {
Martijn Coenene76c7a22016-11-22 14:53:47 +010063 configureRpcThreadpool(maxThreads, true);
Steven Morelandfd9311c2017-01-23 20:37:51 -080064 status_t result = registerPassthroughServiceImplementation<Interface>(name);
65
66 if (result != OK) {
67 return result;
68 }
69
Martijn Coenene76c7a22016-11-22 14:53:47 +010070 joinRpcThreadpool();
71 return 0;
Mikhail Naganov965aa982016-11-11 10:03:21 -080072}
73
Steven Moreland4f6935c2016-10-19 12:45:06 -070074} // namespace hardware
75} // namespace android
76
Mikhail Naganov965aa982016-11-11 10:03:21 -080077#endif // ANDROID_HIDL_LEGACY_SUPPORT_H