Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 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 | |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 20 | #include <memory> |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 21 | #include <mutex> |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 22 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 23 | #include <utils/Errors.h> |
Yifan Hong | 10d8622 | 2018-04-06 15:41:05 -0700 | [diff] [blame] | 24 | #include <vintf/FileSystem.h> |
Yifan Hong | d038b2b | 2018-11-27 13:57:56 -0800 | [diff] [blame] | 25 | #include <vintf/PropertyFetcher.h> |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 26 | #include <vintf/RuntimeInfo.h> |
| 27 | #include <vintf/parse_xml.h> |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace vintf { |
| 31 | namespace details { |
| 32 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 33 | template <typename T> |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 34 | status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path, |
| 35 | const XmlConverter<T>& converter, T* outObject, std::string* error) { |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 36 | std::string info; |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 37 | status_t result = fileSystem->fetch(path, &info, error); |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 38 | |
| 39 | if (result != OK) { |
| 40 | return result; |
| 41 | } |
| 42 | |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 43 | bool success = converter(outObject, info, error); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 44 | if (!success) { |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 45 | if (error) { |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 46 | *error = "Illformed file: " + path + ": " + *error; |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 47 | } |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 48 | return BAD_VALUE; |
| 49 | } |
| 50 | return OK; |
| 51 | } |
| 52 | |
Yifan Hong | afae198 | 2018-01-11 18:15:24 -0800 | [diff] [blame] | 53 | // TODO(b/70628538): Do not infer from Shipping API level. |
| 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 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 66 | class PropertyFetcherImpl : public PropertyFetcher { |
| 67 | public: |
| 68 | virtual std::string getProperty(const std::string& key, |
Yifan Hong | d14640a | 2018-02-27 18:35:39 -0800 | [diff] [blame] | 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 | |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 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 | }; |
Yifan Hong | d14640a | 2018-02-27 18:35:39 -0800 | [diff] [blame] | 83 | |
Yifan Hong | 4c6962d | 2019-01-30 15:50:46 -0800 | [diff] [blame] | 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 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 103 | } // namespace details |
| 104 | } // namespace vintf |
| 105 | } // namespace android |
| 106 | |
| 107 | |
| 108 | |
| 109 | #endif |