blob: fa7ff6888fa03b0146c184f88215af0cfcd70b5d [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>
George Liu09fdcb52022-06-21 08:55:59 +08004#include <phosphor-logging/lg2.hpp>
George Liub0e33192022-06-21 08:28:28 +08005
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;
Anthony Wilsond1c35322018-12-12 16:10:35 -060010using namespace utility;
11
12constexpr auto gpioDefsFile = "/etc/default/obmc/gpio/gpio_defs.json";
13
14int main(void)
15{
16 std::ifstream gpioDefsStream(gpioDefsFile);
17
18 if (!gpioDefsStream.is_open())
19 {
George Liu09fdcb52022-06-21 08:55:59 +080020 lg2::error("Error opening gpio definitions: {PATH}", "PATH",
21 gpioDefsFile);
Anthony Wilsond1c35322018-12-12 16:10:35 -060022 return 1;
23 }
24
25 auto data = json::parse(gpioDefsStream, nullptr, false);
26
27 if (data.is_discarded())
28 {
George Liu09fdcb52022-06-21 08:55:59 +080029 lg2::error("Error parsing gpio definitions: {PATH}", "PATH",
30 gpioDefsFile);
Anthony Wilsond1c35322018-12-12 16:10:35 -060031 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 Liu09fdcb52022-06-21 08:55:59 +080042 lg2::error("Could not find power_up_outs defs: {PATH}", "PATH",
43 gpioDefsFile);
Anthony Wilsond1c35322018-12-12 16:10:35 -060044 return 1;
45 }
46
47 auto defs = data["gpio_definitions"];
48
49 for (const auto& gpio : gpios)
50 {
George Liub0e33192022-06-21 08:28:28 +080051 auto gpioEntry =
52 std::find_if(defs.begin(), defs.end(), [&gpio](const auto& g) {
53 return g["name"] == gpio["name"];
54 });
Anthony Wilsond1c35322018-12-12 16:10:35 -060055
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 Liu09fdcb52022-06-21 08:55:59 +080063 lg2::error("chassiskill::gpioSetValue() failed: {PIN}", "PIN",
64 pin);
Anthony Wilsond1c35322018-12-12 16:10:35 -060065 return 1;
66 }
67 else
68 {
George Liu09fdcb52022-06-21 08:55:59 +080069 lg2::info("chassiskill::operation complete: {PIN}", "PIN", pin);
Anthony Wilsond1c35322018-12-12 16:10:35 -060070 }
71 }
72 }
73
74 return 0;
75}