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 | */ |
| 16 | #include <iostream> |
| 17 | #include <phosphor-logging/log.hpp> |
| 18 | #include <systemd/sd-daemon.h> |
| 19 | #include "argument.hpp" |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame^] | 20 | #include "config.h" |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 21 | #include "event.hpp" |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 22 | #include "power_supply.hpp" |
| 23 | #include "device_monitor.hpp" |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 24 | |
| 25 | using namespace witherspoon::power; |
| 26 | using namespace phosphor::logging; |
| 27 | |
| 28 | int main(int argc, char* argv[]) |
| 29 | { |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 30 | auto options = ArgumentParser(argc, argv); |
| 31 | |
| 32 | auto objpath = (options)["path"]; |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 33 | auto instnum = (options)["instance"]; |
| 34 | auto invpath = (options)["inventory"]; |
| 35 | if (argc < 4) |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 36 | { |
| 37 | std::cerr << std::endl << "Too few arguments" << std::endl; |
| 38 | options.usage(argv); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | if (objpath == ArgumentParser::emptyString) |
| 43 | { |
| 44 | log<level::ERR>("Device monitoring path argument required"); |
| 45 | return -2; |
| 46 | } |
| 47 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 48 | if (instnum == ArgumentParser::emptyString) |
| 49 | { |
| 50 | log<level::ERR>("Device monitoring instance number argument required"); |
| 51 | return -3; |
| 52 | } |
| 53 | |
| 54 | if (invpath == ArgumentParser::emptyString) |
| 55 | { |
| 56 | log<level::ERR>("Device monitoring inventory path argument required"); |
| 57 | return -4; |
| 58 | } |
| 59 | |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 60 | sd_event* events = nullptr; |
| 61 | |
| 62 | auto r = sd_event_default(&events); |
| 63 | if (r < 0) |
| 64 | { |
| 65 | log<level::ERR>("Failed call to sd_event_default()", |
| 66 | entry("ERROR=%s", strerror(-r))); |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 67 | return -5; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 70 | auto bus = sdbusplus::bus::new_default(); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 71 | witherspoon::power::event::Event eventPtr{events}; |
| 72 | |
| 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(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 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); |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 92 | auto psuDevice = std::make_unique<psu::PowerSupply>(objname, |
| 93 | std::move(instance), |
| 94 | std::move(objpath), |
| 95 | std::move(invpath), |
| 96 | bus, |
| 97 | eventPtr, |
Brandon Wyman | 590fc28 | 2017-11-01 18:22:25 -0500 | [diff] [blame] | 98 | powerOnDelay, |
| 99 | presentDelay); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 100 | |
Matt Spinler | c87eb82 | 2018-01-18 14:44:47 -0600 | [diff] [blame^] | 101 | // Get the number of input power history records to keep in D-Bus. |
| 102 | long int numRecords = 0; |
| 103 | auto records = (options)["num-history-records"]; |
| 104 | if (records != ArgumentParser::emptyString) |
| 105 | { |
| 106 | numRecords = std::stol(records); |
| 107 | if (numRecords < 0) |
| 108 | { |
| 109 | std::cerr << "Invalid number of history records specified.\n"; |
| 110 | return -6; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | if (numRecords != 0) |
| 115 | { |
| 116 | // Get the GPIO information for controlling the SYNC signal. |
| 117 | // If one is there, they both must be. |
| 118 | auto syncGPIOPath = (options)["sync-gpio-path"]; |
| 119 | auto syncGPIONum = (options)["sync-gpio-num"]; |
| 120 | |
| 121 | if (((syncGPIOPath == ArgumentParser::emptyString) && |
| 122 | (syncGPIONum != ArgumentParser::emptyString)) || |
| 123 | ((syncGPIOPath != ArgumentParser::emptyString) && |
| 124 | (syncGPIONum == ArgumentParser::emptyString))) |
| 125 | { |
| 126 | std::cerr << "Invalid sync GPIO number or path\n"; |
| 127 | return -7; |
| 128 | } |
| 129 | |
| 130 | size_t gpioNum = 0; |
| 131 | if (syncGPIONum != ArgumentParser::emptyString) |
| 132 | { |
| 133 | gpioNum = stoul(syncGPIONum); |
| 134 | } |
| 135 | |
| 136 | std::string name{"ps" + instnum + "_input_power"}; |
| 137 | std::string basePath = |
| 138 | std::string{INPUT_HISTORY_SENSOR_ROOT} + '/' + name; |
| 139 | |
| 140 | psuDevice->enableHistory(basePath, |
| 141 | numRecords, |
| 142 | syncGPIOPath, |
| 143 | gpioNum); |
| 144 | |
| 145 | // Systemd object manager |
| 146 | sdbusplus::server::manager::manager objManager{bus, basePath.c_str()}; |
| 147 | |
| 148 | std::string busName = |
| 149 | std::string{INPUT_HISTORY_BUSNAME_ROOT} + '.' + name; |
| 150 | bus.request_name(busName.c_str()); |
| 151 | } |
| 152 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 153 | auto pollInterval = std::chrono::milliseconds(1000); |
| 154 | DeviceMonitor mainloop(std::move(psuDevice), eventPtr, pollInterval); |
| 155 | mainloop.run(); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 156 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 157 | return 0; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 158 | } |