blob: 16f47c6f602e38bb05382a18ec27da78396ad81f [file] [log] [blame]
Steven Moreland11a732a2017-03-07 17:44:17 -08001/*
2 * Copyright (C) 2017 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 */
Steven Moreland92119512018-08-08 20:01:21 +000016#include <hidl/HidlBinderSupport.h>
Steven Morelandb9173182018-08-08 20:06:08 +000017#include <hidl/HidlTransportSupport.h>
18#include <hidl/Static.h>
Steven Moreland11a732a2017-03-07 17:44:17 -080019
Steven Morelandb9173182018-08-08 20:06:08 +000020#include <android-base/logging.h>
Yifan Honga8b6eab2017-11-22 22:15:39 -080021#include <android/hidl/manager/1.0/IServiceManager.h>
22
Steven Moreland11a732a2017-03-07 17:44:17 -080023namespace android {
24namespace hardware {
25
Steven Moreland87d991b2018-12-12 15:56:33 -080026using ::android::hidl::base::V1_0::IBase;
27
Steven Moreland11a732a2017-03-07 17:44:17 -080028void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
29 // TODO(b/32756130) this should be transport-dependent
30 configureBinderRpcThreadpool(maxThreads, callerWillJoin);
31}
32void joinRpcThreadpool() {
33 // TODO(b/32756130) this should be transport-dependent
34 joinBinderRpcThreadpool();
35}
36
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -080037int setupTransportPolling() {
38 return setupBinderPolling();
39}
40
41status_t handleTransportPoll(int /*fd*/) {
42 return handleBinderPoll();
43}
44
Steven Moreland399533d2019-01-07 14:13:07 -080045// TODO(b/122472540): only store one data item per object
46template <typename V>
47static void pruneMapLocked(ConcurrentMap<wp<::android::hidl::base::V1_0::IBase>, V>& map) {
48 using ::android::hidl::base::V1_0::IBase;
49
50 std::vector<wp<IBase>> toDelete;
51 for (const auto& kv : map) {
52 if (kv.first.promote() == nullptr) {
53 toDelete.push_back(kv.first);
54 }
55 }
56 for (const auto& k : toDelete) {
57 map.eraseLocked(k);
58 }
59}
60
Martijn Coenen81ef4da2017-04-21 16:21:31 -070061bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
62 int policy, int priority) {
63 if (service->isRemote()) {
Steven Morelandb9173182018-08-08 20:06:08 +000064 LOG(ERROR) << "Can't set scheduler policy on remote service.";
Martijn Coenen81ef4da2017-04-21 16:21:31 -070065 return false;
66 }
67
Steven Morelandb7798be2018-09-19 13:17:37 -070068 switch (policy) {
69 case SCHED_NORMAL: {
70 if (priority < -20 || priority > 19) {
71 LOG(ERROR) << "Invalid priority for SCHED_NORMAL: " << priority;
72 return false;
73 }
74 } break;
75 case SCHED_RR:
76 case SCHED_FIFO: {
77 if (priority < 1 || priority > 99) {
78 LOG(ERROR) << "Invalid priority for " << policy << " policy: " << priority;
79 return false;
80 }
81 } break;
82 default: {
83 LOG(ERROR) << "Invalid scheduler policy " << policy;
84 return false;
85 }
Martijn Coenen81ef4da2017-04-21 16:21:31 -070086 }
87
Steven Moreland49ccc382018-07-24 12:18:36 -070088 // Due to ABI considerations, IBase cannot have a destructor to clean this up.
89 // So, because this API is so infrequently used, (expected to be usually only
90 // one time for a process, but it can be more), we are cleaning it up here.
Steven Moreland49ccc382018-07-24 12:18:36 -070091 std::unique_lock<std::mutex> lock = details::gServicePrioMap.lock();
Steven Moreland070ec132019-04-22 12:03:27 -070092 pruneMapLocked(details::gServicePrioMap);
Steven Moreland49ccc382018-07-24 12:18:36 -070093 details::gServicePrioMap.setLocked(service, {policy, priority});
Martijn Coenen81ef4da2017-04-21 16:21:31 -070094
95 return true;
Steven Moreland11a732a2017-03-07 17:44:17 -080096}
Martijn Coenen81ef4da2017-04-21 16:21:31 -070097
Steven Moreland399533d2019-01-07 14:13:07 -080098bool setRequestingSid(const sp<::android::hidl::base::V1_0::IBase>& service, bool requesting) {
99 if (service->isRemote()) {
100 ALOGE("Can't set requesting sid on remote service.");
101 return false;
102 }
103
104 // Due to ABI considerations, IBase cannot have a destructor to clean this up.
105 // So, because this API is so infrequently used, (expected to be usually only
106 // one time for a process, but it can be more), we are cleaning it up here.
107 std::unique_lock<std::mutex> lock = details::gServiceSidMap.lock();
108 pruneMapLocked(details::gServiceSidMap);
109 details::gServiceSidMap.setLocked(service, requesting);
110
111 return true;
112}
113
Steven Moreland87d991b2018-12-12 15:56:33 -0800114bool interfacesEqual(const sp<IBase>& left, const sp<IBase>& right) {
115 if (left == nullptr || right == nullptr || !left->isRemote() || !right->isRemote()) {
116 return left == right;
117 }
118 return getOrCreateCachedBinder(left.get()) == getOrCreateCachedBinder(right.get());
119}
120
Yifan Hong7a4b7562017-11-20 11:36:51 -0800121namespace details {
122int32_t getPidIfSharable() {
123#if LIBHIDL_TARGET_DEBUGGABLE
124 return getpid();
125#else
126 using android::hidl::manager::V1_0::IServiceManager;
Yifan Honga8b6eab2017-11-22 22:15:39 -0800127 return static_cast<int32_t>(IServiceManager::PidConstant::NO_PID);
Yifan Hong7a4b7562017-11-20 11:36:51 -0800128#endif
129}
130} // namespace details
131
Steven Morelanda15479f2017-10-06 16:06:26 -0700132} // namespace hardware
133} // namespace android