124e422feSBrandon Wyman /**
224e422feSBrandon Wyman * Copyright © 2017 IBM Corporation
324e422feSBrandon Wyman *
424e422feSBrandon Wyman * Licensed under the Apache License, Version 2.0 (the "License");
524e422feSBrandon Wyman * you may not use this file except in compliance with the License.
624e422feSBrandon Wyman * You may obtain a copy of the License at
724e422feSBrandon Wyman *
824e422feSBrandon Wyman * http://www.apache.org/licenses/LICENSE-2.0
924e422feSBrandon Wyman *
1024e422feSBrandon Wyman * Unless required by applicable law or agreed to in writing, software
1124e422feSBrandon Wyman * distributed under the License is distributed on an "AS IS" BASIS,
1224e422feSBrandon Wyman * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1324e422feSBrandon Wyman * See the License for the specific language governing permissions and
1424e422feSBrandon Wyman * limitations under the License.
1524e422feSBrandon Wyman */
16f0f02b9aSMatt Spinler #include "config.h"
17f0f02b9aSMatt Spinler
18f0f02b9aSMatt Spinler #include "argument.hpp"
19f0f02b9aSMatt Spinler #include "device_monitor.hpp"
20f0f02b9aSMatt Spinler #include "power_supply.hpp"
21f0f02b9aSMatt Spinler
2224e422feSBrandon Wyman #include <phosphor-logging/log.hpp>
23e5a8b473SWilliam A. Kennington III #include <sdeventplus/event.hpp>
2424e422feSBrandon Wyman
252c4fbc4cSPatrick Williams #include <iostream>
262c4fbc4cSPatrick Williams
2724e422feSBrandon Wyman using namespace witherspoon::power;
2824e422feSBrandon Wyman using namespace phosphor::logging;
2924e422feSBrandon Wyman
main(int argc,char * argv[])3024e422feSBrandon Wyman int main(int argc, char* argv[])
3124e422feSBrandon Wyman {
3224e422feSBrandon Wyman auto options = ArgumentParser(argc, argv);
3324e422feSBrandon Wyman
3424e422feSBrandon Wyman auto objpath = (options)["path"];
351db9a9e2SBrandon Wyman auto instnum = (options)["instance"];
361db9a9e2SBrandon Wyman auto invpath = (options)["inventory"];
371db9a9e2SBrandon Wyman if (argc < 4)
3824e422feSBrandon Wyman {
3924e422feSBrandon Wyman std::cerr << std::endl << "Too few arguments" << std::endl;
4024e422feSBrandon Wyman options.usage(argv);
4124e422feSBrandon Wyman return -1;
4224e422feSBrandon Wyman }
4324e422feSBrandon Wyman
4424e422feSBrandon Wyman if (objpath == ArgumentParser::emptyString)
4524e422feSBrandon Wyman {
4624e422feSBrandon Wyman log<level::ERR>("Device monitoring path argument required");
4724e422feSBrandon Wyman return -2;
4824e422feSBrandon Wyman }
4924e422feSBrandon Wyman
501db9a9e2SBrandon Wyman if (instnum == ArgumentParser::emptyString)
511db9a9e2SBrandon Wyman {
521db9a9e2SBrandon Wyman log<level::ERR>("Device monitoring instance number argument required");
531db9a9e2SBrandon Wyman return -3;
541db9a9e2SBrandon Wyman }
551db9a9e2SBrandon Wyman
561db9a9e2SBrandon Wyman if (invpath == ArgumentParser::emptyString)
571db9a9e2SBrandon Wyman {
581db9a9e2SBrandon Wyman log<level::ERR>("Device monitoring inventory path argument required");
591db9a9e2SBrandon Wyman return -4;
601db9a9e2SBrandon Wyman }
611db9a9e2SBrandon Wyman
62431fbe43SBrandon Wyman auto bus = sdbusplus::bus::new_default();
63e5a8b473SWilliam A. Kennington III auto event = sdeventplus::Event::get_default();
6424e422feSBrandon Wyman
6524e422feSBrandon Wyman // Attach the event object to the bus object so we can
6624e422feSBrandon Wyman // handle both sd_events (for the timers) and dbus signals.
67e5a8b473SWilliam A. Kennington III bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
6824e422feSBrandon Wyman
69431fbe43SBrandon Wyman auto objname = "power_supply" + instnum;
70431fbe43SBrandon Wyman auto instance = std::stoul(instnum);
711dd3c9aeSBrandon Wyman // The state changes from 0 to 1 when the BMC_POWER_UP line to the power
721dd3c9aeSBrandon Wyman // sequencer is asserted. It can take 50ms for the sequencer to assert the
731dd3c9aeSBrandon Wyman // ENABLE# line that goes to the power supplies. The Witherspoon power
741dd3c9aeSBrandon Wyman // supply can take a max of 100ms from ENABLE# asserted to 12V in spec.
751dd3c9aeSBrandon Wyman // Once 12V in spec., the power supply will nominally take 1 second to
761dd3c9aeSBrandon Wyman // assert DC_GOOD (and update POWER_GOOD Negated), +/1 100ms. That would
771dd3c9aeSBrandon Wyman // give us a 1250ms delay from state=1 to checking STATUS_WORD, however,
781dd3c9aeSBrandon Wyman // the sysfs files will only be updated by the ibm-cffps device driver once
79b08efa4fSBrandon Wyman // a second, so rounding up from 1 to 5 seconds.
80b08efa4fSBrandon Wyman std::chrono::seconds powerOnDelay(5);
81590fc28aSBrandon Wyman // Timer to delay setting internal presence tracking. Allows for servicing
82590fc28aSBrandon Wyman // the power supply.
83590fc28aSBrandon Wyman std::chrono::seconds presentDelay(2);
84f0f02b9aSMatt Spinler auto psuDevice = std::make_unique<psu::PowerSupply>(
85f0f02b9aSMatt Spinler objname, std::move(instance), std::move(objpath), std::move(invpath),
86f0f02b9aSMatt Spinler bus, event, powerOnDelay, presentDelay);
8710295547SBrandon Wyman
88c87eb826SMatt Spinler // Get the number of input power history records to keep in D-Bus.
89c87eb826SMatt Spinler long int numRecords = 0;
90c87eb826SMatt Spinler auto records = (options)["num-history-records"];
91c87eb826SMatt Spinler if (records != ArgumentParser::emptyString)
92c87eb826SMatt Spinler {
93c87eb826SMatt Spinler numRecords = std::stol(records);
94c87eb826SMatt Spinler if (numRecords < 0)
95c87eb826SMatt Spinler {
96c87eb826SMatt Spinler std::cerr << "Invalid number of history records specified.\n";
97c87eb826SMatt Spinler return -6;
98c87eb826SMatt Spinler }
99c87eb826SMatt Spinler }
100c87eb826SMatt Spinler
101c87eb826SMatt Spinler if (numRecords != 0)
102c87eb826SMatt Spinler {
103c87eb826SMatt Spinler // Get the GPIO information for controlling the SYNC signal.
104c87eb826SMatt Spinler // If one is there, they both must be.
105c87eb826SMatt Spinler auto syncGPIOPath = (options)["sync-gpio-path"];
106c87eb826SMatt Spinler auto syncGPIONum = (options)["sync-gpio-num"];
107c87eb826SMatt Spinler
108c87eb826SMatt Spinler if (((syncGPIOPath == ArgumentParser::emptyString) &&
109c87eb826SMatt Spinler (syncGPIONum != ArgumentParser::emptyString)) ||
110c87eb826SMatt Spinler ((syncGPIOPath != ArgumentParser::emptyString) &&
111c87eb826SMatt Spinler (syncGPIONum == ArgumentParser::emptyString)))
112c87eb826SMatt Spinler {
113c87eb826SMatt Spinler std::cerr << "Invalid sync GPIO number or path\n";
114c87eb826SMatt Spinler return -7;
115c87eb826SMatt Spinler }
116c87eb826SMatt Spinler
117c87eb826SMatt Spinler size_t gpioNum = 0;
118c87eb826SMatt Spinler if (syncGPIONum != ArgumentParser::emptyString)
119c87eb826SMatt Spinler {
120c87eb826SMatt Spinler gpioNum = stoul(syncGPIONum);
121c87eb826SMatt Spinler }
122c87eb826SMatt Spinler
123c87eb826SMatt Spinler std::string name{"ps" + instnum + "_input_power"};
124*befec58bSPatrick Williams std::string basePath =
125*befec58bSPatrick Williams std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
126c87eb826SMatt Spinler
127f0f02b9aSMatt Spinler psuDevice->enableHistory(basePath, numRecords, syncGPIOPath, gpioNum);
128c87eb826SMatt Spinler
129c87eb826SMatt Spinler // Systemd object manager
1301426a10bSPatrick Williams sdbusplus::server::manager_t objManager{bus, basePath.c_str()};
131c87eb826SMatt Spinler
132*befec58bSPatrick Williams std::string busName =
133*befec58bSPatrick Williams std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name;
134c87eb826SMatt Spinler bus.request_name(busName.c_str());
135c87eb826SMatt Spinler }
136c87eb826SMatt Spinler
1371db9a9e2SBrandon Wyman auto pollInterval = std::chrono::milliseconds(1000);
138e5a8b473SWilliam A. Kennington III return DeviceMonitor(std::move(psuDevice), event, pollInterval).run();
13924e422feSBrandon Wyman }
140