xref: /openbmc/phosphor-power/power-supply/main.cpp (revision 8b38b177315d40f1f9bc1e4d0784d45ea17cbd5c)
1 /**
2  * Copyright © 2017 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "config.h"
17 
18 #include "device_monitor.hpp"
19 #include "power_supply.hpp"
20 
21 #include <CLI/CLI.hpp>
22 #include <sdeventplus/event.hpp>
23 
24 #include <iostream>
25 
26 using namespace phosphor::power;
27 
28 int main(int argc, char* argv[])
29 {
30     CLI::App app{"PSU Monitor"};
31 
32     std::string objpath{};
33     std::string instnum{};
34     std::string invpath{};
35     std::string records{};
36     std::string syncGPIOPath{};
37     std::string syncGPIONum{};
38 
39     app.add_option("-p,--path", objpath, "Path to location to monitor\n")
40         ->required();
41     app.add_option("-n,--instance", instnum,
42                    "Instance number for this power supply\n")
43         ->required();
44     app.add_option("-i,--inventory", invpath,
45                    "Inventory path for this power supply\n")
46         ->required();
47     app.add_option(
48            "-r,--num-history-records", records,
49            "Number of input power history records to provide on D-Bus\n")
50         ->expected(0, 1);
51     app.add_option(
52            "-a,--sync-gpio-path", syncGPIOPath,
53            "GPIO chip device for the GPIO that performs the sync function\n")
54         ->expected(0, 1);
55     app.add_option("-u,--sync-gpio-num", syncGPIONum,
56                    "GPIO number for the GPIO that performs the sync function\n")
57         ->expected(0, 1);
58 
59     try
60     {
61         app.parse(argc, argv);
62     }
63     catch (const CLI::Error& e)
64     {
65         return app.exit(e);
66     }
67 
68     auto bus = sdbusplus::bus::new_default();
69     auto event = sdeventplus::Event::get_default();
70 
71     // Attach the event object to the bus object so we can
72     // handle both sd_events (for the timers) and dbus signals.
73     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
74 
75     auto objname = "power_supply" + instnum;
76     auto instance = std::stoul(instnum);
77     // The state changes from 0 to 1 when the BMC_POWER_UP line to the power
78     // sequencer is asserted. It can take 50ms for the sequencer to assert the
79     // ENABLE# line that goes to the power supplies. The Witherspoon power
80     // supply can take a max of 100ms from ENABLE# asserted to 12V in spec.
81     // Once 12V in spec., the power supply will nominally take 1 second to
82     // assert DC_GOOD (and update POWER_GOOD Negated), +/1 100ms. That would
83     // give us a 1250ms delay from state=1 to checking STATUS_WORD, however,
84     // the sysfs files will only be updated by the ibm-cffps device driver once
85     // a second, so rounding up from 1 to 5 seconds.
86     std::chrono::seconds powerOnDelay(5);
87     // Timer to delay setting internal presence tracking. Allows for servicing
88     // the power supply.
89     std::chrono::seconds presentDelay(2);
90     auto psuDevice = std::make_unique<psu::PowerSupply>(
91         objname, std::move(instance), std::move(objpath), std::move(invpath),
92         bus, event, powerOnDelay, presentDelay);
93 
94     // Get the number of input power history records to keep in D-Bus.
95     long int numRecords = 0;
96     if (!records.empty())
97     {
98         numRecords = std::stol(records);
99         if (numRecords < 0)
100         {
101             std::cerr << "Invalid number of history records specified.\n";
102             return -6;
103         }
104     }
105 
106     if (numRecords != 0)
107     {
108         // Get the GPIO information for controlling the SYNC signal.
109         // If one is there, they both must be.
110         if ((syncGPIOPath.empty() && !syncGPIONum.empty()) ||
111             (!syncGPIOPath.empty() && syncGPIONum.empty()))
112         {
113             std::cerr << "Invalid sync GPIO number or path\n";
114             return -7;
115         }
116 
117         size_t gpioNum = 0;
118         if (!syncGPIONum.empty())
119         {
120             gpioNum = stoul(syncGPIONum);
121         }
122 
123         std::string name{"ps" + instnum + "_input_power"};
124         std::string basePath =
125             std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
126 
127         psuDevice->enableHistory(basePath, numRecords, syncGPIOPath, gpioNum);
128 
129         // Systemd object manager
130         sdbusplus::server::manager_t objManager{bus, basePath.c_str()};
131 
132         std::string busName =
133             std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name;
134         bus.request_name(busName.c_str());
135     }
136 
137     auto pollInterval = std::chrono::milliseconds(1000);
138     return DeviceMonitor(std::move(psuDevice), event, pollInterval).run();
139 }
140