blob: dd67aea06f51980496ca2f602933e264f27a4003 [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
Ed Tanousb9d36b42022-02-26 21:42:46 -0800139
140 using Mapper = dbus::utility::MapperGetSubTreePathsResponse;
Ed Tanous168e20c2021-12-13 14:39:53 -0800141 auto chassisHandler = [sensorAsyncResp](
142 const boost::system::error_code e,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800143 const Mapper& chassisPaths) {
Ed Tanous168e20c2021-12-13 14:39:53 -0800144 if (e)
145 {
146 BMCWEB_LOG_ERROR
147 << "Power Limit GetSubTreePaths handler Dbus error "
148 << e;
149 return;
150 }
151
152 bool found = false;
153 for (const std::string& chassis : chassisPaths)
154 {
155 size_t len = std::string::npos;
156 size_t lastPos = chassis.rfind('/');
157 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000158 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800159 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000160 }
161
Ed Tanous168e20c2021-12-13 14:39:53 -0800162 if (lastPos == chassis.size() - 1)
Eddie James028f7eb2019-05-17 21:24:36 +0000163 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800164 size_t end = lastPos;
165 lastPos = chassis.rfind('/', lastPos - 1);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700166 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000167 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700168 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000169 }
Eddie James028f7eb2019-05-17 21:24:36 +0000170
Ed Tanous168e20c2021-12-13 14:39:53 -0800171 len = end - (lastPos + 1);
Eddie James028f7eb2019-05-17 21:24:36 +0000172 }
173
Ed Tanous168e20c2021-12-13 14:39:53 -0800174 std::string interfaceChassisName =
175 chassis.substr(lastPos + 1, len);
Ed Tanous55f79e62022-01-25 11:26:16 -0800176 if (interfaceChassisName == sensorAsyncResp->chassisId)
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,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800193 const dbus::utility::DBusPropertiesMap& properties) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700194 if (ec)
195 {
196 messages::internalError(
197 sensorAsyncResp->asyncResp->res);
198 BMCWEB_LOG_ERROR
199 << "Power Limit GetAll handler: Dbus error "
200 << ec;
201 return;
202 }
203
204 nlohmann::json& tempArray =
205 sensorAsyncResp->asyncResp->res
206 .jsonValue["PowerControl"];
207
208 // Put multiple "sensors" into a single PowerControl, 0,
209 // so only create the first one
210 if (tempArray.empty())
211 {
212 // Mandatory properties odata.id and MemberId
213 // A warning without a odata.type
214 tempArray.push_back(
215 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
216 {"@odata.id", "/redfish/v1/Chassis/" +
217 sensorAsyncResp->chassisId +
218 "/Power#/PowerControl/0"},
219 {"Name", "Chassis Power Control"},
220 {"MemberId", "0"}});
221 }
222
223 nlohmann::json& sensorJson = tempArray.back();
224 bool enabled = false;
225 double powerCap = 0.0;
226 int64_t scale = 0;
227
Ed Tanous168e20c2021-12-13 14:39:53 -0800228 for (const std::pair<std::string,
229 dbus::utility::DbusVariantType>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700230 property : properties)
231 {
Ed Tanous55f79e62022-01-25 11:26:16 -0800232 if (property.first == "Scale")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700233 {
234 const int64_t* i =
235 std::get_if<int64_t>(&property.second);
236
Ed Tanouse662eae2022-01-25 10:39:19 -0800237 if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700238 {
239 scale = *i;
240 }
241 }
Ed Tanous55f79e62022-01-25 11:26:16 -0800242 else if (property.first == "PowerCap")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700243 {
244 const double* d =
245 std::get_if<double>(&property.second);
246 const int64_t* i =
247 std::get_if<int64_t>(&property.second);
248 const uint32_t* u =
249 std::get_if<uint32_t>(&property.second);
250
Ed Tanouse662eae2022-01-25 10:39:19 -0800251 if (d != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700252 {
253 powerCap = *d;
254 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800255 else if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700256 {
257 powerCap = static_cast<double>(*i);
258 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800259 else if (u != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700260 {
261 powerCap = *u;
262 }
263 }
Ed Tanous55f79e62022-01-25 11:26:16 -0800264 else if (property.first == "PowerCapEnable")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700265 {
266 const bool* b =
267 std::get_if<bool>(&property.second);
268
Ed Tanouse662eae2022-01-25 10:39:19 -0800269 if (b != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700270 {
271 enabled = *b;
272 }
273 }
274 }
275
276 nlohmann::json& value =
277 sensorJson["PowerLimit"]["LimitInWatts"];
278
279 // LimitException is Mandatory attribute as per OCP
280 // Baseline Profile – v1.0.0, so currently making it
281 // "NoAction" as default value to make it OCP Compliant.
282 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
283
284 if (enabled)
285 {
286 // Redfish specification indicates PowerLimit should
287 // be null if the limit is not enabled.
288 value = powerCap * std::pow(10, scale);
289 }
290 };
291
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700292 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -0800293 std::move(valueHandler), "xyz.openbmc_project.Settings",
294 "/xyz/openbmc_project/control/host0/power_cap",
295 "org.freedesktop.DBus.Properties", "GetAll",
296 "xyz.openbmc_project.Control.Power.Cap");
297 };
298
299 crow::connections::systemBus->async_method_call(
300 std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
301 "/xyz/openbmc_project/object_mapper",
302 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
303 "/xyz/openbmc_project/inventory", 0,
304 std::array<const char*, 2>{
305 "xyz.openbmc_project.Inventory.Item.Board",
306 "xyz.openbmc_project.Inventory.Item.Chassis"});
307 });
Eddie James028f7eb2019-05-17 21:24:36 +0000308
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700309 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700310 .privileges(redfish::privileges::patchPower)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700311 .methods(boost::beast::http::verb::patch)(
312 [](const crow::Request& req,
313 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
314 const std::string& chassisName) {
315 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
316 asyncResp, chassisName,
317 sensors::dbus::paths.at(sensors::node::power),
318 sensors::node::power);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800319
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700320 std::optional<std::vector<nlohmann::json>> voltageCollections;
321 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
zhanghch058d1b46d2021-04-01 11:18:24 +0800322
Willy Tu15ed6782021-12-14 11:03:16 -0800323 if (!json_util::readJsonPatch(
324 req, sensorAsyncResp->asyncResp->res, "PowerControl",
325 powerCtlCollections, "Voltages", voltageCollections))
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700326 {
327 return;
328 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800329
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700330 if (powerCtlCollections)
331 {
332 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
333 }
334 if (voltageCollections)
335 {
336 std::unordered_map<std::string, std::vector<nlohmann::json>>
337 allCollections;
338 allCollections.emplace("Voltages",
339 *std::move(voltageCollections));
Bruce Lee80ac4022021-06-04 15:41:39 +0800340 setSensorsOverride(sensorAsyncResp, allCollections);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700341 }
342 });
343}
Ed Tanous2474adf2018-09-05 16:31:16 -0700344
345} // namespace redfish