• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
20 #include <memory>
21 #include <mutex>
22 
23 #include <utils/Errors.h>
24 #include <vintf/FileSystem.h>
25 #include <vintf/PropertyFetcher.h>
26 #include <vintf/RuntimeInfo.h>
27 #include <vintf/parse_xml.h>
28 
29 namespace android {
30 namespace vintf {
31 namespace details {
32 
33 template <typename T>
fetchAllInformation(const FileSystem * fileSystem,const std::string & path,const XmlConverter<T> & converter,T * outObject,std::string * error)34 status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path,
35                              const XmlConverter<T>& converter, T* outObject, std::string* error) {
36     std::string info;
37     status_t result = fileSystem->fetch(path, &info, error);
38 
39     if (result != OK) {
40         return result;
41     }
42 
43     bool success = converter(outObject, info, error);
44     if (!success) {
45         if (error) {
46             *error = "Illformed file: " + path + ": " + *error;
47         }
48         return BAD_VALUE;
49     }
50     return OK;
51 }
52 
53 // TODO(b/70628538): Do not infer from Shipping API level.
convertFromApiLevel(size_t apiLevel)54 inline 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 
66 class PropertyFetcherImpl : public PropertyFetcher {
67    public:
68     virtual std::string getProperty(const std::string& key,
69                                     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 
75 class 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 };
83 
84 // Merge src into dst.
85 // postcondition (if successful): *src == empty.
86 template <typename T>
87 static 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 
103 }  // namespace details
104 }  // namespace vintf
105 }  // namespace android
106 
107 
108 
109 #endif
110