blob: 5378b67ebe1f814922121de5c67f8602b9e30ee5 [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 Hongd038b2b2018-11-27 13:57:56 -080025#include <vintf/PropertyFetcher.h>
Yifan Honga72bde72017-09-28 13:36:48 -070026#include <vintf/RuntimeInfo.h>
27#include <vintf/parse_xml.h>
Yifan Hong2272bf82017-04-28 14:37:56 -070028
29namespace android {
30namespace vintf {
31namespace details {
32
Michael Schwartz97dc0f92017-05-08 14:07:14 -070033template <typename T>
Yifan Hong9f78c182018-07-12 14:45:52 -070034status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
35 const XmlConverter<T>& converter, T* outObject, std::string* error) {
Michael Schwartz97dc0f92017-05-08 14:07:14 -070036 std::string info;
Yifan Hong9f78c182018-07-12 14:45:52 -070037 status_t result = fileSystem->fetch(path, &info, error);
Michael Schwartz97dc0f92017-05-08 14:07:14 -070038
39 if (result != OK) {
40 return result;
41 }
42
Yifan Hong94757062018-02-09 16:36:31 -080043 bool success = converter(outObject, info, error);
Yifan Hong2272bf82017-04-28 14:37:56 -070044 if (!success) {
Yifan Hong60217032018-01-08 16:19:42 -080045 if (error) {
Yifan Hong94757062018-02-09 16:36:31 -080046 *error = "Illformed file: " + path + ": " + *error;
Yifan Hong60217032018-01-08 16:19:42 -080047 }
Yifan Hong2272bf82017-04-28 14:37:56 -070048 return BAD_VALUE;
49 }
50 return OK;
51}
52
Yifan Hongafae1982018-01-11 18:15:24 -080053// TODO(b/70628538): Do not infer from Shipping API level.
54inline Level convertFromApiLevel(size_t apiLevel) {
55 if (apiLevel < 26) {
56 return Level::LEGACY;
57 } else if (apiLevel == 26) {
58 return Level::O;
59 } else if (apiLevel == 27) {
60 return Level::O_MR1;
61 } else {
62 return Level::UNSPECIFIED;
63 }
64}
65
Yifan Hong9f78c182018-07-12 14:45:52 -070066class PropertyFetcherImpl : public PropertyFetcher {
67 public:
68 virtual std::string getProperty(const std::string& key,
Yifan Hongd14640a2018-02-27 18:35:39 -080069 const std::string& defaultValue = "") const;
70 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
71 uint64_t max = UINT64_MAX) const;
72 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const;
73};
74
Yifan Hong9f78c182018-07-12 14:45:52 -070075class PropertyFetcherNoOp : public PropertyFetcher {
76 public:
77 virtual std::string getProperty(const std::string& key,
78 const std::string& defaultValue = "") const override;
79 virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
80 uint64_t max = UINT64_MAX) const override;
81 virtual bool getBoolProperty(const std::string& key, bool defaultValue) const override;
82};
Yifan Hongd14640a2018-02-27 18:35:39 -080083
Yifan Hong4c6962d2019-01-30 15:50:46 -080084// Merge src into dst.
85// postcondition (if successful): *src == empty.
86template <typename T>
87static bool mergeField(T* dst, T* src, const T& empty = T{}) {
88 if (*dst == *src) {
89 *src = empty;
90 return true; // no conflict
91 }
92 if (*src == empty) {
93 return true;
94 }
95 if (*dst == empty) {
96 *dst = std::move(*src);
97 *src = empty;
98 return true;
99 }
100 return false;
101}
102
Yifan Hong2272bf82017-04-28 14:37:56 -0700103} // namespace details
104} // namespace vintf
105} // namespace android
106
107
108
109#endif