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