Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 1 | #include "utility.hpp" |
| 2 | |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 3 | #include <nlohmann/json.hpp> |
| 4 | #include <phosphor-logging/log.hpp> |
| 5 | |
Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 6 | #include <algorithm> |
Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 7 | #include <fstream> |
Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 8 | |
| 9 | using json = nlohmann::json; |
| 10 | using namespace phosphor::logging; |
| 11 | using namespace utility; |
| 12 | |
| 13 | constexpr auto gpioDefsFile = "/etc/default/obmc/gpio/gpio_defs.json"; |
| 14 | |
| 15 | int main(void) |
| 16 | { |
| 17 | std::ifstream gpioDefsStream(gpioDefsFile); |
| 18 | |
| 19 | if (!gpioDefsStream.is_open()) |
| 20 | { |
| 21 | log<level::ERR>("Error opening gpio definitions file", |
| 22 | entry("FILE=%s", gpioDefsFile)); |
| 23 | return 1; |
| 24 | } |
| 25 | |
| 26 | auto data = json::parse(gpioDefsStream, nullptr, false); |
| 27 | |
| 28 | if (data.is_discarded()) |
| 29 | { |
| 30 | log<level::ERR>("Error parsing gpio definitions file", |
| 31 | entry("FILE=%s", gpioDefsFile)); |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | // To determine what pins are needed to deassert, look in the |
| 36 | // gpioDefsFile for the defined "power_up_outs" under |
| 37 | // gpio_configs->power_config. Then match the name up with |
| 38 | // its definition in "gpio_definitions" to determine the pin id's. |
| 39 | auto gpios = data["gpio_configs"]["power_config"]["power_up_outs"]; |
| 40 | |
| 41 | if (gpios.size() <= 0) |
| 42 | { |
| 43 | log<level::ERR>("Could not find power_up_outs defs", |
| 44 | entry("FILE=%s", gpioDefsFile)); |
| 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | auto defs = data["gpio_definitions"]; |
| 49 | |
| 50 | for (const auto& gpio : gpios) |
| 51 | { |
George Liu | b0e3319 | 2022-06-21 08:28:28 +0800 | [diff] [blame] | 52 | auto gpioEntry = |
| 53 | std::find_if(defs.begin(), defs.end(), [&gpio](const auto& g) { |
| 54 | return g["name"] == gpio["name"]; |
| 55 | }); |
Anthony Wilson | d1c3532 | 2018-12-12 16:10:35 -0600 | [diff] [blame] | 56 | |
| 57 | if (gpioEntry != defs.end()) |
| 58 | { |
| 59 | std::string pin = (*gpioEntry)["pin"]; |
| 60 | bool activeLow = gpio["polarity"]; |
| 61 | |
| 62 | if (!gpioSetValue(pin, !activeLow, false)) |
| 63 | { |
| 64 | log<level::ERR>("chassiskill::gpioSetValue() failed", |
| 65 | entry("PIN=%s", pin.c_str())); |
| 66 | return 1; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | log<level::INFO>("'chassiskill' operation complete", |
| 71 | entry("PIN=%s", pin.c_str())); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return 0; |
| 77 | } |