blob: 3cb64857506e77a9a6bbec078e8d86d0456a6772 [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
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070018#include <map>
19#include <mutex>
20#include <string>
21
Martijn Coenen30791002016-12-01 15:40:46 +010022#include <hidlmemory/mapping.h>
23
24#include <android-base/logging.h>
25#include <android/hidl/memory/1.0/IMapper.h>
26#include <hidl/HidlSupport.h>
27
28using android::sp;
29using android::hidl::memory::V1_0::IMemory;
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070030using android::hidl::memory::V1_0::IMapper;
Martijn Coenen30791002016-12-01 15:40:46 +010031
32namespace android {
33namespace hardware {
34
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070035static std::map<std::string, sp<IMapper>> gMappersByName;
36static std::mutex gMutex;
Martijn Coenen30791002016-12-01 15:40:46 +010037
Martijn Coenene5f1e5a2017-05-30 17:09:50 -070038static inline sp<IMapper> getMapperService(const std::string& name) {
39 std::unique_lock<std::mutex> _lock(gMutex);
40 auto iter = gMappersByName.find(name);
41 if (iter != gMappersByName.end()) {
42 return iter->second;
43 }
44
45 sp<IMapper> mapper = IMapper::getService(name, true /* getStub */);
46 if (mapper != nullptr) {
47 gMappersByName[name] = mapper;
48 }
49 return mapper;
50}
51
52sp<IMemory> mapMemory(const hidl_memory& memory) {
53
54 sp<IMapper> mapper = getMapperService(memory.name());
Martijn Coenen30791002016-12-01 15:40:46 +010055
56 if (mapper == nullptr) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070057 LOG(ERROR) << "Could not fetch mapper for " << memory.name() << " shared memory";
58 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010059 }
60
61 if (mapper->isRemote()) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070062 LOG(ERROR) << "IMapper must be a passthrough service.";
63 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010064 }
65
Martijn Coenend272cb92017-01-02 15:20:38 +010066 Return<sp<IMemory>> ret = mapper->mapMemory(memory);
Martijn Coenen30791002016-12-01 15:40:46 +010067
68 if (!ret.isOk()) {
Steven Moreland10de9ef2017-05-17 09:26:24 -070069 LOG(ERROR) << "hidl_memory map returned transport error.";
70 return nullptr;
Martijn Coenen30791002016-12-01 15:40:46 +010071 }
72
Martijn Coenend272cb92017-01-02 15:20:38 +010073 return ret;
Martijn Coenen30791002016-12-01 15:40:46 +010074}
75
76} // namespace hardware
Martijn Coenend272cb92017-01-02 15:20:38 +010077} // namespace android