blob: e33d283039ad3a4b83f41a5586d5f3f562a280aa [file] [log] [blame]
Yifan Hong2272bf82017-04-28 14:37:56 -07001/*
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#ifndef ANDROID_VINTF_UTILS_H
18#define ANDROID_VINTF_UTILS_H
19
Yifan Hongd52bf3e2018-01-11 16:56:51 -080020#include <memory>
Yifan Hong10d86222018-04-06 15:41:05 -070021#include <mutex>
Yifan Hong2272bf82017-04-28 14:37:56 -070022
Yifan Hong2272bf82017-04-28 14:37:56 -070023#include <utils/Errors.h>
Yifan Hong10d86222018-04-06 15:41:05 -070024#include <vintf/FileSystem.h>
Yifan Honga72bde72017-09-28 13:36:48 -070025#include <vintf/RuntimeInfo.h>
26#include <vintf/parse_xml.h>
Yifan Hong2272bf82017-04-28 14:37:56 -070027
28namespace android {
29namespace vintf {
30namespace details {
31
Yifan Hong8640cd12017-05-17 12:02:28 -070032class PartitionMounter {
33 public:
34 virtual ~PartitionMounter() {}
35 virtual status_t mountSystem() const { return OK; }
36 virtual status_t mountVendor() const { return OK; }
37 virtual status_t umountSystem() const { return OK; }
38 virtual status_t umountVendor() const { return OK; }
39};
40
41extern PartitionMounter* gPartitionMounter;
42
Michael Schwartz97dc0f92017-05-08 14:07:14 -070043template <typename T>
44status_t fetchAllInformation(const std::string& path, const XmlConverter<T>& converter,
Yifan Hong60217032018-01-08 16:19:42 -080045 T* outObject, std::string* error = nullptr) {
Michael Schwartz97dc0f92017-05-08 14:07:14 -070046 std::string info;
47
Yifan Hong10d86222018-04-06 15:41:05 -070048 status_t result = getFileSystem().fetch(path, &info, error);
Michael Schwartz97dc0f92017-05-08 14:07:14 -070049
50 if (result != OK) {
51 return result;
52 }
53
Yifan Hong94757062018-02-09 16:36:31 -080054 bool success = converter(outObject, info, error);
Yifan Hong2272bf82017-04-28 14:37:56 -070055 if (!success) {
Yifan Hong60217032018-01-08 16:19:42 -080056 if (error) {
Yifan Hong94757062018-02-09 16:36:31 -080057 *error = "Illformed file: " + path + ": " + *error;
Yifan Hong60217032018-01-08 16:19:42 -080058 }
Yifan Hong2272bf82017-04-28 14:37:56 -070059 return BAD_VALUE;
60 }
61 return OK;
62}
63
Yifan Hong29bb2d42017-09-27 13:28:00 -070064template <typename T>
65class ObjectFactory {
66 public:
67 virtual ~ObjectFactory() = default;
68 virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); }
69};
70extern ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory;
71
Yifan Hongafae1982018-01-11 18:15:24 -080072// TODO(b/70628538): Do not infer from Shipping API level.
73inline Level convertFromApiLevel(size_t apiLevel) {
74 if (apiLevel < 26) {
75 return Level::LEGACY;
76 } else if (apiLevel == 26) {
77 return Level::O;
78 } else if (apiLevel == 27) {
79 return Level::O_MR1;
80 } else {
81 return Level::UNSPECIFIED;
82 }
83}
84
Yifan Hongd14640a2018-02-27 18:35:39 -080085class PropertyFetcher {
86 public:
87 virtual ~PropertyFetcher() = default;
88 virtual std::string getProperty(const std::string& key,
89 const std::string& defaultValue = "") const;
90 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
91 uint64_t max = UINT64_MAX) const;
92 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const;
93};
94
95const PropertyFetcher& getPropertyFetcher();
96
Yifan Hong2272bf82017-04-28 14:37:56 -070097} // namespace details
98} // namespace vintf
99} // namespace android
100
101
102
103#endif