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 // Get() fetches data and caches it in LockedSharedPtr. The cached data will be 31 // invalidated when `lastModified` is changed from the last call. Typically `lastModified` 32 // can refer to the "last modified" timestamp of the data source. 33 template <typename T, typename F> 34 std::shared_ptr<const T> Get(const char* id, LockedSharedPtr<T>* ptr, const F& fetch, 35 const std::optional<timespec>& lastModified = std::nullopt) { 36 std::unique_lock<std::mutex> lock(ptr->mutex); 37 // Check if the last fetched data is fresh. If it's old, re-fetch the data 38 // with the new timestamp. 39 if (ptr->object && ptr->lastModified != lastModified) { 40 LOG(INFO) << id << ": Reloading VINTF information."; 41 ptr->object = nullptr; 42 } 43 if (!ptr->object) { 44 LOG(INFO) << id << ": Reading VINTF information."; 45 ptr->object = std::make_unique<T>(); 46 ptr->lastModified = lastModified; 47 std::string error; 48 status_t status = fetch(ptr->object.get(), &error); 49 if (status == OK) { 50 LOG(INFO) << id << ": Successfully processed VINTF information"; 51 } else { 52 // Doubled because a malformed error std::string might cause us to 53 // lose the status. 54 LOG(ERROR) << id << ": status from fetching VINTF information: " << status; 55 LOG(ERROR) << id << ": " << status << " VINTF parse error: " << error; 56 ptr->object = nullptr; // frees the old object 57 } 58 } 59 return ptr->object; 60 } 61 62 } // namespace details 63 } // namespace vintf 64 } // namespace android 65