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