Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #include "VintfObject.h" |
| 18 | |
| 19 | #include <functional> |
| 20 | #include <memory> |
| 21 | #include <mutex> |
| 22 | |
| 23 | namespace android { |
| 24 | namespace vintf { |
| 25 | |
| 26 | template <typename T> |
| 27 | struct LockedUniquePtr { |
| 28 | std::unique_ptr<T> object; |
| 29 | std::mutex mutex; |
| 30 | }; |
| 31 | |
| 32 | static LockedUniquePtr<HalManifest> gDeviceManifest; |
| 33 | static LockedUniquePtr<HalManifest> gFrameworkManifest; |
| 34 | static LockedUniquePtr<RuntimeInfo> gDeviceRuntimeInfo; |
| 35 | |
| 36 | template <typename T, typename F> |
| 37 | static const T *Get( |
| 38 | LockedUniquePtr<T> *ptr, |
| 39 | const F &fetchAllInformation) { |
| 40 | std::unique_lock<std::mutex> _lock(ptr->mutex); |
| 41 | if (ptr->object == nullptr) { |
| 42 | ptr->object = std::make_unique<T>(); |
| 43 | if (fetchAllInformation(ptr->object.get()) != OK) { |
| 44 | ptr->object = nullptr; // frees the old object |
| 45 | } |
| 46 | } |
| 47 | return ptr->object.get(); |
| 48 | } |
| 49 | |
| 50 | // static |
| 51 | const HalManifest *VintfObject::GetDeviceHalManifest() { |
| 52 | return Get(&gDeviceManifest, |
| 53 | std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1, |
| 54 | "/vendor/manifest.xml")); |
| 55 | } |
| 56 | |
| 57 | // static |
| 58 | const HalManifest *VintfObject::GetFrameworkHalManifest() { |
| 59 | return Get(&gFrameworkManifest, |
| 60 | std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1, |
| 61 | "/system/manifest.xml")); |
| 62 | } |
| 63 | |
| 64 | // static |
| 65 | const RuntimeInfo *VintfObject::GetRuntimeInfo() { |
| 66 | return Get(&gDeviceRuntimeInfo, |
| 67 | std::bind(&RuntimeInfo::fetchAllInformation, std::placeholders::_1)); |
| 68 | } |
| 69 | |
Yifan Hong | fbbf047 | 2017-04-07 18:14:18 -0700 | [diff] [blame^] | 70 | // static |
| 71 | int32_t VintfObject::CheckCompatibility( |
| 72 | const std::vector<std::string> &, |
| 73 | bool) { |
| 74 | // TODO(b/36814503): actually do the verification. |
| 75 | return -1; |
| 76 | } |
| 77 | |
Yifan Hong | 3daec81 | 2017-02-27 18:49:11 -0800 | [diff] [blame] | 78 | |
| 79 | } // namespace vintf |
| 80 | } // namespace android |