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" |
| 20 | #include "event.hpp" |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 21 | #include "power_supply.hpp" |
| 22 | #include "device_monitor.hpp" |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 23 | |
| 24 | using namespace witherspoon::power; |
| 25 | using namespace phosphor::logging; |
| 26 | |
| 27 | int main(int argc, char* argv[]) |
| 28 | { |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 29 | auto options = ArgumentParser(argc, argv); |
| 30 | |
| 31 | auto objpath = (options)["path"]; |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 32 | auto instnum = (options)["instance"]; |
| 33 | auto invpath = (options)["inventory"]; |
| 34 | if (argc < 4) |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 35 | { |
| 36 | std::cerr << std::endl << "Too few arguments" << std::endl; |
| 37 | options.usage(argv); |
| 38 | return -1; |
| 39 | } |
| 40 | |
| 41 | if (objpath == ArgumentParser::emptyString) |
| 42 | { |
| 43 | log<level::ERR>("Device monitoring path argument required"); |
| 44 | return -2; |
| 45 | } |
| 46 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 47 | if (instnum == ArgumentParser::emptyString) |
| 48 | { |
| 49 | log<level::ERR>("Device monitoring instance number argument required"); |
| 50 | return -3; |
| 51 | } |
| 52 | |
| 53 | if (invpath == ArgumentParser::emptyString) |
| 54 | { |
| 55 | log<level::ERR>("Device monitoring inventory path argument required"); |
| 56 | return -4; |
| 57 | } |
| 58 | |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 59 | sd_event* events = nullptr; |
| 60 | |
| 61 | auto r = sd_event_default(&events); |
| 62 | if (r < 0) |
| 63 | { |
| 64 | log<level::ERR>("Failed call to sd_event_default()", |
| 65 | entry("ERROR=%s", strerror(-r))); |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 66 | return -5; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 69 | auto bus = sdbusplus::bus::new_default(); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 70 | witherspoon::power::event::Event eventPtr{events}; |
| 71 | |
| 72 | //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(eventPtr.get(), SD_EVENT_PRIORITY_NORMAL); |
| 75 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 76 | auto objname = "power_supply" + instnum; |
| 77 | auto instance = std::stoul(instnum); |
Brandon Wyman | 1dd3c9a | 2017-10-10 13:31:18 -0500 | [diff] [blame] | 78 | // 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 Wyman | b08efa4 | 2017-11-16 09:41:51 -0600 | [diff] [blame] | 86 | // a second, so rounding up from 1 to 5 seconds. |
| 87 | std::chrono::seconds powerOnDelay(5); |
Brandon Wyman | 590fc28 | 2017-11-01 18:22:25 -0500 | [diff] [blame] | 88 | // Timer to delay setting internal presence tracking. Allows for servicing |
| 89 | // the power supply. |
| 90 | std::chrono::seconds presentDelay(2); |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 91 | auto psuDevice = std::make_unique<psu::PowerSupply>(objname, |
| 92 | std::move(instance), |
| 93 | std::move(objpath), |
| 94 | std::move(invpath), |
| 95 | bus, |
| 96 | eventPtr, |
Brandon Wyman | 590fc28 | 2017-11-01 18:22:25 -0500 | [diff] [blame] | 97 | powerOnDelay, |
| 98 | presentDelay); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 99 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 100 | auto pollInterval = std::chrono::milliseconds(1000); |
| 101 | DeviceMonitor mainloop(std::move(psuDevice), eventPtr, pollInterval); |
| 102 | mainloop.run(); |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 103 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 104 | return 0; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 105 | } |