1 /*
2 * Copyright (C) 2021 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 // Special utils for VintfObject(s).
18
19 #pragma once
20
21 // This is okay because it is a header private to libvintf. Do not do this in exported headers!
22 #include <android-base/logging.h>
23
24 #include <vintf/VintfObject.h>
25
26 namespace android {
27 namespace vintf {
28 namespace details {
29
30 template <typename T, typename F>
Get(const char * id,LockedSharedPtr<T> * ptr,const F & fetchAllInformation)31 std::shared_ptr<const T> Get(const char* id, LockedSharedPtr<T>* ptr,
32 const F& fetchAllInformation) {
33 std::unique_lock<std::mutex> _lock(ptr->mutex);
34 if (!ptr->fetchedOnce) {
35 LOG(INFO) << id << ": Reading VINTF information.";
36 ptr->object = std::make_unique<T>();
37 std::string error;
38 status_t status = fetchAllInformation(ptr->object.get(), &error);
39 if (status == OK) {
40 ptr->fetchedOnce = true;
41 LOG(INFO) << id << ": Successfully processed VINTF information";
42 } else {
43 // Doubled because a malformed error std::string might cause us to
44 // lose the status.
45 LOG(ERROR) << id << ": status from fetching VINTF information: " << status;
46 LOG(ERROR) << id << ": " << status << " VINTF parse error: " << error;
47 ptr->object = nullptr; // frees the old object
48 }
49 }
50 return ptr->object;
51 }
52
53 } // namespace details
54 } // namespace vintf
55 } // namespace android
56