blob: 8b908be9efc320be7ee6d47f4c8110e7d66cca2d [file] [log] [blame]
Yifan Hong3daec812017-02-27 18:49:11 -08001/*
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
23namespace android {
24namespace vintf {
25
26template <typename T>
27struct LockedUniquePtr {
28 std::unique_ptr<T> object;
29 std::mutex mutex;
30};
31
32static LockedUniquePtr<HalManifest> gDeviceManifest;
33static LockedUniquePtr<HalManifest> gFrameworkManifest;
34static LockedUniquePtr<RuntimeInfo> gDeviceRuntimeInfo;
35
36template <typename T, typename F>
37static 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
51const HalManifest *VintfObject::GetDeviceHalManifest() {
52 return Get(&gDeviceManifest,
53 std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
54 "/vendor/manifest.xml"));
55}
56
57// static
58const HalManifest *VintfObject::GetFrameworkHalManifest() {
59 return Get(&gFrameworkManifest,
60 std::bind(&HalManifest::fetchAllInformation, std::placeholders::_1,
61 "/system/manifest.xml"));
62}
63
64// static
65const RuntimeInfo *VintfObject::GetRuntimeInfo() {
66 return Get(&gDeviceRuntimeInfo,
67 std::bind(&RuntimeInfo::fetchAllInformation, std::placeholders::_1));
68}
69
Yifan Hongfbbf0472017-04-07 18:14:18 -070070// static
71int32_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 Hong3daec812017-02-27 18:49:11 -080078
79} // namespace vintf
80} // namespace android