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 | |
| 18 | #include "argument.hpp" |
| 19 | #include "device_monitor.hpp" |
| 20 | #include "power_supply.hpp" |
| 21 | |
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 | { |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 32 | auto options = ArgumentParser(argc, argv); |
| 33 | |
| 34 | auto objpath = (options)["path"]; |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 35 | auto instnum = (options)["instance"]; |
| 36 | auto invpath = (options)["inventory"]; |
| 37 | if (argc < 4) |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 38 | { |
| 39 | std::cerr << std::endl << "Too few arguments" << std::endl; |
| 40 | options.usage(argv); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | if (objpath == ArgumentParser::emptyString) |
| 45 | { |
| 46 | log<level::ERR>("Device monitoring path argument required"); |
| 47 | return -2; |
| 48 | } |
| 49 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 50 | if (instnum == ArgumentParser::emptyString) |
| 51 | { |
| 52 | log<level::ERR>("Device monitoring instance number argument required"); |
| 53 | return -3; |
| 54 | } |
| 55 | |
| 56 | if (invpath == ArgumentParser::emptyString) |
| 57 | { |
| 58 | log<level::ERR>("Device monitoring inventory path argument required"); |
| 59 | return -4; |
| 60 | } |
| 61 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 62 | auto bus = sdbusplus::bus::new_default(); |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 63 | auto event = sdeventplus::Event::get_default(); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 64 | |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 65 | // Attach the event object to the bus object so we can |
| 66 | // handle both sd_events (for the timers) and dbus signals. |
| 67 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 68 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 69 | auto objname = "power_supply" + instnum; |
| 70 | auto instance = std::stoul(instnum); |
Brandon Wyman | 1dd3c9a | 2017-10-10 13:31:18 -0500 | [diff] [blame] | 71 | // The state changes from 0 to 1 when the BMC_POWER_UP line to the power |
| 72 | // sequencer is asserted. It can take 50ms for the sequencer to assert the |
| 73 | // ENABLE# line that goes to the power supplies. The Witherspoon power |
| 74 | // supply can take a max of 100ms from ENABLE# asserted to 12V in spec. |
| 75 | // Once 12V in spec., the power supply will nominally take 1 second to |
| 76 | // assert DC_GOOD (and update POWER_GOOD Negated), +/1 100ms. That would |
| 77 | // give us a 1250ms delay from state=1 to checking STATUS_WORD, however, |
| 78 | // 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] | 79 | // a second, so rounding up from 1 to 5 seconds. |
| 80 | std::chrono::seconds powerOnDelay(5); |
Brandon Wyman | 590fc28 | 2017-11-01 18:22:25 -0500 | [diff] [blame] | 81 | // Timer to delay setting internal presence tracking. Allows for servicing |
| 82 | // the power supply. |
| 83 | std::chrono::seconds presentDelay(2); |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 84 | auto psuDevice = std::make_unique<psu::PowerSupply>( |
| 85 | objname, std::move(instance), std::move(objpath), std::move(invpath), |
| 86 | bus, event, powerOnDelay, presentDelay); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 87 | |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 88 | // Get the number of input power history records to keep in D-Bus. |
| 89 | long int numRecords = 0; |
| 90 | auto records = (options)["num-history-records"]; |
| 91 | if (records != ArgumentParser::emptyString) |
| 92 | { |
| 93 | numRecords = std::stol(records); |
| 94 | if (numRecords < 0) |
| 95 | { |
| 96 | std::cerr << "Invalid number of history records specified.\n"; |
| 97 | return -6; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (numRecords != 0) |
| 102 | { |
| 103 | // Get the GPIO information for controlling the SYNC signal. |
| 104 | // If one is there, they both must be. |
| 105 | auto syncGPIOPath = (options)["sync-gpio-path"]; |
| 106 | auto syncGPIONum = (options)["sync-gpio-num"]; |
| 107 | |
| 108 | if (((syncGPIOPath == ArgumentParser::emptyString) && |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 109 | (syncGPIONum != ArgumentParser::emptyString)) || |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 110 | ((syncGPIOPath != ArgumentParser::emptyString) && |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 111 | (syncGPIONum == ArgumentParser::emptyString))) |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 112 | { |
| 113 | std::cerr << "Invalid sync GPIO number or path\n"; |
| 114 | return -7; |
| 115 | } |
| 116 | |
| 117 | size_t gpioNum = 0; |
| 118 | if (syncGPIONum != ArgumentParser::emptyString) |
| 119 | { |
| 120 | gpioNum = stoul(syncGPIONum); |
| 121 | } |
| 122 | |
| 123 | std::string name{"ps" + instnum + "_input_power"}; |
| 124 | std::string basePath = |
| 125 | std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name; |
| 126 | |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 127 | psuDevice->enableHistory(basePath, numRecords, syncGPIOPath, gpioNum); |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 128 | |
| 129 | // Systemd object manager |
| 130 | sdbusplus::server::manager::manager objManager{bus, basePath.c_str()}; |
| 131 | |
| 132 | std::string busName = |
Matt Spinler | f0f02b9 | 2018-10-25 16:12:43 -0500 | [diff] [blame] | 133 | std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name; |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame] | 134 | bus.request_name(busName.c_str()); |
| 135 | } |
| 136 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 137 | auto pollInterval = std::chrono::milliseconds(1000); |
William A. Kennington III | e5a8b47 | 2018-10-18 00:40:04 -0700 | [diff] [blame] | 138 | return DeviceMonitor(std::move(psuDevice), event, pollInterval).run(); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 139 | } |