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 | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 25 | #include <vintf/RuntimeInfo.h> |
| 26 | #include <vintf/parse_xml.h> |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | namespace vintf { |
| 30 | namespace details { |
| 31 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 32 | template <typename T> |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 33 | status_t fetchAllInformation(const FileSystem* fileSystem, const std::string& path, |
| 34 | const XmlConverter<T>& converter, T* outObject, std::string* error) { |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 35 | std::string info; |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 36 | status_t result = fileSystem->fetch(path, &info, error); |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 37 | |
| 38 | if (result != OK) { |
| 39 | return result; |
| 40 | } |
| 41 | |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 42 | bool success = converter(outObject, info, error); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 43 | if (!success) { |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 44 | if (error) { |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 45 | *error = "Illformed file: " + path + ": " + *error; |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 46 | } |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 47 | return BAD_VALUE; |
| 48 | } |
| 49 | return OK; |
| 50 | } |
| 51 | |
Yifan Hong | 29bb2d4 | 2017-09-27 13:28:00 -0700 | [diff] [blame] | 52 | template <typename T> |
| 53 | class ObjectFactory { |
| 54 | public: |
| 55 | virtual ~ObjectFactory() = default; |
| 56 | virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); } |
| 57 | }; |
Yifan Hong | 29bb2d4 | 2017-09-27 13:28:00 -0700 | [diff] [blame] | 58 | |
Yifan Hong | afae198 | 2018-01-11 18:15:24 -0800 | [diff] [blame] | 59 | // TODO(b/70628538): Do not infer from Shipping API level. |
| 60 | inline 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 Hong | d14640a | 2018-02-27 18:35:39 -0800 | [diff] [blame] | 72 | class PropertyFetcher { |
| 73 | public: |
| 74 | virtual ~PropertyFetcher() = default; |
| 75 | virtual std::string getProperty(const std::string& key, |
Yifan Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 76 | 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 | |
| 82 | class PropertyFetcherImpl : public PropertyFetcher { |
| 83 | public: |
| 84 | virtual std::string getProperty(const std::string& key, |
Yifan Hong | d14640a | 2018-02-27 18:35:39 -0800 | [diff] [blame] | 85 | 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 Hong | 9f78c18 | 2018-07-12 14:45:52 -0700 | [diff] [blame] | 91 | class 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 Hong | d14640a | 2018-02-27 18:35:39 -0800 | [diff] [blame] | 99 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 100 | } // namespace details |
| 101 | } // namespace vintf |
| 102 | } // namespace android |
| 103 | |
| 104 | |
| 105 | |
| 106 | #endif |