blob: 8eacb9bb1764878325ba28e501870644a1568c30 [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 Tanous45ca1b82022-03-25 13:07:27 -070023#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
John Edward Broadbent7e860f12021-04-08 15:57:16 -070025
Ed Tanous2474adf2018-09-05 16:31:16 -070026namespace redfish
27{
Ed Tanous4f48d5f2021-06-21 08:27:45 -070028inline void setPowerCapOverride(
John Edward Broadbent7e860f12021-04-08 15:57:16 -070029 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
30 std::vector<nlohmann::json>& powerControlCollections)
Ed Tanous2474adf2018-09-05 16:31:16 -070031{
George Liu0fda0f12021-11-16 10:06:17 +080032 auto getChassisPath = [sensorsAsyncResp, powerControlCollections](
33 const std::optional<std::string>&
34 chassisPath) mutable {
35 if (!chassisPath)
36 {
37 BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
38 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
39 "Chassis", sensorsAsyncResp->chassisId);
40 return;
41 }
42
43 if (powerControlCollections.size() != 1)
44 {
45 BMCWEB_LOG_ERROR << "Don't support multiple hosts at present ";
46 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
47 "Power", "PowerControl");
48 return;
49 }
50
51 auto& item = powerControlCollections[0];
52
53 std::optional<nlohmann::json> powerLimit;
54 if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res,
55 "PowerLimit", powerLimit))
56 {
57 return;
58 }
59 if (!powerLimit)
60 {
61 return;
62 }
63 std::optional<uint32_t> value;
64 if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res,
65 "LimitInWatts", value))
66 {
67 return;
68 }
69 if (!value)
70 {
71 return;
72 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -070073 sdbusplus::asio::getProperty<bool>(
74 *crow::connections::systemBus, "xyz.openbmc_project.Settings",
George Liu0fda0f12021-11-16 10:06:17 +080075 "/xyz/openbmc_project/control/host0/power_cap",
Jonathan Doman1e1e5982021-06-11 09:36:17 -070076 "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable",
77 [value, sensorsAsyncResp](const boost::system::error_code ec,
78 bool powerCapEnable) {
79 if (ec)
80 {
81 messages::internalError(sensorsAsyncResp->asyncResp->res);
82 BMCWEB_LOG_ERROR
83 << "powerCapEnable Get handler: Dbus error " << ec;
84 return;
85 }
86 if (!powerCapEnable)
87 {
88 messages::actionNotSupported(
89 sensorsAsyncResp->asyncResp->res,
90 "Setting LimitInWatts when PowerLimit feature is disabled");
91 BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
92 return;
93 }
94
95 crow::connections::systemBus->async_method_call(
96 [sensorsAsyncResp](const boost::system::error_code ec2) {
97 if (ec2)
98 {
99 BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: "
100 << ec2;
101 messages::internalError(
102 sensorsAsyncResp->asyncResp->res);
103 return;
104 }
105 sensorsAsyncResp->asyncResp->res.result(
106 boost::beast::http::status::no_content);
107 },
108 "xyz.openbmc_project.Settings",
109 "/xyz/openbmc_project/control/host0/power_cap",
110 "org.freedesktop.DBus.Properties", "Set",
111 "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
112 std::variant<uint32_t>(*value));
113 });
George Liu0fda0f12021-11-16 10:06:17 +0800114 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700115 getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
116}
117inline void requestRoutesPower(App& app)
118{
Ed Tanous2474adf2018-09-05 16:31:16 -0700119
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700120 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700121 .privileges(redfish::privileges::getPower)
Ed Tanous168e20c2021-12-13 14:39:53 -0800122 .methods(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700123 boost::beast::http::verb::
124 get)([&app](const crow::Request& req,
125 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
126 const std::string& chassisName) {
127 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
128 {
129 return;
130 }
Ed Tanous168e20c2021-12-13 14:39:53 -0800131 asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800132
Ed Tanous168e20c2021-12-13 14:39:53 -0800133 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
134 asyncResp, chassisName,
135 sensors::dbus::paths.at(sensors::node::power),
136 sensors::node::power);
Eddie James028f7eb2019-05-17 21:24:36 +0000137
Ed Tanous168e20c2021-12-13 14:39:53 -0800138 getChassisData(sensorAsyncResp);
Eddie James028f7eb2019-05-17 21:24:36 +0000139
Ed Tanous168e20c2021-12-13 14:39:53 -0800140 // This callback verifies that the power limit is only provided
141 // for the chassis that implements the Chassis inventory item.
142 // This prevents things like power supplies providing the
143 // chassis power limit
Ed Tanousb9d36b42022-02-26 21:42:46 -0800144
145 using Mapper = dbus::utility::MapperGetSubTreePathsResponse;
Ed Tanous168e20c2021-12-13 14:39:53 -0800146 auto chassisHandler = [sensorAsyncResp](
147 const boost::system::error_code e,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800148 const Mapper& chassisPaths) {
Ed Tanous168e20c2021-12-13 14:39:53 -0800149 if (e)
150 {
151 BMCWEB_LOG_ERROR
152 << "Power Limit GetSubTreePaths handler Dbus error "
153 << e;
154 return;
155 }
156
157 bool found = false;
158 for (const std::string& chassis : chassisPaths)
159 {
160 size_t len = std::string::npos;
161 size_t lastPos = chassis.rfind('/');
162 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000163 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800164 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000165 }
166
Ed Tanous168e20c2021-12-13 14:39:53 -0800167 if (lastPos == chassis.size() - 1)
Eddie James028f7eb2019-05-17 21:24:36 +0000168 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800169 size_t end = lastPos;
170 lastPos = chassis.rfind('/', lastPos - 1);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700171 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000172 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700173 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000174 }
Eddie James028f7eb2019-05-17 21:24:36 +0000175
Ed Tanous168e20c2021-12-13 14:39:53 -0800176 len = end - (lastPos + 1);
Eddie James028f7eb2019-05-17 21:24:36 +0000177 }
178
Ed Tanous168e20c2021-12-13 14:39:53 -0800179 std::string interfaceChassisName =
180 chassis.substr(lastPos + 1, len);
Ed Tanous55f79e62022-01-25 11:26:16 -0800181 if (interfaceChassisName == sensorAsyncResp->chassisId)
Eddie James028f7eb2019-05-17 21:24:36 +0000182 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800183 found = true;
184 break;
Eddie James028f7eb2019-05-17 21:24:36 +0000185 }
Ed Tanous168e20c2021-12-13 14:39:53 -0800186 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700187
Ed Tanous168e20c2021-12-13 14:39:53 -0800188 if (!found)
189 {
190 BMCWEB_LOG_DEBUG << "Power Limit not present for "
191 << sensorAsyncResp->chassisId;
192 return;
193 }
194
195 auto valueHandler =
196 [sensorAsyncResp](
197 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800198 const dbus::utility::DBusPropertiesMap& properties) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700199 if (ec)
200 {
201 messages::internalError(
202 sensorAsyncResp->asyncResp->res);
203 BMCWEB_LOG_ERROR
204 << "Power Limit GetAll handler: Dbus error "
205 << ec;
206 return;
207 }
208
209 nlohmann::json& tempArray =
210 sensorAsyncResp->asyncResp->res
211 .jsonValue["PowerControl"];
212
213 // Put multiple "sensors" into a single PowerControl, 0,
214 // so only create the first one
215 if (tempArray.empty())
216 {
217 // Mandatory properties odata.id and MemberId
218 // A warning without a odata.type
219 tempArray.push_back(
220 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
221 {"@odata.id", "/redfish/v1/Chassis/" +
222 sensorAsyncResp->chassisId +
223 "/Power#/PowerControl/0"},
224 {"Name", "Chassis Power Control"},
225 {"MemberId", "0"}});
226 }
227
228 nlohmann::json& sensorJson = tempArray.back();
229 bool enabled = false;
230 double powerCap = 0.0;
231 int64_t scale = 0;
232
Ed Tanous168e20c2021-12-13 14:39:53 -0800233 for (const std::pair<std::string,
234 dbus::utility::DbusVariantType>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700235 property : properties)
236 {
Ed Tanous55f79e62022-01-25 11:26:16 -0800237 if (property.first == "Scale")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700238 {
239 const int64_t* i =
240 std::get_if<int64_t>(&property.second);
241
Ed Tanouse662eae2022-01-25 10:39:19 -0800242 if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700243 {
244 scale = *i;
245 }
246 }
Ed Tanous55f79e62022-01-25 11:26:16 -0800247 else if (property.first == "PowerCap")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700248 {
249 const double* d =
250 std::get_if<double>(&property.second);
251 const int64_t* i =
252 std::get_if<int64_t>(&property.second);
253 const uint32_t* u =
254 std::get_if<uint32_t>(&property.second);
255
Ed Tanouse662eae2022-01-25 10:39:19 -0800256 if (d != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700257 {
258 powerCap = *d;
259 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800260 else if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700261 {
262 powerCap = static_cast<double>(*i);
263 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800264 else if (u != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700265 {
266 powerCap = *u;
267 }
268 }
Ed Tanous55f79e62022-01-25 11:26:16 -0800269 else if (property.first == "PowerCapEnable")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700270 {
271 const bool* b =
272 std::get_if<bool>(&property.second);
273
Ed Tanouse662eae2022-01-25 10:39:19 -0800274 if (b != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700275 {
276 enabled = *b;
277 }
278 }
279 }
280
281 nlohmann::json& value =
282 sensorJson["PowerLimit"]["LimitInWatts"];
283
284 // LimitException is Mandatory attribute as per OCP
285 // Baseline Profile – v1.0.0, so currently making it
286 // "NoAction" as default value to make it OCP Compliant.
287 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
288
289 if (enabled)
290 {
291 // Redfish specification indicates PowerLimit should
292 // be null if the limit is not enabled.
293 value = powerCap * std::pow(10, scale);
294 }
295 };
296
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700297 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -0800298 std::move(valueHandler), "xyz.openbmc_project.Settings",
299 "/xyz/openbmc_project/control/host0/power_cap",
300 "org.freedesktop.DBus.Properties", "GetAll",
301 "xyz.openbmc_project.Control.Power.Cap");
302 };
303
304 crow::connections::systemBus->async_method_call(
305 std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
306 "/xyz/openbmc_project/object_mapper",
307 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
308 "/xyz/openbmc_project/inventory", 0,
309 std::array<const char*, 2>{
310 "xyz.openbmc_project.Inventory.Item.Board",
311 "xyz.openbmc_project.Inventory.Item.Chassis"});
312 });
Eddie James028f7eb2019-05-17 21:24:36 +0000313
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700314 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700315 .privileges(redfish::privileges::patchPower)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700316 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700317 [&app](const crow::Request& req,
318 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
319 const std::string& chassisName) {
320 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
321 {
322 return;
323 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700324 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
325 asyncResp, chassisName,
326 sensors::dbus::paths.at(sensors::node::power),
327 sensors::node::power);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800328
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700329 std::optional<std::vector<nlohmann::json>> voltageCollections;
330 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
zhanghch058d1b46d2021-04-01 11:18:24 +0800331
Willy Tu15ed6782021-12-14 11:03:16 -0800332 if (!json_util::readJsonPatch(
333 req, sensorAsyncResp->asyncResp->res, "PowerControl",
334 powerCtlCollections, "Voltages", voltageCollections))
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700335 {
336 return;
337 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800338
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700339 if (powerCtlCollections)
340 {
341 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
342 }
343 if (voltageCollections)
344 {
345 std::unordered_map<std::string, std::vector<nlohmann::json>>
346 allCollections;
347 allCollections.emplace("Voltages",
348 *std::move(voltageCollections));
Bruce Lee80ac4022021-06-04 15:41:39 +0800349 setSensorsOverride(sensorAsyncResp, allCollections);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700350 }
351 });
352}
Ed Tanous2474adf2018-09-05 16:31:16 -0700353
354} // namespace redfish