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