blob: 2ff178522bce73853c87bcb34fecfb8e925a319e [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
17#include <utils/Log.h>
18
Steven Moreland4f6935c2016-10-19 12:45:06 -070019#include <hwbinder/IPCThreadState.h>
20#include <hwbinder/ProcessState.h>
21#include <utils/Errors.h>
22#include <utils/StrongPointer.h>
23
24#ifndef ANDROID_HIDL_LEGACY_SUPPORT_H
25#define ANDROID_HIDL_LEGACY_SUPPORT_H
26
27namespace android {
28namespace hardware {
29
30/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080031 * Registers passthrough service implementation.
Steven Moreland4f6935c2016-10-19 12:45:06 -070032 */
33template<class Interface>
Mikhail Naganov965aa982016-11-11 10:03:21 -080034status_t registerPassthroughServiceImplementation(std::string name) {
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/**
56 * Launches the RPC threadpool. This method never returns.
57 *
58 * Return value is exit status.
59 */
60inline int launchRpcServer(size_t maxThreads) {
61 ProcessState::self()->setThreadPoolMaxThreadCount(maxThreads);
Steven Moreland4f6935c2016-10-19 12:45:06 -070062 ProcessState::self()->startThreadPool();
63 IPCThreadState::self()->joinThreadPool();
64
65 return 0;
66}
67
Mikhail Naganov965aa982016-11-11 10:03:21 -080068/**
69 * Creates default passthrough service implementation. This method never returns.
70 *
71 * Return value is exit status.
72 */
73template<class Interface>
74int defaultPassthroughServiceImplementation(std::string name) {
75 registerPassthroughServiceImplementation<Interface>(name);
76 return launchRpcServer(0);
77}
78
Steven Moreland4f6935c2016-10-19 12:45:06 -070079} // namespace hardware
80} // namespace android
81
Mikhail Naganov965aa982016-11-11 10:03:21 -080082#endif // ANDROID_HIDL_LEGACY_SUPPORT_H