blob: 48f22b7747041addb049b145354c5dead81f9838 [file] [log] [blame]
Ed Tanous2474adf2018-09-05 16:31:16 -07001/*
2// Copyright (c) 2018 Intel Corporation
3// Copyright (c) 2018 Ampere Computing LLC
4/
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16*/
17#pragma once
18
Ed Tanous2474adf2018-09-05 16:31:16 -070019#include "sensors.hpp"
20
John Edward Broadbent7e860f12021-04-08 15:57:16 -070021#include <app.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080022#include <dbus_utility.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070023#include <registries/privilege_registry.hpp>
John Edward Broadbent7e860f12021-04-08 15:57:16 -070024
Ed Tanous2474adf2018-09-05 16:31:16 -070025namespace redfish
26{
Ed Tanous4f48d5f2021-06-21 08:27:45 -070027inline void setPowerCapOverride(
John Edward Broadbent7e860f12021-04-08 15:57:16 -070028 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
29 std::vector<nlohmann::json>& powerControlCollections)
Ed Tanous2474adf2018-09-05 16:31:16 -070030{
George Liu0fda0f12021-11-16 10:06:17 +080031 auto getChassisPath = [sensorsAsyncResp, powerControlCollections](
32 const std::optional<std::string>&
33 chassisPath) mutable {
34 if (!chassisPath)
35 {
36 BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
37 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
38 "Chassis", sensorsAsyncResp->chassisId);
39 return;
40 }
41
42 if (powerControlCollections.size() != 1)
43 {
44 BMCWEB_LOG_ERROR << "Don't support multiple hosts at present ";
45 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
46 "Power", "PowerControl");
47 return;
48 }
49
50 auto& item = powerControlCollections[0];
51
52 std::optional<nlohmann::json> powerLimit;
53 if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res,
54 "PowerLimit", powerLimit))
55 {
56 return;
57 }
58 if (!powerLimit)
59 {
60 return;
61 }
62 std::optional<uint32_t> value;
63 if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res,
64 "LimitInWatts", value))
65 {
66 return;
67 }
68 if (!value)
69 {
70 return;
71 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -070072 sdbusplus::asio::getProperty<bool>(
73 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
George Liu0fda0f12021-11-16 10:06:17 +080074 "/xyz/openbmc_project/control/host0/power_cap",
Jonathan Doman1e1e5982021-06-11 09:36:17 -070075 "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable",
76 [value, sensorsAsyncResp](const boost::system::error_code ec,
77 bool powerCapEnable) {
78 if (ec)
79 {
80 messages::internalError(sensorsAsyncResp->asyncResp->res);
81 BMCWEB_LOG_ERROR
82 << "powerCapEnable Get handler: Dbus error " << ec;
83 return;
84 }
85 if (!powerCapEnable)
86 {
87 messages::actionNotSupported(
88 sensorsAsyncResp->asyncResp->res,
89 "Setting LimitInWatts when PowerLimit feature is disabled");
90 BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
91 return;
92 }
93
94 crow::connections::systemBus->async_method_call(
95 [sensorsAsyncResp](const boost::system::error_code ec2) {
96 if (ec2)
97 {
98 BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: "
99 << ec2;
100 messages::internalError(
101 sensorsAsyncResp->asyncResp->res);
102 return;
103 }
104 sensorsAsyncResp->asyncResp->res.result(
105 boost::beast::http::status::no_content);
106 },
107 "xyz.openbmc_project.Settings",
108 "/xyz/openbmc_project/control/host0/power_cap",
109 "org.freedesktop.DBus.Properties", "Set",
110 "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
111 std::variant<uint32_t>(*value));
112 });
George Liu0fda0f12021-11-16 10:06:17 +0800113 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700114 getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
115}
116inline void requestRoutesPower(App& app)
117{
Ed Tanous2474adf2018-09-05 16:31:16 -0700118
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700119 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700120 .privileges(redfish::privileges::getPower)
Ed Tanous168e20c2021-12-13 14:39:53 -0800121 .methods(
122 boost::beast::http::verb::get)([](const crow::Request&,
123 const std::shared_ptr<
124 bmcweb::AsyncResp>& asyncResp,
125 const std::string& chassisName) {
126 asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800127
Ed Tanous168e20c2021-12-13 14:39:53 -0800128 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
129 asyncResp, chassisName,
130 sensors::dbus::paths.at(sensors::node::power),
131 sensors::node::power);
Eddie James028f7eb2019-05-17 21:24:36 +0000132
Ed Tanous168e20c2021-12-13 14:39:53 -0800133 getChassisData(sensorAsyncResp);
Eddie James028f7eb2019-05-17 21:24:36 +0000134
Ed Tanous168e20c2021-12-13 14:39:53 -0800135 // This callback verifies that the power limit is only provided
136 // for the chassis that implements the Chassis inventory item.
137 // This prevents things like power supplies providing the
138 // chassis power limit
139 auto chassisHandler = [sensorAsyncResp](
140 const boost::system::error_code e,
141 const std::vector<std::string>&
142 chassisPaths) {
143 if (e)
144 {
145 BMCWEB_LOG_ERROR
146 << "Power Limit GetSubTreePaths handler Dbus error "
147 << e;
148 return;
149 }
150
151 bool found = false;
152 for (const std::string& chassis : chassisPaths)
153 {
154 size_t len = std::string::npos;
155 size_t lastPos = chassis.rfind('/');
156 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000157 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800158 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000159 }
160
Ed Tanous168e20c2021-12-13 14:39:53 -0800161 if (lastPos == chassis.size() - 1)
Eddie James028f7eb2019-05-17 21:24:36 +0000162 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800163 size_t end = lastPos;
164 lastPos = chassis.rfind('/', lastPos - 1);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000166 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700167 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000168 }
Eddie James028f7eb2019-05-17 21:24:36 +0000169
Ed Tanous168e20c2021-12-13 14:39:53 -0800170 len = end - (lastPos + 1);
Eddie James028f7eb2019-05-17 21:24:36 +0000171 }
172
Ed Tanous168e20c2021-12-13 14:39:53 -0800173 std::string interfaceChassisName =
174 chassis.substr(lastPos + 1, len);
Ed Tanouse662eae2022-01-25 10:39:19 -0800175 if (interfaceChassisName.compare(
176 sensorAsyncResp->chassisId) == 0)
Eddie James028f7eb2019-05-17 21:24:36 +0000177 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800178 found = true;
179 break;
Eddie James028f7eb2019-05-17 21:24:36 +0000180 }
Ed Tanous168e20c2021-12-13 14:39:53 -0800181 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700182
Ed Tanous168e20c2021-12-13 14:39:53 -0800183 if (!found)
184 {
185 BMCWEB_LOG_DEBUG << "Power Limit not present for "
186 << sensorAsyncResp->chassisId;
187 return;
188 }
189
190 auto valueHandler =
191 [sensorAsyncResp](
192 const boost::system::error_code ec,
193 const std::vector<std::pair<
194 std::string, dbus::utility::DbusVariantType>>&
195 properties) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700196 if (ec)
197 {
198 messages::internalError(
199 sensorAsyncResp->asyncResp->res);
200 BMCWEB_LOG_ERROR
201 << "Power Limit GetAll handler: Dbus error "
202 << ec;
203 return;
204 }
205
206 nlohmann::json& tempArray =
207 sensorAsyncResp->asyncResp->res
208 .jsonValue["PowerControl"];
209
210 // Put multiple "sensors" into a single PowerControl, 0,
211 // so only create the first one
212 if (tempArray.empty())
213 {
214 // Mandatory properties odata.id and MemberId
215 // A warning without a odata.type
216 tempArray.push_back(
217 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
218 {"@odata.id", "/redfish/v1/Chassis/" +
219 sensorAsyncResp->chassisId +
220 "/Power#/PowerControl/0"},
221 {"Name", "Chassis Power Control"},
222 {"MemberId", "0"}});
223 }
224
225 nlohmann::json& sensorJson = tempArray.back();
226 bool enabled = false;
227 double powerCap = 0.0;
228 int64_t scale = 0;
229
Ed Tanous168e20c2021-12-13 14:39:53 -0800230 for (const std::pair<std::string,
231 dbus::utility::DbusVariantType>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700232 property : properties)
233 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800234 if (property.first.compare("Scale") == 0)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700235 {
236 const int64_t* i =
237 std::get_if<int64_t>(&property.second);
238
Ed Tanouse662eae2022-01-25 10:39:19 -0800239 if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700240 {
241 scale = *i;
242 }
243 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800244 else if (property.first.compare("PowerCap") == 0)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700245 {
246 const double* d =
247 std::get_if<double>(&property.second);
248 const int64_t* i =
249 std::get_if<int64_t>(&property.second);
250 const uint32_t* u =
251 std::get_if<uint32_t>(&property.second);
252
Ed Tanouse662eae2022-01-25 10:39:19 -0800253 if (d != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700254 {
255 powerCap = *d;
256 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800257 else if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700258 {
259 powerCap = static_cast<double>(*i);
260 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800261 else if (u != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700262 {
263 powerCap = *u;
264 }
265 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800266 else if (property.first.compare("PowerCapEnable") ==
267 0)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700268 {
269 const bool* b =
270 std::get_if<bool>(&property.second);
271
Ed Tanouse662eae2022-01-25 10:39:19 -0800272 if (b != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700273 {
274 enabled = *b;
275 }
276 }
277 }
278
279 nlohmann::json& value =
280 sensorJson["PowerLimit"]["LimitInWatts"];
281
282 // LimitException is Mandatory attribute as per OCP
283 // Baseline Profile – v1.0.0, so currently making it
284 // "NoAction" as default value to make it OCP Compliant.
285 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
286
287 if (enabled)
288 {
289 // Redfish specification indicates PowerLimit should
290 // be null if the limit is not enabled.
291 value = powerCap * std::pow(10, scale);
292 }
293 };
294
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700295 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -0800296 std::move(valueHandler), "xyz.openbmc_project.Settings",
297 "/xyz/openbmc_project/control/host0/power_cap",
298 "org.freedesktop.DBus.Properties", "GetAll",
299 "xyz.openbmc_project.Control.Power.Cap");
300 };
301
302 crow::connections::systemBus->async_method_call(
303 std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
304 "/xyz/openbmc_project/object_mapper",
305 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
306 "/xyz/openbmc_project/inventory", 0,
307 std::array<const char*, 2>{
308 "xyz.openbmc_project.Inventory.Item.Board",
309 "xyz.openbmc_project.Inventory.Item.Chassis"});
310 });
Eddie James028f7eb2019-05-17 21:24:36 +0000311
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700312 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700313 .privileges(redfish::privileges::patchPower)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700314 .methods(boost::beast::http::verb::patch)(
315 [](const crow::Request& req,
316 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
317 const std::string& chassisName) {
318 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
319 asyncResp, chassisName,
320 sensors::dbus::paths.at(sensors::node::power),
321 sensors::node::power);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800322
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700323 std::optional<std::vector<nlohmann::json>> voltageCollections;
324 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
zhanghch058d1b46d2021-04-01 11:18:24 +0800325
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700326 if (!json_util::readJson(req, sensorAsyncResp->asyncResp->res,
327 "PowerControl", powerCtlCollections,
328 "Voltages", voltageCollections))
329 {
330 return;
331 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800332
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700333 if (powerCtlCollections)
334 {
335 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
336 }
337 if (voltageCollections)
338 {
339 std::unordered_map<std::string, std::vector<nlohmann::json>>
340 allCollections;
341 allCollections.emplace("Voltages",
342 *std::move(voltageCollections));
Bruce Lee80ac4022021-06-04 15:41:39 +0800343 setSensorsOverride(sensorAsyncResp, allCollections);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700344 }
345 });
346}
Ed Tanous2474adf2018-09-05 16:31:16 -0700347
348} // namespace redfish