blob: f8cc9cd86fa0a2d826d69f1f705636950f5fd5c0 [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
Michael Schwartz97dc0f92017-05-08 14:07:14 -070032template <typename T>
Yifan Hong9f78c182018-07-12 14:45:52 -070033status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
34 const XmlConverter<T>& converter, T* outObject, std::string* error) {
Michael Schwartz97dc0f92017-05-08 14:07:14 -070035 std::string info;
Yifan Hong9f78c182018-07-12 14:45:52 -070036 status_t result = fileSystem->fetch(path, &info, error);
Michael Schwartz97dc0f92017-05-08 14:07:14 -070037
38 if (result != OK) {
39 return result;
40 }
41
Yifan Hong94757062018-02-09 16:36:31 -080042 bool success = converter(outObject, info, error);
Yifan Hong2272bf82017-04-28 14:37:56 -070043 if (!success) {
Yifan Hong60217032018-01-08 16:19:42 -080044 if (error) {
Yifan Hong94757062018-02-09 16:36:31 -080045 *error = "Illformed file: " + path + ": " + *error;
Yifan Hong60217032018-01-08 16:19:42 -080046 }
Yifan Hong2272bf82017-04-28 14:37:56 -070047 return BAD_VALUE;
48 }
49 return OK;
50}
51
Yifan Hong29bb2d42017-09-27 13:28:00 -070052template <typename T>
53class ObjectFactory {
54 public:
55 virtual ~ObjectFactory() = default;
56 virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); }
57};
Yifan Hong29bb2d42017-09-27 13:28:00 -070058
Yifan Hongafae1982018-01-11 18:15:24 -080059// TODO(b/70628538): Do not infer from Shipping API level.
60inline Level convertFromApiLevel(size_t apiLevel) {
61 if (apiLevel < 26) {
62 return Level::LEGACY;
63 } else if (apiLevel == 26) {
64 return Level::O;
65 } else if (apiLevel == 27) {
66 return Level::O_MR1;
67 } else {
68 return Level::UNSPECIFIED;
69 }
70}
71
Yifan Hongd14640a2018-02-27 18:35:39 -080072class PropertyFetcher {
73 public:
74 virtual ~PropertyFetcher() = default;
75 virtual std::string getProperty(const std::string& key,
Yifan Hong9f78c182018-07-12 14:45:52 -070076 const std::string& defaultValue = "") const = 0;
77 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
78 uint64_t max = UINT64_MAX) const = 0;
79 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const = 0;
80};
81
82class PropertyFetcherImpl : public PropertyFetcher {
83 public:
84 virtual std::string getProperty(const std::string& key,
Yifan Hongd14640a2018-02-27 18:35:39 -080085 const std::string& defaultValue = "") const;
86 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
87 uint64_t max = UINT64_MAX) const;
88 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const;
89};
90
Yifan Hong9f78c182018-07-12 14:45:52 -070091class PropertyFetcherNoOp : public PropertyFetcher {
92 public:
93 virtual std::string getProperty(const std::string& key,
94 const std::string& defaultValue = "") const override;
95 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
96 uint64_t max = UINT64_MAX) const override;
97 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const override;
98};
Yifan Hongd14640a2018-02-27 18:35:39 -080099
Yifan Hong2272bf82017-04-28 14:37:56 -0700100} // namespace details
101} // namespace vintf
102} // namespace android
103
104
105
106#endif