blob: 7b2a47ac7a0d37e2243b8b5001ddb78dec142229 [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{
Ed Tanous002d39b2022-05-31 08:59:27 -070032 auto getChassisPath =
33 [sensorsAsyncResp, powerControlCollections](
34 const std::optional<std::string>& chassisPath) mutable {
George Liu0fda0f12021-11-16 10:06:17 +080035 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) {
Ed Tanous002d39b2022-05-31 08:59:27 -070079 if (ec)
80 {
81 messages::internalError(sensorsAsyncResp->asyncResp->res);
82 BMCWEB_LOG_ERROR << "powerCapEnable Get handler: Dbus error "
83 << 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 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -070094
Ed Tanous002d39b2022-05-31 08:59:27 -070095 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: " << ec2;
100 messages::internalError(sensorsAsyncResp->asyncResp->res);
101 return;
102 }
103 sensorsAsyncResp->asyncResp->res.result(
104 boost::beast::http::status::no_content);
105 },
106 "xyz.openbmc_project.Settings",
107 "/xyz/openbmc_project/control/host0/power_cap",
108 "org.freedesktop.DBus.Properties", "Set",
109 "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
110 std::variant<uint32_t>(*value));
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700111 });
George Liu0fda0f12021-11-16 10:06:17 +0800112 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700113 getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
114}
115inline void requestRoutesPower(App& app)
116{
Ed Tanous2474adf2018-09-05 16:31:16 -0700117
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700118 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700119 .privileges(redfish::privileges::getPower)
Ed Tanous002d39b2022-05-31 08:59:27 -0700120 .methods(boost::beast::http::verb::get)(
121 [&app](const crow::Request& req,
122 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
123 const std::string& chassisName) {
124 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
125 {
126 return;
127 }
128 asyncResp->res.jsonValue["PowerControl"] = nlohmann::json::array();
129
130 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
131 asyncResp, chassisName, sensors::dbus::powerPaths,
132 sensors::node::power);
133
134 getChassisData(sensorAsyncResp);
135
136 // This callback verifies that the power limit is only provided
137 // for the chassis that implements the Chassis inventory item.
138 // This prevents things like power supplies providing the
139 // chassis power limit
140
141 using Mapper = dbus::utility::MapperGetSubTreePathsResponse;
142 auto chassisHandler =
143 [sensorAsyncResp](const boost::system::error_code e,
144 const Mapper& chassisPaths) {
145 if (e)
Ed Tanous45ca1b82022-03-25 13:07:27 -0700146 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700147 BMCWEB_LOG_ERROR
148 << "Power Limit GetSubTreePaths handler Dbus error " << e;
Ed Tanous45ca1b82022-03-25 13:07:27 -0700149 return;
150 }
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800151
Ed Tanous002d39b2022-05-31 08:59:27 -0700152 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)
Ed Tanous168e20c2021-12-13 14:39:53 -0800158 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700159 continue;
Ed Tanous168e20c2021-12-13 14:39:53 -0800160 }
161
Ed Tanous002d39b2022-05-31 08:59:27 -0700162 if (lastPos == chassis.size() - 1)
Ed Tanous168e20c2021-12-13 14:39:53 -0800163 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700164 size_t end = lastPos;
165 lastPos = chassis.rfind('/', lastPos - 1);
Ed Tanous168e20c2021-12-13 14:39:53 -0800166 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000167 {
Ed Tanous168e20c2021-12-13 14:39:53 -0800168 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000169 }
170
Ed Tanous002d39b2022-05-31 08:59:27 -0700171 len = end - (lastPos + 1);
Ed Tanous168e20c2021-12-13 14:39:53 -0800172 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700173
Ed Tanous002d39b2022-05-31 08:59:27 -0700174 std::string interfaceChassisName =
175 chassis.substr(lastPos + 1, len);
176 if (interfaceChassisName == sensorAsyncResp->chassisId)
Ed Tanous168e20c2021-12-13 14:39:53 -0800177 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700178 found = true;
179 break;
180 }
181 }
182
183 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 dbus::utility::DBusPropertiesMap& properties) {
194 if (ec)
195 {
196 messages::internalError(sensorAsyncResp->asyncResp->res);
197 BMCWEB_LOG_ERROR
198 << "Power Limit GetAll handler: Dbus error " << ec;
Ed Tanous168e20c2021-12-13 14:39:53 -0800199 return;
200 }
201
Ed Tanous002d39b2022-05-31 08:59:27 -0700202 nlohmann::json& tempArray =
203 sensorAsyncResp->asyncResp->res.jsonValue["PowerControl"];
204
205 // Put multiple "sensors" into a single PowerControl, 0,
206 // so only create the first one
207 if (tempArray.empty())
208 {
209 // Mandatory properties odata.id and MemberId
210 // A warning without a odata.type
211 nlohmann::json::object_t powerControl;
212 powerControl["@odata.type"] = "#Power.v1_0_0.PowerControl";
213 powerControl["@odata.id"] = "/redfish/v1/Chassis/" +
214 sensorAsyncResp->chassisId +
215 "/Power#/PowerControl/0";
216 powerControl["Name"] = "Chassis Power Control";
217 powerControl["MemberId"] = "0";
218 tempArray.push_back(std::move(powerControl));
219 }
220
221 nlohmann::json& sensorJson = tempArray.back();
222 bool enabled = false;
223 double powerCap = 0.0;
224 int64_t scale = 0;
225
226 for (const std::pair<std::string,
227 dbus::utility::DbusVariantType>& property :
228 properties)
229 {
230 if (property.first == "Scale")
231 {
232 const int64_t* i =
233 std::get_if<int64_t>(&property.second);
234
235 if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700236 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700237 scale = *i;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700238 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700239 }
240 else if (property.first == "PowerCap")
241 {
242 const double* d = std::get_if<double>(&property.second);
243 const int64_t* i =
244 std::get_if<int64_t>(&property.second);
245 const uint32_t* u =
246 std::get_if<uint32_t>(&property.second);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700247
Ed Tanous002d39b2022-05-31 08:59:27 -0700248 if (d != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700249 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700250 powerCap = *d;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700251 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700252 else if (i != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700253 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700254 powerCap = static_cast<double>(*i);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700255 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700256 else if (u != nullptr)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700257 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700258 powerCap = *u;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700259 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700260 }
261 else if (property.first == "PowerCapEnable")
262 {
263 const bool* b = std::get_if<bool>(&property.second);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700264
Ed Tanous002d39b2022-05-31 08:59:27 -0700265 if (b != nullptr)
266 {
267 enabled = *b;
268 }
269 }
270 }
271
272 nlohmann::json& value =
273 sensorJson["PowerLimit"]["LimitInWatts"];
274
275 // LimitException is Mandatory attribute as per OCP
276 // Baseline Profile – v1.0.0, so currently making it
277 // "NoAction" as default value to make it OCP Compliant.
278 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
279
280 if (enabled)
281 {
282 // Redfish specification indicates PowerLimit should
283 // be null if the limit is not enabled.
284 value = powerCap * std::pow(10, scale);
285 }
Ed Tanous168e20c2021-12-13 14:39:53 -0800286 };
287
288 crow::connections::systemBus->async_method_call(
Ed Tanous002d39b2022-05-31 08:59:27 -0700289 std::move(valueHandler), "xyz.openbmc_project.Settings",
290 "/xyz/openbmc_project/control/host0/power_cap",
291 "org.freedesktop.DBus.Properties", "GetAll",
292 "xyz.openbmc_project.Control.Power.Cap");
293 };
294
295 crow::connections::systemBus->async_method_call(
296 std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
297 "/xyz/openbmc_project/object_mapper",
298 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
299 "/xyz/openbmc_project/inventory", 0,
300 std::array<const char*, 2>{
301 "xyz.openbmc_project.Inventory.Item.Board",
302 "xyz.openbmc_project.Inventory.Item.Chassis"});
Ed Tanous168e20c2021-12-13 14:39:53 -0800303 });
Eddie James028f7eb2019-05-17 21:24:36 +0000304
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700305 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700306 .privileges(redfish::privileges::patchPower)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700307 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -0700308 [&app](const crow::Request& req,
309 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
310 const std::string& chassisName) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700311 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
312 {
313 return;
314 }
315 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
316 asyncResp, chassisName, sensors::dbus::powerPaths,
317 sensors::node::power);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800318
Ed Tanous002d39b2022-05-31 08:59:27 -0700319 std::optional<std::vector<nlohmann::json>> voltageCollections;
320 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
zhanghch058d1b46d2021-04-01 11:18:24 +0800321
Ed Tanous002d39b2022-05-31 08:59:27 -0700322 if (!json_util::readJsonPatch(req, sensorAsyncResp->asyncResp->res,
323 "PowerControl", powerCtlCollections,
324 "Voltages", voltageCollections))
325 {
326 return;
327 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800328
Ed Tanous002d39b2022-05-31 08:59:27 -0700329 if (powerCtlCollections)
330 {
331 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
332 }
333 if (voltageCollections)
334 {
335 std::unordered_map<std::string, std::vector<nlohmann::json>>
336 allCollections;
337 allCollections.emplace("Voltages", *std::move(voltageCollections));
338 setSensorsOverride(sensorAsyncResp, allCollections);
339 }
340 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700341}
Ed Tanous2474adf2018-09-05 16:31:16 -0700342
343} // namespace redfish