Matt Spinler | 18c42b0 | 2020-06-02 15:59:50 -0500 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 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 | #include "device_callouts.hpp" |
| 17 | |
| 18 | #include "paths.hpp" |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <phosphor-logging/log.hpp> |
| 22 | #include <regex> |
| 23 | |
| 24 | namespace openpower::pels::device_callouts |
| 25 | { |
| 26 | |
| 27 | constexpr auto debugFilePath = "/etc/phosphor-logging/"; |
| 28 | constexpr auto calloutFileSuffix = "_dev_callouts.json"; |
| 29 | |
| 30 | namespace fs = std::filesystem; |
| 31 | using namespace phosphor::logging; |
| 32 | |
| 33 | namespace util |
| 34 | { |
| 35 | |
| 36 | fs::path getJSONFilename(const std::vector<std::string>& compatibleList) |
| 37 | { |
| 38 | auto basePath = getPELReadOnlyDataPath(); |
| 39 | fs::path fullPath; |
| 40 | |
| 41 | // Find an entry in the list of compatible system names that |
| 42 | // matches a filename we have. |
| 43 | |
| 44 | for (const auto& name : compatibleList) |
| 45 | { |
| 46 | fs::path filename = name + calloutFileSuffix; |
| 47 | |
| 48 | // Check the debug path first |
| 49 | fs::path path{fs::path{debugFilePath} / filename}; |
| 50 | |
| 51 | if (fs::exists(path)) |
| 52 | { |
| 53 | log<level::INFO>("Found device callout debug file"); |
| 54 | fullPath = path; |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | path = basePath / filename; |
| 59 | |
| 60 | if (fs::exists(path)) |
| 61 | { |
| 62 | fullPath = path; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (fullPath.empty()) |
| 68 | { |
| 69 | throw std::invalid_argument( |
| 70 | "No JSON dev path callout file for this system"); |
| 71 | } |
| 72 | |
| 73 | return fullPath; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @brief Reads the callout JSON into an object based on the |
| 78 | * compatible system names list. |
| 79 | * |
| 80 | * @param[in] compatibleList - The list of compatible names for this |
| 81 | * system. |
| 82 | * |
| 83 | * @return nlohmann::json - The JSON object |
| 84 | */ |
| 85 | nlohmann::json loadJSON(const std::vector<std::string>& compatibleList) |
| 86 | { |
| 87 | auto filename = getJSONFilename(compatibleList); |
| 88 | std::ifstream file{filename}; |
| 89 | return nlohmann::json::parse(file); |
| 90 | } |
| 91 | |
| 92 | std::vector<device_callouts::Callout> |
| 93 | calloutI2C(size_t i2cBus, uint8_t i2cAddress, |
| 94 | const nlohmann::json& calloutJSON) |
| 95 | { |
| 96 | // TODO |
| 97 | return {}; |
| 98 | } |
| 99 | |
| 100 | std::vector<device_callouts::Callout> findCallouts(const std::string& devPath, |
| 101 | const nlohmann::json& json) |
| 102 | { |
| 103 | std::vector<Callout> callouts; |
| 104 | |
| 105 | // TODO |
| 106 | |
| 107 | return callouts; |
| 108 | } |
| 109 | |
| 110 | } // namespace util |
| 111 | |
| 112 | std::vector<Callout> getCallouts(const std::string& devPath, |
| 113 | const std::vector<std::string>& compatibleList) |
| 114 | { |
| 115 | auto json = util::loadJSON(compatibleList); |
| 116 | return util::findCallouts(devPath, json); |
| 117 | } |
| 118 | |
| 119 | std::vector<Callout> |
| 120 | getI2CCallouts(size_t i2cBus, uint8_t i2cAddress, |
| 121 | const std::vector<std::string>& compatibleList) |
| 122 | { |
| 123 | auto json = util::loadJSON(compatibleList); |
| 124 | return util::calloutI2C(i2cBus, i2cAddress, json); |
| 125 | } |
| 126 | |
| 127 | } // namespace openpower::pels::device_callouts |