blob: 62249c5b03882c7ea0ab44d342e8dad768fa0ab7 [file] [log] [blame]
Brandon Wyman24e422f2017-07-25 19:40:14 -05001/**
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 <iostream>
17#include <phosphor-logging/log.hpp>
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070018#include <sdeventplus/event.hpp>
Brandon Wyman24e422f2017-07-25 19:40:14 -050019#include "argument.hpp"
Matt Spinlerc87eb822018-01-18 14:44:47 -060020#include "config.h"
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050021#include "power_supply.hpp"
22#include "device_monitor.hpp"
Brandon Wyman24e422f2017-07-25 19:40:14 -050023
24using namespace witherspoon::power;
25using namespace phosphor::logging;
26
27int main(int argc, char* argv[])
28{
Brandon Wyman24e422f2017-07-25 19:40:14 -050029 auto options = ArgumentParser(argc, argv);
30
31 auto objpath = (options)["path"];
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050032 auto instnum = (options)["instance"];
33 auto invpath = (options)["inventory"];
34 if (argc < 4)
Brandon Wyman24e422f2017-07-25 19:40:14 -050035 {
36 std::cerr << std::endl << "Too few arguments" << std::endl;
37 options.usage(argv);
38 return -1;
39 }
40
41 if (objpath == ArgumentParser::emptyString)
42 {
43 log<level::ERR>("Device monitoring path argument required");
44 return -2;
45 }
46
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050047 if (instnum == ArgumentParser::emptyString)
48 {
49 log<level::ERR>("Device monitoring instance number argument required");
50 return -3;
51 }
52
53 if (invpath == ArgumentParser::emptyString)
54 {
55 log<level::ERR>("Device monitoring inventory path argument required");
56 return -4;
57 }
58
Brandon Wyman431fbe42017-08-18 16:22:09 -050059 auto bus = sdbusplus::bus::new_default();
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070060 auto event = sdeventplus::Event::get_default();
Brandon Wyman24e422f2017-07-25 19:40:14 -050061
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070062 // Attach the event object to the bus object so we can
63 // handle both sd_events (for the timers) and dbus signals.
64 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Brandon Wyman24e422f2017-07-25 19:40:14 -050065
Brandon Wyman431fbe42017-08-18 16:22:09 -050066 auto objname = "power_supply" + instnum;
67 auto instance = std::stoul(instnum);
Brandon Wyman1dd3c9a2017-10-10 13:31:18 -050068 // The state changes from 0 to 1 when the BMC_POWER_UP line to the power
69 // sequencer is asserted. It can take 50ms for the sequencer to assert the
70 // ENABLE# line that goes to the power supplies. The Witherspoon power
71 // supply can take a max of 100ms from ENABLE# asserted to 12V in spec.
72 // Once 12V in spec., the power supply will nominally take 1 second to
73 // assert DC_GOOD (and update POWER_GOOD Negated), +/1 100ms. That would
74 // give us a 1250ms delay from state=1 to checking STATUS_WORD, however,
75 // the sysfs files will only be updated by the ibm-cffps device driver once
Brandon Wymanb08efa42017-11-16 09:41:51 -060076 // a second, so rounding up from 1 to 5 seconds.
77 std::chrono::seconds powerOnDelay(5);
Brandon Wyman590fc282017-11-01 18:22:25 -050078 // Timer to delay setting internal presence tracking. Allows for servicing
79 // the power supply.
80 std::chrono::seconds presentDelay(2);
Brandon Wyman431fbe42017-08-18 16:22:09 -050081 auto psuDevice = std::make_unique<psu::PowerSupply>(objname,
82 std::move(instance),
83 std::move(objpath),
84 std::move(invpath),
85 bus,
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070086 event,
Brandon Wyman590fc282017-11-01 18:22:25 -050087 powerOnDelay,
88 presentDelay);
Brandon Wyman10295542017-08-09 18:20:44 -050089
Matt Spinlerc87eb822018-01-18 14:44:47 -060090 // Get the number of input power history records to keep in D-Bus.
91 long int numRecords = 0;
92 auto records = (options)["num-history-records"];
93 if (records != ArgumentParser::emptyString)
94 {
95 numRecords = std::stol(records);
96 if (numRecords < 0)
97 {
98 std::cerr << "Invalid number of history records specified.\n";
99 return -6;
100 }
101 }
102
103 if (numRecords != 0)
104 {
105 // Get the GPIO information for controlling the SYNC signal.
106 // If one is there, they both must be.
107 auto syncGPIOPath = (options)["sync-gpio-path"];
108 auto syncGPIONum = (options)["sync-gpio-num"];
109
110 if (((syncGPIOPath == ArgumentParser::emptyString) &&
111 (syncGPIONum != ArgumentParser::emptyString)) ||
112 ((syncGPIOPath != ArgumentParser::emptyString) &&
113 (syncGPIONum == ArgumentParser::emptyString)))
114 {
115 std::cerr << "Invalid sync GPIO number or path\n";
116 return -7;
117 }
118
119 size_t gpioNum = 0;
120 if (syncGPIONum != ArgumentParser::emptyString)
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,
130 numRecords,
131 syncGPIOPath,
132 gpioNum);
133
134 // Systemd object manager
135 sdbusplus::server::manager::manager objManager{bus, basePath.c_str()};
136
137 std::string busName =
138 std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name;
139 bus.request_name(busName.c_str());
140 }
141
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500142 auto pollInterval = std::chrono::milliseconds(1000);
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -0700143 return DeviceMonitor(std::move(psuDevice), event, pollInterval).run();
Brandon Wyman24e422f2017-07-25 19:40:14 -0500144}