blob: e6f1ebaffc867e4832f0fafc79544b415949261d [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
19#include "node.hpp"
20#include "sensors.hpp"
21
22namespace redfish
23{
24
25class Power : public Node
26{
27 public:
28 Power(CrowApp& app) :
29 Node((app), "/redfish/v1/Chassis/<str>/Power/", std::string())
30 {
Ed Tanous2474adf2018-09-05 16:31:16 -070031 entityPrivileges = {
32 {boost::beast::http::verb::get, {{"Login"}}},
33 {boost::beast::http::verb::head, {{"Login"}}},
Richard Marian Thomaiyar6f4fd472019-02-13 10:10:59 +053034 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
Ed Tanous2474adf2018-09-05 16:31:16 -070035 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
36 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
37 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
38 }
39
40 private:
Ed Tanous85e14242019-06-27 15:04:09 -070041 std::vector<const char*> typeList = {"/xyz/openbmc_project/sensors/voltage",
42 "/xyz/openbmc_project/sensors/power"};
Ed Tanous2474adf2018-09-05 16:31:16 -070043 void doGet(crow::Response& res, const crow::Request& req,
44 const std::vector<std::string>& params) override
45 {
46 if (params.size() != 1)
47 {
48 res.result(boost::beast::http::status::internal_server_error);
49 res.end();
50 return;
51 }
52 const std::string& chassis_name = params[0];
53
Jennifer Leec5d03ff2019-03-08 15:42:58 -080054 res.jsonValue["PowerControl"] = nlohmann::json::array();
55
Ed Tanous2474adf2018-09-05 16:31:16 -070056 auto sensorAsyncResp = std::make_shared<SensorsAsyncResp>(
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +053057 res, chassis_name, typeList, "Power");
Eddie James028f7eb2019-05-17 21:24:36 +000058
Ed Tanous2474adf2018-09-05 16:31:16 -070059 getChassisData(sensorAsyncResp);
Eddie James028f7eb2019-05-17 21:24:36 +000060
61 // This callback verifies that the power limit is only provided for the
62 // chassis that implements the Chassis inventory item. This prevents
63 // things like power supplies providing the chassis power limit
64 auto chassisHandler = [sensorAsyncResp](
65 const boost::system::error_code ec,
66 const std::vector<std::string>&
67 chassisPaths) {
68 if (ec)
69 {
70 BMCWEB_LOG_ERROR
71 << "Power Limit GetSubTreePaths handler Dbus error " << ec;
72 return;
73 }
74
75 bool found = false;
76 for (const std::string& chassis : chassisPaths)
77 {
78 size_t len = std::string::npos;
79 size_t lastPos = chassis.rfind("/");
80 if (lastPos == std::string::npos)
81 {
82 continue;
83 }
84
85 if (lastPos == chassis.size() - 1)
86 {
87 size_t end = lastPos;
88 lastPos = chassis.rfind("/", lastPos - 1);
89 if (lastPos == std::string::npos)
90 {
91 continue;
92 }
93
94 len = end - (lastPos + 1);
95 }
96
97 std::string interfaceChassisName =
98 chassis.substr(lastPos + 1, len);
99 if (!interfaceChassisName.compare(sensorAsyncResp->chassisId))
100 {
101 found = true;
102 break;
103 }
104 }
105
106 if (!found)
107 {
108 BMCWEB_LOG_DEBUG << "Power Limit not present for "
109 << sensorAsyncResp->chassisId;
110 return;
111 }
112
113 auto valueHandler =
114 [sensorAsyncResp](
115 const boost::system::error_code ec,
116 const std::vector<std::pair<std::string, SensorVariant>>&
117 properties) {
118 if (ec)
119 {
120 messages::internalError(sensorAsyncResp->res);
121 BMCWEB_LOG_ERROR
122 << "Power Limit GetAll handler: Dbus error " << ec;
123 return;
124 }
125
126 nlohmann::json& tempArray =
127 sensorAsyncResp->res.jsonValue["PowerLimit"];
128
129 if (tempArray.empty())
130 {
131 tempArray.push_back({});
132 }
133
134 nlohmann::json& sensorJson = tempArray.back();
135 bool enabled = false;
136 double powerCap = 0.0;
137 int64_t scale = 0;
138
139 for (const std::pair<std::string, SensorVariant>& property :
140 properties)
141 {
142 if (!property.first.compare("Scale"))
143 {
144 const int64_t* i =
145 sdbusplus::message::variant_ns::get_if<int64_t>(
146 &property.second);
147
148 if (i)
149 {
150 scale = *i;
151 }
152 }
153 else if (!property.first.compare("PowerCap"))
154 {
155 const double* d =
156 sdbusplus::message::variant_ns::get_if<double>(
157 &property.second);
158 const int64_t* i =
159 sdbusplus::message::variant_ns::get_if<int64_t>(
160 &property.second);
161 const uint32_t* u =
162 sdbusplus::message::variant_ns::get_if<
163 uint32_t>(&property.second);
164
165 if (d)
166 {
167 powerCap = *d;
168 }
169 else if (i)
170 {
171 powerCap = *i;
172 }
173 else if (u)
174 {
175 powerCap = *u;
176 }
177 }
178 else if (!property.first.compare("PowerCapEnable"))
179 {
180 const bool* b =
181 sdbusplus::message::variant_ns::get_if<bool>(
182 &property.second);
183
184 if (b)
185 {
186 enabled = *b;
187 }
188 }
189 }
190
191 nlohmann::json& value = sensorJson["LimitInWatts"];
192
193 if (enabled)
194 {
195 // Redfish specification indicates PowerLimit should be
196 // null if the limit is not enabled.
197 value = powerCap * std::pow(10, scale);
198 }
199 };
200
201 crow::connections::systemBus->async_method_call(
202 std::move(valueHandler), "xyz.openbmc_project.Settings",
203 "/xyz/openbmc_project/control/host0/power_cap",
204 "org.freedesktop.DBus.Properties", "GetAll",
205 "xyz.openbmc_project.Control.Power.Cap");
206 };
207
208 crow::connections::systemBus->async_method_call(
209 std::move(chassisHandler), "xyz.openbmc_project.ObjectMapper",
210 "/xyz/openbmc_project/object_mapper",
211 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
212 "/xyz/openbmc_project/inventory", int32_t(0),
213 std::array<const char*, 1>{
214 "xyz.openbmc_project.Inventory.Item.Chassis"});
Ed Tanous2474adf2018-09-05 16:31:16 -0700215 }
Richard Marian Thomaiyar413961d2019-02-01 00:43:39 +0530216 void doPatch(crow::Response& res, const crow::Request& req,
217 const std::vector<std::string>& params) override
218 {
219 setSensorOverride(res, req, params, typeList, "Power");
220 }
Ed Tanous2474adf2018-09-05 16:31:16 -0700221};
222
223} // namespace redfish