blob: 9049fd6d64ff99128806fad7d2af39f4c5d6db3f [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>
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070022#include <sdeventplus/event.hpp>
Brandon Wyman24e422f2017-07-25 19:40:14 -050023
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060024#include <iostream>
25
Lei YUab093322019-10-09 16:43:22 +080026using namespace phosphor::power;
Brandon Wyman24e422f2017-07-25 19:40:14 -050027using namespace phosphor::logging;
28
29int main(int argc, char* argv[])
30{
George Liub60823f2023-08-14 15:50:31 +080031 CLI::App app{"PSU Monitor"};
Brandon Wyman24e422f2017-07-25 19:40:14 -050032
George Liub60823f2023-08-14 15:50:31 +080033 std::string objpath{};
34 std::string instnum{};
35 std::string invpath{};
36 std::string records{};
37 std::string syncGPIOPath{};
38 std::string syncGPIONum{};
39
40 app.add_option("-p,--path", objpath, "Path to location to monitor\n")
41 ->required();
42 app.add_option("-n,--instance", instnum,
43 "Instance number for this power supply\n")
44 ->required();
45 app.add_option("-i,--inventory", invpath,
46 "Inventory path for this power supply\n")
47 ->required();
48 app.add_option(
49 "-r,--num-history-records", records,
50 "Number of input power history records to provide on D-Bus\n")
51 ->expected(0, 1);
52 app.add_option(
53 "-a,--sync-gpio-path", syncGPIOPath,
54 "GPIO chip device for the GPIO that performs the sync function\n")
55 ->expected(0, 1);
56 app.add_option("-u,--sync-gpio-num", syncGPIONum,
57 "GPIO number for the GPIO that performs the sync function\n")
58 ->expected(0, 1);
59
60 try
Brandon Wyman24e422f2017-07-25 19:40:14 -050061 {
George Liub60823f2023-08-14 15:50:31 +080062 app.parse(argc, argv);
Brandon Wyman24e422f2017-07-25 19:40:14 -050063 }
George Liub60823f2023-08-14 15:50:31 +080064 catch (const CLI::Error& e)
Brandon Wyman24e422f2017-07-25 19:40:14 -050065 {
George Liub60823f2023-08-14 15:50:31 +080066 return app.exit(e);
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050067 }
68
Brandon Wyman431fbe42017-08-18 16:22:09 -050069 auto bus = sdbusplus::bus::new_default();
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070070 auto event = sdeventplus::Event::get_default();
Brandon Wyman24e422f2017-07-25 19:40:14 -050071
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -070072 // Attach the event object to the bus object so we can
73 // handle both sd_events (for the timers) and dbus signals.
74 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Brandon Wyman24e422f2017-07-25 19:40:14 -050075
Brandon Wyman431fbe42017-08-18 16:22:09 -050076 auto objname = "power_supply" + instnum;
77 auto instance = std::stoul(instnum);
Brandon Wyman1dd3c9a2017-10-10 13:31:18 -050078 // The state changes from 0 to 1 when the BMC_POWER_UP line to the power
79 // sequencer is asserted. It can take 50ms for the sequencer to assert the
80 // ENABLE# line that goes to the power supplies. The Witherspoon power
81 // supply can take a max of 100ms from ENABLE# asserted to 12V in spec.
82 // Once 12V in spec., the power supply will nominally take 1 second to
83 // assert DC_GOOD (and update POWER_GOOD Negated), +/1 100ms. That would
84 // give us a 1250ms delay from state=1 to checking STATUS_WORD, however,
85 // the sysfs files will only be updated by the ibm-cffps device driver once
Brandon Wymanb08efa42017-11-16 09:41:51 -060086 // a second, so rounding up from 1 to 5 seconds.
87 std::chrono::seconds powerOnDelay(5);
Brandon Wyman590fc282017-11-01 18:22:25 -050088 // Timer to delay setting internal presence tracking. Allows for servicing
89 // the power supply.
90 std::chrono::seconds presentDelay(2);
Matt Spinlerf0f02b92018-10-25 16:12:43 -050091 auto psuDevice = std::make_unique<psu::PowerSupply>(
92 objname, std::move(instance), std::move(objpath), std::move(invpath),
93 bus, event, powerOnDelay, presentDelay);
Brandon Wyman10295542017-08-09 18:20:44 -050094
Matt Spinlerc87eb822018-01-18 14:44:47 -060095 // Get the number of input power history records to keep in D-Bus.
96 long int numRecords = 0;
George Liub60823f2023-08-14 15:50:31 +080097 if (!records.empty())
Matt Spinlerc87eb822018-01-18 14:44:47 -060098 {
99 numRecords = std::stol(records);
100 if (numRecords < 0)
101 {
102 std::cerr << "Invalid number of history records specified.\n";
103 return -6;
104 }
105 }
106
107 if (numRecords != 0)
108 {
109 // Get the GPIO information for controlling the SYNC signal.
110 // If one is there, they both must be.
George Liub60823f2023-08-14 15:50:31 +0800111 if ((syncGPIOPath.empty() && !syncGPIONum.empty()) ||
112 (!syncGPIOPath.empty() && syncGPIONum.empty()))
Matt Spinlerc87eb822018-01-18 14:44:47 -0600113 {
114 std::cerr << "Invalid sync GPIO number or path\n";
115 return -7;
116 }
117
118 size_t gpioNum = 0;
George Liub60823f2023-08-14 15:50:31 +0800119 if (!syncGPIONum.empty())
Matt Spinlerc87eb822018-01-18 14:44:47 -0600120 {
121 gpioNum = stoul(syncGPIONum);
122 }
123
124 std::string name{"ps" + instnum + "_input_power"};
Patrick Williamsf5402192024-08-16 15:20:53 -0400125 std::string basePath =
126 std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name;
Matt Spinlerc87eb822018-01-18 14:44:47 -0600127
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500128 psuDevice->enableHistory(basePath, numRecords, syncGPIOPath, gpioNum);
Matt Spinlerc87eb822018-01-18 14:44:47 -0600129
130 // Systemd object manager
Patrick Williams7354ce62022-07-22 19:26:56 -0500131 sdbusplus::server::manager_t objManager{bus, basePath.c_str()};
Matt Spinlerc87eb822018-01-18 14:44:47 -0600132
Patrick Williamsf5402192024-08-16 15:20:53 -0400133 std::string busName =
134 std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name;
Matt Spinlerc87eb822018-01-18 14:44:47 -0600135 bus.request_name(busName.c_str());
136 }
137
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500138 auto pollInterval = std::chrono::milliseconds(1000);
William A. Kennington IIIe5a8b472018-10-18 00:40:04 -0700139 return DeviceMonitor(std::move(psuDevice), event, pollInterval).run();
Brandon Wyman24e422f2017-07-25 19:40:14 -0500140}