blob: 93b4b77b68f14d2b4cfbc8ffa62956bde99592fa [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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050016#include "config.h"
17
18#include "argument.hpp"
19#include "device_monitor.hpp"
20#include "power_supply.hpp"
21
Brandon Wyman24e422f2017-07-25 19:40:14 -050022#include <iostream>
23#include <phosphor-logging/log.hpp>
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070024#include <sdeventplus/event.hpp>
Brandon Wyman24e422f2017-07-25 19:40:14 -050025
26using namespace witherspoon::power;
27using namespace phosphor::logging;
28
29int main(int argc, char* argv[])
30{
Brandon Wyman24e422f2017-07-25 19:40:14 -050031 auto options = ArgumentParser(argc, argv);
32
33 auto objpath = (options)["path"];
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050034 auto instnum = (options)["instance"];
35 auto invpath = (options)["inventory"];
36 if (argc < 4)
Brandon Wyman24e422f2017-07-25 19:40:14 -050037 {
38 std::cerr << std::endl << "Too few arguments" << std::endl;
39 options.usage(argv);
40 return -1;
41 }
42
43 if (objpath == ArgumentParser::emptyString)
44 {
45 log<level::ERR>("Device monitoring path argument required");
46 return -2;
47 }
48
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050049 if (instnum == ArgumentParser::emptyString)
50 {
51 log<level::ERR>("Device monitoring instance number argument required");
52 return -3;
53 }
54
55 if (invpath == ArgumentParser::emptyString)
56 {
57 log<level::ERR>("Device monitoring inventory path argument required");
58 return -4;
59 }
60
Brandon Wyman431fbe42017-08-18 16:22:09 -050061 auto bus = sdbusplus::bus::new_default();
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070062 auto event = sdeventplus::Event::get_default();
Brandon Wyman24e422f2017-07-25 19:40:14 -050063
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070064 // Attach the event object to the bus object so we can
65 // handle both sd_events (for the timers) and dbus signals.
66 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Brandon Wyman24e422f2017-07-25 19:40:14 -050067
Brandon Wyman431fbe42017-08-18 16:22:09 -050068 auto objname = "power_supply" + instnum;
69 auto instance = std::stoul(instnum);
Brandon Wyman1dd3c9a2017-10-10 13:31:18 -050070 // The state changes from 0 to 1 when the BMC_POWER_UP line to the power
71 // sequencer is asserted. It can take 50ms for the sequencer to assert the
72 // ENABLE# line that goes to the power supplies. The Witherspoon power
73 // supply can take a max of 100ms from ENABLE# asserted to 12V in spec.
74 // Once 12V in spec., the power supply will nominally take 1 second to
75 // assert DC_GOOD (and update POWER_GOOD Negated), +/1 100ms. That would
76 // give us a 1250ms delay from state=1 to checking STATUS_WORD, however,
77 // the sysfs files will only be updated by the ibm-cffps device driver once
Brandon Wymanb08efa42017-11-16 09:41:51 -060078 // a second, so rounding up from 1 to 5 seconds.
79 std::chrono::seconds powerOnDelay(5);
Brandon Wyman590fc282017-11-01 18:22:25 -050080 // Timer to delay setting internal presence tracking. Allows for servicing
81 // the power supply.
82 std::chrono::seconds presentDelay(2);
Matt Spinlerf0f02b92018-10-25 16:12:43 -050083 auto psuDevice = std::make_unique<psu::PowerSupply>(
84 objname, std::move(instance), std::move(objpath), std::move(invpath),
85 bus, event, powerOnDelay, presentDelay);
Brandon Wyman10295542017-08-09 18:20:44 -050086
Matt Spinlerc87eb822018-01-18 14:44:47 -060087 // Get the number of input power history records to keep in D-Bus.
88 long int numRecords = 0;
89 auto records = (options)["num-history-records"];
90 if (records != ArgumentParser::emptyString)
91 {
92 numRecords = std::stol(records);
93 if (numRecords < 0)
94 {
95 std::cerr << "Invalid number of history records specified.\n";
96 return -6;
97 }
98 }
99
100 if (numRecords != 0)
101 {
102 // Get the GPIO information for controlling the SYNC signal.
103 // If one is there, they both must be.
104 auto syncGPIOPath = (options)["sync-gpio-path"];
105 auto syncGPIONum = (options)["sync-gpio-num"];
106
107 if (((syncGPIOPath == ArgumentParser::emptyString) &&
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500108 (syncGPIONum != ArgumentParser::emptyString)) ||
Matt Spinlerc87eb822018-01-18 14:44:47 -0600109 ((syncGPIOPath != ArgumentParser::emptyString) &&
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500110 (syncGPIONum == ArgumentParser::emptyString)))
Matt Spinlerc87eb822018-01-18 14:44:47 -0600111 {
112 std::cerr << "Invalid sync GPIO number or path\n";
113 return -7;
114 }
115
116 size_t gpioNum = 0;
117 if (syncGPIONum != ArgumentParser::emptyString)
118 {
119 gpioNum = stoul(syncGPIONum);
120 }
121
122 std::string name{"ps" + instnum + "_input_power"};
123 std::string basePath =
124 std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
125
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500126 psuDevice->enableHistory(basePath, numRecords, syncGPIOPath, gpioNum);
Matt Spinlerc87eb822018-01-18 14:44:47 -0600127
128 // Systemd object manager
129 sdbusplus::server::manager::manager objManager{bus, basePath.c_str()};
130
131 std::string busName =
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500132 std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name;
Matt Spinlerc87eb822018-01-18 14:44:47 -0600133 bus.request_name(busName.c_str());
134 }
135
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500136 auto pollInterval = std::chrono::milliseconds(1000);
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -0700137 return DeviceMonitor(std::move(psuDevice), event, pollInterval).run();
Brandon Wyman24e422f2017-07-25 19:40:14 -0500138}