blob: c3a30318c5532c974a0adc705e60bd47b10a2122 [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 */
16#include <hidl/HidlTransportSupport.h>
Steven Moreland11a732a2017-03-07 17:44:17 -080017#include <hidl/HidlBinderSupport.h>
18
Yifan Honga8b6eab2017-11-22 22:15:39 -080019#include <android/hidl/manager/1.0/IServiceManager.h>
20
Steven Moreland11a732a2017-03-07 17:44:17 -080021namespace android {
22namespace hardware {
23
24void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin) {
25 // TODO(b/32756130) this should be transport-dependent
26 configureBinderRpcThreadpool(maxThreads, callerWillJoin);
27}
28void joinRpcThreadpool() {
29 // TODO(b/32756130) this should be transport-dependent
30 joinBinderRpcThreadpool();
31}
32
Martijn Coenen3f5ac4c2017-11-27 15:09:28 -080033int setupTransportPolling() {
34 return setupBinderPolling();
35}
36
37status_t handleTransportPoll(int /*fd*/) {
38 return handleBinderPoll();
39}
40
Steven Moreland399533d2019-01-07 14:13:07 -080041// TODO(b/122472540): only store one data item per object
42template <typename V>
43static void pruneMapLocked(ConcurrentMap<wp<::android::hidl::base::V1_0::IBase>, V>& map) {
44 using ::android::hidl::base::V1_0::IBase;
45
46 std::vector<wp<IBase>> toDelete;
47 for (const auto& kv : map) {
48 if (kv.first.promote() == nullptr) {
49 toDelete.push_back(kv.first);
50 }
51 }
52 for (const auto& k : toDelete) {
53 map.eraseLocked(k);
54 }
55}
56
Martijn Coenen81ef4da2017-04-21 16:21:31 -070057bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
58 int policy, int priority) {
59 if (service->isRemote()) {
60 ALOGE("Can't set scheduler policy on remote service.");
61 return false;
62 }
63
64 if (policy != SCHED_NORMAL && policy != SCHED_FIFO && policy != SCHED_RR) {
65 ALOGE("Invalid scheduler policy %d", policy);
66 return false;
67 }
68
69 if (policy == SCHED_NORMAL && (priority < -20 || priority > 19)) {
70 ALOGE("Invalid priority for SCHED_NORMAL: %d", priority);
71 return false;
72 } else if (priority < 1 || priority > 99) {
73 ALOGE("Invalid priority for real-time policy: %d", priority);
74 return false;
75 }
76
77 details::gServicePrioMap.set(service, { policy, priority });
78
79 return true;
Steven Moreland11a732a2017-03-07 17:44:17 -080080}
Martijn Coenen81ef4da2017-04-21 16:21:31 -070081
Steven Moreland399533d2019-01-07 14:13:07 -080082bool setRequestingSid(const sp<::android::hidl::base::V1_0::IBase>& service, bool requesting) {
83 if (service->isRemote()) {
84 ALOGE("Can't set requesting sid on remote service.");
85 return false;
86 }
87
88 // 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.
91 std::unique_lock<std::mutex> lock = details::gServiceSidMap.lock();
92 pruneMapLocked(details::gServiceSidMap);
93 details::gServiceSidMap.setLocked(service, requesting);
94
95 return true;
96}
97
Yifan Hong7a4b7562017-11-20 11:36:51 -080098namespace details {
99int32_t getPidIfSharable() {
100#if LIBHIDL_TARGET_DEBUGGABLE
101 return getpid();
102#else
103 using android::hidl::manager::V1_0::IServiceManager;
Yifan Honga8b6eab2017-11-22 22:15:39 -0800104 return static_cast<int32_t>(IServiceManager::PidConstant::NO_PID);
Yifan Hong7a4b7562017-11-20 11:36:51 -0800105#endif
106}
107} // namespace details
108
Steven Morelanda15479f2017-10-06 16:06:26 -0700109} // namespace hardware
110} // namespace android