Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 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 | |
| 17 | #include "manager.hpp" |
| 18 | |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 19 | #include "utility.hpp" |
| 20 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 21 | #include <sdbusplus/bus.hpp> |
| 22 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 23 | #include <chrono> |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 24 | #include <variant> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 25 | |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame^] | 26 | namespace phosphor::power::regulators |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 27 | { |
| 28 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 29 | Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) : |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 30 | ManagerObject(bus, objPath, true), bus(bus), eventLoop(event), fileName("") |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 31 | { |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame^] | 32 | /* Temporarily comment out until D-Bus interface is defined and available. |
| 33 | // Subscribe to interfacesAdded signal for filename property |
| 34 | std::unique_ptr<sdbusplus::server::match::match> matchPtr = |
| 35 | std::make_unique<sdbusplus::server::match::match>( |
| 36 | bus, |
| 37 | sdbusplus::bus::match::rules::interfacesAdded(sysDbusObj).c_str(), |
| 38 | std::bind(std::mem_fn(&Manager::signalHandler), this, |
| 39 | std::placeholders::_1)); |
| 40 | signals.emplace_back(std::move(matchPtr)); |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 41 | |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame^] | 42 | // Attempt to get the filename property from dbus |
| 43 | setFileName(getFileNameDbus()); |
| 44 | */ |
| 45 | |
| 46 | // Temporarily hard-code JSON config file name to first system that will use |
| 47 | // this application. Remove this when D-Bus interface is available. |
| 48 | fileName = "ibm_rainier.json"; |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 49 | |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 50 | if (!fileName.empty()) |
| 51 | { |
| 52 | // TODO Load & parse JSON configuration data file |
| 53 | } |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 54 | |
| 55 | // Obtain dbus service name |
| 56 | bus.request_name(busName); |
| 57 | } |
| 58 | |
| 59 | void Manager::configure() |
| 60 | { |
| 61 | // TODO Configuration errors that should halt poweron, |
| 62 | // throw InternalFailure exception (or similar) to |
| 63 | // fail the call(busctl) to this method |
| 64 | } |
| 65 | |
| 66 | void Manager::monitor(bool enable) |
| 67 | { |
| 68 | if (enable) |
| 69 | { |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 70 | Timer timer(eventLoop, std::bind(&Manager::timerExpired, this)); |
| 71 | // Set timer as a repeating 1sec timer |
| 72 | timer.restart(std::chrono::milliseconds(1000)); |
| 73 | timers.emplace_back(std::move(timer)); |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 74 | } |
| 75 | else |
| 76 | { |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 77 | // Delete all timers to disable monitoring |
| 78 | timers.clear(); |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 82 | void Manager::timerExpired() |
| 83 | { |
| 84 | // TODO Analyze, refresh sensor status, and |
| 85 | // collect/update telemetry for each regulator |
| 86 | } |
| 87 | |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 88 | void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/, |
| 89 | const struct signalfd_siginfo* /*sigInfo*/) |
| 90 | { |
| 91 | // TODO Reload and process the configuration data |
| 92 | } |
| 93 | |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 94 | void Manager::signalHandler(sdbusplus::message::message& msg) |
| 95 | { |
| 96 | if (msg) |
| 97 | { |
| 98 | sdbusplus::message::object_path op; |
| 99 | msg.read(op); |
| 100 | if (static_cast<const std::string&>(op) != sysDbusPath) |
| 101 | { |
| 102 | // Object path does not match the path |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | // An interfacesAdded signal returns a dictionary of interface |
| 107 | // names to a dictionary of properties and their values |
| 108 | // https://dbus.freedesktop.org/doc/dbus-specification.html |
| 109 | std::map<std::string, std::map<std::string, std::variant<std::string>>> |
| 110 | intfProp; |
| 111 | msg.read(intfProp); |
| 112 | auto itIntf = intfProp.find(sysDbusIntf); |
| 113 | if (itIntf == intfProp.cend()) |
| 114 | { |
| 115 | // Interface not found on the path |
| 116 | return; |
| 117 | } |
| 118 | auto itProp = itIntf->second.find(sysDbusProp); |
| 119 | if (itProp == itIntf->second.cend()) |
| 120 | { |
| 121 | // Property not found on the interface |
| 122 | return; |
| 123 | } |
| 124 | // Set fileName and call parse json function |
| 125 | setFileName(std::get<std::string>(itProp->second)); |
| 126 | // TODO Load & parse JSON configuration data file |
| 127 | } |
| 128 | } |
| 129 | |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 130 | const std::string Manager::getFileNameDbus() |
| 131 | { |
| 132 | std::string fileName = ""; |
| 133 | using namespace phosphor::power::util; |
| 134 | |
| 135 | try |
| 136 | { |
| 137 | // Do not log an error when service or property are not found |
| 138 | auto service = getService(sysDbusPath, sysDbusIntf, bus, false); |
| 139 | if (!service.empty()) |
| 140 | { |
| 141 | getProperty(sysDbusIntf, sysDbusProp, sysDbusPath, service, bus, |
| 142 | fileName); |
| 143 | } |
| 144 | } |
| 145 | catch (const sdbusplus::exception::SdBusError&) |
| 146 | { |
| 147 | // File name property not available on dbus |
| 148 | fileName = ""; |
| 149 | } |
| 150 | |
| 151 | return fileName; |
| 152 | } |
| 153 | |
Shawn McCarney | 84807b9 | 2020-04-30 18:40:03 -0500 | [diff] [blame^] | 154 | } // namespace phosphor::power::regulators |