blob: d441dbefb6df4337397c3e9bfe37d7b1c8c2d939 [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
Matt Spinlerf0f02b92018-10-25 16:12:43 -050018#include "device_monitor.hpp"
19#include "power_supply.hpp"
20
George Liub60823f2023-08-14 15:50:31 +080021#include <CLI/CLI.hpp>
Brandon Wyman24e422f2017-07-25 19:40:14 -050022#include <phosphor-logging/log.hpp>
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070023#include <sdeventplus/event.hpp>
Brandon Wyman24e422f2017-07-25 19:40:14 -050024
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060025#include <iostream>
26
Lei YUab093322019-10-09 16:43:22 +080027using namespace phosphor::power;
Brandon Wyman24e422f2017-07-25 19:40:14 -050028using namespace phosphor::logging;
29
30int main(int argc, char* argv[])
31{
George Liub60823f2023-08-14 15:50:31 +080032 CLI::App app{"PSU Monitor"};
Brandon Wyman24e422f2017-07-25 19:40:14 -050033
George Liub60823f2023-08-14 15:50:31 +080034 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
Brandon Wyman24e422f2017-07-25 19:40:14 -050062 {
George Liub60823f2023-08-14 15:50:31 +080063 app.parse(argc, argv);
Brandon Wyman24e422f2017-07-25 19:40:14 -050064 }
George Liub60823f2023-08-14 15:50:31 +080065 catch (const CLI::Error& e)
Brandon Wyman24e422f2017-07-25 19:40:14 -050066 {
George Liub60823f2023-08-14 15:50:31 +080067 return app.exit(e);
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050068 }
69
Brandon Wyman431fbe42017-08-18 16:22:09 -050070 auto bus = sdbusplus::bus::new_default();
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070071 auto event = sdeventplus::Event::get_default();
Brandon Wyman24e422f2017-07-25 19:40:14 -050072
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070073 // 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);
Brandon Wyman24e422f2017-07-25 19:40:14 -050076
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);
Matt Spinlerf0f02b92018-10-25 16:12:43 -050092 auto psuDevice = std::make_unique<psu::PowerSupply>(
93 objname, std::move(instance), std::move(objpath), std::move(invpath),
94 bus, event, powerOnDelay, presentDelay);
Brandon Wyman10295542017-08-09 18:20:44 -050095
Matt Spinlerc87eb822018-01-18 14:44:47 -060096 // Get the number of input power history records to keep in D-Bus.
97 long int numRecords = 0;
George Liub60823f2023-08-14 15:50:31 +080098 if (!records.empty())
Matt Spinlerc87eb822018-01-18 14:44:47 -060099 {
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.
George Liub60823f2023-08-14 15:50:31 +0800112 if ((syncGPIOPath.empty() && !syncGPIONum.empty()) ||
113 (!syncGPIOPath.empty() && syncGPIONum.empty()))
Matt Spinlerc87eb822018-01-18 14:44:47 -0600114 {
115 std::cerr << "Invalid sync GPIO number or path\n";
116 return -7;
117 }
118
119 size_t gpioNum = 0;
George Liub60823f2023-08-14 15:50:31 +0800120 if (!syncGPIONum.empty())
Matt Spinlerc87eb822018-01-18 14:44:47 -0600121 {
122 gpioNum = stoul(syncGPIONum);
123 }
124
125 std::string name{"ps" + instnum + "_input_power"};
Patrick Williams48781ae2023-05-10 07:50:50 -0500126 std::string basePath = std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' +
127 name;
Matt Spinlerc87eb822018-01-18 14:44:47 -0600128
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500129 psuDevice->enableHistory(basePath, numRecords, syncGPIOPath, gpioNum);
Matt Spinlerc87eb822018-01-18 14:44:47 -0600130
131 // Systemd object manager
Patrick Williams7354ce62022-07-22 19:26:56 -0500132 sdbusplus::server::manager_t objManager{bus, basePath.c_str()};
Matt Spinlerc87eb822018-01-18 14:44:47 -0600133
Patrick Williams48781ae2023-05-10 07:50:50 -0500134 std::string busName = std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' +
135 name;
Matt Spinlerc87eb822018-01-18 14:44:47 -0600136 bus.request_name(busName.c_str());
137 }
138
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500139 auto pollInterval = std::chrono::milliseconds(1000);
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -0700140 return DeviceMonitor(std::move(psuDevice), event, pollInterval).run();
Brandon Wyman24e422f2017-07-25 19:40:14 -0500141}