blob: 9e2f031f3a3d5bb33177f2871bd458a01431c887 [file] [log] [blame]
Matthew Barth29e9e382020-01-23 13:40:49 -06001/**
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 Barthbbc7c582020-02-03 15:55:15 -060019#include "utility.hpp"
20
Matthew Barth29e9e382020-01-23 13:40:49 -060021#include <sdbusplus/bus.hpp>
22
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060023#include <chrono>
Matthew Barth250d0a92020-02-28 13:04:24 -060024#include <variant>
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060025
Shawn McCarney84807b92020-04-30 18:40:03 -050026namespace phosphor::power::regulators
Matthew Barth29e9e382020-01-23 13:40:49 -060027{
28
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060029Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Matthew Barthbbc7c582020-02-03 15:55:15 -060030 ManagerObject(bus, objPath, true), bus(bus), eventLoop(event), fileName("")
Matthew Barth29e9e382020-01-23 13:40:49 -060031{
Shawn McCarney84807b92020-04-30 18:40:03 -050032 /* 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 Barth250d0a92020-02-28 13:04:24 -060041
Shawn McCarney84807b92020-04-30 18:40:03 -050042 // 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 Barthbbc7c582020-02-03 15:55:15 -060049
Matthew Barthbbc7c582020-02-03 15:55:15 -060050 if (!fileName.empty())
51 {
52 // TODO Load & parse JSON configuration data file
53 }
Matthew Barth29e9e382020-01-23 13:40:49 -060054
55 // Obtain dbus service name
56 bus.request_name(busName);
57}
58
59void 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
66void Manager::monitor(bool enable)
67{
68 if (enable)
69 {
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060070 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 Barth29e9e382020-01-23 13:40:49 -060074 }
75 else
76 {
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060077 // Delete all timers to disable monitoring
78 timers.clear();
Matthew Barth29e9e382020-01-23 13:40:49 -060079 }
80}
81
Matthew Barthf2bcf1f2020-01-29 14:42:47 -060082void Manager::timerExpired()
83{
84 // TODO Analyze, refresh sensor status, and
85 // collect/update telemetry for each regulator
86}
87
Matthew Barth7cbc5532020-01-29 15:13:13 -060088void Manager::sighupHandler(sdeventplus::source::Signal& /*sigSrc*/,
89 const struct signalfd_siginfo* /*sigInfo*/)
90{
91 // TODO Reload and process the configuration data
92}
93
Matthew Barth250d0a92020-02-28 13:04:24 -060094void 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 Barthbbc7c582020-02-03 15:55:15 -0600130const 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 McCarney84807b92020-04-30 18:40:03 -0500154} // namespace phosphor::power::regulators