Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 1 | /** |
| 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 Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 18 | #include "device_monitor.hpp" |
| 19 | #include "power_supply.hpp" |
| 20 | |
George Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 21 | #include <CLI/CLI.hpp> |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 22 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 23 | #include <sdeventplus/event.hpp> |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 24 | |
Brandon Wyman | d1bc4ce | 2019-12-13 14:20:34 -0600 | [diff] [blame] | 25 | #include <iostream> |
| 26 | |
Lei YU | ab09332 | 2019-10-09 16:43:22 +0800 | [diff] [blame] | 27 | using namespace phosphor::power; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 28 | using namespace phosphor::logging; |
| 29 | |
| 30 | int main(int argc, char* argv[]) |
| 31 | { |
George Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 32 | CLI::App app{"PSU Monitor"}; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 33 | |
George Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 34 | 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 Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 62 | { |
George Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 63 | app.parse(argc, argv); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 64 | } |
George Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 65 | catch (const CLI::Error& e) |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 66 | { |
George Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 67 | return app.exit(e); |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 70 | auto bus = sdbusplus::bus::new_default(); |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 71 | auto event = sdeventplus::Event::get_default(); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 72 | |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 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(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 76 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 77 | auto objname = "power_supply" + instnum; |
| 78 | auto instance = std::stoul(instnum); |
Brandon Wyman | 1dd3c9a | 2017-10-10 13:31:18 -0500 | [diff] [blame] | 79 | // 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 Wyman | b08efa4 | 2017-11-16 09:41:51 -0600 | [diff] [blame] | 87 | // a second, so rounding up from 1 to 5 seconds. |
| 88 | std::chrono::seconds powerOnDelay(5); |
Brandon Wyman | 590fc28 | 2017-11-01 18:22:25 -0500 | [diff] [blame] | 89 | // Timer to delay setting internal presence tracking. Allows for servicing |
| 90 | // the power supply. |
| 91 | std::chrono::seconds presentDelay(2); |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 92 | auto psuDevice = std::make_unique<psu::PowerSupply>( |
| 93 | objname, std::move(instance), std::move(objpath), std::move(invpath), |
| 94 | bus, event, powerOnDelay, presentDelay); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 95 | |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 96 | // Get the number of input power history records to keep in D-Bus. |
| 97 | long int numRecords = 0; |
George Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 98 | if (!records.empty()) |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 99 | { |
| 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 Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 112 | if ((syncGPIOPath.empty() && !syncGPIONum.empty()) || |
| 113 | (!syncGPIOPath.empty() && syncGPIONum.empty())) |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 114 | { |
| 115 | std::cerr << "Invalid sync GPIO number or path\n"; |
| 116 | return -7; |
| 117 | } |
| 118 | |
| 119 | size_t gpioNum = 0; |
George Liu | b60823f | 2023-08-14 15:50:31 +0800 | [diff] [blame] | 120 | if (!syncGPIONum.empty()) |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 121 | { |
| 122 | gpioNum = stoul(syncGPIONum); |
| 123 | } |
| 124 | |
| 125 | std::string name{"ps" + instnum + "_input_power"}; |
Patrick Williams | 48781ae | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 126 | std::string basePath = std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + |
| 127 | name; |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 128 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 129 | psuDevice->enableHistory(basePath, numRecords, syncGPIOPath, gpioNum); |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 130 | |
| 131 | // Systemd object manager |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 132 | sdbusplus::server::manager_t objManager{bus, basePath.c_str()}; |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 133 | |
Patrick Williams | 48781ae | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 134 | std::string busName = std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + |
| 135 | name; |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 136 | bus.request_name(busName.c_str()); |
| 137 | } |
| 138 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 139 | auto pollInterval = std::chrono::milliseconds(1000); |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 140 | return DeviceMonitor(std::move(psuDevice), event, pollInterval).run(); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 141 | } |