blob: d7fb7dd9be508487e51f4f8f92550c71ee15d430 [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 Tanoused398212021-06-09 17:05:54 -070022#include <registries/privilege_registry.hpp>
John Edward Broadbent7e860f12021-04-08 15:57:16 -070023
Ed Tanous2474adf2018-09-05 16:31:16 -070024namespace redfish
25{
Ed Tanous4f48d5f2021-06-21 08:27:45 -070026inline void setPowerCapOverride(
John Edward Broadbent7e860f12021-04-08 15:57:16 -070027 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
28 std::vector<nlohmann::json>& powerControlCollections)
Ed Tanous2474adf2018-09-05 16:31:16 -070029{
George Liu0fda0f12021-11-16 10:06:17 +080030 auto getChassisPath = [sensorsAsyncResp, powerControlCollections](
31 const std::optional<std::string>&
32 chassisPath) mutable {
33 if (!chassisPath)
34 {
35 BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
36 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
37 "Chassis", sensorsAsyncResp->chassisId);
38 return;
39 }
40
41 if (powerControlCollections.size() != 1)
42 {
43 BMCWEB_LOG_ERROR << "Don't support multiple hosts at present ";
44 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
45 "Power", "PowerControl");
46 return;
47 }
48
49 auto& item = powerControlCollections[0];
50
51 std::optional<nlohmann::json> powerLimit;
52 if (!json_util::readJson(item, sensorsAsyncResp->asyncResp->res,
53 "PowerLimit", powerLimit))
54 {
55 return;
56 }
57 if (!powerLimit)
58 {
59 return;
60 }
61 std::optional<uint32_t> value;
62 if (!json_util::readJson(*powerLimit, sensorsAsyncResp->asyncResp->res,
63 "LimitInWatts", value))
64 {
65 return;
66 }
67 if (!value)
68 {
69 return;
70 }
71 auto valueHandler = [value, sensorsAsyncResp](
72 const boost::system::error_code ec,
73 const SensorVariant& powerCapEnable) {
74 if (ec)
zhanghch058d1b46d2021-04-01 11:18:24 +080075 {
George Liu0fda0f12021-11-16 10:06:17 +080076 messages::internalError(sensorsAsyncResp->asyncResp->res);
77 BMCWEB_LOG_ERROR << "powerCapEnable Get handler: Dbus error "
78 << ec;
79 return;
80 }
81 // Check PowerCapEnable
82 const bool* b = std::get_if<bool>(&powerCapEnable);
83 if (b == nullptr)
84 {
85 messages::internalError(sensorsAsyncResp->asyncResp->res);
86 BMCWEB_LOG_ERROR << "Fail to get PowerCapEnable status ";
87 return;
88 }
89 if (!(*b))
90 {
91 messages::actionNotSupported(
92 sensorsAsyncResp->asyncResp->res,
93 "Setting LimitInWatts when PowerLimit feature is disabled");
94 BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
zhanghch058d1b46d2021-04-01 11:18:24 +080095 return;
96 }
Carol Wang4bb3dc32019-10-17 18:15:02 +080097
zhanghch058d1b46d2021-04-01 11:18:24 +080098 crow::connections::systemBus->async_method_call(
George Liu0fda0f12021-11-16 10:06:17 +080099 [sensorsAsyncResp](const boost::system::error_code ec2) {
100 if (ec2)
101 {
102 BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: "
103 << ec2;
104 messages::internalError(
105 sensorsAsyncResp->asyncResp->res);
106 return;
107 }
108 sensorsAsyncResp->asyncResp->res.result(
109 boost::beast::http::status::no_content);
110 },
111 "xyz.openbmc_project.Settings",
zhanghch058d1b46d2021-04-01 11:18:24 +0800112 "/xyz/openbmc_project/control/host0/power_cap",
George Liu0fda0f12021-11-16 10:06:17 +0800113 "org.freedesktop.DBus.Properties", "Set",
114 "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
115 std::variant<uint32_t>(*value));
zhanghch058d1b46d2021-04-01 11:18:24 +0800116 };
George Liu0fda0f12021-11-16 10:06:17 +0800117 crow::connections::systemBus->async_method_call(
118 std::move(valueHandler), "xyz.openbmc_project.Settings",
119 "/xyz/openbmc_project/control/host0/power_cap",
120 "org.freedesktop.DBus.Properties", "Get",
121 "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable");
122 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700123 getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
124}
125inline void requestRoutesPower(App& app)
126{
Ed Tanous2474adf2018-09-05 16:31:16 -0700127
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700128 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700129 .privileges(redfish::privileges::getPower)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700130 .methods(boost::beast::http::verb::get)(
131 [](const crow::Request&,
132 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
133 const std::string& chassisName) {
134 asyncResp->res.jsonValue["PowerControl"] =
135 nlohmann::json::array();
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800136
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700137 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
138 asyncResp, chassisName,
139 sensors::dbus::paths.at(sensors::node::power),
140 sensors::node::power);
Eddie James028f7eb2019-05-17 21:24:36 +0000141
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700142 getChassisData(sensorAsyncResp);
Eddie James028f7eb2019-05-17 21:24:36 +0000143
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700144 // This callback verifies that the power limit is only provided
145 // for the chassis that implements the Chassis inventory item.
146 // This prevents things like power supplies providing the
147 // chassis power limit
148 auto chassisHandler = [sensorAsyncResp](
149 const boost::system::error_code e,
150 const std::vector<std::string>&
151 chassisPaths) {
152 if (e)
Eddie James028f7eb2019-05-17 21:24:36 +0000153 {
Eddie James028f7eb2019-05-17 21:24:36 +0000154 BMCWEB_LOG_ERROR
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700155 << "Power Limit GetSubTreePaths handler Dbus error "
156 << e;
Eddie James028f7eb2019-05-17 21:24:36 +0000157 return;
158 }
159
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700160 bool found = false;
161 for (const std::string& chassis : chassisPaths)
Eddie James028f7eb2019-05-17 21:24:36 +0000162 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700163 size_t len = std::string::npos;
164 size_t lastPos = chassis.rfind('/');
165 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
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170 if (lastPos == chassis.size() - 1)
171 {
172 size_t end = lastPos;
173 lastPos = chassis.rfind('/', lastPos - 1);
174 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000175 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700176 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000177 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700178
179 len = end - (lastPos + 1);
Eddie James028f7eb2019-05-17 21:24:36 +0000180 }
Eddie James028f7eb2019-05-17 21:24:36 +0000181
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700182 std::string interfaceChassisName =
183 chassis.substr(lastPos + 1, len);
184 if (!interfaceChassisName.compare(
185 sensorAsyncResp->chassisId))
186 {
187 found = true;
188 break;
Eddie James028f7eb2019-05-17 21:24:36 +0000189 }
190 }
191
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700192 if (!found)
Eddie James028f7eb2019-05-17 21:24:36 +0000193 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700194 BMCWEB_LOG_DEBUG << "Power Limit not present for "
195 << sensorAsyncResp->chassisId;
196 return;
Eddie James028f7eb2019-05-17 21:24:36 +0000197 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700198
199 auto valueHandler = [sensorAsyncResp](
200 const boost::system::error_code ec,
201 const std::vector<std::pair<
202 std::string, SensorVariant>>&
203 properties) {
204 if (ec)
205 {
206 messages::internalError(
207 sensorAsyncResp->asyncResp->res);
208 BMCWEB_LOG_ERROR
209 << "Power Limit GetAll handler: Dbus error "
210 << ec;
211 return;
212 }
213
214 nlohmann::json& tempArray =
215 sensorAsyncResp->asyncResp->res
216 .jsonValue["PowerControl"];
217
218 // Put multiple "sensors" into a single PowerControl, 0,
219 // so only create the first one
220 if (tempArray.empty())
221 {
222 // Mandatory properties odata.id and MemberId
223 // A warning without a odata.type
224 tempArray.push_back(
225 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
226 {"@odata.id", "/redfish/v1/Chassis/" +
227 sensorAsyncResp->chassisId +
228 "/Power#/PowerControl/0"},
229 {"Name", "Chassis Power Control"},
230 {"MemberId", "0"}});
231 }
232
233 nlohmann::json& sensorJson = tempArray.back();
234 bool enabled = false;
235 double powerCap = 0.0;
236 int64_t scale = 0;
237
238 for (const std::pair<std::string, SensorVariant>&
239 property : properties)
240 {
241 if (!property.first.compare("Scale"))
242 {
243 const int64_t* i =
244 std::get_if<int64_t>(&property.second);
245
246 if (i)
247 {
248 scale = *i;
249 }
250 }
251 else if (!property.first.compare("PowerCap"))
252 {
253 const double* d =
254 std::get_if<double>(&property.second);
255 const int64_t* i =
256 std::get_if<int64_t>(&property.second);
257 const uint32_t* u =
258 std::get_if<uint32_t>(&property.second);
259
260 if (d)
261 {
262 powerCap = *d;
263 }
264 else if (i)
265 {
266 powerCap = static_cast<double>(*i);
267 }
268 else if (u)
269 {
270 powerCap = *u;
271 }
272 }
273 else if (!property.first.compare("PowerCapEnable"))
274 {
275 const bool* b =
276 std::get_if<bool>(&property.second);
277
278 if (b)
279 {
280 enabled = *b;
281 }
282 }
283 }
284
285 nlohmann::json& value =
286 sensorJson["PowerLimit"]["LimitInWatts"];
287
288 // LimitException is Mandatory attribute as per OCP
289 // Baseline Profile – v1.0.0, so currently making it
290 // "NoAction" as default value to make it OCP Compliant.
291 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
292
293 if (enabled)
294 {
295 // Redfish specification indicates PowerLimit should
296 // be null if the limit is not enabled.
297 value = powerCap * std::pow(10, scale);
298 }
299 };
300
301 crow::connections::systemBus->async_method_call(
302 std::move(valueHandler), "xyz.openbmc_project.Settings",
303 "/xyz/openbmc_project/control/host0/power_cap",
304 "org.freedesktop.DBus.Properties", "GetAll",
305 "xyz.openbmc_project.Control.Power.Cap");
Eddie James028f7eb2019-05-17 21:24:36 +0000306 };
307
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700308 crow::connections::systemBus->async_method_call(
309 std::move(chassisHandler),
310 "xyz.openbmc_project.ObjectMapper",
311 "/xyz/openbmc_project/object_mapper",
312 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
313 "/xyz/openbmc_project/inventory", 0,
314 std::array<const char*, 2>{
315 "xyz.openbmc_project.Inventory.Item.Board",
316 "xyz.openbmc_project.Inventory.Item.Chassis"});
317 });
Eddie James028f7eb2019-05-17 21:24:36 +0000318
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700319 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
Ed Tanoused398212021-06-09 17:05:54 -0700320 .privileges(redfish::privileges::patchPower)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700321 .methods(boost::beast::http::verb::patch)(
322 [](const crow::Request& req,
323 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
324 const std::string& chassisName) {
325 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
326 asyncResp, chassisName,
327 sensors::dbus::paths.at(sensors::node::power),
328 sensors::node::power);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800329
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700330 std::optional<std::vector<nlohmann::json>> voltageCollections;
331 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
zhanghch058d1b46d2021-04-01 11:18:24 +0800332
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700333 if (!json_util::readJson(req, sensorAsyncResp->asyncResp->res,
334 "PowerControl", powerCtlCollections,
335 "Voltages", voltageCollections))
336 {
337 return;
338 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800339
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700340 if (powerCtlCollections)
341 {
342 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
343 }
344 if (voltageCollections)
345 {
346 std::unordered_map<std::string, std::vector<nlohmann::json>>
347 allCollections;
348 allCollections.emplace("Voltages",
349 *std::move(voltageCollections));
Bruce Lee80ac4022021-06-04 15:41:39 +0800350 setSensorsOverride(sensorAsyncResp, allCollections);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700351 }
352 });
353}
Ed Tanous2474adf2018-09-05 16:31:16 -0700354
355} // namespace redfish