blob: b52489ee7482e75bc0ea1919ff3f22ecdaa46762 [file] [log] [blame]
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -05001/**
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 Barth29e9e382020-01-23 13:40:49 -060016#pragma once
17
Shawn McCarneyb464c8b2020-07-16 17:51:38 -050018#include "services.hpp"
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050019#include "system.hpp"
20
Matthew Barth29e9e382020-01-23 13:40:49 -060021#include <interfaces/manager_interface.hpp>
22#include <sdbusplus/bus.hpp>
Matthew Barth250d0a92020-02-28 13:04:24 -060023#include <sdbusplus/bus/match.hpp>
Matthew Barth29e9e382020-01-23 13:40:49 -060024#include <sdbusplus/server/object.hpp>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060025#include <sdeventplus/event.hpp>
Matthew Barth7cbc5532020-01-29 15:13:13 -060026#include <sdeventplus/source/signal.hpp>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060027#include <sdeventplus/utility/timer.hpp>
Matthew Barth29e9e382020-01-23 13:40:49 -060028
Matthew Barthbbc7c582020-02-03 15:55:15 -060029#include <algorithm>
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050030#include <filesystem>
31#include <memory>
32#include <string>
33#include <vector>
Matthew Barthbbc7c582020-02-03 15:55:15 -060034
Matthew Barth29e9e382020-01-23 13:40:49 -060035namespace phosphor::power::regulators
36{
37
38constexpr auto busName = "xyz.openbmc_project.Power.Regulators";
39constexpr auto objPath = "/xyz/openbmc_project/power/regulators/manager";
Matthew Barth23a5e592020-01-30 13:11:08 -060040constexpr auto sysDbusObj = "/xyz/openbmc_project/inventory";
41constexpr auto sysDbusPath = "/xyz/openbmc_project/inventory/system";
42constexpr auto sysDbusIntf = "xyz.openbmc_project.Inventory.Item.System";
43constexpr auto sysDbusProp = "Identifier";
Matthew Barth29e9e382020-01-23 13:40:49 -060044
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060045using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
46
Matthew Barth29e9e382020-01-23 13:40:49 -060047using ManagerObject = sdbusplus::server::object::object<
48 phosphor::power::regulators::interface::ManagerInterface>;
49
50class 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 McCarneyb464c8b2020-07-16 17:51:38 -050064 * @param bus the D-Bus bus
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050065 * @param event the sdevent event
Matthew Barth29e9e382020-01-23 13:40:49 -060066 */
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060067 Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
Matthew Barth29e9e382020-01-23 13:40:49 -060068
69 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050070 * Overridden manager object's configure method
Matthew Barth29e9e382020-01-23 13:40:49 -060071 */
72 void configure() override;
73
74 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050075 * Overridden manager object's monitor method
Matthew Barth29e9e382020-01-23 13:40:49 -060076 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050077 * @param enable Enable or disable regulator monitoring
Matthew Barth29e9e382020-01-23 13:40:49 -060078 */
79 void monitor(bool enable) override;
80
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060081 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050082 * Timer expired callback function
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060083 */
84 void timerExpired();
85
Matthew Barth7cbc5532020-01-29 15:13:13 -060086 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050087 * Callback function to handle receiving a HUP signal
Matthew Barth7cbc5532020-01-29 15:13:13 -060088 * to reload the configuration data.
89 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050090 * @param sigSrc sd_event_source signal wrapper
91 * @param sigInfo signal info on signal fd
Matthew Barth7cbc5532020-01-29 15:13:13 -060092 */
93 void sighupHandler(sdeventplus::source::Signal& sigSrc,
94 const struct signalfd_siginfo* sigInfo);
95
Matthew Barth250d0a92020-02-28 13:04:24 -060096 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050097 * Callback function to handle interfacesAdded dbus signals
Matthew Barth250d0a92020-02-28 13:04:24 -060098 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -050099 * @param msg Expanded sdbusplus message data
Matthew Barth250d0a92020-02-28 13:04:24 -0600100 */
101 void signalHandler(sdbusplus::message::message& msg);
102
Matthew Barth29e9e382020-01-23 13:40:49 -0600103 private:
104 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500105 * Set the JSON configuration data filename
Matthew Barthbbc7c582020-02-03 15:55:15 -0600106 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500107 * @param fName filename without `.json` extension
Matthew Barthbbc7c582020-02-03 15:55:15 -0600108 */
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 McCarneye0c6a2d2020-05-01 11:37:08 -0500121 * Get the JSON configuration data filename from dbus
Matthew Barthbbc7c582020-02-03 15:55:15 -0600122 *
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500123 * @return JSON configuration data filename
Matthew Barthbbc7c582020-02-03 15:55:15 -0600124 */
125 const std::string getFileNameDbus();
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500126
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 McCarneyb464c8b2020-07-16 17:51:38 -0500158 * The D-Bus bus
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500159 */
160 sdbusplus::bus::bus& bus;
161
162 /**
163 * Event to loop on
164 */
165 sdeventplus::Event eventLoop;
166
167 /**
Shawn McCarneyb464c8b2020-07-16 17:51:38 -0500168 * System services like error logging and the journal.
169 */
170 BMCServices services;
171
172 /**
Shawn McCarneye0c6a2d2020-05-01 11:37:08 -0500173 * 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 Barth29e9e382020-01-23 13:40:49 -0600194};
195
196} // namespace phosphor::power::regulators