blob: 349c1f52d69fec2645ba4f22c93a9706ae3bdd29 [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>
22
Ed Tanous2474adf2018-09-05 16:31:16 -070023namespace redfish
24{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070025void setPowerCapOverride(
26 const std::shared_ptr<SensorsAsyncResp>& sensorsAsyncResp,
27 std::vector<nlohmann::json>& powerControlCollections)
Ed Tanous2474adf2018-09-05 16:31:16 -070028{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070029 auto getChassisPath =
30 [sensorsAsyncResp, powerControlCollections](
31 const std::optional<std::string>& chassisPath) mutable {
zhanghch058d1b46d2021-04-01 11:18:24 +080032 if (!chassisPath)
33 {
34 BMCWEB_LOG_ERROR << "Don't find valid chassis path ";
35 messages::resourceNotFound(sensorsAsyncResp->asyncResp->res,
36 "Chassis",
37 sensorsAsyncResp->chassisId);
38 return;
39 }
Carol Wang4bb3dc32019-10-17 18:15:02 +080040
zhanghch058d1b46d2021-04-01 11:18:24 +080041 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,
63 sensorsAsyncResp->asyncResp->res,
64 "LimitInWatts", value))
65 {
66 return;
67 }
68 if (!value)
69 {
70 return;
71 }
72 auto valueHandler = [value, sensorsAsyncResp](
73 const boost::system::error_code ec,
74 const SensorVariant& powerCapEnable) {
75 if (ec)
Carol Wang4bb3dc32019-10-17 18:15:02 +080076 {
zhanghch058d1b46d2021-04-01 11:18:24 +080077 messages::internalError(sensorsAsyncResp->asyncResp->res);
Carol Wang4bb3dc32019-10-17 18:15:02 +080078 BMCWEB_LOG_ERROR
zhanghch058d1b46d2021-04-01 11:18:24 +080079 << "powerCapEnable Get handler: Dbus error " << ec;
80 return;
81 }
82 // Check PowerCapEnable
83 const bool* b = std::get_if<bool>(&powerCapEnable);
84 if (b == nullptr)
85 {
86 messages::internalError(sensorsAsyncResp->asyncResp->res);
87 BMCWEB_LOG_ERROR << "Fail to get PowerCapEnable status ";
88 return;
89 }
90 if (!(*b))
91 {
92 messages::actionNotSupported(
93 sensorsAsyncResp->asyncResp->res,
94 "Setting LimitInWatts when PowerLimit "
95 "feature is disabled");
96 BMCWEB_LOG_ERROR << "PowerLimit feature is disabled ";
Carol Wang4bb3dc32019-10-17 18:15:02 +080097 return;
98 }
99
Carol Wang4bb3dc32019-10-17 18:15:02 +0800100 crow::connections::systemBus->async_method_call(
zhanghch058d1b46d2021-04-01 11:18:24 +0800101 [sensorsAsyncResp](const boost::system::error_code ec2) {
102 if (ec2)
103 {
104 BMCWEB_LOG_DEBUG << "Power Limit Set: Dbus error: "
105 << ec2;
106 messages::internalError(
107 sensorsAsyncResp->asyncResp->res);
108 return;
109 }
110 sensorsAsyncResp->asyncResp->res.result(
111 boost::beast::http::status::no_content);
112 },
113 "xyz.openbmc_project.Settings",
Carol Wang4bb3dc32019-10-17 18:15:02 +0800114 "/xyz/openbmc_project/control/host0/power_cap",
zhanghch058d1b46d2021-04-01 11:18:24 +0800115 "org.freedesktop.DBus.Properties", "Set",
116 "xyz.openbmc_project.Control.Power.Cap", "PowerCap",
117 std::variant<uint32_t>(*value));
Carol Wang4bb3dc32019-10-17 18:15:02 +0800118 };
zhanghch058d1b46d2021-04-01 11:18:24 +0800119 crow::connections::systemBus->async_method_call(
120 std::move(valueHandler), "xyz.openbmc_project.Settings",
121 "/xyz/openbmc_project/control/host0/power_cap",
122 "org.freedesktop.DBus.Properties", "Get",
123 "xyz.openbmc_project.Control.Power.Cap", "PowerCapEnable");
124 };
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700125 getValidChassisPath(sensorsAsyncResp, std::move(getChassisPath));
126}
127inline void requestRoutesPower(App& app)
128{
Ed Tanous2474adf2018-09-05 16:31:16 -0700129
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700130 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
131 .privileges({"Login"})
132 .methods(boost::beast::http::verb::get)(
133 [](const crow::Request&,
134 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
135 const std::string& chassisName) {
136 asyncResp->res.jsonValue["PowerControl"] =
137 nlohmann::json::array();
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800138
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700139 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
140 asyncResp, chassisName,
141 sensors::dbus::paths.at(sensors::node::power),
142 sensors::node::power);
Eddie James028f7eb2019-05-17 21:24:36 +0000143
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700144 getChassisData(sensorAsyncResp);
Eddie James028f7eb2019-05-17 21:24:36 +0000145
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700146 // This callback verifies that the power limit is only provided
147 // for the chassis that implements the Chassis inventory item.
148 // This prevents things like power supplies providing the
149 // chassis power limit
150 auto chassisHandler = [sensorAsyncResp](
151 const boost::system::error_code e,
152 const std::vector<std::string>&
153 chassisPaths) {
154 if (e)
Eddie James028f7eb2019-05-17 21:24:36 +0000155 {
Eddie James028f7eb2019-05-17 21:24:36 +0000156 BMCWEB_LOG_ERROR
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700157 << "Power Limit GetSubTreePaths handler Dbus error "
158 << e;
Eddie James028f7eb2019-05-17 21:24:36 +0000159 return;
160 }
161
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700162 bool found = false;
163 for (const std::string& chassis : chassisPaths)
Eddie James028f7eb2019-05-17 21:24:36 +0000164 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700165 size_t len = std::string::npos;
166 size_t lastPos = chassis.rfind('/');
167 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000168 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700169 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000170 }
Eddie James028f7eb2019-05-17 21:24:36 +0000171
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700172 if (lastPos == chassis.size() - 1)
173 {
174 size_t end = lastPos;
175 lastPos = chassis.rfind('/', lastPos - 1);
176 if (lastPos == std::string::npos)
Eddie James028f7eb2019-05-17 21:24:36 +0000177 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700178 continue;
Eddie James028f7eb2019-05-17 21:24:36 +0000179 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700180
181 len = end - (lastPos + 1);
Eddie James028f7eb2019-05-17 21:24:36 +0000182 }
Eddie James028f7eb2019-05-17 21:24:36 +0000183
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700184 std::string interfaceChassisName =
185 chassis.substr(lastPos + 1, len);
186 if (!interfaceChassisName.compare(
187 sensorAsyncResp->chassisId))
188 {
189 found = true;
190 break;
Eddie James028f7eb2019-05-17 21:24:36 +0000191 }
192 }
193
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700194 if (!found)
Eddie James028f7eb2019-05-17 21:24:36 +0000195 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700196 BMCWEB_LOG_DEBUG << "Power Limit not present for "
197 << sensorAsyncResp->chassisId;
198 return;
Eddie James028f7eb2019-05-17 21:24:36 +0000199 }
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700200
201 auto valueHandler = [sensorAsyncResp](
202 const boost::system::error_code ec,
203 const std::vector<std::pair<
204 std::string, SensorVariant>>&
205 properties) {
206 if (ec)
207 {
208 messages::internalError(
209 sensorAsyncResp->asyncResp->res);
210 BMCWEB_LOG_ERROR
211 << "Power Limit GetAll handler: Dbus error "
212 << ec;
213 return;
214 }
215
216 nlohmann::json& tempArray =
217 sensorAsyncResp->asyncResp->res
218 .jsonValue["PowerControl"];
219
220 // Put multiple "sensors" into a single PowerControl, 0,
221 // so only create the first one
222 if (tempArray.empty())
223 {
224 // Mandatory properties odata.id and MemberId
225 // A warning without a odata.type
226 tempArray.push_back(
227 {{"@odata.type", "#Power.v1_0_0.PowerControl"},
228 {"@odata.id", "/redfish/v1/Chassis/" +
229 sensorAsyncResp->chassisId +
230 "/Power#/PowerControl/0"},
231 {"Name", "Chassis Power Control"},
232 {"MemberId", "0"}});
233 }
234
235 nlohmann::json& sensorJson = tempArray.back();
236 bool enabled = false;
237 double powerCap = 0.0;
238 int64_t scale = 0;
239
240 for (const std::pair<std::string, SensorVariant>&
241 property : properties)
242 {
243 if (!property.first.compare("Scale"))
244 {
245 const int64_t* i =
246 std::get_if<int64_t>(&property.second);
247
248 if (i)
249 {
250 scale = *i;
251 }
252 }
253 else if (!property.first.compare("PowerCap"))
254 {
255 const double* d =
256 std::get_if<double>(&property.second);
257 const int64_t* i =
258 std::get_if<int64_t>(&property.second);
259 const uint32_t* u =
260 std::get_if<uint32_t>(&property.second);
261
262 if (d)
263 {
264 powerCap = *d;
265 }
266 else if (i)
267 {
268 powerCap = static_cast<double>(*i);
269 }
270 else if (u)
271 {
272 powerCap = *u;
273 }
274 }
275 else if (!property.first.compare("PowerCapEnable"))
276 {
277 const bool* b =
278 std::get_if<bool>(&property.second);
279
280 if (b)
281 {
282 enabled = *b;
283 }
284 }
285 }
286
287 nlohmann::json& value =
288 sensorJson["PowerLimit"]["LimitInWatts"];
289
290 // LimitException is Mandatory attribute as per OCP
291 // Baseline Profile – v1.0.0, so currently making it
292 // "NoAction" as default value to make it OCP Compliant.
293 sensorJson["PowerLimit"]["LimitException"] = "NoAction";
294
295 if (enabled)
296 {
297 // Redfish specification indicates PowerLimit should
298 // be null if the limit is not enabled.
299 value = powerCap * std::pow(10, scale);
300 }
301 };
302
303 crow::connections::systemBus->async_method_call(
304 std::move(valueHandler), "xyz.openbmc_project.Settings",
305 "/xyz/openbmc_project/control/host0/power_cap",
306 "org.freedesktop.DBus.Properties", "GetAll",
307 "xyz.openbmc_project.Control.Power.Cap");
Eddie James028f7eb2019-05-17 21:24:36 +0000308 };
309
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700310 crow::connections::systemBus->async_method_call(
311 std::move(chassisHandler),
312 "xyz.openbmc_project.ObjectMapper",
313 "/xyz/openbmc_project/object_mapper",
314 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
315 "/xyz/openbmc_project/inventory", 0,
316 std::array<const char*, 2>{
317 "xyz.openbmc_project.Inventory.Item.Board",
318 "xyz.openbmc_project.Inventory.Item.Chassis"});
319 });
Eddie James028f7eb2019-05-17 21:24:36 +0000320
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700321 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Power/")
322 .privileges({"ConfigureManager"})
323 .methods(boost::beast::http::verb::patch)(
324 [](const crow::Request& req,
325 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
326 const std::string& chassisName) {
327 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
328 asyncResp, chassisName,
329 sensors::dbus::paths.at(sensors::node::power),
330 sensors::node::power);
Carol Wang4bb3dc32019-10-17 18:15:02 +0800331
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700332 std::optional<std::vector<nlohmann::json>> voltageCollections;
333 std::optional<std::vector<nlohmann::json>> powerCtlCollections;
zhanghch058d1b46d2021-04-01 11:18:24 +0800334
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700335 if (!json_util::readJson(req, sensorAsyncResp->asyncResp->res,
336 "PowerControl", powerCtlCollections,
337 "Voltages", voltageCollections))
338 {
339 return;
340 }
Carol Wang4bb3dc32019-10-17 18:15:02 +0800341
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700342 if (powerCtlCollections)
343 {
344 setPowerCapOverride(sensorAsyncResp, *powerCtlCollections);
345 }
346 if (voltageCollections)
347 {
348 std::unordered_map<std::string, std::vector<nlohmann::json>>
349 allCollections;
350 allCollections.emplace("Voltages",
351 *std::move(voltageCollections));
Bruce Lee80ac4022021-06-04 15:41:39 +0800352 setSensorsOverride(sensorAsyncResp, allCollections);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700353 }
354 });
355}
Ed Tanous2474adf2018-09-05 16:31:16 -0700356
357} // namespace redfish