blob: 8fd17952f9e4d7b5ac927d1195f84c9a67c4f341 [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>
Mikhail Naganov965aa982016-11-11 10:03:21 -080033status_t registerPassthroughServiceImplementation(std::string name) {
Steven Moreland4f6935c2016-10-19 12:45:06 -070034 sp<Interface> service = Interface::getService(name, true /* getStub */);
35
36 if (service == nullptr) {
Mikhail Naganov965aa982016-11-11 10:03:21 -080037 ALOGE("Could not get passthrough implementation for %s.", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070038 return EXIT_FAILURE;
39 }
40
Mikhail Naganov965aa982016-11-11 10:03:21 -080041 LOG_FATAL_IF(service->isRemote(), "Implementation of %s is remote!", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070042
43 status_t status = service->registerAsService(name);
44
45 if (status == OK) {
Mikhail Naganov965aa982016-11-11 10:03:21 -080046 ALOGI("Registration complete for %s.", name.c_str());
Steven Moreland4f6935c2016-10-19 12:45:06 -070047 } else {
Mikhail Naganov965aa982016-11-11 10:03:21 -080048 ALOGE("Could not register service %s (%d).", name.c_str(), status);
Steven Moreland4f6935c2016-10-19 12:45:06 -070049 }
50
Mikhail Naganov965aa982016-11-11 10:03:21 -080051 return status;
52}
53
54/**
Mikhail Naganov965aa982016-11-11 10:03:21 -080055 * Creates default passthrough service implementation. This method never returns.
56 *
57 * Return value is exit status.
58 */
59template<class Interface>
Martijn Coenene76c7a22016-11-22 14:53:47 +010060int defaultPassthroughServiceImplementation(std::string name, size_t maxThreads = 1) {
61 configureRpcThreadpool(maxThreads, true);
Mikhail Naganov965aa982016-11-11 10:03:21 -080062 registerPassthroughServiceImplementation<Interface>(name);
Martijn Coenene76c7a22016-11-22 14:53:47 +010063 joinRpcThreadpool();
64 return 0;
Mikhail Naganov965aa982016-11-11 10:03:21 -080065}
66
Steven Moreland4f6935c2016-10-19 12:45:06 -070067} // namespace hardware
68} // namespace android
69
Mikhail Naganov965aa982016-11-11 10:03:21 -080070#endif // ANDROID_HIDL_LEGACY_SUPPORT_H