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