Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [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 | |
| 17 | #ifndef ANDROID_HIDL_TRANSPORT_SUPPORT_H |
| 18 | #define ANDROID_HIDL_TRANSPORT_SUPPORT_H |
| 19 | |
Martijn Coenen | 81ef4da | 2017-04-21 16:21:31 -0700 | [diff] [blame] | 20 | #include <android/hidl/base/1.0/IBase.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 21 | #include <hidl/HidlBinderSupport.h> |
Yifan Hong | b2c9c75 | 2016-12-07 15:15:41 -0800 | [diff] [blame] | 22 | #include <hidl/HidlSupport.h> |
Martijn Coenen | 12f04d9 | 2016-12-07 17:29:41 +0100 | [diff] [blame] | 23 | #include <hidl/HidlTransportUtils.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | |
Martijn Coenen | e76c7a2 | 2016-11-22 14:53:47 +0100 | [diff] [blame] | 28 | /* Configures the threadpool used for handling incoming RPC calls in this process. |
| 29 | * |
| 30 | * This method MUST be called before interacting with any HIDL interfaces, |
| 31 | * including the IFoo::getService and IFoo::registerAsService methods. |
| 32 | * |
| 33 | * @param maxThreads maximum number of threads in this process |
| 34 | * @param callerWillJoin whether the caller will join the threadpool later. |
| 35 | * |
| 36 | * Note that maxThreads must include the caller thread if callerWillJoin is true; |
| 37 | * |
| 38 | * If you want to create a threadpool of 5 threads, without the caller ever joining: |
| 39 | * configureRpcThreadPool(5, false); |
| 40 | * If you want to create a threadpool of 1 thread, with the caller joining: |
| 41 | * configureRpcThreadPool(1, true); // transport won't launch any threads by itself |
| 42 | * |
| 43 | */ |
Steven Moreland | 11a732a | 2017-03-07 17:44:17 -0800 | [diff] [blame] | 44 | void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin); |
Martijn Coenen | e76c7a2 | 2016-11-22 14:53:47 +0100 | [diff] [blame] | 45 | |
| 46 | /* Joins a threadpool that you configured earlier with |
| 47 | * configureRpcThreadPool(x, true); |
| 48 | */ |
Steven Moreland | 11a732a | 2017-03-07 17:44:17 -0800 | [diff] [blame] | 49 | void joinRpcThreadpool(); |
Martijn Coenen | e76c7a2 | 2016-11-22 14:53:47 +0100 | [diff] [blame] | 50 | |
Martijn Coenen | 81ef4da | 2017-04-21 16:21:31 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Sets a minimum scheduler policy for all transactions coming into this |
| 53 | * service. |
| 54 | * |
| 55 | * This method MUST be called before passing this service to another process |
| 56 | * and/or registering it with registerAsService(). |
| 57 | * |
| 58 | * @param service the service to set the policy for |
| 59 | * @param policy scheduler policy as defined in linux UAPI |
| 60 | * @param priority priority. [-20..19] for SCHED_NORMAL, [1..99] for RT |
| 61 | */ |
| 62 | bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service, |
| 63 | int policy, int priority); |
| 64 | |
Steven Moreland | e67a676 | 2017-05-16 14:53:28 -0700 | [diff] [blame^] | 65 | template <typename ILeft, typename IRight> |
| 66 | bool interfacesEqual(sp<ILeft> left, sp<IRight> right) { |
| 67 | if (left == nullptr || right == nullptr || !left->isRemote() || !right->isRemote()) { |
| 68 | return left == right; |
| 69 | } |
| 70 | |
| 71 | return toBinder<ILeft>(left) == toBinder<IRight>(right); |
| 72 | } |
| 73 | |
Yifan Hong | 8d498ef | 2017-03-13 17:47:10 -0700 | [diff] [blame] | 74 | namespace details { |
| 75 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 76 | // cast the interface IParent to IChild. |
Yifan Hong | 6cd7d98 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 77 | // Return nonnull if cast successful. |
| 78 | // Return nullptr if: |
| 79 | // 1. parent is null |
| 80 | // 2. cast failed because IChild is not a child type of IParent. |
| 81 | // 3. !emitError, calling into parent fails. |
| 82 | // Return an error Return object if: |
| 83 | // 1. emitError, calling into parent fails. |
Steven Moreland | e67a676 | 2017-05-16 14:53:28 -0700 | [diff] [blame^] | 84 | template <typename IChild, typename IParent, typename BpChild> |
| 85 | Return<sp<IChild>> castInterface(sp<IParent> parent, const char* childIndicator, bool emitError) { |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 86 | if (parent.get() == nullptr) { |
| 87 | // casts always succeed with nullptrs. |
| 88 | return nullptr; |
| 89 | } |
Yifan Hong | 6cd7d98 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 90 | Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError); |
| 91 | if (!canCastRet.isOk()) { |
| 92 | // call fails, propagate the error if emitError |
| 93 | return emitError |
| 94 | ? details::StatusOf<bool, sp<IChild>>(canCastRet) |
| 95 | : Return<sp<IChild>>(sp<IChild>(nullptr)); |
| 96 | } |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 97 | |
Yifan Hong | 6cd7d98 | 2017-03-29 03:36:07 -0700 | [diff] [blame] | 98 | if (!canCastRet) { |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 99 | return sp<IChild>(nullptr); // cast failed. |
| 100 | } |
| 101 | // TODO b/32001926 Needs to be fixed for socket mode. |
| 102 | if (parent->isRemote()) { |
| 103 | // binderized mode. Got BpChild. grab the remote and wrap it. |
Steven Moreland | e67a676 | 2017-05-16 14:53:28 -0700 | [diff] [blame^] | 104 | return sp<IChild>(new BpChild(toBinder<IParent>(parent))); |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 105 | } |
| 106 | // Passthrough mode. Got BnChild and BsChild. |
| 107 | return sp<IChild>(static_cast<IChild *>(parent.get())); |
| 108 | } |
| 109 | |
Yifan Hong | 8d498ef | 2017-03-13 17:47:10 -0700 | [diff] [blame] | 110 | } // namespace details |
| 111 | |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 112 | } // namespace hardware |
| 113 | } // namespace android |
| 114 | |
| 115 | |
| 116 | #endif // ANDROID_HIDL_TRANSPORT_SUPPORT_H |