blob: ee93f7423ccd9e67e4902114ecaa0747770f62cd [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>
18#include <systemd/sd-daemon.h>
19#include "argument.hpp"
Matt Spinlerc87eb822018-01-18 14:44:47 -060020#include "config.h"
Brandon Wyman24e422f2017-07-25 19:40:14 -050021#include "event.hpp"
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050022#include "power_supply.hpp"
23#include "device_monitor.hpp"
Brandon Wyman24e422f2017-07-25 19:40:14 -050024
25using namespace witherspoon::power;
26using namespace phosphor::logging;
27
28int main(int argc, char* argv[])
29{
Brandon Wyman24e422f2017-07-25 19:40:14 -050030 auto options = ArgumentParser(argc, argv);
31
32 auto objpath = (options)["path"];
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050033 auto instnum = (options)["instance"];
34 auto invpath = (options)["inventory"];
35 if (argc < 4)
Brandon Wyman24e422f2017-07-25 19:40:14 -050036 {
37 std::cerr << std::endl << "Too few arguments" << std::endl;
38 options.usage(argv);
39 return -1;
40 }
41
42 if (objpath == ArgumentParser::emptyString)
43 {
44 log<level::ERR>("Device monitoring path argument required");
45 return -2;
46 }
47
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050048 if (instnum == ArgumentParser::emptyString)
49 {
50 log<level::ERR>("Device monitoring instance number argument required");
51 return -3;
52 }
53
54 if (invpath == ArgumentParser::emptyString)
55 {
56 log<level::ERR>("Device monitoring inventory path argument required");
57 return -4;
58 }
59
Brandon Wyman24e422f2017-07-25 19:40:14 -050060 sd_event* events = nullptr;
61
62 auto r = sd_event_default(&events);
63 if (r < 0)
64 {
65 log<level::ERR>("Failed call to sd_event_default()",
66 entry("ERROR=%s", strerror(-r)));
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050067 return -5;
Brandon Wyman24e422f2017-07-25 19:40:14 -050068 }
69
Brandon Wyman431fbe42017-08-18 16:22:09 -050070 auto bus = sdbusplus::bus::new_default();
Brandon Wyman24e422f2017-07-25 19:40:14 -050071 witherspoon::power::event::Event eventPtr{events};
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(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL);
76
Brandon Wyman431fbe42017-08-18 16:22:09 -050077 auto objname = "power_supply" + instnum;
78 auto instance = std::stoul(instnum);
Brandon Wyman1dd3c9a2017-10-10 13:31:18 -050079 // 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
Brandon Wymanb08efa42017-11-16 09:41:51 -060087 // a second, so rounding up from 1 to 5 seconds.
88 std::chrono::seconds powerOnDelay(5);
Brandon Wyman590fc282017-11-01 18:22:25 -050089 // Timer to delay setting internal presence tracking. Allows for servicing
90 // the power supply.
91 std::chrono::seconds presentDelay(2);
Brandon Wyman431fbe42017-08-18 16:22:09 -050092 auto psuDevice = std::make_unique<psu::PowerSupply>(objname,
93 std::move(instance),
94 std::move(objpath),
95 std::move(invpath),
96 bus,
97 eventPtr,
Brandon Wyman590fc282017-11-01 18:22:25 -050098 powerOnDelay,
99 presentDelay);
Brandon Wyman10295542017-08-09 18:20:44 -0500100
Matt Spinlerc87eb822018-01-18 14:44:47 -0600101 // Get the number of input power history records to keep in D-Bus.
102 long int numRecords = 0;
103 auto records = (options)["num-history-records"];
104 if (records != ArgumentParser::emptyString)
105 {
106 numRecords = std::stol(records);
107 if (numRecords < 0)
108 {
109 std::cerr << "Invalid number of history records specified.\n";
110 return -6;
111 }
112 }
113
114 if (numRecords != 0)
115 {
116 // Get the GPIO information for controlling the SYNC signal.
117 // If one is there, they both must be.
118 auto syncGPIOPath = (options)["sync-gpio-path"];
119 auto syncGPIONum = (options)["sync-gpio-num"];
120
121 if (((syncGPIOPath == ArgumentParser::emptyString) &&
122 (syncGPIONum != ArgumentParser::emptyString)) ||
123 ((syncGPIOPath != ArgumentParser::emptyString) &&
124 (syncGPIONum == ArgumentParser::emptyString)))
125 {
126 std::cerr << "Invalid sync GPIO number or path\n";
127 return -7;
128 }
129
130 size_t gpioNum = 0;
131 if (syncGPIONum != ArgumentParser::emptyString)
132 {
133 gpioNum = stoul(syncGPIONum);
134 }
135
136 std::string name{"ps" + instnum + "_input_power"};
137 std::string basePath =
138 std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
139
140 psuDevice->enableHistory(basePath,
141 numRecords,
142 syncGPIOPath,
143 gpioNum);
144
145 // Systemd object manager
146 sdbusplus::server::manager::manager objManager{bus, basePath.c_str()};
147
148 std::string busName =
149 std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name;
150 bus.request_name(busName.c_str());
151 }
152
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500153 auto pollInterval = std::chrono::milliseconds(1000);
154 DeviceMonitor mainloop(std::move(psuDevice), eventPtr, pollInterval);
155 mainloop.run();
Brandon Wyman24e422f2017-07-25 19:40:14 -0500156
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500157 return 0;
Brandon Wyman24e422f2017-07-25 19:40:14 -0500158}