blob: b4fa12314285156dd44700fa383238978b13751a [file] [log] [blame]
Martijn Coenen30791002016-12-01 15:40:46 +01001/*
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#define LOG_TAG "libhidlmemory"
17
18#include <hidlmemory/mapping.h>
19
20#include <android-base/logging.h>
21#include <android/hidl/memory/1.0/IMapper.h>
22#include <hidl/HidlSupport.h>
23
24using android::sp;
25using android::hidl::memory::V1_0::IMemory;
26
27namespace android {
28namespace hardware {
29
30sp<IMemory> mapMemory(const hidl_memory &memory) {
31 using android::hidl::memory::V1_0::IMapper;
32
33 sp<IMapper> mapper = IMapper::getService(memory.name(), true /* getStub */);
34
35 if (mapper == nullptr) {
36 LOG(FATAL) << "Could not fetch mapper for " << memory.name() << " shared memory";
37 }
38
39 if (mapper->isRemote()) {
40 LOG(FATAL) << "IMapper must be a passthrough service.";
41 }
42
43 sp<IMemory> retMemory = nullptr;
44
45 Return<void> ret = mapper->mapMemory(memory,
46 [&retMemory](const auto &mapped) {
47 retMemory = mapped;
48 });
49
50 if (!ret.isOk()) {
51 LOG(FATAL) << "hidl_memory map returned transport error.";
52 }
53
54 return retMemory;
55}
56
57} // namespace hardware
58} // namespace android