blob: 8ccda3eaf4ac133c94c9c4bebd7ceb2e1165ed7a [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
Michael Schwartz97dc0f92017-05-08 14:07:14 -070041template <typename T>
Yifan Hong9f78c182018-07-12 14:45:52 -070042status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
43 const XmlConverter<T>& converter, T* outObject, std::string* error) {
Michael Schwartz97dc0f92017-05-08 14:07:14 -070044 std::string info;
Yifan Hong9f78c182018-07-12 14:45:52 -070045 status_t result = fileSystem->fetch(path, &info, error);
Michael Schwartz97dc0f92017-05-08 14:07:14 -070046
47 if (result != OK) {
48 return result;
49 }
50
Yifan Hong94757062018-02-09 16:36:31 -080051 bool success = converter(outObject, info, error);
Yifan Hong2272bf82017-04-28 14:37:56 -070052 if (!success) {
Yifan Hong60217032018-01-08 16:19:42 -080053 if (error) {
Yifan Hong94757062018-02-09 16:36:31 -080054 *error = "Illformed file: " + path + ": " + *error;
Yifan Hong60217032018-01-08 16:19:42 -080055 }
Yifan Hong2272bf82017-04-28 14:37:56 -070056 return BAD_VALUE;
57 }
58 return OK;
59}
60
Yifan Hong29bb2d42017-09-27 13:28:00 -070061template <typename T>
62class ObjectFactory {
63 public:
64 virtual ~ObjectFactory() = default;
65 virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); }
66};
Yifan Hong29bb2d42017-09-27 13:28:00 -070067
Yifan Hongafae1982018-01-11 18:15:24 -080068// TODO(b/70628538): Do not infer from Shipping API level.
69inline Level convertFromApiLevel(size_t apiLevel) {
70 if (apiLevel < 26) {
71 return Level::LEGACY;
72 } else if (apiLevel == 26) {
73 return Level::O;
74 } else if (apiLevel == 27) {
75 return Level::O_MR1;
76 } else {
77 return Level::UNSPECIFIED;
78 }
79}
80
Yifan Hongd14640a2018-02-27 18:35:39 -080081class PropertyFetcher {
82 public:
83 virtual ~PropertyFetcher() = default;
84 virtual std::string getProperty(const std::string& key,
Yifan Hong9f78c182018-07-12 14:45:52 -070085 const std::string& defaultValue = "") const = 0;
86 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
87 uint64_t max = UINT64_MAX) const = 0;
88 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const = 0;
89};
90
91class PropertyFetcherImpl : public PropertyFetcher {
92 public:
93 virtual std::string getProperty(const std::string& key,
Yifan Hongd14640a2018-02-27 18:35:39 -080094 const std::string& defaultValue = "") const;
95 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
96 uint64_t max = UINT64_MAX) const;
97 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const;
98};
99
Yifan Hong9f78c182018-07-12 14:45:52 -0700100class PropertyFetcherNoOp : public PropertyFetcher {
101 public:
102 virtual std::string getProperty(const std::string& key,
103 const std::string& defaultValue = "") const override;
104 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
105 uint64_t max = UINT64_MAX) const override;
106 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const override;
107};
Yifan Hongd14640a2018-02-27 18:35:39 -0800108
Yifan Hong2272bf82017-04-28 14:37:56 -0700109} // namespace details
110} // namespace vintf
111} // namespace android
112
113
114
115#endif