blob: eaf7e6cbefff154a8e456e243b6b72bb81d8618c [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 Tanous55f79e62022-01-25 11:26:16 -0800175 if (interfaceChassisName == sensorAsyncResp->chassisId)
Eddie James028f7eb2019-05-17 21:24:36 +0000176 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800177 found = true;
178 break;
Eddie James028f7eb2019-05-17 21:24:36 +0000179 }
Ed Tanous168e20c2021-12-13 14:39:53 -0800180 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700181
Ed Tanous168e20c2021-12-13 14:39:53 -0800182 if (!found)
183 {
184 BMCWEB_LOG_DEBUG << "Power Limit not present for "
185 << sensorAsyncResp->chassisId;
186 return;
187 }
188
189 auto valueHandler =
190 [sensorAsyncResp](
191 const boost::system::error_code ec,
192 const std::vector<std::pair<
193 std::string, dbus::utility::DbusVariantType>>&
194 properties) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700195 if (ec)
196 {
197 messages::internalError(
198 sensorAsyncResp->asyncResp->res);
199 BMCWEB_LOG_ERROR
200 << "Power Limit GetAll handler: Dbus error "
201 << ec;
202 return;
203 }
204
205 nlohmann::json& tempArray =
206 sensorAsyncResp->asyncResp->res
207 .jsonValue["PowerControl"];
208
209 // Put multiple "sensors" into a single PowerControl, 0,
210 // so only create the first one
211 if (tempArray.empty())
212 {
213 // Mandatory properties odata.id and MemberId
214 // A warning without a odata.type
215 tempArray.push_back(
216 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
217 {"@odata.id", "/redfish/v1/Chassis/" +
218 sensorAsyncResp->chassisId +
219 "/Power#/PowerControl/0"},
220 {"Name", "Chassis Power Control"},
221 {"MemberId", "0"}});
222 }
223
224 nlohmann::json& sensorJson = tempArray.back();
225 bool enabled = false;
226 double powerCap = 0.0;
227 int64_t scale = 0;
228
Ed Tanous168e20c2021-12-13 14:39:53 -0800229 for (const std::pair<std::string,
230 dbus::utility::DbusVariantType>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700231 property : properties)
232 {
Ed Tanous55f79e62022-01-25 11:26:16 -0800233 if (property.first == "Scale")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700234 {
235 const int64_t* i =
236 std::get_if<int64_t>(&property.second);
237
Ed Tanouse662eae2022-01-25 10:39:19 -0800238 if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700239 {
240 scale = *i;
241 }
242 }
Ed Tanous55f79e62022-01-25 11:26:16 -0800243 else if (property.first == "PowerCap")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700244 {
245 const double* d =
246 std::get_if<double>(&property.second);
247 const int64_t* i =
248 std::get_if<int64_t>(&property.second);
249 const uint32_t* u =
250 std::get_if<uint32_t>(&property.second);
251
Ed Tanouse662eae2022-01-25 10:39:19 -0800252 if (d != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700253 {
254 powerCap = *d;
255 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800256 else if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700257 {
258 powerCap = static_cast<double>(*i);
259 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800260 else if (u != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700261 {
262 powerCap = *u;
263 }
264 }
Ed Tanous55f79e62022-01-25 11:26:16 -0800265 else if (property.first == "PowerCapEnable")
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700266 {
267 const bool* b =
268 std::get_if<bool>(&property.second);
269
Ed Tanouse662eae2022-01-25 10:39:19 -0800270 if (b != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700271 {
272 enabled = *b;
273 }
274 }
275 }
276
277 nlohmann::json& value =
278 sensorJson["PowerLimit"]["LimitInWatts"];
279
280 // LimitException is Mandatory attribute as per OCP
281 // Baseline Profile – v1.0.0, so currently making it
282 // "NoAction" as default value to make it OCP Compliant.
283 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
284
285 if (enabled)
286 {
287 // Redfish specification indicates PowerLimit should
288 // be null if the limit is not enabled.
289 value = powerCap * std::pow(10, scale);
290 }
291 };
292
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700293 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -0800294 std::move(valueHandler), "xyz.openbmc_project.Settings",
295 "/xyz/openbmc_project/control/host0/power_cap",
296 "org.freedesktop.DBus.Properties", "GetAll",
297 "xyz.openbmc_project.Control.Power.Cap");
298 };
299
300 crow::connections::systemBus->async_method_call(
301 std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
302 "/xyz/openbmc_project/object_mapper",
303 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
304 "/xyz/openbmc_project/inventory", 0,
305 std::array<const char*, 2>{
306 "xyz.openbmc_project.Inventory.Item.Board",
307 "xyz.openbmc_project.Inventory.Item.Chassis"});
308 });
Eddie James028f7eb2019-05-17 21:24:36 +0000309
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700310 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700311 .privileges(redfish::privileges::patchPower)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700312 .methods(boost::beast::http::verb::patch)(
313 [](const crow::Request& req,
314 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
315 const std::string& chassisName) {
316 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
317 asyncResp, chassisName,
318 sensors::dbus::paths.at(sensors::node::power),
319 sensors::node::power);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800320
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700321 std::optional<std::vector<nlohmann::json>> voltageCollections;
322 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
zhanghch058d1b46d2021-04-01 11:18:24 +0800323
Willy Tu15ed6782021-12-14 11:03:16 -0800324 if (!json_util::readJsonPatch(
325 req, sensorAsyncResp->asyncResp->res, "PowerControl",
326 powerCtlCollections, "Voltages", voltageCollections))
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700327 {
328 return;
329 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800330
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700331 if (powerCtlCollections)
332 {
333 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
334 }
335 if (voltageCollections)
336 {
337 std::unordered_map<std::string, std::vector<nlohmann::json>>
338 allCollections;
339 allCollections.emplace("Voltages",
340 *std::move(voltageCollections));
Bruce Lee80ac4022021-06-04 15:41:39 +0800341 setSensorsOverride(sensorAsyncResp, allCollections);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700342 }
343 });
344}
Ed Tanous2474adf2018-09-05 16:31:16 -0700345
346} // namespace redfish