summaryrefslogtreecommitdiff
path: root/transport/include/hidl/HidlTransportSupport.h
blob: a132bfa7a7b9073124c7cb4e73d0b3bef44b8e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_HIDL_TRANSPORT_SUPPORT_H
#define ANDROID_HIDL_TRANSPORT_SUPPORT_H

#include <android/hidl/base/1.0/IBase.h>
#include <hidl/HidlBinderSupport.h>
#include <hidl/HidlPassthroughSupport.h>
#include <hidl/HidlSupport.h>
#include <hidl/HidlTransportUtils.h>
#include <hidl/ServiceManagement.h>

namespace android {
namespace hardware {

/* Configures the threadpool used for handling incoming RPC calls in this process.
 *
 * This method MUST be called before interacting with any HIDL interfaces,
 * including the IFoo::getService and IFoo::registerAsService methods.
 *
 * @param maxThreads maximum number of threads in this process
 * @param callerWillJoin whether the caller will join the threadpool later.
 *
 * Note that maxThreads must include the caller thread if callerWillJoin is true;
 *
 * If you want to create a threadpool of 5 threads, without the caller ever joining:
 *   configureRpcThreadPool(5, false);
 * If you want to create a threadpool of 1 thread, with the caller joining:
 *   configureRpcThreadPool(1, true); // transport won't launch any threads by itself
 *
 */
void configureRpcThreadpool(size_t maxThreads, bool callerWillJoin);

/* Joins a threadpool that you configured earlier with
 * configureRpcThreadPool(x, true);
 */
void joinRpcThreadpool();

/**
 * Sets up the transport for use with (e)poll.
 *
 * Note that all currently supported transports can only be polled
 * from a single thread. When poll() on the returned fd returns,
 * the caller must call handleTransportPoll() to handle the result.
 *
 * @return the file descriptor to be used with (e)poll, or -1 in case of error.
 */
int setupTransportPolling();

/**
 * Handles transport work after poll() returns.
 *
 * @param fd returned from setupTransportPolling()
 *
 * @return OK when successful
 */
status_t handleTransportPoll(int fd);

/**
 * Sets a minimum scheduler policy for all transactions coming into this
 * service.
 *
 * This method MUST be called before passing this service to another process
 * and/or registering it with registerAsService().
 *
 * @param service the service to set the policy for
 * @param policy scheduler policy as defined in linux UAPI
 * @param priority priority. [-20..19] for SCHED_NORMAL, [1..99] for RT
 */
bool setMinSchedulerPolicy(const sp<::android::hidl::base::V1_0::IBase>& service,
                           int policy, int priority);

/**
 * Sets whether or not this object should request security contexts to be populatd for incoming
 * calls (e.g. with getCallingSid).
 *
 * This method MUST be called before passing this service to another process
 * and/or registering it with registerAsService().
 *
 * @param service the service to set the policy for
 * @param requesting whether or not to request sid (default is false)
 */
bool setRequestingSid(const sp<::android::hidl::base::V1_0::IBase>& service, bool requesting);

/**
 * Returns whether two interfaces represent the same interface. References to interfaces in the same
 * process will always be equivalent. However, in order to compare a service that is a proxy to a
 * different process, its underlying structure may have to be checked.
 */
bool interfacesEqual(const sp<::android::hidl::base::V1_0::IBase>& left,
                     const sp<::android::hidl::base::V1_0::IBase>& right);

namespace details {

// Return PID on userdebug / eng builds and IServiceManager::PidConstant::NO_PID on user builds.
int32_t getPidIfSharable();

// cast the interface IParent to IChild.
// Return nonnull if cast successful.
// Return nullptr if:
// 1. parent is null
// 2. cast failed because IChild is not a child type of IParent.
// 3. !emitError, calling into parent fails.
// Return an error Return object if:
// 1. emitError, calling into parent fails.
template <typename IChild, typename IParent, typename BpChild>
Return<sp<IChild>> castInterface(sp<IParent> parent, const char* childIndicator, bool emitError) {
    if (parent.get() == nullptr) {
        // casts always succeed with nullptrs.
        return nullptr;
    }
    Return<bool> canCastRet = details::canCastInterface(parent.get(), childIndicator, emitError);
    if (!canCastRet.isOk()) {
        // call fails, propagate the error if emitError
        return emitError
                ? details::StatusOf<bool, sp<IChild>>(canCastRet)
                : Return<sp<IChild>>(sp<IChild>(nullptr));
    }

    if (!canCastRet) {
        return sp<IChild>(nullptr); // cast failed.
    }
    // TODO b/32001926 Needs to be fixed for socket mode.
    if (parent->isRemote()) {
        // binderized mode. Got BpChild. grab the remote and wrap it.
        return sp<IChild>(new BpChild(getOrCreateCachedBinder(parent.get())));
    }
    // Passthrough mode. Got BnChild or BsChild.
    return sp<IChild>(static_cast<IChild *>(parent.get()));
}

template <typename BpType, typename IType = typename BpType::Pure,
          typename = std::enable_if_t<std::is_same<i_tag, typename IType::_hidl_tag>::value>,
          typename = std::enable_if_t<std::is_same<bphw_tag, typename BpType::_hidl_tag>::value>>
sp<IType> getServiceInternal(const std::string& instance, bool retry, bool getStub) {
    using ::android::hidl::base::V1_0::IBase;

    sp<IBase> base = getRawServiceInternal(IType::descriptor, instance, retry, getStub);

    if (base == nullptr) {
        return nullptr;
    }

    if (base->isRemote()) {
        // getRawServiceInternal guarantees we get the proper class
        return sp<IType>(new BpType(getOrCreateCachedBinder(base.get())));
    }

    return IType::castFrom(base);
}

}  // namespace details

}  // namespace hardware
}  // namespace android


#endif  // ANDROID_HIDL_TRANSPORT_SUPPORT_H