blob: f4bb21e0a6f8ce22899be1fec44d40235c5c6998 [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) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070036 LOG(ERROR) << "Could not fetch mapper for " << memory.name() << " shared memory";
37 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010038 }
39
40 if (mapper->isRemote()) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070041 LOG(ERROR) << "IMapper must be a passthrough service.";
42 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010043 }
44
Martijn Coenend272cb92017-01-02 15:20:38 +010045 Return<sp<IMemory>> ret = mapper->mapMemory(memory);
Martijn Coenen30791002016-12-01 15:40:46 +010046
47 if (!ret.isOk()) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070048 LOG(ERROR) << "hidl_memory map returned transport error.";
49 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010050 }
51
Martijn Coenend272cb92017-01-02 15:20:38 +010052 return ret;
Martijn Coenen30791002016-12-01 15:40:46 +010053}
54
55} // namespace hardware
Martijn Coenend272cb92017-01-02 15:20:38 +010056} // namespace android