Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [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 | */ |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 16 | #pragma once |
| 17 | |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 18 | #include "services.hpp" |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 19 | #include "system.hpp" |
| 20 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 21 | #include <interfaces/manager_interface.hpp> |
| 22 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 23 | #include <sdbusplus/bus/match.hpp> |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 24 | #include <sdbusplus/server/object.hpp> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 25 | #include <sdeventplus/event.hpp> |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 26 | #include <sdeventplus/source/signal.hpp> |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 27 | #include <sdeventplus/utility/timer.hpp> |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 28 | |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 29 | #include <algorithm> |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 30 | #include <filesystem> |
| 31 | #include <memory> |
| 32 | #include <string> |
| 33 | #include <vector> |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 34 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 35 | namespace phosphor::power::regulators |
| 36 | { |
| 37 | |
| 38 | constexpr auto busName = "xyz.openbmc_project.Power.Regulators"; |
| 39 | constexpr auto objPath = "/xyz/openbmc_project/power/regulators/manager"; |
Matthew Barth | 23a5e59 | 2020-01-30 13:11:08 -0600 | [diff] [blame] | 40 | constexpr auto sysDbusObj = "/xyz/openbmc_project/inventory"; |
| 41 | constexpr auto sysDbusPath = "/xyz/openbmc_project/inventory/system"; |
| 42 | constexpr auto sysDbusIntf = "xyz.openbmc_project.Inventory.Item.System"; |
| 43 | constexpr auto sysDbusProp = "Identifier"; |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 44 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 45 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 46 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 47 | using ManagerObject = sdbusplus::server::object::object< |
| 48 | phosphor::power::regulators::interface::ManagerInterface>; |
| 49 | |
| 50 | class Manager : public ManagerObject |
| 51 | { |
| 52 | public: |
| 53 | Manager() = delete; |
| 54 | Manager(const Manager&) = delete; |
| 55 | Manager(Manager&&) = delete; |
| 56 | Manager& operator=(const Manager&) = delete; |
| 57 | Manager& operator=(Manager&&) = delete; |
| 58 | ~Manager() = default; |
| 59 | |
| 60 | /** |
| 61 | * Constructor |
| 62 | * Creates a manager over the regulators. |
| 63 | * |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 64 | * @param bus the D-Bus bus |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 65 | * @param event the sdevent event |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 66 | */ |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 67 | Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event); |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 68 | |
| 69 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 70 | * Overridden manager object's configure method |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 71 | */ |
| 72 | void configure() override; |
| 73 | |
| 74 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 75 | * Overridden manager object's monitor method |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 76 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 77 | * @param enable Enable or disable regulator monitoring |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 78 | */ |
| 79 | void monitor(bool enable) override; |
| 80 | |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 81 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 82 | * Timer expired callback function |
Matthew Barth | f2bcf1f | 2020-01-29 14:42:47 -0600 | [diff] [blame] | 83 | */ |
| 84 | void timerExpired(); |
| 85 | |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 86 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 87 | * Callback function to handle receiving a HUP signal |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 88 | * to reload the configuration data. |
| 89 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 90 | * @param sigSrc sd_event_source signal wrapper |
| 91 | * @param sigInfo signal info on signal fd |
Matthew Barth | 7cbc553 | 2020-01-29 15:13:13 -0600 | [diff] [blame] | 92 | */ |
| 93 | void sighupHandler(sdeventplus::source::Signal& sigSrc, |
| 94 | const struct signalfd_siginfo* sigInfo); |
| 95 | |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 96 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 97 | * Callback function to handle interfacesAdded dbus signals |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 98 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 99 | * @param msg Expanded sdbusplus message data |
Matthew Barth | 250d0a9 | 2020-02-28 13:04:24 -0600 | [diff] [blame] | 100 | */ |
| 101 | void signalHandler(sdbusplus::message::message& msg); |
| 102 | |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 103 | private: |
| 104 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 105 | * Set the JSON configuration data filename |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 106 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 107 | * @param fName filename without `.json` extension |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 108 | */ |
| 109 | inline void setFileName(const std::string& fName) |
| 110 | { |
| 111 | fileName = fName; |
| 112 | if (!fileName.empty()) |
| 113 | { |
| 114 | // Replace all spaces with underscores |
| 115 | std::replace(fileName.begin(), fileName.end(), ' ', '_'); |
| 116 | fileName.append(".json"); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 121 | * Get the JSON configuration data filename from dbus |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 122 | * |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 123 | * @return JSON configuration data filename |
Matthew Barth | bbc7c58 | 2020-02-03 15:55:15 -0600 | [diff] [blame] | 124 | */ |
| 125 | const std::string getFileNameDbus(); |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Finds the JSON configuration file. |
| 129 | * |
| 130 | * Looks for the config file in the test directory and standard directory. |
| 131 | * |
| 132 | * Throws an exception if the file cannot be found or a file system error |
| 133 | * occurs. |
| 134 | * |
| 135 | * The base name of the config file must have already been obtained and |
| 136 | * stored in the fileName data member. |
| 137 | * |
| 138 | * @return absolute path to config file |
| 139 | */ |
| 140 | std::filesystem::path findConfigFile(); |
| 141 | |
| 142 | /** |
| 143 | * Loads the JSON configuration file. |
| 144 | * |
| 145 | * Looks for the config file in the test directory and standard directory. |
| 146 | * |
| 147 | * If the config file is found, it is parsed and the resulting information |
| 148 | * is stored in the system data member. |
| 149 | * |
| 150 | * If the config file cannot be found or parsing fails, an error is logged. |
| 151 | * |
| 152 | * The base name of the config file must have already been obtained and |
| 153 | * stored in the fileName data member. |
| 154 | */ |
| 155 | void loadConfigFile(); |
| 156 | |
| 157 | /** |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 158 | * The D-Bus bus |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 159 | */ |
| 160 | sdbusplus::bus::bus& bus; |
| 161 | |
| 162 | /** |
| 163 | * Event to loop on |
| 164 | */ |
| 165 | sdeventplus::Event eventLoop; |
| 166 | |
| 167 | /** |
Shawn McCarney | b464c8b | 2020-07-16 17:51:38 -0500 | [diff] [blame] | 168 | * System services like error logging and the journal. |
| 169 | */ |
| 170 | BMCServices services; |
| 171 | |
| 172 | /** |
Shawn McCarney | e0c6a2d | 2020-05-01 11:37:08 -0500 | [diff] [blame] | 173 | * List of event timers |
| 174 | */ |
| 175 | std::vector<Timer> timers{}; |
| 176 | |
| 177 | /** |
| 178 | * List of dbus signal matches |
| 179 | */ |
| 180 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> signals{}; |
| 181 | |
| 182 | /** |
| 183 | * JSON configuration file base name. |
| 184 | */ |
| 185 | std::string fileName{}; |
| 186 | |
| 187 | /** |
| 188 | * Computer system being controlled and monitored by the BMC. |
| 189 | * |
| 190 | * Contains the information loaded from the JSON configuration file. |
| 191 | * Contains nullptr if the configuration file has not been loaded. |
| 192 | */ |
| 193 | std::unique_ptr<System> system{}; |
Matthew Barth | 29e9e38 | 2020-01-23 13:40:49 -0600 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | } // namespace phosphor::power::regulators |