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 "config.h" 21 #include "event.hpp" 22 #include "power_supply.hpp" 23 #include "device_monitor.hpp" 24 25 using namespace witherspoon::power; 26 using namespace phosphor::logging; 27 28 int main(int argc, char* argv[]) 29 { 30 auto options = ArgumentParser(argc, argv); 31 32 auto objpath = (options)["path"]; 33 auto instnum = (options)["instance"]; 34 auto invpath = (options)["inventory"]; 35 if (argc < 4) 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 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 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))); 67 return -5; 68 } 69 70 auto bus = sdbusplus::bus::new_default(); 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 77 auto objname = "power_supply" + instnum; 78 auto instance = std::stoul(instnum); 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 87 // a second, so rounding up from 1 to 5 seconds. 88 std::chrono::seconds powerOnDelay(5); 89 // Timer to delay setting internal presence tracking. Allows for servicing 90 // the power supply. 91 std::chrono::seconds presentDelay(2); 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, 98 powerOnDelay, 99 presentDelay); 100 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 153 auto pollInterval = std::chrono::milliseconds(1000); 154 DeviceMonitor mainloop(std::move(psuDevice), eventPtr, pollInterval); 155 mainloop.run(); 156 157 return 0; 158 } 159