xref: /openbmc/phosphor-fan-presence/cooling-type/main.cpp (revision 9d533806250cea56406bdd39e025f0d820c4ed90)
1 #include "cooling_type.hpp"
2 #include "sdbusplus.hpp"
3 
4 #include <CLI/CLI.hpp>
5 #include <phosphor-logging/lg2.hpp>
6 #include <sdbusplus/bus.hpp>
7 
8 #include <iostream>
9 #include <memory>
10 
11 using namespace phosphor::cooling::type;
12 using namespace phosphor::fan::util;
13 
main(int argc,char * argv[])14 int main(int argc, char* argv[])
15 {
16     auto rc = 1;
17 
18     CLI::App app{"Phosphor Cooling type"};
19     std::string objpath{};
20     bool air = false;
21     bool water = false;
22     std::string gpiopath{};
23     std::string keycode{};
24 
25     app.add_option("-p,--path", objpath,
26                    "object path under inventory to have CoolingType updated")
27         ->required();
28     app.add_flag("-a,--air", air,
29                  "Force 'AirCooled' property to be set to true.");
30     app.add_flag("-w,--water", water,
31                  "Force 'WaterCooled' property to be set to true.");
32     app.add_option(
33         "-d,--dev", gpiopath,
34         "Device to read for GPIO pin state to determine 'WaterCooled' (true) and 'AirCooled' (false)");
35     app.add_option("-e,--event", keycode, "Keycode for pin to read");
36     app.allow_extras();
37     try
38     {
39         app.parse(argc, argv);
40     }
41     catch (const CLI::Error& e)
42     {
43         return app.exit(e);
44     }
45 
46     if (objpath.empty())
47     {
48         lg2::error("Bus path argument required");
49         return rc;
50     }
51 
52     auto bus = sdbusplus::bus::new_default();
53     CoolingType coolingType(bus);
54 
55     try
56     {
57         if (air)
58         {
59             coolingType.setAirCooled();
60         }
61 
62         if (water)
63         {
64             coolingType.setWaterCooled();
65         }
66 
67         if (!gpiopath.empty())
68         {
69             if (keycode.empty())
70             {
71                 lg2::error("--event=<keycode> argument required\n");
72                 return rc;
73             }
74             auto gpiocode = std::stoul(keycode);
75             coolingType.readGpio(gpiopath, gpiocode);
76         }
77 
78         coolingType.updateInventory(objpath);
79         rc = 0;
80     }
81     catch (const DBusMethodError& dme)
82     {
83         lg2::error("Uncaught DBus method failure exception "
84                    "Busname: {BUSNAME} "
85                    "Path: {PATH} "
86                    "Interface: {INTERFACE} "
87                    "Method: {METHOD}",
88                    "BUSNAME", dme.busName, "PATH", dme.path, "INTERFACE",
89                    dme.interface, "METHOD", dme.method);
90     }
91     catch (const std::exception& err)
92     {
93         lg2::error("Error with Cooling Type: {ERROR}", "ERROR", err);
94     }
95 
96     return rc;
97 }
98