Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 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 | |
Martijn Coenen | e76c7a2 | 2016-11-22 14:53:47 +0100 | [diff] [blame] | 17 | #include <hidl/HidlTransportSupport.h> |
| 18 | #include <sys/wait.h> |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 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 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | |
Steven Moreland | 3903f46 | 2017-01-13 12:57:00 -0800 | [diff] [blame] | 29 | namespace details { |
| 30 | bool blockingHalBinderizationEnabled(); |
| 31 | void blockIfBinderizationDisabled(const std::string& interface, |
| 32 | const std::string& instance); |
| 33 | } // namespace details |
| 34 | |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 35 | /** |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 36 | * Registers passthrough service implementation. |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 37 | */ |
| 38 | template<class Interface> |
Chris Phoenix | d403ab3 | 2017-01-11 18:00:19 -0800 | [diff] [blame] | 39 | status_t registerPassthroughServiceImplementation( |
Steven Moreland | 3903f46 | 2017-01-13 12:57:00 -0800 | [diff] [blame] | 40 | std::string name = "default") { |
| 41 | // TODO(b/34274385) |
| 42 | // If binderization is enabled, we should start up. Otherwise, wait around. |
| 43 | // If we return/kill ourselves, we will just be restarted by init. This |
| 44 | // function is only called from thin wrapping services, so blocking won't |
| 45 | // stop anything important from happening. |
| 46 | details::blockIfBinderizationDisabled(Interface::descriptor, name); |
| 47 | |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 48 | sp<Interface> service = Interface::getService(name, true /* getStub */); |
| 49 | |
| 50 | if (service == nullptr) { |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 51 | ALOGE("Could not get passthrough implementation for %s.", name.c_str()); |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 52 | return EXIT_FAILURE; |
| 53 | } |
| 54 | |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 55 | LOG_FATAL_IF(service->isRemote(), "Implementation of %s is remote!", name.c_str()); |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 56 | |
| 57 | status_t status = service->registerAsService(name); |
| 58 | |
| 59 | if (status == OK) { |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 60 | ALOGI("Registration complete for %s.", name.c_str()); |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 61 | } else { |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 62 | ALOGE("Could not register service %s (%d).", name.c_str(), status); |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 65 | return status; |
| 66 | } |
| 67 | |
| 68 | /** |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 69 | * Creates default passthrough service implementation. This method never returns. |
| 70 | * |
| 71 | * Return value is exit status. |
| 72 | */ |
| 73 | template<class Interface> |
Steven Moreland | 0586514 | 2017-02-07 10:15:50 -0800 | [diff] [blame] | 74 | int defaultPassthroughServiceImplementation(std::string name, |
Chris Phoenix | d403ab3 | 2017-01-11 18:00:19 -0800 | [diff] [blame] | 75 | size_t maxThreads = 1) { |
Martijn Coenen | e76c7a2 | 2016-11-22 14:53:47 +0100 | [diff] [blame] | 76 | configureRpcThreadpool(maxThreads, true); |
Steven Moreland | fd9311c | 2017-01-23 20:37:51 -0800 | [diff] [blame] | 77 | status_t result = registerPassthroughServiceImplementation<Interface>(name); |
| 78 | |
| 79 | if (result != OK) { |
| 80 | return result; |
| 81 | } |
| 82 | |
Martijn Coenen | e76c7a2 | 2016-11-22 14:53:47 +0100 | [diff] [blame] | 83 | joinRpcThreadpool(); |
| 84 | return 0; |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 85 | } |
Steven Moreland | 0586514 | 2017-02-07 10:15:50 -0800 | [diff] [blame] | 86 | template<class Interface> |
| 87 | int defaultPassthroughServiceImplementation(size_t maxThreads = 1) { |
| 88 | return defaultPassthroughServiceImplementation<Interface>("default", maxThreads); |
| 89 | } |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 90 | |
Steven Moreland | 4f6935c | 2016-10-19 12:45:06 -0700 | [diff] [blame] | 91 | } // namespace hardware |
| 92 | } // namespace android |
| 93 | |
Mikhail Naganov | 965aa98 | 2016-11-11 10:03:21 -0800 | [diff] [blame] | 94 | #endif // ANDROID_HIDL_LEGACY_SUPPORT_H |