blob: a209230e285fd9e541eaaf3127242063fea9c73c [file] [log] [blame]
Anthony Wilsond1c35322018-12-12 16:10:35 -06001#include "utility.hpp"
2
George Liub0e33192022-06-21 08:28:28 +08003#include <nlohmann/json.hpp>
4#include <phosphor-logging/log.hpp>
5
Anthony Wilsond1c35322018-12-12 16:10:35 -06006#include <algorithm>
Anthony Wilsond1c35322018-12-12 16:10:35 -06007#include <fstream>
Anthony Wilsond1c35322018-12-12 16:10:35 -06008
9using json = nlohmann::json;
10using namespace phosphor::logging;
11using namespace utility;
12
13constexpr auto gpioDefsFile = "/etc/default/obmc/gpio/gpio_defs.json";
14
15int 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 Liub0e33192022-06-21 08:28:28 +080052 auto gpioEntry =
53 std::find_if(defs.begin(), defs.end(), [&gpio](const auto& g) {
54 return g["name"] == gpio["name"];
55 });
Anthony Wilsond1c35322018-12-12 16:10:35 -060056
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}