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 <dirent.h> |
| 21 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 22 | #include <fstream> |
| 23 | #include <iostream> |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 24 | #include <memory> |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 27 | #include <utils/Errors.h> |
Yifan Hong | a72bde7 | 2017-09-28 13:36:48 -0700 | [diff] [blame] | 28 | #include <vintf/RuntimeInfo.h> |
| 29 | #include <vintf/parse_xml.h> |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace vintf { |
| 33 | namespace details { |
| 34 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 35 | // Return the file from the given location as a string. |
| 36 | // |
| 37 | // This class can be used to create a mock for overriding. |
| 38 | class FileFetcher { |
| 39 | public: |
| 40 | virtual ~FileFetcher() {} |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 41 | status_t fetchInternal(const std::string& path, std::string& fetched, std::string* error) { |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 42 | std::ifstream in; |
| 43 | |
| 44 | in.open(path); |
| 45 | if (!in.is_open()) { |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 46 | if (error) { |
| 47 | *error = "Cannot open " + path; |
| 48 | } |
Yifan Hong | 5a93bf2 | 2018-01-17 18:22:32 -0800 | [diff] [blame] | 49 | return NAME_NOT_FOUND; |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | std::stringstream ss; |
| 53 | ss << in.rdbuf(); |
| 54 | fetched = ss.str(); |
| 55 | |
| 56 | return OK; |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 57 | } |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 58 | virtual status_t fetch(const std::string& path, std::string& fetched, std::string* error) { |
| 59 | return fetchInternal(path, fetched, error); |
| 60 | } |
| 61 | virtual status_t fetch(const std::string& path, std::string& fetched) { |
| 62 | return fetchInternal(path, fetched, nullptr); |
| 63 | } |
Yifan Hong | d52bf3e | 2018-01-11 16:56:51 -0800 | [diff] [blame] | 64 | virtual status_t listFiles(const std::string& path, std::vector<std::string>* out, |
| 65 | std::string* error) { |
| 66 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); |
| 67 | if (!dir) { |
| 68 | if (error) { |
| 69 | *error = "Cannot open " + path; |
| 70 | } |
| 71 | return NAME_NOT_FOUND; |
| 72 | } |
| 73 | |
| 74 | dirent* dp; |
| 75 | while ((dp = readdir(dir.get())) != nullptr) { |
| 76 | if (dp->d_type != DT_DIR) { |
| 77 | out->push_back(dp->d_name); |
| 78 | } |
| 79 | } |
| 80 | return OK; |
| 81 | } |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | extern FileFetcher* gFetcher; |
| 85 | |
Yifan Hong | 8640cd1 | 2017-05-17 12:02:28 -0700 | [diff] [blame] | 86 | class PartitionMounter { |
| 87 | public: |
| 88 | virtual ~PartitionMounter() {} |
| 89 | virtual status_t mountSystem() const { return OK; } |
| 90 | virtual status_t mountVendor() const { return OK; } |
| 91 | virtual status_t umountSystem() const { return OK; } |
| 92 | virtual status_t umountVendor() const { return OK; } |
| 93 | }; |
| 94 | |
| 95 | extern PartitionMounter* gPartitionMounter; |
| 96 | |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 97 | template <typename T> |
| 98 | status_t fetchAllInformation(const std::string& path, const XmlConverter<T>& converter, |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 99 | T* outObject, std::string* error = nullptr) { |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 100 | std::string info; |
| 101 | |
| 102 | if (gFetcher == nullptr) { |
| 103 | // Should never happen. |
| 104 | return NO_INIT; |
| 105 | } |
| 106 | |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 107 | status_t result = gFetcher->fetch(path, info, error); |
Michael Schwartz | 97dc0f9 | 2017-05-08 14:07:14 -0700 | [diff] [blame] | 108 | |
| 109 | if (result != OK) { |
| 110 | return result; |
| 111 | } |
| 112 | |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 113 | bool success = converter(outObject, info, error); |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 114 | if (!success) { |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 115 | if (error) { |
Yifan Hong | 9475706 | 2018-02-09 16:36:31 -0800 | [diff] [blame] | 116 | *error = "Illformed file: " + path + ": " + *error; |
Yifan Hong | 6021703 | 2018-01-08 16:19:42 -0800 | [diff] [blame] | 117 | } |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 118 | return BAD_VALUE; |
| 119 | } |
| 120 | return OK; |
| 121 | } |
| 122 | |
Yifan Hong | 29bb2d4 | 2017-09-27 13:28:00 -0700 | [diff] [blame] | 123 | template <typename T> |
| 124 | class ObjectFactory { |
| 125 | public: |
| 126 | virtual ~ObjectFactory() = default; |
| 127 | virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); } |
| 128 | }; |
| 129 | extern ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory; |
| 130 | |
Yifan Hong | afae198 | 2018-01-11 18:15:24 -0800 | [diff] [blame] | 131 | // TODO(b/70628538): Do not infer from Shipping API level. |
| 132 | inline Level convertFromApiLevel(size_t apiLevel) { |
| 133 | if (apiLevel < 26) { |
| 134 | return Level::LEGACY; |
| 135 | } else if (apiLevel == 26) { |
| 136 | return Level::O; |
| 137 | } else if (apiLevel == 27) { |
| 138 | return Level::O_MR1; |
| 139 | } else { |
| 140 | return Level::UNSPECIFIED; |
| 141 | } |
| 142 | } |
| 143 | |
Yifan Hong | d14640a | 2018-02-27 18:35:39 -0800 | [diff] [blame^] | 144 | class PropertyFetcher { |
| 145 | public: |
| 146 | virtual ~PropertyFetcher() = default; |
| 147 | virtual std::string getProperty(const std::string& key, |
| 148 | const std::string& defaultValue = "") const; |
| 149 | virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue, |
| 150 | uint64_t max = UINT64_MAX) const; |
| 151 | virtual bool getBoolProperty(const std::string& key, bool defaultValue) const; |
| 152 | }; |
| 153 | |
| 154 | const PropertyFetcher& getPropertyFetcher(); |
| 155 | |
Yifan Hong | 2272bf8 | 2017-04-28 14:37:56 -0700 | [diff] [blame] | 156 | } // namespace details |
| 157 | } // namespace vintf |
| 158 | } // namespace android |
| 159 | |
| 160 | |
| 161 | |
| 162 | #endif |